Skip to content

Exclude views from get_table_names() - #310

Merged
gordthompson merged 4 commits into
cockroachdb:masterfrom
aminghadersohi:get-table-names-exclude-views
Sep 30, 2026
Merged

gordthompson merged 4 commits into
cockroachdb:masterfrom
aminghadersohi:get-table-names-exclude-views

Conversation

@aminghadersohi

@aminghadersohi aminghadersohi commented Sep 26, 2026 •

Copy link
Copy Markdown
Contributor

get_table_names() selected every row of information_schema.tables for the schema, so views were returned as tables in addition to being returned by get_view_names(). SQLAlchemy defines get_table_names() as base tables only; applications that list tables and views separately show each view twice.

Following review, the dialect now extends the upstream PostgreSQL reflection instead of replacing it (74df703, from @gordthompson):

  • get_table_names() calls the upstream implementation (base tables only) and drops the multi_entries_to_ignore entries when no schema is requested. The pre-v2 SHOW TABLES branch and the has_table() override are removed, so has_table() is upstream's and still returns True for views, as SQLAlchemy 2.0 specifies.
  • get_multi_foreign_keys() drops the multi_entries_to_ignore entries from the upstream result when no schema is requested.
  • The schemas test requirement is open, so the suite's schema-qualified tests run. ComputedReflectionTest and the schema variant of test_metadata are skipped, and the dev requirements are refreshed.

664ebf6 passes the requested schema and the caller's postgresql_ignore_search_path through to the upstream calls. Previously they were hard-coded to None/False, so get_table_names(schema="test_schema") listed the default schema's tables. A CHANGES.md entry is under the unreleased 2.0.5 section.

Tests (test/test_introspection.py::TableNamesTest): test_get_table_names_excludes_views, test_has_table_includes_views, test_get_table_names_uses_requested_schema and test_get_foreign_keys_passes_ignore_search_path. The last two fail on 74df703 and pass on 664ebf6.

Full suite at 664ebf6 against CockroachDB latest-v26.3 (single node, test_schema/test_schema_2 created as in CI), SQLAlchemy 2.0.54, Python 3.10:

  • psycopg2: 929 passed, 508 skipped
  • asyncpg: 925 passed, 542 skipped
  • psycopg: 929 passed, 508 skipped

For comparison, master with psycopg2 runs 731 passed, 705 skipped (the schema tests are not enabled there). flake8 (tox -e lint settings) is clean.

get_table_names() read every row of information_schema.tables for the
schema, so views were returned as tables as well as by get_view_names().
SQLAlchemy defines get_table_names() as base tables only, and callers
that list tables and views separately showed each view twice.

Filter on table_type = 'BASE TABLE'. has_table() previously relied on
get_table_names(); it now checks all relations directly, so it keeps
returning True for views as SQLAlchemy 2.0 specifies.
@gordthompson gordthompson self-assigned this Sep 27, 2026

@gordthompson gordthompson left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can now extend SQLAlchemy's upstream code. Please review and merge

aminghadersohi#1

get_table_names() called the upstream implementation with schema=None,
so get_table_names(schema=...) listed the default schema's tables. Pass
the requested schema through, and only drop the default-schema entries
in multi_entries_to_ignore when no schema was requested, as
get_multi_foreign_keys() does. get_multi_foreign_keys() likewise now
forwards the caller's postgresql_ignore_search_path instead of False.
@aminghadersohi

Copy link
Copy Markdown
Contributor Author

@gordthompson Thanks, that's much cleaner. I've brought your commit (74df703) into this branch unchanged. Running the suite against it turned up one problem: get_table_names() called super().get_table_names(connection, schema=None, **kw), so get_table_names(schema="test_schema") returned the public tables. The same pattern hard-coded postgresql_ignore_search_path=False in get_multi_foreign_keys(). 664ebf6 passes both through, and only drops the multi_entries_to_ignore names when no schema is requested, as get_multi_foreign_keys() already does. It also removes the except Exception: raise. Two tests in TableNamesTest cover this; they fail on 74df703 and pass now.

Full suite at 664ebf6 on CockroachDB v26.3 (psycopg2 / asyncpg / psycopg): 929 / 925 / 929 passed, no failures. The description is updated to match.

@gordthompson gordthompson left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm. Thanks!

@gordthompson
gordthompson merged commit db4b387 into cockroachdb:master Sep 30, 2026
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants