1

I want to create a table, which contains a nullable column having GENERATED BY DEFAULT AS IDENTITY option, therefore I run the following query:

CREATE TABLE my_table (
  generated INTEGER NULL GENERATED BY DEFAULT AS IDENTITY,
  data TEXT NOT NULL
);

But once I try to insert a row in the table, which generated field is null like this:

INSERT INTO my_table(generated, data) VALUES(NULL, "some data");

I get a null-constraint violation error.

However if I change the order of my_table.generated column properties:

CREATE TABLE my_table (
  generated INTEGER GENERATED BY DEFAULT AS IDENTITY NULL,
  data TEXT NOT NULL
);

It inserts rows, which generated field is NULL, without any issues. Is this the expected behavior for the case?

  • Looks like a bug in the engine to me. Good catch! You should report it to the PostgreSQL team. I verified the bug affects PosgreSQL 10, 11, 12, and 13. – The Impaler Mar 04 '21 at 03:59
  • @TheImpaler, I appreciate your efforts, thanks! – Deedee Megadoodoo Mar 04 '21 at 04:03
  • 1
    You can report the bug in the mailing list: pgsql-bugs@lists.postgresql.org or at the end of the page https://www.postgresql.org/ – The Impaler Mar 04 '21 at 04:07
  • @TheImpaler I wonder if this is intentional because the old `serial` "type" also included an implicit `not null` - but there is nothing in the manual that suggests that an identity column does this as well. So this might be "just" a documentation bug. –  Mar 04 '21 at 08:05
  • @a_horse_with_no_name Could be. However, it's definitively a very odd behavior that the location of the `NULL` clause produces a different constraint in the database. Still looks like a bug to me. – The Impaler Mar 04 '21 at 13:04
  • @DeedeeMegadoodoo: why did you delete your answer? I think posting that the Postgres devs have accepted this [as a bug](https://www.postgresql.org/message-id/7c02b352-a2e4-e71a-ae69-7e410daf5774%40postgresfriends.org) is a valid answer for this site. –  Mar 05 '21 at 05:21
  • @a_horse_with_no_name I wasn't sure that my answer was related to both GENERATED BY DEFAULT AS IDENTITY and GENERATED ALWAYS AS IDENTITY expressions so I just deleted it, because they added tests for the latter case only. I read postgres sources a bit and it looks like these tests cover both of the expressions since they are both just variations of the same constraint. – Deedee Megadoodoo Mar 05 '21 at 06:21

1 Answers1

0

Postgres developers told me this is a bug since identity columns weren't supposed to be nullable (see the patch file under the response).