Thread

Commits

  1. Fix missed ReleaseVariableStats() in intarray's _int_matchsel().

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

    The Post Office <noreply@postgresql.org> — 2026-05-24T05:13:05Z

    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
    
    
    
    
    
    
  2. Re:BUG #19492: intarray: fix variable stats leak in _int_matchsel

    zengman <zengman@halodbtech.com> — 2026-05-24T05:15:43Z

    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
  3. Re: BUG #19492: intarray: fix variable stats leak in _int_matchsel

    Tom Lane <tgl@sss.pgh.pa.us> — 2026-05-25T22:17:25Z

    "=?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