postgresql/src/include/nodes
Tom Lane e80c85e4e8 Add defenses against putting expanded objects into Const nodes.
Putting a reference to an expanded-format value into a Const node would be
a bad idea for a couple of reasons.  It'd be possible for the supposedly
immutable Const to change value, if something modified the referenced
variable ... in fact, if the Const's reference were R/W, any function that
has the Const as argument might itself change it at runtime.  Also, because
datumIsEqual() is pretty simplistic, the Const might fail to compare equal
to other Consts that it should compare equal to, notably including copies
of itself.  This could lead to unexpected planner behavior, such as "could
not find pathkey item to sort" errors or inferior plans.

I have not been able to find any way to get an expanded value into a Const
within the existing core code; but Paul Ramsey was able to trigger the
problem by writing a datatype input function that returns an expanded
value.

The best fix seems to be to establish a rule that varlena values being
placed into Const nodes should be passed through pg_detoast_datum().
That will do nothing (and cost little) in normal cases, but it will flatten
expanded values and thereby avoid the above problems.  Also, it will
convert short-header or compressed values into canonical format, which will
avoid possible unexpected lack-of-equality issues for those cases too.
And it provides a last-ditch defense against putting a toasted value into
a Const, which we already knew was dangerous, cf commit 2b0c86b665.
(In the light of this discussion, I'm no longer sure that that commit
provided 100% protection against such cases, but this fix should do it.)

The test added in commit 65c3d05e18 to catch datatype input functions
with unstable results would fail for functions that returned expanded
values; but it seems a bit uncharitable to deem a result unstable just
because it's expressed in expanded form, so revise the coding so that we
check for bitwise equality only after applying pg_detoast_datum().  That's
a sufficient condition anyway given the new rule about detoasting when
forming a Const.

Back-patch to 9.5 where the expanded-object facility was added.  It's
possible that this should go back further; but in the absence of clear
evidence that there's any live bug in older branches, I'll refrain for now.
2016-01-21 12:55:59 -05:00
..
bitmapset.h Use FLEXIBLE_ARRAY_MEMBER in a bunch more places. 2015-02-20 00:11:42 -05:00
execnodes.h Allow FDWs to push down quals without breaking EvalPlanQual rechecks. 2015-10-15 13:10:39 -04:00
lockoptions.h Improve representation of PlanRowMark. 2015-03-15 18:41:47 -04:00
makefuncs.h Support GROUPING SETS, CUBE and ROLLUP. 2015-05-16 03:46:31 +02:00
memnodes.h Move memory context callback declarations into palloc.h. 2015-03-01 12:31:32 -05:00
nodeFuncs.h Update copyright for 2015 2015-01-06 11:43:47 -05:00
nodes.h Get rid of the planner's LateralJoinInfo data structure. 2015-12-11 15:52:39 -05:00
params.h Use FLEXIBLE_ARRAY_MEMBER in a bunch more places. 2015-02-20 00:11:42 -05:00
parsenodes.h Do not write out WCOs in Query 2015-10-05 07:38:56 -04:00
pg_list.h Support GROUPING SETS, CUBE and ROLLUP. 2015-05-16 03:46:31 +02:00
plannodes.h Fix incorrect comment in plannodes.h 2015-10-20 11:12:00 -04:00
primnodes.h Add defenses against putting expanded objects into Const nodes. 2016-01-21 12:55:59 -05:00
print.h Update copyright for 2015 2015-01-06 11:43:47 -05:00
readfuncs.h Update copyright for 2015 2015-01-06 11:43:47 -05:00
relation.h Get rid of the planner's LateralJoinInfo data structure. 2015-12-11 15:52:39 -05:00
replnodes.h Update copyright for 2015 2015-01-06 11:43:47 -05:00
tidbitmap.h Use FLEXIBLE_ARRAY_MEMBER in a bunch more places. 2015-02-20 00:11:42 -05:00
value.h Update copyright for 2015 2015-01-06 11:43:47 -05:00