Debian Codegraph @

contrib/pg_overexplain/sql/pg_overexplain.sql

raw
1 -- These tests display internal details that would not be stable under
2 -- debug_parallel_query, so make sure that option is disabled.
3 SET debug_parallel_query = off;
4
5 -- Make sure that we don't print any JIT-related information, as that
6 -- would also make results unstable.
7 SET jit = off;
8
9 -- These options do not exist, so these queries should all fail.
10 EXPLAIN (DEBUFF) SELECT 1;
11 EXPLAIN (DEBUG) SELECT 1;
12 EXPLAIN (RANGE_TABLE) SELECT 1;
13
14 -- Load the module that creates the options.
15 LOAD 'pg_overexplain';
16
17 -- The first option still does not exist, but the others do.
18 EXPLAIN (DEBUFF) SELECT 1;
19 EXPLAIN (DEBUG) SELECT 1;
20 EXPLAIN (RANGE_TABLE) SELECT 1;
21
22 -- Create a partitioned table.
23 CREATE TABLE vegetables (id serial, name text, genus text)
24 PARTITION BY LIST (genus);
25 CREATE TABLE daucus PARTITION OF vegetables FOR VALUES IN ('daucus');
26 CREATE TABLE brassica PARTITION OF vegetables FOR VALUES IN ('brassica');
27 INSERT INTO vegetables (name, genus)
28 VALUES ('carrot', 'daucus'), ('bok choy', 'brassica'),
29 ('brocooli', 'brassica'), ('cauliflower', 'brassica'),
30 ('cabbage', 'brassica'), ('kohlrabi', 'brassica'),
31 ('rutabaga', 'brassica'), ('turnip', 'brassica');
32 VACUUM ANALYZE vegetables;
33
34 -- We filter relation OIDs out of the test output in order to avoid
35 -- test instability. This is currently only needed for EXPLAIN (DEBUG), not
36 -- EXPLAIN (RANGE_TABLE). Also suppress actual row counts, which are not
37 -- stable (e.g. 1/8 is 0.12 on some buildfarm machines and 0.13 on others).
38 CREATE FUNCTION explain_filter(text) RETURNS SETOF text
39 LANGUAGE plpgsql AS
40 $$
41 DECLARE
42 ln text;
43 BEGIN
44 FOR ln IN EXECUTE $1
45 LOOP
46 ln := regexp_replace(ln, 'Relation OIDs:( \m\d+\M)+',
47 'Relation OIDs: NNN...', 'g');
48 ln := regexp_replace(ln, '<Relation-OIDs>( ?\m\d+\M)+</Relation-OIDs>',
49 '<Relation-OIDs>NNN...</Relation-OIDs>', 'g');
50 ln := regexp_replace(ln, 'actual rows=\d+\.\d+',
51 'actual rows=N.NN', 'g');
52 RETURN NEXT ln;
53 END LOOP;
54 END;
55 $$;
56
57 -- Test with both options together and an aggregate.
58 SELECT explain_filter($$
59 EXPLAIN (DEBUG, RANGE_TABLE, COSTS OFF)
60 SELECT genus, array_agg(name ORDER BY name) FROM vegetables GROUP BY genus
61 $$);
62
63 -- Test a different output format.
64 SELECT explain_filter($$
65 EXPLAIN (DEBUG, RANGE_TABLE, FORMAT XML, COSTS OFF)
66 SELECT genus, array_agg(name ORDER BY name) FROM vegetables GROUP BY genus
67 $$);
68
69 -- Test JSON format with RANGE_TABLE to verify valid JSON structure.
70 SELECT explain_filter($$
71 EXPLAIN (RANGE_TABLE, FORMAT JSON, COSTS OFF)
72 SELECT genus, array_agg(name ORDER BY name) FROM vegetables GROUP BY genus
73 $$);
74
75 -- Test just the DEBUG option. Verify that it shows information about
76 -- disabled nodes, parallel safety, and the parallelModeNeeded flag.
77 SET enable_seqscan = false;
78 SET debug_parallel_query = true;
79 SELECT explain_filter($$
80 EXPLAIN (DEBUG, COSTS OFF)
81 SELECT genus, array_agg(name ORDER BY name) FROM vegetables GROUP BY genus
82 $$);
83 SET debug_parallel_query = false;
84 RESET enable_seqscan;
85
86 -- Test the DEBUG option with a non-SELECT query, and also verify that the
87 -- hasReturning flag is shown.
88 SELECT explain_filter($$
89 EXPLAIN (DEBUG, COSTS OFF)
90 INSERT INTO vegetables (name, genus)
91 VALUES ('Brotero''s carrot', 'brassica') RETURNING id
92 $$);
93
94 -- Create an index, and then attempt to force a nested loop with inner index
95 -- scan so that we can see parameter-related information. Also, let's try
96 -- actually running the query, but try to suppress potentially variable output.
97 CREATE INDEX ON vegetables (id);
98 ANALYZE vegetables;
99 SET enable_hashjoin = false;
100 SET enable_material = false;
101 SET enable_mergejoin = false;
102 SET enable_seqscan = false;
103 SELECT explain_filter($$
104 EXPLAIN (BUFFERS OFF, COSTS OFF, SUMMARY OFF, TIMING OFF, ANALYZE, DEBUG)
105 SELECT * FROM vegetables v1, vegetables v2 WHERE v1.id = v2.id;
106 $$);
107 RESET enable_hashjoin;
108 RESET enable_material;
109 RESET enable_mergejoin;
110 RESET enable_seqscan;
111
112 -- Test the RANGE_TABLE option with a case that allows partition pruning.
113 EXPLAIN (RANGE_TABLE, COSTS OFF)
114 SELECT * FROM vegetables WHERE genus = 'daucus';
115
116 -- Also test a case that involves a write.
117 EXPLAIN (RANGE_TABLE, COSTS OFF)
118 INSERT INTO vegetables (name, genus) VALUES ('broccoflower', 'brassica');