1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-07 00:36:50 +03:00

Apply pg_get_serial_sequence() to identity column sequences as well

Bug: #14813
This commit is contained in:
Peter Eisentraut
2017-09-15 14:04:51 -04:00
parent f0e60ee4bc
commit 3012061b86
6 changed files with 44 additions and 20 deletions

View File

@ -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;

View File

@ -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

View File

@ -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);

View File

@ -12,6 +12,8 @@ SELECT table_name, column_name, column_default, is_nullable, is_identity, identi
-- 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;

View File

@ -61,6 +61,8 @@ INSERT INTO serialTest1 VALUES ('wrong', NULL);
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);