Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1ZMwn9-0003P5-3Q for pgsql-bugs@arkaria.postgresql.org; Wed, 05 Aug 2015 11:23:27 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84) (envelope-from ) id 1ZMwn8-000289-L6 for pgsql-bugs@arkaria.postgresql.org; Wed, 05 Aug 2015 11:23:26 +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.84) (envelope-from ) id 1ZMwn6-00026p-RO for pgsql-bugs@postgresql.org; Wed, 05 Aug 2015 11:23:24 +0000 Received: from mail-la0-x236.google.com ([2a00:1450:4010:c03::236]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84) (envelope-from ) id 1ZMwn4-00033W-F1 for pgsql-bugs@postgresql.org; Wed, 05 Aug 2015 11:23:24 +0000 Received: by labgo9 with SMTP id go9so26334782lab.3 for ; Wed, 05 Aug 2015 04:23:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=date:from:to:subject:message-id:references:mime-version :content-type:content-disposition:in-reply-to; bh=BmF+BfPizydTgc+Fq4XNZbE/ytndgR5MjU29e2IHAoI=; b=OoNw5G8UaOkGIswlAxoV3oXU1Os9HEAqdR1/sFWvdJ44MNXbKf1jHdM7xn9V8pe+vP JKxzaKuZlg2Mi3JA7v1S0RODe0Dn4XKRZSSz3L2cK4FDcgJNrJMjx/wEJ0eDtqTV/xmi tYLI3RXSKJPdmJ3xb8RanRan3r+EoWsbA/V/iX1Ft8eY0yoJ2wSezGZN/Tu2T25QLavl 6ayp7dO8Pz92kaqkSkLlfHX4YRh9rvLIKPqArRBfAcck3BrRx4ZLZSQauQFsUD5FMPUX lN05OCAHID/VS1awdMGC/OBVR1lBJoCaisblGbQLaQCVrsyOSmSWw5KyGIrlh0BQjBOC GmnA== X-Received: by 10.112.144.199 with SMTP id so7mr8847951lbb.109.1438773801326; Wed, 05 Aug 2015 04:23:21 -0700 (PDT) Received: from gmail.com (138.246.50.84.sta.estpak.ee. [84.50.246.138]) by smtp.googlemail.com with ESMTPSA id lv12sm485367lac.36.2015.08.05.04.23.20 for (version=TLSv1.2 cipher=RC4-SHA bits=128/128); Wed, 05 Aug 2015 04:23:20 -0700 (PDT) Date: Wed, 5 Aug 2015 14:23:19 +0300 From: Marko Kreen To: Postgres Bugs List Subject: Re: [9.4] SELECT repeatedly rewrites a table Message-ID: <20150805112319.GA14023@gmail.com> References: <20150717112259.GA21878@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20150717112259.GA21878@gmail.com> X-Pg-Spam-Score: -1.3 (-) 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 On Fri, Jul 17, 2015 at 02:22:59PM +0300, Marko Kreen wrote: > We recently upgraded 9.3 to 9.4 in live environment and one batch-job > query floods system with write requests. Query itself is basically > seq-scan on insert-only table. I managed to get EXPLAIN of bad run. Seems bug is dependant on "DECLARE .. NO SCROLL CURSOR WITH HOLD FOR ...". 1) On evening I launced 2 VM's, let's call then VMSELECT and VMCURSOR. 2) In the morning I run "EXPLAIN SELECT .." on VMSELECT twice - one after other. And on VMCURSOR "EXPLAIN DECLATE CURSOR .." twice - one after other. VMCURSOR run #1 - the bad run ----------------------------- chatdb=# explain (analyze true, buffers true, timing true) chatdb-# DECLARE data_maint_cur NO SCROLL CURSOR WITH HOLD FOR chatdb-# select account_id, max(event_nr) as first_event_nr from account.event where store_time < now() - '1 month'::interval group by 1; QUERY PLAN ------------------------------------------------------------------------------------------------------------------------------------------------ GroupAggregate (cost=0.56..8965079.90 rows=82 width=24) (actual time=37.752..9941575.182 rows=301857 loops=1) Group Key: account_id Buffers: shared hit=1417642 read=16900923 dirtied=15670349 written=11141824 I/O Timings: read=1826689.835 write=8017738.089 -> Index Scan using event_pkey on event (cost=0.56..8960823.53 rows=851110 width=24) (actual time=30.470..9940359.172 rows=859710 loops=1) Filter: (store_time < (now() - '1 mon'::interval)) Rows Removed by Filter: 28963464 Buffers: shared hit=1417642 read=16900923 dirtied=15670349 written=11141824 I/O Timings: read=1826689.835 write=8017738.089 Planning time: 1.453 ms Execution time: 9941803.330 ms (11 rows) VMCURSOR run #2 - no so bad --------------------------- chatdb=# explain (analyze true, buffers true, timing true) DECLARE data_maint_cur NO SCROLL CURSOR WITH HOLD FOR chatdb-# select account_id, max(event_nr) as first_event_nr from account.event where store_time < now() - '1 month'::interval group by 1; QUERY PLAN ---------------------------------------------------------------------------------------------------------------------------------------------- GroupAggregate (cost=0.56..9291947.71 rows=89 width=24) (actual time=0.055..126290.079 rows=304188 loops=1) Group Key: account_id Buffers: shared hit=1803475 read=16580860 dirtied=13723 written=10176 I/O Timings: read=82540.306 write=1187.116 -> Index Scan using event_pkey on event (cost=0.56..9287343.81 rows=920602 width=24) (actual time=0.040..125176.023 rows=957393 loops=1) Filter: (store_time < (now() - '1 mon'::interval)) Rows Removed by Filter: 28999419 Buffers: shared hit=1803475 read=16580860 dirtied=13723 written=10176 I/O Timings: read=82540.306 write=1187.116 Planning time: 0.071 ms Execution time: 126510.051 ms (11 rows) VMSELECT run #1 --------------- For some reason it rewrites the table, once. chatdb=# explain (analyze true, buffers true, timing true) chatdb-# select account_id, max(event_nr) as first_event_nr from account.event chatdb-# where store_time < now() - '1 month'::interval group by 1; QUERY PLAN ---------------------------------------------------------------------------------------------------------------------------- HashAggregate (cost=974789.39..974790.27 rows=88 width=24) (actual time=276382.073..276652.235 rows=301857 loops=1) Group Key: account_id Buffers: shared hit=358 read=448265 dirtied=418624 written=418225 I/O Timings: read=246250.610 write=15802.735 -> Seq Scan on event (cost=0.00..970328.57 rows=892164 width=24) (actual time=627.299..275438.301 rows=855133 loops=1) Filter: (store_time < (now() - '1 mon'::interval)) Rows Removed by Filter: 28955031 Buffers: shared hit=358 read=448265 dirtied=418624 written=418225 I/O Timings: read=246250.610 write=15802.735 Planning time: 1.301 ms Execution time: 276869.555 ms (11 rows) VMSELECT run #2 --------------- chatdb=# explain (analyze true, buffers true, timing true) select account_id, max(event_nr) as first_event_nr from account.event where store_time < now() - '1 month'::interval group by 1; QUERY PLAN --------------------------------------------------------------------------------------------------------------------------- HashAggregate (cost=975156.40..975157.28 rows=88 width=24) (actual time=26036.503..26311.292 rows=301857 loops=1) Group Key: account_id Buffers: shared hit=553 read=448232 dirtied=117 I/O Timings: read=14424.903 -> Seq Scan on event (cost=0.00..970678.96 rows=895488 width=24) (actual time=833.044..25130.745 rows=858416 loops=1) Filter: (store_time < (now() - '1 mon'::interval)) Rows Removed by Filter: 28962467 Buffers: shared hit=553 read=448232 dirtied=117 I/O Timings: read=14424.903 Planning time: 0.102 ms Execution time: 26518.603 ms VMSELECT run #3 - now with cursor here too - looks ok ----------------------------------------------------- chatdb=# explain (analyze true, buffers true, timing true) DECLARE data_maint_cur NO SCROLL CURSOR WITH HOLD FOR chatdb-# select account_id, max(event_nr) as first_event_nr from account.event where store_time < now() - '1 month'::interval group by 1; QUERY PLAN ---------------------------------------------------------------------------------------------------------------------------------------------- GroupAggregate (cost=0.56..9230874.03 rows=94 width=24) (actual time=0.220..444906.246 rows=304184 loops=1) Group Key: account_id Buffers: shared hit=1790170 read=16588868 dirtied=78190 written=32718 I/O Timings: read=398411.728 write=2608.564 -> Index Scan using event_pkey on event (cost=0.56..9226058.00 rows=963019 width=24) (actual time=0.183..443823.884 rows=946397 loops=1) Filter: (store_time < (now() - '1 mon'::interval)) Rows Removed by Filter: 29000509 Buffers: shared hit=1790170 read=16588868 dirtied=78190 written=32718 I/O Timings: read=398411.728 write=2608.564 Planning time: 12.062 ms Execution time: 445120.327 ms (11 rows) Does this ring any bells? What more info is needed? -- marko -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs