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 1kaONd-0006gM-Bm for pgsql-performance@arkaria.postgresql.org; Wed, 04 Nov 2020 19:23:37 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kaONc-0005t5-3O for pgsql-performance@arkaria.postgresql.org; Wed, 04 Nov 2020 19:23:36 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kaONb-0005sy-Rx for pgsql-performance@lists.postgresql.org; Wed, 04 Nov 2020 19:23:35 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kaONV-0002BF-K1 for pgsql-performance@lists.postgresql.org; Wed, 04 Nov 2020 19:23:34 +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 0A4JNSN41237917; Wed, 4 Nov 2020 14:23:28 -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> <1237035.1604516511@sss.pgh.pa.us> Comments: In-reply-to Eric Raskin message dated "Wed, 04 Nov 2020 14:12:07 -0500" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <1237915.1604517808.1@sss.pgh.pa.us> Date: Wed, 04 Nov 2020 14:23:28 -0500 Message-ID: <1237916.1604517808@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Eric Raskin writes: > And, of course, your explanation that inserts will not be parallelized must > be the reason. I will certainly re-vacuum the tables. I wonder why > auto-vacuum didn't collect better stats. vacuum analyze is all I > need, right? Plain ANALYZE is enough to collect stats; but I doubt that'll improve matters for you. The problem is basically that the planner can't do anything with a CASE construct, so you end up with default selectivity estimates for anything involving a CASE, statistics or no statistics. You need to try to reformulate the query with simpler join conditions. > As a last resort, what about a PL/PGSQL procedure loop on the query > result? Since the insert is very few rows relative to the work the select > has to do, I could just turn the insert.. select.. into a for loop. Then > the select could be parallel? Maybe, but you're still skating on a cliff edge. I think it's pure chance that the parallelized query is working acceptably well; next month with slightly different conditions, it might not. regards, tom lane