Here is a patch for implementing the NEXT VALUE FOR expression. This is
the SQL-standard conforming version of our nextval() function, and it's
also used by Oracle, MS SQL, DB2. Example:
SELECT NEXT VALUE FOR foo_seq;
The second patch changes the serial column to use this new expression
for its generated default values. This doesn't make an external
difference except perhaps that the generated expression looks less weird
to the user.
--
Peter Eisentraut http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services
From c012df68bdfc8711d142f7a91f8ea0ee4ecef813 Mon Sep 17 00:00:00 2001
From: Peter Eisentraut
Date: Thu, 11 Aug 2016 12:00:00 -0400
Subject: [PATCH 1/2] Add NEXT VALUE FOR expression
This is the SQL-standard-conforming version of the nextval function. It
functions the same way, but has a more SQL-like syntax and separate
parse and executor nodes.
---
doc/src/sgml/func.sgml | 29 ++---
doc/src/sgml/ref/create_sequence.sgml | 17 -
src/backend/catalog/dependency.c | 7 +++
src/backend/commands/sequence.c| 3 +--
src/backend/executor/execQual.c| 21 +
src/backend/nodes/copyfuncs.c | 34 ++
src/backend/nodes/equalfuncs.c | 24
src/backend/nodes/nodeFuncs.c | 18 ++
src/backend/nodes/outfuncs.c | 12
src/backend/nodes/readfuncs.c | 16
src/backend/parser/gram.y | 12
src/backend/parser/parse_expr.c| 23 +++
src/backend/parser/parse_target.c | 3 +++
src/backend/utils/adt/ruleutils.c | 9 +
src/bin/psql/tab-complete.c| 6 ++
src/include/commands/sequence.h| 2 ++
src/include/nodes/nodes.h | 2 ++
src/include/nodes/parsenodes.h | 10 ++
src/include/nodes/primnodes.h | 10 ++
src/test/regress/expected/sequence.out | 8
src/test/regress/sql/sequence.sql | 2 +-
21 files changed, 249 insertions(+), 19 deletions(-)
diff --git a/doc/src/sgml/func.sgml b/doc/src/sgml/func.sgml
index 426e562..dc21e6b 100644
--- a/doc/src/sgml/func.sgml
+++ b/doc/src/sgml/func.sgml
@@ -11476,6 +11476,9 @@ Sequence Manipulation Functions
sequence
+ NEXT VALUE FOR
+
+
nextval
@@ -11489,7 +11492,7 @@ Sequence Manipulation Functions
- This section describes functions for operating on sequence
+ This section describes functions and other expressions for operating on sequence
objects, also called sequence generators or just sequences.
Sequence objects are special single-row tables created with .
@@ -11512,7 +11515,7 @@ Sequence Functions
currval(regclass)
bigint
Return value most recently obtained with
-nextval for specified sequence
+nextval/NEXT VALUE FOR for specified sequence
lastval()
@@ -11526,6 +11529,11 @@ Sequence Functions
Advance sequence and return new value
+NEXT VALUE FOR sequence_name
+bigint
+Advance sequence and return new value
+
+
setval(regclass, bigint)
bigint
Set sequence's current value
@@ -11540,7 +11548,8 @@ Sequence Functions
- The sequence to be operated on by a sequence function is specified by
+ For the function syntax,
+ the sequence to be operated on by a sequence function is specified by
a regclass argument, which is simply the OID of the sequence in the
pg_class system catalog. You do not have to look up the
OID by hand, however, since the regclass data type's input
@@ -11601,11 +11610,21 @@ Sequence Functions
+ For NEXT VALUE FOR, the sequence argument is a regular
+ identifier, e.g.,
+
+NEXT VALUE FOR foo
+NEXT VALUE FOR "Foo"
+
+
+
+
The available sequence functions are:
nextval
+ NEXT VALUE FOR
Advance the sequence object to its next value and return that
@@ -11640,6 +11659,10 @@ Sequence Functions
+
+NEXT VALUE FOR is the syntax specified by the SQL
+standard. All the function call variants are PostgreSQL extensions.
+
diff --git a/doc/src/sgml/ref/create_sequence.sgml b/doc/src/sgml/ref/create_sequence.sgml
index c959146..bcb5aa8 100644
--- a/doc/src/sgml/ref/create_sequence.sgml
+++ b/doc/src/sgml/ref/create_sequence.sgml
@@ -49,11 +49,12 @@ Description
- After a sequence is created, you use the functions
+ After a sequence is created, you use the expression NEXT VALUE FOR
+ and the functions
nextval,
currval, and
setval
- to operate on the sequence. These functions are documented in
+ to operate on the sequence. These