Re: Make PQgetResult() not return NULL on out-of-memory error

From: Fujii Masao <masao(dot)fujii(at)gmail(dot)com>
To: Chao Li <li(dot)evan(dot)chao(at)gmail(dot)com>
Cc: Yugo Nagata <nagata(at)sraoss(dot)co(dot)jp>, Pgsql Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Make PQgetResult() not return NULL on out-of-memory error
Date: 2025-11-11 05:20:45
Message-ID: CAHGQGwEqExOdrv5kExU-zL43sYU07PHx1-OUSY-Dbx3h6=ha4w@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Tue, Nov 11, 2025 at 12:12 PM Chao Li <li(dot)evan(dot)chao(at)gmail(dot)com> wrote:
> For example, pgbench:
>
> * When pgbench calls readCommandResponse()
> * If OOM happens, PQgetResult() returns OOM_reslt whose resultState is PGRES_FATAL_ERROR
> * readCommandResponse() will goto the error tag, then discardAvailableResults() will be called
> * discardAvailableResults() only returns when res is NULL, so that, would discardAvailableResults() fall into an infinite loop?

Yes, that's a valid concern. If PQgetResult() keeps returning OOM_result due to
an out-of-memory error, some functions (e.g., discardAvailableResults()
in pgbench.c or PQexecFinish() in fe-exec.c) that expect PQgetResult() to
eventually return NULL could end up in an infinite loop.

To address this, callers need a way to distinguish between PGRES_FATAL_ERROR
and OOM. Functions that loop until PQgetResult() returns NULL should continue
if the result is PGRES_FATAL_ERROR, but should break if it's an OOM.
For example, one possible solution would be to add PGRES_OOM to
ExecStatusType, allowing callers to detect out-of-memory errors with
PQresultStatus() == PGRES_OOM.

Currently (even with the patch), OOM_result is returned only when
PQmakeEmptyPGresult() fails to allocate memory. But I guess there might be
other allocation failure paths. If we explicitly expose OOM_result,
we may also need to update PQgetResult() to return it in those other cases.
I'm not sure if that's the right direction, though... Probably we need to
look into the original commit that introduced OOM_result and its
related discussion.

Regards,

--
Fujii Masao

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Chao Li 2025-11-11 05:26:46 Re: Is this a typo?
Previous Message kenji uno 2025-11-11 05:11:40 [PATCH] Fix msvc_gendef.pl for aarch64 (Windows on Arm)