Re: Identity columns should own only one sequence

Поиск
Список
Период
Сортировка
От Peter Eisentraut
Тема Re: Identity columns should own only one sequence
Дата
Msg-id 57b6aae1-e487-611b-3377-d372273e0ae0@2ndquadrant.com
обсуждение исходный текст
Ответ на Identity columns should own only one sequence  (Laurenz Albe <laurenz.albe@cybertec.at>)
Ответы Re: Identity columns should own only one sequence  (Laurenz Albe <laurenz.albe@cybertec.at>)
Список pgsql-hackers
On 2019-04-14 17:51, Laurenz Albe wrote:
> Identity columns don't work if they own more than one sequence.

Well, they shouldn't, because then how do they know which sequence they
should use?

> So if one tries to convert a "serial" column to an identity column,
> the following can happen:
> 
> test=> CREATE TABLE ser(id serial);
> CREATE TABLE
> test=> ALTER TABLE ser ALTER id ADD GENERATED ALWAYS AS IDENTITY;
> ERROR:  column "id" of relation "ser" already has a default value
> 
> Hm, ok, let's drop the column default value.
> 
> test=> ALTER TABLE ser ALTER id DROP DEFAULT;
> ALTER TABLE
> 
> Now it works:
> 
> test=> ALTER TABLE ser ALTER id ADD GENERATED ALWAYS AS IDENTITY;
> ALTER TABLE
> 
> But not very much:
> 
> test=> INSERT INTO ser (id) VALUES (DEFAULT);
> ERROR:  more than one owned sequence found

You also need to run

ALTER SEQUENCE ser_id_seq OWNED BY NONE;

because dropping the default doesn't release the linkage of the sequence
with the table.  These are just weird artifacts of how serial is
implemented, but that's why identity columns were added to improve
things.  I don't think we need to make things more complicated here.

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



В списке pgsql-hackers по дате отправления:

Предыдущее
От: Rafia Sabih
Дата:
Сообщение: Re: [PATCH v1] Show whether tables are logged in \dt+
Следующее
От: Laurenz Albe
Дата:
Сообщение: Re: Identity columns should own only one sequence