Skip to content

Commit 89c30d1

Browse files
committed
Fix pg_dump's handling of public schema with both -c and -C options.
Since -c plus -C requests dropping and recreating the target database as a whole, not dropping individual objects in it, we should assume that the public schema already exists and need not be created. The previous coding considered only the state of the -c option, so it would emit "CREATE SCHEMA public" anyway, leading to an unexpected error in restore. Back-patch to 9.2. Older versions did not accept -c with -C so the issue doesn't arise there. (The logic being patched here dates to 8.0, cf commit 2193121, so it's not really wrong that it didn't consider the case at the time.) Note that versions before 9.6 will still attempt to emit REVOKE/GRANT on the public schema; but that happens without -c/-C too, and doesn't seem to be the focus of this complaint. I considered extending this stanza to also skip the public schema's ACL, but that would be a misfeature, as it'd break cases where users intentionally changed that ACL. The real fix for this aspect is Stephen Frost's work to not dump built-in ACLs, and that's not going to get back-ported. Per bugs #13804 and #14271. Solution found by David Johnston and later rediscovered by me. Report: <[email protected]> Report: <[email protected]>
1 parent b2148e1 commit 89c30d1

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

src/bin/pg_dump/pg_backup_archiver.c

+11-4
Original file line numberDiff line numberDiff line change
@@ -3205,15 +3205,22 @@ _printTocEntry(ArchiveHandle *AH, TocEntry *te, bool isData, bool acl_pass)
32053205

32063206
/*
32073207
* Avoid dumping the public schema, as it will already be created ...
3208-
* unless we are using --clean mode, in which case it's been deleted and
3209-
* we'd better recreate it. Likewise for its comment, if any.
3208+
* unless we are using --clean mode (and *not* --create mode), in which
3209+
* case we've previously issued a DROP for it so we'd better recreate it.
3210+
*
3211+
* Likewise for its comment, if any. (We could try issuing the COMMENT
3212+
* command anyway; but it'd fail if the restore is done as non-super-user,
3213+
* so let's not.)
3214+
*
3215+
* XXX it looks pretty ugly to hard-wire the public schema like this, but
3216+
* it sits in a sort of no-mans-land between being a system object and a
3217+
* user object, so it really is special in a way.
32103218
*/
3211-
if (!ropt->dropSchema)
3219+
if (!(ropt->dropSchema && !ropt->createDB))
32123220
{
32133221
if (strcmp(te->desc, "SCHEMA") == 0 &&
32143222
strcmp(te->tag, "public") == 0)
32153223
return;
3216-
/* The comment restore would require super-user privs, so avoid it. */
32173224
if (strcmp(te->desc, "COMMENT") == 0 &&
32183225
strcmp(te->tag, "SCHEMA public") == 0)
32193226
return;

0 commit comments

Comments
 (0)