Skip to content

Commit 6fec216

Browse files
committed
Added new SQL function setval(seq,val,bool) to restore is_called as well as value
(will be used in a future pg_dump).
1 parent 8e72a87 commit 6fec216

File tree

3 files changed

+32
-7
lines changed

3 files changed

+32
-7
lines changed

src/backend/commands/sequence.c

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ static SeqTable init_sequence(char *caller, char *name);
6161
static Form_pg_sequence read_info(char *caller, SeqTable elm, Buffer *buf);
6262
static void init_params(CreateSeqStmt *seq, Form_pg_sequence new);
6363
static int get_param(DefElem *def);
64+
static void do_setval(char *seqname, int32 next, char iscalled);
6465

6566
/*
6667
* DefineSequence
@@ -317,12 +318,9 @@ currval(PG_FUNCTION_ARGS)
317318
PG_RETURN_INT32(result);
318319
}
319320

320-
Datum
321-
setval(PG_FUNCTION_ARGS)
321+
static void
322+
do_setval(char *seqname, int32 next, bool iscalled)
322323
{
323-
text *seqin = PG_GETARG_TEXT_P(0);
324-
int32 next = PG_GETARG_INT32(1);
325-
char *seqname = get_seq_name(seqin);
326324
SeqTable elm;
327325
Buffer buf;
328326
Form_pg_sequence seq;
@@ -356,7 +354,7 @@ setval(PG_FUNCTION_ARGS)
356354

357355
/* save info in sequence relation */
358356
seq->last_value = next; /* last fetched number */
359-
seq->is_called = 't';
357+
seq->is_called = iscalled ? 't' : 'f';
360358

361359
LockBuffer(buf, BUFFER_LOCK_UNLOCK);
362360

@@ -365,6 +363,30 @@ setval(PG_FUNCTION_ARGS)
365363

366364
pfree(seqname);
367365

366+
}
367+
368+
Datum
369+
setval(PG_FUNCTION_ARGS)
370+
{
371+
text *seqin = PG_GETARG_TEXT_P(0);
372+
int32 next = PG_GETARG_INT32(1);
373+
char *seqname = get_seq_name(seqin);
374+
375+
do_setval(seqname, next, true);
376+
377+
PG_RETURN_INT32(next);
378+
}
379+
380+
Datum
381+
setval_and_iscalled(PG_FUNCTION_ARGS)
382+
{
383+
text *seqin = PG_GETARG_TEXT_P(0);
384+
int32 next = PG_GETARG_INT32(1);
385+
bool iscalled = PG_GETARG_BOOL(2);
386+
char *seqname = get_seq_name(seqin);
387+
388+
do_setval(seqname, next, iscalled);
389+
368390
PG_RETURN_INT32(next);
369391
}
370392

src/include/catalog/pg_proc.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2000, PostgreSQL, Inc
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $Id: pg_proc.h,v 1.168 2000/09/25 12:58:47 momjian Exp $
10+
* $Id: pg_proc.h,v 1.169 2000/10/11 15:31:13 pjw Exp $
1111
*
1212
* NOTES
1313
* The script catalog/genbki.sh reads this file and generates .bki
@@ -1978,6 +1978,8 @@ DATA(insert OID = 1575 ( currval PGUID 12 f t f t 1 f 23 "25" 100 0 0 100 cur
19781978
DESCR("sequence current value");
19791979
DATA(insert OID = 1576 ( setval PGUID 12 f t f t 2 f 23 "25 23" 100 0 0 100 setval - ));
19801980
DESCR("set sequence value");
1981+
DATA(insert OID = 1765 ( setval PGUID 12 f t f t 3 f 23 "25 23 16" 100 0 0 100 setval_and_iscalled - ));
1982+
DESCR("set sequence value and iscalled status");
19811983

19821984
DATA(insert OID = 1579 ( varbit_in PGUID 12 f t t t 1 f 1562 "0" 100 0 0 100 varbit_in - ));
19831985
DESCR("(internal)");

src/include/commands/sequence.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
extern Datum nextval(PG_FUNCTION_ARGS);
3131
extern Datum currval(PG_FUNCTION_ARGS);
3232
extern Datum setval(PG_FUNCTION_ARGS);
33+
extern Datum setval_and_iscalled(PG_FUNCTION_ARGS);
3334

3435
extern void DefineSequence(CreateSeqStmt *stmt);
3536
extern void CloseSequences(void);

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