@@ -1469,3 +1469,101 @@ explain (costs off) select * from t union select * from t order by 1,3;
1469
1469
(13 rows)
1470
1470
1471
1471
drop table t;
1472
+ -- Sort pushdown can't go below where expressions are part of the rel target.
1473
+ -- In particular this is interesting for volatile expressions which have to
1474
+ -- go above joins since otherwise we'll incorrectly use expression evaluations
1475
+ -- across multiple rows.
1476
+ set enable_hashagg=off;
1477
+ set enable_seqscan=off;
1478
+ set enable_incremental_sort = off;
1479
+ set parallel_tuple_cost=0;
1480
+ set parallel_setup_cost=0;
1481
+ set min_parallel_table_scan_size = 0;
1482
+ set min_parallel_index_scan_size = 0;
1483
+ -- Parallel sort below join.
1484
+ explain (costs off) select distinct sub.unique1, stringu1
1485
+ from tenk1, lateral (select tenk1.unique1 from generate_series(1, 1000)) as sub;
1486
+ QUERY PLAN
1487
+ --------------------------------------------------------------------------
1488
+ Unique
1489
+ -> Nested Loop
1490
+ -> Gather Merge
1491
+ Workers Planned: 2
1492
+ -> Sort
1493
+ Sort Key: tenk1.unique1, tenk1.stringu1
1494
+ -> Parallel Index Scan using tenk1_unique1 on tenk1
1495
+ -> Function Scan on generate_series
1496
+ (8 rows)
1497
+
1498
+ explain (costs off) select sub.unique1, stringu1
1499
+ from tenk1, lateral (select tenk1.unique1 from generate_series(1, 1000)) as sub
1500
+ order by 1, 2;
1501
+ QUERY PLAN
1502
+ --------------------------------------------------------------------
1503
+ Nested Loop
1504
+ -> Gather Merge
1505
+ Workers Planned: 2
1506
+ -> Sort
1507
+ Sort Key: tenk1.unique1, tenk1.stringu1
1508
+ -> Parallel Index Scan using tenk1_unique1 on tenk1
1509
+ -> Function Scan on generate_series
1510
+ (7 rows)
1511
+
1512
+ -- Parallel sort but with expression that can be safely generated at the base rel.
1513
+ explain (costs off) select distinct sub.unique1, md5(stringu1)
1514
+ from tenk1, lateral (select tenk1.unique1 from generate_series(1, 1000)) as sub;
1515
+ QUERY PLAN
1516
+ ----------------------------------------------------------------------------------------
1517
+ Unique
1518
+ -> Nested Loop
1519
+ -> Gather Merge
1520
+ Workers Planned: 2
1521
+ -> Sort
1522
+ Sort Key: tenk1.unique1, (md5((tenk1.stringu1)::text)) COLLATE "C"
1523
+ -> Parallel Index Scan using tenk1_unique1 on tenk1
1524
+ -> Function Scan on generate_series
1525
+ (8 rows)
1526
+
1527
+ explain (costs off) select sub.unique1, md5(stringu1)
1528
+ from tenk1, lateral (select tenk1.unique1 from generate_series(1, 1000)) as sub
1529
+ order by 1, 2;
1530
+ QUERY PLAN
1531
+ ----------------------------------------------------------------------------------
1532
+ Nested Loop
1533
+ -> Gather Merge
1534
+ Workers Planned: 2
1535
+ -> Sort
1536
+ Sort Key: tenk1.unique1, (md5((tenk1.stringu1)::text)) COLLATE "C"
1537
+ -> Parallel Index Scan using tenk1_unique1 on tenk1
1538
+ -> Function Scan on generate_series
1539
+ (7 rows)
1540
+
1541
+ -- Parallel sort but with expression not available until the upper rel.
1542
+ explain (costs off) select distinct sub.unique1, stringu1 || random()::text
1543
+ from tenk1, lateral (select tenk1.unique1 from generate_series(1, 1000)) as sub;
1544
+ QUERY PLAN
1545
+ ---------------------------------------------------------------------------------------------
1546
+ Unique
1547
+ -> Sort
1548
+ Sort Key: tenk1.unique1, (((tenk1.stringu1)::text || (random())::text)) COLLATE "C"
1549
+ -> Gather
1550
+ Workers Planned: 2
1551
+ -> Nested Loop
1552
+ -> Parallel Index Scan using tenk1_unique1 on tenk1
1553
+ -> Function Scan on generate_series
1554
+ (8 rows)
1555
+
1556
+ explain (costs off) select sub.unique1, stringu1 || random()::text
1557
+ from tenk1, lateral (select tenk1.unique1 from generate_series(1, 1000)) as sub
1558
+ order by 1, 2;
1559
+ QUERY PLAN
1560
+ ---------------------------------------------------------------------------------------
1561
+ Sort
1562
+ Sort Key: tenk1.unique1, (((tenk1.stringu1)::text || (random())::text)) COLLATE "C"
1563
+ -> Gather
1564
+ Workers Planned: 2
1565
+ -> Nested Loop
1566
+ -> Parallel Index Scan using tenk1_unique1 on tenk1
1567
+ -> Function Scan on generate_series
1568
+ (7 rows)
1569
+
0 commit comments