pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Heikki Linnakangas <hlinnaka@iki.fi>
To: Michael Paquier <michael.paquier@gmail.com>
To: Oleksandr Shulgin <oleksandr.shulgin@zalando.de>
Cc: hlinnaka@iki.fi, Tom Lane <tgl@sss.pgh.pa.us>
Cc: Pg Bugs <pgsql-bugs@postgresql.org>
Subject: Re: PQexec() hangs on OOM
Date: Fri, 03 Jul 2015 19:32:36 +0300
Message-ID: <5596B924.6020307@iki.fi> (raw)
In-Reply-To: <CAB7nPqT1TVnaKgfoXE7fgKhvL6-ibDWTj1BQT3TjEgp60_cH0A@mail.gmail.com>
References: <547480DE.4040408@vmware.com>
	<CAB7nPqR8UtXpTOkKOuxhhXM8PzcPa4dQDKE6rYx+tZhPrQQNWg@mail.gmail.com>
	<CAB7nPqSb3RF=s+BDyq1Un2+rng20EQQyxajdTRtpRG7XsyuLnw@mail.gmail.com>
	<25150.1428328002@sss.pgh.pa.us>
	<CAB7nPqQPaDb+=GCkdZb1L-VbpJTiigPx9_UNTA1a4H2MLGi9XA@mail.gmail.com>
	<552427FC.6060302@iki.fi>
	<CAB7nPqTqJmUL_jmATPvXzfET9u6DFQxtvF2J74D7XO6QebyAEw@mail.gmail.com>
	<5526BA0A.4090600@iki.fi>
	<CAB7nPqQBPSwz+g2BN5HQyUiMEUY-Scr9jm7RyBSOeo00HFo3gw@mail.gmail.com>
	<87wq03rz1t.fsf@ashulgin01.corp.ad.zalando.net>
	<CAB7nPqT1TVnaKgfoXE7fgKhvL6-ibDWTj1BQT3TjEgp60_cH0A@mail.gmail.com>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>

On 05/26/2015 10:01 AM, Michael Paquier wrote:
> On Thu, May 21, 2015 at 1:31 AM, Oleksandr Shulgin wrote:
>> Michael Paquier <michael.paquier@gmail.com> writes:
>>> So, attached is take three for all the other things above.
>>
>> There's still one call to pqGetErrorNotice3 that doesn't check returned
>> value for -2, in fe-connect.c.  Shouldn't we also check it like this:
>>
>> [...]
>
> Yes, you are right. Take 4 attached is updated with something similar
> to what you sent to cover this code path.

This is still a few bricks shy of a load. Before this patch, if you run 
out of memory when allocating a large result set - which is probably the 
most common reason for OOM in libpq - you got this error:

postgres=# select generate_series(1, 10000000);
out of memory for query result

With this patch, I got:

postgres=# select generate_series(1, 10000000);
server sent data ("D" message) without prior row description ("T" message)


Looking at the patch again, I think we should actually leave 
getAnotherTuple() alone. It's a lot nicer if getAnotherTuple() can 
handle the OOM error itself than propagating it to the caller.

There's only one caller for getAnotherTuple(), but for 
pqGetErrorNotice3() the same is even more true: it would be much nicer 
if it could handle OOM itself, without propagating it to the caller. And 
it well could do so. When it's processing an ERROR, it could just set 
conn->errorMessage to "out of memory", and discard the error it received 
from the server. When processing a NOTICE, it could likewise just send 
"out of memory" to the NOTICE processsor, and discard the message from 
the server. The real problem with pqGetErrorNotice3() today is that it 
treats OOM the same as "no data available yet", and we can fix that by 
reading but discarding the backend message. Like getAnotherTuple() does.

In short, the idea of returning a status code from parseInput(), instead 
of just dealing with the error, was a bad one. Sorry I didn't have this 
epiphany earlier...

I came up with the attached. It fixes the few cases where we were 
currently returning "need more data" when OOM happened, to deal with the 
OOM error instead, by setting conn->errorMessage. How does this look to you?

- Heikki


-- 
- Heikki

-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs

Attachments:

improve-OOM-handling-in-libpq-2.patch (text/x-diff, 10.2K) download diff --git a/src/interfaces/libpq/fe-protocol2.c b/src/interfaces/libpq/fe-protocol2.c index eeba7f3..7299bea 100644 --- a/src/interfaces/libpq/fe-protocol2.c +++ b/src/interfaces/libpq/fe-protocol2.c @@ -498,10 +498,17 @@ pqParseInput2(PGconn *conn) conn->result = PQmakeEmptyPGresult(conn, PGRES_COMMAND_OK); if (!conn->result) - return; + { + printfPQExpBuffer(&conn->errorMessage, + libpq_gettext("out of memory")); + pqSaveErrorResult(conn); + } + } + if (conn->result) + { + strlcpy(conn->result->cmdStatus, conn->workBuffer.data, + CMDSTATUS_LEN); } - strlcpy(conn->result->cmdStatus, conn->workBuffer.data, - CMDSTATUS_LEN); checkXactStatus(conn, conn->workBuffer.data); conn->asyncStatus = PGASYNC_READY; break; @@ -522,8 +529,16 @@ pqParseInput2(PGconn *conn) "unexpected character %c following empty query response (\"I\" message)", id); if (conn->result == NULL) + { conn->result = PQmakeEmptyPGresult(conn, PGRES_EMPTY_QUERY); + if (!conn->result) + { + printfPQExpBuffer(&conn->errorMessage, + libpq_gettext("out of memory")); + pqSaveErrorResult(conn); + } + } conn->asyncStatus = PGASYNC_READY; break; case 'K': /* secret key data from the backend */ @@ -633,8 +648,7 @@ pqParseInput2(PGconn *conn) /* * parseInput subroutine to read a 'T' (row descriptions) message. * We build a PGresult structure containing the attribute data. - * Returns: 0 if completed message, EOF if error or not enough data - * received yet. + * Returns: 0 if completed message, EOF if not enough data received yet. * * Note that if we run out of data, we have to suspend and reprocess * the message after more data is received. Otherwise, conn->inStart @@ -755,6 +769,10 @@ advance_and_error: conn->result = PQmakeEmptyPGresult(conn, PGRES_FATAL_ERROR); conn->asyncStatus = PGASYNC_READY; + if (result && result != conn->result) + PQclear(result); + return 0; + EOFexit: if (result && result != conn->result) PQclear(result); @@ -764,8 +782,7 @@ EOFexit: /* * parseInput subroutine to read a 'B' or 'D' (row data) message. * We fill rowbuf with column pointers and then call the row processor. - * Returns: 0 if completed message, EOF if error or not enough data - * received yet. + * Returns: 0 if completed message, EOF if not enough data received yet. * * Note that if we run out of data, we have to suspend and reprocess * the message after more data is received. Otherwise, conn->inStart @@ -965,14 +982,17 @@ pqGetErrorNotice2(PGconn *conn, bool isError) * Make a PGresult to hold the message. We temporarily lie about the * result status, so that PQmakeEmptyPGresult doesn't uselessly copy * conn->errorMessage. + * + * NB: This allocation might fail, if you run out of memory. The rest of + * the function handles that gracefully, and we still try to set the error + * message as the connection's error message. */ res = PQmakeEmptyPGresult(conn, PGRES_EMPTY_QUERY); - if (!res) - goto failure; - res->resultStatus = isError ? PGRES_FATAL_ERROR : PGRES_NONFATAL_ERROR; - res->errMsg = pqResultStrdup(res, workBuf.data); - if (!res->errMsg) - goto failure; + if (res) + { + res->resultStatus = isError ? PGRES_FATAL_ERROR : PGRES_NONFATAL_ERROR; + res->errMsg = pqResultStrdup(res, workBuf.data); + } /* * Break the message into fields. We can't do very much here, but we can @@ -1024,15 +1044,22 @@ pqGetErrorNotice2(PGconn *conn, bool isError) pqClearAsyncResult(conn); conn->result = res; resetPQExpBuffer(&conn->errorMessage); - appendPQExpBufferStr(&conn->errorMessage, res->errMsg); + if (res && !PQExpBufferDataBroken(workBuf)) + appendPQExpBufferStr(&conn->errorMessage, res->errMsg); + else + printfPQExpBuffer(&conn->errorMessage, + libpq_gettext("out of memory")); if (conn->xactStatus == PQTRANS_INTRANS) conn->xactStatus = PQTRANS_INERROR; } else { - if (res->noticeHooks.noticeRec != NULL) - (*res->noticeHooks.noticeRec) (res->noticeHooks.noticeRecArg, res); - PQclear(res); + if (res) + { + if (res->noticeHooks.noticeRec != NULL) + (*res->noticeHooks.noticeRec) (res->noticeHooks.noticeRecArg, res); + PQclear(res); + } } termPQExpBuffer(&workBuf); diff --git a/src/interfaces/libpq/fe-protocol3.c b/src/interfaces/libpq/fe-protocol3.c index a847f08..1a6a2c7 100644 --- a/src/interfaces/libpq/fe-protocol3.c +++ b/src/interfaces/libpq/fe-protocol3.c @@ -204,10 +204,15 @@ pqParseInput3(PGconn *conn) conn->result = PQmakeEmptyPGresult(conn, PGRES_COMMAND_OK); if (!conn->result) - return; + { + printfPQExpBuffer(&conn->errorMessage, + libpq_gettext("out of memory")); + pqSaveErrorResult(conn); + } } - strlcpy(conn->result->cmdStatus, conn->workBuffer.data, - CMDSTATUS_LEN); + if (conn->result) + strlcpy(conn->result->cmdStatus, conn->workBuffer.data, + CMDSTATUS_LEN); conn->asyncStatus = PGASYNC_READY; break; case 'E': /* error return */ @@ -226,7 +231,11 @@ pqParseInput3(PGconn *conn) conn->result = PQmakeEmptyPGresult(conn, PGRES_EMPTY_QUERY); if (!conn->result) - return; + { + printfPQExpBuffer(&conn->errorMessage, + libpq_gettext("out of memory")); + pqSaveErrorResult(conn); + } } conn->asyncStatus = PGASYNC_READY; break; @@ -239,7 +248,11 @@ pqParseInput3(PGconn *conn) conn->result = PQmakeEmptyPGresult(conn, PGRES_COMMAND_OK); if (!conn->result) - return; + { + printfPQExpBuffer(&conn->errorMessage, + libpq_gettext("out of memory")); + pqSaveErrorResult(conn); + } } conn->asyncStatus = PGASYNC_READY; } @@ -306,7 +319,11 @@ pqParseInput3(PGconn *conn) conn->result = PQmakeEmptyPGresult(conn, PGRES_COMMAND_OK); if (!conn->result) - return; + { + printfPQExpBuffer(&conn->errorMessage, + libpq_gettext("out of memory")); + pqSaveErrorResult(conn); + } } conn->asyncStatus = PGASYNC_READY; } @@ -628,7 +645,7 @@ getParamDescriptions(PGconn *conn) result = PQmakeEmptyPGresult(conn, PGRES_COMMAND_OK); if (!result) - goto failure; + goto oom_error; /* parseInput already read the 't' label and message length. */ /* the next two bytes are the number of parameters */ @@ -642,7 +659,7 @@ getParamDescriptions(PGconn *conn) result->paramDescs = (PGresParamDesc *) pqResultAlloc(result, nparams * sizeof(PGresParamDesc), TRUE); if (!result->paramDescs) - goto failure; + goto oom_error; MemSet(result->paramDescs, 0, nparams * sizeof(PGresParamDesc)); } @@ -661,8 +678,18 @@ getParamDescriptions(PGconn *conn) return 0; failure: - PQclear(result); + /* could not read the whole message */ + if (result) + PQclear(result); return EOF; + +oom_error: + if (result) + PQclear(result); + printfPQExpBuffer(&conn->errorMessage, + libpq_gettext("out of memory\n")); + pqSaveErrorResult(conn); + return 0; } /* @@ -822,11 +849,14 @@ pqGetErrorNotice3(PGconn *conn, bool isError) * Make a PGresult to hold the accumulated fields. We temporarily lie * about the result status, so that PQmakeEmptyPGresult doesn't uselessly * copy conn->errorMessage. + * + * NB: This allocation might fail, if you run out of memory. The rest of + * the function handles that gracefully, and we still try to set the error + * message as the connection's error message. */ res = PQmakeEmptyPGresult(conn, PGRES_EMPTY_QUERY); - if (!res) - goto fail; - res->resultStatus = isError ? PGRES_FATAL_ERROR : PGRES_NONFATAL_ERROR; + if (res) + res->resultStatus = isError ? PGRES_FATAL_ERROR : PGRES_NONFATAL_ERROR; /* * Read the fields and save into res. @@ -966,20 +996,27 @@ pqGetErrorNotice3(PGconn *conn, bool isError) */ if (isError) { - res->errMsg = pqResultStrdup(res, workBuf.data); - if (!res->errMsg) - goto fail; + if (res) + res->errMsg = pqResultStrdup(res, workBuf.data); pqClearAsyncResult(conn); conn->result = res; - appendPQExpBufferStr(&conn->errorMessage, workBuf.data); + if (PQExpBufferDataBroken(workBuf)) + printfPQExpBuffer(&conn->errorMessage, + libpq_gettext("out of memory")); + else + appendPQExpBufferStr(&conn->errorMessage, workBuf.data); } else { - /* We can cheat a little here and not copy the message. */ - res->errMsg = workBuf.data; - if (res->noticeHooks.noticeRec != NULL) - (*res->noticeHooks.noticeRec) (res->noticeHooks.noticeRecArg, res); - PQclear(res); + /* if we couldn't allocate the result set, just discard the NOTICE */ + if (res) + { + /* We can cheat a little here and not copy the message. */ + res->errMsg = workBuf.data; + if (res->noticeHooks.noticeRec != NULL) + (*res->noticeHooks.noticeRec) (res->noticeHooks.noticeRecArg, res); + PQclear(res); + } } termPQExpBuffer(&workBuf); @@ -1309,6 +1346,7 @@ getNotify(PGconn *conn) * CopyBothResponse message * * parseInput already read the message type and length. + * Returns 0 if successfully consumed the message, EOF if not enough data. */ static int getCopyStart(PGconn *conn, ExecStatusType copytype) @@ -1319,7 +1357,7 @@ getCopyStart(PGconn *conn, ExecStatusType copytype) result = PQmakeEmptyPGresult(conn, copytype); if (!result) - goto failure; + goto oom_error; if (pqGetc(&conn->copy_is_binary, conn)) goto failure; @@ -1335,7 +1373,7 @@ getCopyStart(PGconn *conn, ExecStatusType copytype) result->attDescs = (PGresAttDesc *) pqResultAlloc(result, nfields * sizeof(PGresAttDesc), TRUE); if (!result->attDescs) - goto failure; + goto oom_error; MemSet(result->attDescs, 0, nfields * sizeof(PGresAttDesc)); } @@ -1359,8 +1397,17 @@ getCopyStart(PGconn *conn, ExecStatusType copytype) return 0; failure: - PQclear(result); + if (result) + PQclear(result); return EOF; + +oom_error: + if (result) + PQclear(result); + printfPQExpBuffer(&conn->errorMessage, + libpq_gettext("out of memory\n")); + pqSaveErrorResult(conn); + return 0; } /*

view thread (48+ messages)  latest in thread

Message-ID: <5596B924.6020307@iki.fi>
Permalink:  ../5596B924.6020307@iki.fi/
Also on:    postgresql.org/message-id/5596B924.6020307@iki.fi

 ·  · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-bugs@postgresql.org
  Cc: hlinnaka@iki.fi, michael.paquier@gmail.com, oleksandr.shulgin@zalando.de, tgl@sss.pgh.pa.us
  Subject: Re: PQexec() hangs on OOM
  In-Reply-To: <5596B924.6020307@iki.fi>
  References: <547480DE.4040408@vmware.com> <CAB7nPqR8UtXpTOkKOuxhhXM8PzcPa4dQDKE6rYx+tZhPrQQNWg@mail.gmail.com> <CAB7nPqSb3RF=s+BDyq1Un2+rng20EQQyxajdTRtpRG7XsyuLnw@mail.gmail.com> <25150.1428328002@sss.pgh.pa.us> <CAB7nPqQPaDb+=GCkdZb1L-VbpJTiigPx9_UNTA1a4H2MLGi9XA@mail.gmail.com> <552427FC.6060302@iki.fi> <CAB7nPqTqJmUL_jmATPvXzfET9u6DFQxtvF2J74D7XO6QebyAEw@mail.gmail.com> <5526BA0A.4090600@iki.fi> <CAB7nPqQBPSwz+g2BN5HQyUiMEUY-Scr9jm7RyBSOeo00HFo3gw@mail.gmail.com> <87wq03rz1t.fsf@ashulgin01.corp.ad.zalando.net> <CAB7nPqT1TVnaKgfoXE7fgKhvL6-ibDWTj1BQT3TjEgp60_cH0A@mail.gmail.com> <5596B924.6020307@iki.fi>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox