Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mAhNK-0004Y9-Hw for pgsql-general@arkaria.postgresql.org; Mon, 02 Aug 2021 23:29:38 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mAhNI-0006Kw-VD for pgsql-general@arkaria.postgresql.org; Mon, 02 Aug 2021 23:29:36 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mAhNI-0006Ko-IH for pgsql-general@lists.postgresql.org; Mon, 02 Aug 2021 23:29:36 +0000 Received: from mail-qt1-x82d.google.com ([2607:f8b0:4864:20::82d]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1mAhNE-0003mM-OK for pgsql-general@lists.postgresql.org; Mon, 02 Aug 2021 23:29:35 +0000 Received: by mail-qt1-x82d.google.com with SMTP id g11so12837394qts.11 for ; Mon, 02 Aug 2021 16:29:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-language; bh=kyI7oFDhV0sFuB/nqf6piS/YzZzBW3glNRxbW01LdA0=; b=mPdIVeCe20WaUQAj0DP5FbUaOpS8S+sgP4dwoX8i4vdvhMbJL8glKXleRUVm2yrOOu JROiCjDkBHBIoBwnqJkOQKTefnSvCHQ1wLv+IUZ0yqnVf5HnQQ1worEfMo97FHKfuq97 1xHI6xGxdbvqYQP1K/zdD5m6ZyU3Y5q6Z3jr7xJsdzvIn47XMajJ/5VrdmEg9vZtyXv/ eOSbKoKdLEW1iZZruekqRd3YbGHl2fclZvkJ1klMpX8mo+RtJiEaaNdcORjpYYSLyx12 9I5aUvLE6JwLDr3KVXaOuL7yL0epZ8IDm1OPoMiabiSOuErDDc003b1LRiqFEwfR/8Rg UJEw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language; bh=kyI7oFDhV0sFuB/nqf6piS/YzZzBW3glNRxbW01LdA0=; b=PicGm/uDn7wP/WbZowpHbCCjGfgghvnv8ICn2eUWXKr7R7ua2AqvVdscWmntSyARrg VnBIOcL095Cu4DtvLSlkz+m+2Yz2Cb+iELTmvUHAq5NeZaAQqh2iqurNOp0Ee2w26Sk/ lR6mZYa+IqVUzEY4I814q8i7Gcx9/7bTjuBJ0pWFSs8oFdAENiFdytkAES37bS+NiI1o 0nCFKUBqmFK6EfVFTsPMpXIwftA7ynzJ3vaBw/vfaDfNRuP9v5glQDKEywIaiuZ0W6Iu vCsPqIfMypcbu9Af4ZlNYsa1duTQaoT2E9v1kgHhwXWteGdtKfH0rVQAvCOX5ECU0Od/ IumQ== X-Gm-Message-State: AOAM533EgVgdqD8mOdykQzcTPEDXVYDj45l3lqz18SZl1m4GhfX5d99f QNQj+Udq+/PtTsjCjNPZOjOj4LoEPO49Sw== X-Google-Smtp-Source: ABdhPJwMOsIxft9lOzG1qI9CiidQKgdcpU7oav4qnkNwipN7htbqSvpiwtwfN/N/q4SL6Ac7nNjVag== X-Received: by 2002:ac8:6759:: with SMTP id n25mr16352225qtp.49.1627946970642; Mon, 02 Aug 2021 16:29:30 -0700 (PDT) Received: from [192.168.2.100] (pool-100-8-54-81.nwrknj.fios.verizon.net. [100.8.54.81]) by smtp.gmail.com with ESMTPSA id g13sm5347481qtj.2.2021.08.02.16.29.29 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 02 Aug 2021 16:29:29 -0700 (PDT) Subject: Re: Lazy View's Column Computing To: pgsql-general@lists.postgresql.org References: From: Mladen Gogala Message-ID: <65c04e3a-ca63-166c-8689-9c8cd89d972f@gmail.com> Date: Mon, 2 Aug 2021 19:27:02 -0400 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.11.0 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/alternative; boundary="------------D0B8CAFE3F5DCED1F4F54D0B" Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk This is a multi-part message in MIME format. --------------D0B8CAFE3F5DCED1F4F54D0B Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 8bit 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 --------------D0B8CAFE3F5DCED1F4F54D0B Content-Type: text/html; charset=windows-1252 Content-Transfer-Encoding: 8bit

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
--------------D0B8CAFE3F5DCED1F4F54D0B--