In libpq, free any partial query result before collecting a server error.
authorTom Lane
Fri, 13 Apr 2018 16:53:46 +0000 (12:53 -0400)
committerTom Lane
Fri, 13 Apr 2018 16:53:46 +0000 (12:53 -0400)
commit3dd36aa4b36c97cf9e00d29d8b138a1f1990fc62
tree32d84b1b7f3ae65a53d3779b9da1065e2fa27a39
parentf71d803c8de8daa7219ea52366ad3f4ff9345b3f
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
src/interfaces/libpq/fe-protocol2.c
src/interfaces/libpq/fe-protocol3.c