pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Petr Jelinek <petr.jelinek@2ndquadrant.com>
To: Петър Славов <pet.slavov@gmail.com>
To: michael@paquier.xyz
Cc: Alvaro Herrera <alvherre@alvh.no-ip.org>
Cc: Andres Freund <andres@anarazel.de>
Cc: Petr Jelinek <petr@2ndquadrant.com>
Cc: pgsql-bugs@lists.postgresql.org, Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
Subject: Re: BUG #15114: logical decoding Segmentation fault
Date: Sun, 28 Oct 2018 23:59:49 +0100
Message-ID: <11af0d1b-e48f-fba3-5b1e-49886f648f52@2ndquadrant.com> (raw)
In-Reply-To: <CAC5T6ED-Ti3Ryw9oU-o0JpQ=S3UNZ7BoQJCGzqb=t5736qrPzQ@mail.gmail.com>
References: <20180326160801.ja4j43hwrc35iuhb@alvherre.pgsql>
	<20180416175901.mdevawuuhvwo3yk6@alvherre.pgsql>
	<CAC5T6EBGj6AfapRYORj8_hJR7A36uePdr3Ds5n4yy6=ktcQCyg@mail.gmail.com>
	<20180427012203.GG3419@paquier.xyz>
	<CAC5T6ED-Ti3Ryw9oU-o0JpQ=S3UNZ7BoQJCGzqb=t5736qrPzQ@mail.gmail.com>

Hi,

This thread was brought to my attention during pgconf.eu (as I sadly
missed it).

On 04/10/2018 10:03, Петър Славов wrote:
> Hi,
> What happen here - I cannot see fix for this bug.
> I also cannot see the workaround parch in the code.
> Do I have to do something to mo e this fix forward?
> 
> I think we agreed that this bug braked logical decoding or I am wrong ?
>

Yes it's definitely a bug.

Also please note that we bottom-post in PostgreSQL mailing lists.

> 
> На пт, 27.04.2018 г. в 3:22 ч. Michael Paquier <michael@paquier.xyz
> <mailto:michael@paquier.xyz>> написа:
> 
>     On Fri, Apr 27, 2018 at 12:26:52AM +0200, Петър Славов wrote:
>     > I tried to compile this and i had this error:
>     >
>     > proto.c: In function ‘logicalrep_write_attrs’:
>     > proto.c:565:17: error: ‘FormData_pg_index {aka struct
>     FormData_pg_index}’
>     > has no member named ‘indnkeyatts’
>     >     natts = index->indnkeyatts;
> 
>     The patch Alvaro has sent is made for the master branch, and you are
>     visibly using a build of REL_10_STABLE.  Ths conflict you are seeing
>     comes from 8224de4 which introduced Covering indexes, and renamed
>     indnatts to indnkeyatts in Form_pg_index.
>     --
>     Michael
> 

I wrote my own patch to solve this which is quite similar to Alvaro's
except that it does not do the manual work to build indexes list as we
already have RelationGetReplicaIndex which does all the necessary work
and also uses the same rd_idattr for cache that
RelationGetIndexAttrBitmap does, it's better for performance and looks
safe to me.

I attached both patch for PG11+ (ie master) and the backport patch for
PG10, they only differ in the line identified above as problematic.

-- 
  Petr Jelinek                  http://www.2ndQuadrant.com/
  PostgreSQL Development, 24x7 Support, Training & Services

Attachments: download all (2 files, .tar.gz)

pg10-0001-Fix-REPLICA-IDENTITY-handling-in-logical-replication.patch (text/x-patch, 3.5K) download From c235ea36218bf3466397a713dc5e93112349024a Mon Sep 17 00:00:00 2001 From: Petr Jelinek <pjmodos@pjmodos.net> Date: Sun, 28 Oct 2018 23:22:42 +0100 Subject: [PATCH] Fix REPLICA IDENTITY handling in logical replication protocol We need column bitmap of replica identity so that we can send the correct key to the downstream. We used to build all the index information for this purpose but hat has two flaws: - The logical decoding does not have proper snapshot for building index info for functional indexes. - It's also not very efficient to build info we will never need This commit solves both of those by introducing special purpose function which only builds index info and hence the bitmap for REPLICA IDENTITY index. --- src/backend/replication/logical/proto.c | 58 ++++++++++++++++++++++++- 1 file changed, 56 insertions(+), 2 deletions(-) diff --git a/src/backend/replication/logical/proto.c b/src/backend/replication/logical/proto.c index 94dfee0b24..54855844f6 100644 --- a/src/backend/replication/logical/proto.c +++ b/src/backend/replication/logical/proto.c @@ -18,6 +18,7 @@ #include "libpq/pqformat.h" #include "replication/logicalproto.h" #include "utils/builtins.h" +#include "utils/catcache.h" #include "utils/lsyscache.h" #include "utils/syscache.h" @@ -501,6 +502,60 @@ logicalrep_read_tuple(StringInfo in, LogicalRepTupleData *tuple) } } +/* + * Get a bitmap of identity index attribute numbers. + * + * We cannot use RelationGetIndexAttrBitmap in output plugin because it would + * try to build IndexInfo for all the relation's indexes, which would be a + * problem, as the right authorization environment for that has not been set + * up (a problem particularly if there are any SQL-language functions.) + */ +static Bitmapset * +get_rel_idattrs(Relation relation) +{ + Bitmapset *idattrs = NULL; + Oid idindex = RelationGetReplicaIndex(relation); + int i; + + /* If we already have the bitmap, just return it. */ + if (relation->rd_idattr != NULL) + return bms_copy(relation->rd_idattr); + + /* Otherwise build it from the identity index if there is one. */ + if (OidIsValid(idindex)) + { + HeapTuple tuple; + Form_pg_index index; + int natts; + MemoryContext oldcxt; + + tuple = SearchSysCache1(INDEXRELID, ObjectIdGetDatum(idindex)); + if (!tuple) + elog(ERROR, "cache lookup failed for index %u", idindex); + index = (Form_pg_index) GETSTRUCT(tuple); + natts = index->indnatts; + + for (i = 0; i < natts; i++) + idattrs = bms_add_member(idattrs, index->indkey.values[i] - + FirstLowInvalidHeapAttributeNumber); + ReleaseSysCache(tuple); + + /* + * Save copy of the bitmap in the relcache entry. + * Note this will not make the relcache index info valid for + * RelationGetIndexAttrBitmap, but that does not matter as we don't + * mind if it rebuilds it - it should not anyway as this is supposed + * to be only called in output plugin where + * RelationGetIndexAttrBitmap does not work. + */ + oldcxt = MemoryContextSwitchTo(CacheMemoryContext); + relation->rd_idattr = bms_copy(idattrs); + MemoryContextSwitchTo(oldcxt); + } + + return idattrs; +} + /* * Write relation attributes to the stream. */ @@ -527,8 +582,7 @@ logicalrep_write_attrs(StringInfo out, Relation rel) /* fetch bitmap of REPLICATION IDENTITY attributes */ replidentfull = (rel->rd_rel->relreplident == REPLICA_IDENTITY_FULL); if (!replidentfull) - idattrs = RelationGetIndexAttrBitmap(rel, - INDEX_ATTR_BITMAP_IDENTITY_KEY); + idattrs = get_rel_idattrs(rel); /* send the attributes */ for (i = 0; i < desc->natts; i++) -- 2.17.1
pg11-0001-Fix-REPLICA-IDENTITY-handling-in-logical-replication.patch (text/x-patch, 3.5K) download From d150377286c0e6aa61361586133d6f9e00d86c92 Mon Sep 17 00:00:00 2001 From: Petr Jelinek <pjmodos@pjmodos.net> Date: Sun, 28 Oct 2018 23:22:42 +0100 Subject: [PATCH] Fix REPLICA IDENTITY handling in logical replication protocol We need column bitmap of replica identity so that we can send the correct key to the downstream. We used to build all the index information for this purpose but hat has two flaws: - The logical decoding does not have proper snapshot for building index info for functional indexes. - It's also not very efficient to build info we will never need This commit solves both of those by introducing special purpose function which only builds index info and hence the bitmap for REPLICA IDENTITY index. --- src/backend/replication/logical/proto.c | 58 ++++++++++++++++++++++++- 1 file changed, 56 insertions(+), 2 deletions(-) diff --git a/src/backend/replication/logical/proto.c b/src/backend/replication/logical/proto.c index 19451714da..d7d0fe0407 100644 --- a/src/backend/replication/logical/proto.c +++ b/src/backend/replication/logical/proto.c @@ -18,6 +18,7 @@ #include "libpq/pqformat.h" #include "replication/logicalproto.h" #include "utils/builtins.h" +#include "utils/catcache.h" #include "utils/lsyscache.h" #include "utils/syscache.h" @@ -556,6 +557,60 @@ logicalrep_read_tuple(StringInfo in, LogicalRepTupleData *tuple) } } +/* + * Get a bitmap of identity index attribute numbers. + * + * We cannot use RelationGetIndexAttrBitmap in output plugin because it would + * try to build IndexInfo for all the relation's indexes, which would be a + * problem, as the right authorization environment for that has not been set + * up (a problem particularly if there are any SQL-language functions.) + */ +static Bitmapset * +get_rel_idattrs(Relation relation) +{ + Bitmapset *idattrs = NULL; + Oid idindex = RelationGetReplicaIndex(relation); + int i; + + /* If we already have the bitmap, just return it. */ + if (relation->rd_idattr != NULL) + return bms_copy(relation->rd_idattr); + + /* Otherwise build it from the identity index if there is one. */ + if (OidIsValid(idindex)) + { + HeapTuple tuple; + Form_pg_index index; + int natts; + MemoryContext oldcxt; + + tuple = SearchSysCache1(INDEXRELID, ObjectIdGetDatum(idindex)); + if (!tuple) + elog(ERROR, "cache lookup failed for index %u", idindex); + index = (Form_pg_index) GETSTRUCT(tuple); + natts = index->indnkeyatts; + + for (i = 0; i < natts; i++) + idattrs = bms_add_member(idattrs, index->indkey.values[i] - + FirstLowInvalidHeapAttributeNumber); + ReleaseSysCache(tuple); + + /* + * Save copy of the bitmap in the relcache entry. + * Note this will not make the relcache index info valid for + * RelationGetIndexAttrBitmap, but that does not matter as we don't + * mind if it rebuilds it - it should not anyway as this is supposed + * to be only called in output plugin where + * RelationGetIndexAttrBitmap does not work. + */ + oldcxt = MemoryContextSwitchTo(CacheMemoryContext); + relation->rd_idattr = bms_copy(idattrs); + MemoryContextSwitchTo(oldcxt); + } + + return idattrs; +} + /* * Write relation attributes to the stream. */ @@ -582,8 +637,7 @@ logicalrep_write_attrs(StringInfo out, Relation rel) /* fetch bitmap of REPLICATION IDENTITY attributes */ replidentfull = (rel->rd_rel->relreplident == REPLICA_IDENTITY_FULL); if (!replidentfull) - idattrs = RelationGetIndexAttrBitmap(rel, - INDEX_ATTR_BITMAP_IDENTITY_KEY); + idattrs = get_rel_idattrs(rel); /* send the attributes */ for (i = 0; i < desc->natts; i++) -- 2.17.1

view thread (30+ messages)  latest in thread

Message-ID: <11af0d1b-e48f-fba3-5b1e-49886f648f52@2ndquadrant.com>
Permalink:  ../11af0d1b-e48f-fba3-5b1e-49886f648f52@2ndquadrant.com/
Also on:    postgresql.org/message-id/11af0d1b-e48f-fba3-5b1e-49886f648f52@2ndquadrant.com

 ·  · 

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: petr.jelinek@2ndquadrant.com, pet.slavov@gmail.com, michael@paquier.xyz, alvherre@alvh.no-ip.org, andres@anarazel.de, petr@2ndquadrant.com, peter.eisentraut@2ndquadrant.com
  Subject: Re: BUG #15114: logical decoding Segmentation fault
  In-Reply-To: <11af0d1b-e48f-fba3-5b1e-49886f648f52@2ndquadrant.com>
  References: <20180326160801.ja4j43hwrc35iuhb@alvherre.pgsql> <20180416175901.mdevawuuhvwo3yk6@alvherre.pgsql> <CAC5T6EBGj6AfapRYORj8_hJR7A36uePdr3Ds5n4yy6=ktcQCyg@mail.gmail.com> <20180427012203.GG3419@paquier.xyz> <CAC5T6ED-Ti3Ryw9oU-o0JpQ=S3UNZ7BoQJCGzqb=t5736qrPzQ@mail.gmail.com> <11af0d1b-e48f-fba3-5b1e-49886f648f52@2ndquadrant.com>

* 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