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: Amit Kapila <amit.kapila16@gmail.com>
Cc: Oleksandr Shulgin <oleksandr.shulgin@zalando.de>
Cc: Tom Lane <tgl@sss.pgh.pa.us>
Cc: Pg Bugs <pgsql-bugs@postgresql.org>
Subject: Re: PQexec() hangs on OOM
Date: Mon, 14 Dec 2015 19:11:43 +0200
Message-ID: <566EF84F.1030206@iki.fi> (raw)
In-Reply-To: <CAB7nPqTNrw8LR1HD7ZLnOJjC1BP1Evw_EMAdGOrV+s-sBCrKtw@mail.gmail.com>
References: <547480DE.4040408@vmware.com>
	<CAB7nPqR6LUkcdeiZJmdvTW=Cks2LSJcuGjL-AB4WP30e5bjXeg@mail.gmail.com>
	<CAA4eK1++kwTayKDaj6Nd7SjzsUBJA+Xkas70Dxcd-q8GeML-7A@mail.gmail.com>
	<CAB7nPqRWbL15EFVQdEzcgR4aUrTCtkCJKvUoRkQysgbCjEezKQ@mail.gmail.com>
	<CAA4eK1Kt=vBtb-0jOS=x7xb0zvTYPa91D0yuGA=TtQQUH3ESew@mail.gmail.com>
	<CAB7nPqQY=eXpgkNLaf+xQ0u3ZYAw6wHs72TfEVtiJQtT3HE7oA@mail.gmail.com>
	<CAA4eK1KbDUBGDxfsJ=MDfJuvG3M78hF=SQmh24t86Kt+=ZBThg@mail.gmail.com>
	<CAB7nPqQYOf-MYtgJ5UkvpNKOqOOG-vnXsTs=PJd6gsMDM8LS3Q@mail.gmail.com>
	<55FC501A.5000409@iki.fi>
	<CAA4eK1+MQWJ06H3BW=DxD7mEZNNf0L1OyGLiE+YNg2fC8Nvo2w@mail.gmail.com>
	<CAB7nPqT6gKj6iS9VTPth_h6Sz5Jo-177s6QJN_jrW66wyCjJ=w@mail.gmail.com>
	<CAA4eK1LyEcuX0QzpmQY5JtQkH8u6gCy8kFskn0+GOOgz5y6W2A@mail.gmail.com>
	<CAB7nPqSeZNNz4npbQq2m1sD--juQy0DQJRxSy-C1s2B8LYFHUA@mail.gmail.com>
	<CAA4eK1LG-DHPOHQOF=b2hq5Ee4KEDjT5VuDVkh1h=y6YwKJf8A@mail.gmail.com>
	<CAB7nPqTNrw8LR1HD7ZLnOJjC1BP1Evw_EMAdGOrV+s-sBCrKtw@mail.gmail.com>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>

On 16/10/15 05:00, Michael Paquier wrote:
> On Thu, Oct 15, 2015 at 11:28 PM, Amit Kapila <amit.kapila16@gmail.com> wrote:
>> On Sun, Oct 11, 2015 at 6:31 PM, Michael Paquier <michael.paquier@gmail.com>
>> wrote:
>>>
>>> Yeah, this behavior is caused by this piece of code:
>>> @@ -600,7 +601,9 @@ getRowDescriptions(PGconn *conn, int msgLength)
>>>   advance_and_error:
>>>          /* Discard unsaved result, if any */
>>>          if (result && result != conn->result)
>>>                  PQclear(result);
>>> An idea may be to check for conn->result->resultStatus !=
>>> PGRES_FATAL_ERROR to concatenate correctly the error messages using
>>> pqCatenateResultError. But just returning the first error encountered
>>> as you mention would be more natural. So I updated the patch this way.

I pushed the ParamDescription fix. I'm not quite sure on the details of 
the COPY patch. Do you remember what this change in PQexecFinish is for:

