Skip to content

Commit 8000fdd

Browse files
committed
> > - Move SEQ_MAXVALUE, SEQ_MINVALUE definitions to sequence.h
> > > > - Add check in pg_dump to see if the value returned is the max /min > > values and replace with NO MAXVALUE, NO MINVALUE. > > > > - Change START and INCREMENT to use START WITH and INCREMENT BY syntax. > > This makes it a touch easier to port to other databases with sequences > > (Oracle). PostgreSQL supports both syntaxes already. > > + char bufm[100], > + bufx[100]; > > This seems to be an arbitary size. Why not set it to the actual maximum > length? > > Also: > > + snprintf(bufm, 100, INT64_FORMAT, SEQ_MINVALUE); > + snprintf(bufx, 100, INT64_FORMAT, SEQ_MAXVALUE); > > sizeof(bufm), sizeof(bufx) is probably the more > maintenance-friendly/standard way to do it. I changed the code to use sizeof - but will wait for a response from Peter before changing the size. It's consistent throughout the sequence code to be 100 for this purpose. Rod Taylor <rbt@rbt.ca>
1 parent a00431b commit 8000fdd

File tree

3 files changed

+56
-27
lines changed

3 files changed

+56
-27
lines changed

src/backend/commands/sequence.c

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/commands/sequence.c,v 1.92 2003/03/20 03:34:55 momjian Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/commands/sequence.c,v 1.93 2003/03/20 05:18:14 momjian Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -24,19 +24,6 @@
2424
#include "utils/acl.h"
2525
#include "utils/builtins.h"
2626

27-
28-
#ifndef INT64_IS_BUSTED
29-
#ifdef HAVE_LL_CONSTANTS
30-
#define SEQ_MAXVALUE ((int64) 0x7FFFFFFFFFFFFFFFLL)
31-
#else
32-
#define SEQ_MAXVALUE ((int64) 0x7FFFFFFFFFFFFFFF)
33-
#endif
34-
#else /* INT64_IS_BUSTED */
35-
#define SEQ_MAXVALUE ((int64) 0x7FFFFFFF)
36-
#endif /* INT64_IS_BUSTED */
37-
38-
#define SEQ_MINVALUE (-SEQ_MAXVALUE)
39-
4027
/*
4128
* We don't want to log each fetching of a value from a sequence,
4229
* so we pre-log a few fetches in advance. In the event of

src/bin/pg_dump/pg_dump.c

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* by PostgreSQL
1313
*
1414
* IDENTIFICATION
15-
* $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.319 2003/03/10 22:28:19 tgl Exp $
15+
* $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.320 2003/03/20 05:18:14 momjian Exp $
1616
*
1717
*-------------------------------------------------------------------------
1818
*/
@@ -52,6 +52,8 @@ int optreset;
5252
#include "catalog/pg_trigger.h"
5353
#include "catalog/pg_type.h"
5454

55+
#include "commands/sequence.h"
56+
5557
#include "libpq-fe.h"
5658
#include "libpq/libpq-fs.h"
5759

