Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1UCHEO-0002pK-NX for pgsql-bugs@arkaria.postgresql.org; Sun, 03 Mar 2013 22:18:08 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1UCHEO-0005qE-84 for pgsql-bugs@arkaria.postgresql.org; Sun, 03 Mar 2013 22:18:08 +0000 Received: from makus.postgresql.org ([2001:4800:7903:4::125]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1UCHEM-0005q9-KZ for pgsql-bugs@postgresql.org; Sun, 03 Mar 2013 22:18:06 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1UCHEJ-0008W3-CU for pgsql-bugs@postgresql.org; Sun, 03 Mar 2013 22:18:06 +0000 Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.5/8.14.5) with ESMTP id r23MI2DJ013305; Sun, 3 Mar 2013 17:18:02 -0500 (EST) From: Tom Lane To: andrew@tao11.riddles.org.uk cc: pgsql-bugs@postgresql.org Subject: Re: BUG #7881: SQL function failures in long-lived calling contexts In-reply-to: <22648.1362338864@sss.pgh.pa.us> References: <22648.1362338864@sss.pgh.pa.us> Comments: In-reply-to Tom Lane message dated "Sun, 03 Mar 2013 14:27:44 -0500" MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" Content-ID: <13282.1362349044.0@sss.pgh.pa.us> Date: Sun, 03 Mar 2013 17:18:02 -0500 Message-ID: <13304.1362349082@sss.pgh.pa.us> X-Pg-Spam-Score: -2.5 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org ------- =_aaaaaaaaaa0 Content-Type: text/plain; charset="us-ascii" Content-ID: <13282.1362349044.1@sss.pgh.pa.us> I wrote: > Or we could add fields recording the current transaction/subtransaction > IDs, then throw away and regenerate the function cache entry if that > subxact is no longer active. This would be a bit more invasive/riskier > than throwing a "not supported" error, but it would preserve the > functionality. The attached patch seems fairly reasonable for this. regards, tom lane ------- =_aaaaaaaaaa0 Content-Type: text/x-patch; charset="us-ascii" Content-ID: <13282.1362349044.2@sss.pgh.pa.us> Content-Description: sql-function-cache-fix.patch Content-Disposition: attachment; filename="sql-function-cache-fix.patch" Content-Transfer-Encoding: quoted-printable diff --git a/src/backend/access/transam/xact.c b/src/backend/access/transam= /xact.c index 9a9724574bec2874710316fa74643ae24d2d305e..e62286f9f98eccfd9a30e2e8f49= 08e8757b48672 100644 *** a/src/backend/access/transam/xact.c --- b/src/backend/access/transam/xact.c *************** GetCurrentSubTransactionId(void) *** 570,575 **** --- 570,596 ---- return s->subTransactionId; } =20=20 + /* + * SubTransactionIsActive + * + * Test if the specified subxact ID is still active. Note caller is + * responsible for checking whether this ID is relevant to the current xa= ct. + */ + bool + SubTransactionIsActive(SubTransactionId subxid) + { + TransactionState s; +=20 + for (s =3D CurrentTransactionState; s !=3D NULL; s =3D s->parent) + { + if (s->state =3D=3D TRANS_ABORT) + continue; + if (s->subTransactionId =3D=3D subxid) + return true; + } + return false; + } +=20 =20=20 /* * GetCurrentCommandId diff --git a/src/backend/executor/functions.c b/src/backend/executor/functi= ons.c index 2782b55e876078cda87bc9dac6b980340be0298d..c908f34cfe4bdd7fb0e059679d8= 307b2f75c8b23 100644 *** a/src/backend/executor/functions.c --- b/src/backend/executor/functions.c *************** *** 25,34 **** --- 25,36 ---- #include "nodes/nodeFuncs.h" #include "parser/parse_coerce.h" #include "parser/parse_func.h" + #include "storage/proc.h" #include "tcop/utility.h" #include "utils/builtins.h" #include "utils/datum.h" #include "utils/lsyscache.h" + #include "utils/memutils.h" #include "utils/snapmgr.h" #include "utils/syscache.h" =20=20 *************** typedef struct execution_state *** 74,81 **** * and linked to from the fn_extra field of the FmgrInfo struct. * * Note that currently this has only the lifespan of the calling query. ! * Someday we might want to consider caching the parse/plan results longer ! * than that. */ typedef struct { --- 76,92 ---- * and linked to from the fn_extra field of the FmgrInfo struct. * * Note that currently this has only the lifespan of the calling query. ! * Someday we should rewrite this code to use plancache.c to save parse/p= lan ! * results for longer than that. ! * ! * Physically, though, the data has the lifespan of the FmgrInfo that's u= sed ! * to call the function, and there are cases (particularly with indexes) ! * where the FmgrInfo might survive across transactions. We cannot assume ! * that the parse/plan trees are good for longer than the (sub)transactio= n in ! * which parsing was done, so we must mark the record with the LXID/subxi= d of ! * its creation time, and regenerate everything if that's obsolete. To a= void ! * memory leakage when we do have to regenerate things, all the data is k= ept ! * in a sub-context of the FmgrInfo's fn_mcxt. */ typedef struct { *************** typedef struct *** 106,111 **** --- 117,128 ---- * track of where the original query boundaries are. */ List *func_state; +=20 + MemoryContext fcontext; /* memory context holding this struct and all + * subsidiary data */ +=20 + LocalTransactionId lxid; /* lxid in which cache was made */ + SubTransactionId subxid; /* subxid in which cache was made */ } SQLFunctionCache; =20=20 typedef SQLFunctionCache *SQLFunctionCachePtr; *************** static void *** 551,556 **** --- 568,575 ---- init_sql_fcache(FmgrInfo *finfo, Oid collation, bool lazyEvalOK) { Oid foid =3D finfo->fn_oid; + MemoryContext fcontext; + MemoryContext oldcontext; Oid rettype; HeapTuple procedureTuple; Form_pg_proc procedureStruct; *************** init_sql_fcache(FmgrInfo *finfo, Oid col *** 562,568 **** --- 581,605 ---- Datum tmp; bool isNull; =20=20 + /* + * Create memory context that holds all the SQLFunctionCache data. It + * must be a child of whatever context holds the FmgrInfo. + */ + fcontext =3D AllocSetContextCreate(finfo->fn_mcxt, + "SQL function data", + ALLOCSET_DEFAULT_MINSIZE, + ALLOCSET_DEFAULT_INITSIZE, + ALLOCSET_DEFAULT_MAXSIZE); +=20 + oldcontext =3D MemoryContextSwitchTo(fcontext); +=20 + /* + * Create the struct proper, link it to fcontext and fn_extra. Once this + * is done, we'll be able to recover the memory after failure, even if t= he + * FmgrInfo is long-lived. + */ fcache =3D (SQLFunctionCachePtr) palloc0(sizeof(SQLFunctionCache)); + fcache->fcontext =3D fcontext; finfo->fn_extra =3D (void *) fcache; =20=20 /* *************** init_sql_fcache(FmgrInfo *finfo, Oid col *** 635,640 **** --- 672,682 ---- * sublist, for example if the last query rewrites to DO INSTEAD NOTHING. * (It might not be unreasonable to throw an error in such a case, but * this is the historical behavior and it doesn't seem worth changing.) + * + * Note: since parsing and planning is done in fcontext, we will generate + * a lot of cruft that lives as long as the fcache does. This is annoyi= ng + * but we'll not worry about it until the module is rewritten to use + * plancache.c. */ raw_parsetree_list =3D pg_parse_query(fcache->src); =20=20 *************** init_sql_fcache(FmgrInfo *finfo, Oid col *** 700,706 **** --- 742,754 ---- fcache, lazyEvalOK); =20=20 + /* Mark fcache with time of creation to show it's valid */ + fcache->lxid =3D MyProc->lxid; + fcache->subxid =3D GetCurrentSubTransactionId(); +=20 ReleaseSysCache(procedureTuple); +=20 + MemoryContextSwitchTo(oldcontext); } =20=20 /* Start up execution of one execution_state node */ *************** postquel_get_single_result(TupleTableSlo *** 923,931 **** Datum fmgr_sql(PG_FUNCTION_ARGS) { - MemoryContext oldcontext; SQLFunctionCachePtr fcache; ErrorContextCallback sqlerrcontext; bool randomAccess; bool lazyEvalOK; bool is_first; --- 971,979 ---- Datum fmgr_sql(PG_FUNCTION_ARGS) { SQLFunctionCachePtr fcache; ErrorContextCallback sqlerrcontext; + MemoryContext oldcontext; bool randomAccess; bool lazyEvalOK; bool is_first; *************** fmgr_sql(PG_FUNCTION_ARGS) *** 937,949 **** ListCell *eslc; =20=20 /* - * Switch to context in which the fcache lives. This ensures that - * parsetrees, plans, etc, will have sufficient lifetime. The - * sub-executor is responsible for deleting per-tuple information. - */ - oldcontext =3D MemoryContextSwitchTo(fcinfo->flinfo->fn_mcxt); -=20 - /* * Setup error traceback support for ereport() */ sqlerrcontext.callback =3D sql_exec_error_callback; --- 985,990 ---- *************** fmgr_sql(PG_FUNCTION_ARGS) *** 978,997 **** } =20=20 /* ! * Initialize fcache (build plans) if first time through. */ fcache =3D (SQLFunctionCachePtr) fcinfo->flinfo->fn_extra; if (fcache =3D=3D NULL) { init_sql_fcache(fcinfo->flinfo, PG_GET_COLLATION(), lazyEvalOK); fcache =3D (SQLFunctionCachePtr) fcinfo->flinfo->fn_extra; } ! eslist =3D fcache->func_state; =20=20 /* * Find first unfinished query in function, and note whether it's the * first query. */ es =3D NULL; is_first =3D true; foreach(eslc, eslist) --- 1019,1061 ---- } =20=20 /* ! * Initialize fcache (build plans) if first time through; or re-initiali= ze ! * if the cache is stale. */ fcache =3D (SQLFunctionCachePtr) fcinfo->flinfo->fn_extra; +=20 + if (fcache !=3D NULL) + { + if (fcache->lxid !=3D MyProc->lxid || + !SubTransactionIsActive(fcache->subxid)) + { + /* It's stale; unlink and delete */ + fcinfo->flinfo->fn_extra =3D NULL; + MemoryContextDelete(fcache->fcontext); + fcache =3D NULL; + } + } +=20 if (fcache =3D=3D NULL) { init_sql_fcache(fcinfo->flinfo, PG_GET_COLLATION(), lazyEvalOK); fcache =3D (SQLFunctionCachePtr) fcinfo->flinfo->fn_extra; } !=20 ! /* ! * Switch to context in which the fcache lives. This ensures that our ! * tuplestore etc will have sufficient lifetime. The sub-executor is ! * responsible for deleting per-tuple information. (XXX in the case of a ! * long-lived FmgrInfo, this policy represents more memory leakage, but ! * it's not entirely clear where to keep stuff instead.) ! */ ! oldcontext =3D MemoryContextSwitchTo(fcache->fcontext); =20=20 /* * Find first unfinished query in function, and note whether it's the * first query. */ + eslist =3D fcache->func_state; es =3D NULL; is_first =3D true; foreach(eslc, eslist) diff --git a/src/include/access/xact.h b/src/include/access/xact.h index 09e6a6842c2039dc2b7c99cad74a6e83a6d9e9e6..835f6acbee0e10ee51e5a229542= 9efd5141e3b77 100644 *** a/src/include/access/xact.h --- b/src/include/access/xact.h *************** extern TransactionId GetCurrentTransacti *** 215,220 **** --- 215,221 ---- extern TransactionId GetCurrentTransactionIdIfAny(void); extern TransactionId GetStableLatestTransactionId(void); extern SubTransactionId GetCurrentSubTransactionId(void); + extern bool SubTransactionIsActive(SubTransactionId subxid); extern CommandId GetCurrentCommandId(bool used); extern TimestampTz GetCurrentTransactionStartTimestamp(void); extern TimestampTz GetCurrentStatementStartTimestamp(void); ------- =_aaaaaaaaaa0 Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs ------- =_aaaaaaaaaa0--