Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pIdId-00009t-Dd for pgsql-bugs@arkaria.postgresql.org; Thu, 19 Jan 2023 22:22:23 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pIdIb-00035I-47 for pgsql-bugs@arkaria.postgresql.org; Thu, 19 Jan 2023 22:22:21 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pIdIa-000359-Rv for pgsql-bugs@lists.postgresql.org; Thu, 19 Jan 2023 22:22:20 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pIdIX-0001m6-Qd for pgsql-bugs@lists.postgresql.org; Thu, 19 Jan 2023 22:22:20 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 30JMMF40282373; Thu, 19 Jan 2023 17:22:15 -0500 From: Tom Lane To: David Rowley cc: justin@tonic.ai, pgsql-bugs@lists.postgresql.org Subject: Re: BUG #17753: pg_dump --if-exists bug In-reply-to: <4062761.1674142680@sss.pgh.pa.us> References: <17753-9c8773631747ee1c@postgresql.org> <4062761.1674142680@sss.pgh.pa.us> Comments: In-reply-to Tom Lane message dated "Thu, 19 Jan 2023 10:38:00 -0500" MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" Content-ID: <282231.1674166858.0@sss.pgh.pa.us> Date: Thu, 19 Jan 2023 17:22:15 -0500 Message-ID: <282372.1674166935@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk ------- =_aaaaaaaaaa0 Content-Type: text/plain; charset="us-ascii" Content-ID: <282231.1674166858.1@sss.pgh.pa.us> I wrote: > Noting that that string is only associated with the public schema, > I tried dumping a DB in which the public schema had been dropped > --- no dice --- and then recreated --- and then it reproduces! > Apparently, a schema that's named "public" but isn't the original > one is confusing something somewhere, but I'm not immediately > seeing where. Ah, here's the difference: a schema named "public" will be marked dumpable if its owner is not pg_database_owner: else if (strcmp(nsinfo->dobj.name, "public") == 0) { /* * The public schema is a strange beast that sits in a sort of * no-mans-land between being a system object and a user object. * CREATE SCHEMA would fail, so its DUMP_COMPONENT_DEFINITION is just * a comment and an indication of ownership. If the owner is the * default, omit that superfluous DUMP_COMPONENT_DEFINITION. Before * v15, the default owner was BOOTSTRAP_SUPERUSERID. */ nsinfo->create = false; nsinfo->dobj.dump = DUMP_COMPONENT_ALL; if (nsinfo->nspowner == ROLE_PG_DATABASE_OWNER) nsinfo->dobj.dump &= ~DUMP_COMPONENT_DEFINITION; Having done that, RestoreArchive's kluge to inject IF EXISTS clauses fails, because (unlike other places in pg_backup_archiver.c) it is unaware that the dropStmt might be just a comment. I think we need to do the attached, more or less. Also, although the given case seems to be new, we'd probably better back-patch, because I'm not convinced that there aren't other ways to reach this. The warning message is just cosmetic (the dump output is the same either way), so possibly this has been seen in the field but not reported. regards, tom lane ------- =_aaaaaaaaaa0 Content-Type: text/x-diff; name="fix-dump-if-exists-on-public-schema.patch"; charset="us-ascii" Content-ID: <282231.1674166858.2@sss.pgh.pa.us> Content-Description: fix-dump-if-exists-on-public-schema.patch Content-Transfer-Encoding: quoted-printable diff --git a/src/bin/pg_dump/pg_backup_archiver.c b/src/bin/pg_dump/pg_bac= kup_archiver.c index 7f7a0f1ce7..ba5e6acbbb 100644 --- a/src/bin/pg_dump/pg_backup_archiver.c +++ b/src/bin/pg_dump/pg_backup_archiver.c @@ -538,9 +538,14 @@ RestoreArchive(Archive *AHX) */ if (*te->dropStmt !=3D '\0') { - if (!ropt->if_exists) + if (!ropt->if_exists || + strncmp(te->dropStmt, "--", 2) =3D=3D 0) { - /* No --if-exists? Then just use the original */ + /* + * Without --if-exists, or if it's just a comment (as + * happens for the public schema), print the dropStmt + * as-is. + */ ahprintf(AH, "%s", te->dropStmt); } else ------- =_aaaaaaaaaa0--