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 1pmKAX-0006bS-6K for pgsql-general@arkaria.postgresql.org; Tue, 11 Apr 2023 20:00:46 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pmKAV-0001P6-Gj for pgsql-general@arkaria.postgresql.org; Tue, 11 Apr 2023 20:00:43 +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 1pmKAU-0001Ox-Dr for pgsql-general@lists.postgresql.org; Tue, 11 Apr 2023 20:00:43 +0000 Received: from out4-smtp.messagingengine.com ([66.111.4.28]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1pmKAM-002ICd-Cv for pgsql-general@lists.postgresql.org; Tue, 11 Apr 2023 20:00:41 +0000 Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailout.nyi.internal (Postfix) with ESMTP id 82ADC5C0209; Tue, 11 Apr 2023 16:00:31 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute1.internal (MEProxy); Tue, 11 Apr 2023 16:00:31 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= cc:cc:content-transfer-encoding:content-type:content-type:date :date:from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:sender:subject:subject:to:to; s=fm2; t= 1681243231; x=1681329631; bh=3v6mDWXOHP8IRKFWm4HZNsZy1Ad2PA0wrPj uGIqBMFE=; b=Pl2qsN851riQ2x9Qh7ux6uW34HeJSMqvV0+sKbV1Zrwi4/Pe9d9 N4BUhuEO/XUa6p2EAFLUBo3F5HCFu0GJbXDHd4bEaHo33I5EXdH/w9KJZuSxsXx5 f6Ep5S+U/lM8pKT5lkWdJoQfNhgZxkMDMqySp86bG6IXELMfrQntEoF2PfgMoy6h PVNqRzo0NNLqgT+VsxuuSmalmluMOCXCTnaKUUnKFs+IVH/aA6GAT+x4EwmYNsTf ETpOp40HgUFnC9eqOnLEjQByRkBjEJG+ahRkMlX6IK+iycsncrZyYIYaTkQNas66 eRmGhPDHF7dQvYFF8dMJ5a4mkqxuEv+PBXw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:content-type:date:date:feedback-id:feedback-id :from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:sender:subject:subject:to:to:x-me-proxy :x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm3; t= 1681243231; x=1681329631; bh=3v6mDWXOHP8IRKFWm4HZNsZy1Ad2PA0wrPj uGIqBMFE=; b=e3fREB4NbKsPv+VBuT6HFslaRE1l6DqXv6orxKI8EX1kn7ROIXM JymtAu8lB1yEY5xhtfQOlxM8kU9e25q+hXiTr2wqvLB/8KXpTrxHUcSJXCQJCZXI KH0yKkEmScr/yrRFE4Nz9h/x+FQ56nzQexm9ybiTBfHtJR6juYMwLy2reaJlYV49 p5Q4eeXfHqjS+ASXevHJiTXDmQ7jdBmBzF3vuOqbLpzqbkSYKi9OyWvZObkV/0Z+ 7Y0PY1RBkY4XQc0nezFTrny+fOXrsqjyr7gIrl9HNf7kXAM9DvKFmBoAPnAWmn4s foMY9YLDSgomrL7+DIpyjIDHLuGvsx0uEZg== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvhedrvdekgedgudeghecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecunecujfgurhepkfffgggfuffvvehfhfgjtgfgse htjeertddtfeejnecuhfhrohhmpeetughrihgrnhcumfhlrghvvghruceorggurhhirghn rdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomheqnecuggftrfgrthhtvghrnhepjeegtd fhuefhtefgffdvheduleetgeejudevffekteegjeeiheeuudellefgffffnecuvehluhhs thgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomheprggurhhirghnrdhklh grvhgvrhesrghklhgrvhgvrhdrtghomh X-ME-Proxy: Feedback-ID: i76984098:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Tue, 11 Apr 2023 16:00:30 -0400 (EDT) Message-ID: <9b21a499-9352-e1f6-4936-47b024e8b0ef@aklaver.com> Date: Tue, 11 Apr 2023 13:00:28 -0700 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.9.1 Subject: Re: Guidance on INSERT RETURNING order To: Federico , pgsql-general@lists.postgresql.org Cc: Mike Bayer References: Content-Language: en-US From: Adrian Klaver In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 4/11/23 12:47, Federico wrote: > Hello list, > > I have a few clarification questions regarding using insert with returning. > The use case is SQLAlchemy development, where the orm wants to insert > a list of rows, > get back the generated ids, defaults, etc, and match the returned values with > the original list of orm objects. > > The following assumes a table like this > > CREATE TABLE t( > id SERIAL, > data TEXT -- type here can be anything > ) > > On PostgreSQL, since SQLAlchemy version 1.4 (Mar 2021) this operation has used > > INSERT INTO t(data) VALUES ('a'), ('b'), ('c') RETURNING id > > but we were recently made aware that there is no guarantee on the > order of the returned columns. 1) Because returned data in SQL is inherently unordered. 2) What would you order by, id or data or both? > Sorry for the long email, > Thanks > > Federico > > -- Adrian Klaver adrian.klaver@aklaver.com