@@ -104,6 +104,24 @@ typedef struct
104104RelFileNumber 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+
107125typedef enum OidOptions
108126{
109127zeroIsError = 1,
@@ -17251,6 +17269,19 @@ dumpTableConstraintComment(Archive *fout, const ConstraintInfo *coninfo)
1725117269free(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{
1726117292DumpOptions *dopt = fout->dopt;
1726217293PGresult *res;
17263- char *startv,
17264- *incby,
17265- *maxv,
17266- *minv,
17267- *cache,
17268- *seqtype;
17294+ SeqTypeseqtype;
1726917295boolcycled;
1727017296boolis_ascending;
1727117297int64default_minv,
17272- default_maxv;
17273- charbufm[32],
17274- bufx[32];
17298+ default_maxv,
17299+ minv,
17300+ maxv,
17301+ startv,
17302+ incby,
17303+ cache;
1727517304PQExpBuffer query = createPQExpBuffer();
1727617305PQExpBuffer delqry = createPQExpBuffer();
1727717306char *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 );
1732217351cycled = (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{
1732817359default_minv = is_ascending ? 1 : PG_INT16_MIN;
1732917360default_maxv = is_ascending ? PG_INT16_MAX : -1;
1733017361}
17331- else if (strcmp( seqtype, "integer") ==0 )
17362+ else if (seqtype ==SEQTYPE_INTEGER )
1733217363{
1733317364default_minv = is_ascending ? 1 : PG_INT32_MIN;
1733417365default_maxv = is_ascending ? PG_INT32_MAX : -1;
1733517366}
17336- else if (strcmp( seqtype, "bigint") ==0 )
17367+ else if (seqtype ==SEQTYPE_BIGINT )
1733717368{
1733817369default_minv = is_ascending ? 1 : PG_INT64_MIN;
1733917370default_maxv = is_ascending ? PG_INT64_MAX : -1;
1734017371}
1734117372else
1734217373{
17343- pg_fatal("unrecognized sequence type: %s ", seqtype);
17374+ pg_fatal("unrecognized sequence type: %d ", seqtype);
1734417375default_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);
1741717435else
1741817436appendPQExpBufferStr(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);
1742217440else
1742317441appendPQExpBufferStr(query, " NO MAXVALUE\n");
1742417442
1742517443appendPQExpBuffer(query,
17426- " CACHE%s %s",
17444+ " CACHE" INT64_FORMAT " %s",
1742717445 cache, (cycled ? "\n CYCLE" : ""));
1742817446
1742917447if (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-
1751517531destroyPQExpBuffer(query);
1751617532destroyPQExpBuffer(delqry);
1751717533free(qseqname);