From 41518fa11debf07b131b3d2ccbc648dd1db15416 Mon Sep 17 00:00:00 2001 From: "Zizhuan Liu(X-MAN)" <44973863@qq.com> Date: Mon, 24 Aug 2026 17:59:34 +0800 Subject: [PATCH v3] Avoid unnecessary allocation for tablesync.c COPY copybuf MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit copybuf serves only as a read-only parse buffer and is never appended-to, enlarged or reset. Previously copybuf was a file-level static StringInfoData, allocated via makeStringInfo(). Replace it with a function-scope static StringInfoData (initialized to zero) inside copy_read_data(). It is re-initialized via initReadOnlyStringInfo() for each incoming buffer. Retain existing handling of leftover unconsumed data across function invocations. This removes an unnecessary allocation and encapsulates buffer-related logic inside copy_read_data(), improving readability and maintainability. Author: Chao Li Reviewed-by: álvaro Herrera , Denis Smirnov , ZizhuanLiu (X-MAN) 44973863@qq.com Discussion: https://postgr.es/m/5B2C9B4C-EAE6-4F21-AF99-613A561D26DC@gmail.com CommitFest: https://commitfest.postgresql.org/patch/6757/ --- src/backend/replication/logical/tablesync.c | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/src/backend/replication/logical/tablesync.c b/src/backend/replication/logical/tablesync.c index e5101997..34841f70 100644 --- a/src/backend/replication/logical/tablesync.c +++ b/src/backend/replication/logical/tablesync.c @@ -126,8 +126,6 @@ List *table_states_not_ready = NIL; -static StringInfo copybuf = NULL; - /* * Wait until the relation sync state is set in the catalog to the expected * one; return true when it happens. @@ -645,17 +643,18 @@ make_copy_attnamelist(LogicalRepRelMapEntry *rel) static int copy_read_data(void *outbuf, int minread, int maxread) { + static StringInfoData copybuf = {0}; int bytesread = 0; int avail; /* If there are some leftover data from previous read, use it. */ - avail = copybuf->len - copybuf->cursor; + avail = copybuf.len - copybuf.cursor; if (avail) { if (avail > maxread) avail = maxread; - memcpy(outbuf, ©buf->data[copybuf->cursor], avail); - copybuf->cursor += avail; + memcpy(outbuf, ©buf.data[copybuf.cursor], avail); + copybuf.cursor += avail; maxread -= avail; bytesread += avail; } @@ -680,16 +679,14 @@ copy_read_data(void *outbuf, int minread, int maxread) else { /* Process the data */ - copybuf->data = buf; - copybuf->len = len; - copybuf->cursor = 0; + initReadOnlyStringInfo(©buf, buf, len); - avail = copybuf->len - copybuf->cursor; + avail = copybuf.len - copybuf.cursor; if (avail > maxread) avail = maxread; - memcpy(outbuf, ©buf->data[copybuf->cursor], avail); + memcpy(outbuf, ©buf.data[copybuf.cursor], avail); outbuf = (char *) outbuf + avail; - copybuf->cursor += avail; + copybuf.cursor += avail; maxread -= avail; bytesread += avail; } @@ -1199,8 +1196,6 @@ copy_table(Relation rel) lrel.nspname, lrel.relname, res->err))); walrcv_clear_result(res); - copybuf = makeStringInfo(); - pstate = make_parsestate(NULL); (void) addRangeTableEntryForRelation(pstate, rel, AccessShareLock, NULL, false, false); -- 2.43.0