-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathpgsp_explain.c
232 lines (201 loc) · 6.44 KB
/
pgsp_explain.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*-------------------------------------------------------------------------
*
* pgsp_explain.c: extracted code from explain.c for explain of triggers.
*
* Copyright (c) 2008-2024, PostgreSQL Global Development Group
* Copyright (c) 2012-2024, NIPPON TELEGRAPH AND TELEPHONE CORPORATION
*
* IDENTIFICATION
* pg_store_plans/pgsp_explain.c
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include "commands/explain.h"
#include "utils/rel.h"
#include "utils/lsyscache.h"
#include "utils/json.h"
#include "pgsp_explain.h"
static void pgspExplainOpenGroup(const char *objtype, const char *labelname,
bool labeled, ExplainState *es);
static void pgspExplainCloseGroup(const char *objtype, const char *labelname,
bool labeled, ExplainState *es);
static void report_triggers(ResultRelInfo *rInfo, bool show_relname,
ExplainState *es);
static void pgspExplainPropertyText(const char *qlabel, const char *value, ExplainState *es);
static void pgspExplainPropertyFloat(const char *qlabel, double value, int ndigits,
ExplainState *es);
static void pgspExplainProperty(const char *qlabel, const char *value, bool numeric,
ExplainState *es);
static void pgspExplainJSONLineEnding(ExplainState *es);
/*
* ExplainState is modified at 9.4.1 and 9.3.6. But the change is for
* internal use and to avoid binary-incompatibility not changing the
* size of ExplainState. So we can use ExplainState->extra as if it
* were grouping_stack safely and should do so. Using ->extra as List*
* discards the memory for ExplainStateExtra but it is not a problem
* since it is allocated by palloc.
*/
#if (PG_VERSION_NUM >= 90401 && PG_VERSION_NUM < 90500) || \
(PG_VERSION_NUM >= 90306 && PG_VERSION_NUM < 90400)
#define GROUPING_STACK(es) (*((List **)(&(es)->extra)))
#else
#define GROUPING_STACK(es) ((es)->grouping_stack)
#endif
/* ExplainInitState() is replaced with NewExlainState() in 9.5 */
#if PG_VERSION_NUM < 90500
ExplainState *
NewExplainState(void)
{
ExplainState *es = (ExplainState *)palloc0(sizeof(ExplainState));
ExplainInitState(es);
es->costs = true;
return es;
}
#endif
void
pgspExplainTriggers(ExplainState *es, QueryDesc *queryDesc)
{
if (es->analyze)
{
ResultRelInfo *rInfo;
bool show_relname;
#if PG_VERSION_NUM < 140000
int numrels = queryDesc->estate->es_num_result_relations;
int nr;
#else
List *resultrels;
List *routerels;
#endif
List *targrels = queryDesc->estate->es_trig_target_relations;
ListCell *l;
#if PG_VERSION_NUM >= 140000
resultrels = queryDesc->estate->es_opened_result_relations;
routerels = queryDesc->estate->es_tuple_routing_result_relations;
targrels = queryDesc->estate->es_trig_target_relations;
#endif
pgspExplainOpenGroup("Triggers", "Triggers", false, es);
#if PG_VERSION_NUM < 140000
show_relname = (numrels > 1 || targrels != NIL);
rInfo = queryDesc->estate->es_result_relations;
for (nr = 0; nr < numrels; rInfo++, nr++)
#else
show_relname = (list_length(resultrels) > 1 ||
routerels != NIL || targrels != NIL);
foreach(l, resultrels)
{
rInfo = (ResultRelInfo *) lfirst(l);
#endif
report_triggers(rInfo, show_relname, es);
#if PG_VERSION_NUM >= 140000
}
foreach(l, routerels)
{
rInfo = (ResultRelInfo *) lfirst(l);
report_triggers(rInfo, show_relname, es);
}
#endif
foreach(l, targrels)
{
rInfo = (ResultRelInfo *) lfirst(l);
report_triggers(rInfo, show_relname, es);
}
pgspExplainCloseGroup("Triggers", "Triggers", false, es);
}
}
static void
pgspExplainOpenGroup(const char *objtype, const char *labelname,
bool labeled, ExplainState *es)
{
pgspExplainJSONLineEnding(es);
appendStringInfoSpaces(es->str, 2 * es->indent);
if (labelname)
{
escape_json(es->str, labelname);
appendStringInfoString(es->str, ": ");
}
appendStringInfoChar(es->str, labeled ? '{' : '[');
GROUPING_STACK(es) = lcons_int(0, GROUPING_STACK(es));
es->indent++;
}
static void
pgspExplainCloseGroup(const char *objtype, const char *labelname,
bool labeled, ExplainState *es)
{
es->indent--;
appendStringInfoChar(es->str, '\n');
appendStringInfoSpaces(es->str, 2 * es->indent);
appendStringInfoChar(es->str, labeled ? '}' : ']');
GROUPING_STACK(es) = list_delete_first(GROUPING_STACK(es));
}
static void
report_triggers(ResultRelInfo *rInfo, bool show_relname, ExplainState *es)
{
int nt;
if (!rInfo->ri_TrigDesc || !rInfo->ri_TrigInstrument)
return;
for (nt = 0; nt < rInfo->ri_TrigDesc->numtriggers; nt++)
{
Trigger *trig = rInfo->ri_TrigDesc->triggers + nt;
Instrumentation *instr = rInfo->ri_TrigInstrument + nt;
char *relname;
char *conname = NULL;
/* Must clean up instrumentation state */
InstrEndLoop(instr);
/*
* We ignore triggers that were never invoked; they likely aren't
* relevant to the current query type.
*/
if (instr->ntuples == 0)
continue;
pgspExplainOpenGroup("Trigger", NULL, true, es);
relname = RelationGetRelationName(rInfo->ri_RelationDesc);
if (OidIsValid(trig->tgconstraint))
conname = get_constraint_name(trig->tgconstraint);
pgspExplainPropertyText("Trigger Name", trig->tgname, es);
if (conname)
pgspExplainPropertyText("Constraint Name", conname, es);
pgspExplainPropertyText("Relation", relname, es);
pgspExplainPropertyFloat("Time", 1000.0 * instr->total, 3, es);
pgspExplainPropertyFloat("Calls", instr->ntuples, 0, es);
if (conname)
pfree(conname);
pgspExplainCloseGroup("Trigger", NULL, true, es);
}
}
static void
pgspExplainPropertyText(const char *qlabel, const char *value, ExplainState *es)
{
pgspExplainProperty(qlabel, value, false, es);
}
static void
pgspExplainPropertyFloat(const char *qlabel, double value, int ndigits,
ExplainState *es)
{
char buf[256];
snprintf(buf, sizeof(buf), "%.*f", ndigits, value);
pgspExplainProperty(qlabel, buf, true, es);
}
static void
pgspExplainProperty(const char *qlabel, const char *value, bool numeric,
ExplainState *es)
{
pgspExplainJSONLineEnding(es);
appendStringInfoSpaces(es->str, es->indent * 2);
escape_json(es->str, qlabel);
appendStringInfoString(es->str, ": ");
if (numeric)
appendStringInfoString(es->str, value);
else
escape_json(es->str, value);
}
static void
pgspExplainJSONLineEnding(ExplainState *es)
{
Assert(es->format == EXPLAIN_FORMAT_JSON);
if (linitial_int(GROUPING_STACK(es)) != 0)
appendStringInfoChar(es->str, ',');
else
linitial_int(GROUPING_STACK(es)) = 1;
appendStringInfoChar(es->str, '\n');
}