Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite70187c

Browse files
committed
Add pg_dump support for ALTER obj DEPENDS ON EXTENSION
pg_dump is oblivious to this kind of dependency, so they're lost ondump/restores (and pg_upgrade). Have pg_dump emit ALTER lines so thatthey're preserved. Add some pg_dump tests for the whole thing, also.Reviewed-by: Tom Lane (offlist)Reviewed-by: Ibrar AhmedReviewed-by: Ahsan Hadi (who also reviewed commit899a04f)Discussion:https://postgr.es/m/20200217225333.GA30974@alvherre.pgsql
1 parent63b51df commite70187c

File tree

4 files changed

+133
-8
lines changed

4 files changed

+133
-8
lines changed

‎src/bin/pg_dump/common.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,7 @@ AssignDumpId(DumpableObject *dobj)
587587
dobj->namespace=NULL;/* may be set later */
588588
dobj->dump=DUMP_COMPONENT_ALL;/* default assumption */
589589
dobj->ext_member= false;/* default assumption */
590+
dobj->depends_on_ext= false;/* default assumption */
590591
dobj->dependencies=NULL;
591592
dobj->nDeps=0;
592593
dobj->allocDeps=0;

‎src/bin/pg_dump/pg_dump.c

Lines changed: 99 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4252,6 +4252,55 @@ dumpSubscription(Archive *fout, SubscriptionInfo *subinfo)
42524252
free(qsubname);
42534253
}
42544254

