Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eSDAq-0004wi-PJ for pgsql-general@arkaria.postgresql.org; Fri, 22 Dec 2017 02:35:00 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eSDAp-0008Ik-Vf for pgsql-general@arkaria.postgresql.org; Fri, 22 Dec 2017 02:34:59 +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.84_2) (envelope-from ) id 1eSDAp-0008Ia-O3 for pgsql-general@lists.postgresql.org; Fri, 22 Dec 2017 02:34:59 +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 1eSDAi-0007Ye-19 for pgsql-general@postgresql.org; Fri, 22 Dec 2017 02:34:59 +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 vBM2Ymw4008056; Thu, 21 Dec 2017 21:34:49 -0500 From: Tom Lane To: Victor Yegorov cc: pgsql-general@postgresql.org Subject: Re: Intersection or zero-column queries In-reply-to: References: Comments: In-reply-to Victor Yegorov message dated "Fri, 22 Dec 2017 01:53:35 +0200" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <8054.1513910088.1@sss.pgh.pa.us> Date: Thu, 21 Dec 2017 21:34:48 -0500 Message-ID: <8055.1513910088@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Victor Yegorov writes: > However, if I'll do `EXCPET` or `INTERSECT` of such queries, I'll get 2 > rows: > postgres=# select except select; > -- > (2 rows) > postgres=# select intersect all select; > -- > (2 rows) > Why is it so? The UNION case seems wrong as well: regression=# select union select; -- (2 rows) The reason is that the planner hasn't spent any time thinking about this case: /* Identify the grouping semantics */ groupList = generate_setop_grouplist(op, tlist); /* punt if nothing to group on (can this happen?) */ if (groupList == NIL) return path; so what you actually get for any of these queries is a plan that just appends the inputs and forgets to do any de-duplication: regression=# explain select except select; QUERY PLAN ----------------------------------------------------------------------- Append (cost=0.00..0.04 rows=2 width=4) -> Subquery Scan on "*SELECT* 1" (cost=0.00..0.02 rows=1 width=4) -> Result (cost=0.00..0.01 rows=1 width=0) -> Subquery Scan on "*SELECT* 2" (cost=0.00..0.02 rows=1 width=4) -> Result (cost=0.00..0.01 rows=1 width=0) (5 rows) which would only be the right plan for UNION ALL. So yeah, it's wrong ... but personally I'm not terribly excited about fixing it. Maybe somebody else wants to; but what's the practical use? regards, tom lane