Skip to content

Commit d2e17e1

Browse files
committed
Fix mishandling of after-trigger state when a SQL function returns multiple
rows --- if the surrounding query queued any trigger events between the rows, the events would be fired at the wrong time, leading to bizarre behavior. Per report from Merlin Moncure. This is a simple patch that should solve the problem fully in the back branches, but in HEAD we also need to consider the possibility of queries with RETURNING clauses. Will look into a fix for that separately.
1 parent b38900c commit d2e17e1

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

src/backend/executor/functions.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/executor/functions.c,v 1.107 2006/10/04 00:29:52 momjian Exp $
11+
* $PostgreSQL: pgsql/src/backend/executor/functions.c,v 1.108 2006/10/12 17:02:24 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -327,7 +327,14 @@ postquel_start(execution_state *es, SQLFunctionCachePtr fcache)
327327
/* Utility commands don't need Executor. */
328328
if (es->qd->operation != CMD_UTILITY)
329329
{
330-
AfterTriggerBeginQuery();
330+
/*
331+
* Only set up to collect queued triggers if it's not a SELECT.
332+
* This isn't just an optimization, but is necessary in case a SELECT
333+
* returns multiple rows to caller --- we mustn't exit from the
334+
* function execution with a stacked AfterTrigger level still active.
335+
*/
336+
if (es->qd->operation != CMD_SELECT)
337+
AfterTriggerBeginQuery();
331338
ExecutorStart(es->qd, 0);
332339
}
333340

@@ -401,7 +408,8 @@ postquel_end(execution_state *es)
401408
{
402409
ActiveSnapshot = es->qd->snapshot;
403410

404-
AfterTriggerEndQuery(es->qd->estate);
411+
if (es->qd->operation != CMD_SELECT)
412+
AfterTriggerEndQuery(es->qd->estate);
405413
ExecutorEnd(es->qd);
406414
}
407415
PG_CATCH();

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy