From: Peter Eisentraut <peter.eisentraut@enterprisedb.com>
To: Erik Wienhold <ewie@ewie.name>
To: Ron <ronljohnsonjr@gmail.com>
To: pgsql-general@lists.postgresql.org
Subject: Re: Switching identity column to serial
Date: Wed, 8 Feb 2023 12:03:48 +0100
Message-ID: <87c8d1ad-e220-11db-8e3e-d66226feda74@enterprisedb.com> (raw)
In-Reply-To: <2023065411.42133.1675544152872@office.mailbox.org>
References: <1828022907.15226.1675472079824@office.mailbox.org>
<0400e92f-0d77-4276-be50-6e16321f4ae0@gmail.com>
<2023065411.42133.1675544152872@office.mailbox.org>
On 04.02.23 21:55, Erik Wienhold wrote:
>> Why doesn't this work?
>> BEGIN;
>> DROP SEQUENCE t_id;
[This won't work, you need to use ALTER TABLE / DROP IDENTITY.]
>> CREATE SEQUENCE new_t_id_seq AS INTEGER OWNED BY t.id;
>> ALTER SEQUENCE new_t_id_seq OWNER TO new_owner;
>> SELECT setval('new_t_id', (SELECT MAX(id) FROM t));
>> SELECT nextval('new_t_id');
>> COMMIT;
> This should work but I want to preserve the existing sequence instead of
> re-creating it with the same properties. That's why I was looking for a
> shortcut (also code golfing and sheer curiosity).
This is possible in principle, by implementing the inverse of the
"Upgrading" recipe from
<https://www.2ndquadrant.com/en/blog/postgresql-10-identity-columns/;.
But if you don't want to figure that out, I think dropping and
recreating the sequences as suggested here seems the best solution.
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-general@postgresql.org
Cc: peter.eisentraut@enterprisedb.com, ewie@ewie.name, ronljohnsonjr@gmail.com
Subject: Re: Switching identity column to serial
In-Reply-To: <87c8d1ad-e220-11db-8e3e-d66226feda74@enterprisedb.com>
References: <1828022907.15226.1675472079824@office.mailbox.org> <0400e92f-0d77-4276-be50-6e16321f4ae0@gmail.com> <2023065411.42133.1675544152872@office.mailbox.org> <87c8d1ad-e220-11db-8e3e-d66226feda74@enterprisedb.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox