From 5a676b0e0ecdda04facb3fd4327e6b9615f6c8e4 Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <peter_e@gmx.net>
Date: Tue, 23 Aug 2016 12:00:00 -0400
Subject: [PATCH v2 1/2] pg_dump: Separate table and sequence data object types

Instead of handling both sequence data and table data internally as
"table data", handle sequences separately under a "sequence set" type.
We already handled materialized view data differently, so it makes the
code somewhat cleaner to handle each relation kind separately at the top
level.

This does not change the output format, since there already was a
separate "SEQUENCE SET" archive entry type.  A noticeable difference is
that SEQUENCE SET entries now always appear after TABLE DATA entries.
And in parallel mode there is less sorting to do, because the sequence
data entries are no longer considered table data.
---
 src/bin/pg_dump/pg_dump.c      | 11 +++++----
 src/bin/pg_dump/pg_dump.h      |  1 +
 src/bin/pg_dump/pg_dump_sort.c | 51 ++++++++++++++++++++++++------------------
 3 files changed, 37 insertions(+), 26 deletions(-)

diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c
index ba9c276..004499c 100644
--- a/src/bin/pg_dump/pg_dump.c
+++ b/src/bin/pg_dump/pg_dump.c
@@ -2141,6 +2141,8 @@ makeTableDataInfo(DumpOptions *dopt, TableInfo *tbinfo, bool oids)
 
 	if (tbinfo->relkind == RELKIND_MATVIEW)
 		tdinfo->dobj.objType = DO_REFRESH_MATVIEW;
+	else if (tbinfo->relkind == RELKIND_SEQUENCE)
+		tdinfo->dobj.objType = DO_SEQUENCE_SET;
 	else
 		tdinfo->dobj.objType = DO_TABLE_DATA;
 
@@ -9391,11 +9393,11 @@ dumpDumpableObject(Archive *fout, DumpableObject *dobj)
 		case DO_TRANSFORM:
 			dumpTransform(fout, (TransformInfo *) dobj);
 			break;
+		case DO_SEQUENCE_SET:
+			dumpSequenceData(fout, (TableDataInfo *) dobj);
+			break;
 		case DO_TABLE_DATA:
-			if (((TableDataInfo *) dobj)->tdtable->relkind == RELKIND_SEQUENCE)
-				dumpSequenceData(fout, (TableDataInfo *) dobj);
-			else
-				dumpTableData(fout, (TableDataInfo *) dobj);
+			dumpTableData(fout, (TableDataInfo *) dobj);
 			break;
 		case DO_DUMMY_TYPE:
 			/* table rowtypes and array types are never dumped separately */
