seancorfield / next-jdbc

A modern low-level Clojure wrapper for JDBC-based access to databases.
https://cljdoc.org/d/com.github.seancorfield/next.jdbc/
Eclipse Public License 1.0
767 stars 90 forks source link

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

Closed seancorfield closed 6 months ago

seancorfield commented 8 months ago

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.

igrishaev commented 8 months ago

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