Optimize duplicate code and fix memory leak in function fetch_remote_table_info()
Hackorum builds and tests every patch posted to the lists, not only commitfest submissions. This is Hackorum's own CI rather than the PostgreSQL project's, and it is still under testing - please report anything that looks wrong.
You can run a PostgreSQL built from this patch straight from Docker, with no checkout and no build:
docker run --rm -p 5432:5432 ghcr.io/hackorum-dev/postgres-patch:t49065psql -h localhost -U postgresBuilt from patchset v3 (message #3), July 27, 2026 at 11:26 PM.
Every patchset is also pushed to a branch of our PostgreSQL fork, so you can check out the same tree CI built. Without a PostgreSQL checkout:
git clone --branch t49065_3 https://github.com/hackorum-dev/postgres.gitIn a checkout you already have, add the fork once:
git remote add hackorum https://github.com/hackorum-dev/postgres.gitthen, for this patchset and every later one:
git fetch hackorum t49065_3 && git checkout t49065_3Patchset v3 (message #3) is on t49065_3
This patch fixes two things in the function fetch_remote_table_info().
(1) *pfree(pub_names.data)* to avoid potential memory leaks.
(2) 2 pieces of code can do the same work,
``` C
foreach(lc, MySubscription->publications)
{
if (foreach_current_index(lc) > 0)
appendStringInfoString(&pub_names, ", ");
appendStringInfoString(&pub_names,
quote_literal_cstr(strVal(lfirst(lc))));
}
```
and
``` C
foreach_node(String, pubstr, MySubscription->publications)
{
char *pubname = strVal(pubstr);
if (foreach_current_index(pubstr) > 0)
appendStringInfoString(&pub_names, ", ");
appendStringInfoString(&pub_names, quote_literal_cstr(pubname));
}
```
I wanna integrate them into one function `make_pubname_list()` to make the
code neater.
Thanks for your time.
Regards
Yongtao Huang
Attachments:
0001-Optimize-duplicate-code-and-fix-memory-leak-in-table.patchapplication/octet-stream; name=0001-Optimize-duplicate-code-and-fix-memory-leak-in-table.patchDownload+21-24
On Fri, Jan 19, 2024 at 10:42:46PM +0800, Yongtao Huang wrote:
This patch fixes two things in the function fetch_remote_table_info().
(1) *pfree(pub_names.data)* to avoid potential memory leaks.
True that this code puts some effort in cleaning up the memory used
locally.
(2) 2 pieces of code can do the same work,
```
I wanna integrate them into one function `make_pubname_list()` to make the
code neater.
It does not strike me as a huge problem to let the code be as it is on
HEAD when building the lists, FWIW, as we are talking about two places
and there is clarity in keeping the code as it is.
--
Michael
Thanks for your review.
(1) I think *pfree(pub_names.data)* is necessary.
(2) Agree with you. Considering that the new function is only called
twice, not encapsulating it into a function is not a huge problem.
Best wishes
Yongtao Huang
Michael Paquier <michael@paquier.xyz> 于2024年1月20日周六 11:13写道:
Show quoted text
On Fri, Jan 19, 2024 at 10:42:46PM +0800, Yongtao Huang wrote:
This patch fixes two things in the function fetch_remote_table_info().
(1) *pfree(pub_names.data)* to avoid potential memory leaks.
True that this code puts some effort in cleaning up the memory used
locally.(2) 2 pieces of code can do the same work,
```
I wanna integrate them into one function `make_pubname_list()` to makethe
code neater.
It does not strike me as a huge problem to let the code be as it is on
HEAD when building the lists, FWIW, as we are talking about two places
and there is clarity in keeping the code as it is.
--
Michael
Yongtao Huang <yongtaoh2022@gmail.com> writes:
(1) I think *pfree(pub_names.data)* is necessary.
Really?
It looks to me like copy_table, and thence fetch_remote_table_info,
is called once within a transaction. So whatever it leaks will be
released at transaction end. This is a good thing, because it's
messy enough that I seriously doubt that there aren't other leaks
in it, or that it'd be practical to expect that it can be made
to never leak anything.
If anything, I'd be inclined to remove the random pfree's that
are in it now. It's unlikely that they constitute a net win
compared to allowing memory context reset to clean things up.
regards, tom lane
Hi,
So whatever it leaks will be released at the transaction end.
I learned it. thank you very much for your explanation.
Regards,
Yongtao Huang
Tom Lane <tgl@sss.pgh.pa.us> 于2024年1月20日周六 12:34写道:
Show quoted text
Yongtao Huang <yongtaoh2022@gmail.com> writes:
(1) I think *pfree(pub_names.data)* is necessary.
Really?
It looks to me like copy_table, and thence fetch_remote_table_info,
is called once within a transaction. So whatever it leaks will be
released at transaction end. This is a good thing, because it's
messy enough that I seriously doubt that there aren't other leaks
in it, or that it'd be practical to expect that it can be made
to never leak anything.If anything, I'd be inclined to remove the random pfree's that
are in it now. It's unlikely that they constitute a net win
compared to allowing memory context reset to clean things up.regards, tom lane