@@ -28821,6 +28821,29 @@ acl | {postgres=arwdDxtm/postgres,foo=r/postgres}
28821
28821
</para></entry>
28822
28822
</row>
28823
28823
28824
+ <row>
28825
+ <entry role="func_table_entry"><para role="func_signature">
28826
+ <indexterm>
28827
+ <primary>pg_log_query_plan</primary>
28828
+ </indexterm>
28829
+ <function>pg_log_query_plan</function> ( <parameter>pid</parameter> <type>integer</type> )
28830
+ <returnvalue>boolean</returnvalue>
28831
+ </para>
28832
+ <para>
28833
+ Requests to log the plan of the query currently running on the
28834
+ backend with specified process ID.
28835
+ It will be logged at <literal>LOG</literal> message level and
28836
+ will appear in the server log based on the log
28837
+ configuration set (See <xref linkend="runtime-config-logging"/>
28838
+ for more information), but will not be sent to the client
28839
+ regardless of <xref linkend="guc-client-min-messages"/>.
28840
+ </para>
28841
+ <para>
28842
+ This function is restricted to superusers by default, but other
28843
+ users can be granted EXECUTE to run the function.
28844
+ </para></entry>
28845
+ </row>
28846
+
28824
28847
<row>
28825
28848
<entry role="func_table_entry"><para role="func_signature">
28826
28849
<indexterm>
@@ -28971,6 +28994,38 @@ stats_timestamp | 2025-03-24 13:55:47.796698+01
28971
28994
will be less resource intensive when only the local backend is of interest.
28972
28995
</para>
28973
28996
</note>
28997
+ </para>
28998
+
28999
+ <para>
29000
+ <function>pg_log_query_plan</function> can be used
29001
+ to log the plan of a backend process. For example:
29002
+ <programlisting>
29003
+ postgres=# SELECT pg_log_query_plan(201116);
29004
+ pg_log_query_plan
29005
+ ---------------------------
29006
+ t
29007
+ (1 row)
29008
+ </programlisting>
29009
+ The format of the query plan is the same as when <literal>VERBOSE</literal>,
29010
+ <literal>COSTS</literal>, <literal>SETTINGS</literal> and
29011
+ <literal>FORMAT TEXT</literal> are used in the <command>EXPLAIN</command>
29012
+ command. For example:
29013
+ <screen>
29014
+ LOG: query plan running on backend with PID 201116 is:
29015
+ Query Text: SELECT * FROM pgbench_accounts;
29016
+ Seq Scan on public.pgbench_accounts (cost=0.00..52787.00 rows=2000000 width=97)
29017
+ Output: aid, bid, abalance, filler
29018
+ Settings: work_mem = '1MB'
29019
+ Query Identifier: 8621255546560739680
29020
+ </screen>
29021
+ Note that when the target is executing nested statements(statements executed
29022
+ inside a function), only the innermost query plan is logged.
29023
+ Note that logging plan may take some time, as it occurs when
29024
+ the plan node is executed. For example, when a query is
29025
+ running <function>pg_sleep</function>, the plan will not be
29026
+ logged until the function execution completes.
29027
+ Similarly, when a query is running under the extended query
29028
+ protocol, the plan is logged only during the execute step.
28974
29029
</para>
28975
29030
28976
29031
</sect2>
0 commit comments