---------- * exec_simple_check_plan - Check if a plan is simple enough to * be evaluated by ExecEvalExpr() instead * of SPI. * * Note: the refcount manipulations in this function assume that expr->plan * is a "saved" SPI plan. That's a bit annoying from the caller's standpoint, * but it's otherwise difficult to avoid leaking the plan on failure. * ---------- */
| 7883 | * ---------- |
| 7884 | */ |
| 7885 | static void |
| 7886 | exec_simple_check_plan(PLpgSQL_execstate *estate, PLpgSQL_expr *expr) |
| 7887 | { |
| 7888 | List *plansources; |
| 7889 | CachedPlanSource *plansource; |
| 7890 | Query *query; |
| 7891 | CachedPlan *cplan; |
| 7892 | MemoryContext oldcontext; |
| 7893 | |
| 7894 | /* |
| 7895 | * Initialize to "not simple". |
| 7896 | */ |
| 7897 | expr->expr_simple_expr = NULL; |
| 7898 | expr->expr_rw_param = NULL; |
| 7899 | |
| 7900 | /* |
| 7901 | * Check the analyzed-and-rewritten form of the query to see if we will be |
| 7902 | * able to treat it as a simple expression. Since this function is only |
| 7903 | * called immediately after creating the CachedPlanSource, we need not |
| 7904 | * worry about the query being stale. |
| 7905 | */ |
| 7906 | |
| 7907 | /* |
| 7908 | * We can only test queries that resulted in exactly one CachedPlanSource |
| 7909 | */ |
| 7910 | plansources = SPI_plan_get_plan_sources(expr->plan); |
| 7911 | if (list_length(plansources) != 1) |
| 7912 | return; |
| 7913 | plansource = (CachedPlanSource *) linitial(plansources); |
| 7914 | |
| 7915 | /* |
| 7916 | * 1. There must be one single querytree. |
| 7917 | */ |
| 7918 | if (list_length(plansource->query_list) != 1) |
| 7919 | return; |
| 7920 | query = (Query *) linitial(plansource->query_list); |
| 7921 | |
| 7922 | /* |
| 7923 | * 2. It must be a plain SELECT query without any input tables |
| 7924 | */ |
| 7925 | if (!IsA(query, Query)) |
| 7926 | return; |
| 7927 | if (query->commandType != CMD_SELECT) |
| 7928 | return; |
| 7929 | if (query->rtable != NIL) |
| 7930 | return; |
| 7931 | |
| 7932 | /* |
| 7933 | * 3. Can't have any subplans, aggregates, qual clauses either. (These |
| 7934 | * tests should generally match what inline_function() checks before |
| 7935 | * inlining a SQL function; otherwise, inlining could change our |
| 7936 | * conclusion about whether an expression is simple, which we don't want.) |
| 7937 | */ |
| 7938 | if (query->hasAggs || |
| 7939 | query->hasWindowFuncs || |
| 7940 | query->hasTargetSRFs || |
| 7941 | query->hasSubLinks || |
| 7942 | query->cteList || |
no test coverage detected