Re: Performance issues with custom functions - Mailing list pgsql-performance

From Tom Lane
Subject Re: Performance issues with custom functions
Date
Msg-id 12437.1130368717@sss.pgh.pa.us
Whole thread Raw
In response to Performance issues with custom functions  ("Edward Di Geronimo Jr." <edigeronimo@xtracards.com>)
Responses Re: Performance issues with custom functions  ("Edward Di Geronimo Jr." <edigeronimo@xtracards.com>)
List pgsql-performance
"Edward Di Geronimo Jr." <edigeronimo@xtracards.com> writes:
> ... I'd like to know exactly what causes
> the bottleneck in the original query, and if there are other approaches
> to solving the issue in case I need them in future queries.

This is fairly hard to read ... it would help a lot if you had shown the
view definitions that the query relies on, so that we could match up the
plan elements with the query a bit better.

However, I'm thinking the problem is with this IN clause:

>         where pl.network_id in (select ns.network_id
>                         from development.network_state ns
>                           where ns.from_date < current_time
>                             and (ns.thru_date > current_time or
> ns.thru_date is null)
>                         and (ns.state_cd = pl.state_cd or ns.state_cd='')
>                           )

Because the sub-SELECT references pl.state_cd (an outer variable
reference), there's no chance of optimizing this into a join-style IN.
So the sub-SELECT has to be re-executed for each row of the outer query.

BTW, it's not apparent to me that your "flattened" query gives the same
answers as the original.  What if a pl row can join to more than one
row of the ns output?

            regards, tom lane

pgsql-performance by date:

Previous
From: Tom Lane
Date:
Subject: Re: Materializing a sequential scan
Next
From: "Steinar H. Gunderson"
Date:
Subject: Re: Materializing a sequential scan