On Tue, Apr 6, 2021 at 10:15 AM houzj.f...@fujitsu.com
<houzj.f...@fujitsu.com> wrote:
>
> > WARNING: relcache reference leak: relation "xxx" not closed.
> >
> > Example of the procedure:
> > ------publisher------
> > create table test (a int primary key);
> > create publication pub for table test;
> >
> > ------subscriber------
> > create table test (a int primary key);
> > create subscription sub connection 'dbname=postgres' publication pub;
> > create function funcA() returns trigger as $$ begin return null; end; $$ 
> > language
> > plpgsql; create trigger my_trig after insert or update or delete on test 
> > for each
> > row execute procedure funcA(); alter table test enable replica trigger 
> > my_trig;
> >
> > ------publisher------
> > insert into test values (6);
> >
> > It seems an issue about reference leak. Anyone can fix this?
>
> It seems ExecGetTriggerResultRel will reopen the target table because it 
> cannot find an existing one.
> Storing the opened table in estate->es_opened_result_relations seems solves 
> the problem.

It seems like commit 1375422c is related to this bug. The commit
introduced a new function ExecInitResultRelation() that sets both
estate->es_result_relations and estate->es_opened_result_relations. I
think it's better to use ExecInitResultRelation() rather than directly
setting estate->es_opened_result_relations. It might be better to do
that in create_estate_for_relation() though. Please find an attached
patch.

Since this issue happens on only HEAD and it seems an oversight of
commit 1375422c, I don't think regression tests for this are
essential.

Regards,

--
Masahiko Sawada
EDB:  https://www.enterprisedb.com/
diff --git a/src/backend/replication/logical/worker.c b/src/backend/replication/logical/worker.c
index 354fbe4b4b..190a6b48ec 100644
--- a/src/backend/replication/logical/worker.c
+++ b/src/backend/replication/logical/worker.c
@@ -1173,7 +1173,7 @@ apply_handle_insert(StringInfo s)
 										RelationGetDescr(rel->localrel),
 										&TTSOpsVirtual);
 	resultRelInfo = makeNode(ResultRelInfo);
-	InitResultRelInfo(resultRelInfo, rel->localrel, 1, NULL, 0);
+	ExecInitResultRelation(estate, resultRelInfo, 1);
 
 	/* Input functions may need an active snapshot, so get one */
 	PushActiveSnapshot(GetTransactionSnapshot());
@@ -1298,7 +1298,7 @@ apply_handle_update(StringInfo s)
 										RelationGetDescr(rel->localrel),
 										&TTSOpsVirtual);
 	resultRelInfo = makeNode(ResultRelInfo);
-	InitResultRelInfo(resultRelInfo, rel->localrel, 1, NULL, 0);
+	ExecInitResultRelation(estate, resultRelInfo, 1);
 
 	/*
 	 * Populate updatedCols so that per-column triggers can fire, and so
@@ -1455,7 +1455,7 @@ apply_handle_delete(StringInfo s)
 										RelationGetDescr(rel->localrel),
 										&TTSOpsVirtual);
 	resultRelInfo = makeNode(ResultRelInfo);
-	InitResultRelInfo(resultRelInfo, rel->localrel, 1, NULL, 0);
+	ExecInitResultRelation(estate, resultRelInfo, 1);
 
 	PushActiveSnapshot(GetTransactionSnapshot());
 

Reply via email to