Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add note to docs about PG performing extra queries to get table names #275

Closed
seancorfield opened this issue Feb 7, 2024 · 1 comment
Closed
Assignees
Labels
documentation The documentation is wrong or needs improving

Comments

@seancorfield
Copy link
Owner

Just like some DBs/drivers don't support table names in all circumstances, which affects qualified column names, PostgreSQL actually runs an additional query under the hood to get table names for columns used in a query, if you call .getTableName() (other ResultSetMetaData is directly available, such as column types, however).

MySQL provides table names directly (at least based on the protocol implementation in the source code in the 8.x driver) in the metadata and doesn't require additional queries.

It is unknown what other DBs/drivers do -- but MS SQL Server only provides useful .getTableName() results when queries are executing with specific settings, and Oracle doesn't provide it at all.

@seancorfield seancorfield added the documentation The documentation is wrong or needs improving label Feb 7, 2024
@seancorfield seancorfield self-assigned this Feb 7, 2024
@igrishaev
Copy link

Let me put it here:

SELECT c.oid, a.attnum, a.attname, c.relname, n.nspname,
a.attnotnull OR (t.typtype = 'd' AND t.typnotnull),
pg_catalog.pg_get_expr(d.adbin, d.adrelid) LIKE '%nextval(%' FROM
pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n
ON (c.relnamespace = n.oid) JOIN pg_catalog.pg_attribute a
ON (c.oid = a.attrelid) JOIN pg_catalog.pg_type t ON (a.atttypid
= t.oid) LEFT JOIN pg_catalog.pg_attrdef d ON (d.adrelid =
a.attrelid AND d.adnum = a.attnum) JOIN (SELECT 32824 AS oid , 1
AS attnum UNION ALL SELECT 32824, 2 UNION ALL SELECT 32824, 3
UNION ALL SELECT 32824, 4 UNION ALL SELECT 32824, 5 UNION ALL
SELECT 32824, 6 UNION ALL SELECT 32824, 8 UNION ALL SELECT 32824,
9 UNION ALL SELECT 32835, 1 UNION ALL SELECT 32835, 2 UNION ALL
SELECT 32835, 3) vals ON (c.oid = vals.oid AND a.attnum =
vals.attnum)

spotted by running Postgres with -E flag, e.g:

  pg14:
    image: postgres:14
    command: -E

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation The documentation is wrong or needs improving
Projects
None yet
Development

No branches or pull requests

2 participants