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 1jvicX-0007XX-EH for pgsql-general@arkaria.postgresql.org; Wed, 15 Jul 2020 14:42:53 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jvicV-0003f2-NB for pgsql-general@arkaria.postgresql.org; Wed, 15 Jul 2020 14:42:51 +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 1jvicV-0003eu-Ey for pgsql-general@lists.postgresql.org; Wed, 15 Jul 2020 14:42:51 +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 1jvicT-0006BT-G1 for pgsql-general@postgresql.org; Wed, 15 Jul 2020 14:42:51 +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 06FEggOi3065071; Wed, 15 Jul 2020 10:42:42 -0400 From: Tom Lane To: =?UTF-8?Q?Eudald_Valc=C3=A0rcel_Lacasa?= cc: pgsql-general@postgresql.org Subject: Re: Issue executing query from container In-reply-to: References: <2961873.1594763498@sss.pgh.pa.us> Comments: In-reply-to =?UTF-8?Q?Eudald_Valc=C3=A0rcel_Lacasa?= message dated "Wed, 15 Jul 2020 10:27:52 +0200" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <3065069.1594824162.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Wed, 15 Jul 2020 10:42:42 -0400 Message-ID: <3065070.1594824162@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk =3D?UTF-8?Q?Eudald_Valc=3DC3=3DA0rcel_Lacasa?=3D writes: > After running the query both manually and with the script, I've the > following logs: > MANUALLY: > Update on import_temp_2 tmp (cost=3D116.73..17352.10 rows=3D5557 wi= dth=3D293) > -> Hash Join (cost=3D116.73..17352.10 rows=3D5557 width=3D293) > Hash Cond: (lower((tmp.email)::text) =3D lower((bl.value)::t= ext)) > -> Seq Scan on import_temp_2 tmp (cost=3D0.00..14864.20 > rows=3D370496 width=3D193) > Filter: (status =3D 1) > -> Hash (cost=3D116.70..116.70 rows=3D3 width=3D130) > Buckets: 32768 (originally 1024) Batches: 2 > (originally 1) Memory Usage: 3841kB > -> Foreign Scan on blacklist_central bl > (cost=3D100.00..116.70 rows=3D3 width=3D130) > AUTOMATED: > Update on import_temp_2 tmp (cost=3D100.00..13295.86 rows=3D15 widt= h=3D500) > -> Nested Loop (cost=3D100.00..13295.86 rows=3D15 width=3D500) > Join Filter: (lower((tmp.email)::text) =3D lower((bl.value):= :text)) > -> Seq Scan on import_temp_2 tmp (cost=3D0.00..13118.74 > rows=3D1007 width=3D400) > Filter: (status =3D 1) > -> Materialize (cost=3D100.00..116.71 rows=3D3 width=3D130= ) > -> Foreign Scan on blacklist_central bl > (cost=3D100.00..116.70 rows=3D3 width=3D130) So the question is why you are getting an estimate of 370496 import_temp_2 rows with status =3D 1 in the first case, and only 1007 rows in the second= . I suspect that the true number of rows is quite large, causing the nested-loop plan to run slowly. (Is the row estimate of 3 for the foreign scan anywhere near reality, either?) You may need to insert a manual ANALYZE in your automated process to ensure that import_temp_2 has up-to-date stats before you try to do this step. It seems somewhat likely that autovacuum takes care of that for you in the "manual" case, but its reaction time is too slow to fill the gap for the automated process. regards, tom lane