Patch
Dump rows of partitioned extension configuration tables.
- Branch
- master
- Risk
- –
- Base commit
- 91b1872ff2de16cd353598edbc650a185e649226
To apply it to a checkout of master at the base commit:
curl -s https://ai-annex.j-davis.com/api/v1/patches/01a13c5c-be2b-74ff-88eb-e465aedc1dc7 | jq -r .body | git am
From ee114413f38ceb51e92f26d2fe51f4069a14be69 Mon Sep 17 00:00:00 2001
From: Grok <grok@x.ai>
Date: Wed, 14 Oct 2026 14:23:08 -0700
Subject: [PATCH] Dump rows of partitioned extension configuration tables.
pg_extension_config_dump accepts a partitioned table, but pg_dump
skipped it because the rows live in partitions. Restore therefore
lost those rows. Dump each leaf partition that would not otherwise
be dumped, applying the same WHERE condition. Inheritance parents
still contribute only their own rows.
---
doc/src/sgml/extend.sgml | 8 ++
src/bin/pg_dump/common.c | 13 +-
src/bin/pg_dump/meson.build | 1 +
src/bin/pg_dump/pg_dump.c | 87 +++++++++++-
.../t/007_extension_config_partition.pl | 133 ++++++++++++++++++
5 files changed, 234 insertions(+), 8 deletions(-)
create mode 100644 src/bin/pg_dump/t/007_extension_config_partition.pl
diff --git a/doc/src/sgml/extend.sgml b/doc/src/sgml/extend.sgml
index 63c5ec6d1eb..ab84566544f 100644
--- a/doc/src/sgml/extend.sgml
+++ b/doc/src/sgml/extend.sgml
@@ -1070,6 +1070,14 @@ SELECT pg_catalog.pg_extension_config_dump('my_config', 'WHERE NOT standard_entr
in the rows created by the extension's script.
</para>
+ <para>
+ A partitioned configuration table stores no rows itself.
+ <application>pg_dump</application> dumps the rows of each partition and
+ applies the same <literal>WHERE</literal> condition to every partition.
+ An ordinary inherited table is different: only the rows stored in the
+ parent are dumped.
+ </para>
+
<para>
For sequences, the second argument of <function>pg_extension_config_dump</function>
has no effect.
diff --git a/src/bin/pg_dump/common.c b/src/bin/pg_dump/common.c
index cda9d73a9dd..115831dc290 100644
--- a/src/bin/pg_dump/common.c
+++ b/src/bin/pg_dump/common.c
@@ -194,14 +194,19 @@ getSchemaData(Archive *fout, int *numTablesPtr)
pg_log_info("reading event triggers");
getEventTriggers(fout);
+ /*
+ * Link children to their parents before extension configuration tables
+ * are processed. A partitioned configuration table stores its rows in
+ * partitions, and processExtensionTables walks parents[] to find them. An
+ * inheritance parent is unaffected: only its own heap is dumped.
+ */
+ pg_log_info("finding inheritance relationships");
+ flagInhTables(fout, tblinfo, numTables, inhinfo, numInherits);
+
/* Identify extension configuration tables that should be dumped */
pg_log_info("finding extension tables");
processExtensionTables(fout, extinfo, numExtensions);
- /* Link tables to parents, mark parents of target tables interesting */
- pg_log_info("finding inheritance relationships");
- flagInhTables(fout, tblinfo, numTables, inhinfo, numInherits);
-
pg_log_info("reading column info for interesting tables");
getTableAttrs(fout, tblinfo, numTables);
diff --git a/src/bin/pg_dump/meson.build b/src/bin/pg_dump/meson.build
index 79bd5036841..b95efa11348 100644
--- a/src/bin/pg_dump/meson.build
+++ b/src/bin/pg_dump/meson.build
@@ -103,6 +103,7 @@ tests += {
't/004_pg_dump_parallel.pl',
't/005_pg_dump_filterfile.pl',
't/006_pg_dump_compress.pl',
+ 't/007_extension_config_partition.pl',
't/010_dump_connstr.pl',
],
},
diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c
index 69171d6b859..224f91843c5 100644
--- a/src/bin/pg_dump/pg_dump.c
+++ b/src/bin/pg_dump/pg_dump.c
@@ -353,6 +353,9 @@ static void addConstrChildIdxDeps(DumpableObject *dobj, const IndxInfo *refidx);
static void getDomainConstraints(Archive *fout, TypeInfo *tyinfo);
static void getTableData(DumpOptions *dopt, TableInfo *tblinfo, int numTables, char relkind);
static void makeTableDataInfo(DumpOptions *dopt, TableInfo *tbinfo);
+static void dumpPartitionedConfigData(DumpOptions *dopt, TableInfo *tbinfo,
+ const char *filtercond,
+ DumpableObject **dobjs, int numObjs);
static void buildMatViewRefreshDependencies(Archive *fout);
static void getTableDataFKConstraints(void);
static void determineNotNullFlags(Archive *fout, PGresult *res, int r,
@@ -19932,6 +19935,59 @@ getExtensionMembership(Archive *fout, ExtensionInfo extinfo[],
destroyPQExpBuffer(query);
}
+/*
+ * dumpPartitionedConfigData -
+ * Dump rows of a partitioned extension configuration table.
+ *
+ * The parent has no storage. Rows live in leaf partitions, which are usually
+ * extension members and would otherwise be omitted. Walk immediate
+ * partitions and dump each leaf that does not already have a data object.
+ * The same WHERE condition is applied to every leaf. Inheritance children
+ * are not partitions and are never visited here.
+ *
+ * A partition that already has DUMP_COMPONENT_DATA is a normal table dump
+ * (it is not an extension member, or this is a binary upgrade). Creating a
+ * TableDataInfo for it here would suppress that dump, and the WHERE
+ * condition could then drop rows that the normal dump must keep.
+ */
+static void
+dumpPartitionedConfigData(DumpOptions *dopt, TableInfo *tbinfo,
+ const char *filtercond,
+ DumpableObject **dobjs, int numObjs)
+{
+ int i;
+
+ if (tbinfo->relkind != RELKIND_PARTITIONED_TABLE)
+ {
+ if (tbinfo->dataObj != NULL)
+ return;
+ if (tbinfo->dobj.dump & DUMP_COMPONENT_DATA)
+ return;
+
+ makeTableDataInfo(dopt, tbinfo);
+ if (tbinfo->dataObj != NULL && filtercond != NULL &&
+ filtercond[0] != '\0')
+ tbinfo->dataObj->filtercond = pg_strdup(filtercond);
+ return;
+ }
+
+ for (i = 0; i < numObjs; i++)
+ {
+ TableInfo *child;
+
+ if (dobjs[i]->objType != DO_TABLE)
+ continue;
+
+ child = (TableInfo *) dobjs[i];
+ if (!child->ispartition || child->numParents != 1)
+ continue;
+ if (child->parents[0] != tbinfo)
+ continue;
+
+ dumpPartitionedConfigData(dopt, child, filtercond, dobjs, numObjs);
+ }
+}
+
/*
* processExtensionTables --- deal with extension configuration tables
*
@@ -19945,6 +20001,10 @@ getExtensionMembership(Archive *fout, ExtensionInfo extinfo[],
* expected to be handled at CREATE EXTENSION time, including any indexes or
* foreign keys, which brings us to-
*
+ * A partitioned table has no rows of its own. Its registered contents are
+ * the rows of its partitions, so those partitions are dumped instead.
+ * Ordinary inheritance parents are not expanded: they have their own heap.
+ *
* 2. Record FK dependencies between configuration tables.
*
* Due to the FKs being created at CREATE EXTENSION time and therefore before
@@ -19966,6 +20026,8 @@ processExtensionTables(Archive *fout, ExtensionInfo extinfo[],
i;
int i_conrelid,
i_confrelid;
+ DumpableObject **dobjs = NULL;
+ int numObjs = 0;
/* Nothing to do if no extensions */
if (numExtensions == 0)
@@ -20063,11 +20125,26 @@ processExtensionTables(Archive *fout, ExtensionInfo extinfo[],
if (dumpobj)
{
- makeTableDataInfo(dopt, configtbl);
- if (configtbl->dataObj != NULL)
+ if (configtbl->relkind == RELKIND_PARTITIONED_TABLE)
{
- if (strlen(extconditionarray[j]) > 0)
- configtbl->dataObj->filtercond = pg_strdup(extconditionarray[j]);
+ /*
+ * Snapshot once. TableDataInfo objects created while
+ * walking are not in this array, and must not be.
+ */
+ if (dobjs == NULL)
+ getDumpableObjects(&dobjs, &numObjs);
+ dumpPartitionedConfigData(dopt, configtbl,
+ extconditionarray[j],
+ dobjs, numObjs);
+ }
+ else
+ {
+ makeTableDataInfo(dopt, configtbl);
+ if (configtbl->dataObj != NULL)
+ {
+ if (strlen(extconditionarray[j]) > 0)
+ configtbl->dataObj->filtercond = pg_strdup(extconditionarray[j]);
+ }
}
}
}
@@ -20078,6 +20155,8 @@ processExtensionTables(Archive *fout, ExtensionInfo extinfo[],
free(extconditionarray);
}
+ pg_free(dobjs);
+
/*
* Now that all the TableDataInfo objects have been created for all the
* extensions, check their FK dependencies and register them to try and
diff --git a/src/bin/pg_dump/t/007_extension_config_partition.pl b/src/bin/pg_dump/t/007_extension_config_partition.pl
new file mode 100644
index 00000000000..a03b4dc5d77
--- /dev/null
+++ b/src/bin/pg_dump/t/007_extension_config_partition.pl
@@ -0,0 +1,133 @@
+# Copyright (c) 2026, PostgreSQL Global Development Group
+
+# A partitioned table registered with pg_extension_config_dump has no
+# storage of its own. pg_dump must still emit the rows that live in its
+# partitions, honoring the WHERE condition on each partition. An
+# inheritance parent keeps the old behavior: only its own rows.
+
+use strict;
+use warnings FATAL => 'all';
+
+use PostgreSQL::Test::Cluster;
+use PostgreSQL::Test::Utils;
+use Test::More;
+
+my $ext_name = 'dump_part_cfg';
+my $ext_dir = PostgreSQL::Test::Utils::tempdir;
+mkdir "$ext_dir/extension" or die "could not create extension directory: $!";
+
+open my $cf, '>', "$ext_dir/extension/$ext_name.control"
+ or die "could not create control file: $!";
+print $cf <<'CTL';
+comment = 'extension config partition dump test'
+default_version = '1.0'
+relocatable = true
+CTL
+close $cf;
+
+open my $sqlf, '>', "$ext_dir/extension/$ext_name--1.0.sql"
+ or die "could not create SQL file: $!";
+print $sqlf <<"SQL";
+\\echo Use "CREATE EXTENSION $ext_name" to load this file. \\quit
+
+CREATE TABLE z_cfg (k int, v text) PARTITION BY LIST (k);
+CREATE TABLE z_cfg1 PARTITION OF z_cfg FOR VALUES IN (1);
+CREATE TABLE z_cfg2 PARTITION OF z_cfg FOR VALUES IN (2);
+
+CREATE TABLE z_filt (k int, v text) PARTITION BY LIST (k);
+CREATE TABLE z_filt_mid PARTITION OF z_filt FOR VALUES IN (1) PARTITION BY LIST (k);
+CREATE TABLE z_filt1 PARTITION OF z_filt_mid FOR VALUES IN (1);
+CREATE TABLE z_filt2 PARTITION OF z_filt FOR VALUES IN (2);
+
+CREATE TABLE z_inh (k int, v text);
+CREATE TABLE z_plain (k int, v text);
+
+SELECT pg_catalog.pg_extension_config_dump('z_cfg', '');
+SELECT pg_catalog.pg_extension_config_dump('z_filt', 'WHERE v <> ''skip''');
+SELECT pg_catalog.pg_extension_config_dump('z_inh', '');
+SELECT pg_catalog.pg_extension_config_dump('z_plain', '');
+SQL
+close $sqlf;
+
+my $sep = $windows_os ? ';' : ':';
+my $ext_path = $windows_os ? ($ext_dir =~ s/\\/\\\\/gr) : $ext_dir;
+
+my $node = PostgreSQL::Test::Cluster->new('main');
+$node->init;
+$node->append_conf('postgresql.conf',
+ "extension_control_path = '\$system$sep$ext_path'\n");
+$node->start;
+
+$node->safe_psql('postgres', "CREATE EXTENSION $ext_name");
+$node->safe_psql(
+ 'postgres', q{
+INSERT INTO z_cfg VALUES (1, 'user1'), (2, 'user2');
+CREATE TABLE z_cfg3 PARTITION OF z_cfg FOR VALUES IN (3);
+INSERT INTO z_cfg VALUES (3, 'later');
+INSERT INTO z_filt VALUES (1, 'keep'), (1, 'skip'), (2, 'keep2');
+INSERT INTO z_inh VALUES (1, 'parent');
+CREATE TABLE z_inh_child () INHERITS (z_inh);
+INSERT INTO z_inh_child VALUES (2, 'child');
+INSERT INTO z_plain VALUES (7, 'plain');
+});
+
+my $dumpfile = $node->backup_dir . '/dump.sql';
+$node->command_ok([ 'pg_dump', '--file' => $dumpfile, 'postgres' ],
+ 'dump database');
+
+my $dump = slurp_file($dumpfile);
+
+like($dump, qr/^COPY public\.z_cfg1 /m, 'member partition z_cfg1 is copied');
+like($dump, qr/^COPY public\.z_cfg2 /m, 'member partition z_cfg2 is copied');
+unlike($dump, qr/^COPY public\.z_cfg /m,
+ 'partitioned parent is not copied');
+like($dump, qr/^COPY public\.z_cfg3 /m,
+ 'non-member partition is copied as an ordinary table');
+like($dump, qr/^COPY public\.z_filt1 /m, 'nested partition is copied');
+like($dump, qr/^COPY public\.z_filt2 /m, 'direct filter partition is copied');
+unlike($dump, qr/^COPY public\.z_filt /m,
+ 'filtered partitioned parent is not copied');
+unlike($dump, qr/^COPY public\.z_filt_mid /m,
+ 'intermediate partitioned parent is not copied');
+like($dump, qr/^COPY public\.z_inh /m, 'inheritance parent is copied');
+like($dump, qr/^COPY public\.z_inh_child /m,
+ 'inheritance child is copied separately');
+like($dump, qr/^COPY public\.z_plain /m, 'ordinary config table is copied');
+
+like($dump, qr/^1\tuser1$/m, 'user1 row is dumped');
+like($dump, qr/^2\tuser2$/m, 'user2 row is dumped');
+like($dump, qr/^3\tlater$/m, 'non-member partition row is dumped');
+like($dump, qr/^1\tkeep$/m, 'filtered keep row is dumped');
+unlike($dump, qr/^1\tskip$/m, 'WHERE condition omits skip row');
+like($dump, qr/^2\tkeep2$/m, 'filtered keep2 row is dumped');
+like($dump, qr/^1\tparent$/m, 'inheritance parent row is dumped');
+like($dump, qr/^2\tchild$/m, 'inheritance child row is dumped');
+like($dump, qr/^7\tplain$/m, 'ordinary config row is dumped');
+
+is(scalar(() = $dump =~ /^1\tkeep$/mg), 1, 'keep row is dumped once');
+is(scalar(() = $dump =~ /^3\tlater$/mg), 1, 'later row is dumped once');
+
+$node->safe_psql('postgres', 'CREATE DATABASE restored');
+$node->command_ok(
+ [ 'psql', '-d', 'restored', '-v', 'ON_ERROR_STOP=1', '-f', $dumpfile ],
+ 'restore dump');
+
+is( $node->safe_psql('restored', 'SELECT k, v FROM z_cfg ORDER BY 1, 2'),
+ "1|user1\n2|user2\n3|later",
+ 'partition rows survive dump and restore');
+is( $node->safe_psql('restored', 'SELECT k, v FROM z_filt ORDER BY 1, 2'),
+ "1|keep\n2|keep2",
+ 'WHERE condition filters each partition');
+is( $node->safe_psql('restored', 'SELECT k, v FROM ONLY z_inh ORDER BY 1'),
+ '1|parent',
+ 'inheritance parent restores only its own row');
+is( $node->safe_psql('restored', 'SELECT k, v FROM z_inh_child ORDER BY 1'),
+ '2|child',
+ 'inheritance child restores its own row');
+is( $node->safe_psql('restored', 'SELECT k, v FROM z_plain'),
+ '7|plain',
+ 'ordinary config table is unchanged');
+
+$node->stop;
+
+done_testing();
base-commit: 91b1872ff2de16cd353598edbc650a185e649226
--
2.53.0