Commit 3012061b authored by Peter Eisentraut's avatar Peter Eisentraut

Apply pg_get_serial_sequence() to identity column sequences as well

Bug: #14813
parent f0e60ee4
...@@ -17034,8 +17034,7 @@ SELECT pg_type_is_visible('myschema.widget'::regtype); ...@@ -17034,8 +17034,7 @@ SELECT pg_type_is_visible('myschema.widget'::regtype);
<row> <row>
<entry><literal><function>pg_get_serial_sequence(<parameter>table_name</parameter>, <parameter>column_name</parameter>)</function></literal></entry> <entry><literal><function>pg_get_serial_sequence(<parameter>table_name</parameter>, <parameter>column_name</parameter>)</function></literal></entry>
<entry><type>text</type></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 <entry>get name of the sequence that a serial or identity column uses</entry>
uses</entry>
</row> </row>
<row> <row>
<entry><literal><function>pg_get_statisticsobjdef(<parameter>statobj_oid</parameter>)</function></literal></entry> <entry><literal><function>pg_get_statisticsobjdef(<parameter>statobj_oid</parameter>)</function></literal></entry>
...@@ -17223,19 +17222,27 @@ SELECT pg_type_is_visible('myschema.widget'::regtype); ...@@ -17223,19 +17222,27 @@ SELECT pg_type_is_visible('myschema.widget'::regtype);
<para> <para>
<function>pg_get_serial_sequence</function> returns the name of the <function>pg_get_serial_sequence</function> returns the name of the
sequence associated with a column, or NULL if no sequence is associated sequence associated with a column, or NULL if no sequence is associated
with the column. The first input parameter is a table name with with the column. If the column is an identity column, the associated
optional schema, and the second parameter is a column name. Because sequence is the sequence internally created for the identity column. For
the first parameter is potentially a schema and table, it is not treated columns created using one of the serial types
as a double-quoted identifier, meaning it is lower cased by default, (<type>serial</type>, <type>smallserial</type>, <type>bigserial</type>), it
while the second parameter, being just a column name, is treated as is the sequence created for that serial column definition. In the latter
double-quoted and has its case preserved. The function returns a value case, this association can be modified or removed with <command>ALTER
suitably formatted for passing to sequence functions (see <xref SEQUENCE OWNED BY</>. (The function probably should have been called
linkend="functions-sequence">). This association can be modified or <function>pg_get_owned_sequence</function>; its current name reflects the
removed with <command>ALTER SEQUENCE OWNED BY</>. (The function fact that it has typically been used with <type>serial</>
probably should have been called or <type>bigserial</> columns.) The first input parameter is a table name
<function>pg_get_owned_sequence</function>; its current name reflects the fact with optional schema, and the second parameter is a column name. Because
that it's typically used with <type>serial</> or <type>bigserial</> the first parameter is potentially a schema and table, it is not treated as
columns.) 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 in reading 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>
<para> <para>
......
...@@ -2322,7 +2322,7 @@ pg_get_userbyid(PG_FUNCTION_ARGS) ...@@ -2322,7 +2322,7 @@ pg_get_userbyid(PG_FUNCTION_ARGS)
/* /*
* pg_get_serial_sequence * 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. * formatted suitably for passing to setval, nextval or currval.
* First parameter is not treated as double-quoted, second parameter * First parameter is not treated as double-quoted, second parameter
* is --- see documentation for reason. * is --- see documentation for reason.
...@@ -2380,13 +2380,14 @@ pg_get_serial_sequence(PG_FUNCTION_ARGS) ...@@ -2380,13 +2380,14 @@ pg_get_serial_sequence(PG_FUNCTION_ARGS)
Form_pg_depend deprec = (Form_pg_depend) GETSTRUCT(tup); Form_pg_depend deprec = (Form_pg_depend) GETSTRUCT(tup);
/* /*
* We assume any auto dependency of a sequence on a column must be * Look for an auto dependency (serial column) or internal dependency
* what we are looking for. (We need the relkind test because indexes * (identity column) of a sequence on a column. (We need the relkind
* can also have auto dependencies on columns.) * test because indexes can also have auto dependencies on columns.)
*/ */
if (deprec->classid == RelationRelationId && if (deprec->classid == RelationRelationId &&
deprec->objsubid == 0 && deprec->objsubid == 0 &&
deprec->deptype == DEPENDENCY_AUTO && (deprec->deptype == DEPENDENCY_AUTO ||
deprec->deptype == DEPENDENCY_INTERNAL) &&
get_rel_relkind(deprec->objid) == RELKIND_SEQUENCE) get_rel_relkind(deprec->objid) == RELKIND_SEQUENCE)
{ {
sequenceId = deprec->objid; sequenceId = deprec->objid;
......
...@@ -26,6 +26,12 @@ SELECT sequence_name FROM information_schema.sequences WHERE sequence_name LIKE ...@@ -26,6 +26,12 @@ SELECT sequence_name FROM information_schema.sequences WHERE sequence_name LIKE
--------------- ---------------
(0 rows) (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); 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 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 ERROR: column "a" of relation "itest4" must be declared NOT NULL before identity can be added
......
...@@ -79,6 +79,12 @@ SELECT * FROM serialTest1; ...@@ -79,6 +79,12 @@ SELECT * FROM serialTest1;
force | 100 force | 100
(3 rows) (3 rows)
SELECT pg_get_serial_sequence('serialTest1', 'f2');
pg_get_serial_sequence
---------------------------
public.serialtest1_f2_seq
(1 row)
-- test smallserial / bigserial -- test smallserial / bigserial
CREATE TABLE serialTest2 (f1 text, f2 serial, f3 smallserial, f4 serial2, CREATE TABLE serialTest2 (f1 text, f2 serial, f3 smallserial, f4 serial2,
f5 bigserial, f6 serial8); f5 bigserial, f6 serial8);
......
...@@ -12,6 +12,8 @@ SELECT table_name, column_name, column_default, is_nullable, is_identity, identi ...@@ -12,6 +12,8 @@ SELECT table_name, column_name, column_default, is_nullable, is_identity, identi
-- internal sequences should not be shown here -- internal sequences should not be shown here
SELECT sequence_name FROM information_schema.sequences WHERE sequence_name LIKE 'itest%'; 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); 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 ADD GENERATED ALWAYS AS IDENTITY; -- error, requires NOT NULL
ALTER TABLE itest4 ALTER COLUMN a SET NOT NULL; ALTER TABLE itest4 ALTER COLUMN a SET NOT NULL;
......
...@@ -61,6 +61,8 @@ INSERT INTO serialTest1 VALUES ('wrong', NULL); ...@@ -61,6 +61,8 @@ INSERT INTO serialTest1 VALUES ('wrong', NULL);
SELECT * FROM serialTest1; SELECT * FROM serialTest1;
SELECT pg_get_serial_sequence('serialTest1', 'f2');
-- test smallserial / bigserial -- test smallserial / bigserial
CREATE TABLE serialTest2 (f1 text, f2 serial, f3 smallserial, f4 serial2, CREATE TABLE serialTest2 (f1 text, f2 serial, f3 smallserial, f4 serial2,
f5 bigserial, f6 serial8); f5 bigserial, f6 serial8);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment