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.