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 1k5dve-000181-0y for pgsql-hackers@arkaria.postgresql.org; Tue, 11 Aug 2020 23:43:38 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1k5dvc-0001G7-0R for pgsql-hackers@arkaria.postgresql.org; Tue, 11 Aug 2020 23:43:36 +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 1k5dvb-0001G0-Le for pgsql-hackers@lists.postgresql.org; Tue, 11 Aug 2020 23:43:35 +0000 Received: from mail-qt1-x842.google.com ([2607:f8b0:4864:20::842]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1k5dvU-0002lv-Am for pgsql-hackers@lists.postgresql.org; Tue, 11 Aug 2020 23:43:35 +0000 Received: by mail-qt1-x842.google.com with SMTP id s23so221323qtq.12 for ; Tue, 11 Aug 2020 16:43:27 -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=DqWHFko9rg9BHN0YvHOjzgD9UZvH7XEqroIfS/75i5s=; b=cWooWSZjsmLqKS0bAzMqsBRiPYLt+8edEiKVDTCXtre0JKqGpFoMT7aYO/sY8YUmpm jPsL80PWoaSgywzBjTIMKTGYIO3R5xy7aV4nbfayz6k9sHvPLX5LiJcISgL4v009Db6a 7t1A8JJFFWSkWZ4OvkuSD3pjC+sN6zyb8jnCV0Br02xDIrXAQPweJK+uOZ+CcAMIBUkI TSTmlV2apDx6zHN5aGS75TC79HB2gOQFLFFpKKSd/DBNFejeQhwtjZfGO6lNDCl5tXQK DViVRL/nWF+TKGLx+rRzTz10AWRlbnQx/xE8VNtXL4CLt/66mlZnGE947EOCxAA82ru9 5rDQ== 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=DqWHFko9rg9BHN0YvHOjzgD9UZvH7XEqroIfS/75i5s=; b=a5sy2S/CiOKa6DUWEnT1zCnZSgvNJWUVm/rf4gJIQM88Jw9jP7OdQ1rxr1M0pCwmf1 ZyCivCPkjdA6p2r3Rd+1s8V0H/3SZRYDxWNVNoIreDAGq7vq7ap2VkT/zUx+IEac+ihQ s64xPm1BPThK+6nx3PS69Zf7MS0gmTOaa/4o315ZnCFDm94/MS2iJalnIEbKWXsxIex2 Fe8Xe4+1wlGCWVeBJhLm88WbiZl5ORkWcJdw19BQ7zPcxzRZxL/ov3/LUMrBkDCdB7PR WYktOliCBKfXiECBvvgVmCCtGaUhIP1yPHyFbf7ztNDCeEdbBUYRLtz7IUT9XJWpwoXV 08Bw== X-Gm-Message-State: AOAM532ZJE5oZz/QqlM6qOA/qnLzCRg0/bt7QMXJ+KXkrxKobRt56unU bpPZZHe7Iy88kyNwH3tRXx2K/nmxelX5Y+TKcNBqY9Vm8wExLEU70ca/VZT0GQdAxIcaTszuXHL YdBaOKv2lOWUL0kMYmm1JNO1b1KjtQyPpC4TmzLGTaf3kzRe4zd3maFAm1NrxBzpVR4Ch8DeXHY t4ST1IRdQ3vkmlf0B8 X-Google-Smtp-Source: ABdhPJzY2Do830cbuGlGndzOaKwEh1fr0hTd0RHI5o5/70gut2lVsvaM4ZN7kgfzCy4cv02YHOsLWQ== X-Received: by 2002:ac8:454b:: with SMTP id z11mr3849723qtn.350.1597189404918; Tue, 11 Aug 2020 16:43:24 -0700 (PDT) Received: from perhan.alvh.no-ip.org ([190.95.19.47]) by smtp.gmail.com with ESMTPSA id x67sm455210qke.136.2020.08.11.16.43.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 11 Aug 2020 16:43:23 -0700 (PDT) Received: by perhan.alvh.no-ip.org (Postfix, from userid 1000) id F27472A1528; Tue, 11 Aug 2020 19:43:21 -0400 (-04) Date: Tue, 11 Aug 2020 19:43:21 -0400 From: Alvaro Herrera To: Anastasia Lubennikova Cc: pgsql-hackers@lists.postgresql.org Subject: Re: [BUG] Error in BRIN summarization Message-ID: <20200811234321.GA17597@alvherre.pgsql> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="SLDf9lqlvOQaIe6s" Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <2588667e-d07d-7e10-74e2-7e1e46194491@postgrespro.ru> User-Agent: Mutt/1.10.1 (2018-07-13) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --SLDf9lqlvOQaIe6s Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit On 2020-Jul-30, Anastasia Lubennikova wrote: > While testing this fix, Alexander Lakhin spotted another problem. I > simplified  the test case to this: Ah, good catch. I think a cleaner way to fix this problem is to just consider the range as not summarized and return NULL from there, as in the attached patch. Running your test case with a telltale WARNING added at that point, it's clear that it's being hit. By returning NULL, we're forcing the caller to scan the heap, which is not great. But note that if you retry, and your VACUUM hasn't run yet by the time we go through the loop again, the same thing would happen. So it seems to me a good enough answer. A much more troubling thought is what happens if the range is desummarized, then the index item is used for the summary of a different range. Then the index might end up returning corrupt results. > At first, I tried to fix it by holding the lock on revmap->rm_currBuf until > we locked the regular page, but it causes a deadlock with brinsummarize(), > It can be easily reproduced with the same test as above. > Is there any rule about the order of locking revmap and regular pages in > brin? I haven't found anything in README. Umm, I thought that stuff was in the README, but it seems I didn't add it there. I think I had a .org file with my notes on that ... must be in an older laptop disk, because it's not in my worktree for that. I'll see if I can fish it out. -- Álvaro Herrera https://www.2ndQuadrant.com/ PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services --SLDf9lqlvOQaIe6s Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="brin-desumm-race.patch" diff --git a/src/backend/access/brin/brin_revmap.c b/src/backend/access/brin/brin_revmap.c index e8b8308f82..35746714a7 100644 --- a/src/backend/access/brin/brin_revmap.c +++ b/src/backend/access/brin/brin_revmap.c @@ -282,10 +282,17 @@ brinGetTupleForHeapBlock(BrinRevmap *revmap, BlockNumber heapBlk, /* If we land on a revmap page, start over */ if (BRIN_IS_REGULAR_PAGE(page)) { + /* + * If the offset number is greater than what's in the page, it's + * possible that the range was desummarized concurrently. Just + * return NULL to handle that case. + */ if (*off > PageGetMaxOffsetNumber(page)) - ereport(ERROR, - (errcode(ERRCODE_INDEX_CORRUPTED), - errmsg_internal("corrupted BRIN index: inconsistent range map"))); + { + LockBuffer(*buf, BUFFER_LOCK_UNLOCK); + return NULL; + } + lp = PageGetItemId(page, *off); if (ItemIdIsUsed(lp)) { --SLDf9lqlvOQaIe6s--