what exactly is a PlaceHolderVar?

Started by Robert Haasalmost 16 years ago2 messageshackers
Jump to latest
#1Robert Haas
robertmhaas@gmail.com

I can't find any good documentation of this in the source tree
anywhere. placeholder.c just says:

* PlaceHolderVar and PlaceHolderInfo manipulation routines

and placeholder.h says:

* prototypes for optimizer/util/placeholder.c.

...which is less than informative. The commit message that introduced
them has a few details:

Add a concept of "placeholder" variables to the planner. These
are variables
that represent some expression that we desire to compute below the top level
of the plan, and then let that value "bubble up" as though it were a plain
Var (ie, a column value).

...but I'm still having a hard time wrapping my head around it.

--
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise Postgres Company

#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: Robert Haas (#1)
Re: what exactly is a PlaceHolderVar?

Robert Haas <robertmhaas@gmail.com> writes:

...but I'm still having a hard time wrapping my head around it.

The fundamental point is to be able to force a value to go to NULL
when outer-join logic says it ought to. Consider

CREATE VIEW foo AS SELECT x,y,'zed' FROM bar;

SELECT * FROM baz LEFT JOIN foo ON (baz.a = foo.x);

If you try to flatten the view then you end up with a constant 'zed'
that needs to be replaced by NULL whenever baz.a hasn't got a match
in bar.x. There's no way to make a constant go to NULL though: it's
a constant, n'est-ce pas? Instead, we have the idea of an expression
PlaceHolderVar(foo, 'zed'). This will go to null if variables from foo
ought to go to null. Otherwise it produces 'zed'. Sort of an
anti-COALESCE.

regards, tom lane