Allow Memoize to operate in binary comparison mode
authorDavid Rowley <drowley@postgresql.org>
Tue, 23 Nov 2021 21:06:59 +0000 (10:06 +1300)
committerDavid Rowley <drowley@postgresql.org>
Tue, 23 Nov 2021 21:06:59 +0000 (10:06 +1300)
commite502150f7d0be41e3c8784be007fa871a32d8a7f
treea6c96abbe3eae534d938d05539627b4f03d23f62
parent1922d7c6e1a74178bd2f1d5aa5a6ab921b3fcd34
Allow Memoize to operate in binary comparison mode

Memoize would always use the hash equality operator for the cache key
types to determine if the current set of parameters were the same as some
previously cached set.  Certain types such as floating points where -0.0
and +0.0 differ in their binary representation but are classed as equal by
the hash equality operator may cause problems as unless the join uses the
same operator it's possible that whichever join operator is being used
would be able to distinguish the two values.  In which case we may
accidentally return in the incorrect rows out of the cache.

To fix this here we add a binary mode to Memoize to allow it to the
current set of parameters to previously cached values by comparing
bit-by-bit rather than logically using the hash equality operator.  This
binary mode is always used for LATERAL joins and it's used for normal
joins when any of the join operators are not hashable.

Reported-by: Tom Lane
Author: David Rowley
Discussion: https://postgr.es/m/3004308.1632952496@sss.pgh.pa.us
Backpatch-through: 14, where Memoize was added
19 files changed:
contrib/postgres_fdw/expected/postgres_fdw.out
src/backend/commands/explain.c
src/backend/executor/nodeMemoize.c
src/backend/nodes/copyfuncs.c
src/backend/nodes/outfuncs.c
src/backend/nodes/readfuncs.c
src/backend/optimizer/path/joinpath.c
src/backend/optimizer/plan/createplan.c
src/backend/optimizer/util/pathnode.c
src/backend/utils/adt/datum.c
src/include/nodes/execnodes.h
src/include/nodes/pathnodes.h
src/include/nodes/plannodes.h
src/include/optimizer/pathnode.h
src/include/utils/datum.h
src/test/regress/expected/join.out
src/test/regress/expected/memoize.out
src/test/regress/expected/subselect.out
src/test/regress/sql/memoize.sql