Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1c7Cwb-0001uJ-7P for pgsql-hackers@arkaria.postgresql.org; Thu, 17 Nov 2016 03:00:57 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1c7Cwa-0005Wf-NI for pgsql-hackers@arkaria.postgresql.org; Thu, 17 Nov 2016 03:00:56 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1c7CwY-0005TZ-St for pgsql-hackers@postgreSQL.org; Thu, 17 Nov 2016 03:00:54 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1c7CwV-0007P8-At for pgsql-hackers@postgreSQL.org; Thu, 17 Nov 2016 03:00:54 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id uAH30nkC029255 for ; Wed, 16 Nov 2016 22:00:49 -0500 From: Tom Lane To: pgsql-hackers@postgreSQL.org Subject: Re: pg_dump versus rules, once again In-reply-to: <19092.1479325184@sss.pgh.pa.us> References: <19092.1479325184@sss.pgh.pa.us> Comments: In-reply-to Tom Lane message dated "Wed, 16 Nov 2016 14:39:44 -0500" MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" Content-ID: <29184.1479351574.0@sss.pgh.pa.us> Date: Wed, 16 Nov 2016 22:00:49 -0500 Message-ID: <29254.1479351649@sss.pgh.pa.us> X-Pg-Spam-Score: -4.8 (----) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-hackers Precedence: bulk Sender: pgsql-hackers-owner@postgresql.org ------- =_aaaaaaaaaa0 Content-Type: text/plain; charset="us-ascii" Content-ID: <29184.1479351574.1@sss.pgh.pa.us> I wrote: > We've talked before about replacing this _RETURN-rule business with > CREATE OR REPLACE VIEW, ie the idea would be for pg_dump to first emit > a dummy view with the right column names/types, say > CREATE VIEW vv AS SELECT null::int AS f1, null::text AS f2; > and then later when it's safe, emit CREATE OR REPLACE VIEW with the view's > real query. Here's a proposed patch for that. It turns out there are some other kluges that can be gotten rid of while we're at it: no longer need the idea of reloptions being attached to a rule, for instance. The changes in pg_backup_archiver.c would have to be back-patched into all versions supporting --if-exists, so that they don't fail on dump archives produced by patched versions. We could possibly put the rest only into HEAD; I remain of two minds about that. regards, tom lane ------- =_aaaaaaaaaa0 Content-Type: text/x-diff; name="dump-view-fix.patch"; charset="us-ascii" Content-ID: <29184.1479351574.2@sss.pgh.pa.us> Content-Description: dump-view-fix.patch Content-Transfer-Encoding: quoted-printable diff --git a/src/bin/pg_dump/pg_backup_archiver.c b/src/bin/pg_dump/pg_back= up_archiver.c index b938d79..b89bd99 100644 *** a/src/bin/pg_dump/pg_backup_archiver.c --- b/src/bin/pg_dump/pg_backup_archiver.c *************** RestoreArchive(Archive *AHX) *** 521,527 **** * knows how to do it, without depending on * te->dropStmt; use that. For other objects we need * to parse the command. - * */ if (strncmp(te->desc, "BLOB", 4) =3D=3D 0) { --- 521,526 ---- *************** RestoreArchive(Archive *AHX) *** 529,538 **** } else { - char buffer[40]; - char *mark; char *dropStmt =3D pg_strdup(te->dropStmt); ! char *dropStmtPtr =3D dropStmt; PQExpBuffer ftStmt =3D createPQExpBuffer(); =20=20 /* --- 528,535 ---- } else { char *dropStmt =3D pg_strdup(te->dropStmt); ! char *dropStmtOrig =3D dropStmt; PQExpBuffer ftStmt =3D createPQExpBuffer(); =20=20 /* *************** RestoreArchive(Archive *AHX) *** 549,566 **** /* * ALTER TABLE..ALTER COLUMN..DROP DEFAULT does * not support the IF EXISTS clause, and therefore ! * we simply emit the original command for such ! * objects. For other objects, we need to extract ! * the first part of the DROP which includes the ! * object type. Most of the time this matches * te->desc, so search for that; however for the * different kinds of CONSTRAINTs, we know to * search for hardcoded "DROP CONSTRAINT" instead. */ ! if (strcmp(te->desc, "DEFAULT") =3D=3D 0) appendPQExpBufferStr(ftStmt, dropStmt); else { if (strcmp(te->desc, "CONSTRAINT") =3D=3D 0 || strcmp(te->desc, "CHECK CONSTRAINT") =3D=3D 0 || strcmp(te->desc, "FK CONSTRAINT") =3D=3D 0) --- 546,573 ---- /* * ALTER TABLE..ALTER COLUMN..DROP DEFAULT does * not support the IF EXISTS clause, and therefore ! * we simply emit the original command for DEFAULT ! * objects (modulo the adjustment made above). ! * ! * If we used CREATE OR REPLACE VIEW as a means of ! * quasi-dropping an ON SELECT rule, that should ! * be emitted unchanged as well. ! * ! * For other object types, we need to extract the ! * first part of the DROP which includes the ! * object type. Most of the time this matches * te->desc, so search for that; however for the * different kinds of CONSTRAINTs, we know to * search for hardcoded "DROP CONSTRAINT" instead. */ ! if (strcmp(te->desc, "DEFAULT") =3D=3D 0 || ! strncmp(dropStmt, "CREATE OR REPLACE VIEW", 22) =3D=3D 0) appendPQExpBufferStr(ftStmt, dropStmt); else { + char buffer[40]; + char *mark; +=20 if (strcmp(te->desc, "CONSTRAINT") =3D=3D 0 || strcmp(te->desc, "CHECK CONSTRAINT") =3D=3D 0 || strcmp(te->desc, "FK CONSTRAINT") =3D=3D 0) *************** RestoreArchive(Archive *AHX) *** 570,588 **** te->desc); =20=20 mark =3D strstr(dropStmt, buffer); - Assert(mark !=3D NULL); =20=20 ! *mark =3D '\0'; ! appendPQExpBuffer(ftStmt, "%s%s IF EXISTS%s", ! dropStmt, buffer, ! mark + strlen(buffer)); } =20=20 ahprintf(AH, "%s", ftStmt->data); =20=20 destroyPQExpBuffer(ftStmt); !=20 ! pg_free(dropStmtPtr); } } } --- 577,604 ---- te->desc); =20=20 mark =3D strstr(dropStmt, buffer); =20=20 ! if (mark) ! { ! *mark =3D '\0'; ! appendPQExpBuffer(ftStmt, "%s%s IF EXISTS%s", ! dropStmt, buffer, ! mark + strlen(buffer)); ! } ! else ! { ! /* complain and emit unmodified command */ ! write_msg(modulename, ! "WARNING: could not find where to insert IF EXISTS in statem= ent \"%s\"\n", ! dropStmtOrig); ! appendPQExpBufferStr(ftStmt, dropStmt); ! } } =20=20 ahprintf(AH, "%s", ftStmt->data); =20=20 destroyPQExpBuffer(ftStmt); ! pg_free(dropStmtOrig); } } } diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c index ee1f673..9f59f53 100644 *** a/src/bin/pg_dump/pg_dump.c --- b/src/bin/pg_dump/pg_dump.c *************** getTables(Archive *fout, int *numTables) *** 5484,5490 **** tblinfo[i].dobj.dump &=3D ~DUMP_COMPONENT_ACL; =20=20 tblinfo[i].interesting =3D tblinfo[i].dobj.dump ? true : false; !=20 tblinfo[i].postponed_def =3D false; /* might get set during sort */ =20=20 /* --- 5486,5492 ---- tblinfo[i].dobj.dump &=3D ~DUMP_COMPONENT_ACL; =20=20 tblinfo[i].interesting =3D tblinfo[i].dobj.dump ? true : false; ! tblinfo[i].dummy_view =3D false; /* might get set during sort */ tblinfo[i].postponed_def =3D false; /* might get set during sort */ =20=20 /* *************** getRules(Archive *fout, int *numRules) *** 6205,6220 **** } else ruleinfo[i].separate =3D true; -=20 - /* - * If we're forced to break a dependency loop by dumping a view as a - * table and separate _RETURN rule, we'll move the view's reloptions - * to the rule. (This is necessary because tables and views have - * different valid reloptions, so we can't apply the options until the - * backend knows it's a view.) Otherwise the rule's reloptions stay - * NULL. - */ - ruleinfo[i].reloptions =3D NULL; } =20=20 PQclear(res); --- 6207,6212 ---- *************** createViewAsClause(Archive *fout, TableI *** 13976,13981 **** --- 13968,14021 ---- } =20=20 /* + * Create a dummy AS clause for a view. This is used when the real view + * definition has to be postponed because of circular dependencies. + * We must duplicate the view's external properties -- column names and t= ypes + * (including collation) -- so that it works for subsequent references. + * + * This returns a new buffer which must be freed by the caller. + */ + static PQExpBuffer + createDummyViewAsClause(Archive *fout, TableInfo *tbinfo) + { + PQExpBuffer result =3D createPQExpBuffer(); + int j; +=20 + appendPQExpBufferStr(result, "SELECT"); +=20 + for (j =3D 0; j < tbinfo->numatts; j++) + { + if (j > 0) + appendPQExpBufferChar(result, ','); + appendPQExpBufferStr(result, "\n "); +=20 + appendPQExpBuffer(result, "NULL::%s", tbinfo->atttypnames[j]); +=20 + /* + * Must add collation if not default for the type, because CREATE OR + * REPLACE VIEW won't change it + */ + if (OidIsValid(tbinfo->attcollation[j])) + { + CollInfo *coll; +=20 + coll =3D findCollationByOid(tbinfo->attcollation[j]); + if (coll) + { + /* always schema-qualify, don't try to be smart */ + appendPQExpBuffer(result, " COLLATE %s.", + fmtId(coll->dobj.namespace->dobj.name)); + appendPQExpBufferStr(result, fmtId(coll->dobj.name)); + } + } +=20 + appendPQExpBuffer(result, " AS %s", fmtId(tbinfo->attnames[j])); + } +=20 + return result; + } +=20 + /* * dumpTableSchema * write the declaration (not data) of one user-defined table or view */ *************** dumpTableSchema(Archive *fout, TableInfo *** 14008,14013 **** --- 14048,14057 ---- { PQExpBuffer result; =20=20 + /* + * Note: keep this code in sync with the is_view case in dumpRule() + */ +=20 reltypename =3D "VIEW"; =20=20 /* *************** dumpTableSchema(Archive *fout, TableInfo *** 14024,14040 **** tbinfo->dobj.catId.oid, false); =20=20 appendPQExpBuffer(q, "CREATE VIEW %s", fmtId(tbinfo->dobj.name)); ! if (nonemptyReloptions(tbinfo->reloptions)) { ! appendPQExpBufferStr(q, " WITH ("); ! appendReloptionsArrayAH(q, tbinfo->reloptions, "", fout); ! appendPQExpBufferChar(q, ')'); } - result =3D createViewAsClause(fout, tbinfo); appendPQExpBuffer(q, " AS\n%s", result->data); destroyPQExpBuffer(result); =20=20 ! if (tbinfo->checkoption !=3D NULL) appendPQExpBuffer(q, "\n WITH %s CHECK OPTION", tbinfo->checkoption); appendPQExpBufferStr(q, ";\n"); =20=20 --- 14068,14089 ---- tbinfo->dobj.catId.oid, false); =20=20 appendPQExpBuffer(q, "CREATE VIEW %s", fmtId(tbinfo->dobj.name)); ! if (tbinfo->dummy_view) ! result =3D createDummyViewAsClause(fout, tbinfo); ! else { ! if (nonemptyReloptions(tbinfo->reloptions)) ! { ! appendPQExpBufferStr(q, " WITH ("); ! appendReloptionsArrayAH(q, tbinfo->reloptions, "", fout); ! appendPQExpBufferChar(q, ')'); ! } ! result =3D createViewAsClause(fout, tbinfo); } appendPQExpBuffer(q, " AS\n%s", result->data); destroyPQExpBuffer(result); =20=20 ! if (tbinfo->checkoption !=3D NULL && !tbinfo->dummy_view) appendPQExpBuffer(q, "\n WITH %s CHECK OPTION", tbinfo->checkoption); appendPQExpBufferStr(q, ";\n"); =20=20 *************** dumpRule(Archive *fout, RuleInfo *rinfo) *** 15646,15651 **** --- 15695,15701 ---- { DumpOptions *dopt =3D fout->dopt; TableInfo *tbinfo =3D rinfo->ruletable; + bool is_view; PQExpBuffer query; PQExpBuffer cmd; PQExpBuffer delcmd; *************** dumpRule(Archive *fout, RuleInfo *rinfo) *** 15665,15670 **** --- 15715,15726 ---- return; =20=20 /* + * If it's an ON SELECT rule, we want to print it as a view definition, + * instead of a rule. + */ + is_view =3D (rinfo->ev_type =3D=3D '1' && rinfo->is_instead); +=20 + /* * Make sure we are in proper schema. */ selectSourceSchema(fout, tbinfo->dobj.namespace->dobj.name); *************** dumpRule(Archive *fout, RuleInfo *rinfo) *** 15674,15693 **** delcmd =3D createPQExpBuffer(); labelq =3D createPQExpBuffer(); =20=20 ! appendPQExpBuffer(query, ! "SELECT pg_catalog.pg_get_ruledef('%u'::pg_catalog.oid) AS definition", ! rinfo->dobj.catId.oid); !=20 ! res =3D ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); !=20 ! if (PQntuples(res) !=3D 1) { ! write_msg(NULL, "query to get rule \"%s\" for table \"%s\" failed: wron= g number of rows returned\n", ! rinfo->dobj.name, tbinfo->dobj.name); ! exit_nicely(1); } =20=20 ! printfPQExpBuffer(cmd, "%s\n", PQgetvalue(res, 0, 0)); =20=20 /* * Add the command to alter the rules replication firing semantics if it --- 15730,15779 ---- delcmd =3D createPQExpBuffer(); labelq =3D createPQExpBuffer(); =20=20 ! if (is_view) { ! PQExpBuffer result; !=20 ! /* ! * We need OR REPLACE here because we'll be replacing a dummy view. ! * Otherwise this should look largely like the regular view dump code. ! */ ! appendPQExpBuffer(cmd, "CREATE OR REPLACE VIEW %s", ! fmtId(tbinfo->dobj.name)); ! if (nonemptyReloptions(tbinfo->reloptions)) ! { ! appendPQExpBufferStr(cmd, " WITH ("); ! appendReloptionsArrayAH(cmd, tbinfo->reloptions, "", fout); ! appendPQExpBufferChar(cmd, ')'); ! } ! result =3D createViewAsClause(fout, tbinfo); ! appendPQExpBuffer(cmd, " AS\n%s", result->data); ! destroyPQExpBuffer(result); ! if (tbinfo->checkoption !=3D NULL) ! appendPQExpBuffer(cmd, "\n WITH %s CHECK OPTION", ! tbinfo->checkoption); ! appendPQExpBufferStr(cmd, ";\n"); } + else + { + /* In the rule case, just print pg_get_ruledef's result verbatim */ + appendPQExpBuffer(query, + "SELECT pg_catalog.pg_get_ruledef('%u'::pg_catalog.oid)", + rinfo->dobj.catId.oid); =20=20 ! res =3D ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); !=20 ! if (PQntuples(res) !=3D 1) ! { ! write_msg(NULL, "query to get rule \"%s\" for table \"%s\" failed: wro= ng number of rows returned\n", ! rinfo->dobj.name, tbinfo->dobj.name); ! exit_nicely(1); ! } !=20 ! printfPQExpBuffer(cmd, "%s\n", PQgetvalue(res, 0, 0)); !=20 ! PQclear(res); ! } =20=20 /* * Add the command to alter the rules replication firing semantics if it *************** dumpRule(Archive *fout, RuleInfo *rinfo) *** 15714,15738 **** } =20=20 /* ! * Apply view's reloptions when its ON SELECT rule is separate. */ ! if (nonemptyReloptions(rinfo->reloptions)) { ! appendPQExpBuffer(cmd, "ALTER VIEW %s SET (", fmtId(tbinfo->dobj.name)); - appendReloptionsArrayAH(cmd, rinfo->reloptions, "", fout); - appendPQExpBufferStr(cmd, ");\n"); } -=20 - /* - * DROP must be fully qualified in case same name appears in pg_catalog - */ - appendPQExpBuffer(delcmd, "DROP RULE %s ", - fmtId(rinfo->dobj.name)); - appendPQExpBuffer(delcmd, "ON %s.", - fmtId(tbinfo->dobj.namespace->dobj.name)); - appendPQExpBuffer(delcmd, "%s;\n", - fmtId(tbinfo->dobj.name)); =20=20 appendPQExpBuffer(labelq, "RULE %s", fmtId(rinfo->dobj.name)); --- 15800,15833 ---- } =20=20 /* ! * DROP must be fully qualified in case same name appears in pg_catalog */ ! if (is_view) { ! /* ! * We can't DROP a view's ON SELECT rule. Instead, use CREATE OR ! * REPLACE VIEW to replace the rule with something with minimal ! * dependencies. ! */ ! PQExpBuffer result; !=20 ! appendPQExpBuffer(delcmd, "CREATE OR REPLACE VIEW %s.", ! fmtId(tbinfo->dobj.namespace->dobj.name)); ! appendPQExpBuffer(delcmd, "%s", ! fmtId(tbinfo->dobj.name)); ! result =3D createDummyViewAsClause(fout, tbinfo); ! appendPQExpBuffer(delcmd, " AS\n%s;\n", result->data); ! destroyPQExpBuffer(result); ! } ! else ! { ! appendPQExpBuffer(delcmd, "DROP RULE %s ", ! fmtId(rinfo->dobj.name)); ! appendPQExpBuffer(delcmd, "ON %s.", ! fmtId(tbinfo->dobj.namespace->dobj.name)); ! appendPQExpBuffer(delcmd, "%s;\n", fmtId(tbinfo->dobj.name)); } =20=20 appendPQExpBuffer(labelq, "RULE %s", fmtId(rinfo->dobj.name)); *************** dumpRule(Archive *fout, RuleInfo *rinfo) *** 15759,15766 **** tbinfo->rolname, rinfo->dobj.catId, 0, rinfo->dobj.dumpId); =20=20 - PQclear(res); -=20 free(tag); destroyPQExpBuffer(query); destroyPQExpBuffer(cmd); --- 15854,15859 ---- diff --git a/src/bin/pg_dump/pg_dump.h b/src/bin/pg_dump/pg_dump.h index 642c4d5..f3e5977 100644 *** a/src/bin/pg_dump/pg_dump.h --- b/src/bin/pg_dump/pg_dump.h *************** typedef struct _tableInfo *** 287,292 **** --- 287,293 ---- int relpages; /* table's size in pages (from pg_class) */ =20=20 bool interesting; /* true if need to collect more data */ + bool dummy_view; /* view's real definition must be postponed */ bool postponed_def; /* matview must be postponed into post-data */ =20=20 /* *************** typedef struct _ruleInfo *** 364,371 **** char ev_enabled; bool separate; /* TRUE if must dump as separate item */ /* separate is always true for non-ON SELECT rules */ - char *reloptions; /* options specified by WITH (...) */ - /* reloptions is only set if we need to dump the options with the rule */ } RuleInfo; =20=20 typedef struct _triggerInfo --- 365,370 ---- diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c index 5b96334..9966423 100644 *** a/src/bin/pg_dump/pg_dump_sort.c --- b/src/bin/pg_dump/pg_dump_sort.c *************** repairViewRuleLoop(DumpableObject *viewo *** 786,791 **** --- 786,792 ---- { /* remove rule's dependency on view */ removeObjectDependency(ruleobj, viewobj->dumpId); + /* flags on the two objects are already set correctly for this case */ } =20=20 /* *************** repairViewRuleMultiLoop(DumpableObject * *** 805,831 **** { TableInfo *viewinfo =3D (TableInfo *) viewobj; RuleInfo *ruleinfo =3D (RuleInfo *) ruleobj; - int i; =20=20 /* remove view's dependency on rule */ removeObjectDependency(viewobj, ruleobj->dumpId); ! /* pretend view is a plain table and dump it that way */ ! viewinfo->relkind =3D 'r'; /* RELKIND_RELATION */ /* mark rule as needing its own dump */ ruleinfo->separate =3D true; - /* move any reloptions from view to rule */ - if (viewinfo->reloptions) - { - ruleinfo->reloptions =3D viewinfo->reloptions; - viewinfo->reloptions =3D NULL; - } /* put back rule's dependency on view */ addObjectDependency(ruleobj, viewobj->dumpId); /* now that rule is separate, it must be post-data */ addObjectDependency(ruleobj, postDataBoundId); - /* also, any triggers on the view must be dumped after the rule */ - for (i =3D 0; i < viewinfo->numTriggers; i++) - addObjectDependency(&(viewinfo->triggers[i].dobj), ruleobj->dumpId); } =20=20 /* --- 806,822 ---- { TableInfo *viewinfo =3D (TableInfo *) viewobj; RuleInfo *ruleinfo =3D (RuleInfo *) ruleobj; =20=20 /* remove view's dependency on rule */ removeObjectDependency(viewobj, ruleobj->dumpId); ! /* mark view to be printed with a dummy definition */ ! viewinfo->dummy_view =3D true; /* mark rule as needing its own dump */ ruleinfo->separate =3D true; /* put back rule's dependency on view */ addObjectDependency(ruleobj, viewobj->dumpId); /* now that rule is separate, it must be post-data */ addObjectDependency(ruleobj, postDataBoundId); } =20=20 /* ------- =_aaaaaaaaaa0 Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers ------- =_aaaaaaaaaa0--