The spot taken by the *-bucket row in the results of a query can vary from query to query (and sometimes for the same query issued before or after ANALYZE;!).
In a nutshell, the Sort plan node may or may not be pushed down by the planner, to a spot before the Agg / BucketScan nodes. If it is then the ORDER BY sorting will not impact it, meaning it will come first. In other circumstances, if the Sort node comes last, it is impacted by ORDER BY.
Example query where it didn't get pushed down:
SELECT age, count(*), diffix.is_suppress_bin(*) FROM customers GROUP BY 1 ORDER BY 1 NULLS FIRST
Sort
Sort Key: age
-> Custom Scan (BucketScan)
-> HashAggregate
Group Key: age
-> Seq Scan on customers
And where it did (because id has many unique columns per ANALYZE;, and the HashAggregate has been changed to Sort -> GroupAggregate:
prop_test=# explain SELECT id, count(*), diffix.is_suppress_bin(*) FROM customers GROUP BY 1 ORDER BY 1 NULLS LAST;
Custom Scan (BucketScan)
-> GroupAggregate
Group Key: id
-> Sort
Sort Key: id
-> Seq Scan on customers
Ideas from slack thread:
- ORDER BY volatile_func(id) where that function does nothing but is marked as VOLATILE
- Extract the ORDER BY clause from the query plan and insert the *-bucket conformant to this order. As in, whilst we emit the buckets, we "wait" for the first bucket which doesn't satisfy the sorting criteria, and then emit the *-bucket
prepend Append an ORDER BY is_suppress_bin(*) and let the planner figure it out
The spot taken by the *-bucket row in the results of a query can vary from query to query (and sometimes for the same query issued before or after
ANALYZE;!).In a nutshell, the
Sortplan node may or may not be pushed down by the planner, to a spot before theAgg/BucketScannodes. If it is then theORDER BYsorting will not impact it, meaning it will come first. In other circumstances, if theSortnode comes last, it is impacted byORDER BY.Example query where it didn't get pushed down:
And where it did (because
idhas many unique columns perANALYZE;, and theHashAggregatehas been changed toSort -> GroupAggregate:Ideas from slack thread:
prependAppend anORDER BY is_suppress_bin(*)and let the planner figure it out