Skip to content

Commit ce495f8

Browse files
committed
- Dump relevant parts of sequences only when doing schemaOnly & dataOnly
- Prevent double-dumping of sequences when dataOnly.
1 parent aa28eeb commit ce495f8

File tree

1 file changed

+39
-47
lines changed

1 file changed

+39
-47
lines changed

src/bin/pg_dump/pg_dump.c

Lines changed: 39 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
*
2323
*
2424
* IDENTIFICATION
25-
* $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.193 2001/02/18 18:33:59 momjian Exp $
25+
* $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.194 2001/03/06 04:53:28 pjw Exp $
2626
*
2727
* Modifications - 6/10/96 - dave@bensoft.com - version 1.13.dhb
2828
*
@@ -106,6 +106,8 @@
106106
* - Fix help output: replace 'f' with 't' and change desc.
107107
* - Add extra arg to formatStringLiteral to specify how to handle LF & TAB.
108108
* I opted for encoding them except in procedure bodies.
109+
* - Dump relevant parts of sequences only when doing schemaOnly & dataOnly
110+
* - Prevent double-dumping of sequences when dataOnly.
109111
*
110112
*-------------------------------------------------------------------------
111113
*/
@@ -156,7 +158,7 @@ typedef enum _formatLiteralOptions {
156158
} formatLiteralOptions;
157159

158160
static void dumpComment(Archive *outfile, const char *target, const char *oid);
159-
static void dumpSequence(Archive *fout, TableInfo tbinfo);
161+
static void dumpSequence(Archive *fout, TableInfo tbinfo, const bool schemaOnly, const bool dataOnly);
160162
static void dumpACL(Archive *fout, TableInfo tbinfo);
161163
static void dumpTriggers(Archive *fout, const char *tablename,
162164
TableInfo *tblinfo, int numTables);
@@ -608,24 +610,6 @@ dumpClasses(const TableInfo *tblinfo, const int numTables, Archive *fout,
608610
(onlytable == NULL || (strlen(onlytable) == 0)) ? "s" : "",
609611
g_comment_end);
610612

611-
/* Dump SEQUENCEs first (if dataOnly) */
612-
if (dataOnly)
613-
{
614-
for (i = 0; i < numTables; i++)
615-
{
616-
if (!(tblinfo[i].sequence))
617-
continue;
618-
if (!onlytable || (strcmp(tblinfo[i].relname, onlytable) == 0) || (strlen(onlytable) == 0) )
619-
{
620-
if (g_verbose)
621-
fprintf(stderr, "%s dumping out schema of sequence '%s' %s\n",
622-
g_comment_start, tblinfo[i].relname, g_comment_end);
623-
/* becomeUser(fout, tblinfo[i].usename); */
624-
dumpSequence(fout, tblinfo[i]);
625-
}
626-
}
627-
}
628-
629613
for (i = 0; i < numTables; i++)
630614
{
631615
const char *classname = tblinfo[i].relname;
@@ -3730,7 +3714,7 @@ dumpTables(Archive *fout, TableInfo *tblinfo, int numTables,
37303714
|| (serialSeq && !strcmp(tblinfo[i].relname, serialSeq)))
37313715
{
37323716
/* becomeUser(fout, tblinfo[i].usename); */
3733-
dumpSequence(fout, tblinfo[i]);
3717+
dumpSequence(fout, tblinfo[i], schemaOnly, dataOnly);
37343718
if (!aclsSkip)
37353719
dumpACL(fout, tblinfo[i]);
37363720
}
@@ -4314,7 +4298,7 @@ findLastBuiltinOid(const char* dbname)
43144298

43154299

43164300
static void
4317-
dumpSequence(Archive *fout, TableInfo tbinfo)
4301+
dumpSequence(Archive *fout, TableInfo tbinfo, const bool schemaOnly, const bool dataOnly)
43184302
{
43194303
PGresult *res;
43204304
int4 last,
@@ -4367,44 +4351,52 @@ dumpSequence(Archive *fout, TableInfo tbinfo)
43674351
t = PQgetvalue(res, 0, 7);
43684352
called = *t;
43694353

4370-
PQclear(res);
4371-
4372-
resetPQExpBuffer(delqry);
4373-
appendPQExpBuffer(delqry, "DROP SEQUENCE %s;\n", fmtId(tbinfo.relname, force_quotes));
4374-
43754354
/*
43764355
* The logic we use for restoring sequences is as follows:
43774356
* - Add a basic CREATE SEQUENCE statement
43784357
* (use last_val for start if called == 'f', else use min_val for start_val).
43794358
* - Add a 'SETVAL(seq, last_val, iscalled)' at restore-time iff we load data
43804359
*/
4381-
resetPQExpBuffer(query);
4382-
appendPQExpBuffer(query,
4383-
"CREATE SEQUENCE %s start %d increment %d maxvalue %d "
4384-
"minvalue %d cache %d %s;\n",
4385-
fmtId(tbinfo.relname, force_quotes),
4386-
(called == 't') ? minv : last,
4387-
incby, maxv, minv, cache,
4388-
(cycled == 't') ? "cycle" : "");
43894360

4390-
ArchiveEntry(fout, tbinfo.oid, fmtId(tbinfo.relname, force_quotes), "SEQUENCE", NULL,
4391-
query->data, delqry->data, "", tbinfo.usename, NULL, NULL);
4361+
if (!dataOnly)
4362+
{
4363+
PQclear(res);
43924364

4365+
resetPQExpBuffer(delqry);
4366+
appendPQExpBuffer(delqry, "DROP SEQUENCE %s;\n", fmtId(tbinfo.relname, force_quotes));
43934367

4394-
resetPQExpBuffer(query);
4395-
appendPQExpBuffer(query, "SELECT setval (");
4396-
formatStringLiteral(query, fmtId(tbinfo.relname, force_quotes), CONV_ALL);
4397-
appendPQExpBuffer(query, ", %d, '%c');\n", last, called);
4368+
resetPQExpBuffer(query);
4369+
appendPQExpBuffer(query,
4370+
"CREATE SEQUENCE %s start %d increment %d maxvalue %d "
4371+
"minvalue %d cache %d %s;\n",
4372+
fmtId(tbinfo.relname, force_quotes),
4373+
(called == 't') ? minv : last,
4374+
incby, maxv, minv, cache,
4375+
(cycled == 't') ? "cycle" : "");
4376+
4377+
ArchiveEntry(fout, tbinfo.oid, fmtId(tbinfo.relname, force_quotes), "SEQUENCE", NULL,
4378+
query->data, delqry->data, "", tbinfo.usename, NULL, NULL);
4379+
}
43984380

4399-
ArchiveEntry(fout, tbinfo.oid, fmtId(tbinfo.relname, force_quotes), "SEQUENCE SET", NULL,
4400-
query->data, "" /* Del */, "", "", NULL, NULL);
4381+
if (!schemaOnly)
4382+
{
4383+
resetPQExpBuffer(query);
4384+
appendPQExpBuffer(query, "SELECT setval (");
4385+
formatStringLiteral(query, fmtId(tbinfo.relname, force_quotes), CONV_ALL);
4386+
appendPQExpBuffer(query, ", %d, '%c');\n", last, called);
44014387

4402-
/* Dump Sequence Comments */
4388+
ArchiveEntry(fout, tbinfo.oid, fmtId(tbinfo.relname, force_quotes), "SEQUENCE SET", NULL,
4389+
query->data, "" /* Del */, "", "", NULL, NULL);
4390+
}
44034391

4404-
resetPQExpBuffer(query);
4405-
appendPQExpBuffer(query, "SEQUENCE %s", fmtId(tbinfo.relname, force_quotes));
4406-
dumpComment(fout, query->data, tbinfo.oid);
4392+
if (!dataOnly)
4393+
{
4394+
/* Dump Sequence Comments */
44074395

4396+
resetPQExpBuffer(query);
4397+
appendPQExpBuffer(query, "SEQUENCE %s", fmtId(tbinfo.relname, force_quotes));
4398+
dumpComment(fout, query->data, tbinfo.oid);
4399+
}
44084400
}
44094401

44104402

0 commit comments

Comments
 (0)