yoann
yoann

Reputation: 25

Query on partitioned table on PostgreSQL

I have a table partitioned by year. With this query :

select *
from myTable
where year between '2018' and '2020'

the query plan is good, only 2018,2019,2020 partitions are used.

But with this query

select *
from myTable
where year between '2018' and extract(year from now())::text

the partitions 2021 and 2022 appears in the query plan, why ?

Upvotes: 1

Views: 136

Answers (1)

Laurenz Albe
Laurenz Albe

Reputation: 246163

In PostgreSQL v10, partition pruning (removal of unneeded partitions) can only happen at query planning time, but at that time the value of extract(year from now())::text is not known.

If you use PostgreSQL v11 or later, your query should perform as expected.

Upvotes: 1

Related Questions