1

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 ?

yoann
  • 25
  • 1
  • 6

1 Answers1

1

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.

Laurenz Albe
  • 209,280
  • 17
  • 206
  • 263