BUG #19492: intarray: fix variable stats leak in _int_matchsel

Started by PG Bug reporting form6 months ago3 messagesbugs
Jump to latest
#1PG Bug reporting form
noreply@postgresql.org

The following bug has been logged on the website:

Bug reference: 19492
Logged by: Man Zeng
Email address: zengman@halodbtech.com
PostgreSQL version: 18.4
Operating system: 24.04.1-Ubuntu
Description:

Hi all,

While investigating the intarray contrib module, I noticed a variable
stats leak in _int_matchsel().

When vardata.vartype != INT4ARRAYOID, the function returns
DEFAULT_EQ_SEL early without releasing the variable stats previously
acquired via examine_variable(). This causes a memory leak in the
current memory context.

The fix is straightforward: add ReleaseVariableStats(vardata) before
the early return.

Steps to reproduce:
```sql
postgres@zxm-VMware-Virtual-Platform:~/code/postgres/contrib$ psql
psql (19devel)
Type "help" for help.

postgres=# CREATE EXTENSION intarray;
CREATE EXTENSION
postgres=# CREATE OR REPLACE FUNCTION my_text_eq(a text, b text) RETURNS
boolean AS $$
BEGIN
RETURN a = b;
END;
$$ LANGUAGE plpgsql;
CREATE FUNCTION
postgres=# CREATE OPERATOR %% (
LEFTARG = text,
RIGHTARG = text,
FUNCTION = my_text_eq,
RESTRICT = _int_matchsel
);
CREATE OPERATOR
postgres=# CREATE TEMP TABLE test_vuln (t text);
CREATE TABLE
postgres=# INSERT INTO test_vuln SELECT md5(i::text) FROM generate_series(1,
1000) i;
ANALYZE test_vuln;
INSERT 0 1000
ANALYZE
postgres=# EXPLAIN (COSTS ON) SELECT * FROM test_vuln WHERE t %%
'test'::text;
2026-05-20 23:47:42.257 CST [11370] WARNING: resource was not closed: cache
pg_statistic (73), tuple 9/33 has count 1
WARNING: resource was not closed: cache pg_statistic (73), tuple 9/33 has
count 1
QUERY PLAN
------------------------------------------------------------
Seq Scan on test_vuln (cost=0.00..269.00 rows=5 width=33)
Filter: (t %% 'test'::text)
(2 rows)

postgres=#
```

SQL:
```sql
CREATE EXTENSION intarray;
CREATE OR REPLACE FUNCTION my_text_eq(a text, b text) RETURNS boolean AS $$
BEGIN
RETURN a = b;
END;
$$ LANGUAGE plpgsql;
CREATE OPERATOR %% (
LEFTARG = text,
RIGHTARG = text,
FUNCTION = my_text_eq,
RESTRICT = _int_matchsel
);
CREATE TEMP TABLE test_vuln (t text);
INSERT INTO test_vuln SELECT md5(i::text) FROM generate_series(1, 1000) i;
ANALYZE test_vuln;
EXPLAIN (COSTS ON) SELECT * FROM test_vuln WHERE t %% 'test'::text;
```

Regards,
Man Zeng

#2zengman
zengman@halodbtech.com
In reply to: PG Bug reporting form (#1)
Re:BUG #19492: intarray: fix variable stats leak in _int_matchsel

Patch attached. Also added a test case that exercises this code path
by creating a text operator with _int_matchsel as the restriction
estimator.

Regards,
Man Zeng

Attachments:

t139734_1
0001-Fix-variable-stats-leak-in-_int_matchsel-early-retur.patchapplication/octet-stream; charset=gb18030; name=0001-Fix-variable-stats-leak-in-_int_matchsel-early-retur.patchDownload+44-1
#3Tom Lane
tgl@sss.pgh.pa.us
In reply to: zengman (#2)
Re: BUG #19492: intarray: fix variable stats leak in _int_matchsel

"=?gb18030?B?emVuZ21hbg==?=" <zengman@halodbtech.com> writes:

Patch attached. Also added a test case that exercises this code path
by creating a text operator with _int_matchsel as the restriction
estimator.

Good catch, pushed. I didn't bother with the test case though ---
none of the other early exits in that function have test cases,
and I don't see why this one is any more fragile than the others.

regards, tom lane