mirror of
https://git.postgresql.org/git/postgresql.git
synced 2024-08-30 15:17:19 +02:00
Fix misevaluation of STABLE parameters in CALL within plpgsql.
Before commit84f5c2908
, a STABLE function in a plpgsql CALL statement's argument list would see an up-to-date snapshot, because exec_stmt_call would push a new snapshot. I got rid of that because the possibility of the snapshot disappearing within COMMIT made it too hard to manage a snapshot across the CALL statement. That's fine so far as the procedure itself goes, but I forgot to think about the possibility of STABLE functions within the CALL argument list. As things now stand, those'll be executed with the Portal's snapshot as ActiveSnapshot, keeping them from seeing updates more recent than Portal startup. (VOLATILE functions don't have a problem because they take their own snapshots; which indeed is also why the procedure itself doesn't have a problem. There are no STABLE procedures.) We can fix this by pushing a new snapshot transiently within ExecuteCallStmt itself. Popping the snapshot before we get into the procedure proper eliminates the management problem. The possibly-useless extra snapshot-grab is slightly annoying, but it's no worse than what happened before84f5c2908
. Per bug #17199 from Alexander Nawratil. Back-patch to v11, like the previous patch. Discussion: https://postgr.es/m/17199-1ab2561f0d94af92@postgresql.org
This commit is contained in:
parent
cd35d3909b
commit
e8b0bcae63
@ -69,6 +69,7 @@
|
|||||||
#include "utils/lsyscache.h"
|
#include "utils/lsyscache.h"
|
||||||
#include "utils/memutils.h"
|
#include "utils/memutils.h"
|
||||||
#include "utils/rel.h"
|
#include "utils/rel.h"
|
||||||
|
#include "utils/snapmgr.h"
|
||||||
#include "utils/syscache.h"
|
#include "utils/syscache.h"
|
||||||
#include "utils/typcache.h"
|
#include "utils/typcache.h"
|
||||||
|
|
||||||
@ -2390,6 +2391,16 @@ ExecuteCallStmt(CallStmt *stmt, ParamListInfo params, bool atomic, DestReceiver
|
|||||||
estate->es_param_list_info = params;
|
estate->es_param_list_info = params;
|
||||||
econtext = CreateExprContext(estate);
|
econtext = CreateExprContext(estate);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* If we're called in non-atomic context, we also have to ensure that the
|
||||||
|
* argument expressions run with an up-to-date snapshot. Our caller will
|
||||||
|
* have provided a current snapshot in atomic contexts, but not in
|
||||||
|
* non-atomic contexts, because the possibility of a COMMIT/ROLLBACK
|
||||||
|
* destroying the snapshot makes higher-level management too complicated.
|
||||||
|
*/
|
||||||
|
if (!atomic)
|
||||||
|
PushActiveSnapshot(GetTransactionSnapshot());
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
foreach(lc, fexpr->args)
|
foreach(lc, fexpr->args)
|
||||||
{
|
{
|
||||||
@ -2407,20 +2418,23 @@ ExecuteCallStmt(CallStmt *stmt, ParamListInfo params, bool atomic, DestReceiver
|
|||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Get rid of temporary snapshot for arguments, if we made one */
|
||||||
|
if (!atomic)
|
||||||
|
PopActiveSnapshot();
|
||||||
|
|
||||||
|
/* Here we actually call the procedure */
|
||||||
pgstat_init_function_usage(fcinfo, &fcusage);
|
pgstat_init_function_usage(fcinfo, &fcusage);
|
||||||
retval = FunctionCallInvoke(fcinfo);
|
retval = FunctionCallInvoke(fcinfo);
|
||||||
pgstat_end_function_usage(&fcusage, true);
|
pgstat_end_function_usage(&fcusage, true);
|
||||||
|
|
||||||
|
/* Handle the procedure's outputs */
|
||||||
if (fexpr->funcresulttype == VOIDOID)
|
if (fexpr->funcresulttype == VOIDOID)
|
||||||
{
|
{
|
||||||
/* do nothing */
|
/* do nothing */
|
||||||
}
|
}
|
||||||
else if (fexpr->funcresulttype == RECORDOID)
|
else if (fexpr->funcresulttype == RECORDOID)
|
||||||
{
|
{
|
||||||
/*
|
/* send tuple to client */
|
||||||
* send tuple to client
|
|
||||||
*/
|
|
||||||
|
|
||||||
HeapTupleHeader td;
|
HeapTupleHeader td;
|
||||||
Oid tupType;
|
Oid tupType;
|
||||||
int32 tupTypmod;
|
int32 tupTypmod;
|
||||||
|
@ -600,6 +600,27 @@ SELECT * FROM test2;
|
|||||||
42
|
42
|
||||||
(1 row)
|
(1 row)
|
||||||
|
|
||||||
|
-- another snapshot handling case: argument expressions of a CALL need
|
||||||
|
-- to be evaluated with an up-to-date snapshot
|
||||||
|
CREATE FUNCTION report_count() RETURNS bigint
|
||||||
|
STABLE LANGUAGE sql
|
||||||
|
AS $$ SELECT COUNT(*) FROM test2 $$;
|
||||||
|
CREATE PROCEDURE transaction_test9b(cnt bigint)
|
||||||
|
LANGUAGE plpgsql
|
||||||
|
AS $$
|
||||||
|
BEGIN
|
||||||
|
RAISE NOTICE 'count = %', cnt;
|
||||||
|
END
|
||||||
|
$$;
|
||||||
|
DO $$
|
||||||
|
BEGIN
|
||||||
|
CALL transaction_test9b(report_count());
|
||||||
|
INSERT INTO test2 VALUES(43);
|
||||||
|
CALL transaction_test9b(report_count());
|
||||||
|
END
|
||||||
|
$$;
|
||||||
|
NOTICE: count = 1
|
||||||
|
NOTICE: count = 2
|
||||||
-- Test transaction in procedure with output parameters. This uses a
|
-- Test transaction in procedure with output parameters. This uses a
|
||||||
-- different portal strategy and different code paths in pquery.c.
|
-- different portal strategy and different code paths in pquery.c.
|
||||||
CREATE PROCEDURE transaction_test10a(INOUT x int)
|
CREATE PROCEDURE transaction_test10a(INOUT x int)
|
||||||
|
@ -507,6 +507,29 @@ $$;
|
|||||||
SELECT * FROM test2;
|
SELECT * FROM test2;
|
||||||
|
|
||||||
|
|
||||||
|
-- another snapshot handling case: argument expressions of a CALL need
|
||||||
|
-- to be evaluated with an up-to-date snapshot
|
||||||
|
CREATE FUNCTION report_count() RETURNS bigint
|
||||||
|
STABLE LANGUAGE sql
|
||||||
|
AS $$ SELECT COUNT(*) FROM test2 $$;
|
||||||
|
|
||||||
|
CREATE PROCEDURE transaction_test9b(cnt bigint)
|
||||||
|
LANGUAGE plpgsql
|
||||||
|
AS $$
|
||||||
|
BEGIN
|
||||||
|
RAISE NOTICE 'count = %', cnt;
|
||||||
|
END
|
||||||
|
$$;
|
||||||
|
|
||||||
|
DO $$
|
||||||
|
BEGIN
|
||||||
|
CALL transaction_test9b(report_count());
|
||||||
|
INSERT INTO test2 VALUES(43);
|
||||||
|
CALL transaction_test9b(report_count());
|
||||||
|
END
|
||||||
|
$$;
|
||||||
|
|
||||||
|
|
||||||
-- Test transaction in procedure with output parameters. This uses a
|
-- Test transaction in procedure with output parameters. This uses a
|
||||||
-- different portal strategy and different code paths in pquery.c.
|
-- different portal strategy and different code paths in pquery.c.
|
||||||
CREATE PROCEDURE transaction_test10a(INOUT x int)
|
CREATE PROCEDURE transaction_test10a(INOUT x int)
|
||||||
|
Loading…
Reference in New Issue
Block a user