> @@ -1991,6 +1995,9 @@ PQexecFinish(PGconn *conn)
>  	 * We have to stop if we see copy in/out/both, however. We will resume
>  	 * parsing after application performs the data transfer.
>  	 *
> +	 * Stop if we are in copy mode and error has occurred, the pending results
> +	 * will be discarded during next execution in PQexecStart.
> +	 *
>  	 * Also stop if the connection is lost (else we'll loop infinitely).
>  	 */
>  	lastResult = NULL;
> @@ -2020,6 +2027,11 @@ PQexecFinish(PGconn *conn)
>  			result->resultStatus == PGRES_COPY_BOTH ||
>  			conn->status == CONNECTION_BAD)
>  			break;
> +		else if ((conn->asyncStatus == PGASYNC_COPY_IN ||
> +				  conn->asyncStatus == PGASYNC_COPY_OUT  ||
> +				  conn->asyncStatus == PGASYNC_COPY_BOTH) &&
> +				 result->resultStatus == PGRES_FATAL_ERROR)
> +			break;
>  	}
>
>  	return lastResult;

I don't immediately see why that's needed. I ran the little tester 
program I wrote earlier 
(http://www.postgresql.org/message-id/55FC501A.5000409@iki.fi), with and 
without that change, and it behaved the same.

Also, when I modify my tester program so that when malloc fails, all 
subsequent mallocs fail too, I'm still getting the "another command is 
already in progress" error. The problem scenario is:

1. Application calls PQexec("COPY foo FROM STDIN")
2. getCopyStart sets conn->asyncStatus = PGASYNC_COPY_IN.
3. PQgetResult calls getCopyResult() but it fails with OOM.

Now, libpq is already in PGASYNC_COPY_IN mode, but PQexec() returned 
NULL because of the OOM, so the application doesn't know that it's in 
copy mode. When the application calls PQexec() again, it fails with the 
"another command is already in progress" error.

Let's see if we can fix that too, or if we should put that off to yet 
another patch.

- Heikki



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



view thread (48+ messages)  latest in thread

Message-ID: <566EF84F.1030206@iki.fi>
Permalink:  ../566EF84F.1030206@iki.fi/
Also on:    postgresql.org/message-id/566EF84F.1030206@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, amit.kapila16@gmail.com, oleksandr.shulgin@zalando.de, tgl@sss.pgh.pa.us
  Subject: Re: PQexec() hangs on OOM
  In-Reply-To: <566EF84F.1030206@iki.fi>
  References: <547480DE.4040408@vmware.com> <CAB7nPqR6LUkcdeiZJmdvTW=Cks2LSJcuGjL-AB4WP30e5bjXeg@mail.gmail.com> <CAA4eK1++kwTayKDaj6Nd7SjzsUBJA+Xkas70Dxcd-q8GeML-7A@mail.gmail.com> <CAB7nPqRWbL15EFVQdEzcgR4aUrTCtkCJKvUoRkQysgbCjEezKQ@mail.gmail.com> <CAA4eK1Kt=vBtb-0jOS=x7xb0zvTYPa91D0yuGA=TtQQUH3ESew@mail.gmail.com> <CAB7nPqQY=eXpgkNLaf+xQ0u3ZYAw6wHs72TfEVtiJQtT3HE7oA@mail.gmail.com> <CAA4eK1KbDUBGDxfsJ=MDfJuvG3M78hF=SQmh24t86Kt+=ZBThg@mail.gmail.com> <CAB7nPqQYOf-MYtgJ5UkvpNKOqOOG-vnXsTs=PJd6gsMDM8LS3Q@mail.gmail.com> <55FC501A.5000409@iki.fi> <CAA4eK1+MQWJ06H3BW=DxD7mEZNNf0L1OyGLiE+YNg2fC8Nvo2w@mail.gmail.com> <CAB7nPqT6gKj6iS9VTPth_h6Sz5Jo-177s6QJN_jrW66wyCjJ=w@mail.gmail.com> <CAA4eK1LyEcuX0QzpmQY5JtQkH8u6gCy8kFskn0+GOOgz5y6W2A@mail.gmail.com> <CAB7nPqSeZNNz4npbQq2m1sD--juQy0DQJRxSy-C1s2B8LYFHUA@mail.gmail.com> <CAA4eK1LG-DHPOHQOF=b2hq5Ee4KEDjT5VuDVkh1h=y6YwKJf8A@mail.gmail.com> <CAB7nPqTNrw8LR1HD7ZLnOJjC1BP1Evw_EMAdGOrV+s-sBCrKtw@mail.gmail.com> <566EF84F.1030206@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