pg.ddx.io pgsql-hackers@postgresql.org mailing list archive
help / color / mirror / Atom feedFrom: Chao Li <li.evan.chao@gmail.com>
To: PostgreSQL-development <pgsql-hackers@postgresql.org>
Subject: Fix a relcache reference leak in reorderbuffer.c
Date: Tue, 18 Aug 2026 11:51:48 +0800
Message-ID: <7DD65D03-3B5A-43B2-99AD-8E6AF5372BAB@gmail.com> (raw)
Hi,
While working on a feature patch that I’m going to post soon, I noticed a small issue in reorderbuffer.c. In the REORDER_BUFFER_CHANGE_TRUNCATE branch of ReorderBufferProcessTXN(), some opened relations might be skipped without being closed, leading to leaked relcache references.
The relevant code is:
```
relations = palloc0_array(Relation, nrelids);
for (i = 0; i < nrelids; i++)
{
Oid relid = change->data.truncate.relids[i];
Relation rel;
rel = RelationIdGetRelation(relid);
if (!RelationIsValid(rel))
elog(ERROR, "could not open relation with OID %u", relid);
if (!RelationIsLogicallyLogged(rel))
continue; <===== it should close rel before skipping it
relations[nrelations++] = rel;
}
/* Apply the truncate. */
ReorderBufferApplyTruncate(rb, txn, nrelations,
relations, change,
streaming);
for (i = 0; i < nrelations; i++)
RelationClose(relations[i]);
```
In the loop, each relation that is appended to relations is closed after the loop. However, when RelationIsLogicallyLogged(rel) returns false, the relation is skipped without being closed, causing the leak.
The attached patch makes a small fix to close the relation before continuing.
Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/
Attachments:
[application/octet-stream] v1-0001-Fix-relcache-reference-leak-when-decoding-TRUNCAT.patch (1.2K, ../7DD65D03-3B5A-43B2-99AD-8E6AF5372BAB@gmail.com/2-v1-0001-Fix-relcache-reference-leak-when-decoding-TRUNCAT.patch)
download | inline diff:
From 30a53ee7a12f42181615a61e625596f6dd00014c Mon Sep 17 00:00:00 2001
From: "Chao Li (Evan)" <lic@highgo.com>
Date: Tue, 18 Aug 2026 11:37:54 +0800
Subject: [PATCH v1] Fix relcache reference leak when decoding TRUNCATE
ReorderBufferProcessTXN() opens every relation referenced by a
TRUNCATE change. When RelationIsLogicallyLogged() returns false,
it skips the relation without releasing the reference acquired
by RelationIdGetRelation().
Close the relation before continuing.
Author: Chao Li <lic@highgo.com>
---
src/backend/replication/logical/reorderbuffer.c | 3 +++
1 file changed, 3 insertions(+)
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index 6aed6346366..b34ec5f2145 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -2492,7 +2492,10 @@ ReorderBufferProcessTXN(ReorderBuffer *rb, ReorderBufferTXN *txn,
elog(ERROR, "could not open relation with OID %u", relid);
if (!RelationIsLogicallyLogged(rel))
+ {
+ RelationClose(rel);
continue;
+ }
relations[nrelations++] = rel;
}
--
2.50.1 (Apple Git-155)
=
view thread (7+ messages) latest in thread
Message-ID: <7DD65D03-3B5A-43B2-99AD-8E6AF5372BAB@gmail.com>
Permalink: ../7DD65D03-3B5A-43B2-99AD-8E6AF5372BAB@gmail.com/
Also on: postgresql.org/message-id/7DD65D03-3B5A-43B2-99AD-8E6AF5372BAB@gmail.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 using the --to and --cc options:
reply via email
To: pgsql-hackers@postgresql.org
Cc: li.evan.chao@gmail.com
Subject: Re: Fix a relcache reference leak in reorderbuffer.c
In-Reply-To: <7DD65D03-3B5A-43B2-99AD-8E6AF5372BAB@gmail.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