Skip to content

Commit 5a3423a

Browse files
Add JIT deform_counter
generation_counter includes time spent on both JIT:ing expressions and tuple deforming which are configured independently via options jit_expressions and jit_tuple_deforming. As they are combined in the same counter it's not apparent what fraction of time the tuple deforming takes. This adds deform_counter dedicated to tuple deforming, which allows seeing more directly the influence jit_tuple_deforming is having on the query. The counter is exposed in EXPLAIN and pg_stat_statements bumpin pg_stat_statements to 1.11. Author: Dmitry Dolgov <[email protected]> Reviewed-by: Pavel Stehule <[email protected]> Reviewed-by: Daniel Gustafsson <[email protected]> Discussion: https://postgr.es/m/[email protected]
1 parent 6fe3cef commit 5a3423a

File tree

11 files changed

+145
-6
lines changed

11 files changed

+145
-6
lines changed

contrib/pg_stat_statements/Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ OBJS = \
77

88
EXTENSION = pg_stat_statements
99
DATA = pg_stat_statements--1.4.sql \
10+
pg_stat_statements--1.10--1.11.sql \
1011
pg_stat_statements--1.9--1.10.sql pg_stat_statements--1.8--1.9.sql \
1112
pg_stat_statements--1.7--1.8.sql pg_stat_statements--1.6--1.7.sql \
1213
pg_stat_statements--1.5--1.6.sql pg_stat_statements--1.4--1.5.sql \

contrib/pg_stat_statements/meson.build

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ contrib_targets += pg_stat_statements
2121
install_data(
2222
'pg_stat_statements.control',
2323
'pg_stat_statements--1.4.sql',
24+
'pg_stat_statements--1.10--1.11.sql',
2425
'pg_stat_statements--1.9--1.10.sql',
2526
'pg_stat_statements--1.8--1.9.sql',
2627
'pg_stat_statements--1.7--1.8.sql',
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/* contrib/pg_stat_statements/pg_stat_statements--1.10--1.11.sql */
2+
3+
-- complain if script is sourced in psql, rather than via ALTER EXTENSION
4+
\echo Use "ALTER EXTENSION pg_stat_statements UPDATE TO '1.11'" to load this file. \quit
5+
6+
/* First we have to remove them from the extension */
7+
ALTER EXTENSION pg_stat_statements DROP VIEW pg_stat_statements;
8+
ALTER EXTENSION pg_stat_statements DROP FUNCTION pg_stat_statements(boolean);
9+
10+
/* Then we can drop them */
11+
DROP VIEW pg_stat_statements;
12+
DROP FUNCTION pg_stat_statements(boolean);
13+
14+
/* Now redefine */
15+
CREATE FUNCTION pg_stat_statements(IN showtext boolean,
16+
OUT userid oid,
17+
OUT dbid oid,
18+
OUT toplevel bool,
19+
OUT queryid bigint,
20+
OUT query text,
21+
OUT plans int8,
22+
OUT total_plan_time float8,
23+
OUT min_plan_time float8,
24+
OUT max_plan_time float8,
25+
OUT mean_plan_time float8,
26+
OUT stddev_plan_time float8,
27+
OUT calls int8,
28+
OUT total_exec_time float8,
29+
OUT min_exec_time float8,
30+
OUT max_exec_time float8,
31+
OUT mean_exec_time float8,
32+
OUT stddev_exec_time float8,
33+
OUT rows int8,
34+
OUT shared_blks_hit int8,
35+
OUT shared_blks_read int8,
36+
OUT shared_blks_dirtied int8,
37+
OUT shared_blks_written int8,
38+
OUT local_blks_hit int8,
39+
OUT local_blks_read int8,
40+
OUT local_blks_dirtied int8,
41+
OUT local_blks_written int8,
42+
OUT temp_blks_read int8,
43+
OUT temp_blks_written int8,
44+
OUT blk_read_time float8,
45+
OUT blk_write_time float8,
46+
OUT temp_blk_read_time float8,
47+
OUT temp_blk_write_time float8,
48+
OUT wal_records int8,
49+
OUT wal_fpi int8,
50+
OUT wal_bytes numeric,
51+
OUT jit_functions int8,
52+
OUT jit_generation_time float8,
53+
OUT jit_inlining_count int8,
54+
OUT jit_inlining_time float8,
55+
OUT jit_optimization_count int8,
56+
OUT jit_optimization_time float8,
57+
OUT jit_emission_count int8,
58+
OUT jit_emission_time float8,
59+
OUT jit_deform_count int8,
60+
OUT jit_deform_time float8
61+
)
62+
RETURNS SETOF record
63+
AS 'MODULE_PATHNAME', 'pg_stat_statements_1_11'
64+
LANGUAGE C STRICT VOLATILE PARALLEL SAFE;
65+
66+
CREATE VIEW pg_stat_statements AS
67+
SELECT * FROM pg_stat_statements(true);
68+
69+
GRANT SELECT ON pg_stat_statements TO PUBLIC;

contrib/pg_stat_statements/pg_stat_statements.c

+33-2
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,8 @@ typedef enum pgssVersion
117117
PGSS_V1_3,
118118
PGSS_V1_8,
119119
PGSS_V1_9,
120-
PGSS_V1_10
120+
PGSS_V1_10,
121+
PGSS_V1_11
121122
} pgssVersion;
122123

