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 1fSkGJ-000804-FO for pgsql-general@arkaria.postgresql.org; Tue, 12 Jun 2018 14:27: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 1fSkGI-0004jC-2t for pgsql-general@arkaria.postgresql.org; Tue, 12 Jun 2018 14:27:06 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fSkGH-0004j2-Qg for pgsql-general@lists.postgresql.org; Tue, 12 Jun 2018 14:27:05 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fSkGF-0005ct-67 for pgsql-general@lists.postgresql.org; Tue, 12 Jun 2018 14:27:05 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id w5CEQxt3029387; Tue, 12 Jun 2018 10:26:59 -0400 From: Tom Lane To: "David G. Johnston" cc: Steve Krenzel , "sravikrishna3@gmail.com" , "pgsql-general@lists.postgresql.org" Subject: Re: Semantics around INSERT INTO with SELECT and ORDER BY. In-reply-to: References: Comments: In-reply-to "David G. Johnston" message dated "Tue, 12 Jun 2018 05:29:57 -0700" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <29385.1528813619.1@sss.pgh.pa.us> Date: Tue, 12 Jun 2018 10:26:59 -0400 Message-ID: <29386.1528813619@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk "David G. Johnston" writes: > On Tuesday, June 12, 2018, 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. > The logical insertion order, and thus the sequence values, will be assigned > according to the order by. What is actually going to happen, given say create table targ (d text, id serial); insert into targ select x from src order by y; is that you're going to get a parse tree equivalent to select x, nextval('targ_id_seq') from (select x from src order by y) ss; and then it's a question of whether the planner is capable of reordering the steps into something you don't want. I think that the presence of the explicit "ORDER BY" in the sub-select will prevent flattening of the sub-select, which is enough to make it safe. However, if for some reason you did not say "ORDER BY" but nonetheless expected the serial values to get assigned in the same order that the underlying query would produce rows natively, you might get burnt. As of 9.6, there are more guarantees in this area than there used to be (cf commit 9118d03a8), but I don't think it matters as long as you write an ORDER BY. regards, tom lane