pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
BUG #14813: pg_get_serial_sequence does not return seqence name for IDENTITY columns
7+ messages / 6 participants
[nested] [flat]

* BUG #14813: pg_get_serial_sequence does not return seqence name for IDENTITY columns
@ 2017-09-12 21:20  zam6ak@gmail.com
  0 siblings, 1 reply; 7+ messages in thread

From: zam6ak@gmail.com @ 2017-09-12 21:20 UTC (permalink / raw)
  To: pgsql-bugs; +Cc: zam6ak@gmail.com

The following bug has been logged on the website:

Bug reference:      14813
Logged by:          zam zam
Email address:      zam6ak@gmail.com
PostgreSQL version: 10beta4
Operating system:   windows
Description:        

Function pg_get_serial_sequence returns NULL on columns defined as
IDENTITY

CREATE TABLE IF NOT EXISTS public.test (
    id     bigint  NOT NULL GENERATED ALWAYS AS IDENTITY,
    code   text,

    CONSTRAINT test_pk   PRIMARY KEY (id)
)    

SELECT pg_get_serial_sequence('public.test', 'id'); -- returns NULL instead
of 'test_id_seq'!




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


^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: BUG #14813: pg_get_serial_sequence does not return seqence name for IDENTITY columns
@ 2017-09-12 21:54  David G. Johnston <david.g.johnston@gmail.com>
  parent: zam6ak@gmail.com
  0 siblings, 2 replies; 7+ messages in thread

From: David G. Johnston @ 2017-09-12 21:54 UTC (permalink / raw)
  To: zam6ak@gmail.com; +Cc: pgsql-bugs

On Tue, Sep 12, 2017 at 2:20 PM, <zam6ak@gmail.com> wrote:

> The following bug has been logged on the website:
>
> Bug reference:      14813
> Logged by:          zam zam
> Email address:      zam6ak@gmail.com
> PostgreSQL version: 10beta4
> Operating system:   windows
> Description:
>
> Function pg_get_serial_sequence returns NULL on columns defined as
> IDENTITY
>
> CREATE TABLE IF NOT EXISTS public.test (
>     id     bigint  NOT NULL GENERATED ALWAYS AS IDENTITY,
>     code   text,
>
>     CONSTRAINT test_pk   PRIMARY KEY (id)
> )
>
> SELECT pg_get_serial_sequence('public.test', 'id'); -- returns NULL
> instead
> of 'test_id_seq'!
>

​I suspect this is an opportunity for documentation enhancement as opposed
to a bug.

​The CREATE TABLE docs say that: "It will have an implicit sequence
attached to it."  I take the word "implicit" to mean that an actual
external sequence object is not created.

David J.

^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: BUG #14813: pg_get_serial_sequence does not return seqence name for IDENTITY columns
@ 2017-09-12 22:01  zambak zambak <zam6ak@gmail.com>
  parent: David G. Johnston <david.g.johnston@gmail.com>
  1 sibling, 0 replies; 7+ messages in thread

From: zambak zambak @ 2017-09-12 22:01 UTC (permalink / raw)
  To: David G. Johnston <david.g.johnston@gmail.com>; +Cc: pgsql-bugs

On Tue, Sep 12, 2017 at 5:54 PM, David G. Johnston <
david.g.johnston@gmail.com> wrote:

> On Tue, Sep 12, 2017 at 2:20 PM, <zam6ak@gmail.com> wrote:
>
>> The following bug has been logged on the website:
>>
>> Bug reference:      14813
>> Logged by:          zam zam
>> Email address:      zam6ak@gmail.com
>> PostgreSQL version: 10beta4
>> Operating system:   windows
>> Description:
>>
>> Function pg_get_serial_sequence returns NULL on columns defined as
>> IDENTITY
>>
>> CREATE TABLE IF NOT EXISTS public.test (
>>     id     bigint  NOT NULL GENERATED ALWAYS AS IDENTITY,
>>     code   text,
>>
>>     CONSTRAINT test_pk   PRIMARY KEY (id)
>> )
>>
>> SELECT pg_get_serial_sequence('public.test', 'id'); -- returns NULL
>> instead
>> of 'test_id_seq'!
>>
>
> ​I suspect this is an opportunity for documentation enhancement as opposed
> to a bug.
>
> ​The CREATE TABLE docs say that: "It will have an implicit sequence
> attached to it."  I take the word "implicit" to mean that an actual
> external sequence object is not created.
>
> David J.
>

But the sequence 'test_id_seq' is created....
To my understanding (and I may be wrong), identity columns are backed by
sequences in PG 10...

^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: BUG #14813: pg_get_serial_sequence does not return seqence name for IDENTITY columns
@ 2017-09-12 23:47  Tom Lane <tgl@sss.pgh.pa.us>
  parent: David G. Johnston <david.g.johnston@gmail.com>
  1 sibling, 1 reply; 7+ messages in thread

From: Tom Lane @ 2017-09-12 23:47 UTC (permalink / raw)
  To: David G. Johnston <david.g.johnston@gmail.com>; +Cc: zam6ak@gmail.com, Peter Eisentraut <peter.eisentraut@2ndquadrant.com>; pgsql-bugs

"David G. Johnston" <david.g.johnston@gmail.com> writes:
> On Tue, Sep 12, 2017 at 2:20 PM, <zam6ak@gmail.com> wrote:
>> Function pg_get_serial_sequence returns NULL on columns defined as
>> IDENTITY

This seems like a reasonable complaint to me.

> ​I suspect this is an opportunity for documentation enhancement as opposed
> to a bug.

The reason appears to be that the dependency setup for a sequence created
to support IDENTITY is randomly different from that for a serial sequence.
I'm not sure this is a good thing.

			regards, tom lane


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



^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: BUG #14813: pg_get_serial_sequence does not return seqence name for IDENTITY columns
@ 2017-09-13 13:25  Alvaro Herrera <alvherre@alvh.no-ip.org>
  parent: Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 7+ messages in thread

From: Alvaro Herrera @ 2017-09-13 13:25 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: David G. Johnston <david.g.johnston@gmail.com>; zam6ak@gmail.com, Peter Eisentraut <peter.eisentraut@2ndquadrant.com>; pgsql-bugs

Tom Lane wrote:
> "David G. Johnston" <david.g.johnston@gmail.com> writes:

> > ​I suspect this is an opportunity for documentation enhancement as opposed
> > to a bug.
> 
> The reason appears to be that the dependency setup for a sequence created
> to support IDENTITY is randomly different from that for a serial sequence.
> I'm not sure this is a good thing.

It would be bad to break queries all over the internet that assume that
sequences for columns can be found using pg_get_serial_sequence, so I
agree this should be fixed.

-- 
Álvaro Herrera                https://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services


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



^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: BUG #14813: pg_get_serial_sequence does not return seqence name for IDENTITY columns
@ 2017-09-14 21:15  Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
  parent: Alvaro Herrera <alvherre@alvh.no-ip.org>
  0 siblings, 1 reply; 7+ messages in thread

From: Peter Eisentraut @ 2017-09-14 21:15 UTC (permalink / raw)
  To: Alvaro Herrera <alvherre@alvh.no-ip.org>; Tom Lane <tgl@sss.pgh.pa.us>; +Cc: David G. Johnston <david.g.johnston@gmail.com>; pgsql-bugs

On 9/13/17 09:25, Alvaro Herrera wrote:
> Tom Lane wrote:
>> "David G. Johnston" <david.g.johnston@gmail.com> writes:
> 
>>> ​I suspect this is an opportunity for documentation enhancement as opposed
>>> to a bug.
>>
>> The reason appears to be that the dependency setup for a sequence created
>> to support IDENTITY is randomly different from that for a serial sequence.
>> I'm not sure this is a good thing.
> 
> It would be bad to break queries all over the internet that assume that
> sequences for columns can be found using pg_get_serial_sequence, so I
> agree this should be fixed.

Proposed patch attached.

-- 
Peter Eisentraut              http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services

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

Attachments:

