diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c index af84c25..38b25b0 100644 --- a/src/bin/pg_dump/pg_dump.c +++ b/src/bin/pg_dump/pg_dump.c @@ -1249,6 +1249,49 @@ expand_schema_name_patterns(Archive *fout, destroyPQExpBuffer(query); } +/* TODO: comments? */ +static void +find_partition_by_relid(Archive *fout, Oid parentId, SimpleOidList *oids) +{ + + PGresult *partitions; + int partition; + PQExpBuffer query = createPQExpBuffer(); + + appendPQExpBuffer(query, "SELECT inhrelid " + "FROM pg_catalog.pg_inherits " + "WHERE inhparent = %u", parentId); + + partitions = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); + + for (partition = 0; partition < PQntuples(partitions); partition++) + { + PGresult *res; + char *relkind; + Oid partid = atooid(PQgetvalue(partitions, partition, 0)); + + simple_oid_list_append(oids, partid); + + /* TODO: comments? */ + resetPQExpBuffer(query); + appendPQExpBuffer(query, "SELECT relkind " + "FROM pg_catalog.pg_class " + "WHERE oid = %u", partid); + + res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); + + if (PQntuples(res) == 0) + exit_horribly(NULL, "no matching partition tables were "); + + relkind = PQgetvalue(res, 0, 0); + + if (relkind[0] == RELKIND_PARTITIONED_TABLE) + find_partition_by_relid(fout, partid, oids); + } + PQclear(partitions); + destroyPQExpBuffer(query); +} + /* * Find the OIDs of all tables matching the given list of patterns, * and append them to the given OID list. @@ -1276,7 +1319,7 @@ expand_table_name_patterns(Archive *fout, for (cell = patterns->head; cell; cell = cell->next) { appendPQExpBuffer(query, - "SELECT c.oid" + "SELECT c.oid, c.relkind" "\nFROM pg_catalog.pg_class c" "\n LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace" "\nWHERE c.relkind in ('%c', '%c', '%c', '%c', '%c', '%c')\n", @@ -1293,7 +1336,17 @@ expand_table_name_patterns(Archive *fout, for (i = 0; i < PQntuples(res); i++) { - simple_oid_list_append(oids, atooid(PQgetvalue(res, i, 0))); + Oid relOid = atooid(PQgetvalue(res, i, 0)); + simple_oid_list_append(oids, relOid); + + /* TODO: comments? */ + if (fout->remoteVersion >= 100000) + { + char *relkind = PQgetvalue(res, i, 1); + + if (relkind[0] == RELKIND_PARTITIONED_TABLE) + find_partition_by_relid(fout, relOid, oids); + } } PQclear(res);