mirror of
https://github.com/postgres/postgres.git
synced 2025-10-16 17:07:43 +03:00
Fix re-execution of a failed SQLFunctionCache entry.
If we error out during execution of a SQL-language function, we will often leave behind non-null pointers in its SQLFunctionCache's cplan and eslist fields. This is problematic if the SQLFunctionCache is re-used, because those pointers will point at resources that were released during error cleanup. This problem escaped detection so far because ordinarily we won't re-use an FmgrInfo+SQLFunctionCache struct after a query error. However, in the rather improbable case that someone implements an opclass support function in SQL language, there will be long-lived FmgrInfos for it in the relcache, and then the problem is reachable after the function throws an error. To fix, add a flag to SQLFunctionCache that tracks whether execution escapes out of fmgr_sql, and clear out the relevant fields during init_sql_fcache if so. (This is going to need more thought if we ever try to share FMgrInfos across threads; but it's very far from being the only problem such a project will encounter, since many functions regard fn_extra as being query-local state.) This broke at commit 0313c5dc6; before that we did not try to re-use SQLFunctionCache state across calls. Hence, back-patch to v18. Bug: #19026 Reported-by: Alexander Lakhin <exclusion@gmail.com> Author: Tom Lane <tgl@sss.pgh.pa.us> Discussion: https://postgr.es/m/19026-90aed5e71d0c8af3@postgresql.org Backpatch-through: 18
This commit is contained in:
@@ -143,6 +143,7 @@ typedef struct SQLFunctionCache
|
||||
{
|
||||
SQLFunctionHashEntry *func; /* associated SQLFunctionHashEntry */
|
||||
|
||||
bool active; /* are we executing this cache entry? */
|
||||
bool lazyEvalOK; /* true if lazyEval is safe */
|
||||
bool shutdown_reg; /* true if registered shutdown callback */
|
||||
bool lazyEval; /* true if using lazyEval for result query */
|
||||
@@ -556,6 +557,28 @@ init_sql_fcache(FunctionCallInfo fcinfo, bool lazyEvalOK)
|
||||
finfo->fn_extra = fcache;
|
||||
}
|
||||
|
||||
/*
|
||||
* If the SQLFunctionCache is marked as active, we must have errored out
|
||||
* of a prior execution. Reset state. (It might seem that we could also
|
||||
* reach this during recursive invocation of a SQL function, but we won't
|
||||
* because that case won't involve re-use of the same FmgrInfo.)
|
||||
*/
|
||||
if (fcache->active)
|
||||
{
|
||||
/*
|
||||
* In general, this stanza should clear all the same fields that
|
||||
* ShutdownSQLFunction would. Note we must clear fcache->cplan
|
||||
* without doing ReleaseCachedPlan, because error cleanup from the
|
||||
* prior execution would have taken care of releasing that plan.
|
||||
* Likewise, if tstore is still set then it is pointing at garbage.
|
||||
*/
|
||||
fcache->cplan = NULL;
|
||||
fcache->eslist = NULL;
|
||||
fcache->tstore = NULL;
|
||||
fcache->shutdown_reg = false;
|
||||
fcache->active = false;
|
||||
}
|
||||
|
||||
/*
|
||||
* If we are resuming execution of a set-returning function, just keep
|
||||
* using the same cache. We do not ask funccache.c to re-validate the
|
||||
@@ -1597,6 +1620,9 @@ fmgr_sql(PG_FUNCTION_ARGS)
|
||||
*/
|
||||
fcache = init_sql_fcache(fcinfo, lazyEvalOK);
|
||||
|
||||
/* Mark fcache as active */
|
||||
fcache->active = true;
|
||||
|
||||
/* Remember info that we might need later to construct tuplestore */
|
||||
fcache->tscontext = tscontext;
|
||||
fcache->randomAccess = randomAccess;
|
||||
@@ -1853,6 +1879,9 @@ fmgr_sql(PG_FUNCTION_ARGS)
|
||||
if (es == NULL)
|
||||
fcache->eslist = NULL;
|
||||
|
||||
/* Mark fcache as inactive */
|
||||
fcache->active = false;
|
||||
|
||||
error_context_stack = sqlerrcontext.previous;
|
||||
|
||||
return result;
|
||||
|
@@ -733,6 +733,22 @@ SELECT double_append(array_append(ARRAY[q1], q2), q3)
|
||||
{4,5,6,4,5,6}
|
||||
(2 rows)
|
||||
|
||||
-- Check that we can re-use a SQLFunctionCache after a run-time error.
|
||||
-- This function will fail with zero-divide at run time (not plan time).
|
||||
CREATE FUNCTION part_hashint4_error(value int4, seed int8) RETURNS int8
|
||||
LANGUAGE SQL STRICT IMMUTABLE PARALLEL SAFE AS
|
||||
$$ SELECT value + seed + random()::int/0 $$;
|
||||
-- Put it into an operator class so that FmgrInfo will be cached in relcache.
|
||||
CREATE OPERATOR CLASS part_test_int4_ops_bad FOR TYPE int4 USING hash AS
|
||||
FUNCTION 2 part_hashint4_error(int4, int8);
|
||||
CREATE TABLE pt(i int) PARTITION BY hash (i part_test_int4_ops_bad);
|
||||
CREATE TABLE p1 PARTITION OF pt FOR VALUES WITH (modulus 4, remainder 0);
|
||||
INSERT INTO pt VALUES (1);
|
||||
ERROR: division by zero
|
||||
CONTEXT: SQL function "part_hashint4_error" statement 1
|
||||
INSERT INTO pt VALUES (1);
|
||||
ERROR: division by zero
|
||||
CONTEXT: SQL function "part_hashint4_error" statement 1
|
||||
-- Things that shouldn't work:
|
||||
CREATE FUNCTION test1 (int) RETURNS int LANGUAGE SQL
|
||||
AS 'SELECT ''not an integer'';';
|
||||
@@ -773,7 +789,7 @@ CONTEXT: SQL function "test1" during startup
|
||||
RESET check_function_bodies;
|
||||
-- Cleanup
|
||||
DROP SCHEMA temp_func_test CASCADE;
|
||||
NOTICE: drop cascades to 35 other objects
|
||||
NOTICE: drop cascades to 38 other objects
|
||||
DETAIL: drop cascades to function functest_a_1(text,date)
|
||||
drop cascades to function functest_a_2(text[])
|
||||
drop cascades to function functest_a_3()
|
||||
@@ -808,6 +824,9 @@ drop cascades to function create_and_insert()
|
||||
drop cascades to table ddl_test
|
||||
drop cascades to function alter_and_insert()
|
||||
drop cascades to function double_append(anyarray,anyelement)
|
||||
drop cascades to function part_hashint4_error(integer,bigint)
|
||||
drop cascades to operator family part_test_int4_ops_bad for access method hash
|
||||
drop cascades to table pt
|
||||
drop cascades to function test1(anyelement)
|
||||
DROP USER regress_unpriv_user;
|
||||
RESET search_path;
|
||||
|
@@ -432,6 +432,23 @@ $$ SELECT array_append($1, $2) || array_append($1, $2) $$;
|
||||
SELECT double_append(array_append(ARRAY[q1], q2), q3)
|
||||
FROM (VALUES(1,2,3), (4,5,6)) v(q1,q2,q3);
|
||||
|
||||
-- Check that we can re-use a SQLFunctionCache after a run-time error.
|
||||
|
||||
-- This function will fail with zero-divide at run time (not plan time).
|
||||
CREATE FUNCTION part_hashint4_error(value int4, seed int8) RETURNS int8
|
||||
LANGUAGE SQL STRICT IMMUTABLE PARALLEL SAFE AS
|
||||
$$ SELECT value + seed + random()::int/0 $$;
|
||||
|
||||
-- Put it into an operator class so that FmgrInfo will be cached in relcache.
|
||||
CREATE OPERATOR CLASS part_test_int4_ops_bad FOR TYPE int4 USING hash AS
|
||||
FUNCTION 2 part_hashint4_error(int4, int8);
|
||||
|
||||
CREATE TABLE pt(i int) PARTITION BY hash (i part_test_int4_ops_bad);
|
||||
CREATE TABLE p1 PARTITION OF pt FOR VALUES WITH (modulus 4, remainder 0);
|
||||
|
||||
INSERT INTO pt VALUES (1);
|
||||
INSERT INTO pt VALUES (1);
|
||||
|
||||
-- Things that shouldn't work:
|
||||
|
||||
CREATE FUNCTION test1 (int) RETURNS int LANGUAGE SQL
|
||||
|
Reference in New Issue
Block a user