Skip to content

Commit cafde58

Browse files
committed
Allow compute_query_id to be set to 'auto' and make it default
Allowing only on/off meant that all either all existing configuration guides would become obsolete if we disabled it by default, or that we would have to accept a performance loss in the default config if we enabled it by default. By allowing 'auto' as a middle ground, the performance cost is only paid by those who enable pg_stat_statements and similar modules. I only edited the release notes to comment-out a paragraph that is now factually wrong; further edits are probably needed to describe the related change in more detail. Author: Julien Rouhaud <rjuju123@gmail.com> Reviewed-by: Justin Pryzby <pryzby@telsasoft.com> Discussion: https://postgr.es/m/20210513002623.eugftm4nk2lvvks3@nol
1 parent 30d8bad commit cafde58

File tree

14 files changed

+108
-30
lines changed

14 files changed

+108
-30
lines changed

contrib/pg_stat_statements/pg_stat_statements.c

+6
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,12 @@ _PG_init(void)
369369
if (!process_shared_preload_libraries_in_progress)
370370
return;
371371

372+
/*
373+
* Inform the postmaster that we want to enable query_id calculation if
374+
* compute_query_id is set to auto.
375+
*/
376+
EnableQueryId();
377+
372378
/*
373379
* Define (or redefine) custom GUC variables.
374380
*/
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
shared_preload_libraries = 'pg_stat_statements'
2-
compute_query_id = on

doc/src/sgml/config.sgml

+7-2
Original file line numberDiff line numberDiff line change
@@ -7627,7 +7627,7 @@ COPY postgres_log FROM '/full/path/to/logfile.csv' WITH csv;
76277627
<variablelist>
76287628

76297629
<varlistentry id="guc-compute-query-id" xreflabel="compute_query_id">
7630-
<term><varname>compute_query_id</varname> (<type>boolean</type>)
7630+
<term><varname>compute_query_id</varname> (<type>enum</type>)
76317631
<indexterm>
76327632
<primary><varname>compute_query_id</varname> configuration parameter</primary>
76337633
</indexterm>
@@ -7643,7 +7643,12 @@ COPY postgres_log FROM '/full/path/to/logfile.csv' WITH csv;
76437643
identifier to be computed. Note that an external module can
76447644
alternatively be used if the in-core query identifier computation
76457645
method is not acceptable. In this case, in-core computation
7646-
must be disabled. The default is <literal>off</literal>.
7646+
must be always disabled.
7647+
Valid values are <literal>off</literal> (always disabled),
7648+
<literal>on</literal> (always enabled) and <literal>auto</literal>,
7649+
which lets modules such as <xref linkend="pgstatstatements"/>
7650+
automatically enable it.
7651+
The default is <literal>auto</literal>.
76477652
</para>
76487653
<note>
76497654
<para>

doc/src/sgml/pgstatstatements.sgml

+5-9
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,14 @@
1818
<xref linkend="guc-shared-preload-libraries"/> in
1919
<filename>postgresql.conf</filename>, because it requires additional shared memory.
2020
This means that a server restart is needed to add or remove the module.
21+
In addition, query identifier calculation must be enabled in order for the
22+
module to be active, which is done automatically if <xref linkend="guc-compute-query-id"/>
23+
is set to <literal>auto</literal> or <literal>on</literal>, or any third-party
24+
module that calculates query identifiers is loaded.
2125
</para>
2226

2327
<para>
24-
The module will not track statistics unless query
25-
identifiers are calculated. This can be done by enabling <xref
26-
linkend="guc-compute-query-id"/> or using a third-party module that
27-
computes its own query identifiers. Note that all statistics tracked
28-
by this module must be reset if the query identifier method is changed.
29-
</para>
30-
31-
<para>
32-
When <filename>pg_stat_statements</filename> is loaded, it tracks
28+
When <filename>pg_stat_statements</filename> is active, it tracks
3329
statistics across all databases of the server. To access and manipulate
3430
these statistics, the module provides views
3531
<structname>pg_stat_statements</structname> and

doc/src/sgml/release-14.sgml

+2
Original file line numberDiff line numberDiff line change
@@ -3181,10 +3181,12 @@ Author: Bruce Momjian <bruce@momjian.us>
31813181
Move query hash computation from pg_stat_statements to the core server (Julien Rouhaud)
31823182
</para>
31833183

3184+
<!--
31843185
<para>
31853186
Extension pg_stat_statements will now need to enable query hash computation to function properly.
31863187
This can be done by enabling the server variable compute_query_id or by using an extension with a custom hash computation method.
31873188
</para>
3189+
-->
31883190
</listitem>
31893191

31903192
<listitem>

