Fix a relcache reference leak in reorderbuffer.c

From: Chao Li <li(dot)evan(dot)chao(at)gmail(dot)com>
To: PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Fix a relcache reference leak in reorderbuffer.c
Date: 2026-08-18 03:51:48
Message-ID: 7DD65D03-3B5A-43B2-99AD-8E6AF5372BAB@gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

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/

Attachment Content-Type Size
v1-0001-Fix-relcache-reference-leak-when-decoding-TRUNCAT.patch application/octet-stream 1.2 KB

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Peter Smith 2026-08-18 03:58:43 Re: Support EXCEPT for TABLES IN SCHEMA publications
Previous Message jian he 2026-08-18 03:47:53 Re: MERGE/SPLIT PARTITIONS issues/questions