Skip to content

Commit f2c6e94

Browse files
committed
Rethink recent fix for pg_dump's handling of extension config tables.
Commit 3eb3d3e was a few bricks shy of a load: while it correctly set the table's "interesting" flag when deciding to dump the data of an extension config table, it was not correct to clear that flag if we concluded we shouldn't dump the data. This led to the crash reported in bug #16655, because in fact we'll traverse dumpTableSchema anyway for all extension tables (to see if they have user-added seclabels or RLS policies). The right thing to do is to force "interesting" true in makeTableDataInfo, and otherwise leave the flag alone. (Doing it there is more future-proof in case additional calls are added, and it also avoids setting the flag unnecessarily if that function decides the table is non-dumpable.) This investigation also showed that while only the --inserts code path had an obvious failure in the case considered by 3eb3d3e, the COPY code path also has a problem with not having loaded table subsidiary data. That causes fmtCopyColumnList to silently return an empty string instead of the correct column list. That accidentally mostly works, which perhaps is why we didn't notice this before. It would only fail if the restore column order is different from the dump column order, which only happens in weird inheritance cases, so it's not surprising nobody had hit the case with an extension config table. Nonetheless, it's a bug, and it goes a long way back, not just to v12 where the --inserts code path started to have a problem with this. In hopes of catching such cases a bit sooner in future, add some Asserts that "interesting" has been set in both dumpTableData and dumpTableSchema. Adjust the test case added by 3eb3d3e so that it checks the COPY rather than INSERT form of that bug, allowing it to detect the longer-standing symptom. Per bug #16655 from Cameron Daniel. Back-patch to all supported branches. Discussion: https://postgr.es/m/16655-5c92d6b3a9438137@postgresql.org Discussion: https://postgr.es/m/18048b44-3414-b983-8c7c-9165b177900d@2ndQuadrant.com
1 parent 37c0baa commit f2c6e94

File tree

3 files changed

+56
-6
lines changed

3 files changed

+56
-6
lines changed

src/bin/pg_dump/pg_dump.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2110,6 +2110,9 @@ dumpTableData(Archive *fout, TableDataInfo *tdinfo)
21102110
char *copyStmt;
21112111
const char *copyFrom;
21122112

2113+
/* We had better have loaded per-column details about this table */
2114+
Assert(tbinfo->interesting);
2115+
21132116
if (!dopt->dump_inserts)
21142117
{
21152118
/* Dump/restore using COPY */
@@ -2287,6 +2290,9 @@ makeTableDataInfo(DumpOptions *dopt, TableInfo *tbinfo, bool oids)
22872290
addObjectDependency(&tdinfo->dobj, tbinfo->dobj.dumpId);
22882291

22892292
tbinfo->dataObj = tdinfo;
2293+
2294+
/* Make sure that we'll collect per-column info for this table. */
2295+
tbinfo->interesting = true;
22902296
}
22912297

22922298
/*
@@ -15553,6 +15559,9 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
1555315559
int j,
1555415560
k;
1555515561

15562+
/* We had better have loaded per-column details about this table */
15563+
Assert(tbinfo->interesting);
15564+
1555615565
qrelname = pg_strdup(fmtId(tbinfo->dobj.name));
1555715566
qualrelname = pg_strdup(fmtQualifiedDumpable(tbinfo));
1555815567

