From: Adrian Klaver <adrian.klaver@aklaver.com>
To: Ray O'Donnell <ray@rodonnell.ie>
To: PostgreSQL <pgsql-general@postgresql.org>
Subject: Re: Resetting identity columns
Date: Mon, 22 Apr 2019 12:27:41 -0700
Message-ID: <a7d364bd-e587-a82b-8660-e453cc645fc0@aklaver.com> (raw)
In-Reply-To: <2864b568-76cf-c84b-52e6-8adee24962c3@rodonnell.ie>
References: <5db0ed5c-00ba-65fa-92f0-7e02c1495d18@rodonnell.ie>
<a1c07c88-ac43-992f-52c8-9a1e8318ee90@aklaver.com>
<2864b568-76cf-c84b-52e6-8adee24962c3@rodonnell.ie>
On 4/22/19 10:08 AM, Ray O'Donnell wrote:
> On 22/04/2019 17:02, Adrian Klaver wrote:
>
>> do language plpgsql $$
>> declare m_max_id bigint;
>> begin
>> select max(id) + 1 from identity_test into m_max_id;
>> EXECUTE 'alter table identity_test alter column id restart with ' ||
>> m_max_id;
>> end;
>> $$;
>
> Thanks a million Adrian - EXECUTE did the job, and I finished up
> wrapping it in a function as I used it in a number of places in the
> larger migration script:
>
> create function reset_identity(
> p_table text,
> p_column text
> )
> returns text
> as
> $$
> declare
> m_max_id bigint;
> begin
> execute 'select max(' || quote_ident(p_column) || ') + 1 from '
> || quote_ident(p_table) into m_max_id;
> execute 'alter table ' || quote_ident(p_table)
> || ' alter column ' || quote_ident(p_column)
> || ' restart with ' || m_max_id;
>
> return 'New identity value for ' || p_table || '.' || p_column
> || ': ' || m_max_id;
> end;
> $$
> language plpgsql;
>
>
> In general, then, is it not possible to use an expression thus? -
>
> [...] ALTER COLUMN [...] RESTART WITH <expression here>
No. It took some digging when I first ran into this to find out why. To
follow up on Tom's post and show where it is called out:
https://www.postgresql.org/docs/11/plpgsql-statements.html#PLPGSQL-STATEMENTS-EXECUTING-DYN
"Another restriction on parameter symbols is that they only work in
SELECT, INSERT, UPDATE, and DELETE commands. In other statement types
(generically called utility statements), you must insert values
textually even if they are just data values."
>
> Thanks,
>
> Ray.
>
>
--
Adrian Klaver
adrian.klaver@aklaver.com
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: adrian.klaver@aklaver.com, ray@rodonnell.ie
Subject: Re: Resetting identity columns
In-Reply-To: <a7d364bd-e587-a82b-8660-e453cc645fc0@aklaver.com>
References: <5db0ed5c-00ba-65fa-92f0-7e02c1495d18@rodonnell.ie> <a1c07c88-ac43-992f-52c8-9a1e8318ee90@aklaver.com> <2864b568-76cf-c84b-52e6-8adee24962c3@rodonnell.ie> <a7d364bd-e587-a82b-8660-e453cc645fc0@aklaver.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