Re: Fix for segfault in logical replication on master - Mailing list pgsql-hackers

From Mark Dilger
Subject Re: Fix for segfault in logical replication on master
Date
Msg-id A4C0513E-ED3F-4F5A-8C52-813112F217FA@enterprisedb.com
Whole thread Raw
In response to Re: Fix for segfault in logical replication on master  (Amit Kapila <amit.kapila16@gmail.com>)
Responses Re: Fix for segfault in logical replication on master  (Amit Kapila <amit.kapila16@gmail.com>)
List pgsql-hackers

> On Jun 17, 2021, at 6:40 AM, Amit Kapila <amit.kapila16@gmail.com> wrote:
>
> I think such a problem won't happen because we are using historic
> snapshots in this context. We rely on that in a similar way in
> reorderbuffer.c, see ReorderBufferProcessTXN.

I think you are right, but that's the part I have trouble fully convincing myself is safe.  We certainly have an
historicsnapshot when we call RelationGetIndexList, but that has an early exit if the relation already has fields set,
andwe don't know if those fields were set before or after the historic snapshot was taken.  Within the context of the
pluggableinfrastructure, I think we're safe.  The only caller of RelationGetIdentityKeyBitmap() in core is
logicalrep_write_attrs(),which is only called by logicalrep_write_rel(), which is only called by
send_relation_and_attrs(),which is only called by maybe_send_schema(), which is called by pgoutput_change() and
pgoutput_truncate(),both being callbacks in core's logical replication plugin. 

ReorderBufferProcessTXN calls SetupHistoricSnapshot before opening the relation and then calling
ReorderBufferApplyChangeto invoke the plugin on that opened relation, so the relation's fields could not have been
setupbefore the snapshot was taken.  Any other plugin would similarly get invoked after that same logic, so they'd be
fine,too.  The problem would only be if somebody called RelationGetIdentityKeyBitmap() or one of its calling functions
fromoutside that infrastructure.  Is that worth worrying about?  The function comments for those mention having an
historicsnapshot, and the Assert will catch if code doesn't have one, but I wonder how much of a trap for the unwary
thatis, considering that somebody might open the relation and lookup indexes for the relation before taking an historic
snapshotand calling these functions. 

I thought it was cheap enough to check that the relation we open is an index, because if it is not, we'll segfault when
accessingfields of the relation->rd_index struct.  I wouldn't necessarily advocate doing any really expensive checks
here,but a quick sanity check seemed worth the effort.  If you don't want to commit that part, I'm not going to put up
ahuge fuss. 

Since neither of you knew why I was performing that check, it is clear that my code comment was insufficient.  I have
addeda more detailed code comment to explain the purpose of the check.  I also changed the first check to use
RelationIsValid(),as suggested upthread. 



—
Mark Dilger
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company




Attachment

pgsql-hackers by date:

Previous
From: Andrew Dunstan
Date:
Subject: Re: Patch for bug #17056 fast default on non-plain table
Next
From: Robert Haas
Date:
Subject: Re: Replication protocol doc fix