Skip to content

Commit 23687e9

Browse files
Parse sequence type and integer metadata in dumpSequence().
This commit modifies dumpSequence() to parse all the sequence metadata into the appropriate types instead of carting around string pointers to the PGresult data. Besides allowing us to free the PGresult storage earlier in the function, this eliminates the need to compare min_value and max_value to their respective defaults as strings. This is preparatory work for a follow-up commit that will improve the performance of dumpSequence() in a similar manner to how commit 2329cad optimized binary_upgrade_set_pg_class_oids(). Reviewed-by: Euler Taveira Discussion: https://postgr.es/m/20240503025140.GA1227404%40nathanxps13
1 parent 057ee91 commit 23687e9

File tree

2 files changed

+61
-44
lines changed

2 files changed

+61
-44
lines changed

src/bin/pg_dump/pg_dump.c

Lines changed: 60 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,24 @@ typedef struct
104104
RelFileNumber toast_index_relfilenumber; /* toast table index filenode */
105105
} BinaryUpgradeClassOidItem;
106106

107+
/* sequence types */
108+
typedef enum SeqType
109+
{
110+
SEQTYPE_SMALLINT,
111+
SEQTYPE_INTEGER,
112+
SEQTYPE_BIGINT,
113+
} SeqType;
114+
115+
const char *const SeqTypeNames[] =
116+
{
117+
[SEQTYPE_SMALLINT] = "smallint",
118+
[SEQTYPE_INTEGER] = "integer",
119+
[SEQTYPE_BIGINT] = "bigint",
120+
};
121+
122+
StaticAssertDecl(lengthof(SeqTypeNames) == (SEQTYPE_BIGINT + 1),
123+
"array length mismatch");
124+
107125
typedef enum OidOptions
108126
{
109127
zeroIsError = 1,
@@ -17251,6 +17269,19 @@ dumpTableConstraintComment(Archive *fout, const ConstraintInfo *coninfo)
1725117269
free(qtabname);
1725217270
}
1725317271

17272+
static inline SeqType
17273+
parse_sequence_type(const char *name)
17274+
{
17275+
for (int i = 0; i < lengthof(SeqTypeNames); i++)
17276+
{
17277+
if (strcmp(SeqTypeNames[i], name) == 0)
17278+
return (SeqType) i;
17279+
}
17280+
17281+
pg_fatal("unrecognized sequence type: %s", name);
17282+
return (SeqType) 0; /* keep compiler quiet */
17283+
}
17284+
1725417285
/*
1725517286
* dumpSequence
1725617287
* write the declaration (not data) of one user-defined sequence
@@ -17260,18 +17291,16 @@ dumpSequence(Archive *fout, const TableInfo *tbinfo)
1726017291
{
1726117292
DumpOptions *dopt = fout->dopt;
1726217293
PGresult *res;
17263-
char *startv,
17264-
*incby,
17265-
*maxv,
17266-
*minv,
17267-
*cache,
17268-
*seqtype;
17294+
SeqType seqtype;
1726917295
bool cycled;
1727017296
bool is_ascending;
1727117297
int64 default_minv,
17272-
default_maxv;
17273-
char bufm[32],
17274-
bufx[32];
17298+
default_maxv,
17299+
minv,
17300+
maxv,
17301+
startv,
17302+
incby,
17303+
cache;
1727517304
PQExpBuffer query = createPQExpBuffer();
1727617305
PQExpBuffer delqry = createPQExpBuffer();
1727717306
char *qseqname;
@@ -17313,50 +17342,39 @@ dumpSequence(Archive *fout, const TableInfo *tbinfo)
1731317342
PQntuples(res)),
1731417343
tbinfo->dobj.name, PQntuples(res));
1731517344

17316-
seqtype = PQgetvalue(res, 0, 0);
17317-
startv = PQgetvalue(res, 0, 1);
17318-
incby = PQgetvalue(res, 0, 2);
17319-
maxv = PQgetvalue(res, 0, 3);
17320-
minv = PQgetvalue(res, 0, 4);
17321-
cache = PQgetvalue(res, 0, 5);
17345+
seqtype = parse_sequence_type(PQgetvalue(res, 0, 0));
17346+
startv = strtoi64(PQgetvalue(res, 0, 1), NULL, 10);
17347+
incby = strtoi64(PQgetvalue(res, 0, 2), NULL, 10);
17348+
maxv = strtoi64(PQgetvalue(res, 0, 3), NULL, 10);
17349+
minv = strtoi64(PQgetvalue(res, 0, 4), NULL, 10);
17350+
cache = strtoi64(PQgetvalue(res, 0, 5), NULL, 10);
1732217351
cycled = (strcmp(PQgetvalue(res, 0, 6), "t") == 0);
1732317352

17353+
PQclear(res);
17354+
1732417355
/* Calculate default limits for a sequence of this type */
17325-
is_ascending = (incby[0] != '-');
17326-
if (strcmp(seqtype, "smallint") == 0)
17356+
is_ascending = (incby >= 0);
17357+
if (seqtype == SEQTYPE_SMALLINT)
1732717358
{
1732817359
default_minv = is_ascending ? 1 : PG_INT16_MIN;
1732917360
default_maxv = is_ascending ? PG_INT16_MAX : -1;
1733017361
}
17331-
else if (strcmp(seqtype, "integer") == 0)
17362+
else if (seqtype == SEQTYPE_INTEGER)
1733217363
{
1733317364
default_minv = is_ascending ? 1 : PG_INT32_MIN;
1733417365
default_maxv = is_ascending ? PG_INT32_MAX : -1;
1733517366
}
17336-
else if (strcmp(seqtype, "bigint") == 0)
17367+
else if (seqtype == SEQTYPE_BIGINT)
1733717368
{
1733817369
default_minv = is_ascending ? 1 : PG_INT64_MIN;
1733917370
default_maxv = is_ascending ? PG_INT64_MAX : -1;
1734017371
}
1734117372
else
1734217373
{
17343-
pg_fatal("unrecognized sequence type: %s", seqtype);
17374+
pg_fatal("unrecognized sequence type: %d", seqtype);
1734417375
default_minv = default_maxv = 0; /* keep compiler quiet */
1734517376
}
1734617377

