@@ -104,6 +104,24 @@ typedef struct
104
104
RelFileNumber toast_index_relfilenumber; /* toast table index filenode */
105
105
} BinaryUpgradeClassOidItem;
106
106
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
+
107
125
typedef enum OidOptions
108
126
{
109
127
zeroIsError = 1,
@@ -17251,6 +17269,19 @@ dumpTableConstraintComment(Archive *fout, const ConstraintInfo *coninfo)
17251
17269
free(qtabname);
17252
17270
}
17253
17271
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
+
17254
17285
/*
17255
17286
* dumpSequence
17256
17287
* write the declaration (not data) of one user-defined sequence
@@ -17260,18 +17291,16 @@ dumpSequence(Archive *fout, const TableInfo *tbinfo)
17260
17291
{
17261
17292
DumpOptions *dopt = fout->dopt;
17262
17293
PGresult *res;
17263
- char *startv,
17264
- *incby,
17265
- *maxv,
17266
- *minv,
17267
- *cache,
17268
- *seqtype;
17294
+ SeqType seqtype;
17269
17295
bool cycled;
17270
17296
bool is_ascending;
17271
17297
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;
17275
17304
PQExpBuffer query = createPQExpBuffer();
17276
17305
PQExpBuffer delqry = createPQExpBuffer();
17277
17306
char *qseqname;
@@ -17313,50 +17342,39 @@ dumpSequence(Archive *fout, const TableInfo *tbinfo)
17313
17342
PQntuples(res)),
17314
17343
tbinfo->dobj.name, PQntuples(res));
17315
17344
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 );
17322
17351
cycled = (strcmp(PQgetvalue(res, 0, 6), "t") == 0);
17323
17352
17353
+ PQclear(res);
17354
+
17324
17355
/* 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 )
17327
17358
{
17328
17359
default_minv = is_ascending ? 1 : PG_INT16_MIN;
17329
17360
default_maxv = is_ascending ? PG_INT16_MAX : -1;
17330
17361
}
17331
- else if (strcmp( seqtype, "integer") == 0 )
17362
+ else if (seqtype == SEQTYPE_INTEGER )
17332
17363
{
17333
17364
default_minv = is_ascending ? 1 : PG_INT32_MIN;
17334
17365
default_maxv = is_ascending ? PG_INT32_MAX : -1;
17335
17366
}
17336
- else if (strcmp( seqtype, "bigint") == 0 )
17367
+ else if (seqtype == SEQTYPE_BIGINT )
17337
17368
{
17338
17369
default_minv = is_ascending ? 1 : PG_INT64_MIN;
17339
17370
default_maxv = is_ascending ? PG_INT64_MAX : -1;
17340
17371
}
17341
17372
else
17342
17373
{
17343
- pg_fatal("unrecognized sequence type: %s ", seqtype);
17374
+ pg_fatal("unrecognized sequence type: %d ", seqtype);
17344
17375
default_minv = default_maxv = 0; /* keep compiler quiet */
17345
17376
}
17346
17377
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
-
17360
17378
/*
17361
17379
* Identity sequences are not to be dropped separately.
17362
17380
*/
@@ -17404,26 +17422,26 @@ dumpSequence(Archive *fout, const TableInfo *tbinfo)
17404
17422
"UNLOGGED " : "",
17405
17423
fmtQualifiedDumpable(tbinfo));
17406
17424
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] );
17409
17427
}
17410
17428
17411
- appendPQExpBuffer(query, " START WITH %s \n", startv);
17429
+ appendPQExpBuffer(query, " START WITH " INT64_FORMAT " \n", startv);
17412
17430
17413
- appendPQExpBuffer(query, " INCREMENT BY %s \n", incby);
17431
+ appendPQExpBuffer(query, " INCREMENT BY " INT64_FORMAT " \n", incby);
17414
17432
17415
- if (minv)
17416
- appendPQExpBuffer(query, " MINVALUE %s \n", minv);
17433
+ if (minv != default_minv )
17434
+ appendPQExpBuffer(query, " MINVALUE " INT64_FORMAT " \n", minv);
17417
17435
else
17418
17436
appendPQExpBufferStr(query, " NO MINVALUE\n");
17419
17437
17420
- if (maxv)
17421
- appendPQExpBuffer(query, " MAXVALUE %s \n", maxv);
17438
+ if (maxv != default_maxv )
17439
+ appendPQExpBuffer(query, " MAXVALUE " INT64_FORMAT " \n", maxv);
17422
17440
else
17423
17441
appendPQExpBufferStr(query, " NO MAXVALUE\n");
17424
17442
17425
17443
appendPQExpBuffer(query,
17426
- " CACHE %s %s",
17444
+ " CACHE " INT64_FORMAT " %s",
17427
17445
cache, (cycled ? "\n CYCLE" : ""));
17428
17446
17429
17447
if (tbinfo->is_identity_sequence)
@@ -17510,8 +17528,6 @@ dumpSequence(Archive *fout, const TableInfo *tbinfo)
17510
17528
tbinfo->dobj.namespace->dobj.name, tbinfo->rolname,
17511
17529
tbinfo->dobj.catId, 0, tbinfo->dobj.dumpId);
17512
17530
17513
- PQclear(res);
17514
-
17515
17531
destroyPQExpBuffer(query);
17516
17532
destroyPQExpBuffer(delqry);
17517
17533
free(qseqname);
0 commit comments