Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pPLRW-0005Nh-Ix for pgsql-general@arkaria.postgresql.org; Tue, 07 Feb 2023 10:43:19 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pPLRV-0007u6-C7 for pgsql-general@arkaria.postgresql.org; Tue, 07 Feb 2023 10:43:17 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pPLRU-0007tu-Pi for pgsql-general@lists.postgresql.org; Tue, 07 Feb 2023 10:43:17 +0000 Received: from mx0d-001a4c01.pphosted.com ([67.231.151.23]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pPLRR-000875-1w for pgsql-general@lists.postgresql.org; Tue, 07 Feb 2023 10:43:15 +0000 Received: from pps.filterd (m0210020.ppops.net [127.0.0.1]) by mx0c-001a4c01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 3179SlgR029802 for ; Tue, 7 Feb 2023 05:43:10 -0500 Received: from eur05-vi1-obe.outbound.protection.outlook.com (mail-vi1eur05lp2174.outbound.protection.outlook.com [104.47.17.174]) by mx0c-001a4c01.pphosted.com (PPS) with ESMTPS id 3nhhkekyee-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Tue, 07 Feb 2023 05:43:09 -0500 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=B/csxeTZ9mx3YvOJm0Pllj24u4ih0wOD0OtIhhYOMXPMiGzCZgDmXZco5JIkzqlXzxxgV/rxalYX9BhEAYcOkTvCz2GmiVldlCwnKHm10NirXCF0pHusmM7P2XDvAzPo8PoUqr1tJAFznuLJKlk/Vj3WSy4WHgXbnsLKC5ZNSc2fraFjGtfikR5Ou2SvrVpo20gsMwLk/l/lzQPohkSHR09NWNhZ9+aamHBwVPbCj8mP9zgUWRp+VxKsBQXcNdpeniNslf9GwgzZEthH5WSy6f7yd95bLTg7Kdy3MI/LJkdoELWfmfEVuuedKJTTYRBxLcZxOE9uRyuZBMBkEwd1bg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=h/tbb1lTEmpHmTqc60VNuPMcHxw6xS4dBLA4J2yjfO8=; b=ULfxAMOSpCwjhvNFAoca8nqcQePzChJ9rZU3MyoCEp7DRNNmDyqhIMUH/JGopN3onG2typ30j8hH715MBZmR0YrCnP8r2vQLhPXv9mwvmNN/yCH0JPh49gqW6D2ZXDMpgVr5JaL6qVkE+ZWFkb4EJkreJ1vddVQhbGvnJTtYsDQg6ngIDmwKzvZxN0Ugjgv35SPr0sCa5MnMxwlwH+aIYkjumv3v3DWJbZExQtFwjq1g+FCXeKJMdTYPLANwJN7iExraU+EhY+USn920f0+7+ZyrBj6NcqliiMylmWZxFLyStNh4o0S3aNe7BjWlnlMyHIeYLm6IdHDzEMpGyxbanA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=4js.com; dmarc=pass action=none header.from=4js.com; dkim=pass header.d=4js.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ourvolaris.onmicrosoft.com; s=selector2-ourvolaris-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=h/tbb1lTEmpHmTqc60VNuPMcHxw6xS4dBLA4J2yjfO8=; b=oeedDSsCwFdK7ECFFVzvaqa5uDa3zs2NkTongiZWhx/tjiWvXXqNkRCzmt6JudQrIGeHEEjCYVrr06RHUgQKIBuk0DkQY7+vEyQ0udfb1XHoKxECLh7Ac/jmOw7usQBBIcO3XA4H9eYLX3jlXKPpKb81WZRcDkKZGS1XskujlpU= Received: from DBAP191MB1289.EURP191.PROD.OUTLOOK.COM (2603:10a6:10:1c9::19) by DB8P191MB0812.EURP191.PROD.OUTLOOK.COM (2603:10a6:10:141::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6064.34; Tue, 7 Feb 2023 10:43:06 +0000 Received: from DBAP191MB1289.EURP191.PROD.OUTLOOK.COM ([fe80::1a8d:b5bc:c2a4:1e48]) by DBAP191MB1289.EURP191.PROD.OUTLOOK.COM ([fe80::1a8d:b5bc:c2a4:1e48%8]) with mapi id 15.20.6064.034; Tue, 7 Feb 2023 10:43:06 +0000 From: Sebastien Flaesch To: "pgsql-general@lists.postgresql.org" Subject: Re: Get the sequence name corresponding to a GENERATED { ALWAYS | BY DEFAULT } AS IDENTITY column Thread-Topic: Get the sequence name corresponding to a GENERATED { ALWAYS | BY DEFAULT } AS IDENTITY column Thread-Index: AQHZOk3x4kbB9OrqUU+Fiv52aA7c8K7CMIsAgAADIYKAAAOo64ABEAEd Date: Tue, 7 Feb 2023 10:43:06 +0000 Message-ID: References: In-Reply-To: Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: msip_labels: x-ms-publictraffictype: Email x-ms-traffictypediagnostic: DBAP191MB1289:EE_|DB8P191MB0812:EE_ x-ms-office365-filtering-correlation-id: 4bc0505a-3730-413f-b951-08db08f81910 x-volarisppoutbound: true x-ms-exchange-senderadcheck: 1 x-ms-exchange-antispam-relay: 0 x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: fuStNdCUAbXEU5vsSW/J9+wtBzHkMYzaDRN934ZwPLFeNapD5l9IrtYiO2EcoAsjmcV52Dm4iCCeU6Utxxg2CSM0wWdsNtp5wVljCVVL75MjUbAHuRz47ojZQ9vAoEVaqGhtW3rg0nzeCDhxhb37rW+JIESyDiBhcY2N7YUFzwbbluKZWW7fmj+Vn19E0P08jNSdmLzYsuZ3Nx+5MaYDnKaJnCaEaeNlUET8dIUpFrkK0V94vWJkFlONStZl125Uhy3OwDiMo0Pq0Lj1PwsqqpNSPCMVLZ9LGXIr1QyRRI9/wpqc8EuAH6p4NG0baE/eW41ouAb1mOT6xQQ5FBU1hBTgVvAmMMVzh5fZ0HjQjk8LlpQQXbx3lV/RzZcd2KCmTjk7kEBGAL3nR7Q4iJrekaCm/lC+0MY/S9XbllA9h8QQa6gQhcNLahv6b597Afdq5VnRbuLS6Jji1QIKBb3rZ6F/tD2qZOVPqXy0lO8F2sFjH16MDoBjd7GXAHebWzeZvRJgy7ZzvYpY4QWpUkNYjQIw1JjxOHGYdac5t5OsoQ4vCgfB+mTRJS1nSyr0kgvTz2ma7RlLPs1dNBa5H7l2/vrdup+5UIoPm63Lp9hDO4CVJ55TKxWqQWGwFWHL/8HOhTjaQ3RSKFX8VXHRIhgJFKUKLNG8qIJ3IJ8hGdhaNtpGAoYwvygYIAWr6zdY5OdMcbssSF2UWa/pucDVcfeml9KpRzf5iSnvLt7L3jTPIrs= x-forefront-antispam-report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DBAP191MB1289.EURP191.PROD.OUTLOOK.COM;PTR:;CAT:NONE;SFS:(13230025)(4636009)(136003)(346002)(376002)(39860400002)(366004)(396003)(451199018)(186003)(33656002)(6506007)(9686003)(53546011)(26005)(38070700005)(38100700002)(122000001)(478600001)(52536014)(2906002)(41300700001)(8936002)(55016003)(71200400001)(66446008)(5660300002)(7696005)(44832011)(66946007)(64756008)(83380400001)(8676002)(316002)(66476007)(6916009)(66556008)(86362001)(76116006)(91956017)(19627405001)(15583001);DIR:OUT;SFP:1102; x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: =?us-ascii?Q?fRAYl3ZujPuzNtfF21aCgfJpLUKvQGENefwTT08eLkXnZjlaCFaa1klev5c3?= =?us-ascii?Q?bgVxWFHIM6szShVKxkmttghVYiMoI5g5f3qy5yLj8cuwX0ZTw+MB9AzHIAyU?= =?us-ascii?Q?TQibW7wtdgY2/OBBXInEwE2uCaAnoeZ1dgGhz/IL0F+hl5oh7tr4+DHtn+2m?= =?us-ascii?Q?joXoMY9yu+soLWwVub3ssYOJ6pZQZE61cK8Av7N7I3ko+WzB/+VCpz4fd9fr?= =?us-ascii?Q?rbQPBNVT8GBzaEp66L68tJrnqsmOHMLUstK9MbTxeFfHtM1SuwHoLLno7GdL?= =?us-ascii?Q?9iJlZBUEZ523c5s2azhmQjb8BxjIA2J/S2GTOYcqYY1IdFrzQpyjuk5/+Lkj?= =?us-ascii?Q?ukDLdZFSgAsukKcdUuGiywkmF8aP3/0mnKfn4B1L9Y9V906Fp3H0TRqoYz/S?= =?us-ascii?Q?w4BJjy0fJHi+hCScfEnX3xYD0t8ovhazDhR2uwy9e8QeIk8CoMHZ9LbrQRER?= =?us-ascii?Q?YYzybB/Uv/MEt2TXrI2mj+TaINL0eptUPQfxyNPtLagDFzJ5FxuYaZ+4BEVg?= =?us-ascii?Q?FCgtOcpJ6dq9AQH8Bv+Z9Jeqd0WMiognnpn8PcY8TnA55/BDTPNxPns6BDMN?= =?us-ascii?Q?gBCiJGEx0NwyZph2AOb0cE6rJon+UxhaSfdRvp+VeYOU827+I6xI2CL3o9G9?= =?us-ascii?Q?AXWnsxL7Uy/zekOjtFYNNE459j4gcvZtkVk0nu4oAySi/UuFDTBa7E7LmTeH?= =?us-ascii?Q?NqpoKUcTgT48tBQgugBm53R8/1i+V43ECZsCePGTktu7meoUJMoaGf6zayzr?= =?us-ascii?Q?zMjssxnfL9W4EVbJ+CFXgM2AUzOySg2OQxY+SyC5J2KlL5VAyEXX6eDkhj+y?= =?us-ascii?Q?MsAUv4y0iHPHxbAa9zK9QdGfqm6DofPLoIz90ZTggRKnx/a5kwEYxN86OqFM?= =?us-ascii?Q?gKqWBWMuaKeA39Tms/QsFGwYwLNBnxUrXpmcgheYlrIlC8+Ek9njftGzfdQA?= =?us-ascii?Q?SYwEtbwZdB4r2JN3Iwy0yI5mF71CzBweSTAsEr3eO1B4lK9TOa+iJvPRZBnH?= =?us-ascii?Q?t9MaGU9228ttqLgqCFmqWUYoMUWAaF2Nyx3LNbhSIWk5MoNCWhgjoS4IPUqJ?= =?us-ascii?Q?xjjdDqgtRwD+IZwBcttycxX/qo/tHupZ/5QSybtnvbtCEYqyUda8GpMLRPUM?= =?us-ascii?Q?vXserFNj8ONzYd6FLACHplFde1CgkupyDr45l1zOSbzPRa/lfOm4b9Iu1O/m?= =?us-ascii?Q?utoTfT0oNyPPnyZnZ6GLCDVeA2mjF0GqXVGJd1pLZSeGsJ01LnNb9UtRI9Q0?= =?us-ascii?Q?hk6oF8Ati7UXoFpFAHClxhAwGPOkaK2VpE1AHEZfZyamn1Yi1wivmppncmt4?= =?us-ascii?Q?M+k2skP7W6yLVJ71eJqz2dtBu8+Y1emS1ipkgBdy+0OqnN7B+348qkCmO9B/?= =?us-ascii?Q?ftld89EYgFxQk9cA13UzlFzTHjkaitdJPHlUemM00dSbqQ9R1lcdRn/sEzWy?= =?us-ascii?Q?9J27EyAmG18iteq8lx44kiUqFFIrkQGrS0Oq82hW7DA3hkaMPYatz3FI1heU?= =?us-ascii?Q?ZhCrCFxTgcdD9D8FgjKdrTd2CAXkcwJZTtUdq9qkowXn2qBlvKErALyzacYY?= =?us-ascii?Q?2/OCvi6L+irAxN3Cn67is9XQV26mdQxpbn/Mf8Ha?= Content-Type: multipart/alternative; boundary="_000_DBAP191MB12892B0FF0E108C08A92B655B0DB9DBAP191MB1289EURP_" MIME-Version: 1.0 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: rwRs92gpclKF//cnk9IsQKrSbHvpZTr37iRSK3GqFVuTVt0smIwEI7UYU6YtUJ3/OKI3FSIxP6yAw+reke3bReC5BOgA3pDPyTezUyAqgSztBSuo1fEFtjTxESNnfvaIOBawXjlQvkZY6IqxLDC/zAbipHj3yE6UjiT0DeWEtyjOp1Nt+95kOfHgx+DVnekfL9rexqwDpXH9XaYjcBuQ8/xf8rkG1QYbAn0uoB/tcQpgMXl93LR4HDrHOMvF8KH/grlVDWQeIi14aEazy5XdR4sdGV7rAt26tcJEKnNX/y7F1WPZzLb1Ge9IvsVU9pTLYWNI7lLM8WaNwHmqlbAe6e0Lo/pOPPi9C7awL1YkzTfqQ58jxME1KLciiQmcWOESa8nioWdtf6SZa0X5RkEoRDERDPMkLxhVeVai35rrrWO/YF8wMgL2n15V6Ny9CdQDVZEAGVnDJ1NV2uB8CwDd1VznC3YPTEc4OHkxjS3bqaCD6KBlKKH3bd4tsUrGG6yF25m7LceQfpLnBvUMjW1KKUPdMCjmG67nir75ti40scb1OkwyH08xzJsvikKI+y4PgH1wueTph5aM+WyhYVM3w9YFVn8caPoVg069MkxhE42arLtrH7FcVCOO1oAnFsOqkpYMCklAqywnffhfci7MoYElLs+BU7+CO5fUfH177cZOSMZbcNRKTHKgk4WHJl4paCzo+mj+iZJoVGx2NZh17724QGNtb7rKmG7OZtCS5CeA6eogrktnKLgda6GpsH/suY2nKQrb3jprp64AFhBkKrunYM+SVLb3EBhG5f4gxY9PgmOQdJs7v+TD7c8G7LTM X-OriginatorOrg: 4js.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-AuthSource: DBAP191MB1289.EURP191.PROD.OUTLOOK.COM X-MS-Exchange-CrossTenant-Network-Message-Id: 4bc0505a-3730-413f-b951-08db08f81910 X-MS-Exchange-CrossTenant-originalarrivaltime: 07 Feb 2023 10:43:06.7870 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 75c696ec-5bfb-4892-9a0c-9187a9061cd6 X-MS-Exchange-CrossTenant-mailboxtype: HOSTED X-MS-Exchange-CrossTenant-userprincipalname: qqofknQ3v0urAExa9Ilo3ZXCk0OQ99Fvpa4yhPK0X1T6GeC3yMA/61pKSKSpfeBUTE/Mji5gAokos+TPddtRRmGfBiOM82DXjBbBWP+HzsY= X-MS-Exchange-Transport-CrossTenantHeadersStamped: DB8P191MB0812 X-Proofpoint-GUID: D5VD2jLcGjPxdVlHpXBQgutdPWPGnSEN X-Proofpoint-ORIG-GUID: D5VD2jLcGjPxdVlHpXBQgutdPWPGnSEN X-ProofpointHeader: Yes X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 impostorscore=0 malwarescore=0 phishscore=0 spamscore=0 lowpriorityscore=0 priorityscore=1501 bulkscore=0 adultscore=0 clxscore=1015 suspectscore=0 mlxscore=0 mlxlogscore=999 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2212070000 definitions=main-2302070095 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --_000_DBAP191MB12892B0FF0E108C08A92B655B0DB9DBAP191MB1289EURP_ Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable Just sharing my conclusion so far, maybe this could do the job? Check the usage of double quotes, to manage upper/lower case in schema and = table name. This is not needed for the column name, according to the doc of pg_get_seri= al_sequence(). with table_sequence as ( select '"'||ns.nspname||'"."'||p.relname||'"' tabname, c.attname colname from pg_class p join pg_attribute c on p.oid=3Dc.attrelid join pg_namespace ns on (p.relnamespace=3Dns.oid) where upper(p.relname)=3Dupper('tab1') and c.attidentity =3D 'd' and (ns.nspname=3Dcurrent_schema() or ns.oid=3Dpg_my_temp_schema()) ) select tabname, colname, pg_get_serial_sequence(tabname,colname) from table_sequence; Any better suggestion? Seb ________________________________ From: Sebastien Flaesch Sent: Monday, February 6, 2023 7:11 PM To: Thomas Kellerer ; 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 th= e sender. ... and I would like to automatically return the last generated identity, a= nd also reset the sequence, if a value was specified explicitly for the ide= ntity column and this value is greater than the last generated value. With serial/bigserial/smallserial I am adding a returning clause to each IN= SERT: insert into tab1 (name) VALUES ('aaa') returning tab1.pkey, (select case when tab1.pkey > (select last_value from public.tab1_= pkey_seq) then setval('public.tab1_pkey_seq',tab1.pkey,true) else 0 e= nd) Seb ________________________________ From: Sebastien Flaesch Sent: Monday, February 6, 2023 7:03 PM To: Thomas Kellerer ; 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 th= e sender. I am writing generic code and don't know in advance the name of identity co= lumn. Only the table name is known (parsing of INSERT INTO tabname statement is e= asy) Column names may use uppercase or lowercase letters. I guess I could figure out what column is defined as identity, from pg_attr= ribute.attidentity =3D '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=3Da.adrelid) join pg_attribute c on (p.oid=3Dc.attrelid and a.adnum=3Dc.attnum) join pg_namespace ns on (p.relnamespace=3Dns.oid) where upper(p.relname)=3Dupper('tab1') and pg_get_expr(a.adbin,0) like 'nextval%' and (ns.nspname=3Dcurrent_schema() or ns.oid=3Dpg_my_temp_schema()); I hope it's possible to do something similar for identity columns. Seb ________________________________ From: Thomas Kellerer Sent: Monday, February 6, 2023 6:43 PM To: 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 th= e sender. Sebastien Flaesch schrieb am 06.02.2023 um 18:17: > Assuming that a sequence is used to implement |GENERATED { ALWAYS | BY DE= FAULT } 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 las= t 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 --_000_DBAP191MB12892B0FF0E108C08A92B655B0DB9DBAP191MB1289EURP_ Content-Type: text/html; charset="us-ascii" Content-Transfer-Encoding: quoted-printable
Just sharing my conclusion so far, maybe this could do the job?

Check the usage of double quotes, to manage upper/lower case in schema and = table name.
This is not needed for the column name, according to the doc of pg_get_seri= al_sequence().


with table_seque= nce as (
select '"'||ns.nspname||'"."'||p.relname||'&qu= ot;' tabname, c.attname colname
  from pg_class p
       join pg_attribute c on p.oid=3Dc.a= ttrelid
       join pg_namespace ns on (p.relname= space=3Dns.oid)
 where upper(p.relname)=3Dupper('tab1')
   and c.attidentity =3D 'd'
   and (ns.nspname=3Dcurrent_schema() or ns.oid=3Dp= g_my_temp_schema())
)
select tabname, colname, pg_get_serial_sequence(tabname,colna= me)
  from table_sequence;


Any better suggestion?

Seb

From: Sebastien Flaesch <= ;sebastien.flaesch@4js.com>
Sent: Monday, February 6, 2023 7:11 PM
To: Thomas Kellerer <shammat@gmx.net>; pgsql-general@lists.pos= tgresql.org <pgsql-general@lists.postgresql.org>
Subject: Re: Get the sequence name corresponding to a GENERATED { AL= WAYS | BY DEFAULT } AS IDENTITY column
 

EXTERNAL: Do not c= lick links or open attachments if you do not recognize the sender.=

... and I would like to automatically return the last generated identity, a= nd also reset the sequence, if a value was specified explicitly for the ide= ntity column and this value is greater than the last generated value.

With serial/bigserial/smallserial I am adding a returning clause to each IN= SERT:

insert into tab1 (name) VALUES ('aaa')
      returning tab1.pkey,
         (select case when tab1.pke= y > (select last_value from public.tab1_pkey_seq)
            &nb= sp;   then setval('public.tab1_pkey_seq',tab1.pkey,true) else 0 e= nd)

Seb

From: Sebastien Flaesch &= lt;sebastien.flaesch@4js.com>
Sent: Monday, February 6, 2023 7:03 PM
To: Thomas Kellerer <shammat@gmx.net>; pgsql-general@lists.pos= tgresql.org <pgsql-general@lists.postgresql.org>
Subject: Re: Get the sequence name corresponding to a GENERATED { AL= WAYS | BY DEFAULT } AS IDENTITY column
 

EXTERNAL: Do not c= lick links or open attachments if you do not recognize the sender.=


I am writing generic code and don't know in advance the name of identity co= lumn.

Only the table name is known (parsing of INSERT INTO tabname stateme= nt is easy)

Column names may use uppercase or lowercase letters.

I guess I could figure out what column is defined as identity, from pg_attr= ribute.attidentity =3D '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_attrde= f a on (p.oid=3Da.adrelid)
       join pg_attrib= ute c on (p.oid=3Dc.attrelid and a.adnum=3Dc.attnum)
       join pg_namesp= ace ns on (p.relnamespace=3Dns.oid)
 where upper(p.relname)=3Dupper('tab= 1')
   and pg_get_expr(a.adbin,0) l= ike 'nextval%'
   and (ns.nspname=3Dcurrent_schema() or ns.oid=3Dpg_my_temp_sche= ma());

I hope it's possible to do something similar for identity columns.

Seb


From: Thomas Kellerer &= lt;shammat@gmx.net>
Sent: Monday, February 6, 2023 6:43 PM
To: pgsql-general@lists.postgresql.org <pgsql-general@lists.postg= resql.org>
Subject: Re: Get the sequence name corresponding to a GENERATED { AL= WAYS | BY DEFAULT } AS IDENTITY column
 
EXTERNAL: Do not click links or open attachmen= ts 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 na= me 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

--_000_DBAP191MB12892B0FF0E108C08A92B655B0DB9DBAP191MB1289EURP_--