Re: OOM in libpq and infinite loop with getCopyStart() - Mailing list pgsql-hackers

From Michael Paquier
Subject Re: OOM in libpq and infinite loop with getCopyStart()
Date
Msg-id CAB7nPqQMe1vnHZX5wr_Oi57ORQ=f7_BC9Eegughf+GrSBMDHeg@mail.gmail.com
Whole thread Raw
In response to Re: OOM in libpq and infinite loop with getCopyStart()  (Michael Paquier <michael.paquier@gmail.com>)
List pgsql-hackers
On Mon, Apr 18, 2016 at 4:48 PM, Michael Paquier
<michael.paquier@gmail.com> wrote:
> Another, perhaps, better idea is to add some more extra logic to
> pg_conn as follows:
> bool    is_emergency;
> PGresult *emergency_result;
> bool    is_emergency_consumed;
> So as when an OOM shows up, is_emergency is set to true. Then a first
> call of PQgetResult returns the PGresult with the OOM in
> emergency_result, setting is_emergency_consumed to true and switching
> is_emergency back to false. Then a second call to PQgetResult enforces
> the consumption of any results remaining without waiting for them, at
> the end returning NULL to the caller, resetting is_emergency_consumed
> to false. That's different compared to the extra failure
> PGASYNC_COPY_FAILED in the fact that it does not break PQisBusy().

So, in terms of code, it gives more or less the attached. I have
coupled the emergency_result with an enum flag emergency_status that
has 3 states:
- NONE, which is the default
- ENABLE, which is when an OOM or other error has been found in libpq.
Making the next call of PQgetResult return the emergency_result
- CONSUMED, set after PQgetResult is consuming emergency_result, to
return to caller NULL so as it can get out of any PQgetResult loop
expecting a result at the end of. Once NULL is returned, the flag is
switched back to NONE.
This works in the case of getCopyStart because the OOM failures are
happening before any messages are sent to server.

The checks for the emergency result are done in PQgetResult, so as any
upper-level routine gets the message. Tom, others, what do you think?
--
Michael

Attachment

pgsql-hackers by date:

Previous
From: Michael Paquier
Date:
Subject: Re: VS 2015 support in src/tools/msvc
Next
From: Dean Rasheed
Date:
Subject: Re: [COMMITTERS] pgsql: Add trigonometric functions that work in degrees.