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 1f88PF-0002s9-C2 for pgsql-bugs@arkaria.postgresql.org; Mon, 16 Apr 2018 17:59:09 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1f88PE-0006A0-An for pgsql-bugs@arkaria.postgresql.org; Mon, 16 Apr 2018 17:59:08 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1f88PE-00069r-11 for pgsql-bugs@lists.postgresql.org; Mon, 16 Apr 2018 17:59:08 +0000 Received: from new1-smtp.messagingengine.com ([66.111.4.221]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1f88PB-0000YS-5y for pgsql-bugs@lists.postgresql.org; Mon, 16 Apr 2018 17:59:06 +0000 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailnew.nyi.internal (Postfix) with ESMTP id 749761720; Mon, 16 Apr 2018 13:59:04 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute4.internal (MEProxy); Mon, 16 Apr 2018 13:59:04 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:subject:to :x-me-sender:x-me-sender:x-sasl-enc; s=fm2; bh=Ie7Can63S9yzO9I5u s2/JwQg2jYVrFMyzvO+tn1uN34=; b=kLzIgiQATYXOIxrwAPDcbXHsUjnakls7G hdN3EInB31O0vIo4uyxKLJMdWzQ19HQqt7SN5fBtAZZY73WHjCy8C6eK2lJZLMEN qt4UO+xuy7UDjCQqp/oIiUirE6ZAmb/qqnZg4gK1bUgTv89w0Yx/Rf8FSQtX5pEj t48Qr8rMcLc/AsmgYdXnYbMcowTWYWwKm3hcihOjtz8hzjil/cLM3AiJd7Px65d5 ZG0U/lWLZx9WjNIKIsAkMQH1MZj0d1uFh+yof0mM61oAqTgvuWsnk1UGTXwumS4T Af9/5Yv2pvi53PJ49/gJgLKCV3mIvFgiXD2r9t1kVAk6kUiZpJJXA== X-ME-Sender: Received: from alvin.alvh.no-ip.org (unknown [179.56.29.253]) by mail.messagingengine.com (Postfix) with ESMTPA id 8CEC210256; Mon, 16 Apr 2018 13:59:03 -0400 (EDT) Received: by alvin.alvh.no-ip.org (Postfix, from userid 1000) id B908E7D6; Mon, 16 Apr 2018 14:59:01 -0300 (-03) Date: Mon, 16 Apr 2018 14:59:01 -0300 From: Alvaro Herrera To: Andres Freund Cc: =?utf-8?B?0J/QtdGC0YrRgCDQodC70LDQstC+0LI=?= , Petr Jelinek , pgsql-bugs@lists.postgresql.org, Peter Eisentraut Subject: Re: BUG #15114: logical decoding Segmentation fault Message-ID: <20180416175901.mdevawuuhvwo3yk6@alvherre.pgsql> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="w4uh7iykqtkmpofo" Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20180326160801.ja4j43hwrc35iuhb@alvherre.pgsql> User-Agent: NeoMutt/20170306-137-4415bd-dirty (1.8.0) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --w4uh7iykqtkmpofo Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit Alvaro Herrera wrote: > I agree -- I think logicalrep_write_attrs() should not use > RelationGetIndexAttrBitmap at all but instead use some lower-level way > to obtain columns of the replica identity. I don't know whether there > are historical-snapshot-related considerations to care about, in this > code, though. Here's a patch. There is a disadvantage: one additional syscache lookup each time logicalrep_write_attrs() is called. I think that's all right, since that's done once for each relation being replicated (not, say, once per tuple or once per transaction). Even so, I tried to move this code as a special "quick-path" case in RelationGetIndexAttrBitmap, so that it could be cached separately without processing the other indexes. That turned out a bit too messy for my taste. -- Álvaro Herrera https://www.2ndQuadrant.com/ PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services --w4uh7iykqtkmpofo Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="proto.patch" diff --git a/src/backend/replication/logical/proto.c b/src/backend/replication/logical/proto.c index edc97a7662..06fa5c5647 100644 --- a/src/backend/replication/logical/proto.c +++ b/src/backend/replication/logical/proto.c @@ -582,8 +582,47 @@ 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); + { + Oid idindex = rel->rd_replidindex; + + /* + * Building the index list determines the replica index OID as a side + * effect, so do so now unless we already have it. + */ + if (rel->rd_replidindex == InvalidOid) + { + list_free(RelationGetIndexList(rel)); + idindex = rel->rd_replidindex; + } + + /* + * Now build the bitmap. We cannot use RelationGetIndexAttrBitmap + * here: 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 yet (a problem + * particularly if there are any SQL-language functions.) + * + * It would be nice to cache this somewhere, but it's not clear how to + * invalidate it when the time comes. + */ + if (OidIsValid(idindex)) + { + HeapTuple tuple; + Form_pg_index index; + int natts; + + 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); + } + } /* send the attributes */ for (i = 0; i < desc->natts; i++) --w4uh7iykqtkmpofo--