Permalink
Browse files

feat: do not use pg_depend against PostgreSQL 9.0+ (#689)

Previously, `getImportedExportedKeys` needed to use `pg_depend` to
determine the index that supported a foreign key constraint. This was
done by joining `pg_depend` with `pg_constraint` to find the dependency
between the foreign key constraint and a row in `pg_class` with type
index.

Starting in version 9.0 of postgres, `pg_constraint` was extended with
the `conindid` column. This column gives us exactly what we want: for
foreign key constraint rows, `conindid` is the `oid` of the index that
supports the constraint.

This commit uses `pg_constraint.conindid` when the server version is new
enough instead of joining against `pg_depend`.
  • Loading branch information...
jordanlewis authored and vlsi committed Nov 15, 2016
1 parent 8a95d99 commit 62e25fba70002d7639472c5a1dcd9d1de5b7f872
Showing with 16 additions and 3 deletions.
  1. +16 −3 pgjdbc/src/main/java/org/postgresql/jdbc/PgDatabaseMetaData.java
@@ -2062,10 +2062,23 @@ protected ResultSet getImportedExportedKeys(String primaryCatalog, String primar
+ " pg_catalog.pg_namespace fkn, pg_catalog.pg_class fkc, pg_catalog.pg_attribute fka, "
+ " pg_catalog.pg_constraint con, "
+ " pg_catalog.generate_series(1, " + getMaxIndexKeys() + ") pos(n), "
+ " pg_catalog.pg_depend dep, pg_catalog.pg_class pkic "
+ " WHERE pkn.oid = pkc.relnamespace AND pkc.oid = pka.attrelid AND pka.attnum = con.confkey[pos.n] AND con.confrelid = pkc.oid "
+ " pg_catalog.pg_class pkic";
// Starting in Postgres 9.0, pg_constraint was augmented with the conindid column, which
// contains the oid of the index supporting the constraint. This makes it unnecessary to do a
// further join on pg_depend.
if (!connection.haveMinimumServerVersion(ServerVersion.v9_0)) {
sql += ", pg_catalog.pg_depend dep ";
}
sql +=
" WHERE pkn.oid = pkc.relnamespace AND pkc.oid = pka.attrelid AND pka.attnum = con.confkey[pos.n] AND con.confrelid = pkc.oid "
+ " AND fkn.oid = fkc.relnamespace AND fkc.oid = fka.attrelid AND fka.attnum = con.conkey[pos.n] AND con.conrelid = fkc.oid "
+ " AND con.contype = 'f' AND con.oid = dep.objid AND pkic.oid = dep.refobjid AND pkic.relkind = 'i' AND dep.classid = 'pg_constraint'::regclass::oid AND dep.refclassid = 'pg_class'::regclass::oid ";
+ " AND con.contype = 'f' AND pkic.relkind = 'i' ";
if (!connection.haveMinimumServerVersion(ServerVersion.v9_0)) {
sql += " AND con.oid = dep.objid AND pkic.oid = dep.refobjid AND dep.classid = 'pg_constraint'::regclass::oid AND dep.refclassid = 'pg_class'::regclass::oid ";
} else {
sql += " AND pkic.oid = con.conindid ";
}

if (primarySchema != null && !primarySchema.isEmpty()) {
sql += " AND pkn.nspname = " + escapeQuotes(primarySchema);
}

0 comments on commit 62e25fb

Please sign in to comment.