pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
pgsql: postgres_fdw: Fix handling of pending asynchronous requests.
8+ messages / 4 participants
[nested] [flat]

Commits from this thread:
  2021-08-02 a8ed9bd59d fb234086fe  Fix oversight in commit 1ec7fca8592178281cd5cdada0f27a340fb813fc.
* pgsql: postgres_fdw: Fix handling of pending asynchronous requests.
@ 2021-07-30 08:08 Etsuro Fujita <efujita@postgresql.org>
  2021-07-30 12:45 ` Re: pgsql: postgres_fdw: Fix handling of pending asynchronous requests. Michael Paquier <michael@paquier.xyz>
  0 siblings, 1 reply; 8+ messages in thread

From: Etsuro Fujita @ 2021-07-30 08:08 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

postgres_fdw: Fix handling of pending asynchronous requests.

A pending asynchronous request is handled by process_pending_request(),
which previously not only processed an in-progress remote query but
performed ExecForeignScan() to produce a tuple to return to the local
server asynchronously from the result of the remote query.  But that led
to a server crash when executing a query or led to an "InstrStartNode
called twice in a row" or "InstrEndLoop called on running node" failure
when doing EXPLAIN ANALYZE of it, in cases where the plan tree for it
contained multiple async-capable nodes accessing the same
initplan/subplan that contained multiple async-capable nodes scanning
the same foreign tables as for the parent async-capable nodes, as
reported by Andrey Lepikhov.  The reason is that the second step in
process_pending_request() invoked when executing the initplan/subplan
for one of the parent async-capable nodes caused recursive execution of
the initplan/subplan for another of the parent async-capable nodes.

To fix, split process_pending_request() into the two steps and postpone
the second step until ForeignAsyncConfigureWait() is called for each of
the pending asynchronous requests.  Also, in ExecAppendAsyncEventWait()
we assumed that FDWs would register at least one wait event in a
WaitEventSet created there when they were called from
ForeignAsyncConfigureWait() in that function, but allow FDWs to register
zero wait events in the WaitEventSet; modify ExecAppendAsyncEventWait()
to just return in that case.

Oversight in commit 27e1f1456.  Back-patch to v14 where that commit went
in.

Andrey Lepikhov and Etsuro Fujita

Discussion: https://postgr.es/m/fe5eaa19-1704-e4a4-76ee-3b9d37ade399@postgrespro.ru

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/1ec7fca8592178281cd5cdada0f27a340fb813fc

Modified Files
--------------
contrib/postgres_fdw/expected/postgres_fdw.out | 55 +++++++++++++++++++++-
contrib/postgres_fdw/postgres_fdw.c            | 65 ++++++++++++++++++++------
contrib/postgres_fdw/sql/postgres_fdw.sql      | 13 ++++--
src/backend/executor/nodeAppend.c              | 11 +++++
4 files changed, 126 insertions(+), 18 deletions(-)



^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: pgsql: postgres_fdw: Fix handling of pending asynchronous requests.
  2021-07-30 08:08 pgsql: postgres_fdw: Fix handling of pending asynchronous requests. Etsuro Fujita <efujita@postgresql.org>
@ 2021-07-30 12:45 ` Michael Paquier <michael@paquier.xyz>
  2021-07-30 13:00   ` Re: pgsql: postgres_fdw: Fix handling of pending asynchronous requests. Etsuro Fujita <etsuro.fujita@gmail.com>
  0 siblings, 1 reply; 8+ messages in thread

From: Michael Paquier @ 2021-07-30 12:45 UTC (permalink / raw)
  To: Etsuro Fujita <efujita@postgresql.org>; +Cc: pgsql-committers@lists.postgresql.org

Hi Fujita-san,

On Fri, Jul 30, 2021 at 08:08:07AM +0000, Etsuro Fujita wrote:
> postgres_fdw: Fix handling of pending asynchronous requests.

You have angered many members of the buildfarm here, like:
https://buildfarm.postgresql.org/cgi-bin/show_log.pl?nm=mule&dt=2021-07-30%2011%3A30%3A04

This is crashing.
--
Michael

Attachments:

  signature.asc (application/pgp-signature, 832B) download

^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: pgsql: postgres_fdw: Fix handling of pending asynchronous requests.
  2021-07-30 08:08 pgsql: postgres_fdw: Fix handling of pending asynchronous requests. Etsuro Fujita <efujita@postgresql.org>
  2021-07-30 12:45 ` Re: pgsql: postgres_fdw: Fix handling of pending asynchronous requests. Michael Paquier <michael@paquier.xyz>
@ 2021-07-30 13:00   ` Etsuro Fujita <etsuro.fujita@gmail.com>
  2021-07-31 14:16     ` Re: pgsql: postgres_fdw: Fix handling of pending asynchronous requests. Etsuro Fujita <etsuro.fujita@gmail.com>
  0 siblings, 1 reply; 8+ messages in thread

From: Etsuro Fujita @ 2021-07-30 13:00 UTC (permalink / raw)
  To: Michael Paquier <michael@paquier.xyz>; +Cc: Etsuro Fujita <efujita@postgresql.org>; pgsql-committers <pgsql-committers@lists.postgresql.org>

Hi Michael-san,

On Fri, Jul 30, 2021 at 9:45 PM Michael Paquier <michael@paquier.xyz> wrote:
> On Fri, Jul 30, 2021 at 08:08:07AM +0000, Etsuro Fujita wrote:
> > postgres_fdw: Fix handling of pending asynchronous requests.
>
> You have angered many members of the buildfarm here, like:
> https://buildfarm.postgresql.org/cgi-bin/show_log.pl?nm=mule&dt=2021-07-30%2011%3A30%3A04
>
> This is crashing.

I’ll look into this.Thanks!

Best regards,
Etsuro Fujita





^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: pgsql: postgres_fdw: Fix handling of pending asynchronous requests.
  2021-07-30 08:08 pgsql: postgres_fdw: Fix handling of pending asynchronous requests. Etsuro Fujita <efujita@postgresql.org>
  2021-07-30 12:45 ` Re: pgsql: postgres_fdw: Fix handling of pending asynchronous requests. Michael Paquier <michael@paquier.xyz>
  2021-07-30 13:00   ` Re: pgsql: postgres_fdw: Fix handling of pending asynchronous requests. Etsuro Fujita <etsuro.fujita@gmail.com>
@ 2021-07-31 14:16     ` Etsuro Fujita <etsuro.fujita@gmail.com>
  2021-07-31 16:41       ` Re: pgsql: postgres_fdw: Fix handling of pending asynchronous requests. Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 8+ messages in thread

From: Etsuro Fujita @ 2021-07-31 14:16 UTC (permalink / raw)
  To: Michael Paquier <michael@paquier.xyz>; +Cc: Etsuro Fujita <efujita@postgresql.org>; pgsql-committers <pgsql-committers@lists.postgresql.org>

On Fri, Jul 30, 2021 at 10:00 PM Etsuro Fujita <etsuro.fujita@gmail.com> wrote:
> On Fri, Jul 30, 2021 at 9:45 PM Michael Paquier <michael@paquier.xyz> wrote:
> > On Fri, Jul 30, 2021 at 08:08:07AM +0000, Etsuro Fujita wrote:
> > > postgres_fdw: Fix handling of pending asynchronous requests.
> >
> > You have angered many members of the buildfarm here, like:

> I’ll look into this.

Buildfarm members are causing this assertion failure:

TRAP: FailedAssertion("fsstate->conn_state->pendingAreq == areq",
File: "/home/pgbf/buildroot/HEAD/pgsql.build/../pgsql/contrib/postgres_fdw/postgres_fdw.c",
Line: 6900, PID: 744110)

I couldn’t reproduce this in my environment, but I noticed this, which
didn’t happen in my environment: the case of delivering notifications
to both of the parent async-capable nodes in ExecAppendAsyncEventWait
when processing the added test case query.  In that case, doing
postgresForeignAsyncNotify for the first parent async-capable node
would invoke process_pending_request on the second one when executing
its initplan, which would lead to the assertion failure when doing
postgresForeignAsyncNotify for the second one.  :-(  I think
postgresForeignAsyncNotify would need the same treatment as for
postgresForeignAsyncConfigureWait, like the attached.

Best regards,
Etsuro Fujita

Attachments:

fix-postgresForeignAsyncNotify.patch (application/octet-stream, 2.6K) download diff --git a/contrib/postgres_fdw/postgres_fdw.c b/contrib/postgres_fdw/postgres_fdw.c index 8dfee2e02c..3e4a307880 100644 --- a/contrib/postgres_fdw/postgres_fdw.c +++ b/contrib/postgres_fdw/postgres_fdw.c @@ -6896,12 +6896,26 @@ postgresForeignAsyncNotify(AsyncRequest *areq) ForeignScanState *node = (ForeignScanState *) areq->requestee; PgFdwScanState *fsstate = (PgFdwScanState *) node->fdw_state; - /* The request should be currently in-process */ - Assert(fsstate->conn_state->pendingAreq == areq); - /* The core code would have initialized the callback_pending flag */ Assert(!areq->callback_pending); + /* + * If process_pending_request() has been invoked on the given request + * before we get here, we might have some tuples already; in which case + * produce the next tuple + */ + if (fsstate->next_tuple < fsstate->num_tuples) + { + produce_tuple_asynchronously(areq, true); + return; + } + + /* We must have run out of tuples */ + Assert(fsstate->next_tuple >= fsstate->num_tuples); + + /* The request should be currently in-process */ + Assert(fsstate->conn_state->pendingAreq == areq); + /* On error, report the original query, not the FETCH. */ if (!PQconsumeInput(fsstate->conn)) pgfdw_report_error(ERROR, NULL, fsstate->conn, false, fsstate->query); @@ -7027,7 +7041,7 @@ process_pending_request(AsyncRequest *areq) /* * If we didn't get any tuples, must be end of data; complete the request * now. Otherwise, we postpone completing the request until we are called - * from postgresForeignAsyncConfigureWait(). + * from postgresForeignAsyncConfigureWait()/postgresForeignAsyncNotify(). */ if (fsstate->next_tuple >= fsstate->num_tuples) { diff --git a/src/backend/executor/nodeAppend.c b/src/backend/executor/nodeAppend.c index a4eef19d7f..6a2daa6e76 100644 --- a/src/backend/executor/nodeAppend.c +++ b/src/backend/executor/nodeAppend.c @@ -1082,16 +1082,18 @@ ExecAppendAsyncEventWait(AppendState *node) { AsyncRequest *areq = (AsyncRequest *) w->user_data; - /* - * Mark it as no longer needing a callback. We must do this - * before dispatching the callback in case the callback resets the - * flag. - */ - Assert(areq->callback_pending); - areq->callback_pending = false; - - /* Do the actual work. */ - ExecAsyncNotify(areq); + if (areq->callback_pending) + { + /* + * Mark it as no longer needing a callback. We must do this + * before dispatching the callback in case the callback resets + * the flag. + */ + areq->callback_pending = false; + + /* Do the actual work. */ + ExecAsyncNotify(areq); + } } } }
^ permalink raw reply [nested|flat] 8+ messages in thread

* Re: pgsql: postgres_fdw: Fix handling of pending asynchronous requests.
  2021-07-30 08:08 pgsql: postgres_fdw: Fix handling of pending asynchronous requests. Etsuro Fujita <efujita@postgresql.org>
  2021-07-30 12:45 ` Re: pgsql: postgres_fdw: Fix handling of pending asynchronous requests. Michael Paquier <michael@paquier.xyz>
  2021-07-30 13:00   ` Re: pgsql: postgres_fdw: Fix handling of pending asynchronous requests. Etsuro Fujita <etsuro.fujita@gmail.com>
  2021-07-31 14:16     ` Re: pgsql: postgres_fdw: Fix handling of pending asynchronous requests. Etsuro Fujita <etsuro.fujita@gmail.com>
@ 2021-07-31 16:41       ` Tom Lane <tgl@sss.pgh.pa.us>
  2021-08-01 07:59         ` Re: pgsql: postgres_fdw: Fix handling of pending asynchronous requests. Etsuro Fujita <etsuro.fujita@gmail.com>
  0 siblings, 1 reply; 8+ messages in thread

From: Tom Lane @ 2021-07-31 16:41 UTC (permalink / raw)
  To: Etsuro Fujita <etsuro.fujita@gmail.com>; +Cc: Michael Paquier <michael@paquier.xyz>; Etsuro Fujita <efujita@postgresql.org>; pgsql-committers <pgsql-committers@lists.postgresql.org>

Etsuro Fujita <etsuro.fujita@gmail.com> writes:
> I couldn’t reproduce this in my environment, but I noticed this, which
> didn’t happen in my environment: the case of delivering notifications
> to both of the parent async-capable nodes in ExecAppendAsyncEventWait
> when processing the added test case query.  In that case, doing
> postgresForeignAsyncNotify for the first parent async-capable node
> would invoke process_pending_request on the second one when executing
> its initplan, which would lead to the assertion failure when doing
> postgresForeignAsyncNotify for the second one.  :-(  I think
> postgresForeignAsyncNotify would need the same treatment as for
> postgresForeignAsyncConfigureWait, like the attached.

I tried this patch on a spare machine that seems prone to showing
the failure: on HEAD, postgres_fdw's installcheck failed 3 times
in 4 attempts.  With the patch, it's gotten through 23 consecutive
tries without a failure.  So this is at least way better ...

			regards, tom lane





^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: pgsql: postgres_fdw: Fix handling of pending asynchronous requests.
  2021-07-30 08:08 pgsql: postgres_fdw: Fix handling of pending asynchronous requests. Etsuro Fujita <efujita@postgresql.org>
  2021-07-30 12:45 ` Re: pgsql: postgres_fdw: Fix handling of pending asynchronous requests. Michael Paquier <michael@paquier.xyz>
  2021-07-30 13:00   ` Re: pgsql: postgres_fdw: Fix handling of pending asynchronous requests. Etsuro Fujita <etsuro.fujita@gmail.com>
  2021-07-31 14:16     ` Re: pgsql: postgres_fdw: Fix handling of pending asynchronous requests. Etsuro Fujita <etsuro.fujita@gmail.com>
  2021-07-31 16:41       ` Re: pgsql: postgres_fdw: Fix handling of pending asynchronous requests. Tom Lane <tgl@sss.pgh.pa.us>
@ 2021-08-01 07:59         ` Etsuro Fujita <etsuro.fujita@gmail.com>
  2021-08-02 03:55           ` Re: pgsql: postgres_fdw: Fix handling of pending asynchronous requests. Etsuro Fujita <etsuro.fujita@gmail.com>
  0 siblings, 1 reply; 8+ messages in thread

From: Etsuro Fujita @ 2021-08-01 07:59 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Michael Paquier <michael@paquier.xyz>; Etsuro Fujita <efujita@postgresql.org>; pgsql-committers <pgsql-committers@lists.postgresql.org>

On Sun, Aug 1, 2021 at 1:41 AM Tom Lane <tgl@sss.pgh.pa.us> wrote:
> I tried this patch on a spare machine that seems prone to showing
> the failure: on HEAD, postgres_fdw's installcheck failed 3 times
> in 4 attempts.  With the patch, it's gotten through 23 consecutive
> tries without a failure.  So this is at least way better ...

I retried to reproduce the failure in my environment but I couldn’t.
But I plan on applying the patch tomorrow.

Thanks for the testing!

Best regards,
Etsuro Fujita





^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: pgsql: postgres_fdw: Fix handling of pending asynchronous requests.
  2021-07-30 08:08 pgsql: postgres_fdw: Fix handling of pending asynchronous requests. Etsuro Fujita <efujita@postgresql.org>
  2021-07-30 12:45 ` Re: pgsql: postgres_fdw: Fix handling of pending asynchronous requests. Michael Paquier <michael@paquier.xyz>
  2021-07-30 13:00   ` Re: pgsql: postgres_fdw: Fix handling of pending asynchronous requests. Etsuro Fujita <etsuro.fujita@gmail.com>
  2021-07-31 14:16     ` Re: pgsql: postgres_fdw: Fix handling of pending asynchronous requests. Etsuro Fujita <etsuro.fujita@gmail.com>
  2021-07-31 16:41       ` Re: pgsql: postgres_fdw: Fix handling of pending asynchronous requests. Tom Lane <tgl@sss.pgh.pa.us>
  2021-08-01 07:59         ` Re: pgsql: postgres_fdw: Fix handling of pending asynchronous requests. Etsuro Fujita <etsuro.fujita@gmail.com>
@ 2021-08-02 03:55           ` Etsuro Fujita <etsuro.fujita@gmail.com>
  0 siblings, 0 replies; 8+ messages in thread

From: Etsuro Fujita @ 2021-08-02 03:55 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Michael Paquier <michael@paquier.xyz>; Etsuro Fujita <efujita@postgresql.org>; pgsql-committers <pgsql-committers@lists.postgresql.org>

On Sun, Aug 1, 2021 at 4:59 PM Etsuro Fujita <etsuro.fujita@gmail.com> wrote:
> But I plan on applying the patch tomorrow.

Done, in hopes of making the buildfarm back to green.

Best regards,
Etsuro Fujita





^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* pgsql: postgres_fdw: Fix handling of pending asynchronous requests.
@ 2021-07-30 08:08 Etsuro Fujita <efujita@postgresql.org>
  0 siblings, 0 replies; 8+ messages in thread

From: Etsuro Fujita @ 2021-07-30 08:08 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

postgres_fdw: Fix handling of pending asynchronous requests.

A pending asynchronous request is handled by process_pending_request(),
which previously not only processed an in-progress remote query but
performed ExecForeignScan() to produce a tuple to return to the local
server asynchronously from the result of the remote query.  But that led
to a server crash when executing a query or led to an "InstrStartNode
called twice in a row" or "InstrEndLoop called on running node" failure
when doing EXPLAIN ANALYZE of it, in cases where the plan tree for it
contained multiple async-capable nodes accessing the same
initplan/subplan that contained multiple async-capable nodes scanning
the same foreign tables as for the parent async-capable nodes, as
reported by Andrey Lepikhov.  The reason is that the second step in
process_pending_request() invoked when executing the initplan/subplan
for one of the parent async-capable nodes caused recursive execution of
the initplan/subplan for another of the parent async-capable nodes.

To fix, split process_pending_request() into the two steps and postpone
the second step until ForeignAsyncConfigureWait() is called for each of
the pending asynchronous requests.  Also, in ExecAppendAsyncEventWait()
we assumed that FDWs would register at least one wait event in a
WaitEventSet created there when they were called from
ForeignAsyncConfigureWait() in that function, but allow FDWs to register
zero wait events in the WaitEventSet; modify ExecAppendAsyncEventWait()
to just return in that case.

Oversight in commit 27e1f1456.  Back-patch to v14 where that commit went
in.

Andrey Lepikhov and Etsuro Fujita

Discussion: https://postgr.es/m/fe5eaa19-1704-e4a4-76ee-3b9d37ade399@postgrespro.ru

Branch
------
REL_14_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/1cf7fb376acd48710c1c2c5cd97d11cd82e71ae4

Modified Files
--------------
contrib/postgres_fdw/expected/postgres_fdw.out | 55 +++++++++++++++++++++-
contrib/postgres_fdw/postgres_fdw.c            | 65 ++++++++++++++++++++------
contrib/postgres_fdw/sql/postgres_fdw.sql      | 13 ++++--
src/backend/executor/nodeAppend.c              | 11 +++++
4 files changed, 126 insertions(+), 18 deletions(-)



^ permalink  raw  reply  [nested|flat] 8+ messages in thread


end of thread, other threads:[~2021-08-02 03:55 UTC | newest]

Thread overview: 8+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2021-07-30 08:08 pgsql: postgres_fdw: Fix handling of pending asynchronous requests. Etsuro Fujita <efujita@postgresql.org>
2021-07-30 12:45 ` Michael Paquier <michael@paquier.xyz>
2021-07-30 13:00   ` Etsuro Fujita <etsuro.fujita@gmail.com>
2021-07-31 14:16     ` Etsuro Fujita <etsuro.fujita@gmail.com>
2021-07-31 16:41       ` Tom Lane <tgl@sss.pgh.pa.us>
2021-08-01 07:59         ` Etsuro Fujita <etsuro.fujita@gmail.com>
2021-08-02 03:55           ` Etsuro Fujita <etsuro.fujita@gmail.com>
2021-07-30 08:08 pgsql: postgres_fdw: Fix handling of pending asynchronous requests. Etsuro Fujita <efujita@postgresql.org>

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