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 1k5wtJ-0003xD-LJ for pgsql-hackers@arkaria.postgresql.org; Wed, 12 Aug 2020 19:58:29 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1k5wtH-0007Uv-B8 for pgsql-hackers@arkaria.postgresql.org; Wed, 12 Aug 2020 19:58:27 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1k5wtH-0007Un-0U for pgsql-hackers@lists.postgresql.org; Wed, 12 Aug 2020 19:58:27 +0000 Received: from mail-qv1-xf42.google.com ([2607:f8b0:4864:20::f42]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1k5wtD-0006Jp-O0 for pgsql-hackers@lists.postgresql.org; Wed, 12 Aug 2020 19:58:25 +0000 Received: by mail-qv1-xf42.google.com with SMTP id x6so1608141qvr.8 for ; Wed, 12 Aug 2020 12:58:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:mime-version:content-disposition :content-transfer-encoding:in-reply-to:user-agent; bh=2oNlakA0Geu1pL3csywumY6LjjnVSiiXPwumIm5mlYY=; b=XwZWSfVFADeBJnJKKhQN0y9GJuYNGLD/Yplbkk2Mp26zb1jY46t8gRfmNMShtC0v9V HptvcbJErgllijeFyr8/h/4tvQS9Xc4nIwz7S4l7bej08ouKKyLdkZtMLJxy+gKcE10M IyfVRLouhbi8Zng0yvSU89UxT5Lnhx5rvL2usP0XAaTJXbWilI6BYp+PADG9YBeEVXWr Ay5ZqFJiErz8JqSDstlFys2VumpsKg25UcKIKP1qXQl33pCuAsDWOFG49PGP8CkawhrO o3k8lS8JGyL2BGxZiV0Wt6r83Qmpg5AYy8cM+eMJQIosxFsx0/7mVA5HfCl+CctEDy66 FB7Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:mime-version :content-disposition:content-transfer-encoding:in-reply-to :user-agent; bh=2oNlakA0Geu1pL3csywumY6LjjnVSiiXPwumIm5mlYY=; b=dzN7YO1L3re5MEVTf0CAl4h50L8hBCaEQ+jTPiJW/Ru/SSlwEQg40liAwPfgvdl8Qf OvztShKRIXvy8wEtzUln/NsF7m0oNgZ16jh6jHkMMMG7C9BHD2TxtcowHXCNfoZ8c5ba kOj8y29CHnxOUwZ3B6R+aPkXTZp+0in2Ffg4sUXjgjybV0ZBnVJt6YW1r/k8qhSM92RD AkPaSBvDyKEk1C1H/zO9LC/AEKxF2UOMF6mqarxkNGgI4OV5/6G4iV7cKDltD9Q59fjB 00jqo1QzZ3TqH/cZ3uABe1BQGk+7WiWKsqak7HJ5ksm8VbVLsX1nk3z2BuPUM+H3XFRe POJw== X-Gm-Message-State: AOAM530GMWOeE8vvq8ugJK72sVo1PDenBKnRBQzueLXDAWQAdzqnGRpi mnth4Km1Gy6/9l/UGZu8CmGx4bmkGGCsTuQex3TzyIVmyHe1Rtvv1KKRsmRqVKc05AqVFvLd97z VqQZjBJ3qL5rSCl+2IWk2LCF2d5XRTah8y5p8Mlv51tO5q4jZdE67P4Um1YAQvZ3yv25OmZq08M xOzldYm+tZtprctA== X-Google-Smtp-Source: ABdhPJwKlop96u0VXfvkrvoUzSiagCKQBDw6CTOb85e0mJh5a2kGXmGSqYJQKhtoUkmxWRb4AL2vzA== X-Received: by 2002:ad4:5812:: with SMTP id dd18mr1291691qvb.23.1597262302626; Wed, 12 Aug 2020 12:58:22 -0700 (PDT) Received: from perhan.alvh.no-ip.org ([190.95.19.47]) by smtp.gmail.com with ESMTPSA id j31sm3844133qtb.63.2020.08.12.12.58.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 12 Aug 2020 12:58:21 -0700 (PDT) Received: by perhan.alvh.no-ip.org (Postfix, from userid 1000) id AF40C2A1526; Wed, 12 Aug 2020 15:58:19 -0400 (-04) Date: Wed, 12 Aug 2020 15:58:19 -0400 From: Alvaro Herrera To: Anastasia Lubennikova Cc: PostgreSQL Hackers Subject: Re: [BUG] Error in BRIN summarization Message-ID: <20200812195819.GA20725@alvherre.pgsql> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="opJtzjQTFsWo+cga" Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20200812160149.GA24920@alvherre.pgsql> User-Agent: Mutt/1.10.1 (2018-07-13) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --opJtzjQTFsWo+cga Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit On 2020-Aug-12, Alvaro Herrera wrote: > 'anyvisible' mode is not required AFAICS; reading the code, I think this > could also hit REINDEX CONCURRENTLY and CREATE INDEX CONCURRENTLY, which > do not use that flag. I didn't try to reproduce it there, though. > Anyway, I'm going to remove that Assert() I added. So this is what I propose as the final form of the fix. -- Álvaro Herrera https://www.2ndQuadrant.com/ PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services --opJtzjQTFsWo+cga Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="v4-0001-fix-HOT-tuples-while-scanning-for-index-builds.patch" From e0abe5d957155285980a40fb33c192100699e8c0 Mon Sep 17 00:00:00 2001 From: Alvaro Herrera Date: Wed, 12 Aug 2020 14:02:58 -0400 Subject: [PATCH v4] fix HOT tuples while scanning for index builds --- src/backend/access/heap/heapam_handler.c | 20 ++++++++++++++++++++ src/backend/access/heap/pruneheap.c | 5 +++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index 267a6ee25a..ba44e30035 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -1324,6 +1324,12 @@ heapam_index_build_range_scan(Relation heapRelation, * buffer continuously while visiting the page, so no pruning * operation can occur either. * + * In cases with only ShareUpdateExclusiveLock on the table, it's + * possible for some HOT tuples to appear that we didn't know about + * when we first read the page. To handle that case, we re-obtain the + * list of root offsets when a HOT tuple points to a root item that we + * don't know about. + * * Also, although our opinions about tuple liveness could change while * we scan the page (due to concurrent transaction commits/aborts), * the chain root locations won't, so this info doesn't need to be @@ -1625,6 +1631,20 @@ heapam_index_build_range_scan(Relation heapRelation, offnum = ItemPointerGetOffsetNumber(&heapTuple->t_self); + /* + * If a HOT tuple points to a root that we don't know + * about, obtain root items afresh. If that still fails, + * report it as corruption. + */ + if (root_offsets[offnum - 1] == InvalidOffsetNumber) + { + Page page = BufferGetPage(hscan->rs_cbuf); + + LockBuffer(hscan->rs_cbuf, BUFFER_LOCK_SHARE); + heap_get_root_tuples(page, root_offsets); + LockBuffer(hscan->rs_cbuf, BUFFER_LOCK_UNLOCK); + } + if (!OffsetNumberIsValid(root_offsets[offnum - 1])) ereport(ERROR, (errcode(ERRCODE_DATA_CORRUPTED), diff --git a/src/backend/access/heap/pruneheap.c b/src/backend/access/heap/pruneheap.c index 256df4de10..7e3d44dfd6 100644 --- a/src/backend/access/heap/pruneheap.c +++ b/src/backend/access/heap/pruneheap.c @@ -732,7 +732,7 @@ heap_page_prune_execute(Buffer buffer, * root_offsets[k - 1] = j. * * The passed-in root_offsets array must have MaxHeapTuplesPerPage entries. - * We zero out all unused entries. + * Unused entries are filled with InvalidOffsetNumber (zero). * * The function must be called with at least share lock on the buffer, to * prevent concurrent prune operations. @@ -747,7 +747,8 @@ heap_get_root_tuples(Page page, OffsetNumber *root_offsets) OffsetNumber offnum, maxoff; - MemSet(root_offsets, 0, MaxHeapTuplesPerPage * sizeof(OffsetNumber)); + MemSet(root_offsets, InvalidOffsetNumber, + MaxHeapTuplesPerPage * sizeof(OffsetNumber)); maxoff = PageGetMaxOffsetNumber(page); for (offnum = FirstOffsetNumber; offnum <= maxoff; offnum = OffsetNumberNext(offnum)) -- 2.20.1 --opJtzjQTFsWo+cga--