--
|
-- one_c_planner_or_sort
|
--
|
|
SET client_min_messages = warning;
|
|
DROP SCHEMA IF EXISTS one_c_or_sort CASCADE;
|
CREATE SCHEMA one_c_or_sort;
|
SET search_path = one_c_or_sort, public;
|
|
CREATE FUNCTION explain_root(query_text text)
|
RETURNS jsonb
|
LANGUAGE plpgsql
|
AS $$
|
DECLARE
|
plan_doc jsonb;
|
BEGIN
|
EXECUTE 'EXPLAIN (FORMAT JSON) ' || query_text INTO plan_doc;
|
RETURN plan_doc -> 0 -> 'Plan';
|
END;
|
$$;
|
|
CREATE FUNCTION explain_analyze_root(query_text text)
|
RETURNS jsonb
|
LANGUAGE plpgsql
|
AS $$
|
DECLARE
|
plan_doc jsonb;
|
BEGIN
|
EXECUTE 'EXPLAIN (ANALYZE, COSTS ON, TIMING OFF, SUMMARY OFF, FORMAT JSON) '
|
|| query_text
|
INTO plan_doc;
|
RETURN plan_doc -> 0 -> 'Plan';
|
END;
|
$$;
|
|
CREATE FUNCTION count_node_type(plan jsonb, wanted text)
|
RETURNS integer
|
LANGUAGE sql
|
AS $$
|
WITH RECURSIVE nodes(node) AS (
|
SELECT plan
|
UNION ALL
|
SELECT child.node
|
FROM nodes,
|
LATERAL jsonb_array_elements(COALESCE(nodes.node -> 'Plans',
|
'[]'::jsonb)) AS child(node)
|
)
|
SELECT count(*)::integer
|
FROM nodes
|
WHERE node ->> 'Node Type' = wanted;
|
$$;
|
|
CREATE FUNCTION first_node(plan jsonb, wanted text)
|
RETURNS jsonb
|
LANGUAGE sql
|
AS $$
|
WITH RECURSIVE nodes(node) AS (
|
SELECT plan
|
UNION ALL
|
SELECT child.node
|
FROM nodes,
|
LATERAL jsonb_array_elements(COALESCE(nodes.node -> 'Plans',
|
'[]'::jsonb)) AS child(node)
|
)
|
SELECT node
|
FROM nodes
|
WHERE node ->> 'Node Type' = wanted
|
LIMIT 1;
|
$$;
|
|
CREATE FUNCTION nth_child(plan jsonb, idx integer)
|
RETURNS jsonb
|
LANGUAGE sql
|
IMMUTABLE
|
AS $$
|
SELECT plan -> 'Plans' -> (idx - 1);
|
$$;
|
|
SET max_parallel_workers_per_gather = 0;
|
|
CREATE TABLE ordered_or_t(
|
x integer,
|
y integer,
|
payload text
|
)
|
WITH (autovacuum_enabled = false);
|
|
INSERT INTO ordered_or_t
|
SELECT
|
g % 100,
|
g % 1000,
|
md5(g::text)
|
FROM generate_series(1, 200000) AS g;
|
|
CREATE INDEX ordered_or_xy_idx
|
ON ordered_or_t(x, y);
|
|
ANALYZE ordered_or_t;
|
|
SET enable_bitmapscan = off;
|
SET enable_seqscan = off;
|
|
DO $$
|
DECLARE
|
actual_count integer;
|
asc_result_count integer := 0;
|
desc_result_count integer := 0;
|
range_asc_result_count integer := 0;
|
range_desc_result_count integer := 0;
|
distinct_count integer;
|
asc_plan jsonb;
|
desc_plan jsonb;
|
range_asc_plan jsonb;
|
range_desc_plan jsonb;
|
overlap_plan jsonb;
|
child1 jsonb;
|
child2 jsonb;
|
prev_x integer;
|
prev_y integer;
|
row_record record;
|
BEGIN
|
SELECT count(*)
|
INTO actual_count
|
FROM one_c_or_sort.ordered_or_t
|
WHERE (x = 5 AND y > 900)
|
OR x > 96;
|
|
IF actual_count <> 6200 THEN
|
RAISE EXCEPTION 'positive ordered-OR result mismatch: got %, expected 6200',
|
actual_count;
|
END IF;
|
|
asc_plan := one_c_or_sort.explain_analyze_root($sql$
|
SELECT *
|
FROM one_c_or_sort.ordered_or_t
|
WHERE (x = 5 AND y > 900)
|
OR x > 96
|
ORDER BY x, y
|
$sql$);
|
|
IF asc_plan ->> 'Node Type' <> 'Append' THEN
|
RAISE EXCEPTION 'positive ASC root node mismatch: got %, expected Append',
|
asc_plan ->> 'Node Type';
|
END IF;
|
|
IF one_c_or_sort.count_node_type(asc_plan, 'Append') <> 1 THEN
|
RAISE EXCEPTION 'positive ASC Append count mismatch';
|
END IF;
|
|
IF one_c_or_sort.count_node_type(asc_plan, 'Index Scan') <> 2 THEN
|
RAISE EXCEPTION 'positive ASC Index Scan count mismatch';
|
END IF;
|
|
IF one_c_or_sort.count_node_type(asc_plan, 'Sort') <> 0 THEN
|
RAISE EXCEPTION 'positive ASC unexpectedly contains Sort';
|
END IF;
|
|
IF one_c_or_sort.count_node_type(asc_plan, 'Incremental Sort') <> 0 THEN
|
RAISE EXCEPTION 'positive ASC unexpectedly contains Incremental Sort';
|
END IF;
|
|
IF (asc_plan ->> 'Actual Rows')::numeric <> 6200 THEN
|
RAISE EXCEPTION 'positive ASC actual rows mismatch: got %, expected 6200',
|
(asc_plan ->> 'Actual Rows')::numeric;
|
END IF;
|
|
child1 := one_c_or_sort.nth_child(asc_plan, 1);
|
child2 := one_c_or_sort.nth_child(asc_plan, 2);
|
|
IF child1 ->> 'Node Type' <> 'Index Scan'
|
OR child2 ->> 'Node Type' <> 'Index Scan' THEN
|
RAISE EXCEPTION 'positive ASC child node mismatch: expected two Index Scan nodes';
|
END IF;
|
|
IF child1 ->> 'Index Name' <> 'ordered_or_xy_idx'
|
OR child2 ->> 'Index Name' <> 'ordered_or_xy_idx' THEN
|
RAISE EXCEPTION 'positive ASC index name mismatch';
|
END IF;
|
|
IF child1 ->> 'Scan Direction' <> 'Forward'
|
OR child2 ->> 'Scan Direction' <> 'Forward' THEN
|
RAISE EXCEPTION 'positive ASC scan direction mismatch';
|
END IF;
|
|
IF (child1 ->> 'Actual Rows')::numeric <> 200
|
OR (child2 ->> 'Actual Rows')::numeric <> 6000 THEN
|
RAISE EXCEPTION 'positive ASC child row order mismatch: child1 %, child2 %',
|
(child1 ->> 'Actual Rows')::numeric,
|
(child2 ->> 'Actual Rows')::numeric;
|
END IF;
|
|
prev_x := NULL;
|
prev_y := NULL;
|
|
FOR row_record IN
|
SELECT x, y
|
FROM one_c_or_sort.ordered_or_t
|
WHERE (x = 5 AND y > 900)
|
OR x > 96
|
ORDER BY x, y
|
LOOP
|
asc_result_count := asc_result_count + 1;
|
|
IF prev_x IS NOT NULL
|
AND (prev_x > row_record.x
|
OR (prev_x = row_record.x AND prev_y > row_record.y)) THEN
|
RAISE EXCEPTION 'positive ASC emitted rows out of order at row %: previous (%,%), current (%,%)',
|
asc_result_count, prev_x, prev_y, row_record.x, row_record.y;
|
END IF;
|
|
prev_x := row_record.x;
|
prev_y := row_record.y;
|
END LOOP;
|
|
IF asc_result_count <> 6200 THEN
|
RAISE EXCEPTION 'positive ASC emitted row count mismatch: got %, expected 6200',
|
asc_result_count;
|
END IF;
|
|
desc_plan := one_c_or_sort.explain_analyze_root($sql$
|
SELECT *
|
FROM one_c_or_sort.ordered_or_t
|
WHERE (x = 5 AND y > 900)
|
OR x > 96
|
ORDER BY x DESC, y DESC
|
$sql$);
|
|
IF desc_plan ->> 'Node Type' <> 'Append' THEN
|
RAISE EXCEPTION 'positive DESC root node mismatch: got %, expected Append',
|
desc_plan ->> 'Node Type';
|
END IF;
|
|
IF one_c_or_sort.count_node_type(desc_plan, 'Append') <> 1 THEN
|
RAISE EXCEPTION 'positive DESC Append count mismatch';
|
END IF;
|
|
IF one_c_or_sort.count_node_type(desc_plan, 'Index Scan') <> 2 THEN
|
RAISE EXCEPTION 'positive DESC Index Scan count mismatch';
|
END IF;
|
|
IF one_c_or_sort.count_node_type(desc_plan, 'Sort') <> 0 THEN
|
RAISE EXCEPTION 'positive DESC unexpectedly contains Sort';
|
END IF;
|
|
IF one_c_or_sort.count_node_type(desc_plan, 'Incremental Sort') <> 0 THEN
|
RAISE EXCEPTION 'positive DESC unexpectedly contains Incremental Sort';
|
END IF;
|
|
IF (desc_plan ->> 'Actual Rows')::numeric <> 6200 THEN
|
RAISE EXCEPTION 'positive DESC actual rows mismatch: got %, expected 6200',
|
(desc_plan ->> 'Actual Rows')::numeric;
|
END IF;
|
|
child1 := one_c_or_sort.nth_child(desc_plan, 1);
|
child2 := one_c_or_sort.nth_child(desc_plan, 2);
|
|
IF child1 ->> 'Node Type' <> 'Index Scan'
|
OR child2 ->> 'Node Type' <> 'Index Scan' THEN
|
RAISE EXCEPTION 'positive DESC child node mismatch: expected two Index Scan nodes';
|
END IF;
|
|
IF child1 ->> 'Index Name' <> 'ordered_or_xy_idx'
|
OR child2 ->> 'Index Name' <> 'ordered_or_xy_idx' THEN
|
RAISE EXCEPTION 'positive DESC index name mismatch';
|
END IF;
|
|
IF child1 ->> 'Scan Direction' <> 'Backward'
|
OR child2 ->> 'Scan Direction' <> 'Backward' THEN
|
RAISE EXCEPTION 'positive DESC scan direction mismatch';
|
END IF;
|
|
IF (child1 ->> 'Actual Rows')::numeric <> 6000
|
OR (child2 ->> 'Actual Rows')::numeric <> 200 THEN
|
RAISE EXCEPTION 'positive DESC child row order mismatch: child1 %, child2 %',
|
(child1 ->> 'Actual Rows')::numeric,
|
(child2 ->> 'Actual Rows')::numeric;
|
END IF;
|
|
prev_x := NULL;
|
prev_y := NULL;
|
|
FOR row_record IN
|
SELECT x, y
|
FROM one_c_or_sort.ordered_or_t
|
WHERE (x = 5 AND y > 900)
|
OR x > 96
|
ORDER BY x DESC, y DESC
|
LOOP
|
desc_result_count := desc_result_count + 1;
|
|
IF prev_x IS NOT NULL
|
AND (prev_x < row_record.x
|
OR (prev_x = row_record.x AND prev_y < row_record.y)) THEN
|
RAISE EXCEPTION 'positive DESC emitted rows out of order at row %: previous (%,%), current (%,%)',
|
desc_result_count, prev_x, prev_y, row_record.x, row_record.y;
|
END IF;
|
|
prev_x := row_record.x;
|
prev_y := row_record.y;
|
END LOOP;
|
|
IF desc_result_count <> 6200 THEN
|
RAISE EXCEPTION 'positive DESC emitted row count mismatch: got %, expected 6200',
|
desc_result_count;
|
END IF;
|
|
range_asc_plan := one_c_or_sort.explain_analyze_root($sql$
|
SELECT *
|
FROM one_c_or_sort.ordered_or_t
|
WHERE x < 1
|
OR x >= 99
|
ORDER BY x, y
|
$sql$);
|
|
IF range_asc_plan ->> 'Node Type' <> 'Append' THEN
|
RAISE EXCEPTION 'range ASC root node mismatch: got %, expected Append',
|
range_asc_plan ->> 'Node Type';
|
END IF;
|
|
IF one_c_or_sort.count_node_type(range_asc_plan, 'Index Scan') <> 2 THEN
|
RAISE EXCEPTION 'range ASC Index Scan count mismatch';
|
END IF;
|
|
IF jsonb_array_length(range_asc_plan -> 'Plans') <> 2 THEN
|
RAISE EXCEPTION 'range ASC child count mismatch';
|
END IF;
|
|
IF one_c_or_sort.count_node_type(range_asc_plan, 'Sort') <> 0
|
OR one_c_or_sort.count_node_type(range_asc_plan, 'Incremental Sort') <> 0 THEN
|
RAISE EXCEPTION 'range ASC unexpectedly contains a Sort';
|
END IF;
|
|
child1 := one_c_or_sort.nth_child(range_asc_plan, 1);
|
child2 := one_c_or_sort.nth_child(range_asc_plan, 2);
|
|
IF child1 ->> 'Node Type' <> 'Index Scan'
|
OR child2 ->> 'Node Type' <> 'Index Scan'
|
OR child1 ->> 'Index Name' <> 'ordered_or_xy_idx'
|
OR child2 ->> 'Index Name' <> 'ordered_or_xy_idx'
|
OR child1 ->> 'Scan Direction' <> 'Forward'
|
OR child2 ->> 'Scan Direction' <> 'Forward' THEN
|
RAISE EXCEPTION 'range ASC child node, index, or scan direction mismatch';
|
END IF;
|
|
IF child1 ->> 'Index Cond' <> '(x < 1)'
|
OR child2 ->> 'Index Cond' <> '(x >= 99)' THEN
|
RAISE EXCEPTION 'range ASC child order mismatch: child1 %, child2 %',
|
child1 ->> 'Index Cond', child2 ->> 'Index Cond';
|
END IF;
|
|
prev_x := NULL;
|
prev_y := NULL;
|
|
FOR row_record IN
|
SELECT x, y
|
FROM one_c_or_sort.ordered_or_t
|
WHERE x < 1
|
OR x >= 99
|
ORDER BY x, y
|
LOOP
|
range_asc_result_count := range_asc_result_count + 1;
|
|
IF prev_x IS NOT NULL
|
AND (prev_x > row_record.x
|
OR (prev_x = row_record.x AND prev_y > row_record.y)) THEN
|
RAISE EXCEPTION 'range ASC emitted rows out of order at row %: previous (%,%), current (%,%)',
|
range_asc_result_count, prev_x, prev_y, row_record.x, row_record.y;
|
END IF;
|
|
prev_x := row_record.x;
|
prev_y := row_record.y;
|
END LOOP;
|
|
IF range_asc_result_count <> 4000 THEN
|
RAISE EXCEPTION 'range ASC emitted row count mismatch: got %, expected 4000',
|
range_asc_result_count;
|
END IF;
|
|
range_desc_plan := one_c_or_sort.explain_analyze_root($sql$
|
SELECT *
|
FROM one_c_or_sort.ordered_or_t
|
WHERE 1 > x
|
OR 99 <= x
|
ORDER BY x DESC, y DESC
|
$sql$);
|
|
IF range_desc_plan ->> 'Node Type' <> 'Append' THEN
|
RAISE EXCEPTION 'range DESC root node mismatch: got %, expected Append',
|
range_desc_plan ->> 'Node Type';
|
END IF;
|
|
IF one_c_or_sort.count_node_type(range_desc_plan, 'Index Scan') <> 2 THEN
|
RAISE EXCEPTION 'range DESC Index Scan count mismatch';
|
END IF;
|
|
IF jsonb_array_length(range_desc_plan -> 'Plans') <> 2 THEN
|
RAISE EXCEPTION 'range DESC child count mismatch';
|
END IF;
|
|
IF one_c_or_sort.count_node_type(range_desc_plan, 'Sort') <> 0
|
OR one_c_or_sort.count_node_type(range_desc_plan, 'Incremental Sort') <> 0 THEN
|
RAISE EXCEPTION 'range DESC unexpectedly contains a Sort';
|
END IF;
|
|
child1 := one_c_or_sort.nth_child(range_desc_plan, 1);
|
child2 := one_c_or_sort.nth_child(range_desc_plan, 2);
|
|
IF child1 ->> 'Node Type' <> 'Index Scan'
|
OR child2 ->> 'Node Type' <> 'Index Scan'
|
OR child1 ->> 'Index Name' <> 'ordered_or_xy_idx'
|
OR child2 ->> 'Index Name' <> 'ordered_or_xy_idx'
|
OR child1 ->> 'Scan Direction' <> 'Backward'
|
OR child2 ->> 'Scan Direction' <> 'Backward' THEN
|
RAISE EXCEPTION 'range DESC child node, index, or scan direction mismatch';
|
END IF;
|
|
IF child1 ->> 'Index Cond' <> '(x >= 99)'
|
OR child2 ->> 'Index Cond' <> '(x < 1)' THEN
|
RAISE EXCEPTION 'range DESC child order mismatch: child1 %, child2 %',
|
child1 ->> 'Index Cond', child2 ->> 'Index Cond';
|
END IF;
|
|
prev_x := NULL;
|
prev_y := NULL;
|
|
FOR row_record IN
|
SELECT x, y
|
FROM one_c_or_sort.ordered_or_t
|
WHERE 1 > x
|
OR 99 <= x
|
ORDER BY x DESC, y DESC
|
LOOP
|
range_desc_result_count := range_desc_result_count + 1;
|
|
IF prev_x IS NOT NULL
|
AND (prev_x < row_record.x
|
OR (prev_x = row_record.x AND prev_y < row_record.y)) THEN
|
RAISE EXCEPTION 'range DESC emitted rows out of order at row %: previous (%,%), current (%,%)',
|
range_desc_result_count, prev_x, prev_y, row_record.x, row_record.y;
|
END IF;
|
|
prev_x := row_record.x;
|
prev_y := row_record.y;
|
END LOOP;
|
|
IF range_desc_result_count <> 4000 THEN
|
RAISE EXCEPTION 'range DESC emitted row count mismatch: got %, expected 4000',
|
range_desc_result_count;
|
END IF;
|
|
overlap_plan := one_c_or_sort.explain_analyze_root($sql$
|
SELECT *
|
FROM one_c_or_sort.ordered_or_t
|
WHERE (x = 5 AND y > 900)
|
OR x > 4
|
ORDER BY x, y
|
$sql$);
|
|
IF one_c_or_sort.count_node_type(overlap_plan, 'Append') <> 0 THEN
|
RAISE EXCEPTION 'overlap-negative unexpectedly selected special Append';
|
END IF;
|
|
SELECT count(*)
|
INTO actual_count
|
FROM one_c_or_sort.ordered_or_t
|
WHERE x > 96
|
OR x > 98;
|
|
SELECT count(*)
|
INTO distinct_count
|
FROM (
|
SELECT DISTINCT x, y, payload
|
FROM one_c_or_sort.ordered_or_t
|
WHERE x > 96
|
OR x > 98
|
) AS deduped;
|
|
IF actual_count <> 6000 THEN
|
RAISE EXCEPTION 'contained ordered-OR result mismatch: got %, expected 6000',
|
actual_count;
|
END IF;
|
|
IF distinct_count <> actual_count THEN
|
RAISE EXCEPTION 'contained ordered-OR produced duplicates: count %, distinct %',
|
actual_count, distinct_count;
|
END IF;
|
END;
|
$$;
|
|
SET enable_bitmapscan = on;
|
SET enable_seqscan = on;
|
SET enable_incremental_sort = off;
|
SET work_mem = '256MB';
|
|
CREATE TABLE sort_cost_t(
|
unique_integer integer,
|
low_cardinality_integer integer,
|
wide_text text
|
)
|
WITH (autovacuum_enabled = false);
|
|
INSERT INTO sort_cost_t
|
SELECT
|
g,
|
g % 10,
|
repeat(md5(g::text), 16)
|
FROM generate_series(1, 50000) AS g;
|
|
ANALYZE sort_cost_t;
|
|
DO $$
|
DECLARE
|
int_sort jsonb;
|
text_sort jsonb;
|
unique_first_sort jsonb;
|
low_first_sort jsonb;
|
int_startup numeric;
|
text_startup numeric;
|
unique_first_startup numeric;
|
low_first_startup numeric;
|
BEGIN
|
int_sort := one_c_or_sort.first_node(
|
one_c_or_sort.explain_root($sql$
|
SELECT unique_integer
|
FROM one_c_or_sort.sort_cost_t
|
ORDER BY unique_integer
|
$sql$),
|
'Sort');
|
|
text_sort := one_c_or_sort.first_node(
|
one_c_or_sort.explain_root($sql$
|
SELECT wide_text
|
FROM one_c_or_sort.sort_cost_t
|
ORDER BY wide_text
|
$sql$),
|
'Sort');
|
|
IF int_sort IS NULL OR text_sort IS NULL THEN
|
RAISE EXCEPTION 'expected Sort nodes for single-column sort-cost checks';
|
END IF;
|
|
int_startup := (int_sort ->> 'Startup Cost')::numeric;
|
text_startup := (text_sort ->> 'Startup Cost')::numeric;
|
|
IF text_startup <= int_startup THEN
|
RAISE EXCEPTION 'wide text sort startup cost mismatch: int %, text %',
|
int_startup, text_startup;
|
END IF;
|
|
IF text_startup / int_startup <= 1.05 THEN
|
RAISE EXCEPTION 'wide text sort ratio too small: int %, text %, ratio %',
|
int_startup, text_startup, text_startup / int_startup;
|
END IF;
|
|
unique_first_sort := one_c_or_sort.first_node(
|
one_c_or_sort.explain_root($sql$
|
SELECT unique_integer, wide_text
|
FROM one_c_or_sort.sort_cost_t
|
ORDER BY unique_integer, wide_text
|
$sql$),
|
'Sort');
|
|
low_first_sort := one_c_or_sort.first_node(
|
one_c_or_sort.explain_root($sql$
|
SELECT low_cardinality_integer, wide_text
|
FROM one_c_or_sort.sort_cost_t
|
ORDER BY low_cardinality_integer, wide_text
|
$sql$),
|
'Sort');
|
|
IF unique_first_sort IS NULL OR low_first_sort IS NULL THEN
|
RAISE EXCEPTION 'expected Sort nodes for multi-key sort-cost checks';
|
END IF;
|
|
unique_first_startup := (unique_first_sort ->> 'Startup Cost')::numeric;
|
low_first_startup := (low_first_sort ->> 'Startup Cost')::numeric;
|
|
IF low_first_startup <= unique_first_startup THEN
|
RAISE EXCEPTION 'comparison-frequency sort startup cost mismatch: unique-first %, low-cardinality-first %',
|
unique_first_startup, low_first_startup;
|
END IF;
|
END;
|
$$;
|
|
CREATE TABLE merge_left_t(
|
k integer,
|
v integer
|
)
|
WITH (autovacuum_enabled = false);
|
|
CREATE TABLE merge_right_t(
|
k integer,
|
v integer
|
)
|
WITH (autovacuum_enabled = false);
|
|
INSERT INTO merge_left_t
|
SELECT g, g % 11
|
FROM generate_series(1, 500) AS g;
|
|
INSERT INTO merge_right_t
|
SELECT g, g % 13
|
FROM generate_series(1, 500) AS g;
|
|
ANALYZE merge_left_t;
|
ANALYZE merge_right_t;
|
|
DO $$
|
DECLARE
|
prev_hashjoin text;
|
prev_mergejoin text;
|
prev_nestloop text;
|
prev_seqscan text;
|
unsorted_plan jsonb;
|
ordered_plan jsonb;
|
full_plan jsonb;
|
BEGIN
|
prev_hashjoin := current_setting('enable_hashjoin');
|
prev_mergejoin := current_setting('enable_mergejoin');
|
prev_nestloop := current_setting('enable_nestloop');
|
prev_seqscan := current_setting('enable_seqscan');
|
|
PERFORM set_config('enable_nestloop', 'off', false);
|
PERFORM set_config('enable_hashjoin', 'on', false);
|
PERFORM set_config('enable_mergejoin', 'on', false);
|
|
unsorted_plan := one_c_or_sort.explain_root($sql$
|
SELECT *
|
FROM one_c_or_sort.merge_left_t l
|
JOIN one_c_or_sort.merge_right_t r
|
ON l.k = r.k
|
$sql$);
|
|
IF one_c_or_sort.count_node_type(unsorted_plan, 'Hash Join') < 1 THEN
|
RAISE EXCEPTION 'unsorted INNER JOIN did not produce Hash Join';
|
END IF;
|
|
IF one_c_or_sort.count_node_type(unsorted_plan, 'Merge Join') <> 0 THEN
|
RAISE EXCEPTION 'unsorted INNER JOIN unexpectedly produced Merge Join';
|
END IF;
|
|
CREATE INDEX merge_left_k_idx ON one_c_or_sort.merge_left_t(k);
|
CREATE INDEX merge_right_k_idx ON one_c_or_sort.merge_right_t(k);
|
ANALYZE one_c_or_sort.merge_left_t;
|
ANALYZE one_c_or_sort.merge_right_t;
|
|
PERFORM set_config('enable_hashjoin', 'off', false);
|
PERFORM set_config('enable_nestloop', 'off', false);
|
PERFORM set_config('enable_mergejoin', 'on', false);
|
PERFORM set_config('enable_seqscan', 'off', false);
|
|
ordered_plan := one_c_or_sort.explain_root($sql$
|
SELECT *
|
FROM one_c_or_sort.merge_left_t l
|
JOIN one_c_or_sort.merge_right_t r
|
ON l.k = r.k
|
$sql$);
|
|
IF one_c_or_sort.count_node_type(ordered_plan, 'Merge Join') < 1 THEN
|
RAISE EXCEPTION 'ordered INNER JOIN did not produce Merge Join';
|
END IF;
|
|
IF one_c_or_sort.count_node_type(ordered_plan, 'Sort') <> 0 THEN
|
RAISE EXCEPTION 'ordered INNER JOIN unexpectedly contains Sort under Merge Join';
|
END IF;
|
|
PERFORM set_config('enable_hashjoin', 'off', false);
|
PERFORM set_config('enable_nestloop', 'off', false);
|
PERFORM set_config('enable_mergejoin', 'on', false);
|
PERFORM set_config('enable_seqscan', 'on', false);
|
|
full_plan := one_c_or_sort.explain_root($sql$
|
SELECT *
|
FROM one_c_or_sort.merge_left_t l
|
FULL JOIN one_c_or_sort.merge_right_t r
|
ON l.v = r.v
|
$sql$);
|
|
IF one_c_or_sort.count_node_type(full_plan, 'Merge Join') < 1 THEN
|
RAISE EXCEPTION 'FULL JOIN did not preserve Merge Join exception';
|
END IF;
|
|
PERFORM set_config('enable_hashjoin', prev_hashjoin, false);
|
PERFORM set_config('enable_mergejoin', prev_mergejoin, false);
|
PERFORM set_config('enable_nestloop', prev_nestloop, false);
|
PERFORM set_config('enable_seqscan', prev_seqscan, false);
|
END;
|
$$;
|
|
DROP SCHEMA one_c_or_sort CASCADE;
|