123124
typedef enum pgssStoreKind
@@ -192,6 +193,10 @@ typedef struct Counters
192193
double jit_generation_time; /* total time to generate jit code */
193194
int64 jit_inlining_count; /* number of times inlining time has been
194195
* > 0 */
196+
double jit_deform_time; /* total time to deform tuples in jit code */
197+
int64 jit_deform_count; /* number of times deform time has been >
198+
* 0 */
199+
195200
double jit_inlining_time; /* total time to inline jit code */
196201
int64 jit_optimization_count; /* number of times optimization time
197202
* has been > 0 */
@@ -312,6 +317,7 @@ PG_FUNCTION_INFO_V1(pg_stat_statements_1_3);
312317
PG_FUNCTION_INFO_V1(pg_stat_statements_1_8);
313318
PG_FUNCTION_INFO_V1(pg_stat_statements_1_9);
314319
PG_FUNCTION_INFO_V1(pg_stat_statements_1_10);
320+
PG_FUNCTION_INFO_V1(pg_stat_statements_1_11);
315321
PG_FUNCTION_INFO_V1(pg_stat_statements);
316322
PG_FUNCTION_INFO_V1(pg_stat_statements_info);
317323

@@ -1398,6 +1404,10 @@ pgss_store(const char *query, uint64 queryId,
13981404
e->counters.jit_functions += jitusage->created_functions;
13991405
e->counters.jit_generation_time += INSTR_TIME_GET_MILLISEC(jitusage->generation_counter);
14001406

1407+
if (INSTR_TIME_GET_MILLISEC(jitusage->deform_counter))
1408+
e->counters.jit_deform_count++;
1409+
e->counters.jit_deform_time += INSTR_TIME_GET_MILLISEC(jitusage->deform_counter);
1410+
14011411
if (INSTR_TIME_GET_MILLISEC(jitusage->inlining_counter))
14021412
e->counters.jit_inlining_count++;
14031413
e->counters.jit_inlining_time += INSTR_TIME_GET_MILLISEC(jitusage->inlining_counter);
@@ -1460,7 +1470,8 @@ pg_stat_statements_reset(PG_FUNCTION_ARGS)
14601470
#define PG_STAT_STATEMENTS_COLS_V1_8 32
14611471
#define PG_STAT_STATEMENTS_COLS_V1_9 33
14621472
#define PG_STAT_STATEMENTS_COLS_V1_10 43
1463-
#define PG_STAT_STATEMENTS_COLS 43 /* maximum of above */
1473+
#define PG_STAT_STATEMENTS_COLS_V1_11 45
1474+
#define PG_STAT_STATEMENTS_COLS 45 /* maximum of above */
14641475

14651476
/*
14661477
* Retrieve statement statistics.
@@ -1472,6 +1483,16 @@ pg_stat_statements_reset(PG_FUNCTION_ARGS)
14721483
* expected API version is identified by embedding it in the C name of the
14731484
* function. Unfortunately we weren't bright enough to do that for 1.1.
14741485
*/
1486+
Datum
1487+
pg_stat_statements_1_11(PG_FUNCTION_ARGS)
1488+
{
1489+
bool showtext = PG_GETARG_BOOL(0);
1490+
1491+
pg_stat_statements_internal(fcinfo, PGSS_V1_11, showtext);
1492+
1493+
return (Datum) 0;
1494+
}
1495+
14751496
Datum
14761497
pg_stat_statements_1_10(PG_FUNCTION_ARGS)
14771498
{
@@ -1602,6 +1623,10 @@ pg_stat_statements_internal(FunctionCallInfo fcinfo,
16021623
if (api_version != PGSS_V1_10)
16031624
elog(ERROR, "incorrect number of output arguments");
16041625
break;
1626+
case PG_STAT_STATEMENTS_COLS_V1_11:
1627+
if (api_version != PGSS_V1_11)
1628+
elog(ERROR, "incorrect number of output arguments");
1629+
break;
16051630
default:
16061631
elog(ERROR, "incorrect number of output arguments");
16071632
}
@@ -1834,6 +1859,11 @@ pg_stat_statements_internal(FunctionCallInfo fcinfo,
18341859
values[i++] = Int64GetDatumFast(tmp.jit_emission_count);
18351860
values[i++] = Float8GetDatumFast(tmp.jit_emission_time);
18361861
}
1862+
if (api_version >= PGSS_V1_11)
1863+
{
1864+
values[i++] = Int64GetDatumFast(tmp.jit_deform_count);
1865+
values[i++] = Float8GetDatumFast(tmp.jit_deform_time);
1866+
}
18371867

18381868
Assert(i == (api_version == PGSS_V1_0 ? PG_STAT_STATEMENTS_COLS_V1_0 :
18391869
api_version == PGSS_V1_1 ? PG_STAT_STATEMENTS_COLS_V1_1 :
@@ -1842,6 +1872,7 @@ pg_stat_statements_internal(FunctionCallInfo fcinfo,
18421872
api_version == PGSS_V1_8 ? PG_STAT_STATEMENTS_COLS_V1_8 :
18431873
api_version == PGSS_V1_9 ? PG_STAT_STATEMENTS_COLS_V1_9 :
18441874
api_version == PGSS_V1_10 ? PG_STAT_STATEMENTS_COLS_V1_10 :
1875+
api_version == PGSS_V1_11 ? PG_STAT_STATEMENTS_COLS_V1_11 :
18451876
-1 /* fail if you forget to update this assert */ ));
18461877

18471878
tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc, values, nulls);
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# pg_stat_statements extension
22
comment = 'track planning and execution statistics of all SQL statements executed'
3-
default_version = '1.10'
3+
default_version = '1.11'
44
module_pathname = '$libdir/pg_stat_statements'
55
relocatable = true

doc/src/sgml/jit.sgml

+1-1
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ SET
170170
JIT:
171171
Functions: 3
172172
Options: Inlining false, Optimization false, Expressions true, Deforming true
173-
Timing: Generation 1.259 ms, Inlining 0.000 ms, Optimization 0.797 ms, Emission 5.048 ms, Total 7.104 ms
173+
Timing: Generation 1.259 ms (Deform 0.000 ms), Inlining 0.000 ms, Optimization 0.797 ms, Emission 5.048 ms, Total 7.104 ms
174174
Execution Time: 7.416 ms
175175
</screen>
176176
As visible here, <acronym>JIT</acronym> was used, but inlining and

doc/src/sgml/pgstatstatements.sgml

+19
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,25 @@
420420
</para></entry>
421421
</row>
422422

423+
<row>
424+
<entry role="catalog_table_entry"><para role="column_definition">
425+
<structfield>jit_deform_count</structfield> <type>bigint</type>
426+
</para>
427+
<para>
428+
Total number of tuple deform functions JIT-compiled by the statement
429+
</para></entry>
430+
</row>
431+
432+
<row>
433+
<entry role="catalog_table_entry"><para role="column_definition">
434+
<structfield>jit_deform_time</structfield> <type>double precision</type>
435+
</para>
436+
<para>
437+
Total time spent by the statement on JIT-compiling tuple deform
438+
functions, in milliseconds
439+
</para></entry>
440+
</row>
441+
423442
<row>
424443
<entry role="catalog_table_entry"><para role="column_definition">
425444
<structfield>jit_inlining_count</structfield> <type>bigint</type>

src/backend/commands/explain.c

+10-2
Original file line numberDiff line numberDiff line change
@@ -893,6 +893,7 @@ ExplainPrintJIT(ExplainState *es, int jit_flags, JitInstrumentation *ji)
893893

894894
/* calculate total time */
895895
INSTR_TIME_SET_ZERO(total_time);
896+
/* don't add deform_counter, it's included in generation_counter */
896897
INSTR_TIME_ADD(total_time, ji->generation_counter);
897898
INSTR_TIME_ADD(total_time, ji->inlining_counter);
898899
INSTR_TIME_ADD(total_time, ji->optimization_counter);
@@ -920,8 +921,9 @@ ExplainPrintJIT(ExplainState *es, int jit_flags, JitInstrumentation *ji)
920921
{
921922
ExplainIndentText(es);
922923
appendStringInfo(es->str,
923-
"Timing: %s %.3f ms, %s %.3f ms, %s %.3f ms, %s %.3f ms, %s %.3f ms\n",
924+
"Timing: %s %.3f ms (%s %.3f ms), %s %.3f ms, %s %.3f ms, %s %.3f ms, %s %.3f ms\n",
924925
"Generation", 1000.0 * INSTR_TIME_GET_DOUBLE(ji->generation_counter),
926+
"Deform", 1000.0 * INSTR_TIME_GET_DOUBLE(ji->deform_counter),
925927
"Inlining", 1000.0 * INSTR_TIME_GET_DOUBLE(ji->inlining_counter),
926928
"Optimization", 1000.0 * INSTR_TIME_GET_DOUBLE(ji->optimization_counter),
927929
"Emission", 1000.0 * INSTR_TIME_GET_DOUBLE(ji->emission_counter),
@@ -945,9 +947,15 @@ ExplainPrintJIT(ExplainState *es, int jit_flags, JitInstrumentation *ji)
945947
{
946948
ExplainOpenGroup("Timing", "Timing", true, es);
947949

948-
ExplainPropertyFloat("Generation", "ms",
950+
ExplainOpenGroup("Generation", "Generation", true, es);
951+
ExplainPropertyFloat("Deform", "ms",
952+
1000.0 * INSTR_TIME_GET_DOUBLE(ji->deform_counter),
953+
3, es);
954+
ExplainPropertyFloat("Total", "ms",
949955
1000.0 * INSTR_TIME_GET_DOUBLE(ji->generation_counter),
950956
3, es);
957+
ExplainCloseGroup("Generation", "Generation", true, es);
958+
951959
ExplainPropertyFloat("Inlining", "ms",
952960
1000.0 * INSTR_TIME_GET_DOUBLE(ji->inlining_counter),
953961
3, es);

src/backend/jit/jit.c

+1
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ InstrJitAgg(JitInstrumentation *dst, JitInstrumentation *add)
185185
{
186186
dst->created_functions += add->created_functions;
187187
INSTR_TIME_ADD(dst->generation_counter, add->generation_counter);
188+
INSTR_TIME_ADD(dst->deform_counter, add->deform_counter);
188189
INSTR_TIME_ADD(dst->inlining_counter, add->inlining_counter);
189190
INSTR_TIME_ADD(dst->optimization_counter, add->optimization_counter);
190191
INSTR_TIME_ADD(dst->emission_counter, add->emission_counter);

src/backend/jit/llvm/llvmjit_expr.c

+6
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,9 @@ llvm_compile_expr(ExprState *state)
121121
LLVMValueRef v_aggnulls;
122122

123123
instr_time starttime;
124+
instr_time deform_starttime;
124125
instr_time endtime;
126+
instr_time deform_endtime;
125127

126128
llvm_enter_fatal_on_oom();
127129

@@ -315,10 +317,14 @@ llvm_compile_expr(ExprState *state)
315317
*/
316318
if (tts_ops && desc && (context->base.flags & PGJIT_DEFORM))
317319
{
320+
INSTR_TIME_SET_CURRENT(deform_starttime);
318321
l_jit_deform =
319322
slot_compile_deform(context, desc,
320323
tts_ops,
321324
op->d.fetch.last_var);
325+
INSTR_TIME_SET_CURRENT(deform_endtime);
326+
INSTR_TIME_ACCUM_DIFF(context->base.instr.deform_counter,
327+
deform_endtime, deform_starttime);
322328
}
323329

324330
if (l_jit_deform)

src/include/jit/jit.h

+3
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ typedef struct JitInstrumentation
3232
/* accumulated time to generate code */
3333
instr_time generation_counter;
3434

35+
/* accumulated time to deform tuples, included into generation_counter */
36+
instr_time deform_counter;
37+
3538
/* accumulated time for inlining */
3639
instr_time inlining_counter;
3740

0 commit comments

Comments
 (0)