Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fSjJH-0003Dq-4z for pgsql-general@arkaria.postgresql.org; Tue, 12 Jun 2018 13:26:07 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fSjJF-0000R7-SB for pgsql-general@arkaria.postgresql.org; Tue, 12 Jun 2018 13:26:05 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fSjJD-0000Ox-Rr for pgsql-general@lists.postgresql.org; Tue, 12 Jun 2018 13:26:05 +0000 Received: from out1-smtp.messagingengine.com ([66.111.4.25]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fSjJA-0000jt-1t for pgsql-general@lists.postgresql.org; Tue, 12 Jun 2018 13:26:02 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id F1A1B21AAA; Tue, 12 Jun 2018 09:25:58 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute6.internal (MEProxy); Tue, 12 Jun 2018 09:25:58 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= cc:content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-sender :x-me-sender:x-sasl-enc; s=fm3; bh=zYn7GA/F39Izk4TB1FFX6d5K0S8on Ol1tvzf33Z9Bic=; b=eUjJFL4kWyIzHnTahwbPU7i+Mi7LFnYIpmg3KVP9azgQ8 l6eT0N6ILhwqfG6Uox6lt7SOQkywkAcNXBuhtnPfAC62qyzUmiKoX4bGvUa9wYDH FdjGEAiMK+dysA7FMBdHMx++5SXG3R4YrFrgkgViTFHBFquFdfXwp1Ez7ZZRPC8o lbyp6rj+YXfyNTc2Z+BN5SMrGUYcb/A0e3d9XeLiiaiFMbaHUw8Li65ST6JlACK1 1f31W1VSVut85wiU9QQTpqUrbcP7O2gZEvIq6gBsMX/pB3w/5U6dyP7PQo4J8PVh KBequgL2SqSPtdyUSZZm3izb5qqeUmM5cOo3xrmvw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-sender:x-me-sender:x-sasl-enc; s=fm3; bh=zYn7GA /F39Izk4TB1FFX6d5K0S8onOl1tvzf33Z9Bic=; b=oIoi0aZ6bdmtV4kZbdddLK af39jqZ5nQGzpY4Xr4JCGrXCy88m+Y1YP0QCWT5ZNpiCGBiCwcLTahqeVADkPp5P QaTNo0WsZqF5tMqbTUX37AaYfx6FXqB2V6srWjXNYxWX1o5ZDle+gqBKQ++MvwgL WbkF5wxXJEnetcr1SzrNNGiL0MV/GvUFY2V3MwRLPr2nLRlsxIA8wqQS8Qg8UygV h6wpFQxIczKfG2xec7NE7AN4uPZ2TR+Rjl2QRR6Y7ki82TfXgggkKZsECvgmzrSG VKBNKyX0aBQphXv4JAiNPwUsLF2poKLpnwCHT5ofFyr4bSZyI/GLnhoLpnZfGB/g == X-ME-Proxy: X-ME-Proxy: X-ME-Proxy: X-ME-Proxy: X-ME-Proxy: X-ME-Proxy: X-ME-Sender: Received: from [192.168.1.3] (unknown [71.212.159.209]) by mail.messagingengine.com (Postfix) with ESMTPA id 4C85310262; Tue, 12 Jun 2018 09:25:58 -0400 (EDT) Subject: Re: Semantics around INSERT INTO with SELECT and ORDER BY. To: Steve Krenzel , sravikrishna3@gmail.com Cc: pgsql-general@lists.postgresql.org References: From: Adrian Klaver Message-ID: Date: Tue, 12 Jun 2018 06:25:57 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.8.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 06/12/2018 01:39 AM, Steve Krenzel wrote: > This is relevant for tables that have a column with a SERIAL type, I > need to guarantee that the relative ordering remains the same as the > ordering of the selected result set. > > More concretely, given: > >     > CREATE TABLE foo (id SERIAL, val TEXT); >     > CREATE TABLE bar (id SERIAL, val TEXT); >     > INSERT INTO foo (val) VALUES ('A'), ('B'), ('C'); >     > TABLE foo; >      id | val >     ----+----- >       1 | A >       2 | B >       3 | C >     (3 rows) > > Then, > >     > INSERT INTO bar (val) SELECT val FROM foo ORDER BY id DESC; >     > TABLE bar; >      id | val >     ----+----- >       1 | C >       2 | B >       3 | A >     (3 rows) > > The rows should be inserted in reverse. (Note: I don't care about the > actual value of the id, only the relative ordering). > > Inserting more values should similarly append into the table in order > (where "append" is used in terms of the serial id). > >     > INSERT INTO bar (val) SELECT val FROM foo ORDER BY id DESC; >     > TABLE bar; >      id | val >     ----+----- >       1 | C >       2 | B >       3 | A >       4 | C >       5 | B >       6 | A >     (6 rows) > > Or to put it another way, I want to select values from one table ordered > by complex criteria and insert them into another table. I want to be > able to retrieve the rows from the target table in the same order they > were inserted, but I don't care about the specific ordering criteria. I > only care about the order they were inserted. That will only work until some other INSERT or UPDATE occurs. Using table from your example: UPDATE bar SET val = 'C1' where id = 1; TABLE bar; id | val ----+----- 2 | B 3 | A 4 | C 5 | B 6 | A 1 | C1 (6 rows) You can use CLUSTER: https://www.postgresql.org/docs/10/static/sql-cluster.html to reestablish order based on an index, though that has the same issue: "Clustering is a one-time operation: when the table is subsequently updated, the changes are not clustered." As has been pointed out order of rows is not guaranteed. > > On Tue, Jun 12, 2018 at 1:24 AM Ravi Krishna > wrote: > > Why is it even important?  Once you use ORDER BY clause, you are > guaranteed to get the rows in the order.  Why do you need how it was > inserted in the first place. > -- Adrian Klaver adrian.klaver@aklaver.com