Re: Calculation of unused columns - Mailing list pgsql-performance

From Tom Lane
Subject Re: Calculation of unused columns
Date
Msg-id 26771.1255824441@sss.pgh.pa.us
Whole thread Raw
In response to Calculation of unused columns  (Volker Grabsch <vog@notjusthosting.com>)
List pgsql-performance
Volker Grabsch <vog@notjusthosting.com> writes:
> I'm confused about the absence of a very simple optimization
> in PostgreSQL. Suppose we have a VIEW where some columns are
> expensive to be calculated:

>     CREATE VIEW a AS
>     SELECT
>         (... expensive calculation ...) as expensive,
>         count(*) as cheap
>     FROM
>         x;

> where "x" is a sufficiently large table. I would expect the
> following query to be very fast:

>     SELECT cheap FROM a;

> However, it takes the same time as "SELECT * FROM a;".

I think your main error is in supposing that count(*) is cheap ...

PG will suppress unused columns when a view can be flattened,
but typically not otherwise.  In particular a view involving
aggregates won't get flattened; but given that the aggregates
will force a whole-table scan anyway, I can't get that excited
about removing a subset of them.

            regards, tom lane

pgsql-performance by date:

Previous
From: Volker Grabsch
Date:
Subject: Calculation of unused columns
Next
From: Tom Lane
Date:
Subject: Re: Calculation of unused columns