Re: [HACKERS] support for NEXT VALUE FOR expression

2016-08-16 Thread Tom Lane
Peter Eisentraut  writes:
> 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.

BTW, several of the earlier threads complained of needing to make NEXT
a fully-reserved word in order to get this to parse without shift/reduce
conflicts.  How did you avoid that?  I notice that your patch puts the
new production into c_expr not func_expr_common_subexpr which would
seem like the obvious place.  If that is what's making the difference
it seems rather fragile, and it would mean that NEXT VALUE FOR doesn't
act like a function in some syntactic contexts like a FROM-function.

regards, tom lane


-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers


Re: [HACKERS] support for NEXT VALUE FOR expression

2016-08-16 Thread Tom Lane
Thomas Munro  writes:
> On Wed, Aug 17, 2016 at 3:52 PM, Tom Lane  wrote:
>> We discussed this before and concluded that NEXT VALUE FOR is in fact
>> *not* an exact semantic equivalent of nextval():
>> https://www.postgresql.org/message-id/14790.1083955136%40sss.pgh.pa.us

> And also again 10 years later when I proposed it :-)
> https://www.postgresql.org/message-id/flat/CADLWmXUY2oo4XObQWF3yPUSK%3D5uEiSV%3DeTyLrnu%3DRzteOy%2B3Lg%40mail.gmail.com

And that links to yet another thread, from 2002 ;-)

The 2004 thread does contain some speculation about how to implement the
spec's semantics.  It seems like the first problem is nailing down what
is meant by "once per row", particularly in cases with nested execution
contexts.

regards, tom lane


-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers


Re: [HACKERS] support for NEXT VALUE FOR expression

2016-08-16 Thread Thomas Munro
On Wed, Aug 17, 2016 at 3:52 PM, Tom Lane  wrote:
> Peter Eisentraut  writes:
>> 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:
>
> We discussed this before and concluded that NEXT VALUE FOR is in fact
> *not* an exact semantic equivalent of nextval():
>
> https://www.postgresql.org/message-id/14790.1083955136%40sss.pgh.pa.us

And also again 10 years later when I proposed it :-)

https://www.postgresql.org/message-id/flat/CADLWmXUY2oo4XObQWF3yPUSK%3D5uEiSV%3DeTyLrnu%3DRzteOy%2B3Lg%40mail.gmail.com

> I remain of the opinion that using spec-compliant syntax for
> non-spec-compliant behavior isn't a great advance.

-- 
Thomas Munro
http://www.enterprisedb.com


-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers


Re: [HACKERS] support for NEXT VALUE FOR expression

2016-08-16 Thread Tom Lane
Peter Eisentraut  writes:
> 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:

We discussed this before and concluded that NEXT VALUE FOR is in fact
*not* an exact semantic equivalent of nextval():

https://www.postgresql.org/message-id/14790.1083955136%40sss.pgh.pa.us

I remain of the opinion that using spec-compliant syntax for
non-spec-compliant behavior isn't a great advance.

regards, tom lane


-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers


[HACKERS] support for NEXT VALUE FOR expression

2016-08-16 Thread Peter Eisentraut
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