Skip to content

Commit

Permalink
fix: Partitioned indexes were not found fixes (pgjdbc#2078)
Browse files Browse the repository at this point in the history
  • Loading branch information
davecramer committed Mar 3, 2021
1 parent 6cfa657 commit a9d569a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
19 changes: 18 additions & 1 deletion pgjdbc/src/main/java/org/postgresql/jdbc/PgDatabaseMetaData.java
Original file line number Diff line number Diff line change
Expand Up @@ -1300,6 +1300,7 @@ public ResultSet getTables(@Nullable String catalog, @Nullable String schemaPatt
+ " WHEN 'r' THEN 'TABLE' "
+ " WHEN 'p' THEN 'PARTITIONED TABLE' "
+ " WHEN 'i' THEN 'INDEX' "
+ " WHEN 'P' then 'PARTITIONED INDEX' "
+ " WHEN 'S' THEN 'SEQUENCE' "
+ " WHEN 'v' THEN 'VIEW' "
+ " WHEN 'c' THEN 'TYPE' "
Expand Down Expand Up @@ -1370,6 +1371,10 @@ public ResultSet getTables(@Nullable String catalog, @Nullable String schemaPatt
"c.relkind = 'i' AND n.nspname !~ '^pg_' AND n.nspname <> 'information_schema'");
ht.put("NOSCHEMAS", "c.relkind = 'i' AND c.relname !~ '^pg_'");
ht = new HashMap<String, String>();
tableTypeClauses.put("PARTITIONED INDEX", ht);
ht.put("SCHEMAS", "c.relkind = 'I' AND n.nspname !~ '^pg_' AND n.nspname <> 'information_schema'");
ht.put("NOSCHEMAS", "c.relkind = 'I' AND c.relname !~ '^pg_'");
ht = new HashMap<String, String>();
tableTypeClauses.put("SEQUENCE", ht);
ht.put("SCHEMAS", "c.relkind = 'S'");
ht.put("NOSCHEMAS", "c.relkind = 'S'");
Expand Down Expand Up @@ -2233,7 +2238,19 @@ protected ResultSet getImportedExportedKeys(
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 pkic.relkind = 'i' ";
+ " AND con.contype = 'f' ";
/*
In version 11 we added Partitioned indexes indicated by relkind = 'I'
I could have done this using lower(relkind) = 'i' but chose to be explicit
for clarity
*/

if (!connection.haveMinimumServerVersion(ServerVersion.v11)) {
sql += "AND pkic.relkind = 'i' ";
} else {
sql += "AND (pkic.relkind = 'i' OR 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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1336,11 +1336,15 @@ public void testPartitionedTables() throws SQLException {
try {
stmt = con.createStatement();
stmt.execute(
"CREATE TABLE measurement (logdate date not null,peaktemp int,unitsales int ) PARTITION BY RANGE (logdate);");
"CREATE TABLE measurement (logdate date not null primary key,peaktemp int,unitsales int ) PARTITION BY RANGE (logdate);");
DatabaseMetaData dbmd = con.getMetaData();
ResultSet rs = dbmd.getTables("", "", "measurement", new String[]{"PARTITIONED TABLE"});
assertTrue(rs.next());
assertEquals("measurement", rs.getString("table_name"));
rs.close();
rs = dbmd.getPrimaryKeys("", "", "measurement");
assertTrue(rs.next());
assertEquals("measurement_pkey", rs.getString(8));

} finally {
if (stmt != null) {
Expand Down

0 comments on commit a9d569a

Please sign in to comment.