17347-
/*
17348-
* 64-bit strtol() isn't very portable, so convert the limits to strings
17349-
* and compare that way.
17350-
*/
17351-
snprintf(bufm, sizeof(bufm), INT64_FORMAT, default_minv);
17352-
snprintf(bufx, sizeof(bufx), INT64_FORMAT, default_maxv);
17353-
17354-
/* Don't print minv/maxv if they match the respective default limit */
17355-
if (strcmp(minv, bufm) == 0)
17356-
minv = NULL;
17357-
if (strcmp(maxv, bufx) == 0)
17358-
maxv = NULL;
17359-
1736017378
/*
1736117379
* Identity sequences are not to be dropped separately.
1736217380
*/
@@ -17404,26 +17422,26 @@ dumpSequence(Archive *fout, const TableInfo *tbinfo)
1740417422
"UNLOGGED " : "",
1740517423
fmtQualifiedDumpable(tbinfo));
1740617424

17407-
if (strcmp(seqtype, "bigint") != 0)
17408-
appendPQExpBuffer(query, " AS %s\n", seqtype);
17425+
if (seqtype != SEQTYPE_BIGINT)
17426+
appendPQExpBuffer(query, " AS %s\n", SeqTypeNames[seqtype]);
1740917427
}
1741017428

17411-
appendPQExpBuffer(query, " START WITH %s\n", startv);
17429+
appendPQExpBuffer(query, " START WITH " INT64_FORMAT "\n", startv);
1741217430

17413-
appendPQExpBuffer(query, " INCREMENT BY %s\n", incby);
17431+
appendPQExpBuffer(query, " INCREMENT BY " INT64_FORMAT "\n", incby);
1741417432

17415-
if (minv)
17416-
appendPQExpBuffer(query, " MINVALUE %s\n", minv);
17433+
if (minv != default_minv)
17434+
appendPQExpBuffer(query, " MINVALUE " INT64_FORMAT "\n", minv);
1741717435
else
1741817436
appendPQExpBufferStr(query, " NO MINVALUE\n");
1741917437

17420-
if (maxv)
17421-
appendPQExpBuffer(query, " MAXVALUE %s\n", maxv);
17438+
if (maxv != default_maxv)
17439+
appendPQExpBuffer(query, " MAXVALUE " INT64_FORMAT "\n", maxv);
1742217440
else
1742317441
appendPQExpBufferStr(query, " NO MAXVALUE\n");
1742417442

1742517443
appendPQExpBuffer(query,
17426-
" CACHE %s%s",
17444+
" CACHE " INT64_FORMAT "%s",
1742717445
cache, (cycled ? "\n CYCLE" : ""));
1742817446

1742917447
if (tbinfo->is_identity_sequence)
@@ -17510,8 +17528,6 @@ dumpSequence(Archive *fout, const TableInfo *tbinfo)
1751017528
tbinfo->dobj.namespace->dobj.name, tbinfo->rolname,
1751117529
tbinfo->dobj.catId, 0, tbinfo->dobj.dumpId);
1751217530

17513-
PQclear(res);
17514-
1751517531
destroyPQExpBuffer(query);
1751617532
destroyPQExpBuffer(delqry);
1751717533
free(qseqname);

src/tools/pgindent/typedefs.list

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2572,6 +2572,7 @@ SeqScan
25722572
SeqScanState
25732573
SeqTable
25742574
SeqTableData
2575+
SeqType
25752576
SerCommitSeqNo
25762577
SerialControl
25772578
SerialIOData

0 commit comments

Comments
 (0)