0001-Apply-pg_get_serial_sequence-to-identity-column-sequ.patch (text/plain, 7.7K) download From 05610bd6c456df0794f1de9b79d771baeeab2c47 Mon Sep 17 00:00:00 2001 From: Peter Eisentraut <peter_e@gmx.net> Date: Thu, 14 Sep 2017 17:12:56 -0400 Subject: [PATCH] Apply pg_get_serial_sequence() to identity column sequences as well Bug: #14813 --- doc/src/sgml/func.sgml | 36 ++++++++++++++++++++-------------- src/backend/utils/adt/ruleutils.c | 11 ++++++----- src/test/regress/expected/identity.out | 6 ++++++ src/test/regress/expected/sequence.out | 6 ++++++ src/test/regress/sql/identity.sql | 2 ++ src/test/regress/sql/sequence.sql | 2 ++ 6 files changed, 43 insertions(+), 20 deletions(-) diff --git a/doc/src/sgml/func.sgml b/doc/src/sgml/func.sgml index 641b3b8f4e..821fda274f 100644 --- a/doc/src/sgml/func.sgml +++ b/doc/src/sgml/func.sgml @@ -17034,8 +17034,7 @@ <title>System Catalog Information Functions</title> <row> <entry><literal><function>pg_get_serial_sequence(<parameter>table_name</parameter>, <parameter>column_name</parameter>)</function></literal></entry> <entry><type>text</type></entry> - <entry>get name of the sequence that a <type>serial</type>, <type>smallserial</type> or <type>bigserial</type> column - uses</entry> + <entry>get name of the sequence that a <type>serial</type>, <type>smallserial</type>, <type>bigserial</type>, or identity column uses</entry> </row> <row> <entry><literal><function>pg_get_statisticsobjdef(<parameter>statobj_oid</parameter>)</function></literal></entry> @@ -17223,19 +17222,26 @@ <title>System Catalog Information Functions</title> <para> <function>pg_get_serial_sequence</function> returns the name of the sequence associated with a column, or NULL if no sequence is associated - with the column. The first input parameter is a table name with - optional schema, and the second parameter is a column name. Because - the first parameter is potentially a schema and table, it is not treated - as a double-quoted identifier, meaning it is lower cased by default, - while the second parameter, being just a column name, is treated as - double-quoted and has its case preserved. The function returns a value - suitably formatted for passing to sequence functions (see <xref - linkend="functions-sequence">). This association can be modified or - removed with <command>ALTER SEQUENCE OWNED BY</>. (The function - probably should have been called - <function>pg_get_owned_sequence</function>; its current name reflects the fact - that it's typically used with <type>serial</> or <type>bigserial</> - columns.) + with the column. If the column is an identity column, the associated + sequence is the sequence internally created for the identity column. For + columns created using one of the serial types, it is the sequence created + for that serial column definition. In the latter case, this association + can be modified or removed with <command>ALTER SEQUENCE OWNED BY</>. (The + function probably should have been called + <function>pg_get_owned_sequence</function>; its current name reflects the + fact that it has typically been used with <type>serial</> + or <type>bigserial</> columns.) The first input parameter is a table name + with optional schema, and the second parameter is a column name. Because + the first parameter is potentially a schema and table, it is not treated as + a double-quoted identifier, meaning it is lower cased by default, while the + second parameter, being just a column name, is treated as double-quoted and + has its case preserved. The function returns a value suitably formatted + for passing to sequence functions + (see <xref linkend="functions-sequence">). A typical use is to read the + current value of a sequence for an identity or serial column, for example: +<programlisting> +SELECT currval(pg_get_serial_sequence('sometable', 'id')); +</programlisting> </para> <para> diff --git a/src/backend/utils/adt/ruleutils.c b/src/backend/utils/adt/ruleutils.c index 37d4ef043d..eb01f35463 100644 --- a/src/backend/utils/adt/ruleutils.c +++ b/src/backend/utils/adt/ruleutils.c @@ -2322,7 +2322,7 @@ pg_get_userbyid(PG_FUNCTION_ARGS) /* * pg_get_serial_sequence - * Get the name of the sequence used by a serial column, + * Get the name of the sequence used by an identity or serial column, * formatted suitably for passing to setval, nextval or currval. * First parameter is not treated as double-quoted, second parameter * is --- see documentation for reason. @@ -2380,13 +2380,14 @@ pg_get_serial_sequence(PG_FUNCTION_ARGS) Form_pg_depend deprec = (Form_pg_depend) GETSTRUCT(tup); /* - * We assume any auto dependency of a sequence on a column must be - * what we are looking for. (We need the relkind test because indexes - * can also have auto dependencies on columns.) + * Look for an auto dependency (serial column) or internal dependency + * (identity column) of a sequence on a column. (We need the relkind + * test because indexes can also have auto dependencies on columns.) */ if (deprec->classid == RelationRelationId && deprec->objsubid == 0 && - deprec->deptype == DEPENDENCY_AUTO && + (deprec->deptype == DEPENDENCY_AUTO || + deprec->deptype == DEPENDENCY_INTERNAL) && get_rel_relkind(deprec->objid) == RELKIND_SEQUENCE) { sequenceId = deprec->objid; diff --git a/src/test/regress/expected/identity.out b/src/test/regress/expected/identity.out index 88b56dad93..2800ed7caa 100644 --- a/src/test/regress/expected/identity.out +++ b/src/test/regress/expected/identity.out @@ -26,6 +26,12 @@ SELECT sequence_name FROM information_schema.sequences WHERE sequence_name LIKE --------------- (0 rows) +SELECT pg_get_serial_sequence('itest1', 'a'); + pg_get_serial_sequence +------------------------ + public.itest1_a_seq +(1 row) + CREATE TABLE itest4 (a int, b text); ALTER TABLE itest4 ALTER COLUMN a ADD GENERATED ALWAYS AS IDENTITY; -- error, requires NOT NULL ERROR: column "a" of relation "itest4" must be declared NOT NULL before identity can be added diff --git a/src/test/regress/expected/sequence.out b/src/test/regress/expected/sequence.out index a43b52cfc1..ea05a3382b 100644 --- a/src/test/regress/expected/sequence.out +++ b/src/test/regress/expected/sequence.out @@ -79,6 +79,12 @@ SELECT * FROM serialTest1; force | 100 (3 rows) +SELECT pg_get_serial_sequence('serialTest1', 'f2'); + pg_get_serial_sequence +--------------------------- + public.serialtest1_f2_seq +(1 row) + -- test smallserial / bigserial CREATE TABLE serialTest2 (f1 text, f2 serial, f3 smallserial, f4 serial2, f5 bigserial, f6 serial8); diff --git a/src/test/regress/sql/identity.sql b/src/test/regress/sql/identity.sql index a7e7b15737..7886456a56 100644 --- a/src/test/regress/sql/identity.sql +++ b/src/test/regress/sql/identity.sql @@ -12,6 +12,8 @@ CREATE TABLE itest3 (a smallint generated by default as identity (start with 7 i -- internal sequences should not be shown here SELECT sequence_name FROM information_schema.sequences WHERE sequence_name LIKE 'itest%'; +SELECT pg_get_serial_sequence('itest1', 'a'); + CREATE TABLE itest4 (a int, b text); ALTER TABLE itest4 ALTER COLUMN a ADD GENERATED ALWAYS AS IDENTITY; -- error, requires NOT NULL ALTER TABLE itest4 ALTER COLUMN a SET NOT NULL; diff --git a/src/test/regress/sql/sequence.sql b/src/test/regress/sql/sequence.sql index b41c5a753d..c50834a5b9 100644 --- a/src/test/regress/sql/sequence.sql +++ b/src/test/regress/sql/sequence.sql @@ -61,6 +61,8 @@ CREATE TABLE serialTest1 (f1 text, f2 serial); SELECT * FROM serialTest1; +SELECT pg_get_serial_sequence('serialTest1', 'f2'); + -- test smallserial / bigserial CREATE TABLE serialTest2 (f1 text, f2 serial, f3 smallserial, f4 serial2, f5 bigserial, f6 serial8); -- 2.11.0 (Apple Git-81)
^ permalink raw reply [nested|flat] 7+ messages in thread