src/backend/commands/explain.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ ExplainQuery(ParseState *pstate, ExplainStmt *stmt,
245245
es->summary = (summary_set) ? es->summary : es->analyze;
246246

247247
query = castNode(Query, stmt->query);
248-
if (compute_query_id)
248+
if (IsQueryIdEnabled())
249249
jstate = JumbleQuery(query, pstate->p_sourcetext);
250250

251251
if (post_parse_analyze_hook)

src/backend/parser/analyze.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ parse_analyze(RawStmt *parseTree, const char *sourceText,
124124

125125
query = transformTopLevelStmt(pstate, parseTree);
126126

127-
if (compute_query_id)
127+
if (IsQueryIdEnabled())
128128
jstate = JumbleQuery(query, sourceText);
129129

130130
if (post_parse_analyze_hook)
@@ -163,7 +163,7 @@ parse_analyze_varparams(RawStmt *parseTree, const char *sourceText,
163163
/* make sure all is well with parameter types */
164164
check_variable_parameters(pstate, query);
165165

166-
if (compute_query_id)
166+
if (IsQueryIdEnabled())
167167
jstate = JumbleQuery(query, sourceText);
168168

169169
if (post_parse_analyze_hook)

src/backend/postmaster/postmaster.c

+3
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,7 @@ typedef struct
521521
pg_time_t first_syslogger_file_time;
522522
bool redirection_done;
523523
bool IsBinaryUpgrade;
524+
bool auto_query_id_enabled;
524525
int max_safe_fds;
525526
int MaxBackends;
526527
#ifdef WIN32
@@ -6168,6 +6169,7 @@ save_backend_variables(BackendParameters *param, Port *port,
61686169

61696170
param->redirection_done = redirection_done;
61706171
param->IsBinaryUpgrade = IsBinaryUpgrade;
6172+
param->auto_query_id_enabled = auto_query_id_enabled;
61716173
param->max_safe_fds = max_safe_fds;
61726174

61736175
param->MaxBackends = MaxBackends;
@@ -6401,6 +6403,7 @@ restore_backend_variables(BackendParameters *param, Port *port)
64016403

64026404
redirection_done = param->redirection_done;
64036405
IsBinaryUpgrade = param->IsBinaryUpgrade;
6406+
auto_query_id_enabled = param->auto_query_id_enabled;
64046407
max_safe_fds = param->max_safe_fds;
64056408

64066409
MaxBackends = param->MaxBackends;

src/backend/tcop/postgres.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,7 @@ pg_analyze_and_rewrite_params(RawStmt *parsetree,
704704

705705
query = transformTopLevelStmt(pstate, parsetree);
706706

707-
if (compute_query_id)
707+
if (IsQueryIdEnabled())
708708
jstate = JumbleQuery(query, query_string);
709709

710710
if (post_parse_analyze_hook)

src/backend/utils/misc/guc.c

+28-10
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@
101101
#include "utils/plancache.h"
102102
#include "utils/portal.h"
103103
#include "utils/ps_status.h"
104+
#include "utils/queryjumble.h"
104105
#include "utils/rls.h"
105106
#include "utils/snapmgr.h"
106107
#include "utils/tzparser.h"
@@ -402,6 +403,23 @@ static const struct config_enum_entry backslash_quote_options[] = {
402403
{NULL, 0, false}
403404
};
404405

406+
/*
407+
* Although only "on", "off", and "auto" are documented, we accept
408+
* all the likely variants of "on" and "off".
409+
*/
410+
static const struct config_enum_entry compute_query_id_options[] = {
411+
{"auto", COMPUTE_QUERY_ID_AUTO, false},
412+
{"on", COMPUTE_QUERY_ID_ON, false},
413+
{"off", COMPUTE_QUERY_ID_OFF, false},
414+
{"true", COMPUTE_QUERY_ID_ON, true},
415+
{"false", COMPUTE_QUERY_ID_OFF, true},
416+
{"yes", COMPUTE_QUERY_ID_ON, true},
417+
{"no", COMPUTE_QUERY_ID_OFF, true},
418+
{"1", COMPUTE_QUERY_ID_ON, true},
419+
{"0", COMPUTE_QUERY_ID_OFF, true},
420+
{NULL, 0, false}
421+
};
422+
405423
/*
406424
* Although only "on", "off", and "partition" are documented, we
407425
* accept all the likely variants of "on" and "off".
@@ -534,7 +552,6 @@ extern const struct config_enum_entry dynamic_shared_memory_options[];
534552
/*
535553
* GUC option variables that are exported from this module
536554
*/
537-
bool compute_query_id = false;
538555
bool log_duration = false;
539556
bool Debug_print_plan = false;
540557
bool Debug_print_parse = false;
@@ -1441,15 +1458,6 @@ static struct config_bool ConfigureNamesBool[] =
14411458
true,
14421459
NULL, NULL, NULL
14431460
},
1444-
{
1445-
{"compute_query_id", PGC_SUSET, STATS_MONITORING,
1446-
gettext_noop("Compute query identifiers."),
1447-
NULL
1448-
},
1449-
&compute_query_id,
1450-
false,
1451-
NULL, NULL, NULL
1452-
},
14531461
{
14541462
{"log_parser_stats", PGC_SUSET, STATS_MONITORING,
14551463
gettext_noop("Writes parser performance statistics to the server log."),
@@ -4619,6 +4627,16 @@ static struct config_enum ConfigureNamesEnum[] =
46194627
NULL, NULL, NULL
46204628
},
46214629

4630+
{
4631+
{"compute_query_id", PGC_SUSET, STATS_MONITORING,
4632+
gettext_noop("Compute query identifiers."),
4633+
NULL
4634+
},
4635+
&compute_query_id,
4636+
COMPUTE_QUERY_ID_AUTO, compute_query_id_options,
4637+
NULL, NULL, NULL
4638+
},
4639+
46224640
{
46234641
{"constraint_exclusion", PGC_USERSET, QUERY_TUNING_OTHER,
46244642
gettext_noop("Enables the planner to use constraints to optimize queries."),

src/backend/utils/misc/postgresql.conf.sample

+1-1
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,7 @@
604604

605605
# - Monitoring -
606606

607-
#compute_query_id = off
607+
#compute_query_id = auto
608608
#log_statement_stats = off
609609
#log_parser_stats = off
610610
#log_planner_stats = off

src/backend/utils/misc/queryjumble.c

+21
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@
3939

4040
#define JUMBLE_SIZE 1024 /* query serialization buffer size */
4141

42+
/* GUC parameters */
43+
int compute_query_id = COMPUTE_QUERY_ID_AUTO;
44+
45+
/* True when compute_query_id is ON, or AUTO and a module requests them */
46+
bool query_id_enabled = false;
47+
4248
static uint64 compute_utility_query_id(const char *str, int query_location, int query_len);
4349
static void AppendJumble(JumbleState *jstate,
4450
const unsigned char *item, Size size);
@@ -96,6 +102,8 @@ JumbleQuery(Query *query, const char *querytext)
96102
{
97103
JumbleState *jstate = NULL;
98104

105+
Assert(IsQueryIdEnabled());
106+
99107
if (query->utilityStmt)
100108
{
101109
query->queryId = compute_utility_query_id(querytext,
@@ -132,6 +140,19 @@ JumbleQuery(Query *query, const char *querytext)
132140
return jstate;
133141
}
134142

143+
/*
144+
* Enables query identifier computation.
145+
*
146+
* Third-party plugins can use this function to inform core that they require
147+
* a query identifier to be computed.
148+
*/
149+
void
150+
EnableQueryId(void)
151+
{
152+
if (compute_query_id != COMPUTE_QUERY_ID_OFF)
153+
query_id_enabled = true;
154+
}
155+
135156
/*
136157
* Compute a query identifier for the given utility query string.
137158
*/

src/include/utils/guc.h

-1
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,6 @@ extern bool log_btree_build_stats;
247247
extern PGDLLIMPORT bool check_function_bodies;
248248
extern bool session_auth_is_superuser;
249249

250-
extern bool compute_query_id;
251250
extern bool log_duration;
252251
extern int log_parameter_max_length;
253252
extern int log_parameter_max_length_on_error;

src/include/utils/queryjumble.h

+31-2
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,36 @@ typedef struct JumbleState
5252
int highest_extern_param_id;
5353
} JumbleState;
5454

55-
const char *CleanQuerytext(const char *query, int *location, int *len);
56-
JumbleState *JumbleQuery(Query *query, const char *querytext);
55+
/* Values for the compute_query_id GUC */
56+
typedef enum
57+
{
58+
COMPUTE_QUERY_ID_OFF,
59+
COMPUTE_QUERY_ID_ON,
60+
COMPUTE_QUERY_ID_AUTO
61+
} ComputeQueryIdType;
62+
63+
/* GUC parameters */
64+
extern int compute_query_id;
65+
66+
67+
extern const char *CleanQuerytext(const char *query, int *location, int *len);
68+
extern JumbleState *JumbleQuery(Query *query, const char *querytext);
69+
extern void EnableQueryId(void);
70+
71+
extern bool query_id_enabled;
72+
73+
/*
74+
* Returns whether query identifier computation has been enabled, either
75+
* directly in the GUC or by a module when the setting is 'auto'.
76+
*/
77+
static inline bool
78+
IsQueryIdEnabled(void)
79+
{
80+
if (compute_query_id == COMPUTE_QUERY_ID_OFF)
81+
return false;
82+
if (compute_query_id == COMPUTE_QUERY_ID_ON)
83+
return true;
84+
return query_id_enabled;
85+
}
5786

5887
#endif /* QUERYJUMBLE_H */

0 commit comments

Comments
 (0)