Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1c1z2G-0003pu-0m for pgsql-bugs@arkaria.postgresql.org; Wed, 02 Nov 2016 17:09:12 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1c1z2F-0000Z1-FZ for pgsql-bugs@arkaria.postgresql.org; Wed, 02 Nov 2016 17:09:11 +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.84_2) (envelope-from ) id 1c1z2E-0000YQ-Ea for pgsql-bugs@postgresql.org; Wed, 02 Nov 2016 17:09:10 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1c1z2B-0000PV-SH for pgsql-bugs@postgresql.org; Wed, 02 Nov 2016 17:09:09 +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 uA2H8vuh027651; Wed, 2 Nov 2016 13:08:57 -0400 From: Tom Lane To: Michael Paquier cc: Pavel Stehule , =?UTF-8?B?UGF2ZWwgSGFuw6Fr?= , "pgsql-bugs@postgresql.org" Subject: Re: Problems with "pg.dropped" column after upgrade 9.5 to 9.6 In-reply-to: <12846.1478101936@sss.pgh.pa.us> References: <83shraampf.fsf@is-it.eu> <12846.1478101936@sss.pgh.pa.us> Comments: In-reply-to Tom Lane message dated "Wed, 02 Nov 2016 11:52:16 -0400" MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" Content-ID: <27583.1478106420.0@sss.pgh.pa.us> Date: Wed, 02 Nov 2016 13:08:57 -0400 Message-ID: <27650.1478106537@sss.pgh.pa.us> X-Pg-Spam-Score: -4.2 (----) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org ------- =_aaaaaaaaaa0 Content-Type: text/plain; charset="us-ascii" Content-ID: <27583.1478106420.1@sss.pgh.pa.us> I wrote: > So I'm pretty tempted to fix it as per attached, that is, teach setrefs.c > that it's silly to try to convert a Const into a Var. It is sane to > replace more-complicated expressions with Vars, because that means we save > re-evaluating those expressions. But a quick look at ExecEvalConst and > ExecEvalVar shows that a Const is strictly cheaper to execute than a Var, > so doing such a conversion is actually a pessimization. After further poking around, I concluded that it'd be a good idea to make a similar change in set_dummy_tlist_references(), to prevent a failure if the top plan node below ModifyTable chances to be a Sort or other non-projecting plan node. I'm not sure such a case can arise today, but I'm not sure it can't either. With that, there are a couple more changes in regression test EXPLAIN output. A full patch that passes "make check-world" is attached. I'm not sure if it's worth memorializing the specific test case discussed in this thread as a regression test. It depends enough on the behavior of SQL function planning that I wouldn't have much confidence in it continuing to test what we meant it to test going forward. regards, tom lane ------- =_aaaaaaaaaa0 Content-Type: text/x-diff; name="dont-make-Consts-into-Vars-2.patch"; charset="us-ascii" Content-ID: <27583.1478106420.2@sss.pgh.pa.us> Content-Description: dont-make-Consts-into-Vars-2.patch Content-Transfer-Encoding: quoted-printable diff --git a/contrib/postgres_fdw/expected/postgres_fdw.out b/contrib/postg= res_fdw/expected/postgres_fdw.out index 2745ad5..7339b58 100644 *** a/contrib/postgres_fdw/expected/postgres_fdw.out --- b/contrib/postgres_fdw/expected/postgres_fdw.out *************** select count(c2) w, c2 x, 5 y, 7.0 z fro *** 2447,2456 **** QUERY PLAN=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20 -------------------------------------------------------------------------= --------------------------------- Sort ! Output: (count(c2)), c2, (5), (7.0), (9) Sort Key: ft1.c2 -> Foreign Scan ! Output: (count(c2)), c2, (5), (7.0), (9) Relations: Aggregate on (public.ft1) Remote SQL: SELECT count(c2), c2, 5, 7.0, 9 FROM "S 1"."T 1" GRO= UP BY c2, 5::integer, 9::integer (7 rows) --- 2447,2456 ---- QUERY PLAN=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20 -------------------------------------------------------------------------= --------------------------------- Sort ! Output: (count(c2)), c2, 5, 7.0, 9 Sort Key: ft1.c2 -> Foreign Scan ! Output: (count(c2)), c2, 5, 7.0, 9 Relations: Aggregate on (public.ft1) Remote SQL: SELECT count(c2), c2, 5, 7.0, 9 FROM "S 1"."T 1" GRO= UP BY c2, 5::integer, 9::integer (7 rows) *************** select count(*) from (select c5, count(c *** 2499,2505 **** Aggregate Output: count(*) -> Foreign Scan ! Output: ft1.c5, (NULL::bigint), (sqrt((ft1.c2)::double precision= )) Filter: (((((avg(ft1.c1)) / (avg(ft1.c1))))::double precision * = random()) <=3D '1'::double precision) Relations: Aggregate on (public.ft1) Remote SQL: SELECT c5, NULL::bigint, sqrt(c2), avg("C 1") FROM "= S 1"."T 1" GROUP BY c5, (sqrt(c2)) HAVING ((avg("C 1") < 500::numeric)) --- 2499,2505 ---- Aggregate Output: count(*) -> Foreign Scan ! Output: ft1.c5, NULL::bigint, (sqrt((ft1.c2)::double precision)) Filter: (((((avg(ft1.c1)) / (avg(ft1.c1))))::double precision * = random()) <=3D '1'::double precision) Relations: Aggregate on (public.ft1) Remote SQL: SELECT c5, NULL::bigint, sqrt(c2), avg("C 1") FROM "= S 1"."T 1" GROUP BY c5, (sqrt(c2)) HAVING ((avg("C 1") < 500::numeric)) *************** select sum(q.a), count(q.b) from ft4 lef *** 3139,3145 **** -> Subquery Scan on q Output: q.a, q.b -> Foreign Scan ! Output: (13), (avg(ft1.c1)), (NULL::bigint) Relations: Aggregate on ((public.ft2) LEFT JOI= N (public.ft1)) Remote SQL: SELECT 13, avg(r1."C 1"), NULL::bi= gint FROM ("S 1"."T 1" r2 LEFT JOIN "S 1"."T 1" r1 ON (((r1."C 1" =3D r2."C= 1")))) (16 rows) --- 3139,3145 ---- -> Subquery Scan on q Output: q.a, q.b -> Foreign Scan ! Output: 13, (avg(ft1.c1)), NULL::bigint Relations: Aggregate on ((public.ft2) LEFT JOI= N (public.ft1)) Remote SQL: SELECT 13, avg(r1."C 1"), NULL::bi= gint FROM ("S 1"."T 1" r2 LEFT JOIN "S 1"."T 1" r1 ON (((r1."C 1" =3D r2."C= 1")))) (16 rows) diff --git a/src/backend/optimizer/plan/setrefs.c b/src/backend/optimizer/p= lan/setrefs.c index d10a983..d91bc3b 100644 *** a/src/backend/optimizer/plan/setrefs.c --- b/src/backend/optimizer/plan/setrefs.c *************** set_dummy_tlist_references(Plan *plan, i *** 1823,1828 **** --- 1823,1841 ---- Var *oldvar =3D (Var *) tle->expr; Var *newvar; =20=20 + /* + * As in search_indexed_tlist_for_non_var(), we prefer to keep Consts + * as Consts, not Vars referencing Consts. Here, there's no speed + * advantage to be had, but it makes EXPLAIN output look cleaner, and + * again it avoids confusing the executor. + */ + if (IsA(oldvar, Const)) + { + /* just reuse the existing TLE node */ + output_targetlist =3D lappend(output_targetlist, tle); + continue; + } +=20 newvar =3D makeVar(OUTER_VAR, tle->resno, exprType((Node *) oldvar), *************** search_indexed_tlist_for_non_var(Node *n *** 2010,2015 **** --- 2023,2038 ---- { TargetEntry *tle; =20=20 + /* + * If it's a simple Const, replacing it with a Var is silly, even if the= re + * happens to be an identical Const below; a Var is more expensive to + * execute than a Const. What's more, replacing it could confuse some + * places in the executor that expect to see simple Consts for, eg, + * dropped columns. + */ + if (IsA(node, Const)) + return NULL; +=20 tle =3D tlist_member(node, itlist->tlist); if (tle) { diff --git a/src/test/regress/expected/inherit.out b/src/test/regress/expec= ted/inherit.out index df7cba6..79e9969 100644 *** a/src/test/regress/expected/inherit.out --- b/src/test/regress/expected/inherit.out *************** ORDER BY thousand, tenthous; *** 1426,1432 **** Sort Key: tenk1.thousand, tenk1.tenthous -> Index Only Scan using tenk1_thous_tenthous on tenk1 -> Sort ! Sort Key: (42), (42) -> Index Only Scan using tenk1_hundred on tenk1 tenk1_1 (6 rows) =20=20 --- 1426,1432 ---- Sort Key: tenk1.thousand, tenk1.tenthous -> Index Only Scan using tenk1_thous_tenthous on tenk1 -> Sort ! Sort Key: 42, 42 -> Index Only Scan using tenk1_hundred on tenk1 tenk1_1 (6 rows) =20=20 ------- =_aaaaaaaaaa0 Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs ------- =_aaaaaaaaaa0--