From f52c5d6749a61fc4e0908457c58f5069910d53a6 Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Fri, 12 Mar 2021 11:08:42 -0500 Subject: [PATCH] Forbid marking an identity column as nullable. GENERATED ALWAYS AS IDENTITY implies NOT NULL, but the code failed to complain if you overrode that with "GENERATED ALWAYS AS IDENTITY NULL". One might think the old behavior was a feature, but it was inconsistent because the outcome varied depending on the order of the clauses, so it seems to have been just an oversight. Per bug #16913 from Pavel Boev. Back-patch to v10 where identity columns were introduced. Vik Fearing (minor tweaks by me) Discussion: https://postgr.es/m/16913-3b5198410f67d8c6@postgresql.org --- doc/src/sgml/ref/create_table.sgml | 1 + src/backend/parser/parse_utilcmd.c | 10 ++++++++++ src/test/regress/expected/identity.out | 13 +++++++++++++ src/test/regress/sql/identity.sql | 9 +++++++++ 4 files changed, 33 insertions(+) diff --git a/doc/src/sgml/ref/create_table.sgml b/doc/src/sgml/ref/create_table.sgml index 3b2b227683..71703da85a 100644 --- a/doc/src/sgml/ref/create_table.sgml +++ b/doc/src/sgml/ref/create_table.sgml @@ -840,6 +840,7 @@ WITH ( MODULUS numeric_literal, REM column. It will have an implicit sequence attached to it and the column in new rows will automatically have values from the sequence assigned to it. + Such a column is implicitly NOT NULL. diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index 75266caeb4..d56f81c79f 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -719,7 +719,17 @@ transformColumnDefinition(CreateStmtContext *cxt, ColumnDef *column) column->identity = constraint->generated_when; saw_identity = true; + + /* An identity column is implicitly NOT NULL */ + if (saw_nullable && !column->is_not_null) + ereport(ERROR, + (errcode(ERRCODE_SYNTAX_ERROR), + errmsg("conflicting NULL/NOT NULL declarations for column \"%s\" of table \"%s\"", + column->colname, cxt->relation->relname), + parser_errposition(cxt->pstate, + constraint->location))); column->is_not_null = true; + saw_nullable = true; break; } diff --git a/src/test/regress/expected/identity.out b/src/test/regress/expected/identity.out index fbca0333a2..99811570b7 100644 --- a/src/test/regress/expected/identity.out +++ b/src/test/regress/expected/identity.out @@ -547,3 +547,16 @@ CREATE TABLE itest14 (id serial); ALTER TABLE itest14 ALTER id DROP DEFAULT; ALTER TABLE itest14 ALTER id ADD GENERATED BY DEFAULT AS IDENTITY; INSERT INTO itest14 (id) VALUES (DEFAULT); +-- Identity columns must be NOT NULL (cf bug #16913) +CREATE TABLE itest15 (id integer GENERATED ALWAYS AS IDENTITY NULL); -- fail +ERROR: conflicting NULL/NOT NULL declarations for column "id" of table "itest15" +LINE 1: ...ABLE itest15 (id integer GENERATED ALWAYS AS IDENTITY NULL); + ^ +CREATE TABLE itest15 (id integer NULL GENERATED ALWAYS AS IDENTITY); -- fail +ERROR: conflicting NULL/NOT NULL declarations for column "id" of table "itest15" +LINE 1: CREATE TABLE itest15 (id integer NULL GENERATED ALWAYS AS ID... + ^ +CREATE TABLE itest15 (id integer GENERATED ALWAYS AS IDENTITY NOT NULL); +DROP TABLE itest15; +CREATE TABLE itest15 (id integer NOT NULL GENERATED ALWAYS AS IDENTITY); +DROP TABLE itest15; diff --git a/src/test/regress/sql/identity.sql b/src/test/regress/sql/identity.sql index cdda186720..52800f265c 100644 --- a/src/test/regress/sql/identity.sql +++ b/src/test/regress/sql/identity.sql @@ -346,3 +346,12 @@ CREATE TABLE itest14 (id serial); ALTER TABLE itest14 ALTER id DROP DEFAULT; ALTER TABLE itest14 ALTER id ADD GENERATED BY DEFAULT AS IDENTITY; INSERT INTO itest14 (id) VALUES (DEFAULT); + +-- Identity columns must be NOT NULL (cf bug #16913) + +CREATE TABLE itest15 (id integer GENERATED ALWAYS AS IDENTITY NULL); -- fail +CREATE TABLE itest15 (id integer NULL GENERATED ALWAYS AS IDENTITY); -- fail +CREATE TABLE itest15 (id integer GENERATED ALWAYS AS IDENTITY NOT NULL); +DROP TABLE itest15; +CREATE TABLE itest15 (id integer NOT NULL GENERATED ALWAYS AS IDENTITY); +DROP TABLE itest15;