src/test/modules/test_pg_dump/t/001_base.pl

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,20 @@
135135
"$tempdir/defaults_tar_format.tar",
136136
],
137137
},
138+
exclude_table => {
139+
dump_cmd => [
140+
'pg_dump',
141+
'--exclude-table=regress_table_dumpable',
142+
"--file=$tempdir/exclude_table.sql",
143+
'postgres',
144+
],
145+
},
146+
extension_schema => {
147+
dump_cmd => [
148+
'pg_dump', '--schema=public',
149+
"--file=$tempdir/extension_schema.sql", 'postgres',
150+
],
151+
},
138152
pg_dumpall_globals => {
139153
dump_cmd => [
140154
'pg_dumpall', '--no-sync',
@@ -219,6 +233,7 @@
219233
clean_if_exists => 1,
220234
createdb => 1,
221235
defaults => 1,
236+
exclude_table => 1,
222237
no_privs => 1,
223238
no_owner => 1,);
224239

@@ -301,20 +316,38 @@
301316
\n/xm,
302317
like => {
303318
%full_runs,
304-
data_only => 1,
305-
section_data => 1,
319+
data_only => 1,
320+
section_data => 1,
321+
extension_schema => 1,
306322
},
307323
},
308324
309325
'CREATE TABLE regress_pg_dump_table' => {
310326
regexp => qr/^
311327
\QCREATE TABLE public.regress_pg_dump_table (\E
312328
\n\s+\Qcol1 integer NOT NULL,\E
313-
\n\s+\Qcol2 integer\E
329+
\n\s+\Qcol2 integer,\E
330+
\n\s+\QCONSTRAINT regress_pg_dump_table_col2_check CHECK ((col2 > 0))\E
314331
\n\);\n/xm,
315332
like => { binary_upgrade => 1, },
316333
},
317334
335+
'COPY public.regress_table_dumpable (col1)' => {
336+
regexp => qr/^
337+
\QCOPY public.regress_table_dumpable (col1) FROM stdin;\E
338+
\n/xm,
339+
like => {
340+
%full_runs,
341+
data_only => 1,
342+
section_data => 1,
343+
extension_schema => 1,
344+
},
345+
unlike => {
346+
binary_upgrade => 1,
347+
exclude_table => 1,
348+
},
349+
},
350+
318351
'CREATE ACCESS METHOD regress_test_am' => {
319352
regexp => qr/^
320353
\QCREATE ACCESS METHOD regress_test_am TYPE INDEX HANDLER bthandler;\E
@@ -436,7 +469,8 @@
436469
regexp => qr/^
437470
\QCREATE TABLE regress_pg_dump_schema.test_table (\E
438471
\n\s+\Qcol1 integer,\E
439-
\n\s+\Qcol2 integer\E
472+
\n\s+\Qcol2 integer,\E
473+
\n\s+\QCONSTRAINT test_table_col2_check CHECK ((col2 > 0))\E
440474
\n\);\n/xm,
441475
like => { binary_upgrade => 1, },
442476
},
@@ -536,6 +570,7 @@
536570
like => {%pgdump_runs},
537571
unlike => {
538572
data_only => 1,
573+
extension_schema => 1,
539574
pg_dumpall_globals => 1,
540575
section_data => 1,
541576
section_pre_data => 1,
@@ -549,6 +584,7 @@
549584
like => {%pgdump_runs},
550585
unlike => {
551586
data_only => 1,
587+
extension_schema => 1,
552588
pg_dumpall_globals => 1,
553589
section_data => 1,
554590
section_pre_data => 1,

src/test/modules/test_pg_dump/test_pg_dump--1.0.sql

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,19 @@
55

66
CREATE TABLE regress_pg_dump_table (
77
col1 serial,
8-
col2 int
8+
col2 int check (col2 > 0)
99
);
1010

1111
CREATE SEQUENCE regress_pg_dump_seq;
1212

1313
CREATE SEQUENCE regress_seq_dumpable;
1414
SELECT pg_catalog.pg_extension_config_dump('regress_seq_dumpable', '');
1515

16+
CREATE TABLE regress_table_dumpable (
17+
col1 int check (col1 > 0)
18+
);
19+
SELECT pg_catalog.pg_extension_config_dump('regress_table_dumpable', '');
20+
1621
CREATE SCHEMA regress_pg_dump_schema;
1722

1823
GRANT USAGE ON regress_pg_dump_seq TO regress_dump_test_role;
@@ -29,7 +34,7 @@ CREATE ACCESS METHOD regress_test_am TYPE INDEX HANDLER bthandler;
2934
-- this extension.
3035
CREATE TABLE regress_pg_dump_schema.test_table (
3136
col1 int,
32-
col2 int
37+
col2 int check (col2 > 0)
3338
);
3439
GRANT SELECT ON regress_pg_dump_schema.test_table TO regress_dump_test_role;
3540

0 commit comments

Comments
 (0)