* Re: BUG #14813: pg_get_serial_sequence does not return seqence name for IDENTITY columns
@ 2017-09-15 18:31  Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
  parent: Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
  0 siblings, 0 replies; 7+ messages in thread

From: Peter Eisentraut @ 2017-09-15 18:31 UTC (permalink / raw)
  To: Alvaro Herrera <alvherre@alvh.no-ip.org>; Tom Lane <tgl@sss.pgh.pa.us>; +Cc: David G. Johnston <david.g.johnston@gmail.com>; pgsql-bugs

On 9/14/17 17:15, Peter Eisentraut wrote:
> On 9/13/17 09:25, Alvaro Herrera wrote:
>> Tom Lane wrote:
>>> "David G. Johnston" <david.g.johnston@gmail.com> writes:
>>
>>>> ​I suspect this is an opportunity for documentation enhancement as opposed
>>>> to a bug.
>>>
>>> The reason appears to be that the dependency setup for a sequence created
>>> to support IDENTITY is randomly different from that for a serial sequence.
>>> I'm not sure this is a good thing.
>>
>> It would be bad to break queries all over the internet that assume that
>> sequences for columns can be found using pg_get_serial_sequence, so I
>> agree this should be fixed.
> 
> Proposed patch attached.

committed

-- 
Peter Eisentraut              http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services


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



^ permalink  raw  reply  [nested|flat] 7+ messages in thread


end of thread, other threads:[~2017-09-15 18:31 UTC | newest]

Thread overview: 7+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2017-09-12 21:20 BUG #14813: pg_get_serial_sequence does not return seqence name for IDENTITY columns zam6ak@gmail.com
2017-09-12 21:54 ` David G. Johnston <david.g.johnston@gmail.com>
2017-09-12 22:01   ` zambak zambak <zam6ak@gmail.com>
2017-09-12 23:47   ` Tom Lane <tgl@sss.pgh.pa.us>
2017-09-13 13:25     ` Alvaro Herrera <alvherre@alvh.no-ip.org>
2017-09-14 21:15       ` Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
2017-09-15 18:31         ` Peter Eisentraut <peter.eisentraut@2ndquadrant.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox