Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1gGu2Q-0003q2-VT for pgsql-bugs@arkaria.postgresql.org; Sun, 28 Oct 2018 23:00:07 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gGu2N-0007mu-6o for pgsql-bugs@arkaria.postgresql.org; Sun, 28 Oct 2018 23:00:03 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1gGu2M-0007mn-S8 for pgsql-bugs@lists.postgresql.org; Sun, 28 Oct 2018 23:00:03 +0000 Received: from mail-wr1-x443.google.com ([2a00:1450:4864:20::443]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gGu2F-00046Q-3G for pgsql-bugs@lists.postgresql.org; Sun, 28 Oct 2018 23:00:02 +0000 Received: by mail-wr1-x443.google.com with SMTP id y15-v6so6687269wru.9 for ; Sun, 28 Oct 2018 15:59:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=subject:to:cc:references:from:openpgp:autocrypt:message-id:date :user-agent:mime-version:in-reply-to:content-language; bh=UBhoWJX2FUEppTMZfJ+A8CvsuP2ES7a5WQea506+s68=; b=yE41mkyQs5k/NLRJfwoIGzrrPsp9Mu7Xy52Mf3dJNVkf+1DrobEODabT1XPXPhdlCC ugTTfUV9cBEdzvHxxk2dqTn1izp3Kv+FQGj7ahKXvrZNxZGMzbhUTNnsbzQsLxpfSDiD Eh1MgFekNE/JKyJ3ymO/sCCpmM9Rcf0/CwIAmVdbhLdE+owlx4Br7QQJwZ32qJu7cZ+K 0rmVX4ByMUnE8AohdCVOGThldThhIJO0d1UFwtIxf9xQjyt2/Y6cf9q6EC77XDcfClf2 CiMLOtysNf2JaLDwK4OOqY6XbPaSovkn8Jy0U05z7RGg8mBeg3uirzchC0n+xE8bBjUt 17OA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:openpgp:autocrypt :message-id:date:user-agent:mime-version:in-reply-to :content-language; bh=UBhoWJX2FUEppTMZfJ+A8CvsuP2ES7a5WQea506+s68=; b=uMvuEQS0rr04soUsUvE6lAQxKUiV4WxmmjTy9gGJjSqKdEGuL4V+tXEwFDu1G0yjsJ pz0f6YjSiT400hO6CJYOHIjACTOfjvUUvqv6CV3zXexkuBVm6aMBSw3EQ1IEJbAt3mCa Z2ZAEuBLOv7+vmTBPH4BwZ+RwJwzdm7J/Z7UA+KiSV93pFs1psxQ7i6gc3X+wmjP1VrV bW3EdpQ4xkHuwdhajxGXwng7JlkPs5lDdww3L6Gf80+SKEzBhR2yFwtvQV5IKKyUicls kzyJF72I7ckQgYrPx0LhRdNqLUn3qwC+sLM8jriMH/NtEmwYHJJ+yWocoUUfzpSNtFjc P+tQ== X-Gm-Message-State: AGRZ1gIViHeDjPtYK/Q11twG6MJf1vXzHrDJSXQdCX8qHTKU8lR0lx7C oH4dZPuZVEW3eVoRrMAe2h7oeg== X-Google-Smtp-Source: AJdET5fRiS6D5ROkLxEg6iIXgL7s7WYxefj2hcp7Iwg3vmreKHUy0CbcN0l9+ibO6+O6GNu+rOweOw== X-Received: by 2002:adf:b211:: with SMTP id u17-v6mr11830511wra.52.1540767591060; Sun, 28 Oct 2018 15:59:51 -0700 (PDT) Received: from [192.168.0.99] (150.123.broadband11.iol.cz. [90.178.123.150]) by smtp.gmail.com with ESMTPSA id t198-v6sm16223114wmd.9.2018.10.28.15.59.49 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 28 Oct 2018 15:59:49 -0700 (PDT) Subject: Re: BUG #15114: logical decoding Segmentation fault To: =?UTF-8?B?0J/QtdGC0YrRgCDQodC70LDQstC+0LI=?= , michael@paquier.xyz Cc: Alvaro Herrera , Andres Freund , Petr Jelinek , pgsql-bugs@lists.postgresql.org, Peter Eisentraut References: <20180326160801.ja4j43hwrc35iuhb@alvherre.pgsql> <20180416175901.mdevawuuhvwo3yk6@alvherre.pgsql> <20180427012203.GG3419@paquier.xyz> From: Petr Jelinek Openpgp: preference=signencrypt Autocrypt: addr=petr.jelinek@2ndquadrant.com; prefer-encrypt=mutual; keydata= xsFNBFawwSABEAC45Vua8rSggpM2kkMMmoOHz51a+fi3OXFfnrStVh80pM2/J9nSzjrIKQKf H5PtRnoYzSKnoUDoLOkLwDYitK3HzUUWXma+AWAqef2Y0GSZ7A3jJkD+JHOfkpS1CIpkEB/e WjA8Wqegwzc/OEE5111/l0Q1e6jxgZI86h8MMnm35Of1Y6Lz4kqWCyxHhaTJKTCkq66ICQ2C VJkbvuUoEE+Hr/I38dPHTAqIlCnycVnJPLHBQ/UYGHR/USF8qI/qJJx6tJcNAEqz69QZ/VlY 31UyJHdYe5NtBVz+rYZCOVWfpe3crbHuq9FjAPT+xcZefdxridmMKuW8eBmmXI9iZ3EG+X8J IsdLWL8QoqW0lnO1otYz1QDjdOvbEpq+u5/TAU8pgtaYH2J+6/GaQ6hMLyLBkaIAtnqlLCzq pxXcxMsFT14P5/rZA7EbBI8RS1ZIWJBlS2oO+dtVuENLJUC0SpptaRyn/uyOIopt7hSmBekL SzFnet7P9qofFJY+fQEz84v0G1gvwonHGHiCGgLpbMdPtnfPzda1aiTURvvOD7w/RmX1/ogk TqSGO4ejVNO/28QYouxmxzy8jbbIEiLCbHpLR/M/TvYHHPURxcx+MxVl3G+d/4Ihy4abxOdW pe9zLIcVMvqiUFQO28/79Gfhb7G4K+L21ORB82QYkzm3M7ebXQARAQABzSNQZXRyIEplbMOt bmVrIDxwam1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qtTxN+PmyvNUBmFEcagBX2TXqEkgnI6zOnN13mB4SihJ2lne2r 9RNiPYs/hmRxYg== Message-ID: <11af0d1b-e48f-fba3-5b1e-49886f648f52@2ndquadrant.com> Date: Sun, 28 Oct 2018 23:59:49 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.2.1 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/mixed; boundary="------------E63C6014F6F95EEBB64CB687" Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk This is a multi-part message in MIME format. --------------E63C6014F6F95EEBB64CB687 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit 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 > написа: > > 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 --------------E63C6014F6F95EEBB64CB687 Content-Type: text/x-patch; name="pg10-0001-Fix-REPLICA-IDENTITY-handling-in-logical-replication.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename*0="pg10-0001-Fix-REPLICA-IDENTITY-handling-in-logical-replicati"; filename*1="on.patch" =46rom c235ea36218bf3466397a713dc5e93112349024a Mon Sep 17 00:00:00 2001 From: Petr Jelinek Date: Sun, 28 Oct 2018 23:22:42 +0100 Subject: [PATCH] Fix REPLICA IDENTITY handling in logical replication pro= tocol 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/replic= ation/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" =20 @@ -501,6 +502,60 @@ logicalrep_read_tuple(StringInfo in, LogicalRepTuple= Data *tuple) } } =20 +/* + * 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 =3D NULL; + Oid idindex =3D RelationGetReplicaIndex(relation); + int i; + + /* If we already have the bitmap, just return it. */ + if (relation->rd_idattr !=3D 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 =3D SearchSysCache1(INDEXRELID, ObjectIdGetDatum(idindex)); + if (!tuple) + elog(ERROR, "cache lookup failed for index %u", idindex); + index =3D (Form_pg_index) GETSTRUCT(tuple); + natts =3D index->indnatts; + + for (i =3D 0; i < natts; i++) + idattrs =3D 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 =3D MemoryContextSwitchTo(CacheMemoryContext); + relation->rd_idattr =3D 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 =3D (rel->rd_rel->relreplident =3D=3D REPLICA_IDENTITY_FU= LL); if (!replidentfull) - idattrs =3D RelationGetIndexAttrBitmap(rel, - INDEX_ATTR_BITMAP_IDENTITY_KEY); + idattrs =3D get_rel_idattrs(rel); =20 /* send the attributes */ for (i =3D 0; i < desc->natts; i++) --=20 2.17.1 --------------E63C6014F6F95EEBB64CB687 Content-Type: text/x-patch; name="pg11-0001-Fix-REPLICA-IDENTITY-handling-in-logical-replication.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename*0="pg11-0001-Fix-REPLICA-IDENTITY-handling-in-logical-replicati"; filename*1="on.patch" =46rom d150377286c0e6aa61361586133d6f9e00d86c92 Mon Sep 17 00:00:00 2001 From: Petr Jelinek Date: Sun, 28 Oct 2018 23:22:42 +0100 Subject: [PATCH] Fix REPLICA IDENTITY handling in logical replication pro= tocol 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/replic= ation/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" =20 @@ -556,6 +557,60 @@ logicalrep_read_tuple(StringInfo in, LogicalRepTuple= Data *tuple) } } =20 +/* + * 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 =3D NULL; + Oid idindex =3D RelationGetReplicaIndex(relation); + int i; + + /* If we already have the bitmap, just return it. */ + if (relation->rd_idattr !=3D 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 =3D SearchSysCache1(INDEXRELID, ObjectIdGetDatum(idindex)); + if (!tuple) + elog(ERROR, "cache lookup failed for index %u", idindex); + index =3D (Form_pg_index) GETSTRUCT(tuple); + natts =3D index->indnkeyatts; + + for (i =3D 0; i < natts; i++) + idattrs =3D 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 =3D MemoryContextSwitchTo(CacheMemoryContext); + relation->rd_idattr =3D 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 =3D (rel->rd_rel->relreplident =3D=3D REPLICA_IDENTITY_FU= LL); if (!replidentfull) - idattrs =3D RelationGetIndexAttrBitmap(rel, - INDEX_ATTR_BITMAP_IDENTITY_KEY); + idattrs =3D get_rel_idattrs(rel); =20 /* send the attributes */ for (i =3D 0; i < desc->natts; i++) --=20 2.17.1 --------------E63C6014F6F95EEBB64CB687--