Re: Memory leak in pg_stat_statements when qtext file contains invalid encoding

From: Heikki Linnakangas <hlinnaka(at)iki(dot)fi>
To: Daniel Gustafsson <daniel(at)yesql(dot)se>
Cc: Lukas Fittl <lukas(at)fittl(dot)com>, Gaurav Singh <gaurav(dot)singh(at)yugabyte(dot)com>, pgsql-bugs(at)lists(dot)postgresql(dot)org
Subject: Re: Memory leak in pg_stat_statements when qtext file contains invalid encoding
Date: 2026-03-27 11:13:09
Message-ID: 2ca9a413-fe96-4c96-9781-2e85d876afd1@iki.fi
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

On 27/03/2026 11:05, Daniel Gustafsson wrote:
>> On 27 Mar 2026, at 09:59, Heikki Linnakangas <hlinnaka(at)iki(dot)fi> wrote:
>
>> Hmm, perhaps. But there's a simpler, less invasive fix. When that code was written, we didn't have MCXT_ALLOC_HUGE nor MCXT_ALLOC_NO_OOM. Now that we do, we can just use palloc_extended(MCXT_ALLOC_HUGE | MCXT_ALLOC_NO_OOM) instead of raw malloc(). Per attached.
>
> LGTM.

Committed, thanks!

Here's one way to reproduce the invalid encoding error without
artificially corrupting the file:

-- Run a query with a non-latin character in it. (This needs to be run
in UTF-8 database.)
psql postgres -c 'select g as "omega Ω col" from generate_series(1, 1) g;'

-- Create a database with latin1 encoding
psql postgres -c "create database latindb encoding 'latin1' lc_ctype='C'
lc_collate='C' template template0"

-- check pg_stat_statements() from the latin1 database
-- This fails with encoding conversion error.
PATH=~/pgsql.fsmfork/bin/ psql latindb -c "create extension
pg_stat_statements; select * from pg_stat_statements"

If you repeat the erroring "select * from pg_stat_statements" in latindb
many times, you can see the memory usage grow without this fix.

- Heikki

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Dmytro Astapov 2026-03-27 12:29:12 array_agg(anyarray) silently produces corrupt results with parallel workers when inputs mix NULL and non-NULL array elements
Previous Message Sérgio Duarte 2026-03-27 11:05:22 Re: POSTGIS | SQL Error [42501]: ERROR: permission denied to create "pg_catalog.geometry_dump"