-- Test parallel DISTINCT SET parallel_tuple_cost=0; SET parallel_setup_cost=0; SET min_parallel_table_scan_size=0; SET max_parallel_workers_per_gather=2;
-- Ensure we get a parallel plan EXPLAIN (costs off) SELECTDISTINCT four FROM tenk1;
-- Ensure the parallel plan produces the correct results SELECTDISTINCT four FROM tenk1;
CREATEORREPLACE FUNCTION distinct_func(a INT) RETURNS INTAS $$
BEGIN RETURN a;
END;
$$ LANGUAGE plpgsql PARALLEL UNSAFE;
-- Ensure we don't do parallel distinct with a parallel unsafe function EXPLAIN (COSTS OFF) SELECTDISTINCT distinct_func(1) FROM tenk1;
-- make the function parallel safe CREATEORREPLACE FUNCTION distinct_func(a INT) RETURNS INTAS $$
BEGIN RETURN a;
END;
$$ LANGUAGE plpgsql PARALLEL SAFE;
-- Ensure we do parallel distinct now that the function is parallel safe EXPLAIN (COSTS OFF) SELECTDISTINCT distinct_func(1) FROM tenk1;
-- -- Test the planner's ability to use a LIMIT 1 instead of a Unique node when -- all of the distinct_pathkeys have been marked as redundant --
-- Ensure we get a plan with a Limit 1 EXPLAIN (COSTS OFF) SELECTDISTINCT four FROM tenk1 WHERE four = 0;
-- Ensure the above gives us the correct result SELECTDISTINCT four FROM tenk1 WHERE four = 0;
-- Ensure we get a plan with a Limit 1 EXPLAIN (COSTS OFF) SELECTDISTINCT four FROM tenk1 WHERE four = 0AND two <> 0;
-- Ensure no rows are returned SELECTDISTINCT four FROM tenk1 WHERE four = 0AND two <> 0;
-- Ensure we get a plan with a Limit 1 when the SELECT list contains constants EXPLAIN (COSTS OFF) SELECTDISTINCT four,1,2,3FROM tenk1 WHERE four = 0;
-- Ensure we only get 1 row SELECTDISTINCT four,1,2,3FROM tenk1 WHERE four = 0;
SET parallel_setup_cost=0; SET min_parallel_table_scan_size=0; SET max_parallel_workers_per_gather=2;
-- Ensure we get a plan with a Limit 1 in both partial distinct and final -- distinct EXPLAIN (COSTS OFF) SELECTDISTINCT four FROM tenk1 WHERE four = 10;
-- basic cases SELECT f1, f1 ISDISTINCTFROM2as"not 2"FROM disttable; SELECT f1, f1 ISDISTINCTFROMNULLas"not null"FROM disttable; SELECT f1, f1 ISDISTINCTFROM f1 as"false"FROM disttable; SELECT f1, f1 ISDISTINCTFROM f1+1as"not null"FROM disttable;
-- check that optimizer constant-folds it properly SELECT1ISDISTINCTFROM2as"yes"; SELECT2ISDISTINCTFROM2as"no"; SELECT2ISDISTINCTFROMnullas"yes"; SELECTnullISDISTINCTFROMnullas"no";
-- negated form SELECT1ISNOTDISTINCTFROM2as"no"; SELECT2ISNOTDISTINCTFROM2as"yes"; SELECT2ISNOTDISTINCTFROMnullas"no"; SELECTnullISNOTDISTINCTFROMnullas"yes";
-- -- Test the planner's ability to reorder the distinctClause Pathkeys to match -- the input path's ordering --
CREATETABLE distinct_tbl (x int, y int); INSERTINTO distinct_tbl SELECT i%10, i%10FROM generate_series(1, 1000) AS i; CREATEINDEX distinct_tbl_x_y_idx ON distinct_tbl (x, y); ANALYZE distinct_tbl;
-- Produce results with sorting. SET enable_hashagg TO OFF;
-- Ensure we avoid the need to re-sort by reordering the distinctClause -- Pathkeys to match the ordering of the input path EXPLAIN (COSTS OFF) SELECTDISTINCT y, x FROM distinct_tbl; SELECTDISTINCT y, x FROM distinct_tbl;
-- Ensure we leverage incremental-sort by reordering the distinctClause -- Pathkeys to partially match the ordering of the input path EXPLAIN (COSTS OFF) SELECTDISTINCT y, x FROM (SELECT * FROM distinct_tbl ORDERBY x) s; SELECTDISTINCT y, x FROM (SELECT * FROM distinct_tbl ORDERBY x) s;
-- Ensure we avoid the need to re-sort in partial distinct by reordering the -- distinctClause Pathkeys to match the ordering of the input path SET parallel_tuple_cost=0; SET parallel_setup_cost=0; SET min_parallel_table_scan_size=0; SET min_parallel_index_scan_size=0; SET max_parallel_workers_per_gather=2;
EXPLAIN (COSTS OFF) SELECTDISTINCT y, x FROM distinct_tbl limit10; SELECTDISTINCT y, x FROM distinct_tbl limit10;
-- Ensure we reorder the distinctClause Pathkeys to match the ordering of the -- input path even if there is ORDER BY clause EXPLAIN (COSTS OFF) SELECTDISTINCT y, x FROM distinct_tbl ORDERBY y; SELECTDISTINCT y, x FROM distinct_tbl ORDERBY y;
RESET enable_hashagg;
DROPTABLE distinct_tbl;
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.10 Sekunden
(vorverarbeitet am 2026-08-08)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.