Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kaO2h-00062l-Ft for pgsql-performance@arkaria.postgresql.org; Wed, 04 Nov 2020 19:01:59 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kaO2e-0000Yd-OW for pgsql-performance@arkaria.postgresql.org; Wed, 04 Nov 2020 19:01:56 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kaO2e-0000YV-H1 for pgsql-performance@lists.postgresql.org; Wed, 04 Nov 2020 19:01:56 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kaO2b-0005c5-LA for pgsql-performance@lists.postgresql.org; Wed, 04 Nov 2020 19:01:56 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 0A4J1paI1237036; Wed, 4 Nov 2020 14:01:51 -0500 From: Tom Lane To: Eric Raskin cc: pgsql-performance@lists.postgresql.org Subject: Re: Adding nextval() to a select caused hang/very slow execution In-reply-to: References: <1234726.1604513058@sss.pgh.pa.us> Comments: In-reply-to Eric Raskin message dated "Wed, 04 Nov 2020 13:25:42 -0500" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <1237034.1604516511.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Wed, 04 Nov 2020 14:01:51 -0500 Message-ID: <1237035.1604516511@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Eric Raskin writes: > And, to follow up on your question, the plan shape DOES change when I > add/remove the nextval() on a plain explain. > Without nextval(): https://explain.depesz.com/s/SCdY > With nextval(): https://explain.depesz.com/s/oLPn Ah, there's your problem, I think: the plan without nextval() is parallelized while the plan with nextval() is not, because nextval() is marked as parallel-unsafe. It's not immediately clear why that would result in more than about a 4X speed difference, given that the parallel plan is using 4 workers. But some of the rowcount estimates seem fairly far off, so I'm betting that the planner is just accidentally lighting on a decent plan when it's using parallelism while making some poor choices when it isn't. The reason for the original form of your problem is likely that we don't use parallelism at all in non-SELECT queries, so you ended up with a bad plan even though the nextval() was hidden in a trigger. What you need to do is get the rowcount estimates nearer to reality --- those places where you've got estimated rowcount 1 while reality is tens or hundreds of thousands of rows are just disasters waiting to bite. I suspect most of the problem is join conditions like Join Filter: (CASE WHEN (c.rtype =3D ANY ('{0,1,7,9}'::bpchar[])) THEN c.r= type ELSE x.rtype END =3D '2'::bpchar) The planner just isn't going to have any credible idea how selective that is. I wonder to what extent you could fix this by storing generated columns that represent the derived conditions you want to filter on. regards, tom lane