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 1pnWPl-0000ZW-6Z for pgsql-general@arkaria.postgresql.org; Sat, 15 Apr 2023 03:17:25 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pnWPi-0004p1-7A for pgsql-general@arkaria.postgresql.org; Sat, 15 Apr 2023 03:17:22 +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 1pnWPh-0004oq-Sb for pgsql-general@lists.postgresql.org; Sat, 15 Apr 2023 03:17:21 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1pnWPa-002ydq-CB for pgsql-general@lists.postgresql.org; Sat, 15 Apr 2023 03:17:20 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 33F3H9wL1608814; Fri, 14 Apr 2023 23:17:09 -0400 From: Tom Lane To: Federico cc: "David G. Johnston" , John Howroyd , pgsql-general@lists.postgresql.org Subject: Re: Guidance on INSERT RETURNING order In-reply-to: References: <95fbd8c8-2f7a-38a1-4a41-b2ac3763d89c@aklaver.com> <6644419d-50e1-97c9-5507-b6915bec96fa@gmail.com> <961adda9-4eb1-79f6-ee8f-9440a520ac15@gmail.com> <20230414164036.labzn5sbqzye7jvg@hjp.at> Comments: In-reply-to Federico message dated "Fri, 14 Apr 2023 21:46:37 +0200" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <1608812.1681528629.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Fri, 14 Apr 2023 23:17:09 -0400 Message-ID: <1608813.1681528629@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Federico writes: > Would something like what was proposed by Mike Bayer be considered? >> A new token called "tuple_order" or something >> = >> INSERT INTO table (a, b, c) VALUES ((1, 2, 3), (4, 5, 6), ...) RETURNIN= G table.id, inserted.tuple_order >> = >> tuple_order would be incrementing values 1, 2, 3, 4, 5, ... which corre= late the each row delivered by RETURNING to each entry in the VALUES claus= e, in the order they were stated in that VALUES clause, that is entry (1, = 2, 3) would be tuple_order 1, entry (4, 5, 6) would be tuple order 2, etc. As proposed, I don't think so. Something over in the RETURNING clause has exactly no connection to VALUES. What do you do if it's INSERT ... SELECT and there are several VALUES clauses down inside the SELECT? There is some prior art in this area, though. See the more-or-less SQL-standard WITH ORDINALITY option for functions-in-FROM. It seems to me that it could be plausible to attach WITH ORDINALITY to a VALUES clause, which would give you a rock-solid connection between the VALUES rows and the ordinality-column values, and then you could include that column in RETURNING. regards, tom lane