@@ -502,6 +502,7 @@ pgoutput_change(LogicalDecodingContext *ctx, ReorderBufferTXN *txn,
502502 MemoryContext old ;
503503 RelationSyncEntry * relentry ;
504504 TransactionId xid = InvalidTransactionId ;
505+ Relation ancestor = NULL ;
505506
506507 if (!is_publishable_relation (relation ))
507508 return ;
@@ -552,7 +553,8 @@ pgoutput_change(LogicalDecodingContext *ctx, ReorderBufferTXN *txn,
552553 if (relentry -> publish_as_relid != RelationGetRelid (relation ))
553554 {
554555 Assert (relation -> rd_rel -> relispartition );
555- relation = RelationIdGetRelation (relentry -> publish_as_relid );
556+ ancestor = RelationIdGetRelation (relentry -> publish_as_relid );
557+ relation = ancestor ;
556558 /* Convert tuple if needed. */
557559 if (relentry -> map )
558560 tuple = execute_attr_map_tuple (tuple , relentry -> map );
@@ -574,7 +576,8 @@ pgoutput_change(LogicalDecodingContext *ctx, ReorderBufferTXN *txn,
574576 if (relentry -> publish_as_relid != RelationGetRelid (relation ))
575577 {
576578 Assert (relation -> rd_rel -> relispartition );
577- relation = RelationIdGetRelation (relentry -> publish_as_relid );
579+ ancestor = RelationIdGetRelation (relentry -> publish_as_relid );
580+ relation = ancestor ;
578581 /* Convert tuples if needed. */
579582 if (relentry -> map )
580583 {
@@ -598,7 +601,8 @@ pgoutput_change(LogicalDecodingContext *ctx, ReorderBufferTXN *txn,
598601 if (relentry -> publish_as_relid != RelationGetRelid (relation ))
599602 {
600603 Assert (relation -> rd_rel -> relispartition );
601- relation = RelationIdGetRelation (relentry -> publish_as_relid );
604+ ancestor = RelationIdGetRelation (relentry -> publish_as_relid );
605+ relation = ancestor ;
602606 /* Convert tuple if needed. */
603607 if (relentry -> map )
604608 oldtuple = execute_attr_map_tuple (oldtuple , relentry -> map );
@@ -616,6 +620,12 @@ pgoutput_change(LogicalDecodingContext *ctx, ReorderBufferTXN *txn,
616620 Assert (false);
617621 }
618622
623+ if (RelationIsValid (ancestor ))
624+ {
625+ RelationClose (ancestor );
626+ ancestor = NULL ;
627+ }
628+
619629 /* Cleanup */
620630 MemoryContextSwitchTo (old );
621631 MemoryContextReset (data -> context );
0 commit comments