|
| 1 | +/*------------------------------------------------------------------------- |
| 2 | + * |
| 3 | + * dynamic_explain.c |
| 4 | + * Explain query plans during execution |
| 5 | + * |
| 6 | + * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group |
| 7 | + * Portions Copyright (c) 1994-5, Regents of the University of California |
| 8 | + * |
| 9 | + * IDENTIFICATION |
| 10 | + * src/backend/commands/dynamic_explain.c |
| 11 | + * |
| 12 | + *------------------------------------------------------------------------- |
| 13 | + */ |
| 14 | +#include "postgres.h" |
| 15 | + |
| 16 | +#include "access/xact.h" |
| 17 | +#include "commands/dynamic_explain.h" |
| 18 | +#include "commands/explain.h" |
| 19 | +#include "commands/explain_format.h" |
| 20 | +#include "commands/explain_state.h" |
| 21 | +#include "miscadmin.h" |
| 22 | +#include "storage/proc.h" |
| 23 | +#include "storage/procarray.h" |
| 24 | +#include "storage/procsignal.h" |
| 25 | +#include "utils/backend_status.h" |
| 26 | +#include "utils/injection_point.h" |
| 27 | + |
| 28 | +/* Is plan node wrapping for query plan logging currently in progress? */ |
| 29 | +static bool WrapNodesInProgress = false; |
| 30 | + |
| 31 | +/* |
| 32 | + * Handle receipt of an interrupt indicating logging the plan of the currently |
| 33 | + * running query. |
| 34 | + * |
| 35 | + * All the actual work is deferred to ProcessLogQueryPlanInterrupt(), |
| 36 | + * because we cannot safely emit a log message inside the signal handler. |
| 37 | + */ |
| 38 | +void |
| 39 | +HandleLogQueryPlanInterrupt(void) |
| 40 | +{ |
| 41 | +#ifdef USE_INJECTION_POINTS |
| 42 | + INJECTION_POINT("log-query-interrupt", NULL); |
| 43 | +#endif |
| 44 | + InterruptPending = true; |
| 45 | + LogQueryPlanPending = true; |
| 46 | + /* latch will be set by procsignal_sigusr1_handler */ |
| 47 | +} |
| 48 | + |
| 49 | +/* |
| 50 | + * Actual plan logging function. |
| 51 | + */ |
| 52 | +void |
| 53 | +LogQueryPlan(void) |
| 54 | +{ |
| 55 | + ExplainState *es; |
| 56 | + MemoryContext cxt; |
| 57 | + MemoryContext old_cxt; |
| 58 | + QueryDesc *queryDesc; |
| 59 | + |
| 60 | + cxt = AllocSetContextCreate(CurrentMemoryContext, |
| 61 | + "log_query_plan temporary context", |
| 62 | + ALLOCSET_DEFAULT_SIZES); |
| 63 | + |
| 64 | + old_cxt = MemoryContextSwitchTo(cxt); |
| 65 | + |
| 66 | + es = NewExplainState(); |
| 67 | + |
| 68 | + es->format = EXPLAIN_FORMAT_TEXT; |
| 69 | + es->settings = true; |
| 70 | + es->verbose = true; |
| 71 | + es->signaled = true; |
| 72 | + |
| 73 | + /* |
| 74 | + * Current QueryDesc is valid only during standard_ExecutorRun. However, |
| 75 | + * ExecProcNode can be called afterward(i.e., ExecPostprocessPlan). To |
| 76 | + * handle the case, check whether we have QueryDesc now. |
| 77 | + */ |
| 78 | + queryDesc = GetCurrentQueryDesc(); |
| 79 | + |
| 80 | + if (queryDesc == NULL) |
| 81 | + { |
| 82 | + LogQueryPlanPending = false; |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + ExplainStringAssemble(es, queryDesc, es->format, 0, -1); |
| 87 | + |
| 88 | + ereport(LOG_SERVER_ONLY, |
| 89 | + errmsg("query and its plan running on backend with PID %d are:\n%s", |
| 90 | + MyProcPid, es->str->data)); |
| 91 | + |
| 92 | + MemoryContextSwitchTo(old_cxt); |
| 93 | + MemoryContextDelete(cxt); |
| 94 | + |
| 95 | + LogQueryPlanPending = false; |
| 96 | +} |
| 97 | + |
| 98 | +/* |
| 99 | + * Process the request for logging query plan at CHECK_FOR_INTERRUPTS(). |
| 100 | + * |
| 101 | + * Since executing EXPLAIN-related code at an arbitrary CHECK_FOR_INTERRUPTS() |
| 102 | + * point is potentially unsafe, this function just wraps the nodes of |
| 103 | + * ExecProcNode with ExecProcNodeFirst, which logs query plan if requested. |
| 104 | + * This way ensures that EXPLAIN-related code is executed only during |
| 105 | + * ExecProcNodeFirst, where it is considered safe. |
| 106 | + */ |
| 107 | +void |
| 108 | +ProcessLogQueryPlanInterrupt(void) |
| 109 | +{ |
| 110 | + QueryDesc *querydesc = GetCurrentQueryDesc(); |
| 111 | + |
| 112 | + /* If current query has already finished, we can do nothing but exit */ |
| 113 | + if (querydesc == NULL) |
| 114 | + { |
| 115 | + LogQueryPlanPending = false; |
| 116 | + return; |
| 117 | + } |
| 118 | + |
| 119 | + /* |
| 120 | + * Exit immediately if wrapping plan is already in progress. This prevents |
| 121 | + * recursive calls, which could occur if logging is requested repeatedly and |
| 122 | + * rapidly, potentially leading to infinite recursion and crash. |
| 123 | + */ |
| 124 | + if (WrapNodesInProgress) |
| 125 | + return; |
| 126 | + |
| 127 | + WrapNodesInProgress = true; |
| 128 | + |
| 129 | + PG_TRY(); |
| 130 | + { |
| 131 | + /* |
| 132 | + * Wrap ExecProcNodes with ExecProcNodeFirst, which logs query plan |
| 133 | + * when LogQueryPlanPending is true. |
| 134 | + */ |
| 135 | + ExecSetExecProcNodeRecurse(querydesc->planstate); |
| 136 | + } |
| 137 | + PG_FINALLY(); |
| 138 | + { |
| 139 | + WrapNodesInProgress = false; |
| 140 | + } |
| 141 | + PG_END_TRY(); |
| 142 | +} |
| 143 | + |
| 144 | +/* |
| 145 | + * Signal a backend process to log the query plan of the running query. |
| 146 | + * |
| 147 | + * By default, only superusers are allowed to signal to log the plan because |
| 148 | + * allowing any users to issue this request at an unbounded rate would |
| 149 | + * cause lots of log messages and which can lead to denial of service. |
| 150 | + * Additional roles can be permitted with GRANT. |
| 151 | + */ |
| 152 | +Datum |
| 153 | +pg_log_query_plan(PG_FUNCTION_ARGS) |
| 154 | +{ |
| 155 | + int pid = PG_GETARG_INT32(0); |
| 156 | + PGPROC *proc; |
| 157 | + PgBackendStatus *be_status; |
| 158 | + |
| 159 | + proc = BackendPidGetProc(pid); |
| 160 | + |
| 161 | + if (proc == NULL) |
| 162 | + { |
| 163 | + /* |
| 164 | + * This is just a warning so a loop-through-resultset will not abort |
| 165 | + * if one backend terminated on its own during the run. |
| 166 | + */ |
| 167 | + ereport(WARNING, |
| 168 | + (errmsg("PID %d is not a PostgreSQL backend process", pid))); |
| 169 | + PG_RETURN_BOOL(false); |
| 170 | + } |
| 171 | + |
| 172 | + be_status = pgstat_get_beentry_by_proc_number(proc->vxid.procNumber); |
| 173 | + if (be_status->st_backendType != B_BACKEND) |
| 174 | + { |
| 175 | + ereport(WARNING, |
| 176 | + (errmsg("PID %d is not a PostgreSQL client backend process", pid))); |
| 177 | + PG_RETURN_BOOL(false); |
| 178 | + } |
| 179 | + |
| 180 | + if (SendProcSignal(pid, PROCSIG_LOG_QUERY_PLAN, proc->vxid.procNumber) < 0) |
| 181 | + { |
| 182 | + ereport(WARNING, |
| 183 | + (errmsg("could not send signal to process %d: %m", pid))); |
| 184 | + PG_RETURN_BOOL(false); |
| 185 | + } |
| 186 | + |
| 187 | + PG_RETURN_BOOL(true); |
| 188 | +} |
0 commit comments