Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hqjiF-0006GV-OB for pgsql-general@arkaria.postgresql.org; Thu, 25 Jul 2019 19:47:39 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hqjiE-0003DX-Kz for pgsql-general@arkaria.postgresql.org; Thu, 25 Jul 2019 19:47:38 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hqjiE-0003DA-7V for pgsql-general@lists.postgresql.org; Thu, 25 Jul 2019 19:47:38 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.92) (envelope-from ) id 1hqjiB-0007Jd-Mj for pgsql-general@lists.postgresql.org; Thu, 25 Jul 2019 19:47:37 +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 x6PJlQGl031986; Thu, 25 Jul 2019 15:47:26 -0400 From: Tom Lane To: Adrian Klaver cc: Kevin Brannen , "pgsql-generallists.postgresql.org" Subject: Re: SELECT INTO question In-reply-to: References: Comments: In-reply-to Adrian Klaver message dated "Thu, 25 Jul 2019 12:32:07 -0700" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <31984.1564084046.1@sss.pgh.pa.us> Date: Thu, 25 Jul 2019 15:47:26 -0400 Message-ID: <31985.1564084046@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Adrian Klaver writes: > On 7/25/19 12:23 PM, Kevin Brannen wrote: >> We're trying to understand what happened with a SELECT INTO. The problem >> can be see with this example: >> >> nms=# select into t2 from t1; >> SELECT 5 >> # select * from t2; >> -- >> (5 rows) > The list of output expressions after SELECT can be empty, producing a > zero-column result table. This is not valid syntax according to the SQL > standard. PostgreSQL allows it to be consistent with allowing > zero-column tables. However, an empty list is not allowed when DISTINCT > is used. Right, you selected no columns from t1, so t2 has no columns (and yet five rows). Worth noting here is that psql is a bit squirrely about displaying zero-column results --- it drops the column-names header line, and it doesn't emit a blank-line-per-row as one might expect. Perhaps somebody ought to fix that, but it's such a corner case that no one has bothered yet. regards, tom lane