Re: Want function to be called only once in query

From: Jim Mlodgenski <jimmy76(at)gmail(dot)com>
To: Steve Pritchard <steve(dot)pritchard(at)bto(dot)org>
Cc: Pgsql Performance <pgsql-performance(at)lists(dot)postgresql(dot)org>
Subject: Re: Want function to be called only once in query
Date: 2021-09-16 10:56:05
Message-ID: CAB_5SRee1qAo9U3smCMxezHWi-w+1=1sHk_q2Hcnt5LbgxO9jA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

On Thu, Sep 16, 2021 at 4:51 AM Steve Pritchard <steve(dot)pritchard(at)bto(dot)org> wrote:
>
> I have a PL/pgSQL function that I want to call within a query, but the function is fairly expensive to execute so I only want it executed once within the query. However the planner seems to reorganize my query so that it calls the function for every row.
>
> We were previously on Pg 9.6 and this wasn't a problem then. But now that we have upgraded to Pg 13, the behaviour has changed.
>

The behavior for planning a CTE changed in PG12.

> There must be a "proper" way to get the planner to call a function only once.
>
Add the MATERIALIZED keyword to the WITH statement

In response to

Responses

Browse pgsql-performance by date

  From Date Subject
Next Message Steve Pritchard 2021-09-16 11:07:53 Re: Want function to be called only once in query
Previous Message Steve Pritchard 2021-09-16 08:51:31 Want function to be called only once in query