pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Michael Paquier <michael@paquier.xyz>
To: Andres Freund <andres@anarazel.de>
Cc: Andrey Borodin <x4mmm@yandex-team.ru>
Cc: Ben Chobot <bench@silentmedia.com>
Cc: Peter Geoghegan <pg@bowt.ie>
Cc: PostgreSQL mailing lists <pgsql-bugs@lists.postgresql.org>
Subject: Re: BUG #17401: REINDEX TABLE CONCURRENTLY creates a race condition on a streaming replica
Date: Mon, 25 Apr 2022 13:47:31 +0900
Message-ID: <YmYn46HoX4DeWtds@paquier.xyz> (raw)
In-Reply-To: <20220421161710.znq23x3zkgocbrhq@alap3.anarazel.de>
References: <C50E352E-FE90-46EC-94C2-3E96D9DDB141@yandex-team.ru>
	<bafcc4ab-8732-0503-ef17-0ee0420cf527@silentmedia.com>
	<04D2D137-102E-400D-B66B-C073C27A1327@yandex-team.ru>
	<20220211001240.47idtavhfyzi4aa3@alap3.anarazel.de>
	<AE561C32-E452-43D5-86EB-1CEB57405F95@yandex-team.ru>
	<29311A7B-240A-451A-9DC8-4909A0FEAE12@yandex-team.ru>
	<Yl+y7kvCDR6x4V+E@paquier.xyz>
	<5ABDE9D8-D09C-4281-9F84-BF73A865A726@yandex-team.ru>
	<YmDVWJt4WjRKFvAV@paquier.xyz>
	<20220421161710.znq23x3zkgocbrhq@alap3.anarazel.de>

On Thu, Apr 21, 2022 at 09:17:10AM -0700, Andres Freund wrote:
> On 2022-04-21 12:54:00 +0900, Michael Paquier wrote:
>> Based on the analysis of upthread, I have stuck with logging standby
>> snapshots once we are done in WaitForOlderSnapshots() so as we make sure
>> that a standby does not attempt to look at the data of any running
>> transactions it should wait for and the log of AELs at the end of
>> WaitForLockersMultiple() to avoid the access of the lockers too early.
> 
> Why is this necessary? The comments in WaitForOlderSnapshots() don't really
> explain it. This is pretty darn expensive.

I think that my brain here thought about the standby attempting to
access tuples that could have been deleted before the reference
snapshot was taken for the index validation in phase 3, and that
logging the current snapshot would help in detecting conflicts for
that.  But that won't help much unless the conflicts themselves are
logged in some way.  Is that right?  It looks like this comes down to
log more information when we wait for some of the snapshots in
VirtualXactLock().  That would be new.

>> amcheck could be made more robust here, by calling
>> RelationGetIndexList() after opening the Relation of the parent to
>> check if it is still listed in the returned list as it would look at
>> the relcache and discard any invalid indexes on the way.
> 
> That seems like a weird approach. Why can't the check just be done on the
> relcache entry of the index itself? If that doesn't work, something is still
> broken in cache invalidation.

As far as I looked at (aka checking step by step the concurrent
REINDEX and DROP INDEX flows), the cache looks fine with the
invalidations when the index's indisvalid is switched on/off (I am
taking into account the end of index_concurrently_build() that should
not need one).

FWIW, I would be tempted to do something like the attached for
amcheck, where we skip invalid indexes.  Thoughts welcome.
--
Michael

Attachments:

amcheck-indisvalid.patch (text/x-diff, 1.1K) download diff --git a/contrib/amcheck/verify_nbtree.c b/contrib/amcheck/verify_nbtree.c index 70278c4f93..17e131bbe7 100644 --- a/contrib/amcheck/verify_nbtree.c +++ b/contrib/amcheck/verify_nbtree.c @@ -294,6 +294,14 @@ bt_index_check_internal(Oid indrelid, bool parentcheck, bool heapallindexed, errmsg("could not open parent table of index \"%s\"", RelationGetRelationName(indrel)))); + /* + * An invalid index could be found here while a concurrent DROP INDEX + * or REINDEX is running in parallel. There is nothing interesting to + * do, so just leave. + */ + if (!indrel->rd_index->indisvalid) + return; + /* Relation suitable for checking as B-Tree? */ btree_index_checkable(indrel); @@ -361,13 +369,6 @@ btree_index_checkable(Relation rel) errmsg("cannot access temporary tables of other sessions"), errdetail("Index \"%s\" is associated with temporary relation.", RelationGetRelationName(rel)))); - - if (!rel->rd_index->indisvalid) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("cannot check index \"%s\"", - RelationGetRelationName(rel)), - errdetail("Index is not valid."))); } /*
signature.asc (application/pgp-signature, 832B) download

view thread (25+ messages)  latest in thread

Message-ID: <YmYn46HoX4DeWtds@paquier.xyz>
Permalink:  ../YmYn46HoX4DeWtds@paquier.xyz/
Also on:    postgresql.org/message-id/YmYn46HoX4DeWtds@paquier.xyz

 ·  · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-bugs@postgresql.org
  Cc: michael@paquier.xyz, andres@anarazel.de, x4mmm@yandex-team.ru, bench@silentmedia.com, pg@bowt.ie
  Subject: Re: BUG #17401: REINDEX TABLE CONCURRENTLY creates a race condition on a streaming replica
  In-Reply-To: <YmYn46HoX4DeWtds@paquier.xyz>
  References: <C50E352E-FE90-46EC-94C2-3E96D9DDB141@yandex-team.ru> <bafcc4ab-8732-0503-ef17-0ee0420cf527@silentmedia.com> <04D2D137-102E-400D-B66B-C073C27A1327@yandex-team.ru> <20220211001240.47idtavhfyzi4aa3@alap3.anarazel.de> <AE561C32-E452-43D5-86EB-1CEB57405F95@yandex-team.ru> <29311A7B-240A-451A-9DC8-4909A0FEAE12@yandex-team.ru> <Yl+y7kvCDR6x4V+E@paquier.xyz> <5ABDE9D8-D09C-4281-9F84-BF73A865A726@yandex-team.ru> <YmDVWJt4WjRKFvAV@paquier.xyz> <20220421161710.znq23x3zkgocbrhq@alap3.anarazel.de> <YmYn46HoX4DeWtds@paquier.xyz>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox