mirror of
https://github.com/postgres/postgres.git
synced 2025-11-15 03:41:20 +03:00
Fix up planner infrastructure to support LATERAL properly.
This patch takes care of a number of problems having to do with failure to choose valid join orders and incorrect handling of lateral references pulled up from subqueries. Notable changes: * Add a LateralJoinInfo data structure similar to SpecialJoinInfo, to represent join ordering constraints created by lateral references. (I first considered extending the SpecialJoinInfo structure, but the semantics are different enough that a separate data structure seems better.) Extend join_is_legal() and related functions to prevent trying to form unworkable joins, and to ensure that we will consider joins that satisfy lateral references even if the joins would be clauseless. * Fill in the infrastructure needed for the last few types of relation scan paths to support parameterization. We'd have wanted this eventually anyway, but it is necessary now because a relation that gets pulled up out of a UNION ALL subquery may acquire a reltargetlist containing lateral references, meaning that its paths *have* to be parameterized whether or not we have any code that can push join quals down into the scan. * Compute data about lateral references early in query_planner(), and save in RelOptInfo nodes, to avoid repetitive calculations later. * Assorted corner-case bug fixes. There's probably still some bugs left, but this is a lot closer to being real than it was before.
This commit is contained in:
@@ -1906,6 +1906,20 @@ _copySpecialJoinInfo(const SpecialJoinInfo *from)
|
||||
return newnode;
|
||||
}
|
||||
|
||||
/*
|
||||
* _copyLateralJoinInfo
|
||||
*/
|
||||
static LateralJoinInfo *
|
||||
_copyLateralJoinInfo(const LateralJoinInfo *from)
|
||||
{
|
||||
LateralJoinInfo *newnode = makeNode(LateralJoinInfo);
|
||||
|
||||
COPY_SCALAR_FIELD(lateral_rhs);
|
||||
COPY_BITMAPSET_FIELD(lateral_lhs);
|
||||
|
||||
return newnode;
|
||||
}
|
||||
|
||||
/*
|
||||
* _copyAppendRelInfo
|
||||
*/
|
||||
@@ -4082,6 +4096,9 @@ copyObject(const void *from)
|
||||
case T_SpecialJoinInfo:
|
||||
retval = _copySpecialJoinInfo(from);
|
||||
break;
|
||||
case T_LateralJoinInfo:
|
||||
retval = _copyLateralJoinInfo(from);
|
||||
break;
|
||||
case T_AppendRelInfo:
|
||||
retval = _copyAppendRelInfo(from);
|
||||
break;
|
||||
|
||||
@@ -862,6 +862,15 @@ _equalSpecialJoinInfo(const SpecialJoinInfo *a, const SpecialJoinInfo *b)
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool
|
||||
_equalLateralJoinInfo(const LateralJoinInfo *a, const LateralJoinInfo *b)
|
||||
{
|
||||
COMPARE_SCALAR_FIELD(lateral_rhs);
|
||||
COMPARE_BITMAPSET_FIELD(lateral_lhs);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool
|
||||
_equalAppendRelInfo(const AppendRelInfo *a, const AppendRelInfo *b)
|
||||
{
|
||||
@@ -2646,6 +2655,9 @@ equal(const void *a, const void *b)
|
||||
case T_SpecialJoinInfo:
|
||||
retval = _equalSpecialJoinInfo(a, b);
|
||||
break;
|
||||
case T_LateralJoinInfo:
|
||||
retval = _equalLateralJoinInfo(a, b);
|
||||
break;
|
||||
case T_AppendRelInfo:
|
||||
retval = _equalAppendRelInfo(a, b);
|
||||
break;
|
||||
|
||||
@@ -1699,6 +1699,7 @@ _outPlannerInfo(StringInfo str, const PlannerInfo *node)
|
||||
WRITE_NODE_FIELD(right_join_clauses);
|
||||
WRITE_NODE_FIELD(full_join_clauses);
|
||||
WRITE_NODE_FIELD(join_info_list);
|
||||
WRITE_NODE_FIELD(lateral_info_list);
|
||||
WRITE_NODE_FIELD(append_rel_list);
|
||||
WRITE_NODE_FIELD(rowMarks);
|
||||
WRITE_NODE_FIELD(placeholder_list);
|
||||
@@ -1713,6 +1714,7 @@ _outPlannerInfo(StringInfo str, const PlannerInfo *node)
|
||||
WRITE_FLOAT_FIELD(limit_tuples, "%.0f");
|
||||
WRITE_BOOL_FIELD(hasInheritedTarget);
|
||||
WRITE_BOOL_FIELD(hasJoinRTEs);
|
||||
WRITE_BOOL_FIELD(hasLateralRTEs);
|
||||
WRITE_BOOL_FIELD(hasHavingQual);
|
||||
WRITE_BOOL_FIELD(hasPseudoConstantQuals);
|
||||
WRITE_BOOL_FIELD(hasRecursion);
|
||||
@@ -1743,6 +1745,8 @@ _outRelOptInfo(StringInfo str, const RelOptInfo *node)
|
||||
WRITE_ENUM_FIELD(rtekind, RTEKind);
|
||||
WRITE_INT_FIELD(min_attr);
|
||||
WRITE_INT_FIELD(max_attr);
|
||||
WRITE_NODE_FIELD(lateral_vars);
|
||||
WRITE_BITMAPSET_FIELD(lateral_relids);
|
||||
WRITE_NODE_FIELD(indexlist);
|
||||
WRITE_UINT_FIELD(pages);
|
||||
WRITE_FLOAT_FIELD(tuples, "%.0f");
|
||||
@@ -1890,6 +1894,15 @@ _outSpecialJoinInfo(StringInfo str, const SpecialJoinInfo *node)
|
||||
WRITE_NODE_FIELD(join_quals);
|
||||
}
|
||||
|
||||
static void
|
||||
_outLateralJoinInfo(StringInfo str, const LateralJoinInfo *node)
|
||||
{
|
||||
WRITE_NODE_TYPE("LATERALJOININFO");
|
||||
|
||||
WRITE_UINT_FIELD(lateral_rhs);
|
||||
WRITE_BITMAPSET_FIELD(lateral_lhs);
|
||||
}
|
||||
|
||||
static void
|
||||
_outAppendRelInfo(StringInfo str, const AppendRelInfo *node)
|
||||
{
|
||||
@@ -3036,6 +3049,9 @@ _outNode(StringInfo str, const void *obj)
|
||||
case T_SpecialJoinInfo:
|
||||
_outSpecialJoinInfo(str, obj);
|
||||
break;
|
||||
case T_LateralJoinInfo:
|
||||
_outLateralJoinInfo(str, obj);
|
||||
break;
|
||||
case T_AppendRelInfo:
|
||||
_outAppendRelInfo(str, obj);
|
||||
break;
|
||||
|
||||
Reference in New Issue
Block a user