From: Mladen Gogala <gogala.mladen@gmail.com>
To: pgsql-general@lists.postgresql.org
Subject: Re: Lazy View's Column Computing
Date: Mon, 2 Aug 2021 19:27:02 -0400
Message-ID: <65c04e3a-ca63-166c-8689-9c8cd89d972f@gmail.com> (raw)
In-Reply-To: <DB9PR07MB7180094FC2775BFD3A61CC0DCBEF9@DB9PR07MB7180.eurprd07.prod.outlook.com>
References: <DB9PR07MB7180094FC2775BFD3A61CC0DCBEF9@DB9PR07MB7180.eurprd07.prod.outlook.com>
For all that we know, it may already be happening. That looks like a
pretty reasonable optimization which may already be in place. If we
create a view:
mgogala=# select * from dept;
deptno | dname | loc
--------+------------+----------
10 | ACCOUNTING | NEW YORK
20 | RESEARCH | DALLAS
30 | SALES | CHICAGO
40 | OPERATIONS | BOSTON
(4 rows)
mgogala=# create view acct_view as select * from emp where deptno=10;
CREATE VIEW
The query from the view would probably merge view with the original and
optimize everything as a single query. Unfortunately, there is no way to
tell:
mgogala=# explain select ename,job,sal from acct_view;
QUERY PLAN
----------------------------------------------------
Seq Scan on emp (cost=0.00..1.18 rows=3 width=21)
Filter: (deptno = 10)
(2 rows)
The only tool that you have at your disposal is EXPLAIN. What we need to
ascertain that assumption is an optimizer trace file detailing the
decisions made by optimizer, something like the event 10053 from another
database which will remain unnamed. Merging the view query into the top
level query would produce something like this:
mgogala=# select ename,job,sal from emp
mgogala-# where deptno=10;
ename | job | sal
--------+-----------+------
CLARK | MANAGER | 2450
KING | PRESIDENT | 5000
MILLER | CLERK | 1300
(3 rows)
The table, shown below, has more columns than the 3 used in the above query:
mgogala=# \d emp
Table "mgogala.emp"
Column | Type | Collation | Nullable | Default
----------+-----------------------------+-----------+----------+---------
empno | smallint | | not null |
ename | character varying(10) | | |
job | character varying(9) | | |
mgr | smallint | | |
hiredate | timestamp without time zone | | |
sal | double precision | | |
comm | double precision | | |
deptno | smallint | | |
Indexes:
"emp_pkey" PRIMARY KEY, btree (empno)
Foreign-key constraints:
"fk_deptno" FOREIGN KEY (deptno) REFERENCES dept(deptno)
Merging the top level query with the view query would be smart tactic
which is probably already deployed. However, it is not possible to tell
with the tools at hand. That is what you want: the query touches only
the columns you need, nothing else. That is done by the query optimizer
in the "rewrite" phase of the query.
https://www.postgresql.org/docs/12/query-path.html
I could bet that the top level query gets merged with the view query
during the rewrite and that the columns that aren't needed aren't
touched. That in particular means that the function computing an
untouched column of the query isn't executed as it is.
Regards
Regards
On 8/2/21 10:12 AM, Avi Weinberg wrote:
>
> Hi,
>
> Is there a way to compute a column in a view only if it is referenced
> in the query? I have a view's column that its value is computed by a
> function. If in the query that column is not used at all, can
> Postgres "skip" computing it?
>
> Thanks!
>
> IMPORTANT - This email and any attachments is intended for the above
> named addressee(s), and may contain information which is confidential
> or privileged. If you are not the intended recipient, please inform
> the sender immediately and delete this email: you should not copy or
> use this e-mail for any purpose nor disclose its contents to any person.
--
Mladen Gogala
Database Consultant
Tel: (347) 321-1217
https://dbwhisperer.wordpress.com
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-general@postgresql.org
Cc: gogala.mladen@gmail.com
Subject: Re: Lazy View's Column Computing
In-Reply-To: <65c04e3a-ca63-166c-8689-9c8cd89d972f@gmail.com>
References: <DB9PR07MB7180094FC2775BFD3A61CC0DCBEF9@DB9PR07MB7180.eurprd07.prod.outlook.com> <65c04e3a-ca63-166c-8689-9c8cd89d972f@gmail.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox