Commit d6c84667 authored by Tom Lane's avatar Tom Lane

Reorder code in pg_dump to dump comments etc in a uniform order.

Most of the code in pg_dump dumps an object's comment, security label,
and ACL auxiliary TOC entries, in that order, immediately after the
object's main TOC entry, and at least dumpComment's API spec says this
isn't optional.  dumpDatabase was significantly violating that when
in binary-upgrade mode, by inserting totally unrelated stuff between.
Also, dumpForeignDataWrapper and dumpForeignServer were being randomly
inconsistent.  Reorder code so everybody does it the same.

This may be future-proofing us against some code growing a requirement for
such auxiliary entries to be adjacent to their main entry.  But for now
it's just neatnik-ism, so I see no need for back-patch.

Discussion: https://postgr.es/m/21714.1516553459@sss.pgh.pa.us
parent f4987043
...@@ -2696,6 +2696,68 @@ dumpDatabase(Archive *fout) ...@@ -2696,6 +2696,68 @@ dumpDatabase(Archive *fout)
NULL, /* Dumper */ NULL, /* Dumper */
NULL); /* Dumper Arg */ NULL); /* Dumper Arg */
/* Compute correct tag for comments etc */
appendPQExpBuffer(labelq, "DATABASE %s", fmtId(datname));
/* Dump DB comment if any */
if (fout->remoteVersion >= 80200)
{
/*
* 8.2 and up keep comments on shared objects in a shared table, so we
* cannot use the dumpComment() code used for other database objects.
* Be careful that the ArchiveEntry parameters match that function.
*/
char *comment = PQgetvalue(res, 0, PQfnumber(res, "description"));
if (comment && *comment)
{
resetPQExpBuffer(dbQry);
/*
* Generates warning when loaded into a differently-named
* database.
*/
appendPQExpBuffer(dbQry, "COMMENT ON DATABASE %s IS ", fmtId(datname));
appendStringLiteralAH(dbQry, comment, fout);
appendPQExpBufferStr(dbQry, ";\n");
ArchiveEntry(fout, nilCatalogId, createDumpId(),
labelq->data, NULL, NULL, dba,
false, "COMMENT", SECTION_NONE,
dbQry->data, "", NULL,
&(dbDumpId), 1,
NULL, NULL);
}
}
else
{
dumpComment(fout, labelq->data, NULL, dba,
dbCatId, 0, dbDumpId);
}
/* Dump shared security label. */
if (!dopt->no_security_labels && fout->remoteVersion >= 90200)
{
PGresult *shres;
PQExpBuffer seclabelQry;
seclabelQry = createPQExpBuffer();
buildShSecLabelQuery(conn, "pg_database", dbCatId.oid, seclabelQry);
shres = ExecuteSqlQuery(fout, seclabelQry->data, PGRES_TUPLES_OK);
resetPQExpBuffer(seclabelQry);
emitShSecLabels(conn, shres, seclabelQry, "DATABASE", datname);
if (seclabelQry->len > 0)
ArchiveEntry(fout, nilCatalogId, createDumpId(),
labelq->data, NULL, NULL, dba,
false, "SECURITY LABEL", SECTION_NONE,
seclabelQry->data, "", NULL,
&(dbDumpId), 1,
NULL, NULL);
destroyPQExpBuffer(seclabelQry);
PQclear(shres);
}
/* /*
* pg_largeobject and pg_largeobject_metadata come from the old system * pg_largeobject and pg_largeobject_metadata come from the old system
* intact, so set their relfrozenxids and relminmxids. * intact, so set their relfrozenxids and relminmxids.
...@@ -2788,68 +2850,6 @@ dumpDatabase(Archive *fout) ...@@ -2788,68 +2850,6 @@ dumpDatabase(Archive *fout)
destroyPQExpBuffer(loOutQry); destroyPQExpBuffer(loOutQry);
} }
/* Compute correct tag for comments etc */
appendPQExpBuffer(labelq, "DATABASE %s", fmtId(datname));
/* Dump DB comment if any */
if (fout->remoteVersion >= 80200)
{
/*
* 8.2 and up keep comments on shared objects in a shared table, so we
* cannot use the dumpComment() code used for other database objects.
* Be careful that the ArchiveEntry parameters match that function.
*/
char *comment = PQgetvalue(res, 0, PQfnumber(res, "description"));
if (comment && *comment)
{
resetPQExpBuffer(dbQry);
/*
* Generates warning when loaded into a differently-named
* database.
*/
appendPQExpBuffer(dbQry, "COMMENT ON DATABASE %s IS ", fmtId(datname));
appendStringLiteralAH(dbQry, comment, fout);
appendPQExpBufferStr(dbQry, ";\n");
ArchiveEntry(fout, nilCatalogId, createDumpId(),
labelq->data, NULL, NULL, dba,
false, "COMMENT", SECTION_NONE,
dbQry->data, "", NULL,
&(dbDumpId), 1,
NULL, NULL);
}
}
else
{
dumpComment(fout, labelq->data, NULL, dba,
dbCatId, 0, dbDumpId);
}
/* Dump shared security label. */
if (!dopt->no_security_labels && fout->remoteVersion >= 90200)
{
PGresult *shres;
PQExpBuffer seclabelQry;
seclabelQry = createPQExpBuffer();
buildShSecLabelQuery(conn, "pg_database", dbCatId.oid, seclabelQry);
shres = ExecuteSqlQuery(fout, seclabelQry->data, PGRES_TUPLES_OK);
resetPQExpBuffer(seclabelQry);
emitShSecLabels(conn, shres, seclabelQry, "DATABASE", datname);
if (seclabelQry->len > 0)
ArchiveEntry(fout, nilCatalogId, createDumpId(),
labelq->data, NULL, NULL, dba,
false, "SECURITY LABEL", SECTION_NONE,
seclabelQry->data, "", NULL,
&(dbDumpId), 1,
NULL, NULL);
destroyPQExpBuffer(seclabelQry);
PQclear(shres);
}
PQclear(res); PQclear(res);
destroyPQExpBuffer(dbQry); destroyPQExpBuffer(dbQry);
...@@ -14395,6 +14395,12 @@ dumpForeignDataWrapper(Archive *fout, FdwInfo *fdwinfo) ...@@ -14395,6 +14395,12 @@ dumpForeignDataWrapper(Archive *fout, FdwInfo *fdwinfo)
NULL, 0, NULL, 0,
NULL, NULL); NULL, NULL);
/* Dump Foreign Data Wrapper Comments */
if (fdwinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
dumpComment(fout, labelq->data,
NULL, fdwinfo->rolname,
fdwinfo->dobj.catId, 0, fdwinfo->dobj.dumpId);
/* Handle the ACL */ /* Handle the ACL */
if (fdwinfo->dobj.dump & DUMP_COMPONENT_ACL) if (fdwinfo->dobj.dump & DUMP_COMPONENT_ACL)
dumpACL(fout, fdwinfo->dobj.catId, fdwinfo->dobj.dumpId, dumpACL(fout, fdwinfo->dobj.catId, fdwinfo->dobj.dumpId,
...@@ -14404,12 +14410,6 @@ dumpForeignDataWrapper(Archive *fout, FdwInfo *fdwinfo) ...@@ -14404,12 +14410,6 @@ dumpForeignDataWrapper(Archive *fout, FdwInfo *fdwinfo)
fdwinfo->fdwacl, fdwinfo->rfdwacl, fdwinfo->fdwacl, fdwinfo->rfdwacl,
fdwinfo->initfdwacl, fdwinfo->initrfdwacl); fdwinfo->initfdwacl, fdwinfo->initrfdwacl);
/* Dump Foreign Data Wrapper Comments */
if (fdwinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
dumpComment(fout, labelq->data,
NULL, fdwinfo->rolname,
fdwinfo->dobj.catId, 0, fdwinfo->dobj.dumpId);
free(qfdwname); free(qfdwname);
destroyPQExpBuffer(q); destroyPQExpBuffer(q);
...@@ -14492,6 +14492,12 @@ dumpForeignServer(Archive *fout, ForeignServerInfo *srvinfo) ...@@ -14492,6 +14492,12 @@ dumpForeignServer(Archive *fout, ForeignServerInfo *srvinfo)
NULL, 0, NULL, 0,
NULL, NULL); NULL, NULL);
/* Dump Foreign Server Comments */
if (srvinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
dumpComment(fout, labelq->data,
NULL, srvinfo->rolname,
srvinfo->dobj.catId, 0, srvinfo->dobj.dumpId);
/* Handle the ACL */ /* Handle the ACL */
if (srvinfo->dobj.dump & DUMP_COMPONENT_ACL) if (srvinfo->dobj.dump & DUMP_COMPONENT_ACL)
dumpACL(fout, srvinfo->dobj.catId, srvinfo->dobj.dumpId, dumpACL(fout, srvinfo->dobj.catId, srvinfo->dobj.dumpId,
...@@ -14508,12 +14514,6 @@ dumpForeignServer(Archive *fout, ForeignServerInfo *srvinfo) ...@@ -14508,12 +14514,6 @@ dumpForeignServer(Archive *fout, ForeignServerInfo *srvinfo)
srvinfo->rolname, srvinfo->rolname,
srvinfo->dobj.catId, srvinfo->dobj.dumpId); srvinfo->dobj.catId, srvinfo->dobj.dumpId);
/* Dump Foreign Server Comments */
if (srvinfo->dobj.dump & DUMP_COMPONENT_COMMENT)
dumpComment(fout, labelq->data,
NULL, srvinfo->rolname,
srvinfo->dobj.catId, 0, srvinfo->dobj.dumpId);
free(qsrvname); free(qsrvname);
destroyPQExpBuffer(q); destroyPQExpBuffer(q);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment