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