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 1pP5qW-0007UX-OT for pgsql-general@arkaria.postgresql.org; Mon, 06 Feb 2023 18:04:05 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pP5qV-0005P6-GI for pgsql-general@arkaria.postgresql.org; Mon, 06 Feb 2023 18:04:03 +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 1pP5qV-0005Ox-2E for pgsql-general@lists.postgresql.org; Mon, 06 Feb 2023 18:04:03 +0000 Received: from mx0c-001a4c01.pphosted.com ([67.231.158.153]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pP5qS-0003z3-3m for pgsql-general@lists.postgresql.org; Mon, 06 Feb 2023 18:04:02 +0000 Received: from pps.filterd (m0075701.ppops.net [127.0.0.1]) by mx0c-001a4c01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 316FAWNT032083; Mon, 6 Feb 2023 13:03:55 -0500 Received: from eur03-dba-obe.outbound.protection.outlook.com (mail-dbaeur03lp2177.outbound.protection.outlook.com [104.47.51.177]) by mx0c-001a4c01.pphosted.com (PPS) with ESMTPS id 3nhhgk9bhs-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 06 Feb 2023 13:03:55 -0500 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=FCLqR9S1hENtv4JIPuv7HtbLe98/PtHN34kik9ewKoK+ynzhj4ZqLZ59uGlbe9D5i7i3hv+XSF+gGL/W0OnoNPGH7h/MSQ0Ege2i7VdSn0P0o1oOAT1kbe6akWoHCXpJwbtkML4SuDemkDmyETBHKJNSyh1g50YbxxJ2g2/S4nOgFeWy6ddcnep3ecy+Y3tAbOHXyg77ehh/IELs0cHAbqGqMTFKtAVK2nXEENxo52ZX1HGRtSMgBAUG5YhPVxuk0DxpIz5fmZ+hLMLvqc+4Wfom9vhrsCNl7WDQoYl2tQ3YvgErmyhdwv/1xIybAJmUusKD32mK5fBYws/pvOEHoA== 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=ZzIfzkscLZ7WNUmYo3ljL5/VUjwA5osFJzDjfucT0FI=; b=kJPa6aMgNQzdE0/Bdo5hZRqe27GFXZ4Qwo8mvCNEs3XwsxQGkvNpd1qtzp05bsh7Uwu1vprDH6WOq9N3MpMAY0rHX851bF+cZBrqWddwbusXOrw1xjbcA3dBEVr0Kmnk+AcwAikx/WaG42xgIbA2G1sisu0B8FYTghOT2Ff71E9av4O2NO/j/t3sdP2SQal7a8IaCidvAwxb1j2O/UD9f8EMOUMSJcFDCC0VtAXhg+lH1YbWB78MpKNuafwwRiWO1JT6idp2v7WoLfghw8BRv8jfHFkjVfr7jlsD1s9wQ83ampgRt8y4HJlrs3Jp3Q0sgZaIapf+3GrhKBsNT+LZbA== 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=ZzIfzkscLZ7WNUmYo3ljL5/VUjwA5osFJzDjfucT0FI=; b=DA1HJ6UPqeGQjG5R3PkbpkpziAeM+d9XWDSpgMLOXBCPQAGcGL6NdxkksTu1GGIDHJDygqqdFDRc+Aos+RywIvWMpSWpizSdIPLtyWeSBbklnUiUx1X8zamOZUxJROwZpKP906nJcB07aYDeI7i2wpI3XoZwHbXhqkRvJvwHGn0= Received: from DBAP191MB1289.EURP191.PROD.OUTLOOK.COM (2603:10a6:10:1c9::19) by PR3P191MB0922.EURP191.PROD.OUTLOOK.COM (2603:10a6:102:4e::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6064.35; Mon, 6 Feb 2023 18:03:53 +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; Mon, 6 Feb 2023 18:03:53 +0000 From: Sebastien Flaesch To: Thomas Kellerer , "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+Fiv52aA7c8K7CMIsAgAADIYI= Date: Mon, 6 Feb 2023 18:03:53 +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_|PR3P191MB0922:EE_ x-ms-office365-filtering-correlation-id: 9c8c329c-e0e8-4cd1-01b1-08db086c81f6 x-volarisppoutbound: true x-ms-exchange-senderadcheck: 1 x-ms-exchange-antispam-relay: 0 x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: 19Evu2+BLpEwx5xy4hhsW/CjNa5MqWqR0GUguG6a5ZbQuai3F8wCb7IHulW4i+z8O87JMIuHpH/HRVfxZ3UpnA9U+ToPoFggIiU+rQVQ1sl23U47AKxbp29J/h1xsXQdLGZRo+Abn3D/rg/ms++4xXLenHSg/uXDj0wUa9OOTHXbXERGtuFPmIIx0EnPIjl5p3ghvqoyhLO8tsLOXoJSOpNs5nMBLr86pf6ckwL1Hx3w7ms7JGIdSIfXtC+hEx80xknC5z+nnNx3Sqc68cgfxJxz8e3L+T0Y94QD2ptUCAWGgKkuU+ehgH9VDX3RbyjcuXSgqJNEB7IQdtF2cMJ3khsRWa6Fy9AxxNbse9F6c8GklH7Q1sK4tPh4VrvAuSB9MAcxXOurSjILOIUGYLoVHdm5Lcw5DMveCfVCLy2LikGUhIcXm0XPxJV0S1GTmC+P+MUJ8TICHVZYE1m26l6QCFrUdrjpJs1beB1C7Vx52Y1inxir6liWhO1U3RO59XfcjOpdRC4Z196Vk/AuKtx2BGrN6s+1k+v5Z9wTLJuuECc9x5fws4wZwYOOBadWnNCmaFB/iKlo1/SNb0scZII4ZxTnv7l+u3M8zRIkRrYza6PhTs5YSQHebS/3EYuDk/a9b5UZuJltQffoxBlWWCjr5aF+tYXR3F0VNVnYxXBcXUOKNLc+LelNsuBNMlz0yPQG8tUYC4A+fU2YDhWV5/Vhm1MUjzBRVko7ep01UxXFlMc= 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)(396003)(39860400002)(376002)(136003)(366004)(346002)(451199018)(19627405001)(55016003)(33656002)(44832011)(5660300002)(8936002)(52536014)(2906002)(478600001)(6506007)(186003)(53546011)(66556008)(76116006)(66946007)(7696005)(9686003)(26005)(66476007)(66446008)(38070700005)(38100700002)(64756008)(83380400001)(8676002)(41300700001)(91956017)(110136005)(122000001)(86362001)(71200400001)(316002)(15583001);DIR:OUT;SFP:1102; x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: =?us-ascii?Q?jyCu840i3DmfW9q4cca0yBPbd0GMTOn5h0DUtdbYCgWgt4bhawvPcsOZZft+?= =?us-ascii?Q?FVDQGfYiGdpSjg9IsMGuAsvjeFl+p3dIRN5TCCTbRt6OdnNHv7T3L7XxP/Wm?= =?us-ascii?Q?embphSY5VgsXbOqSb8msn5RiWTuA74iSpgysIJEVPZAPn0wTvHgjTGB9FzFA?= =?us-ascii?Q?FPhMnAmBEqbvnQwKCqYVgev+l1wwu7tBcMNlyyHrpcHWaQdPm8+aRQOFjejR?= =?us-ascii?Q?GzNPCr53ML7UtqJNfcLrDkmWCdsz5JzL17pGsxpPH+DDr9PLHtmRfSaKCjR3?= =?us-ascii?Q?pcDkLIf8V7Xqhsa0gDJiMyFZsLQqXIThTw9IvCvQxP4tkvYY8u8Go6Ifek67?= =?us-ascii?Q?ktJ0SUDlInEdu3b22cmEhD2OigjzkfFTTWxW0nHfZuqbQZi8aFPEfj4xlcQu?= =?us-ascii?Q?9/j/1mRNHGUtyzznu/BiV8cabAPZac05r6xkcUGEJKzHzX3PLQ33FxA4GJ4q?= =?us-ascii?Q?Tpa+p/yy3KqyFLurwRSxRjAAH1WvChNtgXOe5Y7LMGY0sv+wLtiJaYYIsNV+?= =?us-ascii?Q?bhScfLFgSB/qHLKe8RITbO4FRv75MKNuUP0e2mUEgCP+Qi9xIIIP2v9mete7?= =?us-ascii?Q?QxvyPiPsHSRReVRyPGlEDIRKn3okTjBTeq/+VXkKro5xq3bu4Rk1adefEjmp?= =?us-ascii?Q?Z6lcVX6vykAjh9OIm9Hzmhc6bzmQGsy+I6O9kUyJZv81BbsvEaq7t9JHWoFg?= =?us-ascii?Q?x4j3gxqUdG2Qq2+gGmx6G8Vov6FTBK0H9uO65TT0J5zgYkyNuid5nJZKvnjS?= =?us-ascii?Q?ONFGyUyupgvOPZUlcwezHonxbpgTbaJiFkOdmozxDmT4yEf8Gh61RvjRq+OD?= =?us-ascii?Q?JWArxaNuoRgxu3B9hnc1lqaQLtRmZ70E73Jf9pFUl3B4ORIyeRayJT0RsL5r?= =?us-ascii?Q?JNGkp9Io9+ST2Ffjaiq+KUqmTfHu+xojF4OjBMZJ1WlM0HkE6CaRsDrY9k1v?= =?us-ascii?Q?DlgWJrvpHlhLWInH9E8VOk+o/t0L5FZ3NW6UO8TCQQJvqITp66cVe5UJYHaV?= =?us-ascii?Q?N7dCM2Pp/feRNnvHZaBTcZLnkejyVkzQpqE4zHbZ2LF4o1NREaW43Pm9iuJL?= =?us-ascii?Q?htuFBAu1TKMKh81vt3H6RIXwA/FtJaqMYh4mEc/+jeh1dqNIGuE0TLCM7LdC?= =?us-ascii?Q?4qRj86Gt4P1S000Ui3pQ2hhPlw+cRyksJydVoxRke0lBqSYlbcaEQGhxBZNW?= =?us-ascii?Q?238DuhPFOpBagXinPuNTniosZ4PlOsmjYWd3bdYyu99nDzum+BVKDcAAqe/P?= =?us-ascii?Q?bPYYM6pb7wGIW4SbVzQGWHiZ2nplkCRlw2QbmAKShXtB9V4RpVoTzY28gyaM?= =?us-ascii?Q?ctEfMW7ze92KGM+NPLCu8MQHm9LX7zBRnuKwHGmkgMT7n5nMN0jYei7bVwJr?= =?us-ascii?Q?yygW0BlswyQ0l1ZuAuMLUueuXva/I0aA2EmiwWD8Yfjr3zCWRZm3cgILx7UW?= =?us-ascii?Q?Kk+n8OUgxppv7msKT9PGdeKPSNg7QqrgjNW++r6qdndzLoL8iyqBSLqZiSHs?= =?us-ascii?Q?/i0s7mEApq/g0OSSBXkfutJ9MyhCFY14UHD5bzkmTfcBPaN2ZddLMJ+lvjqI?= =?us-ascii?Q?d7tFu9zjWOpV6Uj6b7GfivevoI7gqx2P4OR5+Ok2?= Content-Type: multipart/alternative; boundary="_000_DBAP191MB1289E9989C9F934FC4E54979B0DA9DBAP191MB1289EURP_" MIME-Version: 1.0 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: nHsh8VlwqQ+8dadu+Ww1QworIJgdGm67ARjUej05/QYJlBnUnAErlC8z2yYJ75U6/raTPVif/MYSZWyYV6J3hYY+9TW0tbK84T9FAUY9fvkcmbyWpAbv/THVyyVgUb3ATuqnHg79MtR28wztKhqQW3SSBoULq2/KpAC8i6Iv7FGATucd0Fpa2QVcXwPvOlGfitoKSMrk4Qilge9n1KfXdWWy3ohlT7BRT+jpFOrro4YfIP9G3qgm77th482cra8GBpdH4uLg/fdfA/b5ZbzQ1YNrOFpymr1WrxYr/z7iygR44S4upQ0OI3K9ma5FbMhxqHjUtQSSahMFnsduH9sFiFbQPUD7xdx7DuWNoDUtzgNmdIxHGOPPK+78zyx+DJ4WuNcDxeFQA9x6y9+UDYHIJMBWIqh05Ff06/IOtQMSByCg0W2Y3vBv7XfcqimGvwzdxgxOlJTEVphCdQIi6Kvv8e/Cc4RNj2QPVfEaa/A/NJGzjrGhm4LlwnbcLIHAd5TglSxIQ7ydfUT7VP2FkIfC3PytKJDUm4CuJq4s/1su7dhGIH+8UanjhwYTy0GoTlchBKl1uBoKuzHGquXy6JXWKiELxDFQqpIun5T1EfEm25AME0mFqMjpYf4WTbWHXbyrW4bt7LIpAjEj4XzTjnWmVJp2QtHLoIHONSaBDdFe1d/Bt8H5CHABnzx867yL1KOHoG0ziI80Q77t4hTRLSpkzJWICspzU/cHUnQECGsTawELSXmhnuoIq+dNJH0qeet7E+uJXZmtxedEKdWWEH1DAV9Bnwu929Y+DFgmQN5iJxkqV9sMavLaC/yXcN1DgIYCbI9+hm51VMmU/0OH+DvsIw== 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: 9c8c329c-e0e8-4cd1-01b1-08db086c81f6 X-MS-Exchange-CrossTenant-originalarrivaltime: 06 Feb 2023 18:03:53.2618 (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: 4rKDnnG42ko5gbRFLFb78lfs0d/asAvJzmunagBizFCGGxKjJAuL4ponOGbpPJhcyXJwnnWg5qZ7eVjIcDdAGrCvbmnYG27q7SZgN5BAT1A= X-MS-Exchange-Transport-CrossTenantHeadersStamped: PR3P191MB0922 X-Proofpoint-GUID: CaEpKf90yF9U82c4FnLn4oGXQIs2qHA1 X-Proofpoint-ORIG-GUID: CaEpKf90yF9U82c4FnLn4oGXQIs2qHA1 X-ProofpointHeader: Yes X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 mlxlogscore=999 mlxscore=0 phishscore=0 clxscore=1011 priorityscore=1501 bulkscore=0 malwarescore=0 impostorscore=0 adultscore=0 spamscore=0 suspectscore=0 lowpriorityscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2212070000 definitions=main-2302060157 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --_000_DBAP191MB1289E9989C9F934FC4E54979B0DA9DBAP191MB1289EURP_ Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable 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_DBAP191MB1289E9989C9F934FC4E54979B0DA9DBAP191MB1289EURP_ Content-Type: text/html; charset="us-ascii" Content-Transfer-Encoding: quoted-printable

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_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')<= /div>
   and pg_get_expr(a.adbin,0) like = '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 <s= hammat@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 attachments i= f 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_DBAP191MB1289E9989C9F934FC4E54979B0DA9DBAP191MB1289EURP_--