Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1ZB3uR-0002Zg-4O for pgsql-bugs@arkaria.postgresql.org; Fri, 03 Jul 2015 16:33:51 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84) (envelope-from ) id 1ZB3uQ-00049L-GL for pgsql-bugs@arkaria.postgresql.org; Fri, 03 Jul 2015 16:33:50 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1ZB3tO-0003KM-8K for pgsql-bugs@postgresql.org; Fri, 03 Jul 2015 16:32:46 +0000 Received: from mail-wg0-x22f.google.com ([2a00:1450:400c:c00::22f]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84) (envelope-from ) id 1ZB3tK-0006Ti-6X for pgsql-bugs@postgresql.org; Fri, 03 Jul 2015 16:32:45 +0000 Received: by wgck11 with SMTP id k11so92440880wgc.0 for ; Fri, 03 Jul 2015 09:32:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:message-id:date:from:reply-to:user-agent:mime-version:to:cc :subject:references:in-reply-to:content-type; bh=9MTOoCS2vGSn3qE+tfl2VQD3nSVuaCszhWQfDkk4uCw=; b=hc575EKp/Hf6BwgJpYliJRCqMzHkj2yJ8d2OZWhJsT/3Ej+pwEjTtyILWCZFJ5VVmf VwCvNBkkngGWrgFKecGV0gxhzZC7hIRqY8mXCyaDOHsF4HIvEy3A2HJkE2Tvo7sRbGYX IZzcLyLgAwq5tFF3o/PD2aPOaXS/+8sLDD9+7Noh/iCAa91b8LevF4PuRygGW1zJwITZ IJNAZ5ybMXsl0/ZFxoUJBYb+GG5gOD+r2ojhTFGggmqz0GmVXyV1sO+6KrMFGOP0NXre LiqCvXDTRHjcgbyenIfdcc26MZjQhLdQPhQPLq6io6VMHWGwGe0bgp3m8TtdQ222Dtfi 0WfA== X-Received: by 10.180.82.230 with SMTP id l6mr63674955wiy.61.1435941160554; Fri, 03 Jul 2015 09:32:40 -0700 (PDT) Received: from [192.168.1.110] (dsl-hkibrasgw1-58c38f-82.dhcp.inet.fi. [88.195.143.82]) by mx.google.com with ESMTPSA id i6sm14090261wjf.29.2015.07.03.09.32.38 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 03 Jul 2015 09:32:39 -0700 (PDT) Message-ID: <5596B924.6020307@iki.fi> Date: Fri, 03 Jul 2015 19:32:36 +0300 From: Heikki Linnakangas Reply-To: hlinnaka@iki.fi User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Icedove/31.7.0 MIME-Version: 1.0 To: Michael Paquier , Oleksandr Shulgin CC: hlinnaka@iki.fi, Tom Lane , Pg Bugs Subject: Re: PQexec() hangs on OOM References: <547480DE.4040408@vmware.com> <25150.1428328002@sss.pgh.pa.us> <552427FC.6060302@iki.fi> <5526BA0A.4090600@iki.fi> <87wq03rz1t.fsf@ashulgin01.corp.ad.zalando.net> In-Reply-To: Content-Type: multipart/mixed; boundary="------------060105050105080502060205" X-Pg-Spam-Score: -2.4 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org This is a multi-part message in MIME format. --------------060105050105080502060205 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit On 05/26/2015 10:01 AM, Michael Paquier wrote: > On Thu, May 21, 2015 at 1:31 AM, Oleksandr Shulgin wrote: >> Michael Paquier 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 --------------060105050105080502060205 Content-Type: text/x-diff; name="improve-OOM-handling-in-libpq-2.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="improve-OOM-handling-in-libpq-2.patch" 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; } /* --------------060105050105080502060205 Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs --------------060105050105080502060205--