|
| 1 | +/* |
| 2 | + * pg_execplan.c |
| 3 | + * |
| 4 | + */ |
| 5 | + |
| 6 | +#include "postgres.h" |
| 7 | + |
| 8 | +#include "access/printtup.h" |
| 9 | +#include "commands/extension.h" |
| 10 | +#include "commands/prepare.h" |
| 11 | +#include "executor/executor.h" |
| 12 | +#include "nodes/nodes.h" |
| 13 | +#include "nodes/plannodes.h" |
| 14 | +#include "tcop/pquery.h" |
| 15 | +#include "tcop/utility.h" |
| 16 | +#include "utils/builtins.h" |
| 17 | +#include "utils/memutils.h" |
| 18 | +#include "utils/plancache.h" |
| 19 | +#include "utils/snapmgr.h" |
| 20 | + |
| 21 | + |
| 22 | +#define EXPLAN_DEBUG_LEVEL 0 |
| 23 | + |
| 24 | +PG_MODULE_MAGIC; |
| 25 | + |
| 26 | +PG_FUNCTION_INFO_V1(pg_store_query_plan); |
| 27 | +PG_FUNCTION_INFO_V1(pg_exec_plan); |
| 28 | +PG_FUNCTION_INFO_V1(pg_exec_stored_plan); |
| 29 | + |
| 30 | +void _PG_init(void); |
| 31 | + |
| 32 | +/* |
| 33 | + * Module load/unload callback |
| 34 | + */ |
| 35 | +void |
| 36 | +_PG_init(void) |
| 37 | +{ |
| 38 | + return; |
| 39 | +} |
| 40 | + |
| 41 | +Datum |
| 42 | +pg_store_query_plan(PG_FUNCTION_ARGS) |
| 43 | +{ |
| 44 | + char *query_string = TextDatumGetCString(PG_GETARG_DATUM(1)), |
| 45 | + *filename = TextDatumGetCString(PG_GETARG_DATUM(0)), |
| 46 | + *plan_string; |
| 47 | + int nstmts; |
| 48 | + FILE *fout; |
| 49 | + MemoryContext oldcontext; |
| 50 | + List *parsetree_list; |
| 51 | + RawStmt *parsetree; |
| 52 | + List *querytree_list, |
| 53 | + *plantree_list; |
| 54 | + QueryDesc *queryDesc; |
| 55 | + size_t string_len; |
| 56 | + |
| 57 | + if (EXPLAN_DEBUG_LEVEL > 0) |
| 58 | + elog(LOG, "Store into %s plan of the query %s.", filename, query_string); |
| 59 | + |
| 60 | + oldcontext = MemoryContextSwitchTo(MessageContext); |
| 61 | + |
| 62 | + parsetree_list = pg_parse_query(query_string); |
| 63 | + nstmts = list_length(parsetree_list); |
| 64 | + if (nstmts != 1) |
| 65 | + elog(ERROR, "Query contains %d elements, but must contain only one.", nstmts); |
| 66 | + |
| 67 | + parsetree = (RawStmt *) linitial(parsetree_list); |
| 68 | + querytree_list = pg_analyze_and_rewrite(parsetree, query_string, NULL, 0); |
| 69 | + plantree_list = pg_plan_queries(querytree_list, CURSOR_OPT_PARALLEL_OK, NULL); |
| 70 | + |
| 71 | + queryDesc = CreateQueryDesc((PlannedStmt *) linitial(plantree_list), |
| 72 | + query_string, |
| 73 | + InvalidSnapshot, |
| 74 | + InvalidSnapshot, |
| 75 | + None_Receiver, |
| 76 | + 0, |
| 77 | + 0); |
| 78 | + |
| 79 | + if (EXPLAN_DEBUG_LEVEL > 0) |
| 80 | + elog(INFO, "BEFORE writing %s ...", filename); |
| 81 | + |
| 82 | + fout = fopen(filename, "wb"); |
| 83 | + Assert(fout != NULL); |
| 84 | + string_len = strlen(query_string); |
| 85 | + fwrite(&string_len, sizeof(size_t), 1, fout); |
| 86 | + fwrite(query_string, sizeof(char), string_len, fout); |
| 87 | + |
| 88 | + set_portable_output(true); |
| 89 | + plan_string = nodeToString(queryDesc->plannedstmt); |
| 90 | + set_portable_output(false); |
| 91 | + string_len = strlen(plan_string); |
| 92 | + fwrite(&string_len, sizeof(size_t), 1, fout); |
| 93 | + fwrite(plan_string, sizeof(char), string_len, fout); |
| 94 | + |
| 95 | + fclose(fout); |
| 96 | + MemoryContextSwitchTo(oldcontext); |
| 97 | + PG_RETURN_VOID(); |
| 98 | +} |
| 99 | + |
| 100 | +static void |
| 101 | +exec_plan(char *query_string, char *plan_string) |
| 102 | +{ |
| 103 | + PlannedStmt *pstmt; |
| 104 | + ParamListInfo paramLI = NULL; |
| 105 | + CachedPlanSource *psrc; |
| 106 | + CachedPlan *cplan; |
| 107 | + QueryDesc *queryDesc; |
| 108 | + DestReceiver *receiver; |
| 109 | + int eflags = 0; |
| 110 | + |
| 111 | + PG_TRY(); |
| 112 | + { |
| 113 | + set_portable_input(true); |
| 114 | + pstmt = (PlannedStmt *) stringToNode(plan_string); |
| 115 | + set_portable_input(false); |
| 116 | + } |
| 117 | + PG_CATCH(); |
| 118 | + { |
| 119 | + elog(INFO, "BAD PLAN: %s. Query: %s", plan_string, query_string); |
| 120 | + PG_RE_THROW(); |
| 121 | + } |
| 122 | + PG_END_TRY(); |
| 123 | + |
| 124 | + if (EXPLAN_DEBUG_LEVEL > 0) |
| 125 | + elog(INFO, "query: %s\n", query_string); |
| 126 | + if (EXPLAN_DEBUG_LEVEL > 1) |
| 127 | + elog(INFO, "\nplan: %s\n", plan_string); |
| 128 | + |
| 129 | + psrc = CreateCachedPlan(NULL, query_string, NULL); |
| 130 | + CompleteCachedPlan(psrc, NIL, NULL, NULL, 0, NULL, NULL, |
| 131 | + CURSOR_OPT_GENERIC_PLAN, false); |
| 132 | + |
| 133 | + SetRemoteSubplan(psrc, pstmt); |
| 134 | + cplan = GetCachedPlan(psrc, paramLI, false); |
| 135 | + |
| 136 | + receiver = CreateDestReceiver(DestLog); |
| 137 | + |
| 138 | + PG_TRY(); |
| 139 | + { |
| 140 | + queryDesc = CreateQueryDesc(pstmt, |
| 141 | + query_string, |
| 142 | + GetActiveSnapshot(), |
| 143 | + InvalidSnapshot, |
| 144 | + receiver, |
| 145 | + paramLI, |
| 146 | + 0); |
| 147 | + ExecutorStart(queryDesc, eflags); |
| 148 | + PushActiveSnapshot(queryDesc->snapshot); |
| 149 | + ExecutorRun(queryDesc, ForwardScanDirection, 0); |
| 150 | + PopActiveSnapshot(); |
| 151 | + ExecutorFinish(queryDesc); |
| 152 | + ExecutorEnd(queryDesc); |
| 153 | + FreeQueryDesc(queryDesc); |
| 154 | + } |
| 155 | + PG_CATCH(); |
| 156 | + { |
| 157 | + elog(INFO, "BAD QUERY: '%s'.", query_string); |
| 158 | + ReleaseCachedPlan(cplan, false); |
| 159 | + PG_RE_THROW(); |
| 160 | + } |
| 161 | + PG_END_TRY(); |
| 162 | + |
| 163 | + receiver->rDestroy(receiver); |
| 164 | + ReleaseCachedPlan(cplan, false); |
| 165 | + |
| 166 | + if (EXPLAN_DEBUG_LEVEL > 0) |
| 167 | + elog(INFO, "query execution finished.\n"); |
| 168 | +} |
| 169 | + |
| 170 | +Datum |
| 171 | +pg_exec_plan(PG_FUNCTION_ARGS) |
| 172 | +{ |
| 173 | + char *query_string = TextDatumGetCString(PG_GETARG_DATUM(0)); |
| 174 | + char *plan_string = TextDatumGetCString(PG_GETARG_DATUM(1)); |
| 175 | + |
| 176 | + char *dec_query, |
| 177 | + *dec_plan; |
| 178 | + int dec_query_len, |
| 179 | + dec_query_len1, |
| 180 | + dec_plan_len, |
| 181 | + dec_plan_len1; |
| 182 | + |
| 183 | + Assert(query_string != NULL); |
| 184 | + Assert(plan_string != NULL); |
| 185 | + |
| 186 | + dec_query_len = b64_dec_len(query_string, strlen(query_string) + 1)+1; |
| 187 | + dec_query = palloc0(dec_query_len + 1); |
| 188 | + dec_query_len1 = b64_decode(query_string, strlen(query_string), dec_query); |
| 189 | + Assert(dec_query_len > dec_query_len1); |
| 190 | + |
| 191 | + dec_plan_len = b64_dec_len(plan_string, strlen(plan_string) + 1); |
| 192 | + dec_plan = palloc0(dec_plan_len + 1); |
| 193 | + dec_plan_len1 = b64_decode(plan_string, strlen(plan_string), dec_plan); |
| 194 | + Assert(dec_plan_len > dec_plan_len1); |
| 195 | + |
| 196 | + exec_plan(dec_query, dec_plan); |
| 197 | + pfree(dec_query); |
| 198 | + pfree(dec_plan); |
| 199 | + PG_RETURN_BOOL(true); |
| 200 | +} |
| 201 | + |
| 202 | +static void |
| 203 | +LoadPlanFromFile(const char *filename, char **query_string, char **plan_string) |
| 204 | +{ |
| 205 | + FILE *fin; |
| 206 | + size_t string_len; |
| 207 | + int nelems; |
| 208 | + |
| 209 | + fin = fopen(filename, "rb"); |
| 210 | + Assert(fin != NULL); |
| 211 | + |
| 212 | + nelems = fread(&string_len, sizeof(size_t), 1, fin); |
| 213 | + Assert(nelems == 1); |
| 214 | + *query_string = palloc0(string_len + 1); |
| 215 | + nelems = fread(*query_string, sizeof(char), string_len, fin); |
| 216 | + Assert(nelems == string_len); |
| 217 | + |
| 218 | + nelems = fread(&string_len, sizeof(size_t), 1, fin); |
| 219 | + Assert(nelems == 1); |
| 220 | + *plan_string = palloc0(string_len + 1); |
| 221 | + nelems = fread(*plan_string, sizeof(char), string_len, fin); |
| 222 | + Assert(nelems == string_len); |
| 223 | + |
| 224 | + fclose(fin); |
| 225 | + |
| 226 | +} |
| 227 | + |
| 228 | +Datum |
| 229 | +pg_exec_stored_plan(PG_FUNCTION_ARGS) |
| 230 | +{ |
| 231 | + char *filename = TextDatumGetCString(PG_GETARG_DATUM(0)), |
| 232 | + *query_string = NULL, |
| 233 | + *plan_string = NULL; |
| 234 | + |
| 235 | + LoadPlanFromFile(filename, &query_string, &plan_string); |
| 236 | + exec_plan(query_string, plan_string); |
| 237 | + PG_RETURN_BOOL(true); |
| 238 | +} |
0 commit comments