From: Sebastien Flaesch <sebastien.flaesch@4js.com>
To: Thomas Kellerer <shammat@gmx.net>
To: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>
Subject: Re: Get the sequence name corresponding to a GENERATED { ALWAYS | BY DEFAULT } AS IDENTITY column
Date: Mon, 6 Feb 2023 18:03:53 +0000
Message-ID: <DBAP191MB1289E9989C9F934FC4E54979B0DA9@DBAP191MB1289.EURP191.PROD.OUTLOOK.COM> (raw)
In-Reply-To: <d0102694-471a-08c2-faf7-27e0366cb834@gmx.net>
References: <DBAP191MB12892E942BD10BC2F46A354EB0DA9@DBAP191MB1289.EURP191.PROD.OUTLOOK.COM>
<d0102694-471a-08c2-faf7-27e0366cb834@gmx.net>
I am writing generic code and don't know in advance the name of identity column.
Only the table name is known (parsing of INSERT INTO tabname statement is easy)
Column names may use uppercase or lowercase letters.
I guess I could figure out what column is defined as identity, from pg_attrribute.attidentity = 'd' / 'a', since I know the table name...
For serial/bigserial/smallserial types, I am using:
select ns.nspname||'.'||substring(pg_get_expr(a.adbin,0) from 'nextval.''([^'']*)') seqname, c.attname
from pg_class p
join pg_attrdef a on (p.oid=a.adrelid)
join pg_attribute c on (p.oid=c.attrelid and a.adnum=c.attnum)
join pg_namespace ns on (p.relnamespace=ns.oid)
where upper(p.relname)=upper('tab1')
and pg_get_expr(a.adbin,0) like 'nextval%'
and (ns.nspname=current_schema() or ns.oid=pg_my_temp_schema());
I hope it's possible to do something similar for identity columns.
Seb
________________________________
From: Thomas Kellerer <shammat@gmx.net>
Sent: Monday, February 6, 2023 6:43 PM
To: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>
Subject: Re: Get the sequence name corresponding to a GENERATED { ALWAYS | BY DEFAULT } AS IDENTITY column
EXTERNAL: Do not click links or open attachments if you do not recognize the sender.
Sebastien Flaesch schrieb am 06.02.2023 um 18:17:
> Assuming that a sequence is used to implement |GENERATED { ALWAYS | BY DEFAULT } AS IDENTITY|
>
> Is there any built-in function that returns the underlying sequence name used for such column?
>
> Otherwise, an SQL query to return the sequence name?
>
> I need the sequence name, in order to reset it (setval) or to get the last generated value (currval) ...
>
> The query must work with all PostgreSQL versions 10 to 15 ...
Despite its name pg_get_serial_sequence() also works for identity columns
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: sebastien.flaesch@4js.com, shammat@gmx.net
Subject: Re: Get the sequence name corresponding to a GENERATED { ALWAYS | BY DEFAULT } AS IDENTITY column
In-Reply-To: <DBAP191MB1289E9989C9F934FC4E54979B0DA9@DBAP191MB1289.EURP191.PROD.OUTLOOK.COM>
References: <DBAP191MB12892E942BD10BC2F46A354EB0DA9@DBAP191MB1289.EURP191.PROD.OUTLOOK.COM> <d0102694-471a-08c2-faf7-27e0366cb834@gmx.net> <DBAP191MB1289E9989C9F934FC4E54979B0DA9@DBAP191MB1289.EURP191.PROD.OUTLOOK.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