Hi,
I reproduced a schema-only dump that fails to restore into an empty database on PostgreSQL master that was previously found in greengage (https://github.com/GreengageDB/greengage/pull/605).
The extension creates a partitioned table and a unique index on it. A partition is then created outside the extension.
To reproduce, place attached test_extension.control, test_extension--1.0.sql files in the server installation’s extension directory and install.
Then run:
CREATE EXTENSION test_extension;
CREATE TABLE foo_part PARTITION OF part FOR VALUES FROM ('2023-01-01') TO ('2023-02-01');
$ createdb idx_dump_test && pg_dump | psql -qbt -d idx_dump_test
This leads to an error like:
ERROR: relation "foo_part_id_date_idx" already exists
STATEMENT: CREATE UNIQUE INDEX foo_part_id_date_idx ON public.foo_part USING btree (id, date);
CREATE EXTENSION creates the parent index. When ALTER TABLE … ATTACH PARTITION runs, it creates a matching child index. The explicit CREATE UNIQUE INDEX then fails because the child index already exists.
I’ve attached a port of the Greengage patch to PostgreSQL master. It skips the child index definition and relies on partition attachment to recreate the index.
Would it make sense for pg_dump to emit the child index definition before ALTER TABLE … ATTACH PARTITION in this case, so that partition attachment can reuse the index? I’d appreciate feedback on whether that ordering would conflict with other restore dependencies.
Thanks,
Alexandra
test_extension.control
Description: Binary data
test_extension--1.0.sql
Description: application/sql
From 6ab16ace9e65e9a1a234c9637082e7564a561052 Mon Sep 17 00:00:00 2001 From: Alexandra Pervushina <[email protected]> Date: Tue, 1 Dec 2026 01:16:33 +0300 Subject: [PATCH v1] pg_dump: Avoid duplicate indexes on partitions of extension tables
---
src/bin/pg_dump/common.c | 108 ++++++
src/bin/pg_dump/pg_dump.c | 1 +
src/bin/pg_dump/pg_dump.h | 2 +
.../test_pg_dump/expected/test_pg_dump.out | 4 +-
src/test/modules/test_pg_dump/meson.build | 1 +
src/test/modules/test_pg_dump/t/001_base.pl | 33 ++
.../t/002_ext_partitioned_index.pl | 343 ++++++++++++++++++
.../test_pg_dump/test_pg_dump--1.0.sql | 8 +
8 files changed, 499 insertions(+), 1 deletion(-)
create mode 100644 src/test/modules/test_pg_dump/t/002_ext_partitioned_index.pl
diff --git a/src/bin/pg_dump/common.c b/src/bin/pg_dump/common.c
index cda9d73a9dd..be8bc894284 100644
--- a/src/bin/pg_dump/common.c
+++ b/src/bin/pg_dump/common.c
@@ -379,6 +379,26 @@ flagInhTables(Archive *fout, TableInfo *tblinfo, int numTables,
}
}
+/*
+ * findExtensionAncestorIndex -
+ * Return idx or its nearest ancestor that is an undumped extension table's
+ * index, or NULL if there is none.
+ */
+static IndxInfo *
+findExtensionAncestorIndex(IndxInfo *idx)
+{
+ while (idx != NULL)
+ {
+ if (idx->indextable->dobj.ext_member &&
+ !(idx->dobj.dump & DUMP_COMPONENT_DEFINITION))
+ return idx;
+ if (idx->parentidx == 0)
+ break;
+ idx = findIndexByOid(idx->parentidx);
+ }
+ return NULL;
+}
+
/*
* flagInhIndexes -
* Create IndexAttachInfo objects for partitioned indexes, and add
@@ -401,6 +421,7 @@ flagInhIndexes(Archive *fout, TableInfo tblinfo[], int numTables)
{
IndxInfo *index = &(tblinfo[i].indexes[j]);
IndxInfo *parentidx;
+ IndxInfo *extidx;
IndexAttachInfo *attachinfo;
if (index->parentidx == 0)
@@ -410,6 +431,63 @@ flagInhIndexes(Archive *fout, TableInfo tblinfo[], int numTables)
if (parentidx == NULL)
continue;
+ /*
+ * If an ancestor index belongs to an extension, ATTACH PARTITION
+ * recreates this index, so don't dump it or its constraint. This
+ * loses its custom properties and name (comments on a renamed
+ * index fail to restore). It also loses the index entirely if
+ * CREATE EXTENSION does not recreate the ancestor, e.g. if the
+ * user created it or the extension version differs. We can't
+ * tell these cases apart, so warn about it.
+ */
+ extidx = findExtensionAncestorIndex(parentidx);
+ if (extidx != NULL)
+ {
+ /*
+ * Only report it if index definitions would otherwise be
+ * dumped. There can be many such indexes, so list them only
+ * in verbose mode, and warn once per extension index below.
+ */
+ if ((index->dobj.dump & DUMP_COMPONENT_DEFINITION) &&
+ fout->dopt->dumpSchema &&
+ (fout->dopt->dumpSections & DUMP_POST_DATA))
+ {
+ DumpableObject *constraint = NULL;
+
+ if (index->indexconstraint != 0)
+ constraint = findObjectByDumpId(index->indexconstraint);
+
+ if (constraint != NULL)
+ pg_log_info("not dumping index \"%s.%s\" of constraint \"%s\" on partition \"%s.%s\", attached to index \"%s.%s\" of an extension table",
+ index->dobj.namespace->dobj.name,
+ index->dobj.name,
+ constraint->name,
+ tblinfo[i].dobj.namespace->dobj.name,
+ tblinfo[i].dobj.name,
+ extidx->dobj.namespace->dobj.name,
+ extidx->dobj.name);
+ else
+ pg_log_info("not dumping index \"%s.%s\" on partition \"%s.%s\", attached to index \"%s.%s\" of an extension table",
+ index->dobj.namespace->dobj.name,
+ index->dobj.name,
+ tblinfo[i].dobj.namespace->dobj.name,
+ tblinfo[i].dobj.name,
+ extidx->dobj.namespace->dobj.name,
+ extidx->dobj.name);
+ extidx->numSkippedChildren++;
+ }
+
+ index->dobj.dump &= ~DUMP_COMPONENT_DEFINITION;
+ if (index->indexconstraint != 0)
+ {
+ DumpableObject *constraint;
+
+ constraint = findObjectByDumpId(index->indexconstraint);
+ if (constraint != NULL)
+ constraint->dump &= ~DUMP_COMPONENT_DEFINITION;
+ }
+ }
+
attachinfo = pg_malloc_object(IndexAttachInfo);
attachinfo->dobj.objType = DO_INDEX_ATTACH;
@@ -447,6 +525,36 @@ flagInhIndexes(Archive *fout, TableInfo tblinfo[], int numTables)
simple_ptr_list_append(&parentidx->partattaches, &attachinfo->dobj);
}
}
+
+ /*
+ * Warn once per extension index whose child indexes were not dumped.
+ * pg_dump can't tell whether CREATE EXTENSION recreates it (it may have
+ * been added by the user, or the extension version may differ), and the
+ * child indexes are lost if it doesn't.
+ */
+ for (i = 0; i < numTables; i++)
+ {
+ for (j = 0; j < tblinfo[i].numIndexes; j++)
+ {
+ IndxInfo *index = &(tblinfo[i].indexes[j]);
+
+ if (index->numSkippedChildren == 0)
+ continue;
+
+ pg_log_warning(ngettext("%d index on partitions of extension table \"%s.%s\" is not dumped; "
+ "it is restored only if CREATE EXTENSION creates index \"%s.%s\" "
+ "(use --verbose to list it)",
+ "%d indexes on partitions of extension table \"%s.%s\" are not dumped; "
+ "they are restored only if CREATE EXTENSION creates index \"%s.%s\" "
+ "(use --verbose to list them)",
+ index->numSkippedChildren),
+ index->numSkippedChildren,
+ tblinfo[i].dobj.namespace->dobj.name,
+ tblinfo[i].dobj.name,
+ index->dobj.namespace->dobj.name,
+ index->dobj.name);
+ }
+ }
}
/*
diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c
index a3f38f3b886..06335f03a3b 100644
--- a/src/bin/pg_dump/pg_dump.c
+++ b/src/bin/pg_dump/pg_dump.c
@@ -8095,6 +8095,7 @@ getIndexes(Archive *fout, TableInfo tblinfo[], int numTables)
{
NULL, NULL
};
+ indxinfo[j].numSkippedChildren = 0;
if (indxinfo[j].parentidx == 0)
indexkind = RELKIND_INDEX;
diff --git a/src/bin/pg_dump/pg_dump.h b/src/bin/pg_dump/pg_dump.h
index 41d932f003b..cbbde6a5762 100644
--- a/src/bin/pg_dump/pg_dump.h
+++ b/src/bin/pg_dump/pg_dump.h
@@ -433,6 +433,8 @@ typedef struct _indxInfo
bool indnullsnotdistinct;
Oid parentidx; /* if a partition, parent index OID */
SimplePtrList partattaches; /* if partitioned, partition attach objects */
+ int numSkippedChildren; /* if on an extension table, number of
+ * child indexes not dumped */
/* if there is an associated constraint object, its dumpId: */
DumpId indexconstraint;
diff --git a/src/test/modules/test_pg_dump/expected/test_pg_dump.out b/src/test/modules/test_pg_dump/expected/test_pg_dump.out
index 98c9cd481e7..020934415f9 100644
--- a/src/test/modules/test_pg_dump/expected/test_pg_dump.out
+++ b/src/test/modules/test_pg_dump/expected/test_pg_dump.out
@@ -397,6 +397,8 @@ SELECT pg_describe_object(classid,objid,objsubid) COLLATE "C" AS obj,
sequence regress_seq_dumpable | role regress_dump_test_super | o
sequence wgo_then_regular | role regress_dump_test_super | i
sequence wgo_then_regular | role regress_dump_test_super | o
+ table regress_pg_dump_schema.parttab | role regress_dump_test_super | o
+ table regress_pg_dump_schema.parttab_pk | role regress_dump_test_super | o
table regress_pg_dump_schema.test_table | role regress_dump_test_role | a
table regress_pg_dump_schema.test_table | role regress_dump_test_role | i
table regress_pg_dump_schema.test_table | role regress_dump_test_super | i
@@ -411,7 +413,7 @@ SELECT pg_describe_object(classid,objid,objsubid) COLLATE "C" AS obj,
type regress_pg_dump_schema.test_type | role regress_dump_test_role | i
type regress_pg_dump_schema.test_type | role regress_dump_test_super | i
type regress_pg_dump_schema.test_type | role regress_dump_test_super | o
-(40 rows)
+(42 rows)
REASSIGN OWNED BY regress_dump_test_super TO CURRENT_ROLE;
-- Substitute for current user's name to keep test output consistent
diff --git a/src/test/modules/test_pg_dump/meson.build b/src/test/modules/test_pg_dump/meson.build
index 1d2f5736092..67629b28efa 100644
--- a/src/test/modules/test_pg_dump/meson.build
+++ b/src/test/modules/test_pg_dump/meson.build
@@ -17,6 +17,7 @@ tests += {
'tap': {
'tests': [
't/001_base.pl',
+ 't/002_ext_partitioned_index.pl',
],
},
}
diff --git a/src/test/modules/test_pg_dump/t/001_base.pl b/src/test/modules/test_pg_dump/t/001_base.pl
index 3d65ce4497a..708964ed3bb 100644
--- a/src/test/modules/test_pg_dump/t/001_base.pl
+++ b/src/test/modules/test_pg_dump/t/001_base.pl
@@ -806,6 +806,39 @@ my %tests = (
like => { binary_upgrade => 1, },
},
+ 'CREATE TABLE regress_pg_dump_schema.parttab' => {
+ regexp => qr/^
+ \QCREATE TABLE regress_pg_dump_schema.parttab (\E
+ \n\s+\Qcol1 integer,\E
+ \n\s+\Qcol2 integer\E
+ \n\)\n\QPARTITION BY RANGE (col2)\E/xm,
+ like => { binary_upgrade => 1, },
+ },
+
+ 'CREATE INDEX parttab_col1_col2_idx' => {
+ regexp => qr/^
+ \QCREATE UNIQUE INDEX parttab_col1_col2_idx ON ONLY regress_pg_dump_schema.parttab USING btree (col1, col2);\E
+ /xm,
+ like => { binary_upgrade => 1, },
+ },
+
+ 'CREATE TABLE regress_pg_dump_schema.parttab_pk' => {
+ regexp => qr/^
+ \QCREATE TABLE regress_pg_dump_schema.parttab_pk (\E
+ \n\s+\Qcol1 integer NOT NULL,\E
+ \n\s+\Qcol2 integer NOT NULL\E
+ \n\)\n\QPARTITION BY RANGE (col2)\E/xm,
+ like => { binary_upgrade => 1, },
+ },
+
+ 'ADD CONSTRAINT parttab_pk_pkey' => {
+ regexp => qr/^
+ \QALTER TABLE ONLY regress_pg_dump_schema.parttab_pk\E
+ \n\s+\QADD CONSTRAINT parttab_pk_pkey PRIMARY KEY (col1, col2);\E
+ /xm,
+ like => { binary_upgrade => 1, },
+ },
+
'ALTER INDEX pkey DEPENDS ON extension' => {
create_order => 11,
create_sql =>
diff --git a/src/test/modules/test_pg_dump/t/002_ext_partitioned_index.pl b/src/test/modules/test_pg_dump/t/002_ext_partitioned_index.pl
new file mode 100644
index 00000000000..74b5dade7a5
--- /dev/null
+++ b/src/test/modules/test_pg_dump/t/002_ext_partitioned_index.pl
@@ -0,0 +1,343 @@
+# Test dumping partitions (direct, sub-partitions, primary key) of an
+# extension-owned partitioned table with an extension-owned index: the child
+# index must not be dumped separately, except in a binary upgrade dump, and
+# pg_dump must warn about it. Also check that the comment of a renamed child
+# index is lost on restore, and that the child index of an index the user
+# added to the extension table is lost.
+
+use strict;
+use warnings;
+
+use PostgreSQL::Test::Cluster;
+use PostgreSQL::Test::Utils;
+use Test::More;
+
+my $tempdir = PostgreSQL::Test::Utils::tempdir;
+
+my $node = PostgreSQL::Test::Cluster->new('main');
+$node->init;
+$node->start;
+
+# regress_pg_dump_schema.parttab and its unique index on (col1, col2) are
+# created by the extension; the partition is not. The extension script
+# grants privileges to regress_dump_test_role, so it has to exist.
+$node->safe_psql(
+ 'postgres', q{
+ CREATE ROLE regress_dump_test_role;
+ CREATE EXTENSION test_pg_dump;
+ CREATE TABLE regress_pg_dump_schema.parttab_1
+ PARTITION OF regress_pg_dump_schema.parttab
+ FOR VALUES FROM (1) TO (100);
+ COMMENT ON INDEX regress_pg_dump_schema.parttab_1_col1_col2_idx IS 'child';
+ CREATE TABLE regress_pg_dump_schema.parttab_2
+ PARTITION OF regress_pg_dump_schema.parttab
+ FOR VALUES FROM (100) TO (200) PARTITION BY RANGE (col1);
+ CREATE TABLE regress_pg_dump_schema.parttab_2_1
+ PARTITION OF regress_pg_dump_schema.parttab_2
+ FOR VALUES FROM (1) TO (100);
+ CREATE TABLE regress_pg_dump_schema.parttab_pk_1
+ PARTITION OF regress_pg_dump_schema.parttab_pk
+ FOR VALUES FROM (1) TO (100);
+});
+
+my $create_child_index = qr/^
+ \QCREATE UNIQUE INDEX parttab_1_col1_col2_idx ON regress_pg_dump_schema.parttab_1 USING btree (col1, col2);\E
+ $/xm;
+my $attach_child_index = qr/^
+ \QALTER INDEX regress_pg_dump_schema.parttab_col1_col2_idx ATTACH PARTITION regress_pg_dump_schema.parttab_1_col1_col2_idx;\E
+ $/xm;
+my $create_grandchild_index = qr/^
+ \QCREATE UNIQUE INDEX parttab_2_1_col1_col2_idx ON regress_pg_dump_schema.parttab_2_1 USING btree (col1, col2);\E
+ $/xm;
+my $attach_grandchild_index = qr/^
+ \QALTER INDEX regress_pg_dump_schema.parttab_2_col1_col2_idx ATTACH PARTITION regress_pg_dump_schema.parttab_2_1_col1_col2_idx;\E
+ $/xm;
+my $add_child_pkey = qr/^
+ \s+\QADD CONSTRAINT parttab_pk_1_pkey PRIMARY KEY (col1, col2);\E
+ $/xm;
+my $comment_child_index = qr/^
+ \QCOMMENT ON INDEX regress_pg_dump_schema.parttab_1_col1_col2_idx IS 'child';\E
+ $/xm;
+my $attach_partition = qr/^
+ \QALTER TABLE ONLY regress_pg_dump_schema.parttab ATTACH PARTITION regress_pg_dump_schema.parttab_1 FOR VALUES FROM (1) TO (100);\E
+ $/xm;
+
+#########################################
+# Regular dump
+
+# Expected pg_dump warning, once per extension index, for the child indexes
+# that are not dumped.
+sub skip_warning
+{
+ my ($count, $table, $extindex) = @_;
+ my $what =
+ $count == 1
+ ? "1 index on partitions of extension table \"regress_pg_dump_schema.$table\" is not dumped; it is"
+ : "$count indexes on partitions of extension table \"regress_pg_dump_schema.$table\" are not dumped; they are";
+ return qr/\Qwarning: $what restored only if CREATE EXTENSION creates index "regress_pg_dump_schema.$extindex"\E/;
+}
+
+# Expected pg_dump --verbose line for a child index that is not dumped.
+sub skip_info
+{
+ my ($index, $partition, $extindex, $constraint) = @_;
+ my $of = defined $constraint ? " of constraint \"$constraint\"" : '';
+ return qr/\Qnot dumping index "regress_pg_dump_schema.$index"$of on partition "regress_pg_dump_schema.$partition", attached to index "regress_pg_dump_schema.$extindex" of an extension table\E/;
+}
+
+my ($stdout, $stderr) = run_command(
+ [
+ 'pg_dump', '--no-sync', "--file=$tempdir/defaults.sql",
+ '--dbname=' . $node->connstr('postgres')
+ ]);
+like($stderr, skip_warning(3, 'parttab', 'parttab_col1_col2_idx'),
+ 'pg_dump warns once about the child indexes of the extension index');
+like($stderr, skip_warning(1, 'parttab_pk', 'parttab_pk_pkey'),
+ 'pg_dump warns once about the child primary key of the extension table');
+is(scalar(() = $stderr =~ /warning:/g),
+ 2, 'pg_dump warns once per extension index');
+unlike($stderr, qr/not dumping index/,
+ 'pg_dump lists the child indexes only in verbose mode');
+
+($stdout, $stderr) = run_command(
+ [
+ 'pg_dump', '--no-sync', '--schema-only', '--verbose',
+ '--dbname=' . $node->connstr('postgres')
+ ]);
+like(
+ $stderr,
+ skip_info(
+ 'parttab_1_col1_col2_idx', 'parttab_1', 'parttab_col1_col2_idx'),
+ 'pg_dump --verbose lists the child index');
+like(
+ $stderr,
+ skip_info(
+ 'parttab_2_1_col1_col2_idx', 'parttab_2_1', 'parttab_col1_col2_idx'),
+ 'pg_dump --verbose lists the grandchild index');
+like(
+ $stderr,
+ skip_info(
+ 'parttab_pk_1_pkey', 'parttab_pk_1',
+ 'parttab_pk_pkey', 'parttab_pk_1_pkey'),
+ 'pg_dump --verbose lists the child primary key with its constraint');
+
+my $dump = slurp_file("$tempdir/defaults.sql");
+
+like($dump, $attach_partition, 'dump attaches the partition');
+unlike($dump, $create_child_index,
+ 'dump does not create the child index of the extension index');
+unlike($dump, $attach_child_index,
+ 'dump does not attach the child index of the extension index');
+unlike($dump, $create_grandchild_index,
+ 'dump does not create the grandchild index of the extension index');
+unlike($dump, $attach_grandchild_index,
+ 'dump does not attach the grandchild index of the extension index');
+unlike($dump, $add_child_pkey,
+ 'dump does not add the child primary key of the extension table');
+like($dump, $comment_child_index, 'dump keeps the child index comment');
+
+# Restore the dump and check that each partition ends up with exactly one
+# index, the one created by ATTACH PARTITION.
+$node->safe_psql('postgres', 'CREATE DATABASE restored');
+$node->command_ok(
+ [
+ 'psql', '--no-psqlrc', '--set=ON_ERROR_STOP=1', '--quiet',
+ "--file=$tempdir/defaults.sql", 'restored'
+ ],
+ 'dump restores');
+
+is( $node->safe_psql(
+ 'restored', q{
+ SELECT indrelid::regclass, indexrelid::regclass, i.inhparent::regclass
+ FROM pg_index
+ LEFT JOIN pg_inherits i ON i.inhrelid = indexrelid
+ WHERE indrelid::regclass::text IN
+ ('regress_pg_dump_schema.parttab_1',
+ 'regress_pg_dump_schema.parttab_2',
+ 'regress_pg_dump_schema.parttab_2_1',
+ 'regress_pg_dump_schema.parttab_pk_1')
+ ORDER BY indrelid::regclass::text;
+ }),
+ join("\n",
+ 'regress_pg_dump_schema.parttab_1|regress_pg_dump_schema.parttab_1_col1_col2_idx|regress_pg_dump_schema.parttab_col1_col2_idx',
+ 'regress_pg_dump_schema.parttab_2|regress_pg_dump_schema.parttab_2_col1_col2_idx|regress_pg_dump_schema.parttab_col1_col2_idx',
+ 'regress_pg_dump_schema.parttab_2_1|regress_pg_dump_schema.parttab_2_1_col1_col2_idx|regress_pg_dump_schema.parttab_2_col1_col2_idx',
+ 'regress_pg_dump_schema.parttab_pk_1|regress_pg_dump_schema.parttab_pk_1_pkey|regress_pg_dump_schema.parttab_pk_pkey'),
+ 'restored partitions have a single index each, attached to the parent index'
+);
+
+is( $node->safe_psql(
+ 'restored',
+ q{SELECT obj_description('regress_pg_dump_schema.parttab_1_col1_col2_idx'::regclass)}),
+ 'child',
+ 'restored child index keeps its comment');
+
+#########################################
+# Dumps without index definitions don't warn
+
+$node->command_checks_all(
+ [ 'pg_dump', '--no-sync', '--data-only', 'postgres' ],
+ 0,
+ [qr/^/],
+ [qr/^$/],
+ 'pg_dump --data-only runs without warnings');
+
+$node->command_checks_all(
+ [ 'pg_dump', '--no-sync', '--statistics-only', 'postgres' ],
+ 0,
+ [qr/^/],
+ [qr/^$/],
+ 'pg_dump --statistics-only runs without warnings');
+
+$node->command_checks_all(
+ [ 'pg_dump', '--no-sync', '--section=pre-data', 'postgres' ],
+ 0,
+ [qr/^/],
+ [qr/^$/],
+ 'pg_dump --section=pre-data runs without warnings');
+
+#########################################
+# Binary upgrade dump
+
+$node->command_checks_all(
+ [
+ 'pg_dump', '--no-sync', "--file=$tempdir/binary_upgrade.sql",
+ '--schema-only', '--binary-upgrade', 'postgres'
+ ],
+ 0,
+ [qr/^$/],
+ [qr/^$/],
+ 'pg_dump --binary-upgrade runs without warnings');
+
+$dump = slurp_file("$tempdir/binary_upgrade.sql");
+
+like($dump, $create_child_index,
+ 'binary upgrade dump creates the child index of the extension index');
+like($dump, $attach_child_index,
+ 'binary upgrade dump attaches the child index of the extension index');
+like($dump, $create_grandchild_index,
+ 'binary upgrade dump creates the grandchild index of the extension index');
+like($dump, $attach_grandchild_index,
+ 'binary upgrade dump attaches the grandchild index of the extension index');
+like($dump, $add_child_pkey,
+ 'binary upgrade dump adds the child primary key of the extension table');
+
+#########################################
+# Renamed child index with a comment: the comment is dumped under the old
+# name, which ATTACH PARTITION does not recreate.
+#
+# TODO: this is a known limitation, not the intended behavior. If it is
+# fixed, the child indexes should keep their names and comments, and the
+# checks below should be inverted.
+
+$node->safe_psql('postgres', 'CREATE DATABASE renamed');
+$node->safe_psql(
+ 'renamed', q{
+ CREATE EXTENSION test_pg_dump;
+ CREATE TABLE regress_pg_dump_schema.parttab_1
+ PARTITION OF regress_pg_dump_schema.parttab
+ FOR VALUES FROM (1) TO (100);
+ ALTER INDEX regress_pg_dump_schema.parttab_1_col1_col2_idx
+ RENAME TO parttab_1_renamed_idx;
+ COMMENT ON INDEX regress_pg_dump_schema.parttab_1_renamed_idx IS 'renamed';
+ CREATE TABLE regress_pg_dump_schema.parttab_pk_1
+ PARTITION OF regress_pg_dump_schema.parttab_pk
+ FOR VALUES FROM (1) TO (100);
+ ALTER INDEX regress_pg_dump_schema.parttab_pk_1_pkey
+ RENAME TO parttab_pk_1_renamed_pkey;
+ COMMENT ON CONSTRAINT parttab_pk_1_renamed_pkey
+ ON regress_pg_dump_schema.parttab_pk_1 IS 'renamed pkey';
+});
+
+$node->command_ok(
+ [ 'pg_dump', '--no-sync', "--file=$tempdir/renamed.sql", 'renamed' ],
+ 'pg_dump of renamed child indexes runs');
+
+$dump = slurp_file("$tempdir/renamed.sql");
+
+like(
+ $dump,
+ qr/^\QCOMMENT ON INDEX regress_pg_dump_schema.parttab_1_renamed_idx IS 'renamed';\E$/m,
+ 'dump comments on the child index under its old name');
+like(
+ $dump,
+ qr/^\QCOMMENT ON CONSTRAINT parttab_pk_1_renamed_pkey ON regress_pg_dump_schema.parttab_pk_1 IS 'renamed pkey';\E$/m,
+ 'dump comments on the child constraint under its old name');
+unlike($dump, qr/CREATE UNIQUE INDEX parttab_1_renamed_idx/,
+ 'dump does not create the renamed child index');
+
+$node->safe_psql('postgres', 'CREATE DATABASE renamed_restored_stop');
+my $ret;
+($ret, $stdout, $stderr) =
+ $node->psql('renamed_restored_stop', $dump, on_error_stop => 1);
+isnt($ret, 0, 'restore with ON_ERROR_STOP fails on the renamed child index');
+
+$node->safe_psql('postgres', 'CREATE DATABASE renamed_restored');
+($ret, $stdout, $stderr) =
+ $node->psql('renamed_restored', $dump, on_error_stop => 0);
+like(
+ $stderr,
+ qr/relation "regress_pg_dump_schema.parttab_1_renamed_idx" does not exist/,
+ 'restore reports the missing renamed child index');
+like(
+ $stderr,
+ qr/constraint "parttab_pk_1_renamed_pkey" for table "parttab_pk_1" does not exist/,
+ 'restore reports the missing renamed child constraint');
+
+is( $node->safe_psql(
+ 'renamed_restored', q{
+ SELECT indexrelid::regclass, obj_description(indexrelid, 'pg_class')
+ FROM pg_index
+ WHERE indrelid::regclass::text IN
+ ('regress_pg_dump_schema.parttab_1',
+ 'regress_pg_dump_schema.parttab_pk_1')
+ ORDER BY indrelid::regclass::text;
+ }),
+ join("\n",
+ 'regress_pg_dump_schema.parttab_1_col1_col2_idx|',
+ 'regress_pg_dump_schema.parttab_pk_1_pkey|'),
+ 'restored child indexes get generated names and lose their comments');
+
+#########################################
+# An index the user added to the extension table is not dumped and not
+# recreated by CREATE EXTENSION, so the child index is lost on restore.
+# pg_dump can't tell this from an index created by the extension, so it
+# only warns about it.
+
+$node->safe_psql('postgres', 'CREATE DATABASE user_index');
+$node->safe_psql(
+ 'user_index', q{
+ CREATE EXTENSION test_pg_dump;
+ CREATE INDEX parttab_user_idx ON regress_pg_dump_schema.parttab (col1);
+ CREATE TABLE regress_pg_dump_schema.parttab_1
+ PARTITION OF regress_pg_dump_schema.parttab
+ FOR VALUES FROM (1) TO (100);
+});
+
+$node->command_checks_all(
+ [ 'pg_dump', '--no-sync', "--file=$tempdir/user_index.sql", 'user_index' ],
+ 0,
+ [qr/^$/],
+ [ skip_warning(1, 'parttab', 'parttab_user_idx') ],
+ 'pg_dump warns about the child index of the user-created index');
+
+$node->safe_psql('postgres', 'CREATE DATABASE user_index_restored');
+$node->command_ok(
+ [
+ 'psql', '--no-psqlrc', '--set=ON_ERROR_STOP=1', '--quiet',
+ "--file=$tempdir/user_index.sql", 'user_index_restored'
+ ],
+ 'dump with a user-created extension table index restores');
+
+is( $node->safe_psql(
+ 'user_index_restored', q{
+ SELECT indexrelid::regclass FROM pg_index
+ WHERE indrelid = 'regress_pg_dump_schema.parttab_1'::regclass
+ ORDER BY 1::text;
+ }),
+ 'regress_pg_dump_schema.parttab_1_col1_col2_idx',
+ 'child index of the user-created index is lost on restore');
+
+$node->stop('fast');
+
+done_testing();
diff --git a/src/test/modules/test_pg_dump/test_pg_dump--1.0.sql b/src/test/modules/test_pg_dump/test_pg_dump--1.0.sql
index 1c68e146d91..622c9c0047a 100644
--- a/src/test/modules/test_pg_dump/test_pg_dump--1.0.sql
+++ b/src/test/modules/test_pg_dump/test_pg_dump--1.0.sql
@@ -62,3 +62,11 @@ GRANT EXECUTE ON FUNCTION regress_pg_dump_schema.test_func() TO regress_dump_tes
CREATE AGGREGATE regress_pg_dump_schema.test_agg(int2)
(SFUNC = int2_sum, STYPE = int8);
GRANT EXECUTE ON FUNCTION regress_pg_dump_schema.test_agg(int2) TO regress_dump_test_role;
+
+CREATE TABLE regress_pg_dump_schema.parttab (col1 int, col2 int)
+ PARTITION BY RANGE (col2);
+CREATE UNIQUE INDEX ON regress_pg_dump_schema.parttab (col1, col2);
+
+CREATE TABLE regress_pg_dump_schema.parttab_pk (col1 int, col2 int)
+ PARTITION BY RANGE (col2);
+ALTER TABLE regress_pg_dump_schema.parttab_pk ADD PRIMARY KEY (col1, col2);
base-commit: a68bb26f294c7f1280284f753b26ea7fb3f25964
