BUG #17862: Overall query cost ignores window function
The Post Office <noreply@postgresql.org>
From: PG Bug reporting form <noreply@postgresql.org>
To: pgsql-bugs@lists.postgresql.org
Cc: tim3sp@gmail.com
Date: 2023-03-22T17:42:03Z
Lists: pgsql-bugs
The following bug has been logged on the website:
Bug reference: 17862
Logged by: Tim Palmer
Email address: tim3sp@gmail.com
PostgreSQL version: 15.2
Operating system: Debian
Description:
This query needs to read all of large_table to count the number of rows,
despite the LIMIT clause:
SELECT large_table.*, count(*) OVER ()
FROM generate_series(1, 1000000000000) large_table
LIMIT 10
I would have expected a query plan something like this, with a large overall
cost:
Limit (cost=0.00..22500000000.00 rows=10 width=16)
-> WindowAgg (cost=0.00..22500000000.00 rows=1000000000000 width=16)
-> Function Scan on generate_series large_table
(cost=0.00..10000000000.00 rows=1000000000000 width=8)
But I actually get this query plan, with a cost of 0.23:
Limit (cost=0.00..0.23 rows=10 width=16)
-> WindowAgg (cost=0.00..22500000000.00 rows=1000000000000 width=16)
-> Function Scan on generate_series large_table
(cost=0.00..10000000000.00 rows=1000000000000 width=8)
I believe this (on a more complicated query) is affecting the plan chosen by
the optimizer.
Commits
-
Account for startup rows when costing WindowAggs
- 3900a02c97c7 17.0 landed