@@ -5986,9 +5988,11 @@ dumpOneSequence(Archive *fout, TableInfo *tbinfo,
59865988
PGresult *res;
59875989
char *last,
59885990
*incby,
5989-
*maxv,
5990-
*minv,
5991+
*maxv = NULL,
5992+
*minv = NULL,
59915993
*cache;
5994+
char bufm[100],
5995+
bufx[100];
59925996
bool cycled,
59935997
called;
59945998
PQExpBuffer query = createPQExpBuffer();
@@ -5997,9 +6001,21 @@ dumpOneSequence(Archive *fout, TableInfo *tbinfo,
59976001
/* Make sure we are in proper schema */
59986002
selectSourceSchema(tbinfo->relnamespace->nspname);
59996003

6004+
snprintf(bufm, sizeof(bufm), INT64_FORMAT, SEQ_MINVALUE);
6005+
snprintf(bufx, sizeof(bufx), INT64_FORMAT, SEQ_MAXVALUE);
6006+
60006007
appendPQExpBuffer(query,
6001-
"SELECT sequence_name, last_value, increment_by, max_value, "
6002-
"min_value, cache_value, is_cycled, is_called from %s",
6008+
"SELECT sequence_name, last_value, increment_by, "
6009+
"CASE WHEN increment_by > 0 AND max_value = %s THEN NULL "
6010+
" WHEN increment_by < 0 AND max_value = -1 THEN NULL "
6011+
" ELSE max_value "
6012+
"END AS max_value, "
6013+
"CASE WHEN increment_by > 0 AND min_value = 1 THEN NULL "
6014+
" WHEN increment_by < 0 AND min_value = %s THEN NULL "
6015+
" ELSE min_value "
6016+
"END AS min_value, "
6017+
"cache_value, is_cycled, is_called from %s",
6018+
bufx, bufm,
60036019
fmtId(tbinfo->relname));
60046020

60056021
res = PQexec(g_conn, query->data);
@@ -6028,8 +6044,10 @@ dumpOneSequence(Archive *fout, TableInfo *tbinfo,
60286044

60296045
last = PQgetvalue(res, 0, 1);
60306046
incby = PQgetvalue(res, 0, 2);
6031-
maxv = PQgetvalue(res, 0, 3);
6032-
minv = PQgetvalue(res, 0, 4);
6047+
if (!PQgetisnull(res, 0, 3))
6048+
maxv = PQgetvalue(res, 0, 3);
6049+
if (!PQgetisnull(res, 0, 4))
6050+
minv = PQgetvalue(res, 0, 4);
60336051
cache = PQgetvalue(res, 0, 5);
60346052
cycled = (strcmp(PQgetvalue(res, 0, 6), "t") == 0);
60356053
called = (strcmp(PQgetvalue(res, 0, 7), "t") == 0);
@@ -6060,12 +6078,23 @@ dumpOneSequence(Archive *fout, TableInfo *tbinfo,
60606078

60616079
resetPQExpBuffer(query);
60626080
appendPQExpBuffer(query,
6063-
"CREATE SEQUENCE %s\n START %s\n INCREMENT %s\n"
6064-
" MAXVALUE %s\n MINVALUE %s\n CACHE %s%s;\n",
6081+
"CREATE SEQUENCE %s\n START WITH %s\n INCREMENT BY %s\n",
60656082
fmtId(tbinfo->relname),
6066-
(called ? minv : last),
6067-
incby, maxv, minv, cache,
6068-
(cycled ? "\n CYCLE" : ""));
6083+
(called ? minv : last), incby);
6084+
6085+
if (maxv)
6086+
appendPQExpBuffer(query, " MAXVALUE %s\n", maxv);
6087+
else
6088+
appendPQExpBuffer(query, " NO MAXVALUE\n");
6089+
6090+
if (minv)
6091+
appendPQExpBuffer(query, " MINVALUE %s\n", minv);
6092+
else
6093+
appendPQExpBuffer(query, " NO MINVALUE\n");
6094+
6095+
appendPQExpBuffer(query,
6096+
" CACHE %s%s;\n",
6097+
cache, (cycled ? "\n CYCLE" : ""));
60696098

60706099
ArchiveEntry(fout, tbinfo->oid, tbinfo->relname,
60716100
tbinfo->relnamespace->nspname, tbinfo->usename,

src/include/commands/sequence.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
77
* Portions Copyright (c) 1994, Regents of the University of California
88
*
9-
* $Id: sequence.h,v 1.21 2002/06/20 20:29:49 momjian Exp $
9+
* $Id: sequence.h,v 1.22 2003/03/20 05:18:15 momjian Exp $
1010
*
1111
*-------------------------------------------------------------------------
1212
*/
@@ -89,4 +89,17 @@ extern void seq_redo(XLogRecPtr lsn, XLogRecord *rptr);
8989
extern void seq_undo(XLogRecPtr lsn, XLogRecord *rptr);
9090
extern void seq_desc(char *buf, uint8 xl_info, char *rec);
9191

92+
/* Set the upper and lower bounds of a sequence */
93+
#ifndef INT64_IS_BUSTED
94+
#ifdef HAVE_LL_CONSTANTS
95+
#define SEQ_MAXVALUE ((int64) 0x7FFFFFFFFFFFFFFFLL)
96+
#else
97+
#define SEQ_MAXVALUE ((int64) 0x7FFFFFFFFFFFFFFF)
98+
#endif
99+
#else /* INT64_IS_BUSTED */
100+
#define SEQ_MAXVALUE ((int64) 0x7FFFFFFF)
101+
#endif /* INT64_IS_BUSTED */
102+
103+
#define SEQ_MINVALUE (-SEQ_MAXVALUE)
104+
92105
#endif /* SEQUENCE_H */

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy