* plpgsql_create_econtext --- create an eval_econtext for the current function * * We may need to create a new shared_simple_eval_estate too, if there's not * one already for the current transaction. The EState will be cleaned up at * transaction end. Ditto for shared_simple_eval_resowner. */
| 8235 | * transaction end. Ditto for shared_simple_eval_resowner. |
| 8236 | */ |
| 8237 | static void |
| 8238 | plpgsql_create_econtext(PLpgSQL_execstate *estate) |
| 8239 | { |
| 8240 | SimpleEcontextStackEntry *entry; |
| 8241 | |
| 8242 | /* |
| 8243 | * Create an EState for evaluation of simple expressions, if there's not |
| 8244 | * one already in the current transaction. The EState is made a child of |
| 8245 | * TopTransactionContext so it will have the right lifespan. |
| 8246 | * |
| 8247 | * Note that this path is never taken when beginning a DO block; the |
| 8248 | * required EState was already made by plpgsql_inline_handler. However, |
| 8249 | * if the DO block executes COMMIT or ROLLBACK, then we'll come here and |
| 8250 | * make a shared EState to use for the rest of the DO block. That's OK; |
| 8251 | * see the comments for shared_simple_eval_estate. (Note also that a DO |
| 8252 | * block will continue to use its private cast hash table for the rest of |
| 8253 | * the block. That's okay for now, but it might cause problems someday.) |
| 8254 | */ |
| 8255 | if (estate->simple_eval_estate == NULL) |
| 8256 | { |
| 8257 | MemoryContext oldcontext; |
| 8258 | |
| 8259 | if (shared_simple_eval_estate == NULL) |
| 8260 | { |
| 8261 | oldcontext = MemoryContextSwitchTo(TopTransactionContext); |
| 8262 | shared_simple_eval_estate = CreateExecutorState(); |
| 8263 | MemoryContextSwitchTo(oldcontext); |
| 8264 | } |
| 8265 | estate->simple_eval_estate = shared_simple_eval_estate; |
| 8266 | } |
| 8267 | |
| 8268 | /* |
| 8269 | * Likewise for the simple-expression resource owner. |
| 8270 | */ |
| 8271 | if (estate->simple_eval_resowner == NULL) |
| 8272 | { |
| 8273 | if (shared_simple_eval_resowner == NULL) |
| 8274 | shared_simple_eval_resowner = |
| 8275 | ResourceOwnerCreate(TopTransactionResourceOwner, |
| 8276 | "PL/pgSQL simple expressions"); |
| 8277 | estate->simple_eval_resowner = shared_simple_eval_resowner; |
| 8278 | } |
| 8279 | |
| 8280 | /* |
| 8281 | * Create a child econtext for the current function. |
| 8282 | */ |
| 8283 | estate->eval_econtext = CreateExprContext(estate->simple_eval_estate); |
| 8284 | |
| 8285 | /* |
| 8286 | * Make a stack entry so we can clean up the econtext at subxact end. |
| 8287 | * Stack entries are kept in TopTransactionContext for simplicity. |
| 8288 | */ |
| 8289 | entry = (SimpleEcontextStackEntry *) |
| 8290 | MemoryContextAlloc(TopTransactionContext, |
| 8291 | sizeof(SimpleEcontextStackEntry)); |
| 8292 | |
| 8293 | entry->stack_econtext = estate->eval_econtext; |
| 8294 | entry->xact_subxid = GetCurrentSubTransactionId(); |
no test coverage detected