Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fR2ZT-0007C4-NW for pgsql-general@arkaria.postgresql.org; Thu, 07 Jun 2018 21:35:51 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fR2ZS-00027V-Au for pgsql-general@arkaria.postgresql.org; Thu, 07 Jun 2018 21:35:50 +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.89) (envelope-from ) id 1fR2ZS-00027L-2h for pgsql-general@lists.postgresql.org; Thu, 07 Jun 2018 21:35:50 +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 1fR2ZP-0002ws-0m for pgsql-general@postgresql.org; Thu, 07 Jun 2018 21:35:49 +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 w57LYtTG006300; Thu, 7 Jun 2018 17:34:55 -0400 From: Tom Lane To: Robert Creager cc: pgsql-general@postgresql.org, Robert Creager Subject: Re: Query hitting empty tables taking 48 minutes In-reply-to: <186A2F70-5953-4FE4-9586-BF3F114AF1CC@logicalchaos.org> References: <186A2F70-5953-4FE4-9586-BF3F114AF1CC@logicalchaos.org> Comments: In-reply-to Robert Creager message dated "Thu, 07 Jun 2018 12:17:03 -0600" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <6298.1528407295.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Thu, 07 Jun 2018 17:34:55 -0400 Message-ID: <6299.1528407295@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Robert Creager writes: > Jun 7 17:24:21 blackpearl postgres[10670]: [7737-1] db=3Dtapesystem,use= r=3DAdministrator,app=3D[unknown],client=3D127.0.0.1 LOG: duration: 29036= 12.206 ms execute fetch from S_2037436/C_2037437: SELECT * FROM ds3.blob = WHERE EXISTS (SELECT * FROM ds3.job_entry WHERE blob_id =3D ds3.blob.id AN= D (job_id =3D $1)) > tapesystem=3D# explain analyze SELECT * FROM ds3.blob WHERE EXISTS (SELE= CT * FROM ds3.job_entry WHERE blob_id =3D ds3.blob.id AND (job_id =3D 'b51= 357cd-e07a-4c87-a50b-999c347a5c71')); = > QUERY PLAN > ------------------------------------------------------------------------= --------------------------------------------------------------- = = > Nested Loop (cost=3D0.84..6.89 rows=3D1 width=3D77) (actual time=3D0.0= 44..0.044 rows=3D0 loops=3D1) > -> Index Scan using job_entry_job_id_idx on job_entry (cost=3D0.42.= .2.44 rows=3D1 width=3D16) (actual time=3D0.042..0.042 rows=3D0 loops=3D1) > Index Cond: (job_id =3D 'b51357cd-e07a-4c87-a50b-999c347a5c71':= :uuid) > -> Index Scan using blob_pkey on blob (cost=3D0.42..4.44 rows=3D1 w= idth=3D77) (never executed) > Index Cond: (id =3D job_entry.blob_id) = = > Planning time: 0.388 ms > Execution time: 0.118 ms = = = That's fairly bizarre, but important to notice here is that you don't have an apples-to-apples comparison. The query in the log is evidently parameterized, whereas your EXPLAIN isn't; it's got a hard-wired constant to compare to job_id. I'd suggest trying this in psql: PREPARE foo(uuid) AS SELECT * FROM ds3.blob WHERE EXISTS (SELECT * FROM ds= 3.job_entry WHERE blob_id =3D ds3.blob.id AND (job_id =3D $1)); EXPLAIN ANALYZE EXECUTE foo('b51357cd-e07a-4c87-a50b-999c347a5c71'); Assuming you get a sane plan the first time, repeat the EXPLAIN ANALYZE half a dozen times, and note whether the plan changes after six executions. (The point here is to see if the plancache will shift to a "generic" plan, and if so whether that's worse than a "custom" plan for the specific parameter value.) If the job_id column has fairly uniform statistics, this exercise probably won't turn up anything surprising ... but if it doesn't, we might find that the issue comes from a stupidly chosen generic plan. regards, tom lane