Re: Weird behavior with selects and composite data types

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: lists(at)petri(dot)cc
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Weird behavior with selects and composite data types
Date: 2004-09-20 14:56:47
Message-ID: 11511.1095692207@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

lists(at)petri(dot)cc writes:
> When using the (tbl).field notation for selecting a specific field from a
> composite field then the query returning the field is executed once per
> field.

You mean that the function yielding the field is executed repeatedly:

> SELECT guid, (acl).read, (acl).write,(acl).list FROM (select guid,
> f_all_acl(guid) as acl from rt_host) as i;

This will be flattened into

SELECT guid, (f_all_acl(guid)).read, (f_all_acl(guid)).write,
(f_all_acl(guid)).list
FROM rt_host;

which is generally a good thing, but perhaps not if f_all_acl() is
expensive to compute. The standard hack at present for preventing
such flattening without changing the query semantics is to insert
an OFFSET 0 clause into the subselect:

SELECT guid, (acl).read, (acl).write,(acl).list FROM (select guid,
f_all_acl(guid) as acl from rt_host offset 0) as i;

Note that this has nothing to do with field selection in particular,
it's just a general property of the subselect-in-FROM implementation.

regards, tom lane

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Marc G. Fournier 2004-09-20 14:57:16 New PayPal Donate Option
Previous Message Matthew Wilson 2004-09-20 14:27:41 How to get most frequent and least frequent values in a column?