@@ -17491,6 +17493,7 @@ addBoundaryDependencies(DumpableObject **dobjs, int numObjs,
 				addObjectDependency(preDataBound, dobj->dumpId);
 				break;
 			case DO_TABLE_DATA:
+			case DO_SEQUENCE_SET:
 			case DO_BLOB_DATA:
 				/* Data objects: must come between the boundaries */
 				addObjectDependency(dobj, preDataBound->dumpId);
diff --git a/src/bin/pg_dump/pg_dump.h b/src/bin/pg_dump/pg_dump.h
index 2bfa2d9..a188410 100644
--- a/src/bin/pg_dump/pg_dump.h
+++ b/src/bin/pg_dump/pg_dump.h
@@ -63,6 +63,7 @@ typedef enum
 	DO_PROCLANG,
 	DO_CAST,
 	DO_TABLE_DATA,
+	DO_SEQUENCE_SET,
 	DO_DUMMY_TYPE,
 	DO_TSPARSER,
 	DO_TSDICT,
diff --git a/src/bin/pg_dump/pg_dump_sort.c b/src/bin/pg_dump/pg_dump_sort.c
index d87f08d..8672e06 100644
--- a/src/bin/pg_dump/pg_dump_sort.c
+++ b/src/bin/pg_dump/pg_dump_sort.c
@@ -52,14 +52,15 @@ static const int oldObjectTypePriority[] =
 	5,							/* DO_CONVERSION */
 	6,							/* DO_TABLE */
 	8,							/* DO_ATTRDEF */
-	15,							/* DO_INDEX */
-	16,							/* DO_RULE */
-	17,							/* DO_TRIGGER */
-	14,							/* DO_CONSTRAINT */
-	18,							/* DO_FK_CONSTRAINT */
+	16,							/* DO_INDEX */
+	17,							/* DO_RULE */
+	18,							/* DO_TRIGGER */
+	15,							/* DO_CONSTRAINT */
+	19,							/* DO_FK_CONSTRAINT */
 	2,							/* DO_PROCLANG */
 	2,							/* DO_CAST */
 	11,							/* DO_TABLE_DATA */
+	12,							/* DO_SEQUENCE_SET */
 	7,							/* DO_DUMMY_TYPE */
 	4,							/* DO_TSPARSER */
 	4,							/* DO_TSDICT */
@@ -67,15 +68,15 @@ static const int oldObjectTypePriority[] =
 	4,							/* DO_TSCONFIG */
 	4,							/* DO_FDW */
 	4,							/* DO_FOREIGN_SERVER */
-	19,							/* DO_DEFAULT_ACL */
+	20,							/* DO_DEFAULT_ACL */
 	4,							/* DO_TRANSFORM */
 	9,							/* DO_BLOB */
-	12,							/* DO_BLOB_DATA */
+	13,							/* DO_BLOB_DATA */
 	10,							/* DO_PRE_DATA_BOUNDARY */
-	13,							/* DO_POST_DATA_BOUNDARY */
-	20,							/* DO_EVENT_TRIGGER */
-	15,							/* DO_REFRESH_MATVIEW */
-	21							/* DO_POLICY */
+	14,							/* DO_POST_DATA_BOUNDARY */
+	21,							/* DO_EVENT_TRIGGER */
+	16,							/* DO_REFRESH_MATVIEW */
+	22							/* DO_POLICY */
 };
 
 /*
@@ -103,14 +104,15 @@ static const int newObjectTypePriority[] =
 	11,							/* DO_CONVERSION */
 	18,							/* DO_TABLE */
 	20,							/* DO_ATTRDEF */
-	27,							/* DO_INDEX */
-	28,							/* DO_RULE */
-	29,							/* DO_TRIGGER */
-	26,							/* DO_CONSTRAINT */
-	30,							/* DO_FK_CONSTRAINT */
+	28,							/* DO_INDEX */
+	29,							/* DO_RULE */
+	30,							/* DO_TRIGGER */
+	27,							/* DO_CONSTRAINT */
+	31,							/* DO_FK_CONSTRAINT */
 	2,							/* DO_PROCLANG */
 	10,							/* DO_CAST */
 	23,							/* DO_TABLE_DATA */
+	24,							/* DO_SEQUENCE_SET */
 	19,							/* DO_DUMMY_TYPE */
 	12,							/* DO_TSPARSER */
 	14,							/* DO_TSDICT */
@@ -118,15 +120,15 @@ static const int newObjectTypePriority[] =
 	15,							/* DO_TSCONFIG */
 	16,							/* DO_FDW */
 	17,							/* DO_FOREIGN_SERVER */
-	31,							/* DO_DEFAULT_ACL */
+	32,							/* DO_DEFAULT_ACL */
 	3,							/* DO_TRANSFORM */
 	21,							/* DO_BLOB */
-	24,							/* DO_BLOB_DATA */
+	25,							/* DO_BLOB_DATA */
 	22,							/* DO_PRE_DATA_BOUNDARY */
-	25,							/* DO_POST_DATA_BOUNDARY */
-	32,							/* DO_EVENT_TRIGGER */
-	33,							/* DO_REFRESH_MATVIEW */
-	34							/* DO_POLICY */
+	26,							/* DO_POST_DATA_BOUNDARY */
+	33,							/* DO_EVENT_TRIGGER */
+	34,							/* DO_REFRESH_MATVIEW */
+	35							/* DO_POLICY */
 };
 
 static DumpId preDataBoundId;
@@ -1433,6 +1435,11 @@ describeDumpableObject(DumpableObject *obj, char *buf, int bufsize)
 					 "TABLE DATA %s  (ID %d OID %u)",
 					 obj->name, obj->dumpId, obj->catId.oid);
 			return;
+		case DO_SEQUENCE_SET:
+			snprintf(buf, bufsize,
+					 "SEQUENCE SET %s  (ID %d OID %u)",
+					 obj->name, obj->dumpId, obj->catId.oid);
+			return;
 		case DO_DUMMY_TYPE:
 			snprintf(buf, bufsize,
 					 "DUMMY TYPE %s  (ID %d OID %u)",
-- 
2.10.0

