Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WxmFZ-0006Tf-2Z for pgsql-bugs@arkaria.postgresql.org; Fri, 20 Jun 2014 00:00:13 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1WxmFY-0000uW-Fe for pgsql-bugs@arkaria.postgresql.org; Fri, 20 Jun 2014 00:00:12 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1WxmFW-0000uL-DS for pgsql-bugs@postgresql.org; Fri, 20 Jun 2014 00:00:10 +0000 Received: from mail.anarazel.de ([217.115.131.40]) by magus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WxmFR-0001rP-Sa for pgsql-bugs@postgresql.org; Fri, 20 Jun 2014 00:00:09 +0000 Received: by mail.anarazel.de (Postfix, from userid 108) id 57B45500001; Fri, 20 Jun 2014 02:00:02 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.2.5 (2008-06-10) on mail X-Spam-Level: X-Spam-Status: No, score=-5.8 required=5.0 tests=ALL_TRUSTED,AWL,BAYES_00 autolearn=ham version=3.2.5 Received: from anarazel.de (p5DDC710B.dip0.t-ipconnect.de [93.220.113.11]) (Authenticated sender: andres@anarazel.de) by mail.anarazel.de (Postfix) with ESMTPSA id 684B7500002; Fri, 20 Jun 2014 01:59:58 +0200 (CEST) Received: by anarazel.de (Postfix, from userid 1000) id DC8B810114D; Fri, 20 Jun 2014 01:59:57 +0200 (CEST) Date: Fri, 20 Jun 2014 01:59:57 +0200 From: Andres Freund To: Heikki Linnakangas Cc: levertond@googlemail.com, pgsql-bugs@postgresql.org, Alvaro Herrera Subject: Re: BUG #10533: 9.4 beta1 assertion failure in autovacuum process Message-ID: <20140619235957.GG16260@awork2.anarazel.de> References: <20140605180121.3061.28250@wrigleys.postgresql.org> <5390BA0F.2030103@vmware.com> <20140605213436.GA17217@awork2.anarazel.de> <20140609124616.GC3149@alap3.anarazel.de> <5395B0C6.2070000@vmware.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="V88s5gaDVPzZ0KCq" Content-Disposition: inline In-Reply-To: <5395B0C6.2070000@vmware.com> X-Pg-Spam-Score: -2.6 (--) 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 --V88s5gaDVPzZ0KCq Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Hi Heikki, all, On 2014-06-09 16:04:06 +0300, Heikki Linnakangas wrote: > On 06/09/2014 03:46 PM, Andres Freund wrote: > >>>I haven't thought particularly much about this, but I don't really see > >>>why the heap_page_is_all_visible() bit needs to be in a critical > >>>section? Can't we just do that entire bit after the log_heap_clean()? > >>>Then the heap_page_is_all_visible() can be done outside a critical > >>>section. > >Before I start working on a patch along those lines, do you see any > >problems with making the critical section smaller? > > One subtle difference is that the PD_ALL_VISIBLE flag will not be included > in the full-page-image that log_heap_page() might take. But that seems OK. > visibilitymap_set() writes a WAL record that sets it at replay. Heikki, you've changed things around a bit in 2a8e1ac598. That commit message mentions that there's still a difference in which order things happen on the master vs. the standby. Am I seeing it correctly that reordering things like hinted at above (and in the attached patch) fully gets rid of that problem? And doesn't reintroduce what you fixed? I've verified that the crash occurs without the patch and doesn't with it applied. I'm too tired right now to think enough about my fix to push it. Greetings, Andres Freund -- Andres Freund http://www.2ndQuadrant.com/ PostgreSQL Development, 24x7 Support, Training & Services --V88s5gaDVPzZ0KCq Content-Type: text/x-patch; charset=us-ascii Content-Disposition: attachment; filename="0001-Do-all-visible-handling-in-lazy_vacuum_page-outside-.patch" From ed6cf7928eaa6a724fc2a6d96cd59db65afc6a8b Mon Sep 17 00:00:00 2001 From: Andres Freund Date: Fri, 20 Jun 2014 01:17:33 +0200 Subject: [PATCH] Do all-visible handling in lazy_vacuum_page() outside its critical section. Since fdf9e21196a lazy_vacuum_page() rechecks the all-visible status of pages in the second pass over the heap. It does so inside a critical section, but both visibilitymap_test() and heap_page_is_all_visible() perform operations that should not happen inside one. The former potentially performs IO and both potentially do memory allocations. To fix, simply move all the all-visible handling outside the critical section. Doing so means that the PD_ALL_VISIBLE on the page won't be included in the full page image of the HEAP2_CLEAN record anymore. But that's fine, the flag will be set by the HEAP2_VISIBLE logged later. Backpatch to 9.3 where the problem was introduced. The bug only came to light due to the assertion added in 4a170ee9 and isn't likely to cause problems in production scenarios. The worst outcome is a avoidable PANIC restart. This also gets rid of the difference in the order of operations between master and standby mentioned in 2a8e1ac5. Per reports from David Leverton and Keith Fiske in bug #10533. --- src/backend/commands/vacuumlazy.c | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/src/backend/commands/vacuumlazy.c b/src/backend/commands/vacuumlazy.c index b4abeed..5d6d031 100644 --- a/src/backend/commands/vacuumlazy.c +++ b/src/backend/commands/vacuumlazy.c @@ -1214,13 +1214,6 @@ lazy_vacuum_page(Relation onerel, BlockNumber blkno, Buffer buffer, PageRepairFragmentation(page); /* - * Now that we have removed the dead tuples from the page, once again - * check if the page has become all-visible. - */ - if (heap_page_is_all_visible(onerel, buffer, &visibility_cutoff_xid)) - PageSetAllVisible(page); - - /* * Mark buffer dirty before we write WAL. */ MarkBufferDirty(buffer); @@ -1238,6 +1231,23 @@ lazy_vacuum_page(Relation onerel, BlockNumber blkno, Buffer buffer, } /* + * End critical section, so we safely can do visibility tests (which + * possibly need to perform IO and allocate memory!). If we crash now the + * page (including the corresponding vm bit) might not be marked all + * visible, but that's fine. A later vacuum will fix that. + */ + END_CRIT_SECTION(); + + /* + * Now that we have removed the dead tuples from the page, once again + * check if the page has become all-visible. The page is already marked + * dirty, exclusively locked, and, if needed, a full page image has been + * emitted in the log_heap_clean() above. + */ + if (heap_page_is_all_visible(onerel, buffer, &visibility_cutoff_xid)) + PageSetAllVisible(page); + + /* * All the changes to the heap page have been done. If the all-visible * flag is now set, also set the VM bit. */ @@ -1249,8 +1259,6 @@ lazy_vacuum_page(Relation onerel, BlockNumber blkno, Buffer buffer, visibility_cutoff_xid); } - END_CRIT_SECTION(); - return tupindex; } -- 2.0.0.rc2.4.g1dc51c6.dirty --V88s5gaDVPzZ0KCq Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs --V88s5gaDVPzZ0KCq--