Skip to content

Commit

Permalink
fix: Partitioned indexes were not found fixes (#2078) (#2087) (#2091)
Browse files Browse the repository at this point in the history
* fix: Partitioned indexes were not found fixes (#2078) (#2087)
  • Loading branch information
davecramer committed Mar 9, 2021
1 parent e6b23db commit 72e641c
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 6 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 @@ -810,7 +810,7 @@ public void testPartialIndexInfo() throws SQLException {

@Test
public void testTableTypes() throws SQLException {
final List<String> expectedTableTypes = new ArrayList<String>(Arrays.asList("FOREIGN TABLE", "INDEX",
final List<String> expectedTableTypes = new ArrayList<String>(Arrays.asList("FOREIGN TABLE", "INDEX", "PARTITIONED INDEX",
"MATERIALIZED VIEW", "PARTITIONED TABLE", "SEQUENCE", "SYSTEM INDEX", "SYSTEM TABLE", "SYSTEM TOAST INDEX",
"SYSTEM TOAST TABLE", "SYSTEM VIEW", "TABLE", "TEMPORARY INDEX", "TEMPORARY SEQUENCE", "TEMPORARY TABLE",
"TEMPORARY VIEW", "TYPE", "VIEW"));
Expand All @@ -829,7 +829,7 @@ public void testTableTypes() throws SQLException {
rs.close();
Collections.sort(expectedTableTypes);
Collections.sort(foundTableTypes);
Assert.assertEquals("The table types received from DatabaseMetaData should match the 17 expected types",
Assert.assertEquals("The table types received from DatabaseMetaData should match the 18 expected types",
true, foundTableTypes.equals(expectedTableTypes));
}

Expand Down Expand Up @@ -1329,22 +1329,45 @@ public void testInformationAboutArrayTypes() throws SQLException {
assertTrue(!rs.next());
}

@Test
public void testPartionedTablesIndex() throws SQLException {
if (TestUtil.haveMinimumServerVersion(con, ServerVersion.v11)) {
Statement stmt = null;
try {
stmt = con.createStatement();
stmt.execute(
"CREATE TABLE measurement (logdate date not null primary key,peaktemp int,unitsales int ) PARTITION BY RANGE (logdate);");
DatabaseMetaData dbmd = con.getMetaData();
ResultSet rs = dbmd.getPrimaryKeys("", "", "measurement");
assertTrue(rs.next());
assertEquals("measurement_pkey", rs.getString(6));

} finally {
if (stmt != null) {
stmt.execute("drop table if exists measurement");
stmt.close();
}
}
}

}

@Test
public void testPartitionedTables() throws SQLException {
if (TestUtil.haveMinimumServerVersion(con, ServerVersion.v10)) {
Statement stmt = null;
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 ,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();
} finally {
if (stmt != null) {
stmt.execute("drop table measurement");
stmt.execute("drop table if exists measurement");
stmt.close();
}
}
Expand Down

0 comments on commit 72e641c

Please sign in to comment.