missing PG_FREE_IF_COPY in textlike() and textnlike() ?

Started by CK Tanover 3 years ago3 messages
#1CK Tan
cktan@vitessedata.com

Hi Hackers,

I see in the texteq() function calls to DatumGetTextPP() are followed
by conditional calls to PG_FREE_IF_COPY. e.g.

https://github.com/postgres/postgres/blob/master/src/backend/utils/adt/varlena.c#L1792

text *targ1 = DatumGetTextPP(arg1);
text *targ2 = DatumGetTextPP(arg2);
result = (memcmp(VARDATA_ANY(targ1), VARDATA_ANY(targ2), len1 -
VARHDRSZ) == 0);
PG_FREE_IF_COPY(targ1, 0);
PG_FREE_IF_COPY(targ2, 1);

However, in textlike(), PG_FREE_IF_COPY calls are missing.

https://github.com/postgres/postgres/blob/master/src/backend/utils/adt/like.c#L283

Is this a memory leak bug?

Regards,
-cktan

#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: CK Tan (#1)
Re: missing PG_FREE_IF_COPY in textlike() and textnlike() ?

CK Tan <cktan@vitessedata.com> writes:

I see in the texteq() function calls to DatumGetTextPP() are followed
by conditional calls to PG_FREE_IF_COPY. e.g.

That's because texteq() is potentially usable as a btree index
function, and btree isn't too forgiving about leaks.

However, in textlike(), PG_FREE_IF_COPY calls are missing.
https://github.com/postgres/postgres/blob/master/src/backend/utils/adt/like.c#L283

textlike() isn't a member of any btree opclass.

Is this a memory leak bug?

Not unless you can demonstrate a case where it causes problems.
For the most part, such functions run in short-lived contexts.

regards, tom lane

#3CK Tan
cktan@vitessedata.com
In reply to: Tom Lane (#2)
Re: missing PG_FREE_IF_COPY in textlike() and textnlike() ?

Got it. It is a leak-by-design for efficiency.

Thanks,
-cktan

Show quoted text

On Fri, Sep 16, 2022 at 12:03 AM Tom Lane <tgl@sss.pgh.pa.us> wrote:

CK Tan <cktan@vitessedata.com> writes:

I see in the texteq() function calls to DatumGetTextPP() are followed
by conditional calls to PG_FREE_IF_COPY. e.g.

That's because texteq() is potentially usable as a btree index
function, and btree isn't too forgiving about leaks.

However, in textlike(), PG_FREE_IF_COPY calls are missing.
https://github.com/postgres/postgres/blob/master/src/backend/utils/adt/like.c#L283

textlike() isn't a member of any btree opclass.

Is this a memory leak bug?

Not unless you can demonstrate a case where it causes problems.
For the most part, such functions run in short-lived contexts.

regards, tom lane