RE: BUG #19411: libpq 16.x exhibits a memory leak when connections are repeatedly created and destroyed

From: Matt Carter <Matt(dot)Carter(at)twosigma(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: "pgsql-bugs(at)lists(dot)postgresql(dot)org" <pgsql-bugs(at)lists(dot)postgresql(dot)org>
Subject: RE: BUG #19411: libpq 16.x exhibits a memory leak when connections are repeatedly created and destroyed
Date: 2026-02-17 20:54:35
Message-ID: BL3PR08MB72838C12A57F4D9668D27AF0996DA@BL3PR08MB7283.namprd08.prod.outlook.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
> Please try varying the connection type and encryption.

Good idea. Here's what I found:

Test 1:
Configuration: Original (SQLAlchemy + NullPool)
libpq: 16.0.7
SQLAlchemy: Yes
Leak Rate: 803 KB/min

Test 2:
Configuration: Pure psycopg2 (default)
libpq: 16.0.7
SQLAlchemy: NO
Leak Rate: 801 KB/min

Test 3:
Configuration: Pure psycopg2 + gssencmode=disable
libpq: 16.0.7
SQLAlchemy: NO
Leak Rate: 858 KB/min

Test 4:
Configuration: Pure psycopg2 + SSL only
libpq: 16.0.7
SQLAlchemy: NO
Leak Rate: 861 KB/min

Test 5:
Configuration: Pure psycopg2
libpq: 13.0.11
SQLAlchemy: NO
Leak Rate: 17 KB/min

So, it seems none of these changes avoids the leak:
- Removing SQLAlchemy
- Disabling GSSAPI encryption
- Using SSL-only
- Changing PostgreSQL DB Server version from 16 to 13

The only changes that I found that avoid the leak are:
- Changing libpq version from 16 to 13, or
- Changing psycopg version from 2 to 3.

Tom, since your C test showed no leak, the issue is likely in how psycopg2 calls libpq, not pure libpq itself.

I guess I should report this to the psycopg2 project.

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Andrew Dunstan 2026-02-17 21:25:19 Re: BUG #19409: Function jsonb_strip_nulls() changed from immutable to stable.
Previous Message Andres Freund 2026-02-17 19:45:17 Re: BUG #19411: libpq 16.x exhibits a memory leak when connections are repeatedly created and destroyed