Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1UVYTn-0006kR-5h for pgsql-performance@arkaria.postgresql.org; Fri, 26 Apr 2013 02:33:43 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1UVYTm-00015Y-0t for pgsql-performance@arkaria.postgresql.org; Fri, 26 Apr 2013 02:33:42 +0000 Received: from makus.postgresql.org ([2001:4800:7903:4::125]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1UVYTk-00015S-Kn for pgsql-performance@postgresql.org; Fri, 26 Apr 2013 02:33:40 +0000 Received: from [2404:130:0:10::40:2] (helo=mail.catalyst.net.nz) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1UVYTh-0004NN-0o for pgsql-performance@postgresql.org; Fri, 26 Apr 2013 02:33:40 +0000 Received: from localhost (localhost [127.0.0.1]) by mail.catalyst.net.nz (Postfix) with ESMTP id 7586A32BF8 for ; Fri, 26 Apr 2013 14:33:32 +1200 (NZST) X-Virus-Scanned: Debian amavisd-new at catalyst.net.nz Received: from mail.catalyst.net.nz ([127.0.0.1]) by localhost (bertrand.catalyst.net.nz [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id niZ3T+xg1wSj for ; Fri, 26 Apr 2013 14:33:31 +1200 (NZST) Received: from [IPv6:2404:130:0:1000:222:4dff:fe88:a586] (unknown [IPv6:2404:130:0:1000:222:4dff:fe88:a586]) (Authenticated sender: mark.kirkwood) by mail.catalyst.net.nz (Postfix) with ESMTPSA id 5A3D53232B for ; Fri, 26 Apr 2013 14:33:31 +1200 (NZST) Message-ID: <5179E77B.7050601@catalyst.net.nz> Date: Fri, 26 Apr 2013 14:33:31 +1200 From: Mark Kirkwood User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130404 Thunderbird/17.0.5 MIME-Version: 1.0 To: "pgsql-performance@postgresql.org" Subject: In progress INSERT wrecks plans on table Content-Type: multipart/mixed; boundary="------------080001020208050405080401" X-Host-Lookup-Failed: Reverse DNS lookup failed for 2404:130:0:10::40:2 (failed) X-Pg-Spam-Score: -1.1 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-performance Precedence: bulk Sender: pgsql-performance-owner@postgresql.org This is a multi-part message in MIME format. --------------080001020208050405080401 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Recently we encountered the following unhappy sequence of events: 1/ system running happily 2/ batch load into table begins 3/ very quickly (some) preexisting queries on said table go orders of magnitude slower 4/ database instance becomes unresponsive 5/ application outage After looking down a few false leads, We've isolated the cause to the following: The accumulating in-progress row changes are such that previously optimal plans are optimal no longer. Now this situation will fix itself when the next autoanalyze happens (and new plan will be chosen) - however that cannot occur until the batch load is completed and committed (approx 70 seconds). However during that time there is enough of a performance degradation for queries still using the old plan to cripple the server. Now that we know what is happening we can work around it. But I'm wondering - is there any way (or if not should there be one) to let postgres handle this automatically? I experimented with a quick hack to src/backend/commands/analyze.c (attached) that lets another session's ANALYZE see in progress rows - which works but a) may cause other problems and b) does not help autoaanalyze which has to wait for COMMIT + stats message. I've attached a (synthetic) test case that shows the issue, I'll reproduce the output below to hopefully make the point obvious: Table "public.plan" Column | Type | Modifiers --------+-----------------------------+----------- id | integer | not null typ | integer | not null dat | timestamp without time zone | val | text | not null Indexes: "plan_id" UNIQUE, btree (id) "plan_dat" btree (dat) "plan_typ" btree (typ) [Session 1] EXPLAIN ANALYZE SELECT * FROM plan WHERE typ = 3 AND dat IS NOT NULL; QUERY PLAN --------------------------------------------------------------------------------------------------------------------- Index Scan using plan_dat on plan (cost=0.00..265.47 rows=55 width=117) (actual time=0.130..4.409 rows=75 loops=1) Index Cond: (dat IS NOT NULL) Filter: (typ = 3) Rows Removed by Filter: 5960 Total runtime: 4.440 ms (5 rows) [Session 2] BEGIN; INSERT INTO plan SELECT id + 2000001,typ,current_date + id * '1 seconds'::interval ,val FROM plan ; [Session 1] EXPLAIN ANALYZE SELECT * FROM plan WHERE typ = 3 AND dat IS NOT NULL; QUERY PLAN ----------------------------------------------------------------------------------------------------------------------- Index Scan using plan_dat on plan (cost=0.00..551.35 rows=91 width=117) (actual time=0.131..202.699 rows=75 loops=1) Index Cond: (dat IS NOT NULL) Filter: (typ = 3) Rows Removed by Filter: 5960 Total runtime: 202.729 ms (5 rows) [Session 2] COMMIT; [Session 1...wait for autoanalyze to finish then] EXPLAIN ANALYZE SELECT * FROM plan WHERE typ = 3 AND dat IS NOT NULL; QUERY PLAN --------------------------------------------------------------------------------------------------------------------------- Bitmap Heap Scan on plan (cost=407.87..44991.95 rows=10116 width=117) (actual time=2.692..6.582 rows=75 loops=1) Recheck Cond: (typ = 3) Filter: (dat IS NOT NULL) Rows Removed by Filter: 19925 -> Bitmap Index Scan on plan_typ (cost=0.00..405.34 rows=20346 width=0) (actual time=2.573..2.573 rows=20000 loops=1) Index Cond: (typ = 3) Total runtime: 6.615 ms Regards Mark --------------080001020208050405080401 Content-Type: application/gzip; name="plan.tar.gz" Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename="plan.tar.gz" H4sIAITieVEAA+1cTXPbxhnedOq22iZumt7b17Q6oiwaAkACDKUoE1mGY85I lCVRiZwm44GIlYQJCDAAKFmpnU5P7UzaH9BpZzrTQy+95A+000tmeuyxnR5a 33ppT/049NJ3F4BIUSIpeURKnuxjPwa4u1jsx/O+uwtj0fJsf5aMFiqibBj8 qJUNrfuYgWi6VjSLpqbjuappmqkTMEZcLoF2FNshAGkFUbwbsqhfOmfbHkdx xo0W7/+osceathJ95I3kHryDzVKpb/9rRrGn/4taCftfHUlpevAF7/+ldWux bkF98c6yBVwMNE8BXAeqtbr1trUOtdU61DaXlwsYHB+2Tg137Bjq1RVro764 8oAH7Nse1K2t+lEqOj1PL7uqEqdA2P9HbRYejsz8h9u/Wj6yf61kcPtXDVPa /zhgbT1YXqz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sH/bGaX5n+H7 f9nzH0M1kuc/Jfn9v/FgXOubeSp9xVWEsP9d5nO3r7RG9/3PAd9/xRWYkY3/ ZV03ku9/lqT9jwM3b8y2o3B22/VnWyz06E38Q9sRgygO3UY8L84P7JA//Yxw Ado8hEnHDXGK3z1VmJoXEW6agK8WMEXiN9Qkzm83HzluFLt+I8Y4LQvnaR/h rbsjkwxmj1+UJEcflJwEnnN0jgVJA4MGLoV3djAPvtgpZqGtMNjGsrVC9E6Q Q7mzkPsvLv73/RzGBC3m5+/dL0DuTVG/o7lPbhqePAHHZZDP4bKKP43kaSGL n4PJGzmcB9HJZOGDteIr8vykK35gm8AbSX34+czMNHyf0gl3R6T47qnVx+t4 qokJnuXMzDydeEopTKQ15s0jKj6R1gsDQtt38tp0skoXeadRb3SahGeZLdh4 g/Ee1NHSbqul23oJVHVO/FWnklyeAvOw509ek8Unz5aSFr13H3KTboGXq8DT FR6PAaLjntKGF2A5se/kZFRCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQkJCQuKL jf8D2gDf2wB4AAA= --------------080001020208050405080401 Content-Type: text/x-patch; name="analyze.c.diff" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="analyze.c.diff" *** analyze.c.orig 2013-04-26 10:40:06.634942283 +1200 --- analyze.c 2013-04-26 11:36:13.537404101 +1200 *************** *** 1173,1183 **** * has to adjust the numbers we send to the stats * collector to make this come out right.) */ ! if (TransactionIdIsCurrentTransactionId(HeapTupleHeaderGetXmin(targtuple.t_data))) ! { ! sample_it = true; ! liverows += 1; ! } break; case HEAPTUPLE_DELETE_IN_PROGRESS: --- 1173,1181 ---- * has to adjust the numbers we send to the stats * collector to make this come out right.) */ ! /* Amend so insert in progress tuples are counted */ ! sample_it = true; ! liverows += 1; break; case HEAPTUPLE_DELETE_IN_PROGRESS: --------------080001020208050405080401 Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-performance mailing list (pgsql-performance@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-performance --------------080001020208050405080401--