Postgresql – CTE where to place the where clause to filter rows sooner (in postgresql)

cteoptimizationpostgresql

I have function with CTE which looks like this:

function param(p_userId, p_templateId)

    with a(select...),
         b (select..), 
         c (insert..returning..), 
         d (update..returning..),
         ...k(select..)
    insert ... 
    select * from k 
    where  userId = p_userId 
    and    templateId = p_templateId

I'm able to put the where clause right at the begining in the a CTE. If I place the condition at the end like I have it now, will it filtered the rows out at the earliest or at the latest time? What is faster?

Basically what I do is, in the CTE's which appear sooner, like the a or b, (rather then k), I'm prefetching data or precalculating columns in order to use them later in the following CTEs.

Hope this question is still not too vague.

Best Answer

CTEs pose as optimization fences in Postgres.

Each CTE is planned, executed and materialized separately. The predicates of a later CTE are not visible in earlier CTEs. Place conditions to eliminate rows as early as possible (a or b in your example).

Or rewrite with subqueries to allow more aggressive optimizations in Postgres. (But you should still eliminate irrelevant rows as early as possible, optimization only goes so far ...)

Details in Craig's blog post: