From 209a8d63df404b4c866a3ea430eea05f4d36ebc0 Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Thu, 28 Aug 2003 20:21:34 +0000 Subject: [PATCH] pg_dump and pg_restore -r had managed to diverge on the ordering of different object types. Fix, and centralize logic to try to prevent the same mistake in future. --- src/bin/pg_dump/pg_backup.h | 7 ++-- src/bin/pg_dump/pg_backup_archiver.c | 53 ++++++++++++++++++++++++---- src/bin/pg_dump/pg_dump.c | 30 ++-------------- src/bin/pg_dump/pg_restore.c | 16 +++------ 4 files changed, 59 insertions(+), 47 deletions(-) diff --git a/src/bin/pg_dump/pg_backup.h b/src/bin/pg_dump/pg_backup.h index 5226255f07..5bcb1b2622 100644 --- a/src/bin/pg_dump/pg_backup.h +++ b/src/bin/pg_dump/pg_backup.h @@ -15,7 +15,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_backup.h,v 1.24 2002/09/04 20:31:34 momjian Exp $ + * $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_backup.h,v 1.25 2003/08/28 20:21:34 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -166,8 +166,9 @@ extern void PrintTOCSummary(Archive *AH, RestoreOptions *ropt); extern RestoreOptions *NewRestoreOptions(void); /* Rearrange TOC entries */ -extern void MoveToStart(Archive *AH, char *oType); -extern void MoveToEnd(Archive *AH, char *oType); +extern void MoveToStart(Archive *AH, const char *oType); +extern void MoveToEnd(Archive *AH, const char *oType); +extern void SortTocByObjectType(Archive *AH); extern void SortTocByOID(Archive *AH); extern void SortTocByID(Archive *AH); extern void SortTocFromFile(Archive *AH, RestoreOptions *ropt); diff --git a/src/bin/pg_dump/pg_backup_archiver.c b/src/bin/pg_dump/pg_backup_archiver.c index 294d92682c..6e5914c96e 100644 --- a/src/bin/pg_dump/pg_backup_archiver.c +++ b/src/bin/pg_dump/pg_backup_archiver.c @@ -15,7 +15,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_backup_archiver.c,v 1.74 2003/08/04 00:43:27 momjian Exp $ + * $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_backup_archiver.c,v 1.75 2003/08/28 20:21:34 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -845,11 +845,12 @@ EndRestoreBlob(ArchiveHandle *AH, Oid oid) /* * Move TOC entries of the specified type to the START of the TOC. + * + * This is public, but if you use it anywhere but SortTocByObjectType, + * you are risking breaking things. */ - -/* Public */ void -MoveToStart(Archive *AHX, char *oType) +MoveToStart(Archive *AHX, const char *oType) { ArchiveHandle *AH = (ArchiveHandle *) AHX; TocEntry *te = AH->toc->next; @@ -874,10 +875,12 @@ MoveToStart(Archive *AHX, char *oType) /* * Move TOC entries of the specified type to the end of the TOC. + * + * This is public, but if you use it anywhere but SortTocByObjectType, + * you are risking breaking things. */ -/* Public */ void -MoveToEnd(Archive *AHX, char *oType) +MoveToEnd(Archive *AHX, const char *oType) { ArchiveHandle *AH = (ArchiveHandle *) AHX; TocEntry *te = AH->toc->next; @@ -899,6 +902,44 @@ MoveToEnd(Archive *AHX, char *oType) } } +/* + * Sort TOC by object type (items of same type keep same relative order) + * + * This is factored out to ensure that pg_dump and pg_restore stay in sync + * about the standard ordering. + */ +void +SortTocByObjectType(Archive *AH) +{ + /* + * Procedural languages have to be declared just after database and + * schema creation, before they are used. + */ + MoveToStart(AH, "ACL LANGUAGE"); + MoveToStart(AH, "PROCEDURAL LANGUAGE"); + MoveToStart(AH, "FUNC PROCEDURAL LANGUAGE"); + MoveToStart(AH, "SCHEMA"); + MoveToStart(AH, ""); + /* Database entries *must* be at front (see also pg_restore.c) */ + MoveToStart(AH, "DATABASE"); + + MoveToEnd(AH, "TABLE DATA"); + MoveToEnd(AH, "BLOBS"); + MoveToEnd(AH, "INDEX"); + MoveToEnd(AH, "CONSTRAINT"); + MoveToEnd(AH, "FK CONSTRAINT"); + MoveToEnd(AH, "TRIGGER"); + MoveToEnd(AH, "RULE"); + MoveToEnd(AH, "SEQUENCE SET"); + + /* + * Moving all comments to end is annoying, but must do it for comments + * on stuff we just moved, and we don't seem to have quite enough + * dependency structure to get it really right... + */ + MoveToEnd(AH, "COMMENT"); +} + /* * Sort TOC by OID */ diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c index 6c0ef4f35e..921db3e10b 100644 --- a/src/bin/pg_dump/pg_dump.c +++ b/src/bin/pg_dump/pg_dump.c @@ -12,7 +12,7 @@ * by PostgreSQL * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.345 2003/08/28 18:59:06 tgl Exp $ + * $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.346 2003/08/28 20:21:34 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -572,33 +572,9 @@ main(int argc, char **argv) dumpRules(g_fout, tblinfo, numTables); } - /* Now sort the output nicely */ + /* Now sort the output nicely: by OID within object types */ SortTocByOID(g_fout); - - /* - * Procedural languages have to be declared just after database and - * schema creation, before they are used. - */ - MoveToStart(g_fout, "ACL LANGUAGE"); - MoveToStart(g_fout, "PROCEDURAL LANGUAGE"); - MoveToStart(g_fout, "FUNC PROCEDURAL LANGUAGE"); - MoveToStart(g_fout, "SCHEMA"); - MoveToStart(g_fout, "DATABASE"); - MoveToEnd(g_fout, "TABLE DATA"); - MoveToEnd(g_fout, "BLOBS"); - MoveToEnd(g_fout, "INDEX"); - MoveToEnd(g_fout, "CONSTRAINT"); - MoveToEnd(g_fout, "FK CONSTRAINT"); - MoveToEnd(g_fout, "TRIGGER"); - MoveToEnd(g_fout, "RULE"); - MoveToEnd(g_fout, "SEQUENCE SET"); - - /* - * Moving all comments to end is annoying, but must do it for comments - * on stuff we just moved, and we don't seem to have quite enough - * dependency structure to get it really right... - */ - MoveToEnd(g_fout, "COMMENT"); + SortTocByObjectType(g_fout); if (plainText) { diff --git a/src/bin/pg_dump/pg_restore.c b/src/bin/pg_dump/pg_restore.c index f9217f2c09..e2726b8817 100644 --- a/src/bin/pg_dump/pg_restore.c +++ b/src/bin/pg_dump/pg_restore.c @@ -34,7 +34,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_restore.c,v 1.50 2003/08/07 21:11:58 tgl Exp $ + * $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_restore.c,v 1.51 2003/08/28 20:21:34 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -335,19 +335,13 @@ main(int argc, char **argv) SortTocByID(AH); if (opts->rearrange) + SortTocByObjectType(AH); + else { - MoveToStart(AH, ""); - MoveToEnd(AH, "TABLE DATA"); - MoveToEnd(AH, "BLOBS"); - MoveToEnd(AH, "INDEX"); - MoveToEnd(AH, "TRIGGER"); - MoveToEnd(AH, "RULE"); - MoveToEnd(AH, "SEQUENCE SET"); + /* Database MUST be at start (see also SortTocByObjectType) */ + MoveToStart(AH, "DATABASE"); } - /* Database MUST be at start */ - MoveToStart(AH, "DATABASE"); - if (opts->tocSummary) PrintTOCSummary(AH, opts); else