Received: from localhost (unknown [200.46.204.187]) by postgresql.org (Postfix) with ESMTP id 5EA462E0083 for ; Thu, 13 Dec 2007 02:38:03 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.187]) (amavisd-maia, port 10024) with ESMTP id 48855-09 for ; Thu, 13 Dec 2007 02:37:57 -0400 (AST) Received: from www01.kensystem.com (www01.kensystem.com [72.19.170.120]) by postgresql.org (Postfix) with ESMTP id 9A66F2E2FD1 for ; Thu, 13 Dec 2007 02:37:58 -0400 (AST) Received: from 192.168.0.3 by www01.kensystem.com (SMTPD); id s20071212233751.55410; Wed, 12 Dec 2007 23:37:51 Message-ID: <4760D33E.40004@kensystem.com> Date: Wed, 12 Dec 2007 23:37:50 -0700 From: Ken Johanson User-Agent: Thunderbird 1.5.0.13 (Windows/20070809) MIME-Version: 1.0 To: pgsql-general@postgresql.org Subject: Re: RETURNING clause: how to specifiy column indexes? References: <22a9f7570703301215y44a937d0r5faa706bad7948c@mail.gmail.com> <4610AA0C.30608@ejurka.com> <47579D44.6060005@kensystem.com> <47579FBE.1070503@kensystem.com> <475F7766.2040005@kensystem.com> <12796.1197439644@sss.pgh.pa.us> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200712/869 X-Sequence-Number: 125612 Kris Jurka wrote: > > I think the expectation is that: > > CREATE TABLE t(a int, b int); > INSERT INTO t(b,a) VALUES (1,2) RETURNING *; > > will return 1,2 instead of 2,1 as it does now. In this case the op is > not expecting that the (potentially reorganized) table order is driving > the results, but the order that they've actually specified the columns > in creates the result. > Kris, do you have pointers to a spec that says the named-columns should specify the index, or that it should instead be the order in the table? My interpretation from the JDBC spec was that the latter is true, I may be wrong... In the case where it is table-order, then I presume in PG that the "natural" order of the columns (even if reordering is allowed at a alter date) is specified by data in one of the pg_* tables (pg_class, pg_index, etc). Does anyone know if this is true/false? If true, my next idea would be to derive the column name using a subquery in the returning clause. But it sounds like this may have potential security contraints (will any INSERT query always have read access to the PG tables?). And no guarantee of the order matching in the long term. Is there a more elegant approach, like?: INSERT... RETURNING (PG_LIST_KEYS(tblname)) I looked but did not find such a utility. It seems that such function would be best implemented in the server instead of in a driver (eg. having hardcoded subquery to the schema). Ken