4255+
/*
4256+
* Given a "create query", append as many ALTER ... DEPENDS ON EXTENSION as
4257+
* the object needs.
4258+
*/
4259+
static void
4260+
append_depends_on_extension(Archive *fout,
4261+
PQExpBuffer create,
4262+
DumpableObject *dobj,
4263+
const char *catalog,
4264+
const char *keyword,
4265+
const char *objname)
4266+
{
4267+
if (dobj->depends_on_ext)
4268+
{
4269+
char *nm;
4270+
PGresult *res;
4271+
PQExpBufferquery;
4272+
intntups;
4273+
inti_extname;
4274+
inti;
4275+
4276+
/* dodge fmtId() non-reentrancy */
4277+
nm = pg_strdup(objname);
4278+
4279+
query = createPQExpBuffer();
4280+
appendPQExpBuffer(query,
4281+
"SELECT e.extname "
4282+
"FROM pg_catalog.pg_depend d, pg_catalog.pg_extension e "
4283+
"WHERE d.refobjid = e.oid AND classid = '%s'::pg_catalog.regclass "
4284+
"AND objid = '%u'::pg_catalog.oid AND deptype = 'x' "
4285+
"AND refclassid = 'pg_catalog.pg_extension'::pg_catalog.regclass",
4286+
catalog,
4287+
dobj->catId.oid);
4288+
res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK);
4289+
ntups = PQntuples(res);
4290+
i_extname = PQfnumber(res, "extname");
4291+
for (i = 0; i < ntups; i++)
4292+
{
4293+
appendPQExpBuffer(create, "ALTER %s %s DEPENDS ON EXTENSION %s;\n",
4294+
keyword, nm,
4295+
fmtId(PQgetvalue(res, i, i_extname)));
4296+
}
4297+
4298+
PQclear(res);
4299+
pg_free(nm);
4300+
}
4301+
}
4302+
4303+
42554304
static void
42564305
binary_upgrade_set_type_oids_by_type_oid(Archive *fout,
42574306
PQExpBuffer upgrade_buffer,
@@ -12070,6 +12119,12 @@ dumpFunc(Archive *fout, FuncInfo *finfo)
1207012119

1207112120
appendPQExpBuffer(q, "\n %s;\n", asPart->data);
1207212121

12122+
append_depends_on_extension(fout, q, &finfo->dobj,
12123+
"pg_catalog.pg_proc", keyword,
12124+
psprintf("%s.%s",
12125+
fmtId(finfo->dobj.namespace->dobj.name),
12126+
funcsig));
12127+
1207312128
if (dopt->binary_upgrade)
1207412129
binary_upgrade_extension_member(q, &finfo->dobj,
1207512130
keyword, funcsig,
@@ -15780,6 +15835,14 @@ dumpTableSchema(Archive *fout, TableInfo *tbinfo)
1578015835
else
1578115836
appendPQExpBufferStr(q, ";\n");
1578215837

15838+
/* Materialized views can depend on extensions */
15839+
if (tbinfo->relkind == RELKIND_MATVIEW)
15840+
append_depends_on_extension(fout, q, &tbinfo->dobj,
15841+
"pg_catalog.pg_class",
15842+
tbinfo->relkind == RELKIND_MATVIEW ?
15843+
"MATERIALIZED VIEW" : "INDEX",
15844+
qualrelname);
15845+
1578315846
/*
1578415847
* in binary upgrade mode, update the catalog with any missing values
1578515848
* that might be present.
@@ -16280,6 +16343,7 @@ dumpIndex(Archive *fout, IndxInfo *indxinfo)
1628016343
PQExpBuffer q;
1628116344
PQExpBuffer delq;
1628216345
char *qindxname;
16346+
char *qqindxname;
1628316347

1628416348
if (dopt->dataOnly)
1628516349
return;
@@ -16288,6 +16352,7 @@ dumpIndex(Archive *fout, IndxInfo *indxinfo)
1628816352
delq = createPQExpBuffer();
1628916353

1629016354
qindxname = pg_strdup(fmtId(indxinfo->dobj.name));
16355+
qqindxname = pg_strdup(fmtQualifiedDumpable(indxinfo));
1629116356

1629216357
/*
1629316358
* If there's an associated constraint, don't dump the index per se, but
@@ -16340,8 +16405,7 @@ dumpIndex(Archive *fout, IndxInfo *indxinfo)
1634016405

1634116406
for (j = 0; j < nstatcols; j++)
1634216407
{
16343-
appendPQExpBuffer(q, "ALTER INDEX %s ",
16344-
fmtQualifiedDumpable(indxinfo));
16408+
appendPQExpBuffer(q, "ALTER INDEX %s ", qqindxname);
1634516409

1634616410
/*
1634716411
* Note that this is a column number, so no quotes should be
@@ -16354,6 +16418,11 @@ dumpIndex(Archive *fout, IndxInfo *indxinfo)
1635416418
}
1635516419
}
1635616420

16421+
/* Indexes can depend on extensions */
16422+
append_depends_on_extension(fout, q, &indxinfo->dobj,
16423+
"pg_catalog.pg_class",
16424+
"INDEX", qqindxname);
16425+
1635716426
/* If the index defines identity, we need to record that. */
1635816427
if (indxinfo->indisreplident)
1635916428
{
@@ -16364,8 +16433,7 @@ dumpIndex(Archive *fout, IndxInfo *indxinfo)
1636416433
qindxname);
1636516434
}
1636616435

16367-
appendPQExpBuffer(delq, "DROP INDEX %s;\n",
16368-
fmtQualifiedDumpable(indxinfo));
16436+
appendPQExpBuffer(delq, "DROP INDEX %s;\n", qqindxname);
1636916437

1637016438
if (indxinfo->dobj.dump & DUMP_COMPONENT_DEFINITION)
1637116439
ArchiveEntry(fout, indxinfo->dobj.catId, indxinfo->dobj.dumpId,
@@ -16396,6 +16464,7 @@ dumpIndex(Archive *fout, IndxInfo *indxinfo)
1639616464
destroyPQExpBuffer(q);
1639716465
destroyPQExpBuffer(delq);
1639816466
free(qindxname);
16467+
free(qqindxname);
1639916468
}
1640016469

1640116470
/*
@@ -16625,6 +16694,11 @@ dumpConstraint(Archive *fout, ConstraintInfo *coninfo)
1662516694
fmtId(indxinfo->dobj.name));
1662616695
}
1662716696

16697+
/* Indexes can depend on extensions */
16698+
append_depends_on_extension(fout, q, &indxinfo->dobj,
16699+
"pg_catalog.pg_class", "INDEX",
16700+
fmtQualifiedDumpable(indxinfo));
16701+
1662816702
appendPQExpBuffer(delq, "ALTER TABLE ONLY %s ",
1662916703
fmtQualifiedDumpable(tbinfo));
1663016704
appendPQExpBuffer(delq, "DROP CONSTRAINT %s;\n",
@@ -17148,6 +17222,7 @@ dumpTrigger(Archive *fout, TriggerInfo *tginfo)
1714817222
PQExpBuffer query;
1714917223
PQExpBuffer delqry;
1715017224
PQExpBuffer trigprefix;
17225+
PQExpBuffer trigidentity;
1715117226
char *qtabname;
1715217227
char *tgargs;
1715317228
size_tlentgargs;
@@ -17165,13 +17240,14 @@ dumpTrigger(Archive *fout, TriggerInfo *tginfo)
1716517240
query = createPQExpBuffer();
1716617241
delqry = createPQExpBuffer();
1716717242
trigprefix = createPQExpBuffer();
17243+
trigidentity = createPQExpBuffer();
1716817244

1716917245
qtabname = pg_strdup(fmtId(tbinfo->dobj.name));
1717017246

17171-
appendPQExpBuffer(delqry, "DROP TRIGGER%s ",
17172-
fmtId(tginfo->dobj.name));
17173-
appendPQExpBuffer(delqry, "ON %s;\n",
17174-
fmtQualifiedDumpable(tbinfo));
17247+
appendPQExpBuffer(trigidentity, "%s ", fmtId(tginfo->dobj.name));
17248+
appendPQExpBuffer(trigidentity, "ON %s", fmtQualifiedDumpable(tbinfo));
17249+
17250+
appendPQExpBuffer(delqry, "DROP TRIGGER %s;\n", trigidentity->data);
1717517251

1717617252
if (tginfo->tgdef)
1717717253
{
@@ -17290,6 +17366,11 @@ dumpTrigger(Archive *fout, TriggerInfo *tginfo)
1729017366
appendPQExpBufferStr(query, ");\n");
1729117367
}
1729217368

17369+
/* Triggers can depend on extensions */
17370+
append_depends_on_extension(fout, query, &tginfo->dobj,
17371+
"pg_catalog.pg_trigger", "TRIGGER",
17372+
trigidentity->data);
17373+
1729317374
if (tginfo->tgenabled != 't' && tginfo->tgenabled != 'O')
1729417375
{
1729517376
appendPQExpBuffer(query, "\nALTER TABLE %s ",
@@ -17339,6 +17420,7 @@ dumpTrigger(Archive *fout, TriggerInfo *tginfo)
1733917420
destroyPQExpBuffer(query);
1734017421
destroyPQExpBuffer(delqry);
1734117422
destroyPQExpBuffer(trigprefix);
17423+
destroyPQExpBuffer(trigidentity);
1734217424
free(qtabname);
1734317425
}
1734417426

@@ -17995,6 +18077,15 @@ getDependencies(Archive *fout)
1799518077
continue;
1799618078
}
1799718079

18080+
/*
18081+
* For 'x' dependencies, mark the object for later; we still add the
18082+
* normal dependency, for possible ordering purposes. Currently
18083+
* pg_dump_sort.c knows to put extensions ahead of all object types
18084+
* that could possibly depend on them, but this is safer.
18085+
*/
18086+
if (deptype == 'x')
18087+
dobj->depends_on_ext = true;
18088+
1799818089
/*
1799918090
* Ordinarily, table rowtypes have implicit dependencies on their
1800018091
* tables. However, for a composite type the implicit dependency goes

‎src/bin/pg_dump/pg_dump.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ typedef struct _dumpableObject
136136
DumpComponentsdump;/* bitmask of components to dump */
137137
DumpComponentsdump_contains;/* as above, but for contained objects */
138138
boolext_member;/* true if object is member of extension */
139+
booldepends_on_ext;/* true if object depends on an extension */
139140
DumpId*dependencies;/* dumpIds of objects this one depends on */
140141
intnDeps;/* number of valid dependencies */
141142
intallocDeps;/* allocated size of dependencies[] */

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,38 @@
523523
like => { binary_upgrade => 1, },
524524
},
525525
526+
'ALTER INDEX pkey DEPENDS ON extension' => {
527+
create_order => 11,
528+
create_sql =>
529+
'CREATE TABLE regress_pg_dump_schema.extdependtab (col1 integer primary key, col2 int);
530+
CREATE INDEX ON regress_pg_dump_schema.extdependtab (col2);
531+
ALTER INDEX regress_pg_dump_schema.extdependtab_col2_idx DEPENDS ON EXTENSION test_pg_dump;
532+
ALTER INDEX regress_pg_dump_schema.extdependtab_pkey DEPENDS ON EXTENSION test_pg_dump;',
533+
regexp => qr/^
534+
\QALTER INDEX regress_pg_dump_schema.extdependtab_pkey DEPENDS ON EXTENSION test_pg_dump;\E\n
535+
/xms,
536+
like => {%pgdump_runs},
537+
unlike => {
538+
data_only => 1,
539+
pg_dumpall_globals => 1,
540+
section_data => 1,
541+
section_pre_data => 1,
542+
},
543+
},
544+
545+
'ALTER INDEX idx DEPENDS ON extension' => {
546+
regexp => qr/^
547+
\QALTER INDEX regress_pg_dump_schema.extdependtab_col2_idx DEPENDS ON EXTENSION test_pg_dump;\E\n
548+
/xms,
549+
like => {%pgdump_runs},
550+
unlike => {
551+
data_only => 1,
552+
pg_dumpall_globals => 1,
553+
section_data => 1,
554+
section_pre_data => 1,
555+
},
556+
},
557+
526558
# Objects not included in extension, part of schema created by extension
527559
'CREATE TABLE regress_pg_dump_schema.external_tab' => {
528560
create_order => 4,

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp