Re: Scalar subquery - Mailing list pgsql-general

From Tom Lane
Subject Re: Scalar subquery
Date
Msg-id 10693.1283292995@sss.pgh.pa.us
Whole thread Raw
In response to Re: Scalar subquery  (Vyacheslav Kalinin <vka@mgcp.com>)
Responses Re: Scalar subquery
List pgsql-general
Vyacheslav Kalinin <vka@mgcp.com> writes:
> I just got my hands on mysql (5.0.something) and it does not cache the
> scalar subquery result.
> So... now I'm completely puzzled whether this is a bug, a desired result or
> just a loosely standardized thing.

It's loosely standardized.

AFAICS, the spec doesn't address the detailed semantics of subqueries at
all, except in wording to this effect:

              Each <subquery> in the <search condition> is effectively
              executed for each row of T and the results used in the ap-
              plication of the <search condition> to the given row of T.
              If any executed <subquery> contains an outer reference to a
              column of T, the reference is to the value of that column in
              the given row of T.

There is wording like this for subqueries in WHERE and HAVING, but I
haven't found anything at all that mentions the behavior for subqueries
in the SELECT targetlist.  In any case, the fact that they said
"effectively executed" and not simply "executed" seems to be meant to
leave implementors a lot of wiggle room.

In particular, there isn't any wording that I can find suggesting
that the presence of volatile (or in the spec's classification,
nondeterministic) functions ought to affect the behavior.

PG's interpretation is that if there is no outer reference in a
subquery, it's okay to implement it as an initplan, meaning it gets
evaluated at most once per call of the containing query.  We don't
pay attention to whether there are volatile functions in there.

            regards, tom lane

pgsql-general by date:

Previous
From: Vyacheslav Kalinin
Date:
Subject: Re: Scalar subquery
Next
From: "Joshua D. Drake"
Date:
Subject: Re: pg_dump --compress error