Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit d25c2ee

Browse files
committed
In libpq, free any partial query result before collecting a server error.
We'd throw away the partial result anyway after parsing the error message. Throwing it away beforehand costs nothing and reduces the risk of out-of-memory failure. Also, at least in systems that behave like glibc/Linux, if the partial result was very large then the error PGresult would get allocated at high heap addresses, preventing the heap storage used by the partial result from being released to the OS until the error PGresult is freed. In psql >= 9.6, we hold onto the error PGresult until another error is received (for \errverbose), so that this behavior causes a seeming memory leak to persist for awhile, as in a recent complaint from Darafei Praliaskouski. This is a potential performance regression from older versions, justifying back-patching at least that far. But similar behavior may occur in other client applications, so it seems worth just back-patching to all supported branches. Discussion: https://postgr.es/m/CAC8Q8tJ=7cOkPePyAbJE_Pf691t8nDFhJp0KZxHvnq_uicfyVg@mail.gmail.com
1 parent fafec4c commit d25c2ee

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

src/interfaces/libpq/fe-protocol2.c

+9-1
Original file line numberDiff line numberDiff line change
@@ -966,6 +966,14 @@ pqGetErrorNotice2(PGconn *conn, bool isError)
966966
char *startp;
967967
char *splitp;
968968

969+
/*
970+
* If this is an error message, pre-emptively clear any incomplete query
971+
* result we may have. We'd just throw it away below anyway, and
972+
* releasing it before collecting the error might avoid out-of-memory.
973+
*/
974+
if (isError)
975+
pqClearAsyncResult(conn);
976+
969977
/*
970978
* Since the message might be pretty long, we create a temporary
971979
* PQExpBuffer rather than using conn->workBuffer. workBuffer is intended
@@ -1038,7 +1046,7 @@ pqGetErrorNotice2(PGconn *conn, bool isError)
10381046
*/
10391047
if (isError)
10401048
{
1041-
pqClearAsyncResult(conn);
1049+
pqClearAsyncResult(conn); /* redundant, but be safe */
10421050
conn->result = res;
10431051
resetPQExpBuffer(&conn->errorMessage);
10441052
if (res && !PQExpBufferDataBroken(workBuf) && res->errMsg)

src/interfaces/libpq/fe-protocol3.c

+9-1
Original file line numberDiff line numberDiff line change
@@ -879,6 +879,14 @@ pqGetErrorNotice3(PGconn *conn, bool isError)
879879
PQExpBufferData workBuf;
880880
char id;
881881

882+
/*
883+
* If this is an error message, pre-emptively clear any incomplete query
884+
* result we may have. We'd just throw it away below anyway, and
885+
* releasing it before collecting the error might avoid out-of-memory.
886+
*/
887+
if (isError)
888+
pqClearAsyncResult(conn);
889+
882890
/*
883891
* Since the fields might be pretty long, we create a temporary
884892
* PQExpBuffer rather than using conn->workBuffer. workBuffer is intended
@@ -943,7 +951,7 @@ pqGetErrorNotice3(PGconn *conn, bool isError)
943951
{
944952
if (res)
945953
res->errMsg = pqResultStrdup(res, workBuf.data);
946-
pqClearAsyncResult(conn);
954+
pqClearAsyncResult(conn); /* redundant, but be safe */
947955
conn->result = res;
948956
if (PQExpBufferDataBroken(workBuf))
949957
printfPQExpBuffer(&conn->errorMessage,

0 commit comments

Comments
 (0)