Received: from makus.postgresql.org (makus.postgresql.org [98.129.198.125]) by mail.postgresql.org (Postfix) with ESMTP id C7A4411F7E64 for ; Tue, 19 Jun 2012 23:34:31 -0300 (ADT) Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1ShAkY-0007ex-CK for pgsql-bugs@postgresql.org; Wed, 20 Jun 2012 02:34:31 +0000 Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.5/8.14.5) with ESMTP id q5K2YFhV014086; Tue, 19 Jun 2012 22:34:16 -0400 (EDT) From: Tom Lane To: Alvaro Herrera cc: Ryan Kelly , joe , Pg Bugs Subject: Re: BUG #6699: pg_restore with -j -- doesn't restore view that groups by primary key In-reply-to: <12801.1340155595@sss.pgh.pa.us> References: <20120619202058.GA7792@llserver.lakeliving.com> <1340139400-sup-7121@alvh.no-ip.org> <22882.1340141854@sss.pgh.pa.us> <12801.1340155595@sss.pgh.pa.us> Comments: In-reply-to Tom Lane message dated "Tue, 19 Jun 2012 21:26:35 -0400" MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" Content-ID: <14065.1340159581.0@sss.pgh.pa.us> Date: Tue, 19 Jun 2012 22:34:15 -0400 Message-ID: <14084.1340159655@sss.pgh.pa.us> X-Pg-Spam-Score: -1.9 (-) X-Archive-Number: 201206/167 X-Sequence-Number: 33394 ------- =_aaaaaaaaaa0 Content-Type: text/plain; charset="us-ascii" Content-ID: <14065.1340159581.1@sss.pgh.pa.us> I wrote: > We could possibly hack something for the special case of rules, but > I don't think this would be the last time we hear about this type of > issue. I'm inclined to think that the best solution would be to add > generic logic to pg_dump that "looks through" any dependency references > to objects that are not going to be dumped, and replaces them with the > IDs of any objects they depend on that are going to be dumped. I wrote a trial patch that does things that way (attached) and it appears to work and solve the problem. However, it's not committable as-is because it breaks the build of pg_restore: ld: Unsatisfied symbols: findObjectByDumpId (code) since findObjectByDumpId is in common.c which isn't linked into pg_restore. I guess a brute force solution would be to link it, but probably we ought to think about refactoring the code to avoid that. I'm not coming up with any very nice ideas about exactly how to refactor, though. Thoughts? regards, tom lane ------- =_aaaaaaaaaa0 Content-Type: text/x-patch; charset="us-ascii" Content-ID: <14065.1340159581.2@sss.pgh.pa.us> Content-Description: expand-dump-dependencies-1.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 aa9c8eed5dafabd49429874d973e22abe93d5294..2db67d6fd3d5616944ff0b086a= a606af11e9eed4 100644 *** a/src/bin/pg_dump/pg_backup_archiver.c --- b/src/bin/pg_dump/pg_backup_archiver.c *************** static void _becomeUser(ArchiveHandle *A *** 129,134 **** --- 129,135 ---- static void _becomeOwner(ArchiveHandle *AH, TocEntry *te); static void _selectOutputSchema(ArchiveHandle *AH, const char *schemaNam= e); static void _selectTablespace(ArchiveHandle *AH, const char *tablespace)= ; + static void writeDependencies(ArchiveHandle *AH, const DumpId *dependenc= ies, int nDeps); static void processEncodingEntry(ArchiveHandle *AH, TocEntry *te); static void processStdStringsEntry(ArchiveHandle *AH, TocEntry *te); static teReqs _tocEntryRequired(TocEntry *te, teSection curSection, Rest= oreOptions *ropt); *************** WriteToc(ArchiveHandle *AH) *** 2144,2150 **** TocEntry *te; char workbuf[32]; int tocCount; - int i; = /* count entries that will actually be dumped */ tocCount =3D 0; --- 2145,2150 ---- *************** WriteToc(ArchiveHandle *AH) *** 2184,2195 **** WriteStr(AH, te->withOids ? "true" : "false"); = /* Dump list of dependencies */ ! for (i =3D 0; i < te->nDeps; i++) ! { ! sprintf(workbuf, "%d", te->dependencies[i]); ! WriteStr(AH, workbuf); ! } ! WriteStr(AH, NULL); /* Terminate List */ = if (AH->WriteExtraTocPtr) (*AH->WriteExtraTocPtr) (AH, te); --- 2184,2191 ---- WriteStr(AH, te->withOids ? "true" : "false"); = /* Dump list of dependencies */ ! writeDependencies(AH, te->dependencies, te->nDeps); ! WriteStr(AH, NULL); /* list terminator */ = if (AH->WriteExtraTocPtr) (*AH->WriteExtraTocPtr) (AH, te); *************** ReadToc(ArchiveHandle *AH) *** 2353,2358 **** --- 2349,2400 ---- } } = + /* + * Dump an object's dependencies in a usable form, ie, referencing only + * objects that are included in the dump. We need to make this distinct= ion + * since, for example, a view will depend on its _RETURN rule while the + * _RETURN rule will depend on other objects --- but the rule will not a= ppear + * as a separate object in the dump. We have to look through the rule t= o get + * a useful representation of the view's dependencies. + * + * We rely here on the assumption that sortDumpableObjects already broke= any + * dependency loops, else we might recurse infinitely. + */ + static void + writeDependencies(ArchiveHandle *AH, const DumpId *dependencies, int nDe= ps) + { + char workbuf[32]; + int i; + = + for (i =3D 0; i < nDeps; i++) + { + DumpId depid =3D dependencies[i]; + TocEntry *depte =3D getTocEntryByDumpId(AH, depid); + = + if (depte && + (depte->reqs & (REQ_SCHEMA | REQ_DATA | REQ_SPECIAL)) !=3D 0) + { + /* Object will be dumped, so just reference it as a dependency */ + /* Dependency IDs are printed as strings for historical reasons */ + sprintf(workbuf, "%d", depid); + WriteStr(AH, workbuf); + } + else + { + /* + * Object will not be dumped, so recursively consider its deps. + * Here we must resort to the underlying DumpableObject data + * structures, since often there will be no TocEntry for such + * an object. + */ + DumpableObject *dobj =3D findObjectByDumpId(depid); + = + if (dobj) + writeDependencies(AH, dobj->dependencies, dobj->nDeps); + } + } + } + = static void processEncodingEntry(ArchiveHandle *AH, TocEntry *te) { ------- =_aaaaaaaaaa0--