From 87c2e274cb2de2d58d3540fdfc8d91ee82a9dd5a Mon Sep 17 00:00:00 2001 From: Jianghua Yang Date: Tue, 13 Oct 2026 16:27:16 -0700 Subject: [PATCH 01/27] Enhancement: skip redundant part-prop requests Hash and nested-loop joins create two partition propagation requests for every combination of their other property requests: request 0 may push DPE predicates to a child, request 1 passes the incoming requirement through. Parallel hash joins always create both, and so does every nested-loop join. When the join has no partition consumers below it and its parent requires no partition propagation, both requests produce an empty spec for every child. The second request then optimizes exactly the same child contexts as the first one, only for its cost context to be dropped as a duplicate. With parallel plans enabled this repeats a large share of the optimization jobs of every join group. Reject such requests in CEngine::FCheckReqdProps, before any child is optimized, and add CPartitionPropagationSpec::IsEmpty() for the check. Measured on a debug build with star, outer, semi, anti, aggregate, multi-key, union and DPE queries: plans and costs are unchanged, and ORCA planning time with parallel plans drops by 10-19% on the join-heavy queries. Claude-Session: https://claude.ai/code/session_01M1CxTMjApWd2XBG3C4wnyi Claude-Session: https://claude.ai/code/session_01ALBeQzzkBwDJDY1UJ65JrA (cherry picked from commit f4d28f67ae59dc572bb78d46b087b7cbc9bbec65) --- .../gpopt/base/CPartitionPropagationSpec.h | 7 +++++ .../libgpopt/include/gpopt/engine/CEngine.h | 4 +++ .../gporca/libgpopt/src/engine/CEngine.cpp | 30 +++++++++++++++++++ 3 files changed, 41 insertions(+) diff --git a/src/backend/gporca/libgpopt/include/gpopt/base/CPartitionPropagationSpec.h b/src/backend/gporca/libgpopt/include/gpopt/base/CPartitionPropagationSpec.h index 92921bd389a..0ec52dd65c7 100644 --- a/src/backend/gporca/libgpopt/include/gpopt/base/CPartitionPropagationSpec.h +++ b/src/backend/gporca/libgpopt/include/gpopt/base/CPartitionPropagationSpec.h @@ -177,6 +177,13 @@ class CPartitionPropagationSpec : public CPropSpec BOOL ContainsAnyConsumers() const; + // does this spec carry no partition propagation requests? + BOOL + IsEmpty() const + { + return 0 == m_part_prop_spec_infos->Size(); + } + // equality check to determine compatibility of derived & required properties BOOL Equals(const CPartitionPropagationSpec *ppps) const; diff --git a/src/backend/gporca/libgpopt/include/gpopt/engine/CEngine.h b/src/backend/gporca/libgpopt/include/gpopt/engine/CEngine.h index 42564394740..bd0823eba93 100644 --- a/src/backend/gporca/libgpopt/include/gpopt/engine/CEngine.h +++ b/src/backend/gporca/libgpopt/include/gpopt/engine/CEngine.h @@ -256,6 +256,10 @@ class CEngine : public DbgPrintMixin BOOL FCheckReqdProps(CExpressionHandle &exprhdl, CReqdPropPlan *prpp, ULONG ulOptReq); + // check whether a join's non-DPE propagation request is redundant + BOOL FRedundantPartPropagationRequest(CExpressionHandle &exprhdl, + CReqdPropPlan *prpp, ULONG ulOptReq); + // check enforceable properties; // return false if it's impossible for the operator to satisfy one or more BOOL FCheckEnfdProps(CMemoryPool *mp, CGroupExpression *pgexpr, diff --git a/src/backend/gporca/libgpopt/src/engine/CEngine.cpp b/src/backend/gporca/libgpopt/src/engine/CEngine.cpp index e68bc85f248..43dca20207d 100644 --- a/src/backend/gporca/libgpopt/src/engine/CEngine.cpp +++ b/src/backend/gporca/libgpopt/src/engine/CEngine.cpp @@ -25,6 +25,7 @@ #include "gpopt/base/CEnfdPartitionPropagation.h" #include "gpopt/base/COptCtxt.h" #include "gpopt/base/COptimizationContext.h" +#include "gpopt/base/CPartInfo.h" #include "gpopt/base/CQueryContext.h" #include "gpopt/base/CReqdPropPlan.h" #include "gpopt/base/CReqdPropRelational.h" @@ -2248,6 +2249,31 @@ CEngine::FProhibited(CEnfdProp::EPropEnforcingType epetOrder, } +BOOL +CEngine::FRedundantPartPropagationRequest(CExpressionHandle &exprhdl, + CReqdPropPlan *prpp, ULONG ulOptReq) +{ + CPhysical *popPhysical = CPhysical::PopConvert(exprhdl.Pop()); + + // Hash and nested-loop joins can enumerate DPE and non-DPE requests. + // Without partition consumers or incoming propagation requirements, + // both produce empty specs for every child. The other property request + // numbers are unchanged, so keep only the DPE request for each tuple. + if (2 != popPhysical->UlPartPropagateRequests() || + !CUtils::FPhysicalJoin(popPhysical) || + !prpp->Pepp()->PppsRequired()->IsEmpty()) + { + return false; + } + + ULONG order_req, distribution_req, rewind_req, partition_req; + popPhysical->LookupRequest(ulOptReq, &order_req, &distribution_req, + &rewind_req, &partition_req); + return 1 == partition_req && + 0 == exprhdl.DerivePartitionInfo()->UlConsumers(); +} + + //--------------------------------------------------------------------------- // @function: // CEngine::FCheckReqdProps @@ -2289,6 +2315,10 @@ CEngine::FCheckReqdProps(CExpressionHandle &exprhdl, CReqdPropPlan *prpp, CPhysical *popPhysical = CPhysical::PopConvert(exprhdl.Pop()); COperator::EOperatorId op_id = popPhysical->Eopid(); + if (FRedundantPartPropagationRequest(exprhdl, prpp, ulOptReq)) + { + return false; + } // check if sort operator is passed an empty order spec; // this check is required to avoid self-deadlocks, i.e. From 54cf3f4ec50752a0ea47a12701c9ca246fd7b35c Mon Sep 17 00:00:00 2001 From: Jianghua Yang Date: Thu, 15 Oct 2026 10:29:31 -0700 Subject: [PATCH 02/27] Enhancement: reuse child contexts of a request A request of a group expression is optimized once per context of its group. Under contexts that differ only in the required distribution, it usually computes the same child requirements and reaches the same child contexts again, after computing every child requirement, looking up the child contexts, waiting for the child jobs and deriving the plan properties of each optimized child. Store the child contexts a request reaches on its group expression, keyed by the request number, the search stage and the parent's required plan properties apart from distribution. A later request with an equal key reuses them if it would reach the same contexts: - Every child requirement except distribution only depends on the matching part of the parent's requirements, which the key makes equal, and on the plans of the children optimized before, which are the best plans of the same contexts. Only Ped() sees the whole parent requirement, so the required distribution of each child is computed again and compared. The plan properties of earlier children are derived again for this, as DerivePrevChildProps() does: some PdsRequired() implementations change the derived distribution of the first child in place, and the properties cached on a cost context are shared. - A child context must have a plan, must not be the context itself, and must be the one a lookup of its requirements finds. A group can hold several contexts with equal requirements when a requirement changes after its context was inserted. The cost bound checks made after each child are repeated with the reused contexts. CTE sequences and multi-stage search are left out. The developer GUC optimizer_enable_child_opt_memo (default on) turns the memo off. optimizer_verify_child_opt_memo optimizes the children anyway and logs where the result differs from the reused contexts. optimizer_print_optimization_stats prints the memo counters. In 13 join and aggregation queries the plans are identical with and without the memo, and 11914 of 16127 lookups reuse child contexts with parallel plans (5020 of 7870 serial). Planning time drops by 12.6% geomean (15.4% in total) with parallel plans and by 8.3% (9.4%) serial. Verify mode finds no difference there or in 25 related regression tests, whose results do not change apart from run-to-run noise. Claude-Session: https://claude.ai/code/session_01M1CxTMjApWd2XBG3C4wnyi Claude-Session: https://claude.ai/code/session_01ALBeQzzkBwDJDY1UJ65JrA (cherry picked from commit 9bb35ccdd3c1e13809776bbe76189c1bc34175c4) --- .../gpopt/config/CConfigParamMapping.cpp | 10 + .../libgpopt/include/gpopt/engine/CEngine.h | 66 +++ .../libgpopt/include/gpopt/search/CGroup.h | 4 + .../include/gpopt/search/CGroupExpression.h | 62 +++ .../search/CJobGroupExpressionOptimization.h | 14 + .../gporca/libgpopt/src/engine/CEngine.cpp | 461 ++++++++++++++++++ .../gporca/libgpopt/src/search/CGroup.cpp | 20 + .../libgpopt/src/search/CGroupExpression.cpp | 29 ++ .../CJobGroupExpressionOptimization.cpp | 94 +++- .../include/naucrates/traceflags/traceflags.h | 8 + src/backend/utils/misc/guc_gp.c | 22 + src/include/utils/guc.h | 2 + src/include/utils/unsync_guc_name.h | 2 + 13 files changed, 790 insertions(+), 4 deletions(-) diff --git a/src/backend/gpopt/config/CConfigParamMapping.cpp b/src/backend/gpopt/config/CConfigParamMapping.cpp index bdeb3cbb411..ab23ec820dc 100644 --- a/src/backend/gpopt/config/CConfigParamMapping.cpp +++ b/src/backend/gpopt/config/CConfigParamMapping.cpp @@ -346,6 +346,16 @@ CConfigParamMapping::SConfigMappingElem CConfigParamMapping::m_elements[] = { GPOS_WSZ_LIT( "Disable streaming hash agg in ORCA-generated local partial aggregations.")}, + {EopttraceChildOptMemo, &optimizer_enable_child_opt_memo, + false, // m_negate_param + GPOS_WSZ_LIT( + "Reuse the child contexts that the same request of a group expression reached under another context.")}, + + {EopttraceVerifyChildOptMemo, &optimizer_verify_child_opt_memo, + false, // m_negate_param + GPOS_WSZ_LIT( + "Optimize the children of requests that could reuse child contexts and report differences.")}, + }; //--------------------------------------------------------------------------- diff --git a/src/backend/gporca/libgpopt/include/gpopt/engine/CEngine.h b/src/backend/gporca/libgpopt/include/gpopt/engine/CEngine.h index bd0823eba93..bc6e009dcec 100644 --- a/src/backend/gporca/libgpopt/include/gpopt/engine/CEngine.h +++ b/src/backend/gporca/libgpopt/include/gpopt/engine/CEngine.h @@ -377,6 +377,72 @@ class CEngine : public DbgPrintMixin CCostContext *pccChild, ULONG child_index, CCost *pcostLowerBound); + // events of the child optimization memo + enum EChildOptMemoEvent + { + EcomeLookup, // a request looked for child contexts to reuse + EcomeHit, // a request reused child contexts + EcomePrunedHit, // a request reusing child contexts was pruned + EcomeDistMismatch, // an entry failed the distribution check + EcomeShadowed, // a child context is shadowed by an equal one + EcomeInsert, // a request stored the child contexts it reached + EcomeVerifyMismatch, // verify: the children reached other contexts + EcomeVerifyPruneDiff, // verify: pruning after a child differed + EcomeVerifyIncomplete, // verify: child optimization failed otherwise + + EcomeSentinel + }; + +private: + // counters of child optimization memo events + ULONG m_rgulChildOptMemoEvents[EcomeSentinel]{}; + +public: + // count an event of the child optimization memo + void + CountChildOptMemoEvent(EChildOptMemoEvent ecome) + { + m_rgulChildOptMemoEvents[ecome]++; + } + + // check if a request of the given group expression can reuse and store + // child contexts + BOOL FChildOptMemoApplicable(CGroupExpression *pgexpr, + BOOL fOptimizeCTESequence) const; + + // find child contexts that the same request reached under another + // context and would reach under the given context as well + COptimizationContextArray *PdrgpocChildOptMemo(CMemoryPool *mp, + CGroupExpression *pgexpr, + COptimizationContext *poc, + ULONG ulOptReq); + + // check if a request reusing child contexts is pruned after one of them, + // as it would be when optimizing its children + BOOL FSafeToPruneWithChildOptMemo(CGroupExpression *pgexpr, + COptimizationContext *poc, + COptimizationContextArray *pdrgpoc, + CCost *pcostLowerBound); + + // store the child contexts reached by a request + void AddChildOptMemo(CMemoryPool *mp, CGroupExpression *pgexpr, + COptimizationContext *poc, ULONG ulOptReq, + COptimizationContextArray *pdrgpoc); + + // compare the child contexts reached by a request with the ones the + // memo offered, in verify mode + void VerifyChildOptMemo(CGroupExpression *pgexpr, COptimizationContext *poc, + ULONG ulOptReq, + COptimizationContextArray *pdrgpocMemo, + COptimizationContextArray *pdrgpoc, + BOOL fMemoPredictsPrune); + + // count and report a difference found in verify mode + void ReportChildOptMemoDifference(EChildOptMemoEvent ecome, + CGroupExpression *pgexpr, + COptimizationContext *poc, + ULONG ulOptReq); + // print IOstream &OsPrint(IOstream &) const; diff --git a/src/backend/gporca/libgpopt/include/gpopt/search/CGroup.h b/src/backend/gporca/libgpopt/include/gpopt/search/CGroup.h index eaeaf22024f..8bd9ede7e42 100644 --- a/src/backend/gporca/libgpopt/include/gpopt/search/CGroup.h +++ b/src/backend/gporca/libgpopt/include/gpopt/search/CGroup.h @@ -542,6 +542,10 @@ class CGroup : public CRefCount, public DbgPrintMixin // insert given context into contexts hash table COptimizationContext *PocInsert(COptimizationContext *poc); + // check if looking up the requirements of a context of this group finds + // that context, and not another context with equal requirements + BOOL FLookupFinds(COptimizationContext *poc); + // update the best group cost under the given optimization context void UpdateBestCost(COptimizationContext *poc, CCostContext *pcc); diff --git a/src/backend/gporca/libgpopt/include/gpopt/search/CGroupExpression.h b/src/backend/gporca/libgpopt/include/gpopt/search/CGroupExpression.h index 1edfd359774..cf01e25da2c 100644 --- a/src/backend/gporca/libgpopt/include/gpopt/search/CGroupExpression.h +++ b/src/backend/gporca/libgpopt/include/gpopt/search/CGroupExpression.h @@ -26,6 +26,53 @@ namespace gpopt { using namespace gpos; +//--------------------------------------------------------------------------- +// @class: +// CChildOptMemoEntry +// +// @doc: +// Child contexts that a request of a group expression reached, with the +// required plan properties of the context the request was made under +// +//--------------------------------------------------------------------------- +class CChildOptMemoEntry +{ +public: + // optimization request number + ULONG m_ulOptReq; + + // search stage of the context the request was made under + ULONG m_ulSearchStage; + + // required plan properties of that context + CReqdPropPlan *m_prpp; + + // child contexts the request reached, one per non-scalar child + COptimizationContextArray *m_pdrgpoc; + + CChildOptMemoEntry(ULONG ulOptReq, ULONG ulSearchStage, CReqdPropPlan *prpp, + COptimizationContextArray *pdrgpoc) + : m_ulOptReq(ulOptReq), + m_ulSearchStage(ulSearchStage), + m_prpp(prpp), + m_pdrgpoc(pdrgpoc) + { + } + + CChildOptMemoEntry(const CChildOptMemoEntry &) = delete; + + CChildOptMemoEntry &operator=(const CChildOptMemoEntry &) = delete; + + ~CChildOptMemoEntry() + { + m_prpp->Release(); + m_pdrgpoc->Release(); + } +}; + +using ChildOptMemoArray = + CDynamicPtrArray>; + //--------------------------------------------------------------------------- // @class: // CGroupExpression @@ -130,6 +177,9 @@ class CGroupExpression : public CRefCount, // hashtable of cost contexts ShtCC m_sht; + // child contexts reached by requests of this group expression + ChildOptMemoArray *m_pdrgpcome{nullptr}; + // set group back pointer void SetGroup(CGroup *pgroup); @@ -201,6 +251,18 @@ class CGroupExpression : public CRefCount, // cleanup cost contexts void CleanupContexts(); + // child contexts reached by requests of this group expression + ChildOptMemoArray * + PdrgpcomeChildOptMemo() const + { + return m_pdrgpcome; + } + + // store the child contexts reached by a request + void AddChildOptMemo(CMemoryPool *mp, ULONG ulOptReq, ULONG ulSearchStage, + CReqdPropPlan *prpp, + COptimizationContextArray *pdrgpoc); + // check if cost context already exists in group expression hash table BOOL FCostContextExists(COptimizationContext *poc, COptimizationContextArray *pdrgpoc); diff --git a/src/backend/gporca/libgpopt/include/gpopt/search/CJobGroupExpressionOptimization.h b/src/backend/gporca/libgpopt/include/gpopt/search/CJobGroupExpressionOptimization.h index 91dd018bc19..d35906d2c5c 100644 --- a/src/backend/gporca/libgpopt/include/gpopt/search/CJobGroupExpressionOptimization.h +++ b/src/backend/gporca/libgpopt/include/gpopt/search/CJobGroupExpressionOptimization.h @@ -117,6 +117,20 @@ class CJobGroupExpressionOptimization : public CJobGroupExpression // a handle object for required relational property computation CExpressionHandle *m_pexprhdlRel; + // flag to indicate if the child contexts were reused from the child + // optimization memo instead of optimizing the children + BOOL m_fChildOptMemoHit; + + // verify mode: child contexts the memo offered, compared with the ones + // reached by optimizing the children + COptimizationContextArray *m_pdrgpocMemo; + + // verify mode: flag to indicate if the memo predicted pruning after a child + BOOL m_fMemoPredictsPrune; + + // flag to indicate if child optimization was pruned after a child + BOOL m_fPrunedAfterChild; + // initialization routine for child groups optimization void InitChildGroupsOptimization(CSchedulerContext *psc); diff --git a/src/backend/gporca/libgpopt/src/engine/CEngine.cpp b/src/backend/gporca/libgpopt/src/engine/CEngine.cpp index 43dca20207d..9fd7bdbfa99 100644 --- a/src/backend/gporca/libgpopt/src/engine/CEngine.cpp +++ b/src/backend/gporca/libgpopt/src/engine/CEngine.cpp @@ -20,6 +20,7 @@ #include "gpos/task/CAutoTaskProxy.h" #include "gpos/task/CAutoTraceFlag.h" +#include "gpopt/base/CCTEReq.h" #include "gpopt/base/CCostContext.h" #include "gpopt/base/CDrvdPropCtxtPlan.h" #include "gpopt/base/CEnfdPartitionPropagation.h" @@ -1379,6 +1380,451 @@ CEngine::DbgPrintExpr(int group_no, int context_no) #endif // GPOS_DEBUG +//--------------------------------------------------------------------------- +// @function: +// FEqualExceptDistribution +// +// @doc: +// Check if two required plan properties are equal apart from the +// required distribution +// +//--------------------------------------------------------------------------- +static BOOL +FEqualExceptDistribution(CReqdPropPlan *prppFst, CReqdPropPlan *prppSnd) +{ + if (!prppFst->PcrsRequired()->Equals(prppSnd->PcrsRequired()) || + !prppFst->Pcter()->Equals(prppSnd->Pcter()) || + !prppFst->Peo()->Matches(prppSnd->Peo()) || + !prppFst->Per()->Matches(prppSnd->Per())) + { + return false; + } + + if (nullptr == prppFst->Pepp() || nullptr == prppSnd->Pepp()) + { + return nullptr == prppFst->Pepp() && nullptr == prppSnd->Pepp(); + } + + return prppFst->Pepp()->Matches(prppSnd->Pepp()); +} + + +//--------------------------------------------------------------------------- +// @function: +// UlNonScalarChildPos +// +// @doc: +// Position of a child in an array of child contexts, which holds one +// context per non-scalar child +// +//--------------------------------------------------------------------------- +static ULONG +UlNonScalarChildPos(CGroupExpression *pgexpr, ULONG child_index) +{ + ULONG ulPos = 0; + for (ULONG ul = 0; ul < child_index; ul++) + { + if (!(*pgexpr)[ul]->FScalar()) + { + ulPos++; + } + } + + return ulPos; +} + + +//--------------------------------------------------------------------------- +// @function: +// FSameChildContexts +// +// @doc: +// Check if two arrays hold the same child contexts +// +//--------------------------------------------------------------------------- +static BOOL +FSameChildContexts(COptimizationContextArray *pdrgpocFst, + COptimizationContextArray *pdrgpocSnd) +{ + const ULONG size = pdrgpocFst->Size(); + if (size != pdrgpocSnd->Size()) + { + return false; + } + + for (ULONG ul = 0; ul < size; ul++) + { + if ((*pdrgpocFst)[ul] != (*pdrgpocSnd)[ul]) + { + return false; + } + } + + return true; +} + + +//--------------------------------------------------------------------------- +// @function: +// FChildDistributionsMatch +// +// @doc: +// Check if a request made under the given context requires from each +// child the distribution that the child's context in pdrgpoc was +// created for. +// +// The caller has checked that the context's other requirements equal +// the ones the child contexts were reached under. The other child +// requirements only depend on those, on the request, and on the plans +// of the children optimized before, which are the best plans of the +// same child contexts. So the distribution is the only child +// requirement that can differ. +// +// The plan properties of earlier children are derived again, as +// optimizing the children does in DerivePrevChildProps(), rather than +// taken from the child's cost context: some PdsRequired() +// implementations change the derived distribution of the first child +// in place, and the properties cached on the cost context are shared. +// +//--------------------------------------------------------------------------- +static BOOL +FChildDistributionsMatch(CMemoryPool *mp, CGroupExpression *pgexpr, + COptimizationContext *poc, ULONG ulOptReq, + COptimizationContextArray *pdrgpoc, + BOOL *pfShadowed) +{ + *pfShadowed = false; + + CPhysical *popPhysical = CPhysical::PopConvert(pgexpr->Pop()); + ULONG ulOrderReq = 0; + ULONG ulDistrReq = 0; + ULONG ulRewindReq = 0; + ULONG ulPartPropagateReq = 0; + popPhysical->LookupRequest(ulOptReq, &ulOrderReq, &ulDistrReq, + &ulRewindReq, &ulPartPropagateReq); + + CExpressionHandle exprhdl(mp); + exprhdl.Attach(pgexpr); + exprhdl.DeriveProps(nullptr /*pdpctxt*/); + exprhdl.InitReqdProps(poc->Prpp()); + + CDrvdPropArray *pdrgpdp = GPOS_NEW(mp) CDrvdPropArray(mp); + BOOL fMatch = true; + ULONG ulPrevChildIndex = gpos::ulong_max; + ULONG child_index = exprhdl.UlFirstOptimizedChildIndex(); + do + { + CGroup *pgroupChild = (*pgexpr)[child_index]; + if (pgroupChild->FScalar()) + { + ulPrevChildIndex = child_index; + continue; + } + + COptimizationContext *pocChild = + (*pdrgpoc)[UlNonScalarChildPos(pgexpr, child_index)]; + CCostContext *pccChild = pocChild->PccBest(); + if (nullptr == pccChild || + (pgroupChild == pgexpr->Pgroup() && pocChild->Matches(poc))) + { + // the child has no plan, or it would be the context itself + fMatch = false; + break; + } + + // as DerivePrevChildProps(): the plan of the previous child, if that + // one is not scalar + if (gpos::ulong_max != ulPrevChildIndex && + !(*pgexpr)[ulPrevChildIndex]->FScalar()) + { + CCostContext *pccPrev = + (*pdrgpoc)[UlNonScalarChildPos(pgexpr, ulPrevChildIndex)] + ->PccBest(); + CExpressionHandle exprhdlPrev(mp); + exprhdlPrev.Attach(pccPrev); + exprhdlPrev.DerivePlanPropsForCostContext(); + exprhdlPrev.Pdp()->AddRef(); + pdrgpdp->Append(exprhdlPrev.Pdp()); + } + + CEnfdDistribution *ped = popPhysical->Ped( + mp, exprhdl, poc->Prpp(), child_index, pdrgpdp, ulDistrReq); + fMatch = ped->Matches(pocChild->Prpp()->Ped()); + ped->Release(); + if (!fMatch) + { + break; + } + + // the request reaches the child context that a lookup of its + // requirements finds, which is another one if the group holds + // several contexts with equal requirements + if (!pgroupChild->FLookupFinds(pocChild)) + { + *pfShadowed = true; + fMatch = false; + break; + } + + // later children see the requirements of this child as they would + // after optimizing it + pocChild->Prpp()->AddRef(); + exprhdl.CopyChildReqdProps(child_index, pocChild->Prpp()); + ulPrevChildIndex = child_index; + } while (exprhdl.FNextChildIndex(&child_index)); + + pdrgpdp->Release(); + + return fMatch; +} + + +//--------------------------------------------------------------------------- +// @function: +// CEngine::FChildOptMemoApplicable +// +// @doc: +// Check if a request of the given group expression can reuse and store +// child contexts +// +//--------------------------------------------------------------------------- +BOOL +CEngine::FChildOptMemoApplicable(CGroupExpression *pgexpr, + BOOL fOptimizeCTESequence) const +{ + if (!GPOS_FTRACE(EopttraceChildOptMemo) && + !GPOS_FTRACE(EopttraceVerifyChildOptMemo)) + { + return false; + } + + // a CTE sequence may optimize its producer under requirements derived + // from the consumers, and with several search stages the children of a + // request use the best context of any stage + if (fOptimizeCTESequence || 1 < UlSearchStages()) + { + return false; + } + + const ULONG arity = pgexpr->Arity(); + for (ULONG ul = 0; ul < arity; ul++) + { + if (!(*pgexpr)[ul]->FScalar()) + { + return true; + } + } + + return false; +} + + +//--------------------------------------------------------------------------- +// @function: +// CEngine::PdrgpocChildOptMemo +// +// @doc: +// Find child contexts that the same request of the group expression +// reached under another context, and that it would reach under the +// given context as well. The other context must have the same required +// plan properties apart from distribution; the required distribution +// of each child is then computed again and compared. +// +//--------------------------------------------------------------------------- +COptimizationContextArray * +CEngine::PdrgpocChildOptMemo(CMemoryPool *mp, CGroupExpression *pgexpr, + COptimizationContext *poc, ULONG ulOptReq) +{ + CountChildOptMemoEvent(EcomeLookup); + + ChildOptMemoArray *pdrgpcome = pgexpr->PdrgpcomeChildOptMemo(); + if (nullptr == pdrgpcome) + { + return nullptr; + } + + CReqdPropPlan *prpp = poc->Prpp(); + const ULONG size = pdrgpcome->Size(); + for (ULONG ul = 0; ul < size; ul++) + { + CChildOptMemoEntry *pcome = (*pdrgpcome)[ul]; + if (pcome->m_ulOptReq != ulOptReq || + pcome->m_ulSearchStage != poc->UlSearchStageIndex() || + !FEqualExceptDistribution(pcome->m_prpp, prpp)) + { + continue; + } + + BOOL fShadowed = false; + if (FChildDistributionsMatch(mp, pgexpr, poc, ulOptReq, + pcome->m_pdrgpoc, &fShadowed)) + { + pcome->m_pdrgpoc->AddRef(); + return pcome->m_pdrgpoc; + } + + CountChildOptMemoEvent(fShadowed ? EcomeShadowed : EcomeDistMismatch); + } + + return nullptr; +} + + +//--------------------------------------------------------------------------- +// @function: +// CEngine::FSafeToPruneWithChildOptMemo +// +// @doc: +// Check if a request reusing child contexts is pruned after one of its +// children. Optimizing the children checks cost bounds after each +// non-scalar child that is followed by another non-scalar child in +// optimization order; make the same checks with the reused contexts. +// +//--------------------------------------------------------------------------- +BOOL +CEngine::FSafeToPruneWithChildOptMemo(CGroupExpression *pgexpr, + COptimizationContext *poc, + COptimizationContextArray *pdrgpoc, + CCost *pcostLowerBound) +{ + CExpressionHandle exprhdl(m_mp); + exprhdl.Attach(pgexpr); + + ULONG ulPrevChildIndex = gpos::ulong_max; + ULONG child_index = exprhdl.UlFirstOptimizedChildIndex(); + do + { + if (gpos::ulong_max != ulPrevChildIndex && + !(*pgexpr)[ulPrevChildIndex]->FScalar() && + !(*pgexpr)[child_index]->FScalar()) + { + CCostContext *pccPrev = + (*pdrgpoc)[UlNonScalarChildPos(pgexpr, ulPrevChildIndex)] + ->PccBest(); + if (FSafeToPrune(pgexpr, poc->Prpp(), pccPrev, ulPrevChildIndex, + pcostLowerBound)) + { + return true; + } + } + ulPrevChildIndex = child_index; + } while (exprhdl.FNextChildIndex(&child_index)); + + return false; +} + + +//--------------------------------------------------------------------------- +// @function: +// CEngine::AddChildOptMemo +// +// @doc: +// Store the child contexts reached by a request, unless a child has no +// plan or the same entry exists +// +//--------------------------------------------------------------------------- +void +CEngine::AddChildOptMemo(CMemoryPool *mp, CGroupExpression *pgexpr, + COptimizationContext *poc, ULONG ulOptReq, + COptimizationContextArray *pdrgpoc) +{ + const ULONG ulChildren = pdrgpoc->Size(); + for (ULONG ul = 0; ul < ulChildren; ul++) + { + if (nullptr == (*pdrgpoc)[ul]->PccBest()) + { + return; + } + } + + ChildOptMemoArray *pdrgpcome = pgexpr->PdrgpcomeChildOptMemo(); + if (nullptr != pdrgpcome) + { + const ULONG size = pdrgpcome->Size(); + for (ULONG ul = 0; ul < size; ul++) + { + CChildOptMemoEntry *pcome = (*pdrgpcome)[ul]; + if (pcome->m_ulOptReq == ulOptReq && + pcome->m_ulSearchStage == poc->UlSearchStageIndex() && + FSameChildContexts(pcome->m_pdrgpoc, pdrgpoc) && + FEqualExceptDistribution(pcome->m_prpp, poc->Prpp())) + { + return; + } + } + } + + pgexpr->AddChildOptMemo(mp, ulOptReq, poc->UlSearchStageIndex(), + poc->Prpp(), pdrgpoc); + CountChildOptMemoEvent(EcomeInsert); +} + + +//--------------------------------------------------------------------------- +// @function: +// CEngine::VerifyChildOptMemo +// +// @doc: +// Compare the child contexts reached by optimizing the children of a +// request with the ones the memo offered, and report a difference +// +//--------------------------------------------------------------------------- +void +CEngine::VerifyChildOptMemo(CGroupExpression *pgexpr, + COptimizationContext *poc, ULONG ulOptReq, + COptimizationContextArray *pdrgpocMemo, + COptimizationContextArray *pdrgpoc, + BOOL fMemoPredictsPrune) +{ + if (!FSameChildContexts(pdrgpocMemo, pdrgpoc)) + { + ReportChildOptMemoDifference(EcomeVerifyMismatch, pgexpr, poc, + ulOptReq); + } + else if (fMemoPredictsPrune) + { + ReportChildOptMemoDifference(EcomeVerifyPruneDiff, pgexpr, poc, + ulOptReq); + } +} + + +//--------------------------------------------------------------------------- +// @function: +// CEngine::ReportChildOptMemoDifference +// +// @doc: +// Count and report a difference between reusing child contexts and +// optimizing the children, found in verify mode +// +//--------------------------------------------------------------------------- +void +CEngine::ReportChildOptMemoDifference(EChildOptMemoEvent ecome, + CGroupExpression *pgexpr, + COptimizationContext *poc, + ULONG ulOptReq) +{ + CountChildOptMemoEvent(ecome); + + const CHAR *szWhat = "other child contexts"; + if (EcomeVerifyPruneDiff == ecome) + { + szWhat = "other pruning"; + } + else if (EcomeVerifyIncomplete == ecome) + { + szWhat = "child optimization failed"; + } + + CAutoTrace at(m_mp); + at.Os() << "[OPT]: child optimization memo difference (" << szWhat + << "): " << pgexpr->Pop()->SzId() << " (group " + << pgexpr->Pgroup()->Id() << ", expression " << pgexpr->Id() + << ", request " << ulOptReq << ") under "; + poc->Prpp()->OsPrint(at.Os()); +} + + //--------------------------------------------------------------------------- // @function: // CEngine::PdrgpocChildren @@ -1571,6 +2017,21 @@ CEngine::PrintActivatedXforms(IOstream &os) const } os << "[OPT]: " << std::endl; + + os << "[OPT]: child optimization memo: " + << m_rgulChildOptMemoEvents[EcomeLookup] << " lookups, " + << m_rgulChildOptMemoEvents[EcomeHit] << " hits, " + << m_rgulChildOptMemoEvents[EcomePrunedHit] << " pruned hits, " + << m_rgulChildOptMemoEvents[EcomeDistMismatch] + << " distribution mismatches, " + << m_rgulChildOptMemoEvents[EcomeShadowed] << " shadowed, " + << m_rgulChildOptMemoEvents[EcomeInsert] << " entries, " + << m_rgulChildOptMemoEvents[EcomeVerifyMismatch] + << " verify mismatches, " + << m_rgulChildOptMemoEvents[EcomeVerifyPruneDiff] + << " verify pruning differences, " + << m_rgulChildOptMemoEvents[EcomeVerifyIncomplete] + << " verify incomplete" << std::endl; } } diff --git a/src/backend/gporca/libgpopt/src/search/CGroup.cpp b/src/backend/gporca/libgpopt/src/search/CGroup.cpp index 02fd7fa9ccc..dfc8d85ba86 100644 --- a/src/backend/gporca/libgpopt/src/search/CGroup.cpp +++ b/src/backend/gporca/libgpopt/src/search/CGroup.cpp @@ -439,6 +439,26 @@ CGroup::PocInsert(COptimizationContext *poc) } +//--------------------------------------------------------------------------- +// @function: +// CGroup::FLookupFinds +// +// @doc: +// Check if looking up the requirements of a context of this group finds +// that context. If requirements change after their context was +// inserted, a group can hold several contexts with equal requirements, +// of which a lookup finds only one. +// +//--------------------------------------------------------------------------- +BOOL +CGroup::FLookupFinds(COptimizationContext *poc) +{ + ShtAcc shta(Sht(), *poc); + + return poc == shta.Find(); +} + + //--------------------------------------------------------------------------- // @function: // CGroup::PgexprBest diff --git a/src/backend/gporca/libgpopt/src/search/CGroupExpression.cpp b/src/backend/gporca/libgpopt/src/search/CGroupExpression.cpp index dffd10cb35f..c6fc40e3b5c 100644 --- a/src/backend/gporca/libgpopt/src/search/CGroupExpression.cpp +++ b/src/backend/gporca/libgpopt/src/search/CGroupExpression.cpp @@ -143,6 +143,35 @@ CGroupExpression::CleanupContexts() } } } + + CRefCount::SafeRelease(m_pdrgpcome); + m_pdrgpcome = nullptr; +} + + +//--------------------------------------------------------------------------- +// @function: +// CGroupExpression::AddChildOptMemo +// +// @doc: +// Store the child contexts reached by a request, together with the +// required plan properties of the context the request was made under +// +//--------------------------------------------------------------------------- +void +CGroupExpression::AddChildOptMemo(CMemoryPool *mp, ULONG ulOptReq, + ULONG ulSearchStage, CReqdPropPlan *prpp, + COptimizationContextArray *pdrgpoc) +{ + if (nullptr == m_pdrgpcome) + { + m_pdrgpcome = GPOS_NEW(mp) ChildOptMemoArray(mp); + } + + prpp->AddRef(); + pdrgpoc->AddRef(); + m_pdrgpcome->Append(GPOS_NEW(mp) CChildOptMemoEntry( + ulOptReq, ulSearchStage, prpp, pdrgpoc)); } diff --git a/src/backend/gporca/libgpopt/src/search/CJobGroupExpressionOptimization.cpp b/src/backend/gporca/libgpopt/src/search/CJobGroupExpressionOptimization.cpp index 404a772c1d3..534025337b0 100644 --- a/src/backend/gporca/libgpopt/src/search/CJobGroupExpressionOptimization.cpp +++ b/src/backend/gporca/libgpopt/src/search/CJobGroupExpressionOptimization.cpp @@ -211,6 +211,10 @@ CJobGroupExpressionOptimization::Init(CGroupExpression *pgexpr, m_pdrgpdp = nullptr; m_pexprhdlPlan = nullptr; m_pexprhdlRel = nullptr; + m_fChildOptMemoHit = false; + m_pdrgpocMemo = nullptr; + m_fMemoPredictsPrune = false; + m_fPrunedAfterChild = false; m_eceo = CPhysical::PopConvert(pgexpr->Pop())->Eceo(); m_ulArity = pgexpr->Arity(); m_ulChildIndex = gpos::ulong_max; @@ -246,6 +250,8 @@ CJobGroupExpressionOptimization::Cleanup() CRefCount::SafeRelease(m_pdrgpstatCurrentCtxt); CRefCount::SafeRelease(m_pdrgpdp); CRefCount::SafeRelease(m_prppCTEProducer); + CRefCount::SafeRelease(m_pdrgpocMemo); + m_pdrgpocMemo = nullptr; GPOS_DELETE(m_pexprhdlPlan); GPOS_DELETE(m_pexprhdlRel); } @@ -337,6 +343,48 @@ CJobGroupExpressionOptimization::EevtInitialize(CSchedulerContext *psc, return eevFinalized; } + // reuse the child contexts that the same request reached under another + // context, if the request would reach them under this context as well + CEngine *peng = psc->Peng(); + if (peng->FChildOptMemoApplicable(pjgeo->m_pgexpr, + pjgeo->m_fOptimizeCTESequence)) + { + COptimizationContextArray *pdrgpoc = peng->PdrgpocChildOptMemo( + psc->GetGlobalMemoryPool(), pjgeo->m_pgexpr, pjgeo->m_poc, + pjgeo->m_ulOptReq); + if (nullptr != pdrgpoc) + { + CCost costLowerBoundMemo(GPOPT_INVALID_COST); + BOOL fPruned = peng->FSafeToPruneWithChildOptMemo( + pjgeo->m_pgexpr, pjgeo->m_poc, pdrgpoc, &costLowerBoundMemo); + peng->CountChildOptMemoEvent(fPruned ? CEngine::EcomePrunedHit + : CEngine::EcomeHit); + if (GPOS_FTRACE(EopttraceVerifyChildOptMemo)) + { + // optimize the children anyway, and compare the contexts + // they reach with the reused ones + pjgeo->m_pdrgpocMemo = pdrgpoc; + pjgeo->m_fMemoPredictsPrune = fPruned; + } + else if (fPruned) + { + (void) pjgeo->m_pgexpr->PccComputeCost( + psc->GetGlobalMemoryPool(), pjgeo->m_poc, + pjgeo->m_ulOptReq, nullptr /*pdrgpoc*/, true /*fPruned*/, + costLowerBoundMemo); + pdrgpoc->Release(); + return eevFinalized; + } + else + { + pjgeo->m_pdrgpoc = pdrgpoc; + pjgeo->m_fChildOptMemoHit = true; + pjgeo->SetChildrenScheduled(); + return eevOptimizingChildren; + } + } + } + pjgeo->InitChildGroupsOptimization(psc); return eevOptimizingChildren; @@ -389,6 +437,7 @@ CJobGroupExpressionOptimization::DerivePrevChildProps(CSchedulerContext *psc) m_ulOptReq, nullptr /*pdrgpoc*/, true /*fPruned*/, costLowerBound); m_fChildOptimizationFailed = true; + m_fPrunedAfterChild = true; return; } @@ -544,6 +593,24 @@ CJobGroupExpressionOptimization::EevtOptimizeChildren(CSchedulerContext *psc, pjgeo->ScheduleChildGroupsJobs(psc); if (pjgeo->m_fChildOptimizationFailed) { + if (nullptr != pjgeo->m_pdrgpocMemo) + { + // verify mode: the reused child contexts would have let the + // request continue, or be pruned at another point + if (!pjgeo->m_fPrunedAfterChild) + { + psc->Peng()->ReportChildOptMemoDifference( + CEngine::EcomeVerifyIncomplete, pjgeo->m_pgexpr, + pjgeo->m_poc, pjgeo->m_ulOptReq); + } + else if (!pjgeo->m_fMemoPredictsPrune) + { + psc->Peng()->ReportChildOptMemoDifference( + CEngine::EcomeVerifyPruneDiff, pjgeo->m_pgexpr, + pjgeo->m_poc, pjgeo->m_ulOptReq); + } + } + // failed to optimize child, terminate job pjgeo->Cleanup(); return eevFinalized; @@ -571,10 +638,29 @@ CJobGroupExpressionOptimization::EevtAddEnforcers(CSchedulerContext *psc, // get a job pointer CJobGroupExpressionOptimization *pjgeo = PjConvert(pjOwner); - // build child contexts array - GPOS_ASSERT(nullptr == pjgeo->m_pdrgpoc); - pjgeo->m_pdrgpoc = psc->Peng()->PdrgpocChildren(psc->GetGlobalMemoryPool(), - *pjgeo->m_pexprhdlPlan); + // build child contexts array, unless reused from the child + // optimization memo + if (!pjgeo->m_fChildOptMemoHit) + { + GPOS_ASSERT(nullptr == pjgeo->m_pdrgpoc); + CEngine *peng = psc->Peng(); + pjgeo->m_pdrgpoc = peng->PdrgpocChildren(psc->GetGlobalMemoryPool(), + *pjgeo->m_pexprhdlPlan); + if (nullptr != pjgeo->m_pdrgpocMemo) + { + peng->VerifyChildOptMemo(pjgeo->m_pgexpr, pjgeo->m_poc, + pjgeo->m_ulOptReq, pjgeo->m_pdrgpocMemo, + pjgeo->m_pdrgpoc, + pjgeo->m_fMemoPredictsPrune); + } + if (peng->FChildOptMemoApplicable(pjgeo->m_pgexpr, + pjgeo->m_fOptimizeCTESequence)) + { + peng->AddChildOptMemo(psc->GetGlobalMemoryPool(), pjgeo->m_pgexpr, + pjgeo->m_poc, pjgeo->m_ulOptReq, + pjgeo->m_pdrgpoc); + } + } // enforce physical properties BOOL fCheckEnfdProps = psc->Peng()->FCheckEnfdProps( diff --git a/src/backend/gporca/libnaucrates/include/naucrates/traceflags/traceflags.h b/src/backend/gporca/libnaucrates/include/naucrates/traceflags/traceflags.h index 54a72b082f0..981f4b2d090 100644 --- a/src/backend/gporca/libnaucrates/include/naucrates/traceflags/traceflags.h +++ b/src/backend/gporca/libnaucrates/include/naucrates/traceflags/traceflags.h @@ -261,6 +261,14 @@ enum EOptTraceFlag // so that they don't need to sort it themselves on the coordinator EopttraceDisablePresortedAgg = 103055, + // reuse the child contexts that the same request of a group expression + // reached under another context, instead of optimizing the children again + EopttraceChildOptMemo = 103070, + + // optimize the children of requests that could reuse child contexts, and + // report when the child contexts differ from the reused ones + EopttraceVerifyChildOptMemo = 103071, + /////////////////////////////////////////////////////// ///////////////////// statistics flags //////////////// ////////////////////////////////////////////////////// diff --git a/src/backend/utils/misc/guc_gp.c b/src/backend/utils/misc/guc_gp.c index aa3042ea938..11f5588b79c 100644 --- a/src/backend/utils/misc/guc_gp.c +++ b/src/backend/utils/misc/guc_gp.c @@ -153,6 +153,8 @@ bool enable_parallel_semi_join = true; bool enable_parallel_dedup_semi_join = true; bool enable_parallel_dedup_semi_reverse_join = true; bool parallel_query_use_streaming_hashagg = false; +bool optimizer_enable_child_opt_memo = true; +bool optimizer_verify_child_opt_memo = false; bool gp_use_streaming_hashagg = true; bool optimizer_use_streaming_hashagg = true; int gp_appendonly_insert_files = 0; @@ -3260,6 +3262,26 @@ struct config_bool ConfigureNamesBool_gp[] = true, NULL, NULL, NULL }, + { + {"optimizer_enable_child_opt_memo", PGC_USERSET, DEVELOPER_OPTIONS, + gettext_noop("Reuse the child contexts that the same request of a group expression reached under another context."), + NULL, + GUC_NO_SHOW_ALL | GUC_NOT_IN_SAMPLE + }, + &optimizer_enable_child_opt_memo, + true, NULL, NULL + }, + + { + {"optimizer_verify_child_opt_memo", PGC_USERSET, DEVELOPER_OPTIONS, + gettext_noop("Optimize the children of requests that could reuse child contexts and report differences."), + NULL, + GUC_NO_SHOW_ALL | GUC_NOT_IN_SAMPLE + }, + &optimizer_verify_child_opt_memo, + false, NULL, NULL + }, + { {"parallel_query_use_streaming_hashagg", PGC_USERSET, QUERY_TUNING_METHOD, gettext_noop("allow to use of streaming hashagg in parallel query for DISTINCT."), diff --git a/src/include/utils/guc.h b/src/include/utils/guc.h index 226beabaf89..980eb4340cc 100644 --- a/src/include/utils/guc.h +++ b/src/include/utils/guc.h @@ -300,6 +300,8 @@ extern bool enable_parallel_semi_join; extern bool enable_parallel_dedup_semi_join; extern bool enable_parallel_dedup_semi_reverse_join; extern bool parallel_query_use_streaming_hashagg; +extern bool optimizer_enable_child_opt_memo; +extern bool optimizer_verify_child_opt_memo; extern bool gp_use_streaming_hashagg; extern bool optimizer_use_streaming_hashagg; extern int gp_appendonly_insert_files; diff --git a/src/include/utils/unsync_guc_name.h b/src/include/utils/unsync_guc_name.h index a82fe9b7afd..e2fa575da4f 100644 --- a/src/include/utils/unsync_guc_name.h +++ b/src/include/utils/unsync_guc_name.h @@ -404,6 +404,7 @@ "optimizer_enable_associativity", "optimizer_enable_bitmapscan", "optimizer_enable_broadcast_nestloop_outer_child", + "optimizer_enable_child_opt_memo", "optimizer_enable_constant_expression_evaluation", "optimizer_enable_ctas", "optimizer_enable_derive_stats_all_groups", @@ -513,6 +514,7 @@ "optimizer_use_external_constant_expression_evaluation_for_ints", "optimizer_use_gpdb_allocators", "optimizer_use_streaming_hashagg", + "optimizer_verify_child_opt_memo", "optimizer_xform_bind_threshold", "parallel_leader_participation", "parallel_query_use_streaming_hashagg", From e33f99efcf6ee23d1e3018fd30577b9639287163 Mon Sep 17 00:00:00 2001 From: Jianghua Yang Date: Fri, 4 Sep 2026 06:48:14 +0800 Subject: [PATCH 03/27] fix(orca): never build index conditions on INCLUDE-only index columns Since 37224a3d171 CXformUtils::PdrgpcrIndexKeys() has been a thin wrapper around PdrgpcrIndexColumns(), which appends the index's INCLUDE columns after its key columns. CPredicateUtils::ExtractIndexPredicates() therefore treated a predicate on an INCLUDE-only column as indexable. Ordinary comparisons were still rejected later by CMDIndexGPDB::IsCompatible(), but the shortcuts for boolean column references (col -> col = true, NOT col -> col = false) and for IS [NOT] NULL bypass that check and went straight into the index condition list. The DXL-to-plan translator then maps the column through GetKeyPos(), which returns ulong_max for a non-key column, so the Var's varattno wrapped to 0 and execution failed with ERROR: bogus index qualification (nodeIndexscan.c:1250) or, for IS [NOT] NULL, ERROR: btree index keys must be ordered by attribute (nbtutils.c:777) Restore the key-only semantics: reintroduce the EIndexCols selector (EicKey / EicKeyAndIncluded) on PdrgpcrIndexColumns()/PcrsIndexColumns() and make PdrgpcrIndexKeys()/PcrsIndexKeys() request key columns only. The cost model keeps asking for key + INCLUDE columns so that ComputeUnusedIndexWeight() costing is unchanged. Index-only-scan coverage checks are unaffected: they already use PcrsIndexReturnableColumns(). Add regression coverage to gp_covering_index. Fixes #1948 --- .../gporca/libgpdbcost/src/CCostModelGPDB.cpp | 8 +- .../include/gpopt/xforms/CXformUtils.h | 37 +++++-- .../libgpopt/src/xforms/CXformUtils.cpp | 28 ++++-- .../regress/expected/gp_covering_index.out | 98 +++++++++++++++++++ .../expected/gp_covering_index_optimizer.out | 98 +++++++++++++++++++ src/test/regress/sql/gp_covering_index.sql | 36 +++++++ 6 files changed, 284 insertions(+), 21 deletions(-) diff --git a/src/backend/gporca/libgpdbcost/src/CCostModelGPDB.cpp b/src/backend/gporca/libgpdbcost/src/CCostModelGPDB.cpp index f4dad6c59df..b42b75aa65c 100644 --- a/src/backend/gporca/libgpdbcost/src/CCostModelGPDB.cpp +++ b/src/backend/gporca/libgpdbcost/src/CCostModelGPDB.cpp @@ -1824,8 +1824,12 @@ CCostModelGPDB::GetCommonIndexData(T *ptr, ULONG &ulIndexKeys, const IMDIndex *pmdindex = md_accessor->RetrieveIndex(ptr->Pindexdesc()->MDId()); - pdrgpcrIndexColumns = CXformUtils::PdrgpcrIndexKeys( - mp, ptr->PdrgpcrOutput(), pmdindex, pmdrel); + // Costing has always weighed every column stored in the index (keys and + // INCLUDE columns alike) in ComputeUnusedIndexWeight; keep that behavior + // here so plan costs are unaffected by PdrgpcrIndexKeys becoming key-only. + pdrgpcrIndexColumns = CXformUtils::PdrgpcrIndexColumns( + mp, ptr->PdrgpcrOutput(), pmdindex, pmdrel, + CXformUtils::EicKeyAndIncluded); stats = ptr->PstatsBaseTable(); } diff --git a/src/backend/gporca/libgpopt/include/gpopt/xforms/CXformUtils.h b/src/backend/gporca/libgpopt/include/gpopt/xforms/CXformUtils.h index 6494cfc992a..92f41a5530c 100644 --- a/src/backend/gporca/libgpopt/include/gpopt/xforms/CXformUtils.h +++ b/src/backend/gporca/libgpopt/include/gpopt/xforms/CXformUtils.h @@ -68,6 +68,19 @@ using CExpressionArrays = CDynamicPtrArray; //--------------------------------------------------------------------------- class CXformUtils { +public: + // which columns of an index to consider + enum EIndexCols + { + // KEY columns only: these are the only columns an index condition + // (index qual) may be built on + EicKey, + + // KEY columns followed by INCLUDE ("payload") columns: everything + // physically stored in the index + EicKeyAndIncluded + }; + private: // create a logical assert for the not nullable columns of the given table // on top of the given child expression @@ -83,11 +96,12 @@ class CXformUtils CColRefArray *colref_array); // return the set of columns from the given array of columns which appear - // in the index included / key columns + // in the index columns of the specified type (key only, or key + included) static CColRefSet *PcrsIndexColumns(CMemoryPool *mp, CColRefArray *colref_array, const IMDIndex *pmdindex, - const IMDRelation *pmdrel); + const IMDRelation *pmdrel, + EIndexCols eic); // return the set of columns from the given array of columns which are // returnable through the index (to determine index-only scan capable) @@ -96,13 +110,6 @@ class CXformUtils const IMDIndex *pmdindex, const IMDRelation *pmdrel); - // return the ordered array of columns from the given array of columns which appear - // in the index included / key columns - static CColRefArray *PdrgpcrIndexColumns(CMemoryPool *mp, - CColRefArray *colref_array, - const IMDIndex *pmdindex, - const IMDRelation *pmdrel); - // lookup join keys in scalar child group static void LookupJoinKeys(CMemoryPool *mp, CExpression *pexpr, CExpressionArray **ppdrgpexprOuter, @@ -369,8 +376,18 @@ class CXformUtils static CWStringConst *PstrErrorMessage(CMemoryPool *mp, ULONG major, ULONG minor, ...); + // return the ordered array of columns from the given array of columns which + // appear in the index columns of the specified type (key only, or + // key + included) + static CColRefArray *PdrgpcrIndexColumns(CMemoryPool *mp, + CColRefArray *colref_array, + const IMDIndex *pmdindex, + const IMDRelation *pmdrel, + EIndexCols eic); + // return the array of key columns from the given array of columns which appear - // in the index key columns + // in the index key columns (INCLUDE columns are deliberately excluded: + // a predicate on them must never become an index condition) static CColRefArray *PdrgpcrIndexKeys(CMemoryPool *mp, CColRefArray *colref_array, const IMDIndex *pmdindex, diff --git a/src/backend/gporca/libgpopt/src/xforms/CXformUtils.cpp b/src/backend/gporca/libgpopt/src/xforms/CXformUtils.cpp index b810b058f4d..3f8b8005586 100644 --- a/src/backend/gporca/libgpopt/src/xforms/CXformUtils.cpp +++ b/src/backend/gporca/libgpopt/src/xforms/CXformUtils.cpp @@ -1784,7 +1784,9 @@ CXformUtils::PstrErrorMessage(CMemoryPool *mp, ULONG major, ULONG minor, ...) // // @doc: // Return the array of columns from the given array of columns which appear -// in the index key columns +// in the index key columns. INCLUDE columns are not keys: the executor +// only accepts index quals on key attributes (see ExecIndexBuildScanKeys), +// so callers building index conditions must never see them here. // //--------------------------------------------------------------------------- CColRefArray * @@ -1792,7 +1794,7 @@ CXformUtils::PdrgpcrIndexKeys(CMemoryPool *mp, CColRefArray *colref_array, const IMDIndex *pmdindex, const IMDRelation *pmdrel) { - return PdrgpcrIndexColumns(mp, colref_array, pmdindex, pmdrel); + return PdrgpcrIndexColumns(mp, colref_array, pmdindex, pmdrel, EicKey); } //--------------------------------------------------------------------------- @@ -1808,7 +1810,7 @@ CColRefSet * CXformUtils::PcrsIndexKeys(CMemoryPool *mp, CColRefArray *colref_array, const IMDIndex *pmdindex, const IMDRelation *pmdrel) { - return PcrsIndexColumns(mp, colref_array, pmdindex, pmdrel); + return PcrsIndexColumns(mp, colref_array, pmdindex, pmdrel, EicKey); } //--------------------------------------------------------------------------- @@ -1817,16 +1819,16 @@ CXformUtils::PcrsIndexKeys(CMemoryPool *mp, CColRefArray *colref_array, // // @doc: // Return the set of columns from the given array of columns which appear -// in the index columns of the specified type (included / key) +// in the index columns of the specified type (key only / key + included) // //--------------------------------------------------------------------------- CColRefSet * CXformUtils::PcrsIndexColumns(CMemoryPool *mp, CColRefArray *colref_array, const IMDIndex *pmdindex, - const IMDRelation *pmdrel) + const IMDRelation *pmdrel, EIndexCols eic) { CColRefArray *pdrgpcrIndexColumns = - PdrgpcrIndexColumns(mp, colref_array, pmdindex, pmdrel); + PdrgpcrIndexColumns(mp, colref_array, pmdindex, pmdrel, eic); CColRefSet *pcrsCols = GPOS_NEW(mp) CColRefSet(mp, pdrgpcrIndexColumns); pdrgpcrIndexColumns->Release(); @@ -1836,7 +1838,7 @@ CXformUtils::PcrsIndexColumns(CMemoryPool *mp, CColRefArray *colref_array, //--------------------------------------------------------------------------- // @function: -// CXformUtils::PdrgpcrIndexColumns +// CXformUtils::PcrsIndexReturnableColumns // // @doc: // Return the set of columns from the given array of columns which are @@ -1872,14 +1874,17 @@ CXformUtils::PcrsIndexReturnableColumns(CMemoryPool *mp, // // @doc: // Return the ordered list of columns from the given array of columns which -// appear in the index columns of the specified type (included / key) +// appear in the index columns of the specified type: key columns only +// (EicKey), or key columns followed by INCLUDE columns (EicKeyAndIncluded) // //--------------------------------------------------------------------------- CColRefArray * CXformUtils::PdrgpcrIndexColumns(CMemoryPool *mp, CColRefArray *colref_array, const IMDIndex *pmdindex, - const IMDRelation *pmdrel) + const IMDRelation *pmdrel, EIndexCols eic) { + GPOS_ASSERT(EicKey == eic || EicKeyAndIncluded == eic); + CColRefArray *pdrgpcrIndex = GPOS_NEW(mp) CColRefArray(mp); // key columns @@ -1894,6 +1899,11 @@ CXformUtils::PdrgpcrIndexColumns(CMemoryPool *mp, CColRefArray *colref_array, pdrgpcrIndex->Append(colref); } + if (EicKey == eic) + { + return pdrgpcrIndex; + } + // included columns for (ULONG ul = 0; ul < pmdindex->IncludedCols(); ul++) { diff --git a/src/test/regress/expected/gp_covering_index.out b/src/test/regress/expected/gp_covering_index.out index e67cd8d7ad1..b904d0db43c 100644 --- a/src/test/regress/expected/gp_covering_index.out +++ b/src/test/regress/expected/gp_covering_index.out @@ -837,5 +837,103 @@ SELECT b FROM test_combined_index_scan WHERE a < 42 OR b < 42; Optimizer: Postgres query optimizer (6 rows) +-- Test predicates on INCLUDE-only columns (https://github.com/apache/cloudberry/issues/1948) +-- +-- A predicate that only references INCLUDE ("payload") columns must never be +-- turned into an index condition: the executor rejects index quals on non-key +-- attributes ("bogus index qualification"). Boolean column references and +-- IS [NOT] NULL tests used to slip through ORCA's index predicate extraction, +-- so cover them explicitly. +CREATE TABLE test_include_col_pred(c0 boolean, c1 boolean, c2 int) DISTRIBUTED BY (c2); +CREATE INDEX i_test_include_col_pred ON test_include_col_pred(c0) INCLUDE (c1); +INSERT INTO test_include_col_pred VALUES (true, true, 1), (false, true, 2), (true, false, 3), (true, NULL, 4); +VACUUM ANALYZE test_include_col_pred; +-- KEYS: [c0] INCLUDED: [c1] +EXPLAIN (COSTS OFF) +SELECT * FROM test_include_col_pred WHERE c1; + QUERY PLAN +------------------------------------------ + Gather Motion 3:1 (slice1; segments: 3) + -> Seq Scan on test_include_col_pred + Filter: c1 + Optimizer: Postgres query optimizer +(4 rows) + +SELECT * FROM test_include_col_pred WHERE c1 ORDER BY c2; + c0 | c1 | c2 +----+----+---- + t | t | 1 + f | t | 2 +(2 rows) + +EXPLAIN (COSTS OFF) +SELECT * FROM test_include_col_pred WHERE NOT c1; + QUERY PLAN +------------------------------------------ + Gather Motion 3:1 (slice1; segments: 3) + -> Seq Scan on test_include_col_pred + Filter: (NOT c1) + Optimizer: Postgres query optimizer +(4 rows) + +SELECT * FROM test_include_col_pred WHERE NOT c1 ORDER BY c2; + c0 | c1 | c2 +----+----+---- + t | f | 3 +(1 row) + +EXPLAIN (COSTS OFF) +SELECT * FROM test_include_col_pred WHERE c1 IS NULL; + QUERY PLAN +------------------------------------------ + Gather Motion 3:1 (slice1; segments: 3) + -> Seq Scan on test_include_col_pred + Filter: (c1 IS NULL) + Optimizer: Postgres query optimizer +(4 rows) + +SELECT * FROM test_include_col_pred WHERE c1 IS NULL ORDER BY c2; + c0 | c1 | c2 +----+----+---- + t | | 4 +(1 row) + +EXPLAIN (COSTS OFF) +SELECT c1 FROM test_include_col_pred WHERE c1 IS NOT NULL; + QUERY PLAN +------------------------------------------------------------------------------ + Gather Motion 3:1 (slice1; segments: 3) + -> Index Only Scan using i_test_include_col_pred on test_include_col_pred + Filter: (c1 IS NOT NULL) + Optimizer: Postgres query optimizer +(4 rows) + +SELECT c1 FROM test_include_col_pred WHERE c1 IS NOT NULL ORDER BY 1; + c1 +---- + f + t + t +(3 rows) + +-- key predicate combined with an INCLUDE-column predicate: only the key +-- predicate may become an index condition, the rest must stay a filter +EXPLAIN (COSTS OFF) +SELECT * FROM test_include_col_pred WHERE c0 AND c1; + QUERY PLAN +------------------------------------------------------------------------- + Gather Motion 3:1 (slice1; segments: 3) + -> Index Scan using i_test_include_col_pred on test_include_col_pred + Index Cond: (c0 = true) + Filter: c1 + Optimizer: Postgres query optimizer +(5 rows) + +SELECT * FROM test_include_col_pred WHERE c0 AND c1 ORDER BY c2; + c0 | c1 | c2 +----+----+---- + t | t | 1 +(1 row) + reset optimizer_trace_fallback; reset enable_seqscan; diff --git a/src/test/regress/expected/gp_covering_index_optimizer.out b/src/test/regress/expected/gp_covering_index_optimizer.out index d709e659256..a543af2d140 100644 --- a/src/test/regress/expected/gp_covering_index_optimizer.out +++ b/src/test/regress/expected/gp_covering_index_optimizer.out @@ -795,5 +795,103 @@ SELECT b FROM test_combined_index_scan WHERE a < 42 OR b < 42; Optimizer: Pivotal Optimizer (GPORCA) (5 rows) +-- Test predicates on INCLUDE-only columns (https://github.com/apache/cloudberry/issues/1948) +-- +-- A predicate that only references INCLUDE ("payload") columns must never be +-- turned into an index condition: the executor rejects index quals on non-key +-- attributes ("bogus index qualification"). Boolean column references and +-- IS [NOT] NULL tests used to slip through ORCA's index predicate extraction, +-- so cover them explicitly. +CREATE TABLE test_include_col_pred(c0 boolean, c1 boolean, c2 int) DISTRIBUTED BY (c2); +CREATE INDEX i_test_include_col_pred ON test_include_col_pred(c0) INCLUDE (c1); +INSERT INTO test_include_col_pred VALUES (true, true, 1), (false, true, 2), (true, false, 3), (true, NULL, 4); +VACUUM ANALYZE test_include_col_pred; +-- KEYS: [c0] INCLUDED: [c1] +EXPLAIN (COSTS OFF) +SELECT * FROM test_include_col_pred WHERE c1; + QUERY PLAN +------------------------------------------ + Gather Motion 3:1 (slice1; segments: 3) + -> Seq Scan on test_include_col_pred + Filter: c1 + Optimizer: GPORCA +(4 rows) + +SELECT * FROM test_include_col_pred WHERE c1 ORDER BY c2; + c0 | c1 | c2 +----+----+---- + t | t | 1 + f | t | 2 +(2 rows) + +EXPLAIN (COSTS OFF) +SELECT * FROM test_include_col_pred WHERE NOT c1; + QUERY PLAN +------------------------------------------ + Gather Motion 3:1 (slice1; segments: 3) + -> Seq Scan on test_include_col_pred + Filter: (NOT c1) + Optimizer: GPORCA +(4 rows) + +SELECT * FROM test_include_col_pred WHERE NOT c1 ORDER BY c2; + c0 | c1 | c2 +----+----+---- + t | f | 3 +(1 row) + +EXPLAIN (COSTS OFF) +SELECT * FROM test_include_col_pred WHERE c1 IS NULL; + QUERY PLAN +------------------------------------------ + Gather Motion 3:1 (slice1; segments: 3) + -> Seq Scan on test_include_col_pred + Filter: (c1 IS NULL) + Optimizer: GPORCA +(4 rows) + +SELECT * FROM test_include_col_pred WHERE c1 IS NULL ORDER BY c2; + c0 | c1 | c2 +----+----+---- + t | | 4 +(1 row) + +EXPLAIN (COSTS OFF) +SELECT c1 FROM test_include_col_pred WHERE c1 IS NOT NULL; + QUERY PLAN +------------------------------------------ + Gather Motion 3:1 (slice1; segments: 3) + -> Seq Scan on test_include_col_pred + Filter: (NOT (c1 IS NULL)) + Optimizer: GPORCA +(4 rows) + +SELECT c1 FROM test_include_col_pred WHERE c1 IS NOT NULL ORDER BY 1; + c1 +---- + f + t + t +(3 rows) + +-- key predicate combined with an INCLUDE-column predicate: only the key +-- predicate may become an index condition, the rest must stay a filter +EXPLAIN (COSTS OFF) +SELECT * FROM test_include_col_pred WHERE c0 AND c1; + QUERY PLAN +------------------------------------------------------------------------- + Gather Motion 3:1 (slice1; segments: 3) + -> Index Scan using i_test_include_col_pred on test_include_col_pred + Index Cond: (c0 = true) + Filter: c1 + Optimizer: GPORCA +(5 rows) + +SELECT * FROM test_include_col_pred WHERE c0 AND c1 ORDER BY c2; + c0 | c1 | c2 +----+----+---- + t | t | 1 +(1 row) + reset optimizer_trace_fallback; reset enable_seqscan; diff --git a/src/test/regress/sql/gp_covering_index.sql b/src/test/regress/sql/gp_covering_index.sql index defdbb2a12d..5585b4cc60d 100644 --- a/src/test/regress/sql/gp_covering_index.sql +++ b/src/test/regress/sql/gp_covering_index.sql @@ -443,5 +443,41 @@ EXPLAIN (ANALYZE, COSTS OFF, TIMING OFF, SUMMARY OFF) SELECT b FROM test_combined_index_scan WHERE a < 42 OR b < 42; +-- Test predicates on INCLUDE-only columns (https://github.com/apache/cloudberry/issues/1948) +-- +-- A predicate that only references INCLUDE ("payload") columns must never be +-- turned into an index condition: the executor rejects index quals on non-key +-- attributes ("bogus index qualification"). Boolean column references and +-- IS [NOT] NULL tests used to slip through ORCA's index predicate extraction, +-- so cover them explicitly. +CREATE TABLE test_include_col_pred(c0 boolean, c1 boolean, c2 int) DISTRIBUTED BY (c2); +CREATE INDEX i_test_include_col_pred ON test_include_col_pred(c0) INCLUDE (c1); +INSERT INTO test_include_col_pred VALUES (true, true, 1), (false, true, 2), (true, false, 3), (true, NULL, 4); +VACUUM ANALYZE test_include_col_pred; + +-- KEYS: [c0] INCLUDED: [c1] +EXPLAIN (COSTS OFF) +SELECT * FROM test_include_col_pred WHERE c1; +SELECT * FROM test_include_col_pred WHERE c1 ORDER BY c2; + +EXPLAIN (COSTS OFF) +SELECT * FROM test_include_col_pred WHERE NOT c1; +SELECT * FROM test_include_col_pred WHERE NOT c1 ORDER BY c2; + +EXPLAIN (COSTS OFF) +SELECT * FROM test_include_col_pred WHERE c1 IS NULL; +SELECT * FROM test_include_col_pred WHERE c1 IS NULL ORDER BY c2; + +EXPLAIN (COSTS OFF) +SELECT c1 FROM test_include_col_pred WHERE c1 IS NOT NULL; +SELECT c1 FROM test_include_col_pred WHERE c1 IS NOT NULL ORDER BY 1; + +-- key predicate combined with an INCLUDE-column predicate: only the key +-- predicate may become an index condition, the rest must stay a filter +EXPLAIN (COSTS OFF) +SELECT * FROM test_include_col_pred WHERE c0 AND c1; +SELECT * FROM test_include_col_pred WHERE c0 AND c1 ORDER BY c2; + + reset optimizer_trace_fallback; reset enable_seqscan; From 99fa6737af94c4ac7196551bf45dcd9f9e8c0ec3 Mon Sep 17 00:00:00 2001 From: Jianghua Yang Date: Fri, 4 Sep 2026 10:03:18 +0800 Subject: [PATCH 04/27] test: pin index-only scan choice in memoize expr_key case for ORCA memoize_optimizer.out expects an Index Scan on expr_key_idx_x_t for the expr_key join, but ORCA chooses an Index Only Scan whenever the preceding VACUUM managed to mark expr_key all-visible. Whether it can depends on the snapshots held by tests running concurrently in the same parallel group, so the test intermittently fails with - -> Index Scan using expr_key_idx_x_t on expr_key t2 ... + -> Index Only Scan using expr_key_idx_x_t on expr_key t2 ... + Heap Fetches: N The scan type is irrelevant to what the test checks (memoize cache hits and misses on an expression key), so disable optimizer_enable_indexonlyscan around that query. The GUC only affects ORCA; the planner output merely gains the echoed SET/RESET lines. --- src/test/regress/expected/memoize.out | 5 +++++ src/test/regress/expected/memoize_optimizer.out | 5 +++++ src/test/regress/sql/memoize.sql | 5 +++++ 3 files changed, 15 insertions(+) diff --git a/src/test/regress/expected/memoize.out b/src/test/regress/expected/memoize.out index 96e6737be27..afebea20104 100644 --- a/src/test/regress/expected/memoize.out +++ b/src/test/regress/expected/memoize.out @@ -118,6 +118,10 @@ SELECT d1::numeric, d1::text FROM ( INSERT INTO expr_key SELECT * FROM expr_key; CREATE INDEX expr_key_idx_x_t ON expr_key (x, t); VACUUM ANALYZE expr_key; +-- ORCA may pick either an Index Scan or an Index Only Scan for the inner +-- side depending on how much of expr_key the VACUUM above could mark +-- all-visible, which varies with concurrently running tests. Pin the plan. +SET optimizer_enable_indexonlyscan TO off; -- Ensure we get we get a cache miss and hit for each of the 20 distinct values SELECT explain_memoize(' SELECT * FROM expr_key t1 INNER JOIN expr_key t2 @@ -141,6 +145,7 @@ ON t1.x = t2.t::numeric AND t1.t::numeric = t2.x;', false); Optimizer: Postgres query optimizer (15 rows) +RESET optimizer_enable_indexonlyscan; DROP TABLE expr_key; -- Reduce work_mem and hash_mem_multiplier so that we see some cache evictions SET work_mem TO '64kB'; diff --git a/src/test/regress/expected/memoize_optimizer.out b/src/test/regress/expected/memoize_optimizer.out index 08ac4f997ef..6dd4bc6ce5c 100644 --- a/src/test/regress/expected/memoize_optimizer.out +++ b/src/test/regress/expected/memoize_optimizer.out @@ -115,6 +115,10 @@ SELECT d1::numeric, d1::text FROM ( INSERT INTO expr_key SELECT * FROM expr_key; CREATE INDEX expr_key_idx_x_t ON expr_key (x, t); VACUUM ANALYZE expr_key; +-- ORCA may pick either an Index Scan or an Index Only Scan for the inner +-- side depending on how much of expr_key the VACUUM above could mark +-- all-visible, which varies with concurrently running tests. Pin the plan. +SET optimizer_enable_indexonlyscan TO off; -- Ensure we get we get a cache miss and hit for each of the 20 distinct values SELECT explain_memoize(' SELECT * FROM expr_key t1 INNER JOIN expr_key t2 @@ -133,6 +137,7 @@ ON t1.x = t2.t::numeric AND t1.t::numeric = t2.x;', false); Optimizer: GPORCA (10 rows) +RESET optimizer_enable_indexonlyscan; DROP TABLE expr_key; -- Reduce work_mem and hash_mem_multiplier so that we see some cache evictions SET work_mem TO '64kB'; diff --git a/src/test/regress/sql/memoize.sql b/src/test/regress/sql/memoize.sql index 683889d8814..2829a7b52f4 100644 --- a/src/test/regress/sql/memoize.sql +++ b/src/test/regress/sql/memoize.sql @@ -79,11 +79,16 @@ INSERT INTO expr_key SELECT * FROM expr_key; CREATE INDEX expr_key_idx_x_t ON expr_key (x, t); VACUUM ANALYZE expr_key; +-- ORCA may pick either an Index Scan or an Index Only Scan for the inner +-- side depending on how much of expr_key the VACUUM above could mark +-- all-visible, which varies with concurrently running tests. Pin the plan. +SET optimizer_enable_indexonlyscan TO off; -- Ensure we get we get a cache miss and hit for each of the 20 distinct values SELECT explain_memoize(' SELECT * FROM expr_key t1 INNER JOIN expr_key t2 ON t1.x = t2.t::numeric AND t1.t::numeric = t2.x;', false); +RESET optimizer_enable_indexonlyscan; DROP TABLE expr_key; -- Reduce work_mem and hash_mem_multiplier so that we see some cache evictions From d54c2681e2cc6bba26087ccb947a39d3bc79ad9e Mon Sep 17 00:00:00 2001 From: Jianghua Yang Date: Thu, 22 Oct 2026 03:54:13 +0800 Subject: [PATCH 05/27] fix(orca): keep INCLUDE columns in the bitmap index width comparison PexprBitmapSelectBestIndex() breaks ties between equally selective bitmap index candidates by the number of index columns, as a proxy for how big the index is on disk. It got that number from PdrgpcrIndexKeys(), which the previous commit made key-only, so an index with INCLUDE columns now looked exactly as narrow as one without. Two indexes on the same key then tie, and the first one in pg_index order wins - which can be the much wider one: CREATE INDEX wide ON t(a) INCLUDE (payload); -- 12 MB CREATE INDEX narrow ON t(a); -- 720 kB SELECT * FROM t WHERE a IN (1, 100, 1000) OR b = 17; -> Bitmap Index Scan on wide -- was: narrow INCLUDE columns are stored in every index tuple, so the width comparison must count them; only index *conditions* have to stay key-only. Ask for EicKeyAndIncluded there. Key columns still come first in the array, so the adjacent first-index-key check is unaffected. Also fix a typo in the PcrsIndexReturnableColumns comment. Reported-by: Alena0704 --- .../libgpopt/src/xforms/CXformUtils.cpp | 13 +++++++-- .../regress/expected/gp_covering_index.out | 28 +++++++++++++++++++ .../expected/gp_covering_index_optimizer.out | 28 +++++++++++++++++++ src/test/regress/sql/gp_covering_index.sql | 17 +++++++++++ 4 files changed, 83 insertions(+), 3 deletions(-) diff --git a/src/backend/gporca/libgpopt/src/xforms/CXformUtils.cpp b/src/backend/gporca/libgpopt/src/xforms/CXformUtils.cpp index 3f8b8005586..83d8662bc51 100644 --- a/src/backend/gporca/libgpopt/src/xforms/CXformUtils.cpp +++ b/src/backend/gporca/libgpopt/src/xforms/CXformUtils.cpp @@ -1842,7 +1842,7 @@ CXformUtils::PcrsIndexColumns(CMemoryPool *mp, CColRefArray *colref_array, // // @doc: // Return the set of columns from the given array of columns which are -// retunable through the index (to determine index-only scan capable) +// returnable through the index (to determine index-only scan capable) // //--------------------------------------------------------------------------- CColRefSet * @@ -3036,8 +3036,15 @@ CXformUtils::PexprBitmapSelectBestIndex( continue; } - CColRefArray *indexColumns = CXformUtils::PdrgpcrIndexKeys( - mp, pdrgpcrOutput, pmdindex, pmdrel); + // This array is used below for two things: to check the first key + // of the index, and to compare candidate indexes by width. The + // width comparison is a proxy for how big the index is on disk, so + // it must count the INCLUDE columns as well - they are stored in + // every index tuple. Key columns still come first, so looking at + // element 0 for the first key stays correct. + CColRefArray *indexColumns = CXformUtils::PdrgpcrIndexColumns( + mp, pdrgpcrOutput, pmdindex, pmdrel, + CXformUtils::EicKeyAndIncluded); // make sure the first key of index is included in the scalar predicate // (except for BRIN, which are symmetrical) diff --git a/src/test/regress/expected/gp_covering_index.out b/src/test/regress/expected/gp_covering_index.out index b904d0db43c..f3bf8903654 100644 --- a/src/test/regress/expected/gp_covering_index.out +++ b/src/test/regress/expected/gp_covering_index.out @@ -935,5 +935,33 @@ SELECT * FROM test_include_col_pred WHERE c0 AND c1 ORDER BY c2; t | t | 1 (1 row) +-- When two indexes have the same keys, the bitmap candidate with fewer stored +-- columns must still win: INCLUDE columns make the index wider on disk even +-- though they can never be used in an index condition. i_bmidx_wide is created +-- first on purpose, so a tie on width would hand it the plan. +CREATE TABLE test_bitmap_include_width(a int, b int, payload text) DISTRIBUTED BY (a); +CREATE INDEX i_bmidx_wide ON test_bitmap_include_width(a) INCLUDE (payload); +CREATE INDEX i_bmidx_narrow ON test_bitmap_include_width(a); +CREATE INDEX i_bmidx_b ON test_bitmap_include_width(b); +INSERT INTO test_bitmap_include_width SELECT i, i, repeat('x', 100) FROM generate_series(1, 2000) i; +VACUUM ANALYZE test_bitmap_include_width; +-- KEYS: [a] INCLUDED: [payload] +-- KEYS: [a] +-- KEYS: [b] +EXPLAIN (COSTS OFF) +SELECT * FROM test_bitmap_include_width WHERE a IN (1, 100, 1000) OR b = 17; + QUERY PLAN +--------------------------------------------------------------------------- + Gather Motion 3:1 (slice1; segments: 3) + -> Bitmap Heap Scan on test_bitmap_include_width + Recheck Cond: ((a = ANY ('{1,100,1000}'::integer[])) OR (b = 17)) + -> BitmapOr + -> Bitmap Index Scan on i_bmidx_narrow + Index Cond: (a = ANY ('{1,100,1000}'::integer[])) + -> Bitmap Index Scan on i_bmidx_b + Index Cond: (b = 17) + Optimizer: Postgres query optimizer +(9 rows) + reset optimizer_trace_fallback; reset enable_seqscan; diff --git a/src/test/regress/expected/gp_covering_index_optimizer.out b/src/test/regress/expected/gp_covering_index_optimizer.out index a543af2d140..a27737cc2c3 100644 --- a/src/test/regress/expected/gp_covering_index_optimizer.out +++ b/src/test/regress/expected/gp_covering_index_optimizer.out @@ -893,5 +893,33 @@ SELECT * FROM test_include_col_pred WHERE c0 AND c1 ORDER BY c2; t | t | 1 (1 row) +-- When two indexes have the same keys, the bitmap candidate with fewer stored +-- columns must still win: INCLUDE columns make the index wider on disk even +-- though they can never be used in an index condition. i_bmidx_wide is created +-- first on purpose, so a tie on width would hand it the plan. +CREATE TABLE test_bitmap_include_width(a int, b int, payload text) DISTRIBUTED BY (a); +CREATE INDEX i_bmidx_wide ON test_bitmap_include_width(a) INCLUDE (payload); +CREATE INDEX i_bmidx_narrow ON test_bitmap_include_width(a); +CREATE INDEX i_bmidx_b ON test_bitmap_include_width(b); +INSERT INTO test_bitmap_include_width SELECT i, i, repeat('x', 100) FROM generate_series(1, 2000) i; +VACUUM ANALYZE test_bitmap_include_width; +-- KEYS: [a] INCLUDED: [payload] +-- KEYS: [a] +-- KEYS: [b] +EXPLAIN (COSTS OFF) +SELECT * FROM test_bitmap_include_width WHERE a IN (1, 100, 1000) OR b = 17; + QUERY PLAN +--------------------------------------------------------------------------- + Gather Motion 3:1 (slice1; segments: 3) + -> Bitmap Heap Scan on test_bitmap_include_width + Recheck Cond: ((a = ANY ('{1,100,1000}'::integer[])) OR (b = 17)) + -> BitmapOr + -> Bitmap Index Scan on i_bmidx_narrow + Index Cond: (a = ANY ('{1,100,1000}'::integer[])) + -> Bitmap Index Scan on i_bmidx_b + Index Cond: (b = 17) + Optimizer: GPORCA +(9 rows) + reset optimizer_trace_fallback; reset enable_seqscan; diff --git a/src/test/regress/sql/gp_covering_index.sql b/src/test/regress/sql/gp_covering_index.sql index 5585b4cc60d..b927da0e022 100644 --- a/src/test/regress/sql/gp_covering_index.sql +++ b/src/test/regress/sql/gp_covering_index.sql @@ -478,6 +478,23 @@ EXPLAIN (COSTS OFF) SELECT * FROM test_include_col_pred WHERE c0 AND c1; SELECT * FROM test_include_col_pred WHERE c0 AND c1 ORDER BY c2; +-- When two indexes have the same keys, the bitmap candidate with fewer stored +-- columns must still win: INCLUDE columns make the index wider on disk even +-- though they can never be used in an index condition. i_bmidx_wide is created +-- first on purpose, so a tie on width would hand it the plan. +CREATE TABLE test_bitmap_include_width(a int, b int, payload text) DISTRIBUTED BY (a); +CREATE INDEX i_bmidx_wide ON test_bitmap_include_width(a) INCLUDE (payload); +CREATE INDEX i_bmidx_narrow ON test_bitmap_include_width(a); +CREATE INDEX i_bmidx_b ON test_bitmap_include_width(b); +INSERT INTO test_bitmap_include_width SELECT i, i, repeat('x', 100) FROM generate_series(1, 2000) i; +VACUUM ANALYZE test_bitmap_include_width; + +-- KEYS: [a] INCLUDED: [payload] +-- KEYS: [a] +-- KEYS: [b] +EXPLAIN (COSTS OFF) +SELECT * FROM test_bitmap_include_width WHERE a IN (1, 100, 1000) OR b = 17; + reset optimizer_trace_fallback; reset enable_seqscan; From 89eb1e76adea639a1175af38a8f0437464a86fdf Mon Sep 17 00:00:00 2001 From: Andrey Kazachkov Date: Mon, 26 Oct 2026 17:03:11 +0300 Subject: [PATCH 06/27] Trace per-node buffer accesses for EXPLAIN (ANALYZE, BUFFERS) EXPLAIN (ANALYZE, BUFFERS) did not include buffer usage collected by query executors in the instrumentation deposited on the coordinator. Send per-node buffer usage to the coordinator and accumulate block counters across all workers within each slice. Node statistics are inclusive, so a parent also contains buffer usage accumulated by its descendants within the same slice. --- .../src/test/regress/expected/gp_explain.out | 40 +++++++++++++++++++ .../regress/expected/gp_explain_optimizer.out | 40 +++++++++++++++++++ .../src/test/regress/sql/gp_explain.sql | 32 +++++++++++++++ src/backend/commands/explain_gp.c | 8 +++- src/backend/executor/instrument.c | 7 ++-- src/include/commands/explain_gp.h | 1 + src/include/executor/instrument.h | 3 +- src/test/regress/expected/gp_explain.out | 40 +++++++++++++++++++ .../regress/expected/gp_explain_optimizer.out | 40 +++++++++++++++++++ src/test/regress/sql/gp_explain.sql | 32 +++++++++++++++ 10 files changed, 238 insertions(+), 5 deletions(-) diff --git a/contrib/pax_storage/src/test/regress/expected/gp_explain.out b/contrib/pax_storage/src/test/regress/expected/gp_explain.out index 6fc3b1ef844..3e02c273089 100644 --- a/contrib/pax_storage/src/test/regress/expected/gp_explain.out +++ b/contrib/pax_storage/src/test/regress/expected/gp_explain.out @@ -41,6 +41,29 @@ begin end loop; end; $$ language plpgsql; +-- Run EXPLAIN ANALYZE BUFFERS, preserving the positions of buffer statistics +create or replace function explain_buffers_filter(explain_query text) returns setof text as +$$ +declare + explainrow text; +begin + for explainrow in execute + 'EXPLAIN (ANALYZE, BUFFERS, COSTS OFF, TIMING OFF, SUMMARY OFF) ' || explain_query + loop + if explainrow ~ '^\s*Buffers:' then + explainrow := regexp_replace(explainrow, 'Buffers:.*$', 'Buffers: N'); + else + explainrow := regexp_replace(explainrow, + '\m\d+([kK]B?)\M', 'N\1', 'g'); + explainrow := regexp_replace(explainrow, + '\m\d+(x)', 'N\1', 'g'); + explainrow := regexp_replace(explainrow, + '-?\m\d+\M', 'N', 'g'); + end if; + return next explainrow; + end loop; +end; +$$ language plpgsql; -- Test explain wal option CREATE TABLE explainwal (id int4); NOTICE: Table doesn't have 'DISTRIBUTED BY' clause -- Using column named 'id' as the Apache Cloudberry data distribution key for this table. @@ -104,6 +127,23 @@ NOTICE: Table doesn't have 'DISTRIBUTED BY' clause -- Using column named 'id' a HINT: The 'DISTRIBUTED BY' clause determines the distribution of data. Make sure column(s) chosen are the optimal data distribution key to minimize skew. INSERT INTO explaintest SELECT generate_series(1, 10); ANALYZE explaintest; +-- Compare normalized buffer placement +-- explain_processing_off +SELECT plan_line +FROM explain_buffers_filter($$ + SELECT * FROM explaintest; +$$) WITH ORDINALITY AS plan(plan_line, line_number) +ORDER BY line_number; + plan_line +------------------------------------------------------------------ + Gather Motion N:N (slice1; segments: N) (actual rows=N loops=N) + -> Seq Scan on explaintest (actual rows=N loops=N) + Buffers: N + Planning: + Buffers: N + Optimizer: Postgres query optimizer +(6 rows) + EXPLAIN ANALYZE SELECT * FROM explaintest; QUERY PLAN ---------------------------------------------------------------------------------------------------------------------------------------- diff --git a/contrib/pax_storage/src/test/regress/expected/gp_explain_optimizer.out b/contrib/pax_storage/src/test/regress/expected/gp_explain_optimizer.out index 4244ab91437..8c500ee69e0 100644 --- a/contrib/pax_storage/src/test/regress/expected/gp_explain_optimizer.out +++ b/contrib/pax_storage/src/test/regress/expected/gp_explain_optimizer.out @@ -41,6 +41,29 @@ begin end loop; end; $$ language plpgsql; +-- Run EXPLAIN ANALYZE BUFFERS, preserving the positions of buffer statistics +create or replace function explain_buffers_filter(explain_query text) returns setof text as +$$ +declare + explainrow text; +begin + for explainrow in execute + 'EXPLAIN (ANALYZE, BUFFERS, COSTS OFF, TIMING OFF, SUMMARY OFF) ' || explain_query + loop + if explainrow ~ '^\s*Buffers:' then + explainrow := regexp_replace(explainrow, 'Buffers:.*$', 'Buffers: N'); + else + explainrow := regexp_replace(explainrow, + '\m\d+([kK]B?)\M', 'N\1', 'g'); + explainrow := regexp_replace(explainrow, + '\m\d+(x)', 'N\1', 'g'); + explainrow := regexp_replace(explainrow, + '-?\m\d+\M', 'N', 'g'); + end if; + return next explainrow; + end loop; +end; +$$ language plpgsql; -- Test explain wal option CREATE TABLE explainwal (id int4); NOTICE: Table doesn't have 'DISTRIBUTED BY' clause -- Using column named 'id' as the Apache Cloudberry data distribution key for this table. @@ -104,6 +127,23 @@ NOTICE: Table doesn't have 'DISTRIBUTED BY' clause -- Using column named 'id' a HINT: The 'DISTRIBUTED BY' clause determines the distribution of data. Make sure column(s) chosen are the optimal data distribution key to minimize skew. INSERT INTO explaintest SELECT generate_series(1, 10); ANALYZE explaintest; +-- Compare normalized buffer placement +-- explain_processing_off +SELECT plan_line +FROM explain_buffers_filter($$ + SELECT * FROM explaintest; +$$) WITH ORDINALITY AS plan(plan_line, line_number) +ORDER BY line_number; + plan_line +------------------------------------------------------------------ + Gather Motion N:N (slice1; segments: N) (actual rows=N loops=N) + -> Seq Scan on explaintest (actual rows=N loops=N) + Buffers: N + Planning: + Buffers: N + Optimizer: GPORCA +(6 rows) + EXPLAIN ANALYZE SELECT * FROM explaintest; QUERY PLAN -------------------------------------------------------------------------------------------------------------------------- diff --git a/contrib/pax_storage/src/test/regress/sql/gp_explain.sql b/contrib/pax_storage/src/test/regress/sql/gp_explain.sql index 591d8f61540..6885d078238 100644 --- a/contrib/pax_storage/src/test/regress/sql/gp_explain.sql +++ b/contrib/pax_storage/src/test/regress/sql/gp_explain.sql @@ -45,6 +45,30 @@ begin end; $$ language plpgsql; +-- Run EXPLAIN ANALYZE BUFFERS, preserving the positions of buffer statistics +create or replace function explain_buffers_filter(explain_query text) returns setof text as +$$ +declare + explainrow text; +begin + for explainrow in execute + 'EXPLAIN (ANALYZE, BUFFERS, COSTS OFF, TIMING OFF, SUMMARY OFF) ' || explain_query + loop + if explainrow ~ '^\s*Buffers:' then + explainrow := regexp_replace(explainrow, 'Buffers:.*$', 'Buffers: N'); + else + explainrow := regexp_replace(explainrow, + '\m\d+([kK]B?)\M', 'N\1', 'g'); + explainrow := regexp_replace(explainrow, + '\m\d+(x)', 'N\1', 'g'); + explainrow := regexp_replace(explainrow, + '-?\m\d+\M', 'N', 'g'); + end if; + return next explainrow; + end loop; +end; +$$ language plpgsql; + -- Test explain wal option CREATE TABLE explainwal (id int4); @@ -94,6 +118,14 @@ CREATE TABLE explaintest (id int4); INSERT INTO explaintest SELECT generate_series(1, 10); ANALYZE explaintest; +-- Compare normalized buffer placement +-- explain_processing_off +SELECT plan_line +FROM explain_buffers_filter($$ + SELECT * FROM explaintest; +$$) WITH ORDINALITY AS plan(plan_line, line_number) +ORDER BY line_number; + EXPLAIN ANALYZE SELECT * FROM explaintest; set explain_memory_verbosity='summary'; diff --git a/src/backend/commands/explain_gp.c b/src/backend/commands/explain_gp.c index c3ff48d483b..25f482a9c27 100644 --- a/src/backend/commands/explain_gp.c +++ b/src/backend/commands/explain_gp.c @@ -816,6 +816,7 @@ cdbexplain_collectStatsFromNode(PlanState *planstate, CdbExplain_SendStatCtx *ct si->ntuples = instr->ntuples; si->ntuples2 = instr->ntuples2; si->nloops = instr->nloops; + si->bufusage = instr->bufusage; si->nfiltered1 = instr->nfiltered1; si->nfiltered2 = instr->nfiltered2; si->prf_work = instr->prf_work; @@ -951,7 +952,6 @@ cdbexplain_depStatAcc_saveText(CdbExplain_DepStatAcc *acc, } } /* cdbexplain_depStatAcc_saveText */ - /* * cdbexplain_depositStatsToNode * @@ -992,6 +992,7 @@ cdbexplain_depositStatsToNode(PlanState *planstate, CdbExplain_RecvStatCtx *ctx) CdbExplain_DepStatAcc vmem_reserved; CdbExplain_DepStatAcc totalPartTableScanned; CdbExplain_DepStatAcc sortSpaceUsed[NUM_SORT_SPACE_TYPE][NUM_SORT_METHOD]; + BufferUsage bufusage; int imsgptr; int nInst; @@ -1016,6 +1017,7 @@ cdbexplain_depositStatsToNode(PlanState *planstate, CdbExplain_RecvStatCtx *ctx) cdbexplain_depStatAcc_init0(&workmemwanted); cdbexplain_depStatAcc_init0(&totalWorkfileCreated); cdbexplain_depStatAcc_init0(&totalPartTableScanned); + memset(&bufusage, 0, sizeof(bufusage)); for (int i = 0; i < NUM_SORT_METHOD; i++) { for (int j = 0; j < NUM_SORT_SPACE_TYPE; j++) @@ -1061,6 +1063,7 @@ cdbexplain_depositStatsToNode(PlanState *planstate, CdbExplain_RecvStatCtx *ctx) cdbexplain_depStatAcc_upd(&workmemwanted, rsi->workmemwanted, rsh, rsi, nsi); cdbexplain_depStatAcc_upd(&totalWorkfileCreated, (rsi->workfileCreated ? 1 : 0), rsh, rsi, nsi); cdbexplain_depStatAcc_upd(&totalPartTableScanned, rsi->numPartScanned, rsh, rsi, nsi); + BufferUsageAdd(&bufusage, &rsi->bufusage); Assert(rsi->sortstats.sortMethod < NUM_SORT_METHOD); Assert(rsi->sortstats.spaceType < NUM_SORT_SPACE_TYPE); if (rsi->sortstats.sortMethod != SORT_TYPE_STILL_IN_PROGRESS) @@ -1132,6 +1135,9 @@ cdbexplain_depositStatsToNode(PlanState *planstate, CdbExplain_RecvStatCtx *ctx) instr->firststart = nodeAcc->nsimax->firststart; } + /* Buffer usage is summed across all workers, not taken from the winner. */ + instr->bufusage = bufusage; + /* Save extra message text for the most interesting winning qExecs. */ if (ctx->extratextbuf) { diff --git a/src/backend/executor/instrument.c b/src/backend/executor/instrument.c index 3fd39dfb167..354a029c440 100644 --- a/src/backend/executor/instrument.c +++ b/src/backend/executor/instrument.c @@ -32,7 +32,6 @@ static BufferUsage save_pgBufferUsage; WalUsage pgWalUsage; static WalUsage save_pgWalUsage; -static void BufferUsageAdd(BufferUsage *dst, const BufferUsage *add); static void WalUsageAdd(WalUsage *dst, WalUsage *add); /* GPDB specific */ @@ -87,6 +86,7 @@ InstrInit(Instrumentation *instr, int instrument_options) instr->need_bufusage = (instrument_options & INSTRUMENT_BUFFERS) != 0; instr->need_walusage = (instrument_options & INSTRUMENT_WAL) != 0; instr->need_timer = (instrument_options & INSTRUMENT_TIMER) != 0; + instr->need_cdb = (instrument_options & INSTRUMENT_CDB) != 0; } /* Entry to a plan node */ @@ -271,7 +271,7 @@ InstrAccumParallelQuery(BufferUsage *bufusage, WalUsage *walusage) } /* dst += add */ -static void +void BufferUsageAdd(BufferUsage *dst, const BufferUsage *add) { dst->shared_blks_hit += add->shared_blks_hit; @@ -506,8 +506,9 @@ pickInstrFromShmem(const Plan *plan, int instrument_options) MemoryContextSwitchTo(contextSave); } - if (NULL != instr && instrument_options & (INSTRUMENT_TIMER | INSTRUMENT_CDB)) + if (instr != NULL) { + instr->need_bufusage = (instrument_options & INSTRUMENT_BUFFERS) != 0; instr->need_timer = (instrument_options & INSTRUMENT_TIMER) != 0; instr->need_cdb = (instrument_options & INSTRUMENT_CDB) != 0; } diff --git a/src/include/commands/explain_gp.h b/src/include/commands/explain_gp.h index 016e5449543..c9210507e9f 100644 --- a/src/include/commands/explain_gp.h +++ b/src/include/commands/explain_gp.h @@ -34,6 +34,7 @@ typedef struct CdbExplain_StatInst double ntuples; /* Total tuples produced */ double ntuples2; double nloops; /* # of run cycles for this node */ + BufferUsage bufusage; /* Total buffer usage */ double nfiltered1; double nfiltered2; bool prf_work; diff --git a/src/include/executor/instrument.h b/src/include/executor/instrument.h index bce4b33c2f8..1fba5b70de3 100644 --- a/src/include/executor/instrument.h +++ b/src/include/executor/instrument.h @@ -64,7 +64,7 @@ typedef enum InstrumentOption { INSTRUMENT_NONE = 0, INSTRUMENT_TIMER = 1 << 0, /* needs timer (and row counts) */ - INSTRUMENT_BUFFERS = 1 << 1, /* needs buffer usage (not implemented yet) */ + INSTRUMENT_BUFFERS = 1 << 1, /* needs buffer usage */ INSTRUMENT_ROWS = 1 << 2, /* needs row count */ INSTRUMENT_WAL = 1 << 3, /* needs WAL usage */ INSTRUMENT_MEMORY_DETAIL = 0x20000000, /* needs detailed memory accounting */ @@ -135,6 +135,7 @@ extern void InstrAggNode(Instrumentation *dst, Instrumentation *add); extern void InstrStartParallelQuery(void); extern void InstrEndParallelQuery(BufferUsage *bufusage, WalUsage *walusage); extern void InstrAccumParallelQuery(BufferUsage *bufusage, WalUsage *walusage); +extern void BufferUsageAdd(BufferUsage *dst, const BufferUsage *add); extern void BufferUsageAccumDiff(BufferUsage *dst, const BufferUsage *add, const BufferUsage *sub); extern void WalUsageAccumDiff(WalUsage *dst, const WalUsage *add, diff --git a/src/test/regress/expected/gp_explain.out b/src/test/regress/expected/gp_explain.out index 6fc3b1ef844..3e02c273089 100644 --- a/src/test/regress/expected/gp_explain.out +++ b/src/test/regress/expected/gp_explain.out @@ -41,6 +41,29 @@ begin end loop; end; $$ language plpgsql; +-- Run EXPLAIN ANALYZE BUFFERS, preserving the positions of buffer statistics +create or replace function explain_buffers_filter(explain_query text) returns setof text as +$$ +declare + explainrow text; +begin + for explainrow in execute + 'EXPLAIN (ANALYZE, BUFFERS, COSTS OFF, TIMING OFF, SUMMARY OFF) ' || explain_query + loop + if explainrow ~ '^\s*Buffers:' then + explainrow := regexp_replace(explainrow, 'Buffers:.*$', 'Buffers: N'); + else + explainrow := regexp_replace(explainrow, + '\m\d+([kK]B?)\M', 'N\1', 'g'); + explainrow := regexp_replace(explainrow, + '\m\d+(x)', 'N\1', 'g'); + explainrow := regexp_replace(explainrow, + '-?\m\d+\M', 'N', 'g'); + end if; + return next explainrow; + end loop; +end; +$$ language plpgsql; -- Test explain wal option CREATE TABLE explainwal (id int4); NOTICE: Table doesn't have 'DISTRIBUTED BY' clause -- Using column named 'id' as the Apache Cloudberry data distribution key for this table. @@ -104,6 +127,23 @@ NOTICE: Table doesn't have 'DISTRIBUTED BY' clause -- Using column named 'id' a HINT: The 'DISTRIBUTED BY' clause determines the distribution of data. Make sure column(s) chosen are the optimal data distribution key to minimize skew. INSERT INTO explaintest SELECT generate_series(1, 10); ANALYZE explaintest; +-- Compare normalized buffer placement +-- explain_processing_off +SELECT plan_line +FROM explain_buffers_filter($$ + SELECT * FROM explaintest; +$$) WITH ORDINALITY AS plan(plan_line, line_number) +ORDER BY line_number; + plan_line +------------------------------------------------------------------ + Gather Motion N:N (slice1; segments: N) (actual rows=N loops=N) + -> Seq Scan on explaintest (actual rows=N loops=N) + Buffers: N + Planning: + Buffers: N + Optimizer: Postgres query optimizer +(6 rows) + EXPLAIN ANALYZE SELECT * FROM explaintest; QUERY PLAN ---------------------------------------------------------------------------------------------------------------------------------------- diff --git a/src/test/regress/expected/gp_explain_optimizer.out b/src/test/regress/expected/gp_explain_optimizer.out index 4244ab91437..8c500ee69e0 100644 --- a/src/test/regress/expected/gp_explain_optimizer.out +++ b/src/test/regress/expected/gp_explain_optimizer.out @@ -41,6 +41,29 @@ begin end loop; end; $$ language plpgsql; +-- Run EXPLAIN ANALYZE BUFFERS, preserving the positions of buffer statistics +create or replace function explain_buffers_filter(explain_query text) returns setof text as +$$ +declare + explainrow text; +begin + for explainrow in execute + 'EXPLAIN (ANALYZE, BUFFERS, COSTS OFF, TIMING OFF, SUMMARY OFF) ' || explain_query + loop + if explainrow ~ '^\s*Buffers:' then + explainrow := regexp_replace(explainrow, 'Buffers:.*$', 'Buffers: N'); + else + explainrow := regexp_replace(explainrow, + '\m\d+([kK]B?)\M', 'N\1', 'g'); + explainrow := regexp_replace(explainrow, + '\m\d+(x)', 'N\1', 'g'); + explainrow := regexp_replace(explainrow, + '-?\m\d+\M', 'N', 'g'); + end if; + return next explainrow; + end loop; +end; +$$ language plpgsql; -- Test explain wal option CREATE TABLE explainwal (id int4); NOTICE: Table doesn't have 'DISTRIBUTED BY' clause -- Using column named 'id' as the Apache Cloudberry data distribution key for this table. @@ -104,6 +127,23 @@ NOTICE: Table doesn't have 'DISTRIBUTED BY' clause -- Using column named 'id' a HINT: The 'DISTRIBUTED BY' clause determines the distribution of data. Make sure column(s) chosen are the optimal data distribution key to minimize skew. INSERT INTO explaintest SELECT generate_series(1, 10); ANALYZE explaintest; +-- Compare normalized buffer placement +-- explain_processing_off +SELECT plan_line +FROM explain_buffers_filter($$ + SELECT * FROM explaintest; +$$) WITH ORDINALITY AS plan(plan_line, line_number) +ORDER BY line_number; + plan_line +------------------------------------------------------------------ + Gather Motion N:N (slice1; segments: N) (actual rows=N loops=N) + -> Seq Scan on explaintest (actual rows=N loops=N) + Buffers: N + Planning: + Buffers: N + Optimizer: GPORCA +(6 rows) + EXPLAIN ANALYZE SELECT * FROM explaintest; QUERY PLAN -------------------------------------------------------------------------------------------------------------------------- diff --git a/src/test/regress/sql/gp_explain.sql b/src/test/regress/sql/gp_explain.sql index 2b5aaa0c722..0e014ac234b 100644 --- a/src/test/regress/sql/gp_explain.sql +++ b/src/test/regress/sql/gp_explain.sql @@ -45,6 +45,30 @@ begin end; $$ language plpgsql; +-- Run EXPLAIN ANALYZE BUFFERS, preserving the positions of buffer statistics +create or replace function explain_buffers_filter(explain_query text) returns setof text as +$$ +declare + explainrow text; +begin + for explainrow in execute + 'EXPLAIN (ANALYZE, BUFFERS, COSTS OFF, TIMING OFF, SUMMARY OFF) ' || explain_query + loop + if explainrow ~ '^\s*Buffers:' then + explainrow := regexp_replace(explainrow, 'Buffers:.*$', 'Buffers: N'); + else + explainrow := regexp_replace(explainrow, + '\m\d+([kK]B?)\M', 'N\1', 'g'); + explainrow := regexp_replace(explainrow, + '\m\d+(x)', 'N\1', 'g'); + explainrow := regexp_replace(explainrow, + '-?\m\d+\M', 'N', 'g'); + end if; + return next explainrow; + end loop; +end; +$$ language plpgsql; + -- Test explain wal option CREATE TABLE explainwal (id int4); @@ -94,6 +118,14 @@ CREATE TABLE explaintest (id int4); INSERT INTO explaintest SELECT generate_series(1, 10); ANALYZE explaintest; +-- Compare normalized buffer placement +-- explain_processing_off +SELECT plan_line +FROM explain_buffers_filter($$ + SELECT * FROM explaintest; +$$) WITH ORDINALITY AS plan(plan_line, line_number) +ORDER BY line_number; + EXPLAIN ANALYZE SELECT * FROM explaintest; set explain_memory_verbosity='summary'; From 1480f4f636c601221d266aa7520aeb25be7d740e Mon Sep 17 00:00:00 2001 From: Jianghua Yang Date: Thu, 3 Sep 2026 23:40:30 +0800 Subject: [PATCH 07/27] interconnect: prune cursor IC history table by local xid, not distributed xid SetupUDPIFCInterconnect_Internal() records one CursorICHistoryEntry per interconnect instance on the QD and prunes old entries only when the statement runs in a different transaction than the previous one, using the distributed xid as the transaction identity. Distributed xids are assigned lazily (only for transactions that write or are dispatched two-phase), so autocommit read-only statements always see InvalidDistributedTransactionId on both sides of the comparison. The table is therefore never pruned on read-only sessions and grows by one 48-byte entry per Motion-containing statement for the life of the backend (2.8 MB after 60000 statements, plus the heap fragmentation the steady stream of small long-lived allocations induces). Use MyProc->lxid instead: it is assigned to every top-level transaction, including read-only ones, and stays constant within a transaction block, so entries belonging to cursors that are still open in the current transaction remain protected exactly as before. Verified on a single long-lived session running the queries from #1947: UdpInterconnectMemContext grew 0 -> 2814 KB over 60000 statements before the change and stays at 12 KB after it; RssAnon went 12.7 -> 24.6 MB before and is flat at 14.4 MB after. Pruning still does not happen inside an explicit transaction block or while a cursor is open, and FETCH from an open cursor after 300 intervening statements works. Related: #1947 --- contrib/interconnect/udp/ic_udpifc.c | 32 +++++++++++++++------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/contrib/interconnect/udp/ic_udpifc.c b/contrib/interconnect/udp/ic_udpifc.c index 1965ca998b1..f34037ef39c 100644 --- a/contrib/interconnect/udp/ic_udpifc.c +++ b/contrib/interconnect/udp/ic_udpifc.c @@ -46,6 +46,7 @@ #include "nodes/pg_list.h" #include "nodes/print.h" #include "miscadmin.h" +#include "storage/proc.h" #include "libpq/libpq-be.h" #include "port/atomics.h" #include "port/pg_crc32c.h" @@ -353,11 +354,18 @@ struct ReceiveControlInfo CursorICHistoryTable cursorHistoryTable; /* - * Last distributed transaction id when SetupUDPInterconnect is called. - * Coupled with cursorHistoryTable, it is used to handle multiple - * concurrent cursor cases. + * Local (virtual) transaction id of the top-level transaction the last + * time SetupUDPInterconnect was called. Coupled with cursorHistoryTable, + * it is used to handle multiple concurrent cursor cases: entries may only + * be pruned once we are in a different transaction. + * + * We deliberately use MyProc->lxid rather than the distributed xid: a + * distributed xid is only assigned lazily to transactions that write (or + * are dispatched two-phase), so for autocommit read-only statements it is + * always InvalidDistributedTransactionId and the "different transaction" + * test would never fire, letting the history table grow without bound. */ - DistributedTransactionId lastDXatId; + LocalTransactionId lastLxid; }; /* @@ -1892,7 +1900,7 @@ InitMotionUDPIFC(int *listenerSocketFd, int32 *listenerPort) /* allocate a buffer for sending disorder messages */ rx_control_info.disorderBuffer = palloc0(MIN_PACKET_SIZE); - rx_control_info.lastDXatId = InvalidTransactionId; + rx_control_info.lastLxid = InvalidLocalTransactionId; rx_control_info.lastTornIcId = 0; initCursorICHistoryTable(&rx_control_info.cursorHistoryTable); @@ -3571,19 +3579,13 @@ SetupUDPIFCInterconnect_Internal(SliceTable *sliceTable) if (Gp_role == GP_ROLE_DISPATCH) { - DistributedTransactionId distTransId = 0; - TransactionId localTransId = 0; - TransactionId subtransId = 0; - - GetAllTransactionXids(&(distTransId), - &(localTransId), - &(subtransId)); + LocalTransactionId curLxid = MyProc->lxid; /* - * Prune only when we are not in the save transaction and there is a + * Prune only when we are not in the same transaction and there is a * large number of entries in the table */ - if (distTransId != rx_control_info.lastDXatId && rx_control_info.cursorHistoryTable.count > (2 * CURSOR_IC_TABLE_SIZE)) + if (curLxid != rx_control_info.lastLxid && rx_control_info.cursorHistoryTable.count > (2 * CURSOR_IC_TABLE_SIZE)) { if (gp_log_interconnect >= GPVARS_VERBOSITY_DEBUG) elog(DEBUG1, "prune cursor history table (count %d), icid %d", rx_control_info.cursorHistoryTable.count, sliceTable->ic_instance_id); @@ -3593,7 +3595,7 @@ SetupUDPIFCInterconnect_Internal(SliceTable *sliceTable) addCursorIcEntry(&rx_control_info.cursorHistoryTable, sliceTable->ic_instance_id, gp_command_count); /* save the latest transaction id. */ - rx_control_info.lastDXatId = distTransId; + rx_control_info.lastLxid = curLxid; } /* now we'll do some setup for each of our Receiving Motion Nodes. */ From 18cbbccaec23b5ab20a1c086268fb8bb1778c2b3 Mon Sep 17 00:00:00 2001 From: Dianjin Wang Date: Tue, 27 Oct 2026 10:15:42 +0800 Subject: [PATCH 08/27] Drop unused variable in extended_protocol_resqueue paramValues is declared and never used. The GNUmakefile builds extended_protocol_resqueue without the usual warning flags, so nothing has reported it; with -Wall the compiler warns about it. Assisted-by: Claude Code --- src/test/regress/extended_protocol_resqueue.c | 1 - 1 file changed, 1 deletion(-) diff --git a/src/test/regress/extended_protocol_resqueue.c b/src/test/regress/extended_protocol_resqueue.c index 1932f28138e..c1e4b84d058 100644 --- a/src/test/regress/extended_protocol_resqueue.c +++ b/src/test/regress/extended_protocol_resqueue.c @@ -31,7 +31,6 @@ main(int argc, char **argv) const char *execrole; PGconn *conn; PGresult *res; - const char *paramValues[1]; char buf[80]; conninfo = argv[1]; From 8782ef0327b3e85374569e0e1dab9e9f137d4f62 Mon Sep 17 00:00:00 2001 From: roseduan Date: Mon, 9 Nov 2026 16:52:14 +0800 Subject: [PATCH 09/27] Fix MERGE into replicated tables MERGE into a DISTRIBUTED REPLICATED table did not broadcast the source relation, so each segment merged only the source rows stored locally. The replicas diverged and the reported row count was wrong. Treat MERGE like UPDATE/DELETE when flagging a replicated result relation (upd_del_replicated_table), so the join broadcasts the source to all segments. Also reject volatile functions in MERGE actions on replicated tables. The existing check only looked at the subplan targetlist, but MERGE action expressions are kept in mergeActionLists, so e.g. "UPDATE SET v = random()" was accepted and the replicas diverged. --- src/backend/cdb/cdbpath.c | 7 ++- src/backend/optimizer/util/relnode.c | 3 +- src/test/regress/expected/rpt.out | 51 +++++++++++++++++++++ src/test/regress/expected/rpt_optimizer.out | 51 +++++++++++++++++++++ src/test/regress/sql/rpt.sql | 26 +++++++++++ 5 files changed, 135 insertions(+), 3 deletions(-) diff --git a/src/backend/cdb/cdbpath.c b/src/backend/cdb/cdbpath.c index cc703b6e1a7..95db110d668 100644 --- a/src/backend/cdb/cdbpath.c +++ b/src/backend/cdb/cdbpath.c @@ -2853,9 +2853,12 @@ create_motion_path_for_merge(PlannerInfo *root, List *resultRelations, GpPolicy /* * The statement that insert/update/delete on replicated table has to * be dispatched to each segment and executed on each segment. Thus - * the targetlist cannot contain volatile functions. + * the targetlist cannot contain volatile functions. MERGE action + * expressions (WHEN conditions, SET and VALUES) are kept in + * mergeActionLists, not in the subpath targetlist, so check them too. */ - if (contain_volatile_functions((Node *) (subpath->pathtarget->exprs))) + if (contain_volatile_functions((Node *) (subpath->pathtarget->exprs)) || + contain_volatile_functions((Node *) mergeActionLists)) elog(ERROR, "could not devise a plan."); } else diff --git a/src/backend/optimizer/util/relnode.c b/src/backend/optimizer/util/relnode.c index d32e48c5514..cce6e172f67 100644 --- a/src/backend/optimizer/util/relnode.c +++ b/src/backend/optimizer/util/relnode.c @@ -376,7 +376,8 @@ build_simple_rel(PlannerInfo *root, int relid, RelOptInfo *parent) } if ((root->parse->commandType == CMD_UPDATE || - root->parse->commandType == CMD_DELETE) && + root->parse->commandType == CMD_DELETE || + root->parse->commandType == CMD_MERGE) && root->parse->resultRelation == relid && GpPolicyIsReplicated(rel->cdbpolicy)) { diff --git a/src/test/regress/expected/rpt.out b/src/test/regress/expected/rpt.out index 103d426a8c3..3b63bf4be7e 100644 --- a/src/test/regress/expected/rpt.out +++ b/src/test/regress/expected/rpt.out @@ -541,6 +541,49 @@ select * from foo; ---+--- (0 rows) +-- MERGE into a replicated table must broadcast the source to every segment, +-- like UPDATE/DELETE, otherwise each replica only sees the source rows stored +-- on its own segment and the replicas diverge. +create table rpt_merge_tgt(k int, v int) distributed replicated; +create table rpt_merge_src(k int, v int) distributed by (k); +insert into rpt_merge_tgt select g, 0 from generate_series(1, 10) g; +insert into rpt_merge_src select g, 1 from generate_series(6, 15) g; +explain (costs off) +merge into rpt_merge_tgt t using rpt_merge_src s on t.k = s.k + when matched then update set v = s.v + when not matched then insert values (s.k, s.v); + QUERY PLAN +--------------------------------------------------------- + Merge on rpt_merge_tgt t + -> Hash Left Join + Hash Cond: (s.k = t.k) + -> Broadcast Motion 3:3 (slice1; segments: 3) + -> Seq Scan on rpt_merge_src s + -> Hash + -> Seq Scan on rpt_merge_tgt t + Optimizer: Postgres query optimizer +(8 rows) + +merge into rpt_merge_tgt t using rpt_merge_src s on t.k = s.k + when matched then update set v = s.v + when not matched then insert values (s.k, s.v); +select count(*), count(*) filter (where v = 1) as v_is_1 from rpt_merge_tgt; + count | v_is_1 +-------+-------- + 15 | 10 +(1 row) + +-- every replica must hold the same 15 rows +select gp_execution_segment() as seg, count(*), count(*) filter (where v = 1) as v_is_1 +from gp_dist_random('rpt_merge_tgt') group by 1 order by 1; + seg | count | v_is_1 +-----+-------+-------- + 0 | 15 | 10 + 1 | 15 | 10 + 2 | 15 | 10 +(3 rows) + +drop table rpt_merge_tgt, rpt_merge_src; -- Test replicate table within init plan insert into foo values (1, 1), (2, 1); select * from bar where exists (select * from foo); @@ -973,6 +1016,14 @@ explain (costs off) delete from t_replicate_volatile using t_replicate_volatile ERROR: could not devise a plan (cdbpath.c:2578) explain (costs off) update t_replicate_volatile set a = random(); ERROR: could not devise a plan. (cdbpath.c:2440) +-- merge: volatile functions in MERGE actions are evaluated by ModifyTable, +-- not in the subplan targetlist, but must be rejected all the same +explain (costs off) merge into t_replicate_volatile using t_hashdist x on x.a = t_replicate_volatile.a when matched then update set b = random(); +ERROR: could not devise a plan. (cdbpath.c:2862) +explain (costs off) merge into t_replicate_volatile using t_hashdist x on x.a = t_replicate_volatile.a when not matched then insert values (x.a, random()); +ERROR: could not devise a plan. (cdbpath.c:2862) +explain (costs off) merge into t_replicate_volatile using t_hashdist x on x.a = t_replicate_volatile.a when matched and random() < 0.5 then delete; +ERROR: could not devise a plan. (cdbpath.c:2862) -- limit explain (costs off) insert into t_replicate_volatile select * from t_replicate_volatile limit random(); QUERY PLAN diff --git a/src/test/regress/expected/rpt_optimizer.out b/src/test/regress/expected/rpt_optimizer.out index 5612de4e95f..ca8621bcf94 100644 --- a/src/test/regress/expected/rpt_optimizer.out +++ b/src/test/regress/expected/rpt_optimizer.out @@ -541,6 +541,49 @@ select * from foo; ---+--- (0 rows) +-- MERGE into a replicated table must broadcast the source to every segment, +-- like UPDATE/DELETE, otherwise each replica only sees the source rows stored +-- on its own segment and the replicas diverge. +create table rpt_merge_tgt(k int, v int) distributed replicated; +create table rpt_merge_src(k int, v int) distributed by (k); +insert into rpt_merge_tgt select g, 0 from generate_series(1, 10) g; +insert into rpt_merge_src select g, 1 from generate_series(6, 15) g; +explain (costs off) +merge into rpt_merge_tgt t using rpt_merge_src s on t.k = s.k + when matched then update set v = s.v + when not matched then insert values (s.k, s.v); + QUERY PLAN +--------------------------------------------------------- + Merge on rpt_merge_tgt t + -> Hash Left Join + Hash Cond: (s.k = t.k) + -> Broadcast Motion 3:3 (slice1; segments: 3) + -> Seq Scan on rpt_merge_src s + -> Hash + -> Seq Scan on rpt_merge_tgt t + Optimizer: Postgres query optimizer +(8 rows) + +merge into rpt_merge_tgt t using rpt_merge_src s on t.k = s.k + when matched then update set v = s.v + when not matched then insert values (s.k, s.v); +select count(*), count(*) filter (where v = 1) as v_is_1 from rpt_merge_tgt; + count | v_is_1 +-------+-------- + 15 | 10 +(1 row) + +-- every replica must hold the same 15 rows +select gp_execution_segment() as seg, count(*), count(*) filter (where v = 1) as v_is_1 +from gp_dist_random('rpt_merge_tgt') group by 1 order by 1; + seg | count | v_is_1 +-----+-------+-------- + 0 | 15 | 10 + 1 | 15 | 10 + 2 | 15 | 10 +(3 rows) + +drop table rpt_merge_tgt, rpt_merge_src; -- Test replicate table within init plan insert into foo values (1, 1), (2, 1); select * from bar where exists (select * from foo); @@ -965,6 +1008,14 @@ explain (costs off) delete from t_replicate_volatile using t_replicate_volatile ERROR: could not devise a plan (cdbpath.c:2578) explain (costs off) update t_replicate_volatile set a = random(); ERROR: could not devise a plan. (cdbpath.c:2441) +-- merge: volatile functions in MERGE actions are evaluated by ModifyTable, +-- not in the subplan targetlist, but must be rejected all the same +explain (costs off) merge into t_replicate_volatile using t_hashdist x on x.a = t_replicate_volatile.a when matched then update set b = random(); +ERROR: could not devise a plan. (cdbpath.c:2862) +explain (costs off) merge into t_replicate_volatile using t_hashdist x on x.a = t_replicate_volatile.a when not matched then insert values (x.a, random()); +ERROR: could not devise a plan. (cdbpath.c:2862) +explain (costs off) merge into t_replicate_volatile using t_hashdist x on x.a = t_replicate_volatile.a when matched and random() < 0.5 then delete; +ERROR: could not devise a plan. (cdbpath.c:2862) -- limit explain (costs off) insert into t_replicate_volatile select * from t_replicate_volatile limit random(); QUERY PLAN diff --git a/src/test/regress/sql/rpt.sql b/src/test/regress/sql/rpt.sql index 79a4a3db649..2375688920d 100644 --- a/src/test/regress/sql/rpt.sql +++ b/src/test/regress/sql/rpt.sql @@ -273,6 +273,26 @@ select * from foo; delete from foo where y = 1; select * from foo; +-- MERGE into a replicated table must broadcast the source to every segment, +-- like UPDATE/DELETE, otherwise each replica only sees the source rows stored +-- on its own segment and the replicas diverge. +create table rpt_merge_tgt(k int, v int) distributed replicated; +create table rpt_merge_src(k int, v int) distributed by (k); +insert into rpt_merge_tgt select g, 0 from generate_series(1, 10) g; +insert into rpt_merge_src select g, 1 from generate_series(6, 15) g; +explain (costs off) +merge into rpt_merge_tgt t using rpt_merge_src s on t.k = s.k + when matched then update set v = s.v + when not matched then insert values (s.k, s.v); +merge into rpt_merge_tgt t using rpt_merge_src s on t.k = s.k + when matched then update set v = s.v + when not matched then insert values (s.k, s.v); +select count(*), count(*) filter (where v = 1) as v_is_1 from rpt_merge_tgt; +-- every replica must hold the same 15 rows +select gp_execution_segment() as seg, count(*), count(*) filter (where v = 1) as v_is_1 +from gp_dist_random('rpt_merge_tgt') group by 1 order by 1; +drop table rpt_merge_tgt, rpt_merge_src; + -- Test replicate table within init plan insert into foo values (1, 1), (2, 1); select * from bar where exists (select * from foo); @@ -435,6 +455,12 @@ explain (costs off) delete from t_replicate_volatile where a < random(); explain (costs off) delete from t_replicate_volatile using t_replicate_volatile x where t_replicate_volatile.a + x.b < random(); explain (costs off) update t_replicate_volatile set a = random(); +-- merge: volatile functions in MERGE actions are evaluated by ModifyTable, +-- not in the subplan targetlist, but must be rejected all the same +explain (costs off) merge into t_replicate_volatile using t_hashdist x on x.a = t_replicate_volatile.a when matched then update set b = random(); +explain (costs off) merge into t_replicate_volatile using t_hashdist x on x.a = t_replicate_volatile.a when not matched then insert values (x.a, random()); +explain (costs off) merge into t_replicate_volatile using t_hashdist x on x.a = t_replicate_volatile.a when matched and random() < 0.5 then delete; + -- limit explain (costs off) insert into t_replicate_volatile select * from t_replicate_volatile limit random(); explain (costs off) select * from t_hashdist cross join (select * from t_replicate_volatile limit random()) x; From f589c2b5f08271370dfb9e4478e1292be197b2da Mon Sep 17 00:00:00 2001 From: Dianjin Wang Date: Tue, 27 Oct 2026 10:14:40 +0800 Subject: [PATCH 10/27] Keep backend headers out of frontend src/common src/common is compiled twice, for the frontend programs and for the backend, but two of its files include backend headers in both: - percentrepl.c includes cdb/cdbvars.h, postmaster/postmaster.h and utils/builtins.h for GpIdentity, terminal_fd and pg_ltoa(), which it uses only under #ifndef FRONTEND. - kmgr_utils.c includes postgres.h, which no frontend file may include, and utils/builtins.h after choosing between postgres.h and postgres_fe.h, and uses neither. Through them, the frontend copies of both files depended on utils/fmgrprotos.h, which the backend build generates from its function catalog, and on postgres.h itself. A frontend compile has no business with either, and it could not run until the backend's headers had been generated; a top-level make generates every header before it builds src/common, which is why this went unnoticed. After this change neither file's frontend compile includes a generated header at all. Move percentrepl.c's three includes under #ifndef FRONTEND, and drop kmgr_utils.c's two. Assisted-by: Claude Code --- src/common/kmgr_utils.c | 2 -- src/common/percentrepl.c | 6 +++--- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/src/common/kmgr_utils.c b/src/common/kmgr_utils.c index ede043d1078..0d2204c4fd8 100644 --- a/src/common/kmgr_utils.c +++ b/src/common/kmgr_utils.c @@ -34,8 +34,6 @@ #include "crypto/kmgr.h" #include "lib/stringinfo.h" #include "storage/fd.h" -#include "postgres.h" -#include "utils/builtins.h" #ifndef FRONTEND #include "pgstat.h" diff --git a/src/common/percentrepl.c b/src/common/percentrepl.c index 53b717edda9..5feaf64ac6e 100644 --- a/src/common/percentrepl.c +++ b/src/common/percentrepl.c @@ -15,16 +15,16 @@ #ifndef FRONTEND #include "postgres.h" +#include "cdb/cdbvars.h" +#include "postmaster/postmaster.h" +#include "utils/builtins.h" #else #include "postgres_fe.h" #include "common/logging.h" #endif -#include "cdb/cdbvars.h" #include "common/percentrepl.h" #include "lib/stringinfo.h" -#include "postmaster/postmaster.h" -#include "utils/builtins.h" /* * replace_percent_placeholders From f2ad06b18dac982eff5c6272537c4a35b3b87fd0 Mon Sep 17 00:00:00 2001 From: Leonid Borchuk Date: Thu, 15 Oct 2026 13:58:45 +0300 Subject: [PATCH 11/27] Let an extension share runtime information between QEs Anser needs to move a small piece of information -- today a bloom filter over a join key -- from the segments to the coordinator and back, over the dispatch connection that is already open. Four things in the backend stand in the way, and none of them mentions Anser. A QE can already send a NOTIFY to the coordinator, but the dispatcher dropped any it did not recognise. Add cdbdisp_notify_hook so an extension can claim one, and keep the ones that arrive in the same read as a command completion instead of losing them. The other direction had no message type at all. Add GP_SIDEBAND_MESSAGE, which the QE command loop accepts and discards when nobody is waiting -- a waiter can be cancelled after the coordinator has written, and an unknown type would otherwise be fatal. A CustomScan travelled through the binary plan writer as the text " :methods " plus a token, and the reader took the first four bytes of that as a length and asked for 1.7 GB. Any plan with a CustomScan failed to deserialize on the segments; write the name as a counted string instead. catversion is bumped accordingly. Finally, a bloom filter cannot be shipped without reaching its bitset, so expose it along with the sizing its holder must agree on. Co-Authored-By: Claude Opus 5 --- src/backend/cdb/dispatcher/cdbdisp_async.c | 235 +++++++++++++++------ src/backend/lib/bloomfilter.c | 41 ++++ src/backend/tcop/postgres.c | 17 ++ src/include/catalog/catversion.h | 2 +- src/include/cdb/cdbdisp.h | 23 ++ src/include/cdb/cdbvars.h | 13 ++ src/include/lib/bloomfilter.h | 6 + 7 files changed, 269 insertions(+), 68 deletions(-) diff --git a/src/backend/cdb/dispatcher/cdbdisp_async.c b/src/backend/cdb/dispatcher/cdbdisp_async.c index eb8e4714396..536ef3c15ae 100644 --- a/src/backend/cdb/dispatcher/cdbdisp_async.c +++ b/src/backend/cdb/dispatcher/cdbdisp_async.c @@ -116,6 +116,12 @@ static void cdbdisp_waitDispatchFinish_async(struct CdbDispatcherState *ds); static bool cdbdisp_checkForCancel_async(struct CdbDispatcherState *ds); static int *cdbdisp_getWaitSocketFds_async(struct CdbDispatcherState *ds, int *nsocks); +/* + * Lets an extension handle its own NOTIFY channels arriving from QEs; see the + * declaration in cdbdisp.h for the contract. + */ +cdbdisp_notify_hook_type cdbdisp_notify_hook = NULL; + DispatcherInternalFuncs DispatcherAsyncFuncs = { cdbdisp_checkForCancel_async, @@ -136,6 +142,8 @@ static void checkDispatchResult(CdbDispatcherState *ds, int timeout_sec); static bool processResults(CdbDispatchResult *dispatchResult); +static void processNotifies(CdbDispatchResult *dispatchResult); + static void signalQEs(CdbDispatchCmdAsync *pParms); @@ -458,6 +466,7 @@ checkDispatchResult(CdbDispatcherState *ds, int timeout_sec) int timeout = 0; bool sentSignal = false; struct pollfd *fds; + bool *buffered; struct timeval start_ts, now; #ifdef USE_INTERNAL_FTS uint8 ftsVersion = 0; @@ -467,6 +476,12 @@ checkDispatchResult(CdbDispatcherState *ds, int timeout_sec) db_count = pParms->dispatchCount; fds = (struct pollfd *) palloc(db_count * sizeof(struct pollfd)); + /* + * Parallel to fds[]: whether that connection already holds a complete + * message in libpq's buffer rather than in its socket. See the loop below. + */ + buffered = (bool *) palloc(db_count * sizeof(bool)); + /* * OK, we are finished submitting the command to the segdbs. Now, we have * to wait for them to finish. @@ -478,6 +493,7 @@ checkDispatchResult(CdbDispatcherState *ds, int timeout_sec) int n; int nfds = 0; int ack_count = 0; + bool anyBuffered = false; PGconn *conn; /* @@ -555,6 +571,33 @@ checkDispatchResult(CdbDispatcherState *ds, int timeout_sec) Assert(sock >= 0); fds[nfds].fd = sock; fds[nfds].events = POLLIN; + + /* + * poll() reports what is in the socket, which is only the same + * thing as "what is left to read" while we are the only thing + * moving bytes off this connection. Anything else that writes to + * it through libpq breaks that: when a write cannot complete in one + * go, pqSendSome() calls pqReadData() to absorb whatever the peer + * has already sent (fe-misc.c), deliberately, so that a large write + * cannot deadlock against a peer that is writing back at us. The + * bytes then sit in conn->inBuffer, the socket goes quiet, and a QE + * that has finished and gone idle will never make it readable + * again -- so the results are in our own memory and we sleep + * forever waiting to be told about them. + * + * PQisBusy() runs libpq's parser over the buffer it already has and + * never touches the socket, so asking is free. A false answer means + * a complete message is ready now; take it in this round instead of + * polling, exactly as libpq's own async protocol prescribes (call + * poll() only once PQisBusy() says there is nothing to parse). + * + * This is the same class of lost wakeup as the outbound flush just + * above, and the same reason for handling it here. + */ + buffered[nfds] = !PQisBusy(conn); + if (buffered[nfds]) + anyBuffered = true; + nfds++; } @@ -582,6 +625,14 @@ checkDispatchResult(CdbDispatcherState *ds, int timeout_sec) else timeout = DISPATCH_WAIT_CANCEL_TIMEOUT_MSEC; + /* + * Something is already parseable, so there is nothing to wait for. + * Still poll, with no timeout, to pick up whatever else has arrived in + * the same round. + */ + if (anyBuffered) + timeout = 0; + n = poll(fds, nfds, timeout); /* @@ -620,6 +671,25 @@ checkDispatchResult(CdbDispatcherState *ds, int timeout_sec) if (timeout_sec >= 0 && diff_us >= timeout_sec * 1000000L) break; } + /* + * Buffered input counts as readable. poll() cleared revents and + * reported only the sockets, so put the buffered ones back before + * handing the array on -- handlePollSuccess() walks the same + * connections in the same order and reads revents to decide which to + * process. Checked ahead of the n == 0 arm: a round that found + * something to parse is not an expired wait, and treating it as one + * would signal the QEs and then loop back to the same state forever. + */ + else if (anyBuffered) + { + for (i = 0; i < nfds; i++) + { + if (buffered[i]) + fds[i].revents |= POLLIN; + } + + handlePollSuccess(pParms, fds); + } /* If the time limit expires, poll() returns 0 */ else if (n == 0) { @@ -657,6 +727,7 @@ checkDispatchResult(CdbDispatcherState *ds, int timeout_sec) handlePollSuccess(pParms, fds); } + pfree(buffered); pfree(fds); } @@ -988,6 +1059,96 @@ send_sequence_response(PGconn *conn, Oid oid, int64 last, int64 cached, int64 in elog(ERROR, "Failed to send sequence response: %s", PQerrorMessage(conn)); } +/* + * Hand over any notifications this QE has sent us. + * + * Kept separate from processResults() because it must run on every path that + * consumed input, not only the one where the QE still has work to do. A + * notification that arrives in the same read as the QE's command completion is + * already parsed out of the socket and queued on the connection, but that + * connection is about to be dropped from the poll set (stillRunning goes + * false), so nothing would ever wake us for it again -- the notification would + * be silently lost. nextval() never hit this because its QE blocks for a + * reply, leaving the command incomplete; a fire-and-forget sender does hit it. + */ +static void +processNotifies(CdbDispatchResult *dispatchResult) +{ + SegmentDatabaseDescriptor *segdbDesc = dispatchResult->segdbDesc; + + PGnotify *qnotifies = PQnotifies(segdbDesc->conn); + while(qnotifies && elog_geterrcode() == 0) + { + if (strcmp(qnotifies->relname, CDB_NOTIFY_NEXTVAL) == 0) + { + /* + * If there was nextval request then respond back on this libpq + * connection with the next value. Check and process nextval + * message only if QD has not already hit the error. Since QD could + * have hit the error while processing the previous nextval_qd() + * request itself and since full error handling is not complete yet + * (ex: releasing all the locks, etc.), shouldn't attempt to call + * nextval_qd() again. + */ + + CHECK_FOR_INTERRUPTS(); + + int64 last; + int64 cached; + int64 increment; + bool overflow; + Oid dbid; + Oid seq_oid; + + if (sscanf(qnotifies->extra, "%u:%u", &dbid, &seq_oid) != 2) + elog(ERROR, "invalid nextval message"); + + if (dbid != MyDatabaseId) + elog(ERROR, "nextval message database id:%u doesn't match my database id:%u", + dbid, MyDatabaseId); + + PG_TRY(); + { + nextval_qd(seq_oid, &last, &cached, &increment, &overflow); + } + PG_CATCH(); + { + send_sequence_response(segdbDesc->conn, seq_oid, last, cached, increment, overflow, true /* error */); + PG_RE_THROW(); + } + PG_END_TRY(); + /* respond back on this libpq connection with the next value */ + send_sequence_response(segdbDesc->conn, seq_oid, last, cached, increment, overflow, false /* error */); + } + else if (strcmp(qnotifies->relname, CDB_NOTIFY_ENDPOINT_ACK) == 0) + { + qnotifies->next = (struct pgNotify *) dispatchResult->ackPGNotifies; + dispatchResult->ackPGNotifies = qnotifies; + + /* Don't free the notify here since it in queue now */ + qnotifies = NULL; + } + else if (cdbdisp_notify_hook != NULL && + cdbdisp_notify_hook(dispatchResult, qnotifies)) + { + /* Consumed by an extension; nothing further to do here. */ + } + else + { + /* Got an unknown PGnotify, just record it in log */ + if (qnotifies->relname) + elog(LOG, "got an unknown notify message : %s", qnotifies->relname); + } + + if (qnotifies) + PQfreemem(qnotifies); + qnotifies = PQnotifies(segdbDesc->conn); + } + + forwardQENotices(); + +} + /* * Receive and process input from one QE. * @@ -1058,7 +1219,12 @@ processResults(CdbDispatchResult *dispatchResult) if (!pRes) { ELOG_DISPATCHER_DEBUG("%s -> idle", segdbDesc->whoami); - /* this is normal end of command */ + /* + * Normal end of command. Take any notifications with us: this + * connection is about to leave the poll set, so this is the last + * chance to see them. + */ + processNotifies(dispatchResult); return true; } @@ -1155,72 +1321,7 @@ processResults(CdbDispatchResult *dispatchResult) } forwardQENotices(); - - PGnotify *qnotifies = PQnotifies(segdbDesc->conn); - while(qnotifies && elog_geterrcode() == 0) - { - if (strcmp(qnotifies->relname, CDB_NOTIFY_NEXTVAL) == 0) - { - /* - * If there was nextval request then respond back on this libpq - * connection with the next value. Check and process nextval - * message only if QD has not already hit the error. Since QD could - * have hit the error while processing the previous nextval_qd() - * request itself and since full error handling is not complete yet - * (ex: releasing all the locks, etc.), shouldn't attempt to call - * nextval_qd() again. - */ - - CHECK_FOR_INTERRUPTS(); - - int64 last; - int64 cached; - int64 increment; - bool overflow; - Oid dbid; - Oid seq_oid; - - if (sscanf(qnotifies->extra, "%u:%u", &dbid, &seq_oid) != 2) - elog(ERROR, "invalid nextval message"); - - if (dbid != MyDatabaseId) - elog(ERROR, "nextval message database id:%u doesn't match my database id:%u", - dbid, MyDatabaseId); - - PG_TRY(); - { - nextval_qd(seq_oid, &last, &cached, &increment, &overflow); - } - PG_CATCH(); - { - send_sequence_response(segdbDesc->conn, seq_oid, last, cached, increment, overflow, true /* error */); - PG_RE_THROW(); - } - PG_END_TRY(); - /* respond back on this libpq connection with the next value */ - send_sequence_response(segdbDesc->conn, seq_oid, last, cached, increment, overflow, false /* error */); - } - else if (strcmp(qnotifies->relname, CDB_NOTIFY_ENDPOINT_ACK) == 0) - { - qnotifies->next = (struct pgNotify *) dispatchResult->ackPGNotifies; - dispatchResult->ackPGNotifies = qnotifies; - - /* Don't free the notify here since it in queue now */ - qnotifies = NULL; - } - else - { - /* Got an unknown PGnotify, just record it in log */ - if (qnotifies->relname) - elog(LOG, "got an unknown notify message : %s", qnotifies->relname); - } - - if (qnotifies) - PQfreemem(qnotifies); - qnotifies = PQnotifies(segdbDesc->conn); - } - - forwardQENotices(); + processNotifies(dispatchResult); return false; /* we must keep on monitoring this socket */ } diff --git a/src/backend/lib/bloomfilter.c b/src/backend/lib/bloomfilter.c index bde46f5b6e6..c7f0ed3f0fd 100644 --- a/src/backend/lib/bloomfilter.c +++ b/src/backend/lib/bloomfilter.c @@ -293,6 +293,47 @@ mod_m(uint32 val, uint64 m) return val & (m - 1); } +Size +bloom_bitset_bytes(const bloom_filter *filter) +{ + return filter != NULL ? (Size) (filter->m / BITS_PER_BYTE) : 0; +} + +const unsigned char * +bloom_bitset_data(const bloom_filter *filter) +{ + return filter != NULL ? filter->bitset : NULL; +} + +/* + * Create a filter sized exactly as bloom_create(total_elems, work_mem, seed) and + * initialize its bitset from the supplied bytes in one step. Returns NULL if the + * supplied length is not the filter's bitset size, so a wrongly-sized bitset is + * rejected rather than partially loaded. This is the only supported way to + * populate a filter from serialized bytes; afterwards a filter is only ever grown + * by bloom_add_element (or a raw bitwise OR of two equal-sized serialized parts, + * as Anser's coordinator fold does). + */ +bloom_filter * +bloom_create_from_bitset(int64 total_elems, int bloom_work_mem, uint64 seed, + const unsigned char *bitset, Size bitset_len) +{ + bloom_filter *filter; + + if (bitset == NULL) + return NULL; + + filter = bloom_create(total_elems, bloom_work_mem, seed); + if (bitset_len != (Size) (filter->m / BITS_PER_BYTE)) + { + bloom_free(filter); + return NULL; + } + + memcpy(filter->bitset, bitset, bitset_len); + return filter; +} + double bloom_false_positive_rate(bloom_filter *filter) { diff --git a/src/backend/tcop/postgres.c b/src/backend/tcop/postgres.c index 806ccfcef85..394f8c9b39f 100644 --- a/src/backend/tcop/postgres.c +++ b/src/backend/tcop/postgres.c @@ -596,6 +596,11 @@ SocketBackend(StringInfo inBuf) doing_extended_query_message = false; break; + case GP_SIDEBAND_MESSAGE: /* Cloudberry QD -> QE sideband data */ + maxmsglen = PQ_LARGE_MESSAGE_LIMIT; + doing_extended_query_message = false; + break; + default: /* @@ -6437,6 +6442,18 @@ PostgresMain(const char *dbname, const char *username) */ break; + case GP_SIDEBAND_MESSAGE: /* Cloudberry QD -> QE sideband data */ + /* + * Accept but ignore this message. A QE reads sideband data + * explicitly at the point it expects it; reaching the command + * loop means nobody is waiting for it any more -- typically + * the waiter was interrupted (query cancel) after the QD had + * already written the message. Same reasoning as '?' above, + * but a wider window, since a sideband waiter may block for + * the lifetime of a query. + */ + break; + default: ereport(FATAL, (errcode(ERRCODE_PROTOCOL_VIOLATION), diff --git a/src/include/catalog/catversion.h b/src/include/catalog/catversion.h index c9c2fd83275..8ec2c11f0e9 100644 --- a/src/include/catalog/catversion.h +++ b/src/include/catalog/catversion.h @@ -60,6 +60,6 @@ */ /* 3yyymmddN */ -#define CATALOG_VERSION_NO 302608261 +#define CATALOG_VERSION_NO 302609151 #endif diff --git a/src/include/cdb/cdbdisp.h b/src/include/cdb/cdbdisp.h index 9fac725c5fd..3c7179fffe6 100644 --- a/src/include/cdb/cdbdisp.h +++ b/src/include/cdb/cdbdisp.h @@ -22,11 +22,34 @@ #define CDB_MOTION_LOST_CONTACT_STRING "Interconnect error master lost contact with segment." struct CdbDispatchResults; /* #include "cdb/cdbdispatchresult.h" */ +struct CdbDispatchResult; /* #include "cdb/cdbdispatchresult.h" */ struct CdbPgResults; struct Gang; /* #include "cdb/cdbgang.h" */ struct ResourceOwnerData; +struct pgNotify; /* #include "libpq-fe.h" */ enum GangType; +/* + * Hook for an extension to consume a NOTIFY sent by a QE on its dispatch + * connection, alongside the sequence and endpoint-ack channels the dispatcher + * handles itself. Return true if the notify was consumed, false to let the + * dispatcher fall through to its own handling. + * + * 'dispatchResult' identifies the sending QE and, through its meleeResults, + * gives access to every connection of the current dispatch -- which is what + * lets a handler answer on other QEs' connections (see GP_SIDEBAND_MESSAGE). + * + * The hook runs inside the QD's dispatch-result processing, which is reached + * from the interconnect wait loop while the query is executing. It must + * therefore be quick, must not re-enter the dispatcher, and should not throw + * for conditions it can recover from: an error here propagates into the + * running query. The PGnotify belongs to the caller and is freed after the + * hook returns, so a handler must copy anything it wants to keep. + */ +typedef bool (*cdbdisp_notify_hook_type) (struct CdbDispatchResult *dispatchResult, + struct pgNotify *notify); +extern PGDLLIMPORT cdbdisp_notify_hook_type cdbdisp_notify_hook; + /* * Types of message to QE when we wait for it. */ diff --git a/src/include/cdb/cdbvars.h b/src/include/cdb/cdbvars.h index 5389e5a3979..efd05538a17 100644 --- a/src/include/cdb/cdbvars.h +++ b/src/include/cdb/cdbvars.h @@ -802,6 +802,19 @@ extern const char * lookup_autostats_mode_by_value(GpAutoStatsModeValue val); */ #define CDB_NOTIFY_ENDPOINT_ACK "ack_notify" +/* + * Message type for QD -> QE sideband data sent on the dispatch connection + * outside the normal query protocol, in the same spirit as the '?' sequence + * response. The QE reads it explicitly where it expects it; a message that + * arrives unexpectedly (typically because the QE was cancelled while waiting + * for one) is accepted and discarded by the command loop, so it cannot break + * message-boundary sync. + * + * Unlike '?', a sideband message may be large: it is read with + * PQ_LARGE_MESSAGE_LIMIT. + */ +#define GP_SIDEBAND_MESSAGE '!' + typedef enum WarehouseStatus { WAREHOUSE_STATUS_CREATING, diff --git a/src/include/lib/bloomfilter.h b/src/include/lib/bloomfilter.h index 05b736f566e..51afa223e03 100644 --- a/src/include/lib/bloomfilter.h +++ b/src/include/lib/bloomfilter.h @@ -27,5 +27,11 @@ extern double bloom_false_positive_rate(bloom_filter *filter); extern uint64 bloom_total_bits(bloom_filter *filter); extern bloom_filter *bloom_create_aggresive(int64 total_elems, int work_mem, uint64 seed); +extern Size bloom_bitset_bytes(const bloom_filter *filter); +extern const unsigned char *bloom_bitset_data(const bloom_filter *filter); +extern bloom_filter *bloom_create_from_bitset(int64 total_elems, + int bloom_work_mem, uint64 seed, + const unsigned char *bitset, + Size bitset_len); #endif /* BLOOMFILTER_H */ From ddcd2e931aaa952691f0533f62370d9f0de97747 Mon Sep 17 00:00:00 2001 From: Leonid Borchuk Date: Thu, 15 Oct 2026 13:59:18 +0300 Subject: [PATCH 12/27] Feature: Anser runtime filters for hash joins Anser is a publish/subscribe facility for MPP execution. Producers on the segments publish something about a running query, the coordinator combines the per-segment parts into one, and consumers on the segments receive it and prune work with it. Today that is a bloom filter over a hash join's build key, used to skip probe rows that cannot join. A channel is the rendezvous for one such piece of information in one query, keyed by session, command, and the condition it describes. It lives only in the coordinator backend running the query, for exactly as long as that query runs: no shared memory, no background worker, no second connection to authenticate. Parts travel up as a NOTIFY and the merged payload comes back as a sideband message. Both nodes wrap a base scan rather than an intermediate node, which makes the filter a superset of the keys the joins below would have left -- less selective, never wrong. The direction is fixed to build -> probe, because a consumer on the inner side would wait on a producer the join is not yet reading, which is a deadlock and not a slow path. Every failure ends in unfiltered execution: a filter too small to reject anything, a squelched producer, a malformed part, a missed deadline. The planner gate is the one that costs nothing, and the later ones exist because a row estimate can be wrong. Built only with --with-anser and off by default; the subsystem creates no catalog objects and is configured entirely by GUCs. Includes the regression suites and the CI job that runs them. Co-Authored-By: Claude Opus 5 --- .github/workflows/build-cloudberry.yml | 14 + configure | 32 + configure.ac | 7 + .../scripts/configure-cloudberry.sh | 2 + gpcontrib/Makefile | 7 + gpcontrib/anser/Makefile | 103 +++ gpcontrib/anser/README.md | 454 ++++++++++ gpcontrib/anser/anser_test--1.0.sql | 99 +++ gpcontrib/anser/anser_test.control | 22 + gpcontrib/anser/expected/anser_multijoin.out | 258 ++++++ .../anser/expected/anser_runtime_filter.out | 399 +++++++++ gpcontrib/anser/expected/anser_test.out | 286 +++++++ gpcontrib/anser/include/anser.h | 84 ++ gpcontrib/anser/include/anserbloom.h | 79 ++ gpcontrib/anser/include/anserfilter.h | 127 +++ gpcontrib/anser/include/anserpayload.h | 150 ++++ gpcontrib/anser/include/anserplan.h | 101 +++ gpcontrib/anser/include/ansersideband.h | 249 ++++++ gpcontrib/anser/sql/anser_multijoin.sql | 228 +++++ gpcontrib/anser/sql/anser_runtime_filter.sql | 305 +++++++ gpcontrib/anser/sql/anser_test.sql | 184 ++++ gpcontrib/anser/src/anser_test.c | 765 +++++++++++++++++ gpcontrib/anser/src/anserbloomconsume.c | 202 +++++ gpcontrib/anser/src/anserbloomproduce.c | 219 +++++ gpcontrib/anser/src/anserdispatch.c | 713 ++++++++++++++++ gpcontrib/anser/src/anserfilter.c | 376 ++++++++ gpcontrib/anser/src/anserinit.c | 235 +++++ gpcontrib/anser/src/anserpayload.c | 108 +++ gpcontrib/anser/src/anserplan.c | 637 ++++++++++++++ gpcontrib/anser/src/anserplanexec.c | 803 ++++++++++++++++++ gpcontrib/anser/src/ansersideband.c | 641 ++++++++++++++ pom.xml | 4 + src/Makefile.global.in | 1 + 33 files changed, 7894 insertions(+) create mode 100644 gpcontrib/anser/Makefile create mode 100644 gpcontrib/anser/README.md create mode 100644 gpcontrib/anser/anser_test--1.0.sql create mode 100644 gpcontrib/anser/anser_test.control create mode 100644 gpcontrib/anser/expected/anser_multijoin.out create mode 100644 gpcontrib/anser/expected/anser_runtime_filter.out create mode 100644 gpcontrib/anser/expected/anser_test.out create mode 100644 gpcontrib/anser/include/anser.h create mode 100644 gpcontrib/anser/include/anserbloom.h create mode 100644 gpcontrib/anser/include/anserfilter.h create mode 100644 gpcontrib/anser/include/anserpayload.h create mode 100644 gpcontrib/anser/include/anserplan.h create mode 100644 gpcontrib/anser/include/ansersideband.h create mode 100644 gpcontrib/anser/sql/anser_multijoin.sql create mode 100644 gpcontrib/anser/sql/anser_runtime_filter.sql create mode 100644 gpcontrib/anser/sql/anser_test.sql create mode 100644 gpcontrib/anser/src/anser_test.c create mode 100644 gpcontrib/anser/src/anserbloomconsume.c create mode 100644 gpcontrib/anser/src/anserbloomproduce.c create mode 100644 gpcontrib/anser/src/anserdispatch.c create mode 100644 gpcontrib/anser/src/anserfilter.c create mode 100644 gpcontrib/anser/src/anserinit.c create mode 100644 gpcontrib/anser/src/anserpayload.c create mode 100644 gpcontrib/anser/src/anserplan.c create mode 100644 gpcontrib/anser/src/anserplanexec.c create mode 100644 gpcontrib/anser/src/ansersideband.c diff --git a/.github/workflows/build-cloudberry.yml b/.github/workflows/build-cloudberry.yml index 83536c413f7..3dc5f38e5c6 100644 --- a/.github/workflows/build-cloudberry.yml +++ b/.github/workflows/build-cloudberry.yml @@ -243,6 +243,11 @@ jobs: "make_configs":["src/test/regress:installcheck-good"], "pg_settings":{"optimizer":"on"} }, + {"test":"ic-anser", + "make_configs":["gpcontrib/anser:installcheck-with-install"], + "shared_preload_libraries":"anser", + "postgres_conf_addons":"anser.enable=on" + }, {"test":"pax-ic-good-opt-off", "make_configs":[ "contrib/pax_storage/:pax-test", @@ -1570,6 +1575,15 @@ jobs: echo "Adding shared_preload_libraries: ${{ matrix.shared_preload_libraries }}" fi + # Append extra postgresql.conf settings requested by the matrix + # entry (e.g. postmaster-context GUCs a test module requires). + # BLDWRAP_POSTGRES_CONF_ADDONS is a pipe-separated list of + # key=value pairs (see gpAux/gpdemo/demo_cluster.sh). + if [[ -n "${{ matrix.postgres_conf_addons }}" ]]; then + EXTRA_CONF="${EXTRA_CONF}${EXTRA_CONF:+|}${{ matrix.postgres_conf_addons }}" + echo "Adding postgres_conf_addons: ${{ matrix.postgres_conf_addons }}" + fi + if ! time su - gpadmin -c "cd ${SRC_DIR} && NUM_PRIMARY_MIRROR_PAIRS='${{ matrix.num_primary_mirror_pairs }}' BLDWRAP_POSTGRES_CONF_ADDONS=\"${EXTRA_CONF}\" SRC_DIR=${SRC_DIR} ${SRC_DIR}/devops/build/automation/cloudberry/scripts/create-cloudberry-demo-cluster.sh"; then echo "::error::Demo cluster creation failed" exit 1 diff --git a/configure b/configure index ce7322eb553..b21834c7d66 100755 --- a/configure +++ b/configure @@ -818,6 +818,7 @@ GCOV enable_debug enable_orafce enable_faultinjector +with_anser enable_debug_extensions enable_gpfdist enable_strong_random @@ -889,6 +890,7 @@ enable_atomics enable_strong_random enable_gpfdist enable_debug_extensions +with_anser enable_faultinjector enable_orafce enable_debug @@ -1657,6 +1659,7 @@ Optional Packages: --with-libraries=DIRS look for additional libraries in DIRS --with-libs=DIRS alternative spelling of --with-libraries --with-pgport=PORTNUM set default port number [5432] + --with-anser build the Anser runtime-filter extension --with-blocksize=BLOCKSIZE set table block size in kB [8] --with-segsize=SEGSIZE set table segment size in GB [1] @@ -3597,6 +3600,35 @@ fi +# +# Anser adaptive information sharing (runtime filters) +# + + + +# Check whether --with-anser was given. +if test "${with_anser+set}" = set; then : + withval=$with_anser; + case $withval in + yes) + : + ;; + no) + : + ;; + *) + as_fn_error $? "no argument expected for --with-anser option" "$LINENO" 5 + ;; + esac + +else + with_anser=no + +fi + + + + # # Enable fault injector # diff --git a/configure.ac b/configure.ac index 8c8e3fa54be..9be6ba62ffc 100644 --- a/configure.ac +++ b/configure.ac @@ -234,6 +234,13 @@ PGAC_ARG_BOOL(enable, debug-extensions, yes, [exclude debug extensions in gpcontrib]) AC_SUBST(enable_debug_extensions) +# +# Anser adaptive information sharing (runtime filters) +# +PGAC_ARG_BOOL(with, anser, no, + [build the Anser runtime-filter extension]) +AC_SUBST(with_anser) + # # Enable fault injector # diff --git a/devops/build/automation/cloudberry/scripts/configure-cloudberry.sh b/devops/build/automation/cloudberry/scripts/configure-cloudberry.sh index 989b1432365..370c97703e6 100755 --- a/devops/build/automation/cloudberry/scripts/configure-cloudberry.sh +++ b/devops/build/automation/cloudberry/scripts/configure-cloudberry.sh @@ -32,6 +32,7 @@ # - IC Proxy Support # - Oracle Compatibility (orafce) # - ORCA Query Optimizer +# - Anser Runtime Filters # - PAX Access Method # - Test Automation Support (tap-tests) # @@ -158,6 +159,7 @@ execute_cmd ./configure --prefix=${BUILD_DESTINATION} \ --enable-pax \ --enable-tap-tests \ ${CONFIGURE_DEBUG_OPTS} \ + --with-anser \ --with-diskquota \ --with-gp-stats-collector \ --with-gssapi \ diff --git a/gpcontrib/Makefile b/gpcontrib/Makefile index 63c6f80c8f6..c5af15e5e12 100755 --- a/gpcontrib/Makefile +++ b/gpcontrib/Makefile @@ -51,6 +51,12 @@ endif ifeq "$(with_gp_stats_collector)" "yes" recurse_targets += gp_stats_collector endif + +# Anser is opt-in at configure time: --with-anser (off by default). +ifeq "$(with_anser)" "yes" + recurse_targets += anser +endif + $(call recurse,all install clean distclean, $(recurse_targets)) all: gpcloud orafce @@ -92,3 +98,4 @@ installcheck: $(MAKE) -C gp_toolkit installcheck $(MAKE) -C gp_exttable_fdw installcheck if [ "$(with_diskquota)" = "yes" ]; then $(MAKE) -C diskquota installcheck; fi + if [ "$(with_anser)" = "yes" ]; then $(MAKE) -C anser installcheck; fi diff --git a/gpcontrib/anser/Makefile b/gpcontrib/anser/Makefile new file mode 100644 index 00000000000..42c93ac0ff7 --- /dev/null +++ b/gpcontrib/anser/Makefile @@ -0,0 +1,103 @@ +#------------------------------------------------------------------------- +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# +# Makefile for the anser extension. +# +# IDENTIFICATION +# gpcontrib/anser/Makefile +# +#------------------------------------------------------------------------- + +MODULE_big = anser +OBJS = \ + $(WIN32RES) \ + src/anserbloomconsume.o \ + src/anserbloomproduce.o \ + src/anserdispatch.o \ + src/anserfilter.o \ + src/anserinit.o \ + src/anserpayload.o \ + src/anserplan.o \ + src/anserplanexec.o \ + src/ansersideband.o \ + src/anser_test.o + +PGFILEDESC = "anser - adaptive information sharing runtime filters" + +# The subsystem itself needs no catalog objects -- it is configured entirely by +# GUCs and travels over the dispatch connection. The only extension here is +# anser_test, which exposes the internal C API to the regression tests from +# inside the same library. +EXTENSION = anser_test +DATA = anser_test--1.0.sql + +REGRESS = anser_test anser_runtime_filter anser_multijoin + +# src/anserdispatch.c writes sideband messages onto the dispatcher's own libpq +# connections, so it needs libpq's internal headers (as the dispatcher itself +# does). Deliberately NOT linked against libpq: the pq* writers we use stay +# global in the postgres binary, while the public PQ* API is made local there +# on purpose (src/backend/Makefile), so linking libpq.so would put a second +# copy of libpq on connections the backend's copy created. +PG_CPPFLAGS = -I$(srcdir)/include -I$(libpq_srcdir) +SHLIB_PREREQS = submake-libpq + +ifdef USE_PGXS +PG_CONFIG ?= pg_config +PGXS := $(shell $(PG_CONFIG) --pgxs) +include $(PGXS) +else +subdir = gpcontrib/anser +top_builddir = ../.. +include $(top_builddir)/src/Makefile.global +include $(top_srcdir)/contrib/contrib-global.mk +endif + +# The tests need the live gather/send services, which only exist when the +# library is preloaded and anser.enable is on -- both postmaster-context +# settings. Ensure them (and restart) before installcheck, as +# gp_relsizes_stats does for its worker. +installcheck: preload-anser + +.PHONY: preload-anser +preload-anser: + @if [ -z "$$COORDINATOR_DATA_DIRECTORY$$MASTER_DATA_DIRECTORY" ]; then \ + echo "ERROR: COORDINATOR_DATA_DIRECTORY (or MASTER_DATA_DIRECTORY) is not set;" >&2; \ + echo " source cloudberry-env.sh and gpdemo-env.sh before running installcheck." >&2; \ + exit 1; \ + fi + @current=`psql -d postgres -At -c "SHOW shared_preload_libraries;" | tr -d ' '`; \ + case ",$$current," in \ + *,anser,*) \ + echo "==> shared_preload_libraries already contains anser" ;; \ + *) \ + if [ -z "$$current" ]; then new=anser; else new="$$current,anser"; fi; \ + echo "==> Adding anser to shared_preload_libraries ($$new)"; \ + gpconfig -c shared_preload_libraries -v "'$$new'" --skipvalidation ;; \ + esac + gpconfig -c anser.enable -v on --skipvalidation + gpstop -ra + psql -d postgres -c "SHOW shared_preload_libraries;" + +# CI runs a single make target per test config, so offer one target that +# installs the extension, arms the cluster, and runs the tests. +.PHONY: installcheck-with-install +installcheck-with-install: + $(MAKE) install + $(MAKE) installcheck diff --git a/gpcontrib/anser/README.md b/gpcontrib/anser/README.md new file mode 100644 index 00000000000..7d6e9305581 --- /dev/null +++ b/gpcontrib/anser/README.md @@ -0,0 +1,454 @@ +# Anser — adaptive information sharing + +Anser is a runtime pub/sub facility for MPP query execution. Producers on the +segments publish a small piece of information about a running query (today: a +bloom filter over a join-build key), the coordinator combines the per-segment +parts into one, and consumers on the segments receive it and use it to prune +work (today: skip probe rows that cannot join). + +Everything travels over the **dispatch connection the coordinator already holds +open to every segment**. There is no shared memory, no background worker, no +second connection and nothing extra to authenticate: a channel exists only in +the coordinator backend running the query, for exactly as long as that query +runs. + +This document covers installation, the architecture, the wire protocol, the +configuration surface, and what a *channel* is. For the plan-tree integration +see `anserplan.c`; for the payload/bloom protocol see `anserfilter.c` and +`lib/bloomfilter.c`. + +## Installation + +Anser is configured entirely by GUCs — it creates no catalog objects, so there +is nothing to `CREATE EXTENSION` in each database. + +``` +gpconfig -c shared_preload_libraries -v "'anser'" --skipvalidation +gpconfig -c anser.enable -v on +gpstop -ra +``` + +The library **must** be preloaded: a segment backend deserializing a dispatched +plan has no opportunity to load it on demand, and its CustomScan providers have +to be registered before that happens. + +Then turn the filter on where you want it — `anser.runtime_filter` is +`PGC_USERSET`, so per session, per role, or cluster-wide. + +`anser_test` is a separate control file over the same library, exposing the +internal C API to the regression tests. It is test-only; do not create it in +production databases. + +## Architecture + +A **channel** is one rendezvous point between the producers and consumers of a +single piece of runtime information, for a single query: + +``` +AnserChannelKey = { gp_session_id, gp_command_count, condition_id, condition_key[64] } +``` + +- `gp_session_id` + `gp_command_count` scope the channel to one query execution, + so keys never collide across sessions or across statements in a session. +- `condition_id` distinguishes multiple filters within the same query. +- `condition_key` is an opaque string describing the filtered condition — today + just `anser_rf_`, since the planner stamps the same key into + both plan nodes at injection time. It exists for the case where the key has to + be derived from the build's semantic identity instead (so that producer and + consumer can find each other without having been planned together), which is + why the wire format treats it as arbitrary bytes rather than as an identifier. + +Channels live in a hash in the coordinator backend, created on first use and +dropped at `ExecutorEnd` (or on transaction abort). Since the merge and the +delivery both happen in that one process, the accumulator is an ordinary +`palloc`'d buffer. + +### Where the nodes go + +Both nodes wrap a **base scan**, never an intermediate node. The producer goes +above the scan the join's build key comes from, the consumer above the scan its +probe key comes from; either may be several joins away, and `anser_resolve_key_scan()` +follows the key column down through Hash, Motion, HashJoin and our own nodes to +find it. + +Wrapping a base scan is not a preference. After `set_plan_references` an +intermediate node's targetlist holds `OUTER_VAR`/`INNER_VAR` Vars, and a +`CustomScan` keeps its child in `custom_plans` rather than in `lefttree`, so +copying such a targetlist into `custom_scan_tlist` leaves EXPLAIN resolving an +`OUTER_VAR` against a node with no outer plan — `get_variable()` raises +"bogus varno" and the plan cannot be printed at all. + +Two consequences: + +- A filter built from a base scan holds that relation's **whole** key column, + not the subset the joins below it would have left. A superset never rejects a + row that could join, so it is correct; it is just less selective than a filter + built on the joined result would be. +- When two joins reach the same relation, the second **stacks** its consumer + under the first rather than replacing it, so the scan is filtered by both. + +The direction is fixed: a filter can only flow **build → probe**. A hash join +builds its hash table from the inner side before reading the outer, so a +consumer on the inner side would wait for a producer whose rows the join is not +yet reading — and that producer only publishes once its subtree is drained, +which it cannot be. That is a deadlock, not a slow path, and it is why the +intermediate result cannot filter the table it is being joined to. + +### Giving up early + +A bloom filter that is too small for its key count matches almost everything: it +costs a hash and `k` probes per probe row and eliminates nothing. Anser checks +for that at the three points where new information becomes available, and always +fails open — giving up means the query runs unfiltered, never that it runs wrong. + +| Where | Knows | Check | Cost of giving up | +| --- | --- | --- | --- | +| Planner (`anserplan.c`) | the row estimate | bitset bits / estimated keys ≥ 4 | nothing — the nodes are never injected | +| Producer init (`anserfilter.c`) | the plan parameters | the filter fits the payload cap, and still ≥ 4 bits/key | one `palloc0`, freed immediately; cancel published on the first tuple, before the build side is scanned | +| Producer publish (`anserbloomproduce.c`) | the filter, fully built | estimated FPR (`fill^k`) ≤ 50% | the build scan, already spent; saves the network and every consumer's probing | +| Coordinator merge (`anserdispatch.c`) | the merged payload | fill ≤ 95% | the merge, already spent; saves delivery and probing | + +The planner gate is the one that matters, because it is the only one that costs +nothing. The rest exist because a row estimate can be wrong, and the last one +exists because **folding changes the answer**: OR-ing three parts that are each +60% full gives a merged filter that is 94% full, so no producer can tell whether +the result will be useful. + +One subtlety worth knowing if you touch the sizing: do **not** clamp the element +estimate to make a filter fit. `total_elems` is also what `optimal_k()` derives +the hash count from, so understating it misconfigures the filter — a 128M-row +build side declared as 33.5M gets `k=10` where the optimum is `k=3`, turning a +13% false positive rate into 38%. The keys all go in regardless of what was +written down. Clamp the *size*; keep the count honest. + +### Parallel execution + +A parallel slice runs `numsegments × parallel_workers` processes, and **every +one of them is a full QE with its own dispatch connection** — not a PostgreSQL +background worker. So each publishes and subscribes for itself, and the +coordinator folds however many parts arrive. Nothing about the transport or the +merge changes. + +Only the number of parts to expect differs, and it is not the segment count: it +is the width of the build scan's slice, `numsegments × parallel_workers`. The +planner computes that while injecting and stamps it into the plan node +(`ANSER_RF_PRIV_N_PRODUCERS`); the executor reads it from there. Both optimizers +go through the same path, and a join whose slice cannot be identified is not +injected into. + +### Delivery cost + +Fan-in is parallel and fan-out is not. Every producer uploads its part at the +same time while the coordinator reads as it goes, but `anser_disp_deliver()` +then walks its subscribers and writes the merged filter to each in turn, +blocking until that peer has taken it. Delivery latency is therefore the *sum* +of the writes, not the slowest one, and it grows with the width of the slice. + +Measured on 4 segments, 5000 build rows, a 1 MB filter (`bloom_create` floors +every bitset at 1 MB, whatever the cardinality): + +| Slice width | Subscribers | Fan-in | Fan-out | Consumer stalls | +| --- | --- | --- | --- | --- | +| 4 | 4 | ~40 ms | ~520 ms | 549 ms | +| 16 | 16 | ~100 ms | ~1140 ms | 1005 ms, then gave up | + +The second row is why the default deadline is 100 s rather than 1 s: at 16 +subscribers the fan-out outlived a 1 s deadline, so every consumer ran +unfiltered. Raising it makes the filter arrive, but the wait is not free — the +first subscriber is served in under a millisecond and the last over a second +later, and none of them can tell where in the queue they are. + +**A filter can cost more than it saves.** In the 16-wide plan the consumer waits +160 ms to skip a probe scan that takes 40 ms. Anser is worth enabling when the +build side is selective *and* the probe side is large enough that the scan +dominates the exchange — not on small joins, and not, today, on wide slices. + +Each part is a whole bitset, because a union is a bitwise OR: N producers send +N × the full payload, and the coordinator returns the full payload to each of N +consumers. Nothing amortises with cluster size. For a 64 MB filter — the +ceiling `anser.max_info_size` allows — on 128 segments: + +| `parallel_workers` | Producers = consumers | Fan-in (base64, ×4/3) | Fan-out (raw) | Through the coordinator | +| --- | --- | --- | --- | --- | +| 1 | 128 | 10.7 GiB | 8.0 GiB | **18.7 GiB** | +| 4 | 512 | 42.7 GiB | 32.0 GiB | **74.7 GiB** | +| 8 | 1024 | 85.3 GiB | 64.0 GiB | **149.3 GiB** | + +Per join, over one coordinator NIC, with a CRC and a base64 decode per part on +one process. A star topology through a single coordinator is the wrong shape for +a reduction that is associative, commutative and idempotent; +`docs/local-fold.md` describes folding a segment's workers together before they +reach the coordinator, which is the first step away from it. + +### Plan shapes: the two optimizers + +Both optimizers reach the same exchange through the same code, but they choose +different slice widths for the same query, and the width is what governs +delivery cost. Same query, same cluster (4 segments): + +GPORCA, `max_parallel_workers_per_gather = 8` — a **parallel** build slice: + +``` + Gather Motion 16:1 (slice1; segments: 16) + -> Hash Join + -> Custom Scan (Anser Bloom Consumer) + -> Parallel Seq Scan on p_probe p + -> Hash + -> Broadcast Motion 16:16 (slice2; segments: 16) + -> Custom Scan (Anser Bloom Producer) + -> Parallel Seq Scan on p_build b +``` + +PostgreSQL planner (`optimizer = off`) — a **serial** build slice: + +``` + Gather Motion 8:1 (slice1; segments: 8) + -> Hash Join + -> Custom Scan (Anser Bloom Consumer) + -> Parallel Seq Scan on p_probe p + -> Hash + -> Broadcast Motion 2:8 (slice2; segments: 2) + -> Custom Scan (Anser Bloom Producer) + -> Seq Scan on p_build b +``` + +The difference is `Broadcast Motion 16:16` against `2:8`: ORCA parallelises the +build scan, the PG planner does not. So ORCA produces 16 producers and 16 +consumers where the PG planner produces 2 and 8 — eight times the parts to fold +and four times the deliveries, for the same filter. The PG plan finished the +whole query in 123 ms; the ORCA plan needed 282 ms with the filter delivered, +and 1293 ms when the fan-out overran the old 1 s deadline. + +Read `Gather Motion N:1` as the process count, not the segment count: +`FillSliceGangInfo()` fills a slice's segment list with `numsegments × +parallel_workers` entries, so `16:1` on 4 segments means 4 workers each. The +`x(0) workers` in the per-slice memory footer is a PostgreSQL `Gather`'s +`nworkers_launched`, which is always 0 here — an MPP slice has no `Gather` node — +and says nothing about MPP parallelism. + +### Payload types + +A channel carries one **payload type**, declared on the wire and registered in +`anserpayload.c`. The transport itself moves opaque bytes and never branches on +what they mean; everything type-specific lives in one descriptor +(`AnserPayloadOps`): + +| | `fold()` | `checksum_body` | +| --- | --- | --- | +| `B` — bloom filter | bitwise OR of equal-sized bitsets | yes | +| `-` — none (a subscription) | n/a | n/a | + +`fold()` is how the coordinator reduces one part per producer to a single +payload; there is no generic answer, which is why each type supplies it. A row +count, for instance, would fold by summing. + +`checksum_body` is about consequence, not size. A bloom filter fails +asymmetrically — a bit flipped 1 → 0 removes a key, so a joinable row is +rejected and the query silently returns too few rows — whereas a row count only +feeds a planning decision, where corruption costs a worse plan and never a wrong +answer. Types of the second kind opt out and pay nothing. (The header and +condition key are checksummed either way; that is ~100 ns and it is what +protects the routing fields.) + +Adding a type is three steps, none of which touch the framing: define a code, +add a row to `AnserPayloadTable`, and pass the code from the producer and +consumer nodes. `anserpayload.h` has the details. + +### How it attaches to the server + +Everything is reached through an existing extensibility point, so the server +carries no Anser-specific code (`anserinit.c`): + +| Hook | Used for | +| --- | --- | +| `planner_hook` | the injection pass, on the finished plan; wrapping the hook covers ORCA too, since it is dispatched from inside `standard_planner()` | +| `RegisterCustomScanMethods` | the producer/consumer nodes, so their methods resolve by name in every backend that deserializes a dispatched plan | +| `cdbdisp_notify_hook` | parts and subscriptions arriving from segments | +| `ExecutorEnd_hook` | dropping a query's channels | +| `DefineCustom*Variable` | the `anser.*` GUCs below | + +The first two already existed; `cdbdisp_notify_hook` and the +`GP_SIDEBAND_MESSAGE` tolerance in the QE command loop are the only additions +Anser needed in the server, and neither mentions Anser. + +## Wire protocol + +The two directions are deliberately asymmetric, because the constraints differ. + +**Segment → coordinator** is a `NOTIFY` on channel `anser_rf`, the model +`nextval()` uses (`cdb_sequence_nextval_qe` in `commands/sequence.c`). The QD is +a libpq *client*, and libpq rejects message types it does not know, so this +direction has to be a message type libpq already understands. `NotifyMyFrontEnd` +imposes no length limit of its own — the ~8 KB `NOTIFY_PAYLOAD_MAX_LENGTH` +applies to the SQL-level `NOTIFY`, which must fit a queue page — but it delivers +through `pq_sendstring`, so the payload must be a NUL-free string: + +``` +anser3 K T SSSSSSSSSS CCCCCCCCCC DDDDDDDDDD PPPPPPPPPP TTTTTTTTTT FFFF KKKK BBBBBBBBBB XXXXXXXX +^tag ^ ^ session command condition part total flags keylen bodylen crc + | \ payload type + \ kind + +``` + +`kind` is `P` (a producer's part) or `S` (a consumer subscribing) — what the +message *does*, as opposed to the payload type, which is what it *carries*. The +header is **95 bytes of fixed-width ASCII**, so the key and the body begin at +offsets that nothing in their own contents can shift, and the two lengths must +account for the message exactly — a short, long or misaligned message is rejected +before any of it is used. There is deliberately no delimiter anywhere in the +format: the earlier version ended its header with a newline and found it with +`strchr()`, which was correct only while the key could not itself contain a +newline, an invariant nothing enforced. + +**Coordinator → segment** is a `GP_SIDEBAND_MESSAGE`, written with `pqPutnchar`, +which performs no conversion — so the merged filter travels as **raw binary**, +with no base64 tax. That is the direction that matters most, since the merged +payload is sent once *per consumer* while each part is sent once. + +Both directions carry a **CRC32C**, and both discard a message that fails it. +The transport is TCP, not the UDP interconnect, so this is not about a lossy +link — TCP's 16-bit checksum is simply thin cover for a megabyte, and hardware +CRC32C costs about 0.05 ms/MB. The header and key are always covered; whether +the body is covered too is the payload type's decision, for the reasons in +[Payload types](#payload-types) above. Where it applies, the segment → +coordinator CRC covers the pre-base64 bytes, so it validates the decode as well. + +The coordinator services these while it is blocked receiving tuples: the +interconnect adds every dispatch socket to its wait set +(`ic_udpifc.c`, `ic_tcp.c`), and a readable one leads to +`checkForCancelFromQD` → `cdbdisp_checkForCancel` → `processResults`, which is +where the notify handler runs. Under `gp_interconnect_type=proxy` that check is +driven by a 2 s timer instead (`ic_proxy_backend.c`), so filter delivery can lag +by up to that long. + +## GUCs + +| GUC | Default | Context | Meaning | +| --- | --- | --- | --- | +| `anser.enable` | `off` | SIGHUP | Master switch. With it off the plan pass never injects anything and no filters are exchanged. | +| `anser.runtime_filter` | `off` | USERSET | Enables the post-planning pass that injects bloom-filter producer/consumer nodes into a matching plan. Requires `anser.enable`. | +| `anser.max_info_size` | `65 MB` | POSTMASTER | Maximum serialized payload (merged bloom filter + part header) a channel may hold; caps the effective bloom-filter size. The default is `64 MB + 1 MB` so a full 64 MB power-of-two bitset fits with its header; `bloom_create` also floors every bitset at 1 MB. | +| `anser.timeout_ms` | `100 s` | USERSET | How long a consumer waits for its filter before running unfiltered. The deadline matters because a producer that gets squelched never publishes at all: `ExecSquelchNode` only marks a `CustomScanState`, it does not call the node back. The default is generous because delivery is serial — see [Delivery cost](#delivery-cost). | +| `anser.debug` | `off` | USERSET | Traces the exchange — publish, merge, delivery, receive — in the log of the process each step happens in. See below. | + +### Tracing an exchange + +Until a filter is either used or timed out, none of the handoff is visible in +`EXPLAIN`: a consumer that waited and got nothing looks exactly like one whose +producers never published. `anser.debug` makes each step log where it happened, +which is normally the fastest way to find where an exchange broke: + +``` +seg0 producer init cond=0 part=0/3 elems=3334 payload=67108928 state=ok +seg0 producer child exhausted, publishing (state=ok) +seg0 published cond=0 part=0/3 bytes=1048592 cancelled=0 sent=1 +QD part cond=0 from seg0 (says part 0 of 3) 1/3 bytes=1048592 -> collecting +QD part cond=0 from seg1 (says part 1 of 3) 2/3 bytes=1048592 -> collecting +QD part cond=0 from seg2 (says part 2 of 3) 3/3 bytes=1048592 -> complete +QD delivering cond=0 to 3 subscriber(s) +QD pushed cond=0 bytes=1048592 cancelled=0 +seg0 received cond=0 bytes=1048592 cancelled=0 +``` + +Set it in `postgresql.conf` (`gpconfig -c anser.debug -v on`) rather than with +`SET` if you need to see the producer gang: a session-level `SET` does not +reliably reach every gang, and the producers are the half you usually want. + +A consumer that gives up reports what it saw — `read 0 message(s), 0 +unclaimed` means nothing arrived at all, whereas unclaimed messages mean +something arrived for a channel it was not waiting on. + +## Data flow: producer → merge (bitwise union) → consumer + +The parts from all segments are combined into **one** payload by a **bitwise OR +on the coordinator**, and that single combined payload is delivered to every +consumer. This is the core of Anser and worth stating precisely, because it is +*not* a concatenation: + +``` +segment 0 producer: bitset 0000 0001 ┐ +segment 1 producer: bitset 0000 0010 ├─ NOTIFY ─► QD backend (notify hook) +segment N producer: ... ┘ │ + │ fold each part into the + │ running merged bitset: + │ 0000 0001 + │ OR 0000 0010 + ▼ = 0000 0011 (one part) + channel payload = single merged bitset + │ + sideband push ───────────┼───────────────┐ + ▼ ▼ ▼ + consumer seg 0 consumer seg 1 ... consumer seg N + each receives the SAME combined 0000 0011 +``` + +Step by step: + +1. **Produce (per segment, in parallel).** Each segment's producer builds a bloom + filter over its local build keys (`bloom_create` from `total_elems` / + `max_payload` / a `condition_key`-derived seed, all carried in the plan node — + *not* on the wire) and serializes it as one *part*. Because every producer and + the consumer pass the identical parameters, every part has a byte-for-byte + identical size and shape. Publishing is fire-and-forget: the producer sends + its part and carries on without waiting for an acknowledgement. + +2. **Merge (coordinator, once per part).** The coordinator never reconstructs a + filter — it works on raw bytes. The **first** part is stored verbatim; every + later part is folded into the channel's payload with an in-place **bitwise OR** + of the bitset (`AnserBloomFoldPartInPlace` in `anserfilter.c`, called from + `anserdispatch.c`). The payload is therefore always a **single merged bitset**, + the size of one filter — it does **not** grow with the segment count. Folding + happens as parts arrive, so only the last fold is on the critical path. The OR + requires the incoming part to be the same size as the accumulator (guaranteed + by the shared parameters); a mismatch cancels the channel and consumers fail + open. + +3. **Deliver (coordinator → every consumer).** Once every expected part is + folded, the merged payload is judged once (see [Giving up + early](#giving-up-early)) and then pushed to each subscriber. Delivery is per + consumer: a failed write costs that one segment its filter and leaves the + others alone. A consumer that subscribes *after* the channel completed — which + happens routinely, since producers on other segments may finish first — is + served immediately. + +4. **Consume (per segment).** Each consumer rebuilds an empty filter from its own + plan parameters (the same `total_elems` / `max_payload` / seed the producers + used) and loads the received bitset into it (`AnserBloomDeserializePart`), + requiring the received length to match exactly (else it fails open). It does + **not** re-union anything. + +Correctness note: the combined filter is the OR (super-set) of every segment's +build keys, so it can only ever have *false positives*, never false negatives — +a probe row it rejects genuinely cannot join. Anser therefore only changes +performance, never results; any failure along this path degrades to "no filter" +(fail open). + +## Failure handling + +Every failure mode ends in unfiltered execution, never in a wrong answer and +never in an error raised into the query: + +- a producer that is squelched, errors, or produces an oversized part → the + channel never completes (or is cancelled) → consumers hit `anser.timeout_ms` + and run unfiltered; +- a malformed or undecodable part → the channel is cancelled and every consumer + is told so; +- a broken connection → the query is failing anyway, and the interconnect + reports it far more usefully than the filter path could; +- query cancellation → the consumer's wait is a `CHECK_FOR_INTERRUPTS` loop, and + a delivery that arrives after nobody is waiting is discarded by the QE command + loop (`GP_SIDEBAND_MESSAGE` is accepted and ignored there). + +## Tests + +`make installcheck` runs two suites: + +- `anser_test` — the payload protocol (serialize, fold, reject a mismatched + part) and the four give-up decisions, each walked along its boundary: the + estimate either side of 4 bits/key, the payload cap either side of "1 MB plus + a header", fill either side of the 95% limit, and a producer whose filter + saturates. The case tables live in the `.sql` file, so the expected output + records real sizes rather than a bare `t`. +- `anser_runtime_filter` — plan-tree integration end to end: the nodes are + injected, and query results are identical with the feature on and off. diff --git a/gpcontrib/anser/anser_test--1.0.sql b/gpcontrib/anser/anser_test--1.0.sql new file mode 100644 index 00000000000..3c2f8d14cfd --- /dev/null +++ b/gpcontrib/anser/anser_test--1.0.sql @@ -0,0 +1,99 @@ +/* gpcontrib/anser/anser_test--1.0.sql */ + +-- complain if script is sourced in psql, rather than via CREATE EXTENSION +\echo Use "CREATE EXTENSION anser_test" to load this file. \quit + +CREATE FUNCTION anser_test_bloom_roundtrip( + condition_key text, + value int4) +RETURNS bool +AS 'MODULE_PATHNAME' +LANGUAGE C STRICT; + +CREATE FUNCTION anser_test_bloom_fold_inplace() +RETURNS bool +AS 'MODULE_PATHNAME' +LANGUAGE C STRICT; + +CREATE FUNCTION anser_test_bloom_rejects_mismatch() +RETURNS bool +AS 'MODULE_PATHNAME' +LANGUAGE C STRICT; + +CREATE FUNCTION anser_test_node_roundtrip(value int4) +RETURNS bool +AS 'MODULE_PATHNAME' +LANGUAGE C STRICT; + +-- The planner's give-up decision: NULL means no filter is injected. +CREATE FUNCTION anser_test_rf_size( + est_rows float8, + OUT injected bool, + OUT total_elems bigint, + OUT max_payload bigint, + OUT planned_bytes bigint) +RETURNS record +AS 'MODULE_PATHNAME' +LANGUAGE C STRICT; + +-- What AnserBloomCreate builds, or NULL when it declines to build anything. +CREATE FUNCTION anser_test_bloom_shape( + total_elems bigint, + cap bigint, + OUT built bool, + OUT bits bigint, + OUT serialized bigint, + OUT bits_per_key float8) +RETURNS record +AS 'MODULE_PATHNAME' +LANGUAGE C STRICT; + +-- The coordinator's decision about a merged payload, on a synthetic part. +CREATE FUNCTION anser_test_worth_delivering( + bitset_bytes int4, + bytes_set int4, + damage text) +RETURNS bool +AS 'MODULE_PATHNAME' +LANGUAGE C STRICT; + +-- Producer end to end: ":". +CREATE FUNCTION anser_test_producer_decision( + total_elems bigint, + cap bigint, + n_keys int4) +RETURNS text +AS 'MODULE_PATHNAME' +LANGUAGE C STRICT; + +-- The formatted QE -> QD message, verbatim. +CREATE FUNCTION anser_test_wire_format( + kind "char", + payload_type "char", + flags int4, + condition_key text, + body bytea) +RETURNS text +AS 'MODULE_PATHNAME' +LANGUAGE C STRICT; + +-- Format, alter one byte, parse: reports what the reader made of it. +CREATE FUNCTION anser_test_wire_roundtrip( + condition_key text, + body bytea, + tamper text, + payload_type "char") +RETURNS text +AS 'MODULE_PATHNAME' +LANGUAGE C STRICT; + +-- The QD -> QE checksum, so a test can compare two of them. +CREATE FUNCTION anser_test_push_crc( + payload_type "char", + condition_id int4, + flags int4, + condition_key text, + body bytea) +RETURNS int8 +AS 'MODULE_PATHNAME' +LANGUAGE C STRICT; diff --git a/gpcontrib/anser/anser_test.control b/gpcontrib/anser/anser_test.control new file mode 100644 index 00000000000..8a4f941a49b --- /dev/null +++ b/gpcontrib/anser/anser_test.control @@ -0,0 +1,22 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +# anser_test extension +comment = 'test helpers for the anser extension' +default_version = '1.0' +module_pathname = '$libdir/anser' +relocatable = true diff --git a/gpcontrib/anser/expected/anser_multijoin.out b/gpcontrib/anser/expected/anser_multijoin.out new file mode 100644 index 00000000000..0740fef3f15 --- /dev/null +++ b/gpcontrib/anser/expected/anser_multijoin.out @@ -0,0 +1,258 @@ +-- Anser runtime bloom filter: chains of 2..10 joins. +-- +-- Each table joins to the next on its own key pair -- t1.nxt = t2.own, then +-- t2.nxt = t3.own, and so on -- which is what a real chain looks like: no two +-- joins share a key, so no table's distribution can serve more than one of +-- them and every join needs a motion of its own. Joining a whole chain on one +-- column would make the joins co-located and would test the easy case only. +-- +-- This is also the shape the feature has to handle: the build side of the third +-- join is the result of the first two, and the key column for that join comes +-- from t2, so it reaches its base relation through a join rather than straight +-- through a Hash. +-- +-- What these cases assert is not that a filter was injected -- that is the +-- planner's choice and it differs between the two optimizers and with the +-- cluster width -- but that the answer is the same either way. A bloom filter +-- fails asymmetrically: a bit that should be set and is not silently removes +-- joinable rows, so "same rows with the filter on as with it off" is the +-- property worth asserting. Plan shapes are deliberately not asserted; an +-- expected file full of them would break on a three-segment cluster and pass on +-- a one-segment one without saying anything about correctness. +-- +-- Every query below reports its violations, so the expected output is empty +-- results and zeros. A failure names the chain that broke rather than leaving +-- a table of numbers to be compared by eye. +SET enable_nestloop = off; +SET enable_mergejoin = off; +-- A regress test must not inherit the production deadline. anser.timeout_ms +-- defaults to 100 s because delivery is serial and a wide slice needs that +-- long to be served; a test that waits it out has turned a delivery failure +-- into a slow pass. Pinned short here, so a filter that never arrives fails +-- the run instead of hiding in the clock. +SET anser.timeout_ms = 5000; +-- own is what the previous table joins to, nxt is what this table joins to the +-- next one with. They hold the same value, which keeps the chain 1:1 and the +-- answer predictable -- 1000 rows summing to 500500, whatever the join count -- +-- while still being two different columns as far as the planner is concerned. +-- +-- The distribution key is deliberately never the whole story: a table +-- distributed by own is joined on nxt, and the ones distributed by payload are +-- joined on both. Every join therefore redistributes or broadcasts one side, +-- and the producer ends up in a different slice from its consumer. +-- +-- t1 is the smallest and every later table contains it, so the chain returns +-- t1 whatever order the planner picks. The widening sizes give it a reason to +-- hash the joined result rather than the next table, which is the shape that +-- exercises the descent through a join. +CREATE TABLE anser_mj_t1 (own int, nxt int, payload text) DISTRIBUTED BY (own); +CREATE TABLE anser_mj_t2 (own int, nxt int, payload text) DISTRIBUTED BY (payload); +CREATE TABLE anser_mj_t3 (own int, nxt int, payload text) DISTRIBUTED BY (own); +CREATE TABLE anser_mj_t4 (own int, nxt int, payload text) DISTRIBUTED BY (payload); +CREATE TABLE anser_mj_t5 (own int, nxt int, payload text) DISTRIBUTED BY (own); +CREATE TABLE anser_mj_t6 (own int, nxt int, payload text) DISTRIBUTED BY (payload); +CREATE TABLE anser_mj_t7 (own int, nxt int, payload text) DISTRIBUTED BY (own); +CREATE TABLE anser_mj_t8 (own int, nxt int, payload text) DISTRIBUTED BY (payload); +CREATE TABLE anser_mj_t9 (own int, nxt int, payload text) DISTRIBUTED BY (own); +CREATE TABLE anser_mj_t10 (own int, nxt int, payload text) DISTRIBUTED BY (payload); +CREATE TABLE anser_mj_t11 (own int, nxt int, payload text) DISTRIBUTED BY (own); +INSERT INTO anser_mj_t1 SELECT g, g, 't1_' || g FROM generate_series(1, 1000) g; +INSERT INTO anser_mj_t2 SELECT g, g, 't2_' || g FROM generate_series(1, 2000) g; +INSERT INTO anser_mj_t3 SELECT g, g, 't3_' || g FROM generate_series(1, 3000) g; +INSERT INTO anser_mj_t4 SELECT g, g, 't4_' || g FROM generate_series(1, 4000) g; +INSERT INTO anser_mj_t5 SELECT g, g, 't5_' || g FROM generate_series(1, 5000) g; +INSERT INTO anser_mj_t6 SELECT g, g, 't6_' || g FROM generate_series(1, 6000) g; +INSERT INTO anser_mj_t7 SELECT g, g, 't7_' || g FROM generate_series(1, 7000) g; +INSERT INTO anser_mj_t8 SELECT g, g, 't8_' || g FROM generate_series(1, 8000) g; +INSERT INTO anser_mj_t9 SELECT g, g, 't9_' || g FROM generate_series(1, 9000) g; +INSERT INTO anser_mj_t10 SELECT g, g, 't10_' || g FROM generate_series(1, 10000) g; +INSERT INTO anser_mj_t11 SELECT g, g, 't11_' || g FROM generate_series(1, 11000) g; +ANALYZE anser_mj_t1; +ANALYZE anser_mj_t2; +ANALYZE anser_mj_t3; +ANALYZE anser_mj_t4; +ANALYZE anser_mj_t5; +ANALYZE anser_mj_t6; +ANALYZE anser_mj_t7; +ANALYZE anser_mj_t8; +ANALYZE anser_mj_t9; +ANALYZE anser_mj_t10; +ANALYZE anser_mj_t11; +-- Runs one chain twice, with the filter on and off, and reports whether the two +-- agree. The checksum is what makes the comparison mean something: equal row +-- counts would also come out of two different sets of rows, and a filter that +-- wrongly rejects is exactly the failure that changes which rows come back. +CREATE FUNCTION anser_mj_check(njoins int) +RETURNS TABLE (joins int, nrows bigint, own_sum bigint, agrees boolean) +LANGUAGE plpgsql AS $$ +DECLARE + sql text; + i int; + on_rows bigint; + on_sum bigint; + off_rows bigint; + off_sum bigint; +BEGIN + sql := 'SELECT count(*), coalesce(sum(anser_mj_t1.own), 0) FROM anser_mj_t1'; + FOR i IN 2 .. njoins + 1 LOOP + sql := sql || format(' JOIN anser_mj_t%s ON anser_mj_t%s.nxt = anser_mj_t%s.own', + i, i - 1, i); + END LOOP; + PERFORM set_config('anser.runtime_filter', 'on', false); + EXECUTE sql INTO on_rows, on_sum; + PERFORM set_config('anser.runtime_filter', 'off', false); + EXECUTE sql INTO off_rows, off_sum; + RETURN QUERY SELECT njoins, on_rows, on_sum, + (on_rows = off_rows AND on_sum = off_sum); +END; +$$; +-- 2 through 10 joins, Postgres planner. Expected: no rows. +SET optimizer = off; +SELECT joins, nrows, own_sum, agrees + FROM generate_series(2, 10) g, anser_mj_check(g) + WHERE NOT agrees OR nrows <> 1000 OR own_sum <> 500500 + ORDER BY joins; + joins | nrows | own_sum | agrees +-------+-------+---------+-------- +(0 rows) + +-- The same under ORCA, which picks different join orders and slice widths. +SET optimizer = on; +SELECT joins, nrows, own_sum, agrees + FROM generate_series(2, 10) g, anser_mj_check(g) + WHERE NOT agrees OR nrows <> 1000 OR own_sum <> 500500 + ORDER BY joins; + joins | nrows | own_sum | agrees +-------+-------+---------+-------- +(0 rows) + +SET optimizer = off; +-- A chain whose middle join is an outer join, and whose next join reads the +-- nullable side's key. The joined result there holds NULLs the base relation +-- does not, and a NULL must not be taken for a key the filter is missing. +SET anser.runtime_filter = on; +CREATE TEMP TABLE anser_mj_lj_on AS + SELECT a.own AS aown, c.own AS cown, d.own AS down + FROM anser_mj_t1 a + JOIN anser_mj_t2 b ON a.nxt = b.own + LEFT JOIN anser_mj_t3 c ON b.nxt = c.own + JOIN anser_mj_t4 d ON c.nxt = d.own + DISTRIBUTED BY (aown); +SET anser.runtime_filter = off; +CREATE TEMP TABLE anser_mj_lj_off AS + SELECT a.own AS aown, c.own AS cown, d.own AS down + FROM anser_mj_t1 a + JOIN anser_mj_t2 b ON a.nxt = b.own + LEFT JOIN anser_mj_t3 c ON b.nxt = c.own + JOIN anser_mj_t4 d ON c.nxt = d.own + DISTRIBUTED BY (aown); +SELECT count(*) AS lj_rows FROM anser_mj_lj_on; + lj_rows +--------- + 1000 +(1 row) + +SELECT count(*) AS lj_only_on + FROM (SELECT * FROM anser_mj_lj_on EXCEPT ALL SELECT * FROM anser_mj_lj_off) d; + lj_only_on +------------ + 0 +(1 row) + +SELECT count(*) AS lj_only_off + FROM (SELECT * FROM anser_mj_lj_off EXCEPT ALL SELECT * FROM anser_mj_lj_on) d; + lj_only_off +------------- + 0 +(1 row) + +-- A star rather than a chain: every join reads t1's key, so one table's +-- distribution serves them all and the build sides are base scans throughout. +-- This is the shape that worked before the descent went through joins, and it +-- has to keep working. +SET anser.runtime_filter = on; +CREATE TEMP TABLE anser_mj_star_on AS + SELECT a.own AS aown, e.payload AS epayload + FROM anser_mj_t1 a + JOIN anser_mj_t2 b ON a.nxt = b.own + JOIN anser_mj_t3 c ON a.nxt = c.own + JOIN anser_mj_t4 d ON a.nxt = d.own + JOIN anser_mj_t5 e ON a.nxt = e.own + DISTRIBUTED BY (aown); +SET anser.runtime_filter = off; +CREATE TEMP TABLE anser_mj_star_off AS + SELECT a.own AS aown, e.payload AS epayload + FROM anser_mj_t1 a + JOIN anser_mj_t2 b ON a.nxt = b.own + JOIN anser_mj_t3 c ON a.nxt = c.own + JOIN anser_mj_t4 d ON a.nxt = d.own + JOIN anser_mj_t5 e ON a.nxt = e.own + DISTRIBUTED BY (aown); +SELECT count(*) AS star_rows FROM anser_mj_star_on; + star_rows +----------- + 1000 +(1 row) + +SELECT count(*) AS star_only_on + FROM (SELECT * FROM anser_mj_star_on EXCEPT ALL SELECT * FROM anser_mj_star_off) d; + star_only_on +-------------- + 0 +(1 row) + +SELECT count(*) AS star_only_off + FROM (SELECT * FROM anser_mj_star_off EXCEPT ALL SELECT * FROM anser_mj_star_on) d; + star_only_off +--------------- + 0 +(1 row) + +-- A chain that returns nothing. A filter that wrongly accepts is only slow, +-- never wrong, so this is the one direction an empty result can still check. +SET anser.runtime_filter = on; +SELECT count(*) AS empty_rows + FROM anser_mj_t1 a + JOIN anser_mj_t2 b ON a.nxt = b.own + JOIN anser_mj_t3 c ON b.nxt = c.own + WHERE a.own < 0; + empty_rows +------------ + 0 +(1 row) + +-- The chains above assert that the filter changes no answer. That also holds +-- when the filter never arrives and every consumer fails open, which is how a +-- broken exchange passes a green test. This asserts the other half: a chain +-- whose third table is three times the size of the first has rows to prune, so +-- a filter that arrived removes some. +CREATE FUNCTION anser_mj_pruned(q text) RETURNS bigint +LANGUAGE plpgsql AS $$ +DECLARE + line text; + removed bigint := 0; +BEGIN + FOR line IN EXECUTE 'EXPLAIN (ANALYZE, TIMING OFF, COSTS OFF) ' || q LOOP + IF line ~ 'Rows Removed by (Bloom Filter|Pushdown Runtime Filter): ' THEN + removed := removed + coalesce(substring(line from ': *([0-9]+)')::bigint, 0); + END IF; + END LOOP; + RETURN removed; +END; +$$; +SET anser.runtime_filter = on; +SELECT 'the chain pruned nothing: no filter arrived, or none was injected' AS problem + WHERE anser_mj_pruned('SELECT count(*) FROM anser_mj_t1 a JOIN anser_mj_t2 b ON a.nxt = b.own JOIN anser_mj_t3 c ON b.nxt = c.own') = 0; + problem +--------- +(0 rows) + +DROP FUNCTION anser_mj_pruned(text); +DROP FUNCTION anser_mj_check(int); +DROP TABLE anser_mj_t1, anser_mj_t2, anser_mj_t3, anser_mj_t4, anser_mj_t5, + anser_mj_t6, anser_mj_t7, anser_mj_t8, anser_mj_t9, anser_mj_t10, + anser_mj_t11; +RESET anser.runtime_filter; +RESET optimizer; +RESET enable_nestloop; +RESET enable_mergejoin; diff --git a/gpcontrib/anser/expected/anser_runtime_filter.out b/gpcontrib/anser/expected/anser_runtime_filter.out new file mode 100644 index 00000000000..cbc5fbceee8 --- /dev/null +++ b/gpcontrib/anser/expected/anser_runtime_filter.out @@ -0,0 +1,399 @@ +-- Anser runtime bloom filter: plan-tree integration (PR4). +-- +-- Requires the cluster to run with anser.enable=on so the gather/send +-- services are live. On a multi-segment cluster the build side redistributes, +-- exercising the Motion-directly-under-CustomScan case; on a single segment it +-- degrades to the leaf case. Either way the feature must (a) inject the +-- producer/consumer nodes and (b) never change query results. +-- Deterministic plan shape: force a hash join. +SET enable_nestloop = off; +SET enable_mergejoin = off; +-- A regress test must not inherit the production deadline. anser.timeout_ms +-- defaults to 100 s because delivery is serial and a wide slice needs that +-- long to be served; a test that waits it out has turned a delivery failure +-- into a slow pass. Pinned short here, so a filter that never arrives fails +-- the run instead of hiding in the clock. +SET anser.timeout_ms = 5000; +-- build is the smaller (hashed / preserved) side, distributed so the join key +-- must be redistributed; probe is the larger side distributed by the join key. +CREATE TABLE anser_rf_build (id int, name text) DISTRIBUTED BY (name); +CREATE TABLE anser_rf_probe (id int, payload text) DISTRIBUTED BY (id); +INSERT INTO anser_rf_build SELECT g, 'b' || g FROM generate_series(1, 200) g; +INSERT INTO anser_rf_probe SELECT g, 'p' || g FROM generate_series(1, 2000) g; +ANALYZE anser_rf_build; +ANALYZE anser_rf_probe; +-- Use the Postgres planner for a deterministic plan shape. +SET optimizer = off; +-- With the filter on the plan carries an "Anser Bloom Producer" under the Hash +-- and an "Anser Bloom Consumer" above the probe scan (with the planned bloom +-- size); with it off neither node appears. (COSTS OFF keeps the output stable.) +SET anser.runtime_filter = on; +EXPLAIN (COSTS OFF) +SELECT b.name, p.payload FROM anser_rf_build b LEFT JOIN anser_rf_probe p ON b.id = p.id; + QUERY PLAN +------------------------------------------------------------------ + Gather Motion 3:1 (slice1; segments: 3) + -> Hash Right Join + Hash Cond: (p.id = b.id) + -> Custom Scan (Anser Bloom Consumer) + Bloom Filter Size: 1048576 bytes + -> Seq Scan on anser_rf_probe p + -> Hash + -> Redistribute Motion 3:3 (slice2; segments: 3) + Hash Key: b.id + -> Custom Scan (Anser Bloom Producer) + Bloom Filter Size: 1048576 bytes + Bloom Filter Stats: memory=1024kB + -> Seq Scan on anser_rf_build b + Optimizer: Postgres query optimizer +(14 rows) + +SET anser.runtime_filter = off; +EXPLAIN (COSTS OFF) +SELECT b.name, p.payload FROM anser_rf_build b LEFT JOIN anser_rf_probe p ON b.id = p.id; + QUERY PLAN +------------------------------------------------------------------ + Gather Motion 3:1 (slice1; segments: 3) + -> Hash Right Join + Hash Cond: (p.id = b.id) + -> Seq Scan on anser_rf_probe p + -> Hash + -> Redistribute Motion 3:3 (slice2; segments: 3) + Hash Key: b.id + -> Seq Scan on anser_rf_build b + Optimizer: Postgres query optimizer +(9 rows) + +-- Correctness: identical results with the filter on vs off (Postgres planner). +SET anser.runtime_filter = on; +CREATE TEMP TABLE anser_rf_r_on AS + SELECT b.name, p.payload FROM anser_rf_build b LEFT JOIN anser_rf_probe p ON b.id = p.id + DISTRIBUTED BY (name); +SET anser.runtime_filter = off; +CREATE TEMP TABLE anser_rf_r_off AS + SELECT b.name, p.payload FROM anser_rf_build b LEFT JOIN anser_rf_probe p ON b.id = p.id + DISTRIBUTED BY (name); +SELECT count(*) AS rows_on FROM anser_rf_r_on; + rows_on +--------- + 200 +(1 row) + +SELECT count(*) AS rows_off FROM anser_rf_r_off; + rows_off +---------- + 200 +(1 row) + +SELECT count(*) AS only_on + FROM (SELECT * FROM anser_rf_r_on EXCEPT ALL SELECT * FROM anser_rf_r_off) d; + only_on +--------- + 0 +(1 row) + +SELECT count(*) AS only_off + FROM (SELECT * FROM anser_rf_r_off EXCEPT ALL SELECT * FROM anser_rf_r_on) d; + only_off +---------- + 0 +(1 row) + +-- Correctness must also hold under ORCA (whether or not it injects the nodes). +SET optimizer = on; +SET anser.runtime_filter = on; +CREATE TEMP TABLE anser_rf_r_orca AS + SELECT b.name, p.payload FROM anser_rf_build b LEFT JOIN anser_rf_probe p ON b.id = p.id + DISTRIBUTED BY (name); +SELECT count(*) AS only_orca + FROM (SELECT * FROM anser_rf_r_orca EXCEPT ALL SELECT * FROM anser_rf_r_off) d; + only_orca +----------- + 0 +(1 row) + +SELECT count(*) AS only_off_orca + FROM (SELECT * FROM anser_rf_r_off EXCEPT ALL SELECT * FROM anser_rf_r_orca) d; + only_off_orca +--------------- + 0 +(1 row) + +-- Pushdown mix: with gp_enable_runtime_filter_pushdown on, the consumer hands +-- the unioned filter to the probe SeqScan as an SK_BLOOM_FILTER scan key and +-- the scan (or the table AM) does the pruning. Results must still match the +-- filter-off run. +SET optimizer = off; +SET gp_enable_runtime_filter_pushdown = on; +SET anser.runtime_filter = on; +CREATE TEMP TABLE anser_rf_r_push AS + SELECT b.name, p.payload FROM anser_rf_build b LEFT JOIN anser_rf_probe p ON b.id = p.id + DISTRIBUTED BY (name); +SELECT count(*) AS only_push + FROM (SELECT * FROM anser_rf_r_push EXCEPT ALL SELECT * FROM anser_rf_r_off) d; + only_push +----------- + 0 +(1 row) + +SELECT count(*) AS only_off_push + FROM (SELECT * FROM anser_rf_r_off EXCEPT ALL SELECT * FROM anser_rf_r_push) d; + only_off_push +--------------- + 0 +(1 row) + +RESET gp_enable_runtime_filter_pushdown; +-- Everything above checks that the nodes were injected and that the results did +-- not change. Both of those also hold when the filter never arrives and every +-- consumer fails open -- which is how a broken exchange passes a green test. +-- This is the assertion that the filter was used: the probe side holds 2000 +-- rows of which 200 join, so a filter that arrived removes the rest. Only the +-- Postgres planner is asserted, because its plan shape here is fixed; ORCA +-- chooses and the count would not be a stable thing to require. +CREATE FUNCTION anser_rf_pruned(q text) RETURNS bigint +LANGUAGE plpgsql AS $$ +DECLARE + line text; + removed bigint := 0; +BEGIN + FOR line IN EXECUTE 'EXPLAIN (ANALYZE, TIMING OFF, COSTS OFF) ' || q LOOP + IF line ~ 'Rows Removed by (Bloom Filter|Pushdown Runtime Filter): ' THEN + removed := removed + coalesce(substring(line from ': *([0-9]+)')::bigint, 0); + END IF; + END LOOP; + RETURN removed; +END; +$$; +SET optimizer = off; +SET anser.runtime_filter = on; +SELECT 'the filter pruned nothing: it never arrived, or it arrived empty' AS problem + WHERE anser_rf_pruned('SELECT b.name, p.payload FROM anser_rf_build b LEFT JOIN anser_rf_probe p ON b.id = p.id') = 0; + problem +--------- +(0 rows) + +DROP FUNCTION anser_rf_pruned(text); +DROP TABLE anser_rf_r_push; +DROP TABLE anser_rf_r_on, anser_rf_r_off, anser_rf_r_orca; +DROP TABLE anser_rf_build, anser_rf_probe; +-- Datatype guard: producer and consumer hash the raw Datum bytes, so injection +-- is restricted to keys where SQL equality is bitwise Datum equality. A +-- cross-type join (float4 vs float8 Datums for equal values differ) must not +-- be injected; results must match the filter-off run either way. +CREATE TABLE anser_rf_build_f (id float8, name text) DISTRIBUTED BY (name); +CREATE TABLE anser_rf_probe_f (id float4, payload text) DISTRIBUTED BY (id); +INSERT INTO anser_rf_build_f SELECT g, 'b' || g FROM generate_series(1, 200) g; +INSERT INTO anser_rf_probe_f SELECT g, 'p' || g FROM generate_series(1, 2000) g; +ANALYZE anser_rf_build_f; +ANALYZE anser_rf_probe_f; +SET anser.runtime_filter = on; +CREATE TEMP TABLE anser_rf_f_on AS + SELECT b.name, p.payload FROM anser_rf_build_f b JOIN anser_rf_probe_f p ON b.id = p.id + DISTRIBUTED BY (name); +SET anser.runtime_filter = off; +CREATE TEMP TABLE anser_rf_f_off AS + SELECT b.name, p.payload FROM anser_rf_build_f b JOIN anser_rf_probe_f p ON b.id = p.id + DISTRIBUTED BY (name); +SELECT count(*) AS rows_on FROM anser_rf_f_on; + rows_on +--------- + 200 +(1 row) + +SELECT count(*) AS only_on + FROM (SELECT * FROM anser_rf_f_on EXCEPT ALL SELECT * FROM anser_rf_f_off) d; + only_on +--------- + 0 +(1 row) + +SELECT count(*) AS only_off + FROM (SELECT * FROM anser_rf_f_off EXCEPT ALL SELECT * FROM anser_rf_f_on) d; + only_off +---------- + 0 +(1 row) + +DROP TABLE anser_rf_f_on, anser_rf_f_off, anser_rf_build_f, anser_rf_probe_f; +-- Same-typed float keys are also excluded: -0.0 and 0.0 compare equal in SQL +-- but are not bitwise equal, so hashing raw Datums could prune a joinable row. +CREATE TABLE anser_rf_build_z (id float8, name text) DISTRIBUTED BY (name); +CREATE TABLE anser_rf_probe_z (id float8, payload text) DISTRIBUTED BY (id); +INSERT INTO anser_rf_build_z VALUES (1, 'b1'), (-0.0, 'bz'); +INSERT INTO anser_rf_probe_z VALUES (1, 'p1'), (0.0, 'pz'), (2, 'p2'); +ANALYZE anser_rf_build_z; +ANALYZE anser_rf_probe_z; +SET anser.runtime_filter = on; +CREATE TEMP TABLE anser_rf_z_on AS + SELECT b.name, p.payload FROM anser_rf_build_z b JOIN anser_rf_probe_z p ON b.id = p.id + DISTRIBUTED BY (name); +SET anser.runtime_filter = off; +CREATE TEMP TABLE anser_rf_z_off AS + SELECT b.name, p.payload FROM anser_rf_build_z b JOIN anser_rf_probe_z p ON b.id = p.id + DISTRIBUTED BY (name); +SELECT count(*) AS rows_on FROM anser_rf_z_on; + rows_on +--------- + 2 +(1 row) + +SELECT count(*) AS only_on + FROM (SELECT * FROM anser_rf_z_on EXCEPT ALL SELECT * FROM anser_rf_z_off) d; + only_on +--------- + 0 +(1 row) + +SELECT count(*) AS only_off + FROM (SELECT * FROM anser_rf_z_off EXCEPT ALL SELECT * FROM anser_rf_z_on) d; + only_off +---------- + 0 +(1 row) + +DROP TABLE anser_rf_z_on, anser_rf_z_off, anser_rf_build_z, anser_rf_probe_z; +-- --------------------------------------------------------------------------- +-- Parallel execution. +-- +-- A parallel slice runs numsegments * parallel_workers processes and every one +-- of them publishes its own part, so the coordinator must wait for that many +-- parts rather than for one per segment. Undercounting completes the channel +-- early and delivers a filter missing build keys; a bloom filter short of keys +-- gives false negatives, which silently drops joinable rows. So the assertion +-- that matters is not the plan shape but that results are *identical* with the +-- filter on and off -- at more than one worker count, and under both planners, +-- which disagree here: for one query the Postgres planner may give a serial +-- build slice where GPORCA gives a parallel one. +-- +-- The join key is the distribution key on both sides, so no Motion is needed +-- to co-locate the join. That is the only shape either planner parallelises: +-- a redistributed or broadcast build side is fine for GPORCA but the Postgres +-- planner marks Motion paths parallel-unsafe. +-- --------------------------------------------------------------------------- +CREATE TABLE anser_rf_par_build (id int, v text) DISTRIBUTED BY (id); +CREATE TABLE anser_rf_par_probe (id int, pad text) DISTRIBUTED BY (id); +INSERT INTO anser_rf_par_build SELECT g, 'b' || g FROM generate_series(1, 500) g; +INSERT INTO anser_rf_par_probe SELECT g, 'p' || g FROM generate_series(1, 20000) g; +ANALYZE anser_rf_par_build; +ANALYZE anser_rf_par_probe; +-- Force a parallel plan on tables this small, the way PostgreSQL's own tests +-- do; without this the scans are far too cheap to be worth splitting. +SET enable_parallel = on; +SET min_parallel_table_scan_size = 0; +SET parallel_setup_cost = 0; +SET parallel_tuple_cost = 0; +-- Postgres planner, serial baseline +SET optimizer = off; +SET max_parallel_workers_per_gather = 0; +SET anser.runtime_filter = on; +CREATE TEMP TABLE anser_rf_par_on AS + SELECT b.v, p.pad FROM anser_rf_par_probe p + JOIN anser_rf_par_build b ON p.id = b.id + DISTRIBUTED BY (v); +SET anser.runtime_filter = off; +CREATE TEMP TABLE anser_rf_par_off AS + SELECT b.v, p.pad FROM anser_rf_par_probe p + JOIN anser_rf_par_build b ON p.id = b.id + DISTRIBUTED BY (v); +SELECT (SELECT count(*) FROM anser_rf_par_on) AS rows_on, + (SELECT count(*) FROM (SELECT * FROM anser_rf_par_on + EXCEPT ALL + SELECT * FROM anser_rf_par_off) d) AS only_on, + (SELECT count(*) FROM (SELECT * FROM anser_rf_par_off + EXCEPT ALL + SELECT * FROM anser_rf_par_on) d) AS only_off; + rows_on | only_on | only_off +---------+---------+---------- + 500 | 0 | 0 +(1 row) + +DROP TABLE anser_rf_par_on, anser_rf_par_off; +-- Postgres planner, parallel +SET optimizer = off; +SET max_parallel_workers_per_gather = 4; +SET anser.runtime_filter = on; +CREATE TEMP TABLE anser_rf_par_on AS + SELECT b.v, p.pad FROM anser_rf_par_probe p + JOIN anser_rf_par_build b ON p.id = b.id + DISTRIBUTED BY (v); +SET anser.runtime_filter = off; +CREATE TEMP TABLE anser_rf_par_off AS + SELECT b.v, p.pad FROM anser_rf_par_probe p + JOIN anser_rf_par_build b ON p.id = b.id + DISTRIBUTED BY (v); +SELECT (SELECT count(*) FROM anser_rf_par_on) AS rows_on, + (SELECT count(*) FROM (SELECT * FROM anser_rf_par_on + EXCEPT ALL + SELECT * FROM anser_rf_par_off) d) AS only_on, + (SELECT count(*) FROM (SELECT * FROM anser_rf_par_off + EXCEPT ALL + SELECT * FROM anser_rf_par_on) d) AS only_off; + rows_on | only_on | only_off +---------+---------+---------- + 500 | 0 | 0 +(1 row) + +DROP TABLE anser_rf_par_on, anser_rf_par_off; +-- GPORCA, serial baseline +SET optimizer = on; +SET max_parallel_workers_per_gather = 0; +SET anser.runtime_filter = on; +CREATE TEMP TABLE anser_rf_par_on AS + SELECT b.v, p.pad FROM anser_rf_par_probe p + JOIN anser_rf_par_build b ON p.id = b.id + DISTRIBUTED BY (v); +SET anser.runtime_filter = off; +CREATE TEMP TABLE anser_rf_par_off AS + SELECT b.v, p.pad FROM anser_rf_par_probe p + JOIN anser_rf_par_build b ON p.id = b.id + DISTRIBUTED BY (v); +SELECT (SELECT count(*) FROM anser_rf_par_on) AS rows_on, + (SELECT count(*) FROM (SELECT * FROM anser_rf_par_on + EXCEPT ALL + SELECT * FROM anser_rf_par_off) d) AS only_on, + (SELECT count(*) FROM (SELECT * FROM anser_rf_par_off + EXCEPT ALL + SELECT * FROM anser_rf_par_on) d) AS only_off; + rows_on | only_on | only_off +---------+---------+---------- + 500 | 0 | 0 +(1 row) + +DROP TABLE anser_rf_par_on, anser_rf_par_off; +-- GPORCA, parallel +SET optimizer = on; +SET max_parallel_workers_per_gather = 4; +SET anser.runtime_filter = on; +CREATE TEMP TABLE anser_rf_par_on AS + SELECT b.v, p.pad FROM anser_rf_par_probe p + JOIN anser_rf_par_build b ON p.id = b.id + DISTRIBUTED BY (v); +SET anser.runtime_filter = off; +CREATE TEMP TABLE anser_rf_par_off AS + SELECT b.v, p.pad FROM anser_rf_par_probe p + JOIN anser_rf_par_build b ON p.id = b.id + DISTRIBUTED BY (v); +SELECT (SELECT count(*) FROM anser_rf_par_on) AS rows_on, + (SELECT count(*) FROM (SELECT * FROM anser_rf_par_on + EXCEPT ALL + SELECT * FROM anser_rf_par_off) d) AS only_on, + (SELECT count(*) FROM (SELECT * FROM anser_rf_par_off + EXCEPT ALL + SELECT * FROM anser_rf_par_on) d) AS only_off; + rows_on | only_on | only_off +---------+---------+---------- + 500 | 0 | 0 +(1 row) + +DROP TABLE anser_rf_par_on, anser_rf_par_off; +DROP TABLE anser_rf_par_build, anser_rf_par_probe; +RESET enable_parallel; +RESET min_parallel_table_scan_size; +RESET parallel_setup_cost; +RESET parallel_tuple_cost; +RESET max_parallel_workers_per_gather; +RESET anser.runtime_filter; +RESET optimizer; +RESET enable_nestloop; +RESET enable_mergejoin; diff --git a/gpcontrib/anser/expected/anser_test.out b/gpcontrib/anser/expected/anser_test.out new file mode 100644 index 00000000000..9e663672a02 --- /dev/null +++ b/gpcontrib/anser/expected/anser_test.out @@ -0,0 +1,286 @@ +CREATE EXTENSION anser_test; +-- Bloom payload protocol: serialize one part and read it back. +SELECT anser_test_bloom_roundtrip('bf_roundtrip', 42); + anser_test_bloom_roundtrip +---------------------------- + t +(1 row) + +-- In-place fold: same-size union mutates the accumulator; a mismatched size is +-- rejected. This is the coordinator's only combine path (the first part is +-- kept verbatim, every later part folds into it). +SELECT anser_test_bloom_fold_inplace() AS fold_inplace_ok; + fold_inplace_ok +----------------- + t +(1 row) + +-- Safety regression: the consumer rebuilds the filter from its own parameters +-- and requires the received bitset to be exactly the expected size (and the +-- header magic to match); truncated/oversized/corrupt parts are rejected. +SELECT anser_test_bloom_rejects_mismatch() AS reject_mismatch; + reject_mismatch +----------------- + t +(1 row) + +-- Producer and consumer driven end to end in one backend: publish a part, let +-- the coordinator side merge it, then receive and query the filter. +SELECT anser_test_node_roundtrip(168); + anser_test_node_roundtrip +--------------------------- + t +(1 row) + +-- --------------------------------------------------------------------------- +-- Sizing, and the decision not to bother. +-- +-- Anser can decline to build a filter at four points, and these cases walk the +-- boundary of each. Declining is always safe: it costs the query its filter, +-- never its correctness. The payload cap is pinned here because every size +-- below follows from it. +-- --------------------------------------------------------------------------- +SET anser.max_info_size = 68157440; +-- 1. The planner's gate -- the only one that costs nothing, since nothing is +-- injected and no consumer is left waiting. A 64 MB bitset is 536870912 bits, +-- so 134217728 estimated keys is exactly 4 bits/key and one more key is not. +-- A zero or negative estimate is treated as one key, not as "no filter". +SELECT t.est_rows, s.injected, s.total_elems, s.planned_bytes +FROM (VALUES (0), (-5), (1), (1000), (1000000), (10000000), + (134217728), (134217729), (150000000), (1000000000)) + AS t(est_rows), + LATERAL anser_test_rf_size(t.est_rows) AS s; + est_rows | injected | total_elems | planned_bytes +------------+----------+-------------+--------------- + 0 | t | 1 | 1048576 + -5 | t | 1 | 1048576 + 1 | t | 1 | 1048576 + 1000 | t | 1000 | 1048576 + 1000000 | t | 1000000 | 1048576 + 10000000 | t | 10000000 | 16777216 + 134217728 | t | 134217728 | 67108864 + 134217729 | f | | + 150000000 | f | | + 1000000000 | f | | +(10 rows) + +-- 2. Filter construction. bloom_create floors every bitset at 1 MB and that +-- floor overrides the work_mem cap, so a cap below "1 MB + header" can only +-- yield a filter too large to send -- which is why a flat 1 MB cap is refused +-- while 1048592 (the floor plus exactly one header) is accepted and serializes +-- to precisely the cap. fits_cap is the invariant that must never be false. +SELECT t.what, t.elems, t.cap, s.built, s.bits, s.serialized, + round(s.bits_per_key::numeric, 3) AS bits_per_key, + (NOT s.built OR s.serialized <= t.cap) AS fits_cap +FROM (VALUES + ('no keys', 0::bigint, 1048640::bigint), + ('negative keys', -1, 1048640), + ('cap equals the header', 32, 16), + ('cap one byte over the header', 32, 17), + ('cap is 1 MB, no header room', 32, 1048576), + ('cap is 1 MB plus a header', 32, 1048592), + ('cap one byte short of that', 32, 1048591), + ('a single key', 1, 1048640), + ('exactly 4 bits/key', 2097152, 1048640), + ('one key too many', 2097153, 1048640), + ('exactly 4 bits/key at 2 MB', 4194304, 2097216), + ('one key too many at 2 MB', 4194305, 2097216), + ('a billion keys', 1000000000, 68157440)) + AS t(what, elems, cap), + LATERAL anser_test_bloom_shape(t.elems, t.cap) AS s; + what | elems | cap | built | bits | serialized | bits_per_key | fits_cap +------------------------------+------------+----------+-------+----------+------------+--------------+---------- + no keys | 0 | 1048640 | f | | | | t + negative keys | -1 | 1048640 | f | | | | t + cap equals the header | 32 | 16 | f | | | | t + cap one byte over the header | 32 | 17 | f | | | | t + cap is 1 MB, no header room | 32 | 1048576 | f | | | | t + cap is 1 MB plus a header | 32 | 1048592 | t | 8388608 | 1048592 | 262144.000 | t + cap one byte short of that | 32 | 1048591 | f | | | | t + a single key | 1 | 1048640 | t | 8388608 | 1048592 | 8388608.000 | t + exactly 4 bits/key | 2097152 | 1048640 | t | 8388608 | 1048592 | 4.000 | t + one key too many | 2097153 | 1048640 | f | | | | t + exactly 4 bits/key at 2 MB | 4194304 | 2097216 | t | 16777216 | 2097168 | 4.000 | t + one key too many at 2 MB | 4194305 | 2097216 | f | | | | t + a billion keys | 1000000000 | 68157440 | f | | | | t +(13 rows) + +-- 3. The coordinator's gate on a merged payload. The fill is fixed exactly by +-- setting whole bytes rather than by inserting keys, which would be both slow +-- and only statistically precise. The rule is "reject above 95%", so a payload +-- sitting exactly on the limit is still delivered. Malformed framing is not +-- worth delivering either, since it cannot be a filter at all. +SELECT t.what, t.bitset_bytes, t.bytes_set, t.damage, + anser_test_worth_delivering(t.bitset_bytes, t.bytes_set, t.damage) + AS worth_delivering +FROM (VALUES + ('empty', 1000, 0, ''), + ('half full', 1000, 500, ''), + ('just under the limit', 1000, 949, ''), + ('exactly on the limit', 1000, 950, ''), + ('just over the limit', 1000, 951, ''), + ('completely full', 1000, 1000, ''), + ('no bitset at all', 0, 0, ''), + ('corrupt magic', 1000, 0, 'magic'), + ('corrupt version', 1000, 0, 'version'), + ('zero part count', 1000, 0, 'parts'), + ('null payload', 1000, 0, 'null')) + AS t(what, bitset_bytes, bytes_set, damage); + what | bitset_bytes | bytes_set | damage | worth_delivering +----------------------+--------------+-----------+---------+------------------ + empty | 1000 | 0 | | t + half full | 1000 | 500 | | t + just under the limit | 1000 | 949 | | t + exactly on the limit | 1000 | 950 | | t + just over the limit | 1000 | 951 | | f + completely full | 1000 | 1000 | | f + no bitset at all | 0 | 0 | | f + corrupt magic | 1000 | 0 | magic | f + corrupt version | 1000 | 0 | version | f + zero part count | 1000 | 0 | parts | f + null payload | 1000 | 0 | null | f +(11 rows) + +-- 4. The producer driven end to end on the coordinator-local path: build, +-- insert, publish, consume. "no-filter:cancelled" is the case that matters +-- most -- construction declined, so the cancel goes out before the build side +-- is scanned and no consumer waits for its timeout. The last row is the one +-- the planner cannot predict: a filter sized for 32 keys that receives three +-- million, which saturates and so publishes a cancel instead of a payload. +SELECT t.what, + anser_test_producer_decision(t.elems, t.cap, t.n_keys) AS decision +FROM (VALUES + ('sparse filter', 32::bigint, 1048640::bigint, 100), + ('cap with no header room', 32, 1048576, 100), + ('no keys declared', 0, 1048640, 0), + ('one key past the floor', 2097153, 1048640, 0), + ('exactly 4 bits/key', 2097152, 1048640, 100000), + ('estimate of 32, three million inserted', 32, 1048640, 3000000)) + AS t(what, elems, cap, n_keys); + what | decision +----------------------------------------+--------------------- + sparse filter | built:delivered + cap with no header room | no-filter:cancelled + no keys declared | no-filter:cancelled + one key past the floor | no-filter:cancelled + exactly 4 bits/key | built:delivered + estimate of 32, three million inserted | built:cancelled +(6 rows) + +-- --------------------------------------------------------------------------- +-- The wire protocol. +-- +-- Not exhaustive; a fuzzer would be the right tool for that. These cover the +-- properties that are cheap to check and expensive to lose: a fixed-width +-- header with the fields where they are documented, a payload that cannot be +-- mistaken for framing, a length cross-check that rejects a message which does +-- not add up, and a checksum that rejects one altered byte anywhere it covers. +-- --------------------------------------------------------------------------- +-- The bytes, exactly. Session/command/condition are 42/7/3 and part is 1 of 3, +-- so these are golden values: any change to the layout, the field widths or +-- what the checksum covers shows up here as a diff. Note the third row -- a +-- cancelled message carries no body even though one was passed, and its +-- checksum must be computed over what is actually sent. +-- +-- That the message comes back as text is itself the check that it is NUL-free, +-- which a NOTIFY payload has to be: the fourth row's body is 00 ff 00. +SELECT t.what, length(m.msg) AS len, m.msg +FROM (VALUES + ('part with a 3-byte body', 'P', 'B', 0, '\x616263'::bytea), + ('subscription, no body', 'S', '-', 0, '\x'::bytea), + ('cancelled: body suppressed', 'P', 'B', 1, '\x616263'::bytea), + ('body of NUL and 0xff bytes', 'P', 'B', 0, '\x00ff00'::bytea)) + AS t(what, kind, ptype, flags, body), + LATERAL (SELECT anser_test_wire_format(t.kind::"char", t.ptype::"char", + t.flags, 'anser_rf_3', t.body) + AS msg) m; + what | len | msg +----------------------------+-----+--------------------------------------------------------------------------------------------------------------- + part with a 3-byte body | 109 | anser3 P B 0000000042 0000000007 0000000003 0000000001 0000000003 0000 0010 0000000004 f7b3c0d5anser_rf_3YWJj + subscription, no body | 105 | anser3 S - 0000000042 0000000007 0000000003 0000000001 0000000003 0000 0010 0000000000 ecd98662anser_rf_3 + cancelled: body suppressed | 105 | anser3 P B 0000000042 0000000007 0000000003 0000000001 0000000003 0001 0010 0000000000 e88148e9anser_rf_3 + body of NUL and 0xff bytes | 109 | anser3 P B 0000000042 0000000007 0000000003 0000000001 0000000003 0000 0010 0000000004 027f789banser_rf_3AP8A +(4 rows) + +-- Alter exactly one byte (or one length) and see what the reader does. +-- +-- The split matters: "malformed" is rejected by framing, before a byte is +-- allocated or a channel created, while "checksum-mismatch" got past framing +-- and was caught by the CRC. Corrupting the kind byte is the interesting one +-- -- the message stays perfectly well-formed, and only the checksum notices, +-- which is why the checksum covers the header and not just the payload. +SELECT t.tamper, + anser_test_wire_roundtrip('anser_rf_3', '\x616263'::bytea, t.tamper, + 'B'::"char") AS outcome +FROM (VALUES (''), ('truncate'), ('append'), ('bodylen'), ('tag'), ('kind'), + ('type'), ('crc'), ('key'), ('body')) AS t(tamper); + tamper | outcome +----------+------------------- + | ok + truncate | malformed + append | malformed + bodylen | malformed + tag | malformed + kind | checksum-mismatch + type | unknown-type + crc | checksum-mismatch + key | checksum-mismatch + body | checksum-mismatch +(10 rows) + +-- Keys and bodies that must survive the trip unchanged. "ok" means every +-- field and the decoded body came back identical, so each of these is a case +-- where a naive framing would have gone wrong: the key travels as raw bytes +-- and is taken by length, which is what lets it contain a newline, a space, or +-- the protocol's own tag without any escaping. +SELECT t.what, + anser_test_wire_roundtrip(t.key, t.body, '', 'B'::"char") AS outcome +FROM (VALUES + ('plain key', 'anser_rf_3' , '\x616263'::bytea), + ('key containing a newline', E'rf:a\nb' , '\x616263'::bytea), + ('key that looks like a header', 'anser3 P B 0000000000', '\x616263'::bytea), + ('key with spaces and quotes', 'rf:"a b".c' , '\x616263'::bytea), + ('key at the 63-byte limit', repeat('k', 63) , '\x616263'::bytea), + ('empty key', '' , '\x616263'::bytea), + ('empty body', 'anser_rf_3' , '\x'::bytea), + ('body of a single NUL', 'anser_rf_3' , '\x00'::bytea), + ('body of framing-hostile bytes', 'anser_rf_3' , '\x00ff0a0d5c22'::bytea), + ('1 KB body', 'anser_rf_3' , decode(repeat('00ff', 512), 'hex'))) + AS t(what, key, body); + what | outcome +-------------------------------+--------- + plain key | ok + key containing a newline | ok + key that looks like a header | ok + key with spaces and quotes | ok + key at the 63-byte limit | ok + empty key | ok + empty body | ok + body of a single NUL | ok + body of framing-hostile bytes | ok + 1 KB body | ok +(10 rows) + +-- The QD -> QE checksum. The value itself does not matter; which inputs +-- change it does. Every routing field and the key are always covered, and the +-- body only for a payload type that asks for it -- the fifth column is that +-- deliberate exemption, not an oversight. +SELECT + anser_test_push_crc('B'::"char", 1, 0, 'k', '\x01') + <> anser_test_push_crc('B'::"char", 2, 0, 'k', '\x01') AS condition_covered, + anser_test_push_crc('B'::"char", 1, 0, 'k', '\x01') + <> anser_test_push_crc('B'::"char", 1, 1, 'k', '\x01') AS flags_covered, + anser_test_push_crc('B'::"char", 1, 0, 'k', '\x01') + <> anser_test_push_crc('B'::"char", 1, 0, 'j', '\x01') AS key_covered, + anser_test_push_crc('B'::"char", 1, 0, 'k', '\x01') + <> anser_test_push_crc('B'::"char", 1, 0, 'k', '\x02') AS bloom_body_covered, + anser_test_push_crc('-'::"char", 1, 0, 'k', '\x01') + = anser_test_push_crc('-'::"char", 1, 0, 'k', '\x02') AS other_body_exempt, + anser_test_push_crc('B'::"char", 1, 0, 'k', '\x01') + <> anser_test_push_crc('-'::"char", 1, 0, 'k', '\x01') AS type_covered; + condition_covered | flags_covered | key_covered | bloom_body_covered | other_body_exempt | type_covered +-------------------+---------------+-------------+--------------------+-------------------+-------------- + t | t | t | t | t | t +(1 row) + +DROP EXTENSION anser_test; diff --git a/gpcontrib/anser/include/anser.h b/gpcontrib/anser/include/anser.h new file mode 100644 index 00000000000..5bfa5d772e8 --- /dev/null +++ b/gpcontrib/anser/include/anser.h @@ -0,0 +1,84 @@ +/*------------------------------------------------------------------------- + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * anser.h + * Shared definitions for the Anser adaptive information sharing + * subsystem. + * + * Anser lets producers on the segments publish a small piece of information + * about a running query -- today a bloom filter over a join-build key -- have + * the coordinator combine the per-segment parts, and hand the result back to + * consumers on the segments, which use it to prune work. + * + * Everything travels over the dispatch connection the coordinator already + * holds open to each segment (see ansersideband.h), so there is no shared + * memory, no background worker and no second connection to authenticate: a + * channel exists only in the coordinator backend running the query, for as + * long as that query runs. + * + * IDENTIFICATION + * gpcontrib/anser/include/anser.h + * + *------------------------------------------------------------------------- + */ +#ifndef ANSER_H +#define ANSER_H + +#include "postgres.h" + +#define ANSER_CONDITION_KEY_SIZE 64 + +/* + * Identifies one channel: a single condition within one running command. + * + * condition_key is an opaque symbol naming the condition -- the + * optimizer-generated equivalence-class symbol described in the Anser paper. + * Channels are only ever compared for equality, never ordered. + */ +typedef struct AnserChannelKey +{ + int gp_session_id; + int gp_command_count; + uint32 condition_id; + char condition_key[ANSER_CONDITION_KEY_SIZE]; +} AnserChannelKey; + +/* GUCs (defined in anserinit.c). */ +extern bool gp_anser_enable; +extern bool gp_anser_runtime_filter; +extern bool gp_anser_debug; +extern int gp_anser_max_info_size; +extern int gp_anser_timeout_ms; + +/* + * Trace the handoff between producers, the coordinator and consumers. + * + * Every step of the exchange is invisible in EXPLAIN until it is over -- a + * consumer that waits and gets nothing looks exactly like one whose producers + * never published -- so the path is traceable on demand rather than only + * reconstructable from a debugger. LOG level, so it lands in the log of + * whichever process it happened in. + */ +#define ANSER_DEBUG(...) \ + do { \ + if (gp_anser_debug) \ + elog(LOG, __VA_ARGS__); \ + } while (0) + +#endif /* ANSER_H */ diff --git a/gpcontrib/anser/include/anserbloom.h b/gpcontrib/anser/include/anserbloom.h new file mode 100644 index 00000000000..24c7eaba7f6 --- /dev/null +++ b/gpcontrib/anser/include/anserbloom.h @@ -0,0 +1,79 @@ +/*------------------------------------------------------------------------- + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * anserbloom.h + * Standalone Anser Bloom filter producer/consumer executor helpers. + * + * IDENTIFICATION + * gpcontrib/anser/include/anserbloom.h + * + *------------------------------------------------------------------------- + */ +#ifndef ANSERBLOOM_H +#define ANSERBLOOM_H + +#include "postgres.h" + +#include "anser.h" +#include "lib/bloomfilter.h" + +/* + * Opaque executor state handles for the Bloom filter producer and + * consumer; the struct definitions are private to + * anserbloomproduce.c and anserbloomconsume.c. + */ +typedef struct AnserBloomFilterProduceState AnserBloomFilterProduceState; +typedef struct AnserBloomFilterConsumeState AnserBloomFilterConsumeState; + +/* Producer side: build one Bloom filter part and publish it to the channel. */ + +/* + * libpq connection (parallel-retrieve-cursor model); NULL means connect + * without it and rely on pg_hba. Ignored on the coordinator-local path. + */ +extern AnserBloomFilterProduceState *ExecInitAnserBloomFilterProduce( + const AnserChannelKey *channel_key, + int64 total_elems, + Size max_payload_bytes, + uint32 part_index, + uint32 total_parts); +extern void ExecAnserBloomFilterProduceAddDatum(AnserBloomFilterProduceState *state, + Datum value, bool isnull); +extern bool ExecAnserBloomFilterProduceHasFilter(AnserBloomFilterProduceState *state); +extern bool ExecAnserBloomFilterProducePublish(AnserBloomFilterProduceState *state); +extern bool ExecAnserBloomFilterProduceCancel(AnserBloomFilterProduceState *state); +extern void ExecEndAnserBloomFilterProduce(AnserBloomFilterProduceState *state); + +/* Consumer side: gather all parts of a channel into one Bloom filter. */ +extern AnserBloomFilterConsumeState *ExecInitAnserBloomFilterConsume( + const AnserChannelKey *channel_key, + int64 total_elems, + Size max_payload_bytes, + uint32 expected_parts); +extern bool ExecAnserBloomFilterConsume(AnserBloomFilterConsumeState *state, + long registration_timeout_ms); +extern bloom_filter *ExecAnserBloomFilterConsumerGetFilter( + AnserBloomFilterConsumeState *state); +extern uint32 ExecAnserBloomFilterConsumerReceivedParts( + AnserBloomFilterConsumeState *state); +extern bool ExecAnserBloomFilterConsumerWasCancelled( + AnserBloomFilterConsumeState *state); +extern void ExecEndAnserBloomFilterConsume(AnserBloomFilterConsumeState *state); + +#endif /* ANSERBLOOM_H */ diff --git a/gpcontrib/anser/include/anserfilter.h b/gpcontrib/anser/include/anserfilter.h new file mode 100644 index 00000000000..7e50e25c042 --- /dev/null +++ b/gpcontrib/anser/include/anserfilter.h @@ -0,0 +1,127 @@ +/*------------------------------------------------------------------------- + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * anserfilter.h + * Bloom-filter payload helpers for Anser channels. + * + * IDENTIFICATION + * gpcontrib/anser/include/anserfilter.h + * + *------------------------------------------------------------------------- + */ +#ifndef ANSERFILTER_H +#define ANSERFILTER_H + +#include "postgres.h" + +#include "lib/bloomfilter.h" + +#define ANSER_BLOOM_PART_MAGIC 0x41424631U /* ABF1 */ +#define ANSER_BLOOM_PART_VERSION 1U + +/* + * On-wire framing for a serialized bloom part. It deliberately does NOT carry + * the bitset parameters (size / seed / hash count): both the producer and the + * consumer build the filter with bloom_create from the same plan parameters, so + * the shape is agreed by construction and never reconstructed from the wire. + * magic/version guard the framing; part_index/total_parts track the coordinator + * fold count (surfaced as diagnostics). Kept as a struct for forward + * extensibility. + */ +typedef struct AnserBloomPartHeader +{ + uint32 magic; + uint32 version; + uint32 part_index; + uint32 total_parts; +} AnserBloomPartHeader; + +/* + * When a bloom filter stops being worth building, or sending once built. + * Three checks, at the three points where new information arrives. + * + * ANSER_BLOOM_MIN_BITS_PER_KEY -- a floor on the *planned* density: bitset bits + * divided by the number of distinct keys we expect. Below it no amount of care + * in building the filter helps, so the filter is not built and, at plan time, + * the nodes are not even injected. At 4 bits/key the optimal hash count is 3 + * and the false positive rate is already ~15%; below that it collapses (2 + * bits/key is ~40%). + * + * ANSER_BLOOM_MAX_FPR -- the check on the *realized* filter, at publish time. + * It catches what a row estimate cannot: the estimate was too low, so the + * filter saturated anyway. This one is expressed as a false positive rate + * rather than a fill fraction on purpose. FPR is fill^k, so a single fill + * limit is not a single quality bar: 80% full is a 51% FPR at k=3 but only + * 10.7% at k=10, and a rule that cancelled the latter would be throwing away a + * filter that eliminates nine probe rows in ten. The producer has the filter + * and therefore its k, so it can just ask. + * + * ANSER_BLOOM_MERGED_MAX_FILL -- the same question asked by the coordinator of + * the merged payload, which is the only place it can be asked about what + * consumers will actually receive. It has to fall back on fill, because k is + * derived from plan parameters and is not carried in the serialized part. It + * is therefore set where even k=10 is past saving (0.95^10 = 60% FPR), so that + * it only ever rejects the hopeless. Putting k in AnserBloomPartHeader would + * let this use the FPR too. + * + * All three numbers are first cuts. Calibrating them is the "stop building + * filters that add nothing" part of the bloom-performance work; the debug trace + * logs fill and FPR at every decision point so that study has data. + */ +#define ANSER_BLOOM_MIN_BITS_PER_KEY 4.0 +#define ANSER_BLOOM_MAX_FPR 0.50 +#define ANSER_BLOOM_MERGED_MAX_FILL 0.95 + +/* + * Is a serialized part (or a merged accumulator of them) still worth + * delivering? False when too many of its bits are set for it to reject + * anything useful. Works on the wire form, so the coordinator can ask this of + * a merged payload without rebuilding a filter. + */ +extern bool AnserBloomPartWorthSending(const void *payload, Size payload_len); + +extern uint64 AnserBloomSeed(const char *condition_key); + +/* + * Build an empty filter, or return NULL when AnserBloomShapeFor says it is not + * worth building. A NULL return is not an error: the producer turns it into an + * immediate cancel, so consumers stop waiting instead of timing out. + */ +extern bloom_filter *AnserBloomCreate(int64 total_elems, + Size max_payload_bytes, + uint64 seed); +extern Size AnserBloomSerializedSize(const bloom_filter *filter); +extern bool AnserBloomSerializePart(const bloom_filter *filter, + uint32 part_index, + uint32 total_parts, + void *buffer, + Size buffer_size, + Size *payload_len); +extern bloom_filter *AnserBloomDeserializePart(const void *payload, + Size payload_len, + int64 total_elems, + Size max_payload_bytes, + uint64 seed, + uint32 *part_index, + uint32 *total_parts); +extern bool AnserBloomLooksLikePart(const void *payload, Size payload_len); +extern bool AnserBloomFoldPartInPlace(void *acc, Size acc_len, + const void *part, Size part_len); + +#endif /* ANSERFILTER_H */ diff --git a/gpcontrib/anser/include/anserpayload.h b/gpcontrib/anser/include/anserpayload.h new file mode 100644 index 00000000000..cb5f30dffcf --- /dev/null +++ b/gpcontrib/anser/include/anserpayload.h @@ -0,0 +1,150 @@ +/*------------------------------------------------------------------------- + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * anserpayload.h + * What a channel carries, and who knows what to do with it. + * + * The transport (ansersideband.c on segments, anserdispatch.c on the + * coordinator) moves opaque bytes: it cannot tell a bloom filter from a row + * count, and should not have to. Everything that depends on what the bytes + * mean lives in one descriptor per payload type, so a new kind of runtime + * information needs no change to the framing, the checksumming or the channel + * bookkeeping. + * + * Exactly two things genuinely differ per type. + * + * How parts merge. The coordinator receives one part per producer and must + * reduce them to a single payload. For a bloom filter that is a bitwise OR of + * equal-sized bitsets; for a row count it would be a sum; for a min/max + * summary it would be two comparisons. There is no generic answer, so each + * type supplies fold(). + * + * Whether corruption can give a wrong answer. This is what 'checksum_body' + * decides, and it is about consequence, not size. A bloom filter fails + * asymmetrically: a bit flipped 0 -> 1 merely costs selectivity, but a bit + * flipped 1 -> 0 removes a key from the filter, so a joinable row is rejected + * and the query returns fewer rows than it should -- silently. A row count, by + * contrast, only feeds a planning decision: a corrupted one costs a worse plan, + * never an incorrect result. It does not need to be checksummed, and paying + * 0.05 ms/MB to checksum it anyway would be waste. + * + * Note what 'checksum_body' does *not* cover: the header and the condition key + * are checksummed for every message regardless. That is ~100 ns for a 95-byte + * header and it is what validates the routing fields, so there is nothing to + * gain by skipping it; the flag controls only the payload, which is where the + * cost lives. + * + * To add a payload type: + * + * 1. #define a code below. It travels as one byte and appears in the + * anser.debug trace, so keep it printable -- and it may not be '\0', + * which AnserPayloadTable uses to recognize a slot nobody registered. + * 2. Write a fold() and add a row to AnserPayloadTable in anserpayload.c, + * keyed by the code: the table is indexed by the wire byte, so the + * designator and the row's own 'code' field must agree (an assertion in + * AnserPayloadLookup catches it if they do not). + * 3. Pass the code to AnserSidebandPublish/AnserDispatchLocalPublish in the + * producer node, and to AnserSidebandConsumeWait/AnserDispatchLocalConsume + * in the consumer node. + * + * That is the whole extension point. In particular, do not add a branch on the + * payload type to the transport: if something there needs to know the type, + * that knowledge belongs in this descriptor instead. + * + * IDENTIFICATION + * gpcontrib/anser/include/anserpayload.h + * + *------------------------------------------------------------------------- + */ +#ifndef ANSERPAYLOAD_H +#define ANSERPAYLOAD_H + +#include "anser.h" + +/* + * Payload type codes. + * + * Distinct from the message kind (P/S), which says what a message *does* + * rather than what it *carries*: a subscription carries nothing, and a + * cancelled part still declares the type it would have carried. + */ +#define ANSER_PAYLOAD_NONE '-' /* no payload (a subscription) */ +#define ANSER_PAYLOAD_BLOOM 'B' /* serialized bloom filter part */ + +/* '\0' is reserved: it is how an unregistered code is recognized. */ + +typedef struct AnserPayloadOps +{ + char code; /* the wire byte */ + const char *name; /* for logs; no other use */ + bool checksum_body; /* see the file header */ + + /* + * Merge 'part' into the accumulator in place. + * + * The accumulator is the first part the channel received, kept verbatim, so + * the two always have the same type -- but not necessarily the same length + * or internal parameters, which is what this has to check. Return false if + * they cannot be merged; the caller then cancels the channel, which costs + * the consumers their filter but is never silently wrong. + * + * Must not modify 'accum' unless it returns true: the caller reports a + * false return and carries on, so a half-folded accumulator would be + * indistinguishable from a good one. + * + * NULL for a type that is never folded (one that has no payload). + */ + bool (*fold) (void *accum, Size accum_len, + const void *part, Size part_len); + + /* + * Is the finished accumulator worth delivering? Asked once, after the last + * part has been folded and before anything is sent to a consumer; a false + * return cancels the channel, so consumers run unfiltered. + * + * This is the only place the question can be answered honestly, because + * folding changes the answer: OR-ing bloom parts together makes the result + * denser than any part, so three parts that each looked worth sending can + * merge into one that is not. Producers cannot see that, and consumers + * should not be made to pay for it. + * + * NULL for a type that is always worth delivering -- a row count does not + * become less useful for having been summed. + */ + bool (*worth_delivering) (const void *accum, Size accum_len); +} AnserPayloadOps; + +/* + * Resolve a code to its descriptor, or NULL if nothing has registered it. + * + * Callers must handle the NULL: the code arrives from the wire, so an + * unregistered one is a real case, and defaulting it silently would turn a + * forgotten table row into a mystery rather than a log line. + */ +extern const AnserPayloadOps *AnserPayloadLookup(char code); + +/* + * Whether a type's body is covered by the message checksum. Takes a code + * rather than a descriptor because the transport reaches this while parsing, + * before it has resolved anything; an unregistered code answers false, which + * is why this one needs no error handling. + */ +extern bool AnserPayloadChecksumsBody(char code); + +#endif /* ANSERPAYLOAD_H */ diff --git a/gpcontrib/anser/include/anserplan.h b/gpcontrib/anser/include/anserplan.h new file mode 100644 index 00000000000..4e59453210d --- /dev/null +++ b/gpcontrib/anser/include/anserplan.h @@ -0,0 +1,101 @@ +/*------------------------------------------------------------------------- + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * anserplan.h + * Post-planning transformation that injects Anser runtime bloom-filter + * producer/consumer nodes into a finished plan tree. + * + * IDENTIFICATION + * gpcontrib/anser/include/anserplan.h + * + *------------------------------------------------------------------------- + */ +#ifndef ANSERPLAN_H +#define ANSERPLAN_H + +#include "nodes/plannodes.h" + +/* + * Post-plan pass: recognize the supported join shape in a finished PlannedStmt + * and inject an Anser bloom-filter producer (on the hash build side) and a + * consumer (above the probe scan). Called once from planner(), so it covers + * both the Postgres planner and ORCA. A no-op unless the Anser runtime-filter + * GUCs are on and this is a coordinator SELECT. + */ +extern void AnserApplyRuntimeFilters(PlannedStmt *stmt); + +/* + * Register the two CustomScan providers (producer, consumer) so their methods + * resolve by name when a dispatched plan is deserialized. Must run once per + * backend (QD and every QE) before any plan execution. + */ +extern void AnserRegisterRuntimeFilterMethods(void); + +/* + * Node builders (implemented in anserplanexec.c, where the CustomScan method + * tables live). Each wraps `child` in a pass-through CustomScan carrying the + * runtime-filter parameters in custom_private; the caller assigns plan_node_id. + * `key_attno` is the build (producer) / probe (consumer) join-key attno in the + * child's output tuple. `n_producers` is how many processes will publish a + * part -- the width of the build scan's slice, which under parallel execution + * is numsegments * parallel_workers rather than the segment count. + */ +/* + * True for the producer and consumer nodes this module injects. The injection + * pass meets them where it expects a scan, because two joins can want to filter + * the same base relation; coordinator-only, see the implementation. + */ +extern bool AnserIsRuntimeFilterScan(const Plan *plan); + +extern CustomScan *AnserBuildBloomProducerScan(Plan *child, AttrNumber key_attno, + uint32 condition_id, + const char *condition_key, + int64 total_elems, + Size max_payload_bytes, + int64 planned_bytes, + int n_producers); +/* + * `defer_first` tells the consumer to pass its first tuple through unfiltered. + * ExecHashJoin pulls one outer tuple before building its hash table, and a + * consumer that blocks there waits for an inner side the join has not started + * -- in a co-located join, for its own process. Set it whenever the prefetch + * cannot be ruled out; the cost is that such a consumer cannot push its filter + * into the scan and probes it itself instead. + */ +extern CustomScan *AnserBuildBloomConsumerScan(Plan *child, AttrNumber key_attno, + uint32 condition_id, + const char *condition_key, + int64 total_elems, + Size max_payload_bytes, + int64 planned_bytes, + int n_producers, + bool defer_first); + +/* + * Bloom sizing for one join, from its estimated build cardinality. False means + * no filter is worth injecting -- see the density rule in anserfilter.h. + * + * Exposed rather than static because it is the cheapest of Anser's give-up + * decisions and therefore the one most worth testing directly at its boundary + * (anser_test.c); nothing but the injection pass and the tests should call it. + */ +extern bool AnserRuntimeFilterSize(double est_rows, int64 *total_elems, + int64 *max_payload, int64 *planned_bytes); + +#endif /* ANSERPLAN_H */ diff --git a/gpcontrib/anser/include/ansersideband.h b/gpcontrib/anser/include/ansersideband.h new file mode 100644 index 00000000000..2cbea9336cd --- /dev/null +++ b/gpcontrib/anser/include/ansersideband.h @@ -0,0 +1,249 @@ +/*------------------------------------------------------------------------- + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * ansersideband.h + * Anser transport over the existing QD <-> QE dispatch connection. + * + * Instead of opening a second libpq connection back to the coordinator, a + * segment executor reuses the connection the dispatcher already holds open: + * QE -> QD travels as a NOTIFY (the model nextval() uses, see + * cdb_sequence_nextval_qe in commands/sequence.c), and QD -> QE as a + * GP_SIDEBAND_MESSAGE the waiting QE reads off its own socket. + * + * The two directions are not symmetric, and the wire formats differ for a + * reason. A NOTIFY payload is delivered by pq_sendstring(), so it must be a + * NUL-free C string -- hence the text header and base64 body. The QD -> QE + * push is written with pqPutnchar(), which performs no conversion, so the + * merged filter travels as raw binary. That matters: the merged payload is + * sent once per consumer, while each part is sent once. + * + * Both directions carry a CRC32C. Not because the transport is lossy -- it is + * TCP, or a Unix socket; the UDP interconnect is a different channel entirely + * -- but because some payloads fail asymmetrically and TCP's 16-bit checksum is + * thin cover for a megabyte. The header and condition key are always covered; + * whether the body is covered too is the payload type's decision + * (AnserPayloadOps.checksum_body, see anserpayload.h), because that is the part + * that costs 0.05 ms/MB and only some payloads can turn corruption into a wrong + * answer. The QE -> QD checksum covers the raw pre-base64 bytes, so where it + * applies it validates the decode as well. + * + * Neither direction branches on the payload type beyond consulting that one + * flag: everything else about what the bytes mean belongs in the registry. + * + * IDENTIFICATION + * gpcontrib/anser/include/ansersideband.h + * + *------------------------------------------------------------------------- + */ +#ifndef ANSERSIDEBAND_H +#define ANSERSIDEBAND_H + +#include "anser.h" +#include "anserpayload.h" +#include "port/pg_crc32c.h" + +struct CdbDispatchResult; /* #include "cdb/cdbdispatchresult.h" */ +struct pgNotify; /* #include "libpq-fe.h" */ + +/* + * NOTIFY channel for QE -> QD Anser traffic. The dispatcher hands notifies it + * does not recognize to cdbdisp_notify_hook, where we match on this name. + */ +#define ANSER_NOTIFY_CHANNEL "anser_rf" + +/* First token of every QE -> QD payload; bump when the format changes. */ +#define ANSER_WIRE_TAG "anser3" + +/* + * Message kinds (QE -> QD): what the message does. What it carries is a + * separate field, the payload type -- see anserpayload.h. + */ +#define ANSER_WIRE_KIND_PART 'P' /* a producer's serialized part */ +#define ANSER_WIRE_KIND_SUBSCRIBE 'S' /* a consumer registering interest */ + +/* Flag bits, shared by both directions. */ +#define ANSER_WIRE_F_CANCELLED 0x0001 + +/* + * QE -> QD header: fixed width, so the key and body start at known offsets and + * nothing about their contents can affect parsing. + * + * anser3 K T SSSSSSSSSS CCCCCCCCCC DDDDDDDDDD PPPPPPPPPP TTTTTTTTTT FFFF KKKK BBBBBBBBBB XXXXXXXX + * ^tag ^ ^ session command condition part total ^ ^ bodylen crc + * | \ payload type flags keylen + * \ kind + * + * followed immediately by key bytes and body bytes. Every + * field is zero-padded (flags and CRC are hex, the rest decimal), so the header + * is pure ASCII of a constant length and holds no delimiter that payload bytes + * could imitate. The two lengths are then a cross-check: the total payload + * length must be exactly the header plus both, or the message is rejected. + * + * The signed session and command ids travel as uint32 and are cast back on + * receipt, which round-trips them exactly while keeping the width fixed -- + * "%010d" of a negative value is eleven characters, which would have made the + * header variable-width again. + * + * The previous format ended its header with a newline and found it with + * strchr(). That was correct only because the header could not itself contain + * a newline -- an invariant nothing enforced, and one that a key derived from + * relation names (quoted identifiers may contain anything) would have broken + * silently. + */ +#define ANSER_WIRE_HDR_FORMAT \ + ANSER_WIRE_TAG " %c %c %010u %010u %010u %010u %010u %04x %04u %010u %08x" + +/* + * Read format for the same header. The widths are what make the parse safe: + * every conversion stops after exactly its field, so no value can consume the + * separator or run into the key bytes that follow the header. + */ +#define ANSER_WIRE_HDR_SCANF \ + ANSER_WIRE_TAG " %c %c %10u %10u %10u %10u %10u %4x %4u %10u %8x" + +/* 6 + the 11 fields above, each preceded by its separating space. */ +#define ANSER_WIRE_HDR_LEN (6 + (1 + 1) + (1 + 1) + (1 + 10) + (1 + 10) \ + + (1 + 10) + (1 + 10) + (1 + 10) + (1 + 4) \ + + (1 + 4) + (1 + 10) + (1 + 8)) + +/* Offset of the 8-hex-digit CRC; the checksum covers everything before it. */ +#define ANSER_WIRE_CRC_OFFSET (ANSER_WIRE_HDR_LEN - 8) + +/* + * Largest key and body a header can describe. Checked before formatting so a + * field can never overflow its width and shift every field after it. + */ +#define ANSER_WIRE_MAX_KEYLEN 9999 +#define ANSER_WIRE_MAX_BODYLEN 999999999 + +/* + * The codec. + * + * Four functions, exported rather than static because together they *are* the + * protocol: the two directions have to agree byte for byte about offsets and + * about what the checksum covers, and that agreement is only auditable -- and + * only testable -- if both halves are reachable by name. sql/anser_test.sql + * drives them directly. + * + * They are implemented on the side that writes: AnserWireFormat in + * ansersideband.c (only a QE formats), AnserWireParse and AnserWireCheckCrc in + * anserdispatch.c (only the QD parses). + * + * AnserWireFormat returns a palloc'd NUL-terminated string. AnserWireParse + * fills 'out' and returns false for anything it will not vouch for; its + * contract and the checks it makes are documented at the definition. + * AnserWireCheckCrc takes the *decoded* body, or NULL/0 when there is none. + */ +typedef struct AnserWireMsg +{ + const char *wire; /* the message itself; the CRC covers its head */ + char kind; + char payload_type; /* resolved against the registry by the caller */ + AnserChannelKey key; + int key_len; + int part_index; + int total_parts; + int flags; + const char *body; /* base64, not NUL-terminated */ + int body_len; + uint32 crc; /* of the header, key and decoded body */ +} AnserWireMsg; + +extern char *AnserWireFormat(const AnserChannelKey *channel_key, char kind, + char payload_type, uint32 part_index, + uint32 total_parts, int flags, + const void *payload, Size payload_len); +extern bool AnserWireParse(const char *msg, AnserWireMsg *out); +extern bool AnserWireCheckCrc(const AnserWireMsg *msg, const void *body, + Size body_len); + +/* + * QD -> QE push: raw binary, written with pqPutInt/pqPutnchar. + * + * payload_type | crc32c | condition_id | flags | keylen | key | bodylen | body + * + * with each field a 4-byte integer except the key and body. The CRC covers all + * of the other fields in that order, the integers in network byte order, so it + * validates the routing information as well as the payload; the body is + * included only when the payload type says so. Both ends compute it through + * this one function so the two cannot drift apart. It is implemented in + * ansersideband.c, beside the reader that verifies it. + */ +extern pg_crc32c AnserWirePushCrc(char payload_type, uint32 condition_id, + uint32 flags, const char *key, int keylen, + const void *body, int bodylen); + +/* + * QE side (ansersideband.c). + * + * AnserSidebandPublish is fire-and-forget: unlike the libpq transport it does + * not wait for the coordinator to acknowledge the part. 'payload_type' is + * stamped on the wire even when cancelled, since it is what tells the + * coordinator how to fold the channel's parts. + * + * AnserSidebandConsumeWait blocks on this backend's own dispatch socket until + * the merged payload arrives, the channel is cancelled, or timeout_ms elapses. + * On success *payload is palloc'd in the caller's context. A false return + * always means "run unfiltered", never an error -- including when what arrived + * is not of 'payload_type', which would mean two different kinds of information + * had collided on one channel. + */ +extern bool AnserSidebandPublish(const AnserChannelKey *channel_key, + char payload_type, + uint32 part_index, uint32 total_parts, + const void *payload, Size payload_len, + bool cancelled); +extern bool AnserSidebandConsumeWait(const AnserChannelKey *channel_key, + char payload_type, + void **payload, Size *payload_len, + bool *cancelled, long timeout_ms); + +/* + * QD side (anserdispatch.c). + * + * AnserDispatchNotifyHandler is installed as cdbdisp_notify_hook; it folds + * arriving parts and pushes the merged payload to subscribers. The Local + * variants serve producers and consumers running on the coordinator itself, + * which have no dispatch connection to themselves and so operate on the same + * per-query channel table directly. They take the same payload type for the + * same reasons, so that a coordinator-local producer and a segment producer are + * interchangeable on one channel. + */ +extern bool AnserDispatchNotifyHandler(struct CdbDispatchResult *dispatchResult, + struct pgNotify *notify); +extern bool AnserDispatchLocalPublish(const AnserChannelKey *channel_key, + char payload_type, + uint32 part_index, uint32 total_parts, + const void *payload, Size payload_len, + bool cancelled); +extern bool AnserDispatchLocalConsume(const AnserChannelKey *channel_key, + char payload_type, + void **payload, Size *payload_len, + bool *cancelled); + +/* + * Drop per-query state. AnserSidebandResetAll does both sides and is what + * the executor/transaction-end callbacks call; the halves are exposed because + * each lives with the state it owns. + */ +extern void AnserDispatchReset(void); +extern void AnserSidebandResetInbox(void); +extern void AnserSidebandResetAll(void); + +#endif /* ANSERSIDEBAND_H */ diff --git a/gpcontrib/anser/sql/anser_multijoin.sql b/gpcontrib/anser/sql/anser_multijoin.sql new file mode 100644 index 00000000000..e10247295d6 --- /dev/null +++ b/gpcontrib/anser/sql/anser_multijoin.sql @@ -0,0 +1,228 @@ +-- Anser runtime bloom filter: chains of 2..10 joins. +-- +-- Each table joins to the next on its own key pair -- t1.nxt = t2.own, then +-- t2.nxt = t3.own, and so on -- which is what a real chain looks like: no two +-- joins share a key, so no table's distribution can serve more than one of +-- them and every join needs a motion of its own. Joining a whole chain on one +-- column would make the joins co-located and would test the easy case only. +-- +-- This is also the shape the feature has to handle: the build side of the third +-- join is the result of the first two, and the key column for that join comes +-- from t2, so it reaches its base relation through a join rather than straight +-- through a Hash. +-- +-- What these cases assert is not that a filter was injected -- that is the +-- planner's choice and it differs between the two optimizers and with the +-- cluster width -- but that the answer is the same either way. A bloom filter +-- fails asymmetrically: a bit that should be set and is not silently removes +-- joinable rows, so "same rows with the filter on as with it off" is the +-- property worth asserting. Plan shapes are deliberately not asserted; an +-- expected file full of them would break on a three-segment cluster and pass on +-- a one-segment one without saying anything about correctness. +-- +-- Every query below reports its violations, so the expected output is empty +-- results and zeros. A failure names the chain that broke rather than leaving +-- a table of numbers to be compared by eye. + +SET enable_nestloop = off; +SET enable_mergejoin = off; + +-- A regress test must not inherit the production deadline. anser.timeout_ms +-- defaults to 100 s because delivery is serial and a wide slice needs that +-- long to be served; a test that waits it out has turned a delivery failure +-- into a slow pass. Pinned short here, so a filter that never arrives fails +-- the run instead of hiding in the clock. +SET anser.timeout_ms = 5000; + +-- own is what the previous table joins to, nxt is what this table joins to the +-- next one with. They hold the same value, which keeps the chain 1:1 and the +-- answer predictable -- 1000 rows summing to 500500, whatever the join count -- +-- while still being two different columns as far as the planner is concerned. +-- +-- The distribution key is deliberately never the whole story: a table +-- distributed by own is joined on nxt, and the ones distributed by payload are +-- joined on both. Every join therefore redistributes or broadcasts one side, +-- and the producer ends up in a different slice from its consumer. +-- +-- t1 is the smallest and every later table contains it, so the chain returns +-- t1 whatever order the planner picks. The widening sizes give it a reason to +-- hash the joined result rather than the next table, which is the shape that +-- exercises the descent through a join. +CREATE TABLE anser_mj_t1 (own int, nxt int, payload text) DISTRIBUTED BY (own); +CREATE TABLE anser_mj_t2 (own int, nxt int, payload text) DISTRIBUTED BY (payload); +CREATE TABLE anser_mj_t3 (own int, nxt int, payload text) DISTRIBUTED BY (own); +CREATE TABLE anser_mj_t4 (own int, nxt int, payload text) DISTRIBUTED BY (payload); +CREATE TABLE anser_mj_t5 (own int, nxt int, payload text) DISTRIBUTED BY (own); +CREATE TABLE anser_mj_t6 (own int, nxt int, payload text) DISTRIBUTED BY (payload); +CREATE TABLE anser_mj_t7 (own int, nxt int, payload text) DISTRIBUTED BY (own); +CREATE TABLE anser_mj_t8 (own int, nxt int, payload text) DISTRIBUTED BY (payload); +CREATE TABLE anser_mj_t9 (own int, nxt int, payload text) DISTRIBUTED BY (own); +CREATE TABLE anser_mj_t10 (own int, nxt int, payload text) DISTRIBUTED BY (payload); +CREATE TABLE anser_mj_t11 (own int, nxt int, payload text) DISTRIBUTED BY (own); + +INSERT INTO anser_mj_t1 SELECT g, g, 't1_' || g FROM generate_series(1, 1000) g; +INSERT INTO anser_mj_t2 SELECT g, g, 't2_' || g FROM generate_series(1, 2000) g; +INSERT INTO anser_mj_t3 SELECT g, g, 't3_' || g FROM generate_series(1, 3000) g; +INSERT INTO anser_mj_t4 SELECT g, g, 't4_' || g FROM generate_series(1, 4000) g; +INSERT INTO anser_mj_t5 SELECT g, g, 't5_' || g FROM generate_series(1, 5000) g; +INSERT INTO anser_mj_t6 SELECT g, g, 't6_' || g FROM generate_series(1, 6000) g; +INSERT INTO anser_mj_t7 SELECT g, g, 't7_' || g FROM generate_series(1, 7000) g; +INSERT INTO anser_mj_t8 SELECT g, g, 't8_' || g FROM generate_series(1, 8000) g; +INSERT INTO anser_mj_t9 SELECT g, g, 't9_' || g FROM generate_series(1, 9000) g; +INSERT INTO anser_mj_t10 SELECT g, g, 't10_' || g FROM generate_series(1, 10000) g; +INSERT INTO anser_mj_t11 SELECT g, g, 't11_' || g FROM generate_series(1, 11000) g; + +ANALYZE anser_mj_t1; +ANALYZE anser_mj_t2; +ANALYZE anser_mj_t3; +ANALYZE anser_mj_t4; +ANALYZE anser_mj_t5; +ANALYZE anser_mj_t6; +ANALYZE anser_mj_t7; +ANALYZE anser_mj_t8; +ANALYZE anser_mj_t9; +ANALYZE anser_mj_t10; +ANALYZE anser_mj_t11; + +-- Runs one chain twice, with the filter on and off, and reports whether the two +-- agree. The checksum is what makes the comparison mean something: equal row +-- counts would also come out of two different sets of rows, and a filter that +-- wrongly rejects is exactly the failure that changes which rows come back. +CREATE FUNCTION anser_mj_check(njoins int) +RETURNS TABLE (joins int, nrows bigint, own_sum bigint, agrees boolean) +LANGUAGE plpgsql AS $$ +DECLARE + sql text; + i int; + on_rows bigint; + on_sum bigint; + off_rows bigint; + off_sum bigint; +BEGIN + sql := 'SELECT count(*), coalesce(sum(anser_mj_t1.own), 0) FROM anser_mj_t1'; + FOR i IN 2 .. njoins + 1 LOOP + sql := sql || format(' JOIN anser_mj_t%s ON anser_mj_t%s.nxt = anser_mj_t%s.own', + i, i - 1, i); + END LOOP; + PERFORM set_config('anser.runtime_filter', 'on', false); + EXECUTE sql INTO on_rows, on_sum; + PERFORM set_config('anser.runtime_filter', 'off', false); + EXECUTE sql INTO off_rows, off_sum; + RETURN QUERY SELECT njoins, on_rows, on_sum, + (on_rows = off_rows AND on_sum = off_sum); +END; +$$; + +-- 2 through 10 joins, Postgres planner. Expected: no rows. +SET optimizer = off; +SELECT joins, nrows, own_sum, agrees + FROM generate_series(2, 10) g, anser_mj_check(g) + WHERE NOT agrees OR nrows <> 1000 OR own_sum <> 500500 + ORDER BY joins; + +-- The same under ORCA, which picks different join orders and slice widths. +SET optimizer = on; +SELECT joins, nrows, own_sum, agrees + FROM generate_series(2, 10) g, anser_mj_check(g) + WHERE NOT agrees OR nrows <> 1000 OR own_sum <> 500500 + ORDER BY joins; + +SET optimizer = off; + +-- A chain whose middle join is an outer join, and whose next join reads the +-- nullable side's key. The joined result there holds NULLs the base relation +-- does not, and a NULL must not be taken for a key the filter is missing. +SET anser.runtime_filter = on; +CREATE TEMP TABLE anser_mj_lj_on AS + SELECT a.own AS aown, c.own AS cown, d.own AS down + FROM anser_mj_t1 a + JOIN anser_mj_t2 b ON a.nxt = b.own + LEFT JOIN anser_mj_t3 c ON b.nxt = c.own + JOIN anser_mj_t4 d ON c.nxt = d.own + DISTRIBUTED BY (aown); +SET anser.runtime_filter = off; +CREATE TEMP TABLE anser_mj_lj_off AS + SELECT a.own AS aown, c.own AS cown, d.own AS down + FROM anser_mj_t1 a + JOIN anser_mj_t2 b ON a.nxt = b.own + LEFT JOIN anser_mj_t3 c ON b.nxt = c.own + JOIN anser_mj_t4 d ON c.nxt = d.own + DISTRIBUTED BY (aown); + +SELECT count(*) AS lj_rows FROM anser_mj_lj_on; +SELECT count(*) AS lj_only_on + FROM (SELECT * FROM anser_mj_lj_on EXCEPT ALL SELECT * FROM anser_mj_lj_off) d; +SELECT count(*) AS lj_only_off + FROM (SELECT * FROM anser_mj_lj_off EXCEPT ALL SELECT * FROM anser_mj_lj_on) d; + +-- A star rather than a chain: every join reads t1's key, so one table's +-- distribution serves them all and the build sides are base scans throughout. +-- This is the shape that worked before the descent went through joins, and it +-- has to keep working. +SET anser.runtime_filter = on; +CREATE TEMP TABLE anser_mj_star_on AS + SELECT a.own AS aown, e.payload AS epayload + FROM anser_mj_t1 a + JOIN anser_mj_t2 b ON a.nxt = b.own + JOIN anser_mj_t3 c ON a.nxt = c.own + JOIN anser_mj_t4 d ON a.nxt = d.own + JOIN anser_mj_t5 e ON a.nxt = e.own + DISTRIBUTED BY (aown); +SET anser.runtime_filter = off; +CREATE TEMP TABLE anser_mj_star_off AS + SELECT a.own AS aown, e.payload AS epayload + FROM anser_mj_t1 a + JOIN anser_mj_t2 b ON a.nxt = b.own + JOIN anser_mj_t3 c ON a.nxt = c.own + JOIN anser_mj_t4 d ON a.nxt = d.own + JOIN anser_mj_t5 e ON a.nxt = e.own + DISTRIBUTED BY (aown); + +SELECT count(*) AS star_rows FROM anser_mj_star_on; +SELECT count(*) AS star_only_on + FROM (SELECT * FROM anser_mj_star_on EXCEPT ALL SELECT * FROM anser_mj_star_off) d; +SELECT count(*) AS star_only_off + FROM (SELECT * FROM anser_mj_star_off EXCEPT ALL SELECT * FROM anser_mj_star_on) d; + +-- A chain that returns nothing. A filter that wrongly accepts is only slow, +-- never wrong, so this is the one direction an empty result can still check. +SET anser.runtime_filter = on; +SELECT count(*) AS empty_rows + FROM anser_mj_t1 a + JOIN anser_mj_t2 b ON a.nxt = b.own + JOIN anser_mj_t3 c ON b.nxt = c.own + WHERE a.own < 0; + +-- The chains above assert that the filter changes no answer. That also holds +-- when the filter never arrives and every consumer fails open, which is how a +-- broken exchange passes a green test. This asserts the other half: a chain +-- whose third table is three times the size of the first has rows to prune, so +-- a filter that arrived removes some. +CREATE FUNCTION anser_mj_pruned(q text) RETURNS bigint +LANGUAGE plpgsql AS $$ +DECLARE + line text; + removed bigint := 0; +BEGIN + FOR line IN EXECUTE 'EXPLAIN (ANALYZE, TIMING OFF, COSTS OFF) ' || q LOOP + IF line ~ 'Rows Removed by (Bloom Filter|Pushdown Runtime Filter): ' THEN + removed := removed + coalesce(substring(line from ': *([0-9]+)')::bigint, 0); + END IF; + END LOOP; + RETURN removed; +END; +$$; +SET anser.runtime_filter = on; +SELECT 'the chain pruned nothing: no filter arrived, or none was injected' AS problem + WHERE anser_mj_pruned('SELECT count(*) FROM anser_mj_t1 a JOIN anser_mj_t2 b ON a.nxt = b.own JOIN anser_mj_t3 c ON b.nxt = c.own') = 0; +DROP FUNCTION anser_mj_pruned(text); + +DROP FUNCTION anser_mj_check(int); +DROP TABLE anser_mj_t1, anser_mj_t2, anser_mj_t3, anser_mj_t4, anser_mj_t5, + anser_mj_t6, anser_mj_t7, anser_mj_t8, anser_mj_t9, anser_mj_t10, + anser_mj_t11; + +RESET anser.runtime_filter; +RESET optimizer; +RESET enable_nestloop; +RESET enable_mergejoin; diff --git a/gpcontrib/anser/sql/anser_runtime_filter.sql b/gpcontrib/anser/sql/anser_runtime_filter.sql new file mode 100644 index 00000000000..06e12ac2f2f --- /dev/null +++ b/gpcontrib/anser/sql/anser_runtime_filter.sql @@ -0,0 +1,305 @@ +-- Anser runtime bloom filter: plan-tree integration (PR4). +-- +-- Requires the cluster to run with anser.enable=on so the gather/send +-- services are live. On a multi-segment cluster the build side redistributes, +-- exercising the Motion-directly-under-CustomScan case; on a single segment it +-- degrades to the leaf case. Either way the feature must (a) inject the +-- producer/consumer nodes and (b) never change query results. + + +-- Deterministic plan shape: force a hash join. +SET enable_nestloop = off; +SET enable_mergejoin = off; + +-- A regress test must not inherit the production deadline. anser.timeout_ms +-- defaults to 100 s because delivery is serial and a wide slice needs that +-- long to be served; a test that waits it out has turned a delivery failure +-- into a slow pass. Pinned short here, so a filter that never arrives fails +-- the run instead of hiding in the clock. +SET anser.timeout_ms = 5000; + +-- build is the smaller (hashed / preserved) side, distributed so the join key +-- must be redistributed; probe is the larger side distributed by the join key. +CREATE TABLE anser_rf_build (id int, name text) DISTRIBUTED BY (name); +CREATE TABLE anser_rf_probe (id int, payload text) DISTRIBUTED BY (id); +INSERT INTO anser_rf_build SELECT g, 'b' || g FROM generate_series(1, 200) g; +INSERT INTO anser_rf_probe SELECT g, 'p' || g FROM generate_series(1, 2000) g; +ANALYZE anser_rf_build; +ANALYZE anser_rf_probe; + +-- Use the Postgres planner for a deterministic plan shape. +SET optimizer = off; + +-- With the filter on the plan carries an "Anser Bloom Producer" under the Hash +-- and an "Anser Bloom Consumer" above the probe scan (with the planned bloom +-- size); with it off neither node appears. (COSTS OFF keeps the output stable.) +SET anser.runtime_filter = on; +EXPLAIN (COSTS OFF) +SELECT b.name, p.payload FROM anser_rf_build b LEFT JOIN anser_rf_probe p ON b.id = p.id; + +SET anser.runtime_filter = off; +EXPLAIN (COSTS OFF) +SELECT b.name, p.payload FROM anser_rf_build b LEFT JOIN anser_rf_probe p ON b.id = p.id; + +-- Correctness: identical results with the filter on vs off (Postgres planner). +SET anser.runtime_filter = on; +CREATE TEMP TABLE anser_rf_r_on AS + SELECT b.name, p.payload FROM anser_rf_build b LEFT JOIN anser_rf_probe p ON b.id = p.id + DISTRIBUTED BY (name); +SET anser.runtime_filter = off; +CREATE TEMP TABLE anser_rf_r_off AS + SELECT b.name, p.payload FROM anser_rf_build b LEFT JOIN anser_rf_probe p ON b.id = p.id + DISTRIBUTED BY (name); + +SELECT count(*) AS rows_on FROM anser_rf_r_on; +SELECT count(*) AS rows_off FROM anser_rf_r_off; +SELECT count(*) AS only_on + FROM (SELECT * FROM anser_rf_r_on EXCEPT ALL SELECT * FROM anser_rf_r_off) d; +SELECT count(*) AS only_off + FROM (SELECT * FROM anser_rf_r_off EXCEPT ALL SELECT * FROM anser_rf_r_on) d; + +-- Correctness must also hold under ORCA (whether or not it injects the nodes). +SET optimizer = on; +SET anser.runtime_filter = on; +CREATE TEMP TABLE anser_rf_r_orca AS + SELECT b.name, p.payload FROM anser_rf_build b LEFT JOIN anser_rf_probe p ON b.id = p.id + DISTRIBUTED BY (name); +SELECT count(*) AS only_orca + FROM (SELECT * FROM anser_rf_r_orca EXCEPT ALL SELECT * FROM anser_rf_r_off) d; +SELECT count(*) AS only_off_orca + FROM (SELECT * FROM anser_rf_r_off EXCEPT ALL SELECT * FROM anser_rf_r_orca) d; + +-- Pushdown mix: with gp_enable_runtime_filter_pushdown on, the consumer hands +-- the unioned filter to the probe SeqScan as an SK_BLOOM_FILTER scan key and +-- the scan (or the table AM) does the pruning. Results must still match the +-- filter-off run. +SET optimizer = off; +SET gp_enable_runtime_filter_pushdown = on; +SET anser.runtime_filter = on; +CREATE TEMP TABLE anser_rf_r_push AS + SELECT b.name, p.payload FROM anser_rf_build b LEFT JOIN anser_rf_probe p ON b.id = p.id + DISTRIBUTED BY (name); +SELECT count(*) AS only_push + FROM (SELECT * FROM anser_rf_r_push EXCEPT ALL SELECT * FROM anser_rf_r_off) d; +SELECT count(*) AS only_off_push + FROM (SELECT * FROM anser_rf_r_off EXCEPT ALL SELECT * FROM anser_rf_r_push) d; +RESET gp_enable_runtime_filter_pushdown; + + +-- Everything above checks that the nodes were injected and that the results did +-- not change. Both of those also hold when the filter never arrives and every +-- consumer fails open -- which is how a broken exchange passes a green test. +-- This is the assertion that the filter was used: the probe side holds 2000 +-- rows of which 200 join, so a filter that arrived removes the rest. Only the +-- Postgres planner is asserted, because its plan shape here is fixed; ORCA +-- chooses and the count would not be a stable thing to require. +CREATE FUNCTION anser_rf_pruned(q text) RETURNS bigint +LANGUAGE plpgsql AS $$ +DECLARE + line text; + removed bigint := 0; +BEGIN + FOR line IN EXECUTE 'EXPLAIN (ANALYZE, TIMING OFF, COSTS OFF) ' || q LOOP + IF line ~ 'Rows Removed by (Bloom Filter|Pushdown Runtime Filter): ' THEN + removed := removed + coalesce(substring(line from ': *([0-9]+)')::bigint, 0); + END IF; + END LOOP; + RETURN removed; +END; +$$; +SET optimizer = off; +SET anser.runtime_filter = on; +SELECT 'the filter pruned nothing: it never arrived, or it arrived empty' AS problem + WHERE anser_rf_pruned('SELECT b.name, p.payload FROM anser_rf_build b LEFT JOIN anser_rf_probe p ON b.id = p.id') = 0; +DROP FUNCTION anser_rf_pruned(text); + +DROP TABLE anser_rf_r_push; +DROP TABLE anser_rf_r_on, anser_rf_r_off, anser_rf_r_orca; +DROP TABLE anser_rf_build, anser_rf_probe; + +-- Datatype guard: producer and consumer hash the raw Datum bytes, so injection +-- is restricted to keys where SQL equality is bitwise Datum equality. A +-- cross-type join (float4 vs float8 Datums for equal values differ) must not +-- be injected; results must match the filter-off run either way. +CREATE TABLE anser_rf_build_f (id float8, name text) DISTRIBUTED BY (name); +CREATE TABLE anser_rf_probe_f (id float4, payload text) DISTRIBUTED BY (id); +INSERT INTO anser_rf_build_f SELECT g, 'b' || g FROM generate_series(1, 200) g; +INSERT INTO anser_rf_probe_f SELECT g, 'p' || g FROM generate_series(1, 2000) g; +ANALYZE anser_rf_build_f; +ANALYZE anser_rf_probe_f; + +SET anser.runtime_filter = on; +CREATE TEMP TABLE anser_rf_f_on AS + SELECT b.name, p.payload FROM anser_rf_build_f b JOIN anser_rf_probe_f p ON b.id = p.id + DISTRIBUTED BY (name); +SET anser.runtime_filter = off; +CREATE TEMP TABLE anser_rf_f_off AS + SELECT b.name, p.payload FROM anser_rf_build_f b JOIN anser_rf_probe_f p ON b.id = p.id + DISTRIBUTED BY (name); + +SELECT count(*) AS rows_on FROM anser_rf_f_on; +SELECT count(*) AS only_on + FROM (SELECT * FROM anser_rf_f_on EXCEPT ALL SELECT * FROM anser_rf_f_off) d; +SELECT count(*) AS only_off + FROM (SELECT * FROM anser_rf_f_off EXCEPT ALL SELECT * FROM anser_rf_f_on) d; + +DROP TABLE anser_rf_f_on, anser_rf_f_off, anser_rf_build_f, anser_rf_probe_f; + +-- Same-typed float keys are also excluded: -0.0 and 0.0 compare equal in SQL +-- but are not bitwise equal, so hashing raw Datums could prune a joinable row. +CREATE TABLE anser_rf_build_z (id float8, name text) DISTRIBUTED BY (name); +CREATE TABLE anser_rf_probe_z (id float8, payload text) DISTRIBUTED BY (id); +INSERT INTO anser_rf_build_z VALUES (1, 'b1'), (-0.0, 'bz'); +INSERT INTO anser_rf_probe_z VALUES (1, 'p1'), (0.0, 'pz'), (2, 'p2'); +ANALYZE anser_rf_build_z; +ANALYZE anser_rf_probe_z; + +SET anser.runtime_filter = on; +CREATE TEMP TABLE anser_rf_z_on AS + SELECT b.name, p.payload FROM anser_rf_build_z b JOIN anser_rf_probe_z p ON b.id = p.id + DISTRIBUTED BY (name); +SET anser.runtime_filter = off; +CREATE TEMP TABLE anser_rf_z_off AS + SELECT b.name, p.payload FROM anser_rf_build_z b JOIN anser_rf_probe_z p ON b.id = p.id + DISTRIBUTED BY (name); + +SELECT count(*) AS rows_on FROM anser_rf_z_on; +SELECT count(*) AS only_on + FROM (SELECT * FROM anser_rf_z_on EXCEPT ALL SELECT * FROM anser_rf_z_off) d; +SELECT count(*) AS only_off + FROM (SELECT * FROM anser_rf_z_off EXCEPT ALL SELECT * FROM anser_rf_z_on) d; + +DROP TABLE anser_rf_z_on, anser_rf_z_off, anser_rf_build_z, anser_rf_probe_z; + +-- --------------------------------------------------------------------------- +-- Parallel execution. +-- +-- A parallel slice runs numsegments * parallel_workers processes and every one +-- of them publishes its own part, so the coordinator must wait for that many +-- parts rather than for one per segment. Undercounting completes the channel +-- early and delivers a filter missing build keys; a bloom filter short of keys +-- gives false negatives, which silently drops joinable rows. So the assertion +-- that matters is not the plan shape but that results are *identical* with the +-- filter on and off -- at more than one worker count, and under both planners, +-- which disagree here: for one query the Postgres planner may give a serial +-- build slice where GPORCA gives a parallel one. +-- +-- The join key is the distribution key on both sides, so no Motion is needed +-- to co-locate the join. That is the only shape either planner parallelises: +-- a redistributed or broadcast build side is fine for GPORCA but the Postgres +-- planner marks Motion paths parallel-unsafe. +-- --------------------------------------------------------------------------- +CREATE TABLE anser_rf_par_build (id int, v text) DISTRIBUTED BY (id); +CREATE TABLE anser_rf_par_probe (id int, pad text) DISTRIBUTED BY (id); +INSERT INTO anser_rf_par_build SELECT g, 'b' || g FROM generate_series(1, 500) g; +INSERT INTO anser_rf_par_probe SELECT g, 'p' || g FROM generate_series(1, 20000) g; +ANALYZE anser_rf_par_build; +ANALYZE anser_rf_par_probe; + +-- Force a parallel plan on tables this small, the way PostgreSQL's own tests +-- do; without this the scans are far too cheap to be worth splitting. +SET enable_parallel = on; +SET min_parallel_table_scan_size = 0; +SET parallel_setup_cost = 0; +SET parallel_tuple_cost = 0; + +-- Postgres planner, serial baseline +SET optimizer = off; +SET max_parallel_workers_per_gather = 0; +SET anser.runtime_filter = on; +CREATE TEMP TABLE anser_rf_par_on AS + SELECT b.v, p.pad FROM anser_rf_par_probe p + JOIN anser_rf_par_build b ON p.id = b.id + DISTRIBUTED BY (v); +SET anser.runtime_filter = off; +CREATE TEMP TABLE anser_rf_par_off AS + SELECT b.v, p.pad FROM anser_rf_par_probe p + JOIN anser_rf_par_build b ON p.id = b.id + DISTRIBUTED BY (v); +SELECT (SELECT count(*) FROM anser_rf_par_on) AS rows_on, + (SELECT count(*) FROM (SELECT * FROM anser_rf_par_on + EXCEPT ALL + SELECT * FROM anser_rf_par_off) d) AS only_on, + (SELECT count(*) FROM (SELECT * FROM anser_rf_par_off + EXCEPT ALL + SELECT * FROM anser_rf_par_on) d) AS only_off; +DROP TABLE anser_rf_par_on, anser_rf_par_off; + +-- Postgres planner, parallel +SET optimizer = off; +SET max_parallel_workers_per_gather = 4; +SET anser.runtime_filter = on; +CREATE TEMP TABLE anser_rf_par_on AS + SELECT b.v, p.pad FROM anser_rf_par_probe p + JOIN anser_rf_par_build b ON p.id = b.id + DISTRIBUTED BY (v); +SET anser.runtime_filter = off; +CREATE TEMP TABLE anser_rf_par_off AS + SELECT b.v, p.pad FROM anser_rf_par_probe p + JOIN anser_rf_par_build b ON p.id = b.id + DISTRIBUTED BY (v); +SELECT (SELECT count(*) FROM anser_rf_par_on) AS rows_on, + (SELECT count(*) FROM (SELECT * FROM anser_rf_par_on + EXCEPT ALL + SELECT * FROM anser_rf_par_off) d) AS only_on, + (SELECT count(*) FROM (SELECT * FROM anser_rf_par_off + EXCEPT ALL + SELECT * FROM anser_rf_par_on) d) AS only_off; +DROP TABLE anser_rf_par_on, anser_rf_par_off; + +-- GPORCA, serial baseline +SET optimizer = on; +SET max_parallel_workers_per_gather = 0; +SET anser.runtime_filter = on; +CREATE TEMP TABLE anser_rf_par_on AS + SELECT b.v, p.pad FROM anser_rf_par_probe p + JOIN anser_rf_par_build b ON p.id = b.id + DISTRIBUTED BY (v); +SET anser.runtime_filter = off; +CREATE TEMP TABLE anser_rf_par_off AS + SELECT b.v, p.pad FROM anser_rf_par_probe p + JOIN anser_rf_par_build b ON p.id = b.id + DISTRIBUTED BY (v); +SELECT (SELECT count(*) FROM anser_rf_par_on) AS rows_on, + (SELECT count(*) FROM (SELECT * FROM anser_rf_par_on + EXCEPT ALL + SELECT * FROM anser_rf_par_off) d) AS only_on, + (SELECT count(*) FROM (SELECT * FROM anser_rf_par_off + EXCEPT ALL + SELECT * FROM anser_rf_par_on) d) AS only_off; +DROP TABLE anser_rf_par_on, anser_rf_par_off; + +-- GPORCA, parallel +SET optimizer = on; +SET max_parallel_workers_per_gather = 4; +SET anser.runtime_filter = on; +CREATE TEMP TABLE anser_rf_par_on AS + SELECT b.v, p.pad FROM anser_rf_par_probe p + JOIN anser_rf_par_build b ON p.id = b.id + DISTRIBUTED BY (v); +SET anser.runtime_filter = off; +CREATE TEMP TABLE anser_rf_par_off AS + SELECT b.v, p.pad FROM anser_rf_par_probe p + JOIN anser_rf_par_build b ON p.id = b.id + DISTRIBUTED BY (v); +SELECT (SELECT count(*) FROM anser_rf_par_on) AS rows_on, + (SELECT count(*) FROM (SELECT * FROM anser_rf_par_on + EXCEPT ALL + SELECT * FROM anser_rf_par_off) d) AS only_on, + (SELECT count(*) FROM (SELECT * FROM anser_rf_par_off + EXCEPT ALL + SELECT * FROM anser_rf_par_on) d) AS only_off; +DROP TABLE anser_rf_par_on, anser_rf_par_off; + +DROP TABLE anser_rf_par_build, anser_rf_par_probe; +RESET enable_parallel; +RESET min_parallel_table_scan_size; +RESET parallel_setup_cost; +RESET parallel_tuple_cost; +RESET max_parallel_workers_per_gather; + +RESET anser.runtime_filter; +RESET optimizer; +RESET enable_nestloop; +RESET enable_mergejoin; + diff --git a/gpcontrib/anser/sql/anser_test.sql b/gpcontrib/anser/sql/anser_test.sql new file mode 100644 index 00000000000..0c6e3585c89 --- /dev/null +++ b/gpcontrib/anser/sql/anser_test.sql @@ -0,0 +1,184 @@ +CREATE EXTENSION anser_test; + +-- Bloom payload protocol: serialize one part and read it back. +SELECT anser_test_bloom_roundtrip('bf_roundtrip', 42); + +-- In-place fold: same-size union mutates the accumulator; a mismatched size is +-- rejected. This is the coordinator's only combine path (the first part is +-- kept verbatim, every later part folds into it). +SELECT anser_test_bloom_fold_inplace() AS fold_inplace_ok; + +-- Safety regression: the consumer rebuilds the filter from its own parameters +-- and requires the received bitset to be exactly the expected size (and the +-- header magic to match); truncated/oversized/corrupt parts are rejected. +SELECT anser_test_bloom_rejects_mismatch() AS reject_mismatch; + +-- Producer and consumer driven end to end in one backend: publish a part, let +-- the coordinator side merge it, then receive and query the filter. +SELECT anser_test_node_roundtrip(168); + +-- --------------------------------------------------------------------------- +-- Sizing, and the decision not to bother. +-- +-- Anser can decline to build a filter at four points, and these cases walk the +-- boundary of each. Declining is always safe: it costs the query its filter, +-- never its correctness. The payload cap is pinned here because every size +-- below follows from it. +-- --------------------------------------------------------------------------- +SET anser.max_info_size = 68157440; + +-- 1. The planner's gate -- the only one that costs nothing, since nothing is +-- injected and no consumer is left waiting. A 64 MB bitset is 536870912 bits, +-- so 134217728 estimated keys is exactly 4 bits/key and one more key is not. +-- A zero or negative estimate is treated as one key, not as "no filter". +SELECT t.est_rows, s.injected, s.total_elems, s.planned_bytes +FROM (VALUES (0), (-5), (1), (1000), (1000000), (10000000), + (134217728), (134217729), (150000000), (1000000000)) + AS t(est_rows), + LATERAL anser_test_rf_size(t.est_rows) AS s; + +-- 2. Filter construction. bloom_create floors every bitset at 1 MB and that +-- floor overrides the work_mem cap, so a cap below "1 MB + header" can only +-- yield a filter too large to send -- which is why a flat 1 MB cap is refused +-- while 1048592 (the floor plus exactly one header) is accepted and serializes +-- to precisely the cap. fits_cap is the invariant that must never be false. +SELECT t.what, t.elems, t.cap, s.built, s.bits, s.serialized, + round(s.bits_per_key::numeric, 3) AS bits_per_key, + (NOT s.built OR s.serialized <= t.cap) AS fits_cap +FROM (VALUES + ('no keys', 0::bigint, 1048640::bigint), + ('negative keys', -1, 1048640), + ('cap equals the header', 32, 16), + ('cap one byte over the header', 32, 17), + ('cap is 1 MB, no header room', 32, 1048576), + ('cap is 1 MB plus a header', 32, 1048592), + ('cap one byte short of that', 32, 1048591), + ('a single key', 1, 1048640), + ('exactly 4 bits/key', 2097152, 1048640), + ('one key too many', 2097153, 1048640), + ('exactly 4 bits/key at 2 MB', 4194304, 2097216), + ('one key too many at 2 MB', 4194305, 2097216), + ('a billion keys', 1000000000, 68157440)) + AS t(what, elems, cap), + LATERAL anser_test_bloom_shape(t.elems, t.cap) AS s; + +-- 3. The coordinator's gate on a merged payload. The fill is fixed exactly by +-- setting whole bytes rather than by inserting keys, which would be both slow +-- and only statistically precise. The rule is "reject above 95%", so a payload +-- sitting exactly on the limit is still delivered. Malformed framing is not +-- worth delivering either, since it cannot be a filter at all. +SELECT t.what, t.bitset_bytes, t.bytes_set, t.damage, + anser_test_worth_delivering(t.bitset_bytes, t.bytes_set, t.damage) + AS worth_delivering +FROM (VALUES + ('empty', 1000, 0, ''), + ('half full', 1000, 500, ''), + ('just under the limit', 1000, 949, ''), + ('exactly on the limit', 1000, 950, ''), + ('just over the limit', 1000, 951, ''), + ('completely full', 1000, 1000, ''), + ('no bitset at all', 0, 0, ''), + ('corrupt magic', 1000, 0, 'magic'), + ('corrupt version', 1000, 0, 'version'), + ('zero part count', 1000, 0, 'parts'), + ('null payload', 1000, 0, 'null')) + AS t(what, bitset_bytes, bytes_set, damage); + +-- 4. The producer driven end to end on the coordinator-local path: build, +-- insert, publish, consume. "no-filter:cancelled" is the case that matters +-- most -- construction declined, so the cancel goes out before the build side +-- is scanned and no consumer waits for its timeout. The last row is the one +-- the planner cannot predict: a filter sized for 32 keys that receives three +-- million, which saturates and so publishes a cancel instead of a payload. +SELECT t.what, + anser_test_producer_decision(t.elems, t.cap, t.n_keys) AS decision +FROM (VALUES + ('sparse filter', 32::bigint, 1048640::bigint, 100), + ('cap with no header room', 32, 1048576, 100), + ('no keys declared', 0, 1048640, 0), + ('one key past the floor', 2097153, 1048640, 0), + ('exactly 4 bits/key', 2097152, 1048640, 100000), + ('estimate of 32, three million inserted', 32, 1048640, 3000000)) + AS t(what, elems, cap, n_keys); + +-- --------------------------------------------------------------------------- +-- The wire protocol. +-- +-- Not exhaustive; a fuzzer would be the right tool for that. These cover the +-- properties that are cheap to check and expensive to lose: a fixed-width +-- header with the fields where they are documented, a payload that cannot be +-- mistaken for framing, a length cross-check that rejects a message which does +-- not add up, and a checksum that rejects one altered byte anywhere it covers. +-- --------------------------------------------------------------------------- + +-- The bytes, exactly. Session/command/condition are 42/7/3 and part is 1 of 3, +-- so these are golden values: any change to the layout, the field widths or +-- what the checksum covers shows up here as a diff. Note the third row -- a +-- cancelled message carries no body even though one was passed, and its +-- checksum must be computed over what is actually sent. +-- +-- That the message comes back as text is itself the check that it is NUL-free, +-- which a NOTIFY payload has to be: the fourth row's body is 00 ff 00. +SELECT t.what, length(m.msg) AS len, m.msg +FROM (VALUES + ('part with a 3-byte body', 'P', 'B', 0, '\x616263'::bytea), + ('subscription, no body', 'S', '-', 0, '\x'::bytea), + ('cancelled: body suppressed', 'P', 'B', 1, '\x616263'::bytea), + ('body of NUL and 0xff bytes', 'P', 'B', 0, '\x00ff00'::bytea)) + AS t(what, kind, ptype, flags, body), + LATERAL (SELECT anser_test_wire_format(t.kind::"char", t.ptype::"char", + t.flags, 'anser_rf_3', t.body) + AS msg) m; + +-- Alter exactly one byte (or one length) and see what the reader does. +-- +-- The split matters: "malformed" is rejected by framing, before a byte is +-- allocated or a channel created, while "checksum-mismatch" got past framing +-- and was caught by the CRC. Corrupting the kind byte is the interesting one +-- -- the message stays perfectly well-formed, and only the checksum notices, +-- which is why the checksum covers the header and not just the payload. +SELECT t.tamper, + anser_test_wire_roundtrip('anser_rf_3', '\x616263'::bytea, t.tamper, + 'B'::"char") AS outcome +FROM (VALUES (''), ('truncate'), ('append'), ('bodylen'), ('tag'), ('kind'), + ('type'), ('crc'), ('key'), ('body')) AS t(tamper); + +-- Keys and bodies that must survive the trip unchanged. "ok" means every +-- field and the decoded body came back identical, so each of these is a case +-- where a naive framing would have gone wrong: the key travels as raw bytes +-- and is taken by length, which is what lets it contain a newline, a space, or +-- the protocol's own tag without any escaping. +SELECT t.what, + anser_test_wire_roundtrip(t.key, t.body, '', 'B'::"char") AS outcome +FROM (VALUES + ('plain key', 'anser_rf_3' , '\x616263'::bytea), + ('key containing a newline', E'rf:a\nb' , '\x616263'::bytea), + ('key that looks like a header', 'anser3 P B 0000000000', '\x616263'::bytea), + ('key with spaces and quotes', 'rf:"a b".c' , '\x616263'::bytea), + ('key at the 63-byte limit', repeat('k', 63) , '\x616263'::bytea), + ('empty key', '' , '\x616263'::bytea), + ('empty body', 'anser_rf_3' , '\x'::bytea), + ('body of a single NUL', 'anser_rf_3' , '\x00'::bytea), + ('body of framing-hostile bytes', 'anser_rf_3' , '\x00ff0a0d5c22'::bytea), + ('1 KB body', 'anser_rf_3' , decode(repeat('00ff', 512), 'hex'))) + AS t(what, key, body); + +-- The QD -> QE checksum. The value itself does not matter; which inputs +-- change it does. Every routing field and the key are always covered, and the +-- body only for a payload type that asks for it -- the fifth column is that +-- deliberate exemption, not an oversight. +SELECT + anser_test_push_crc('B'::"char", 1, 0, 'k', '\x01') + <> anser_test_push_crc('B'::"char", 2, 0, 'k', '\x01') AS condition_covered, + anser_test_push_crc('B'::"char", 1, 0, 'k', '\x01') + <> anser_test_push_crc('B'::"char", 1, 1, 'k', '\x01') AS flags_covered, + anser_test_push_crc('B'::"char", 1, 0, 'k', '\x01') + <> anser_test_push_crc('B'::"char", 1, 0, 'j', '\x01') AS key_covered, + anser_test_push_crc('B'::"char", 1, 0, 'k', '\x01') + <> anser_test_push_crc('B'::"char", 1, 0, 'k', '\x02') AS bloom_body_covered, + anser_test_push_crc('-'::"char", 1, 0, 'k', '\x01') + = anser_test_push_crc('-'::"char", 1, 0, 'k', '\x02') AS other_body_exempt, + anser_test_push_crc('B'::"char", 1, 0, 'k', '\x01') + <> anser_test_push_crc('-'::"char", 1, 0, 'k', '\x01') AS type_covered; + +DROP EXTENSION anser_test; diff --git a/gpcontrib/anser/src/anser_test.c b/gpcontrib/anser/src/anser_test.c new file mode 100644 index 00000000000..456d1a4a004 --- /dev/null +++ b/gpcontrib/anser/src/anser_test.c @@ -0,0 +1,765 @@ +/*------------------------------------------------------------------------- + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * anser_test.c + * SQL-callable test helpers for the Anser subsystem. + * + * IDENTIFICATION + * gpcontrib/anser/src/anser_test.c + * + *------------------------------------------------------------------------- + */ +#include "postgres.h" + +#include "access/htup_details.h" +#include "anser.h" +#include "anserbloom.h" +#include "anserfilter.h" +#include "anserpayload.h" +#include "anserplan.h" +#include "ansersideband.h" +#include "cdb/cdbvars.h" +#include "common/base64.h" +#include "fmgr.h" +#include "funcapi.h" +#include "lib/bloomfilter.h" +#include "miscadmin.h" +#include "utils/builtins.h" +#include "varatt.h" + +/* + * Bloom sizing used by the test helpers. bloom_create floors every filter at + * 1 MB, so these are the smallest filters we can build; producer and consumer + * sides must pass the identical pair (that is the whole point of carrying the + * parameters in the node rather than on the wire). + */ +#define ANSER_TEST_ELEMS 32 + +/* + * bloom_create floors every bitset at 1 MB, so a payload cap must be 1 MB + * *plus* room for the serialized-part header -- the same allowance the planner + * makes (ANSER_RF_HEADER_ROOM). Passing a flat 1 MB asks AnserBloomCreate for + * a filter that cannot fit the cap it was given, and it now declines. + */ +#define ANSER_TEST_MAX_PAYLOAD (1024 * 1024 + 64) + +PG_FUNCTION_INFO_V1(anser_test_bloom_roundtrip); +PG_FUNCTION_INFO_V1(anser_test_bloom_fold_inplace); +PG_FUNCTION_INFO_V1(anser_test_bloom_rejects_mismatch); +PG_FUNCTION_INFO_V1(anser_test_node_roundtrip); + + +Datum +anser_test_bloom_roundtrip(PG_FUNCTION_ARGS) +{ + char *key = text_to_cstring(PG_GETARG_TEXT_PP(0)); + int32 value_arg = PG_GETARG_INT32(1); + Datum value = Int32GetDatum(value_arg); + uint64 seed = AnserBloomSeed(key); + bloom_filter *filter; + bloom_filter *roundtrip; + char *payload; + Size payload_size; + Size payload_len = 0; + uint32 part_index = 0; + uint32 total_parts = 0; + bool lacks; + + filter = AnserBloomCreate(ANSER_TEST_ELEMS, ANSER_TEST_MAX_PAYLOAD, seed); + if (filter == NULL) + PG_RETURN_BOOL(false); + + bloom_add_element(filter, (unsigned char *) &value, sizeof(Datum)); + payload_size = AnserBloomSerializedSize(filter); + payload = palloc(payload_size); + if (!AnserBloomSerializePart(filter, 0, 1, payload, payload_size, + &payload_len)) + PG_RETURN_BOOL(false); + + roundtrip = AnserBloomDeserializePart(payload, payload_len, + 32, 1024 * 1024, seed, + &part_index, &total_parts); + if (roundtrip == NULL) + PG_RETURN_BOOL(false); + + lacks = bloom_lacks_element(roundtrip, (unsigned char *) &value, + sizeof(Datum)); + bloom_free(filter); + bloom_free(roundtrip); + PG_RETURN_BOOL(!lacks && part_index == 0 && total_parts == 1); +} + +/* + * In-place fold: folding an equally-sized part into a merged part is a bitwise + * OR of the bitset plus a fold-count bump, mutating the buffer without realloc. + * This is the coordinator's only combine path: the first part is stored + * verbatim, every later part folds in here. A differently-sized part is + * rejected and leaves the accumulator untouched. + */ +Datum +anser_test_bloom_fold_inplace(PG_FUNCTION_ARGS) +{ + uint64 seed = AnserBloomSeed("inplace_bloom"); + bloom_filter *left; + bloom_filter *right; + bloom_filter *big; + bloom_filter *merged; + Datum left_value = Int32GetDatum(7); + Datum right_value = Int32GetDatum(9); + char *acc; + char *part; + char *big_part; + Size acc_size; + Size part_size; + Size big_size; + Size acc_len = 0; + Size part_len = 0; + Size big_len = 0; + uint32 part_index = 0; + uint32 total_parts = 0; + uint32 tp_before = 0; + uint32 tp_after = 0; + bool same_ok; + bool mismatch_rejected; + + /* Two same-parameter parts: acc is the running merged part, part folds in. */ + left = AnserBloomCreate(ANSER_TEST_ELEMS, ANSER_TEST_MAX_PAYLOAD, seed); + right = AnserBloomCreate(ANSER_TEST_ELEMS, ANSER_TEST_MAX_PAYLOAD, seed); + if (left == NULL || right == NULL) + PG_RETURN_BOOL(false); + bloom_add_element(left, (unsigned char *) &left_value, sizeof(Datum)); + bloom_add_element(right, (unsigned char *) &right_value, sizeof(Datum)); + acc_size = AnserBloomSerializedSize(left); + part_size = AnserBloomSerializedSize(right); + acc = palloc(acc_size); + part = palloc(part_size); + if (!AnserBloomSerializePart(left, 0, 1, acc, acc_size, &acc_len) || + !AnserBloomSerializePart(right, 0, 1, part, part_size, &part_len)) + { + bloom_free(left); + bloom_free(right); + PG_RETURN_BOOL(false); + } + bloom_free(left); + bloom_free(right); + + same_ok = AnserBloomFoldPartInPlace(acc, acc_len, part, part_len); + merged = same_ok ? + AnserBloomDeserializePart(acc, acc_len, 32, 1024 * 1024, seed, + &part_index, &total_parts) : NULL; + same_ok = same_ok && + acc_len == acc_size && /* size unchanged, folded in place */ + merged != NULL && + part_index == 0 && + total_parts == 2 && /* one more part folded */ + !bloom_lacks_element(merged, (unsigned char *) &left_value, + sizeof(Datum)) && + !bloom_lacks_element(merged, (unsigned char *) &right_value, + sizeof(Datum)); + if (merged != NULL) + bloom_free(merged); + + /* + * A differently-sized part must be rejected and leave acc untouched. Since + * bloom_create floors every filter at 1 MB, we need a genuinely larger + * cardinality/budget to get a bigger (2 MB) bitset than the 1 MB acc. + */ + big = AnserBloomCreate(1500000, 4 * 1024 * 1024, seed); + if (big == NULL) + PG_RETURN_BOOL(false); + big_size = AnserBloomSerializedSize(big); + big_part = palloc(big_size); + if (!AnserBloomSerializePart(big, 0, 1, big_part, big_size, &big_len)) + { + bloom_free(big); + PG_RETURN_BOOL(false); + } + bloom_free(big); + + tp_before = ((const AnserBloomPartHeader *) acc)->total_parts; + mismatch_rejected = big_len != acc_len && + !AnserBloomFoldPartInPlace(acc, acc_len, big_part, big_len); + tp_after = ((const AnserBloomPartHeader *) acc)->total_parts; + mismatch_rejected = mismatch_rejected && tp_before == tp_after; + + PG_RETURN_BOOL(same_ok && mismatch_rejected); +} + +/* + * Safety regression for the size/format check in AnserBloomDeserializePart. + * + * The consumer rebuilds the filter from its OWN (total_elems, max_payload, seed) + * parameters, then requires the received bitset to be exactly the size those + * parameters imply and the wire header to carry the expected magic. A + * well-formed part must load; a truncated one, an oversized one, and one with a + * corrupted magic must all be rejected (NULL) so the consumer fails open rather + * than loading a wrongly-shaped bitset. Returns true iff the good part loads and + * every bad one is rejected. + */ +Datum +anser_test_bloom_rejects_mismatch(PG_FUNCTION_ARGS) +{ + uint64 seed = AnserBloomSeed("reject_mismatch"); + bloom_filter *filter; + char *good; + Size good_size; + Size good_len = 0; + bloom_filter *ok_load; + bloom_filter *short_load; + bloom_filter *long_load; + bloom_filter *magic_load; + AnserBloomPartHeader *hdr; + uint32 saved_magic; + bool ok; + + filter = AnserBloomCreate(ANSER_TEST_ELEMS, ANSER_TEST_MAX_PAYLOAD, seed); + if (filter == NULL) + PG_RETURN_BOOL(false); + + good_size = AnserBloomSerializedSize(filter); + good = palloc(good_size); + if (!AnserBloomSerializePart(filter, 0, 1, good, good_size, &good_len)) + { + bloom_free(filter); + PG_RETURN_BOOL(false); + } + bloom_free(filter); + + /* Well-formed: loads. */ + ok_load = AnserBloomDeserializePart(good, good_len, ANSER_TEST_ELEMS, + ANSER_TEST_MAX_PAYLOAD, seed, NULL, NULL); + + /* One byte short of the expected bitset: rejected. */ + short_load = AnserBloomDeserializePart(good, good_len - 1, ANSER_TEST_ELEMS, + ANSER_TEST_MAX_PAYLOAD, seed, NULL, NULL); + + /* Claiming more bytes than the expected bitset: rejected. */ + long_load = AnserBloomDeserializePart(good, good_len + 1, ANSER_TEST_ELEMS, + ANSER_TEST_MAX_PAYLOAD, seed, NULL, NULL); + + /* Corrupted wire magic: rejected before the size check. */ + hdr = (AnserBloomPartHeader *) good; + saved_magic = hdr->magic; + hdr->magic = saved_magic ^ 0xFFFFFFFFU; + magic_load = AnserBloomDeserializePart(good, good_len, ANSER_TEST_ELEMS, + ANSER_TEST_MAX_PAYLOAD, seed, NULL, NULL); + hdr->magic = saved_magic; + + ok = ok_load != NULL && short_load == NULL && long_load == NULL && + magic_load == NULL; + + if (ok_load != NULL) + bloom_free(ok_load); + if (short_load != NULL) + bloom_free(short_load); + if (long_load != NULL) + bloom_free(long_load); + if (magic_load != NULL) + bloom_free(magic_load); + pfree(good); + + PG_RETURN_BOOL(ok); +} + +/* Send a cancel request for conn's in-flight query (best effort). */ +/* Printable name for a channel state ("UNKNOWN" when out of range). */ +/* + * Drive a producer and a consumer through the whole path in this one backend. + * + * Coordinator-local, so it exercises the merge, the channel table and the + * lifetime rules without needing segments; the segment half (NOTIFY out, + * sideband message in) is covered by the runtime-filter test on a cluster. + */ +Datum +anser_test_node_roundtrip(PG_FUNCTION_ARGS) +{ + AnserChannelKey key; + AnserBloomFilterProduceState *producer; + AnserBloomFilterConsumeState *consumer; + int32 value_arg = PG_GETARG_INT32(0); + Datum value = Int32GetDatum(value_arg); + bool ok = false; + + MemSet(&key, 0, sizeof(key)); + key.gp_session_id = gp_session_id; + key.gp_command_count = gp_command_count; + key.condition_id = 77; + strlcpy(key.condition_key, "node_roundtrip", ANSER_CONDITION_KEY_SIZE); + + PG_TRY(); + { + producer = ExecInitAnserBloomFilterProduce(&key, ANSER_TEST_ELEMS, + ANSER_TEST_MAX_PAYLOAD, 0, 1); + if (producer == NULL) + ok = false; + else + { + ExecAnserBloomFilterProduceAddDatum(producer, value, false); + ok = ExecAnserBloomFilterProducePublish(producer); + ExecEndAnserBloomFilterProduce(producer); + } + + if (ok) + { + consumer = ExecInitAnserBloomFilterConsume(&key, ANSER_TEST_ELEMS, + ANSER_TEST_MAX_PAYLOAD, 1); + if (consumer == NULL) + ok = false; + else + { + ok = ExecAnserBloomFilterConsume(consumer, 1000) && + ExecAnserBloomFilterConsumerGetFilter(consumer) != NULL && + ExecAnserBloomFilterConsumerReceivedParts(consumer) == 1 && + !ExecAnserBloomFilterConsumerWasCancelled(consumer) && + !bloom_lacks_element(ExecAnserBloomFilterConsumerGetFilter(consumer), + (unsigned char *) &value, + sizeof(Datum)); + ExecEndAnserBloomFilterConsume(consumer); + } + } + } + PG_FINALLY(); + { + AnserSidebandResetAll(); + } + PG_END_TRY(); + + PG_RETURN_BOOL(ok); +} + +/* + * --------------------------------------------------------------------------- + * Sizing and give-up decisions. + * + * These four take their inputs as arguments and return what Anser decided, so + * the case tables live in sql/anser_test.sql where they are readable and the + * expected output records real sizes rather than a bare "ok". Between them + * they cover each of the four points where Anser can decide not to bother: + * the planner, filter construction, publication, and the merged result. + * --------------------------------------------------------------------------- + */ + +PG_FUNCTION_INFO_V1(anser_test_rf_size); +PG_FUNCTION_INFO_V1(anser_test_bloom_shape); +PG_FUNCTION_INFO_V1(anser_test_worth_delivering); +PG_FUNCTION_INFO_V1(anser_test_producer_decision); + +/* + * The planner gate: what AnserRuntimeFilterSize decides for an estimated build + * cardinality. NULL means no filter would be injected at all. + */ +Datum +anser_test_rf_size(PG_FUNCTION_ARGS) +{ + double est_rows = PG_GETARG_FLOAT8(0); + int64 total_elems = 0; + int64 max_payload = 0; + int64 planned_bytes = 0; + bool injected; + Datum values[4] = {0, 0, 0, 0}; + bool nulls[4] = {false, false, false, false}; + TupleDesc tupdesc; + + if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE) + elog(ERROR, "anser_test_rf_size: expected a composite return type"); + tupdesc = BlessTupleDesc(tupdesc); + + injected = AnserRuntimeFilterSize(est_rows, &total_elems, &max_payload, + &planned_bytes); + + values[0] = BoolGetDatum(injected); + values[1] = Int64GetDatum(total_elems); + values[2] = Int64GetDatum(max_payload); + values[3] = Int64GetDatum(planned_bytes); + nulls[1] = nulls[2] = nulls[3] = !injected; + + PG_RETURN_DATUM(HeapTupleGetDatum(heap_form_tuple(tupdesc, values, nulls))); +} + +/* + * Filter construction: what AnserBloomCreate makes of (total_elems, cap). + * NULL means it declined -- either the smallest possible filter does not fit + * the cap, or there are too many keys for it to be worth building. + * + * Returns the realized sizes so the expected output pins down bloom_create's + * actual behaviour (its 1 MB floor, its power-of-two rounding) and not merely + * our verdict on it. + */ +Datum +anser_test_bloom_shape(PG_FUNCTION_ARGS) +{ + int64 total_elems = PG_GETARG_INT64(0); + int64 cap = PG_GETARG_INT64(1); + bloom_filter *filter; + Datum values[4] = {0, 0, 0, 0}; + bool nulls[4] = {false, false, false, false}; + TupleDesc tupdesc; + + if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE) + elog(ERROR, "anser_test_bloom_shape: expected a composite return type"); + tupdesc = BlessTupleDesc(tupdesc); + + /* A negative cap would be a Size wraparound; the SQL side never sends one. */ + if (cap < 0) + elog(ERROR, "anser_test_bloom_shape: negative cap"); + + filter = AnserBloomCreate(total_elems, (Size) cap, AnserBloomSeed("shape")); + + values[0] = BoolGetDatum(filter != NULL); + nulls[1] = nulls[2] = nulls[3] = (filter == NULL); + if (filter != NULL) + { + values[1] = Int64GetDatum((int64) bloom_total_bits(filter)); + values[2] = Int64GetDatum((int64) AnserBloomSerializedSize(filter)); + values[3] = Float8GetDatum((double) bloom_total_bits(filter) / + (double) total_elems); + bloom_free(filter); + } + + PG_RETURN_DATUM(HeapTupleGetDatum(heap_form_tuple(tupdesc, values, nulls))); +} + +/* + * The coordinator's gate, on a synthetic part. + * + * Builds a part whose bitset has 'bytes_set' of its 'bitset_bytes' bytes fully + * set, which pins the fill fraction exactly -- reaching a given fill by + * inserting keys would be both slow and only statistically precise. 'damage' + * corrupts the framing instead: 'magic', 'version', 'parts' or 'null'. + */ +Datum +anser_test_worth_delivering(PG_FUNCTION_ARGS) +{ + int32 bitset_bytes = PG_GETARG_INT32(0); + int32 bytes_set = PG_GETARG_INT32(1); + const char *damage = text_to_cstring(PG_GETARG_TEXT_PP(2)); + AnserBloomPartHeader *header; + char *payload; + Size payload_len; + + if (strcmp(damage, "null") == 0) + PG_RETURN_BOOL(AnserBloomPartWorthSending(NULL, 1024)); + + if (bitset_bytes < 0 || bytes_set < 0 || bytes_set > bitset_bytes) + elog(ERROR, "anser_test_worth_delivering: bad bitset arguments"); + + payload_len = sizeof(AnserBloomPartHeader) + (Size) bitset_bytes; + payload = palloc0(payload_len); + header = (AnserBloomPartHeader *) payload; + header->magic = ANSER_BLOOM_PART_MAGIC; + header->version = ANSER_BLOOM_PART_VERSION; + header->part_index = 0; + header->total_parts = 1; + + if (strcmp(damage, "magic") == 0) + header->magic = ANSER_BLOOM_PART_MAGIC + 1; + else if (strcmp(damage, "version") == 0) + header->version = ANSER_BLOOM_PART_VERSION + 1; + else if (strcmp(damage, "parts") == 0) + header->total_parts = 0; + else if (damage[0] != '\0') + elog(ERROR, "anser_test_worth_delivering: unknown damage \"%s\"", damage); + + if (bytes_set > 0) + memset(payload + sizeof(AnserBloomPartHeader), 0xff, (Size) bytes_set); + + PG_RETURN_BOOL(AnserBloomPartWorthSending(payload, payload_len)); +} + +/* + * Producer end to end, on the coordinator-local path: build a filter for + * (total_elems, cap), insert 'n_keys' distinct keys, publish, then consume. + * + * Returns ":", where construction is built/no-filter + * and delivery is delivered/cancelled/missing. Every combination that can + * occur says something different: + * + * built:delivered the normal case + * built:cancelled the filter saturated, so publication became a cancel + * no-filter:cancelled construction declined, cancelled before any scan + * + * Each call takes a fresh condition_id, since several rows of one query share a + * session and command counter and would otherwise collide on one channel. + */ +Datum +anser_test_producer_decision(PG_FUNCTION_ARGS) +{ + static uint32 next_condition_id = 1000; + + int64 total_elems = PG_GETARG_INT64(0); + int64 cap = PG_GETARG_INT64(1); + int32 n_keys = PG_GETARG_INT32(2); + AnserChannelKey key; + AnserBloomFilterProduceState *producer; + const char *construction; + const char *delivery; + void *payload = NULL; + Size payload_len = 0; + bool cancelled = false; + int32 i; + + MemSet(&key, 0, sizeof(key)); + key.gp_session_id = gp_session_id; + key.gp_command_count = gp_command_count; + key.condition_id = next_condition_id++; + snprintf(key.condition_key, ANSER_CONDITION_KEY_SIZE, "anser_rf_%u", + key.condition_id); + + producer = ExecInitAnserBloomFilterProduce(&key, total_elems, (Size) cap, + 0, 1); + if (producer == NULL) + PG_RETURN_TEXT_P(cstring_to_text("no-producer:missing")); + + construction = ExecAnserBloomFilterProduceHasFilter(producer) + ? "built" : "no-filter"; + + for (i = 0; i < n_keys; i++) + { + Datum value = Int32GetDatum(i); + + ExecAnserBloomFilterProduceAddDatum(producer, value, false); + } + + (void) ExecAnserBloomFilterProducePublish(producer); + ExecEndAnserBloomFilterProduce(producer); + + if (!AnserDispatchLocalConsume(&key, ANSER_PAYLOAD_BLOOM, &payload, + &payload_len, &cancelled)) + delivery = cancelled ? "cancelled" : "missing"; + else + delivery = "delivered"; + + if (payload != NULL) + pfree(payload); + + PG_RETURN_TEXT_P(cstring_to_text(psprintf("%s:%s", construction, delivery))); +} + +/* + * --------------------------------------------------------------------------- + * The wire protocol. + * + * Not exhaustive -- a fuzzer would be the right tool for that, and pg_regress + * is not one. What these cover is the part of the format that is easy to get + * wrong and expensive to get wrong: that the header is a fixed width with the + * fields where they are documented, that a payload cannot be mistaken for + * framing (the reason the newline-delimited format was replaced), that the + * length cross-check rejects a message that does not add up, and that the + * checksum rejects a single altered byte anywhere it is supposed to cover -- + * and does not reject one where it deliberately does not. + * --------------------------------------------------------------------------- + */ + +PG_FUNCTION_INFO_V1(anser_test_wire_format); +PG_FUNCTION_INFO_V1(anser_test_wire_roundtrip); +PG_FUNCTION_INFO_V1(anser_test_push_crc); + +/* Fixed channel coordinates, so the golden headers below are stable. */ +#define ANSER_TEST_WIRE_SESSION 42 +#define ANSER_TEST_WIRE_COMMAND 7 +#define ANSER_TEST_WIRE_CONDITION 3 +#define ANSER_TEST_WIRE_PART 1 +#define ANSER_TEST_WIRE_TOTAL 3 + +static void +anser_test_wire_key(AnserChannelKey *key, const char *condition_key) +{ + MemSet(key, 0, sizeof(*key)); + key->gp_session_id = ANSER_TEST_WIRE_SESSION; + key->gp_command_count = ANSER_TEST_WIRE_COMMAND; + key->condition_id = ANSER_TEST_WIRE_CONDITION; + strlcpy(key->condition_key, condition_key, ANSER_CONDITION_KEY_SIZE); +} + +/* + * The formatted message, verbatim. + * + * Returning it as text is itself a check: a NOTIFY payload travels through + * pq_sendstring and so must be free of NUL bytes, and a text Datum cannot + * carry one -- which is why the body is base64 even though the header is not. + * Pass a body containing NULs and the length in the expected output proves it. + */ +Datum +anser_test_wire_format(PG_FUNCTION_ARGS) +{ + char kind = PG_GETARG_CHAR(0); + char payload_type = PG_GETARG_CHAR(1); + int32 flags = PG_GETARG_INT32(2); + char *condition_key = text_to_cstring(PG_GETARG_TEXT_PP(3)); + bytea *body = PG_GETARG_BYTEA_PP(4); + AnserChannelKey key; + char *msg; + + anser_test_wire_key(&key, condition_key); + msg = AnserWireFormat(&key, kind, payload_type, ANSER_TEST_WIRE_PART, + ANSER_TEST_WIRE_TOTAL, flags, + VARDATA_ANY(body), VARSIZE_ANY_EXHDR(body)); + + PG_RETURN_TEXT_P(cstring_to_text(msg)); +} + +/* + * Format a message, optionally alter one byte of it, then parse it back and + * report what the reader made of it. + * + * The offsets touched are derived from the format macros, never hardcoded, so + * this keeps working if a field is added. Outcomes mirror what the notify + * handler does with each: "malformed" is rejected by framing before anything + * is allocated, "unknown-type" has no registry entry, "checksum-mismatch" is + * well-framed but altered, and "ok" means every field and the decoded body + * came back exactly as they went in. + */ +Datum +anser_test_wire_roundtrip(PG_FUNCTION_ARGS) +{ + char *condition_key = text_to_cstring(PG_GETARG_TEXT_PP(0)); + bytea *body = PG_GETARG_BYTEA_PP(1); + char *tamper = text_to_cstring(PG_GETARG_TEXT_PP(2)); + char payload_type = PG_GETARG_CHAR(3); + AnserChannelKey key; + AnserWireMsg parsed; + char *msg; + Size msg_len; + const char *raw_body = VARDATA_ANY(body); + int raw_len = VARSIZE_ANY_EXHDR(body); + char *decoded = NULL; + int decoded_len = 0; + + anser_test_wire_key(&key, condition_key); + msg = AnserWireFormat(&key, ANSER_WIRE_KIND_PART, payload_type, + ANSER_TEST_WIRE_PART, ANSER_TEST_WIRE_TOTAL, 0, + raw_len > 0 ? raw_body : NULL, (Size) raw_len); + msg_len = strlen(msg); + + /* + * One byte, or one length, altered -- see the case table in the test. An + * empty 'tamper' falls through every branch and leaves the message intact. + */ + if (strcmp(tamper, "truncate") == 0) + msg[msg_len - 1] = '\0'; + else if (strcmp(tamper, "append") == 0) + { + char *longer = palloc(msg_len + 2); + + memcpy(longer, msg, msg_len); + longer[msg_len] = 'x'; + longer[msg_len + 1] = '\0'; + msg = longer; + } + else if (strcmp(tamper, "bodylen") == 0) + { + /* Last digit of the bodylen field: it ends one space before the CRC. */ + char *digit = msg + ANSER_WIRE_CRC_OFFSET - 2; + + *digit = (*digit == '9') ? '8' : (char) (*digit + 1); + } + else if (strcmp(tamper, "tag") == 0) + msg[0] = 'x'; + else if (strcmp(tamper, "kind") == 0) + msg[sizeof(ANSER_WIRE_TAG)] = ANSER_WIRE_KIND_SUBSCRIBE; + else if (strcmp(tamper, "type") == 0) + msg[sizeof(ANSER_WIRE_TAG) + 2] = 'Z'; + else if (strcmp(tamper, "crc") == 0) + { + char *digit = msg + ANSER_WIRE_HDR_LEN - 1; + + *digit = (*digit == '0') ? '1' : '0'; + } + else if (strcmp(tamper, "key") == 0) + { + char *first = msg + ANSER_WIRE_HDR_LEN; + + *first = (*first == 'a') ? 'b' : 'a'; + } + else if (strcmp(tamper, "body") == 0) + { + /* Stay inside the base64 alphabet so this tests the CRC, not decoding. */ + char *first = msg + ANSER_WIRE_HDR_LEN + strlen(condition_key); + + *first = (*first == 'A') ? 'B' : 'A'; + } + else if (strcmp(tamper, "") != 0) + elog(ERROR, "anser_test_wire_roundtrip: unknown tamper \"%s\"", tamper); + + if (!AnserWireParse(msg, &parsed)) + PG_RETURN_TEXT_P(cstring_to_text("malformed")); + + if (AnserPayloadLookup(parsed.payload_type) == NULL) + PG_RETURN_TEXT_P(cstring_to_text("unknown-type")); + + if (parsed.body_len > 0) + { + int maxlen = pg_b64_dec_len(parsed.body_len); + + decoded = palloc(maxlen); + decoded_len = pg_b64_decode(parsed.body, parsed.body_len, decoded, + maxlen); + if (decoded_len < 0) + PG_RETURN_TEXT_P(cstring_to_text("undecodable")); + } + + if (!AnserWireCheckCrc(&parsed, decoded, (Size) decoded_len)) + PG_RETURN_TEXT_P(cstring_to_text("checksum-mismatch")); + + /* Framed and vouched for: now every field must have survived the trip. */ + if (parsed.kind != ANSER_WIRE_KIND_PART) + PG_RETURN_TEXT_P(cstring_to_text("mismatch: kind")); + if (parsed.payload_type != payload_type) + PG_RETURN_TEXT_P(cstring_to_text("mismatch: payload_type")); + if (parsed.key.gp_session_id != ANSER_TEST_WIRE_SESSION || + parsed.key.gp_command_count != ANSER_TEST_WIRE_COMMAND || + parsed.key.condition_id != ANSER_TEST_WIRE_CONDITION) + PG_RETURN_TEXT_P(cstring_to_text("mismatch: channel")); + if (parsed.part_index != ANSER_TEST_WIRE_PART || + parsed.total_parts != ANSER_TEST_WIRE_TOTAL) + PG_RETURN_TEXT_P(cstring_to_text("mismatch: part")); + if (parsed.flags != 0) + PG_RETURN_TEXT_P(cstring_to_text("mismatch: flags")); + if (strcmp(parsed.key.condition_key, condition_key) != 0) + PG_RETURN_TEXT_P(cstring_to_text("mismatch: condition_key")); + if (decoded_len != raw_len || + (raw_len > 0 && memcmp(decoded, raw_body, raw_len) != 0)) + PG_RETURN_TEXT_P(cstring_to_text("mismatch: body")); + + PG_RETURN_TEXT_P(cstring_to_text("ok")); +} + +/* + * The QD -> QE checksum, as an integer so the test can compare two of them. + * + * What matters is not the value but which inputs change it: every routing field + * and the key always, and the body only for a payload type that asks for its + * body to be covered. + */ +Datum +anser_test_push_crc(PG_FUNCTION_ARGS) +{ + char payload_type = PG_GETARG_CHAR(0); + int32 condition_id = PG_GETARG_INT32(1); + int32 flags = PG_GETARG_INT32(2); + char *condition_key = text_to_cstring(PG_GETARG_TEXT_PP(3)); + bytea *body = PG_GETARG_BYTEA_PP(4); + + PG_RETURN_INT64((int64) (uint32) + AnserWirePushCrc(payload_type, (uint32) condition_id, + (uint32) flags, condition_key, + (int) strlen(condition_key), + VARDATA_ANY(body), + (int) VARSIZE_ANY_EXHDR(body))); +} diff --git a/gpcontrib/anser/src/anserbloomconsume.c b/gpcontrib/anser/src/anserbloomconsume.c new file mode 100644 index 00000000000..694959efd2f --- /dev/null +++ b/gpcontrib/anser/src/anserbloomconsume.c @@ -0,0 +1,202 @@ +/*------------------------------------------------------------------------- + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * anserbloomconsume.c + * Standalone Anser Bloom filter consumer executor helper. + * + * IDENTIFICATION + * gpcontrib/anser/src/anserbloomconsume.c + * + *------------------------------------------------------------------------- + */ +#include "postgres.h" + +#include "anser.h" +#include "anserbloom.h" +#include "anserfilter.h" +#include "anserpayload.h" +#include "ansersideband.h" +#include "cdb/cdbvars.h" +#include "miscadmin.h" +#include "tcop/dest.h" +#include "tcop/tcopprot.h" + +/* + * State for one Bloom filter consumer. Consumes the merged payload for a + * channel exactly once (consumed), either from the coordinator's shared + * coordinator. cancelled records that the producer side aborted instead of + * delivering the payload. + */ +struct AnserBloomFilterConsumeState +{ + AnserChannelKey channel_key; + bloom_filter *filter; + int64 total_elems; /* filter sizing, shared with the producer */ + Size max_payload_bytes; + uint64 seed; + uint32 expected_parts; + uint32 received_parts; + bool consumed; + bool cancelled; +}; + +static bool ExecAnserBloomFilterConsumeSideband(AnserBloomFilterConsumeState *state, + long timeout_ms); +static bool ExecAnserBloomFilterConsumeFinish(AnserBloomFilterConsumeState *state, + void *payload, Size payload_len); + +AnserBloomFilterConsumeState * +ExecInitAnserBloomFilterConsume(const AnserChannelKey *channel_key, + int64 total_elems, Size max_payload_bytes, + uint32 expected_parts) +{ + AnserBloomFilterConsumeState *state; + + if (channel_key == NULL || expected_parts == 0) + return NULL; + + state = palloc0(sizeof(AnserBloomFilterConsumeState)); + state->channel_key = *channel_key; + state->total_elems = total_elems; + state->max_payload_bytes = max_payload_bytes; + state->seed = AnserBloomSeed(channel_key->condition_key); + state->expected_parts = expected_parts; + return state; +} + +bool +ExecAnserBloomFilterConsume(AnserBloomFilterConsumeState *state, + long registration_timeout_ms) +{ + if (state == NULL) + return false; + + if (state->consumed) + return state->filter != NULL; + + return ExecAnserBloomFilterConsumeSideband(state, registration_timeout_ms); +} + +/* + * Dispatch-connection consume path. + * + * On a segment this blocks on our own dispatch socket until the coordinator + * pushes the merged filter; on the coordinator the merge happened in this very + * process, so there is nothing to wait for and we just read it. + */ +static bool +ExecAnserBloomFilterConsumeSideband(AnserBloomFilterConsumeState *state, + long timeout_ms) +{ + void *payload = NULL; + Size payload_len = 0; + bool cancelled = false; + bool got; + + /* + * Which of the two paths a process takes is decided by Gp_role alone, and + * a process that takes the wrong one fails open without ever reaching the + * coordinator: the local path finds no channel on a segment, and the + * sideband path finds no frontend on the coordinator. Logged with the + * facts the choice rests on, because a consumer that reports no filter + * cannot otherwise be told apart from one that asked and was refused. + */ + ANSER_DEBUG("anser: seg%d consumer cond=%u taking the %s path (role=%d writer=%d port=%s dest=%d expected_parts=%u timeout=%ld)", + GpIdentity.segindex, state->channel_key.condition_id, + Gp_role == GP_ROLE_EXECUTE ? "sideband" : "local", + (int) Gp_role, Gp_is_writer ? 1 : 0, + MyProcPort != NULL ? "ok" : "NULL", + (int) whereToSendOutput, + state->expected_parts, timeout_ms); + + if (Gp_role == GP_ROLE_EXECUTE) + got = AnserSidebandConsumeWait(&state->channel_key, ANSER_PAYLOAD_BLOOM, + &payload, &payload_len, &cancelled, + timeout_ms); + else + got = AnserDispatchLocalConsume(&state->channel_key, ANSER_PAYLOAD_BLOOM, + &payload, &payload_len, &cancelled); + + if (!got || cancelled) + { + if (payload != NULL) + pfree(payload); + state->cancelled = cancelled; + state->consumed = true; + return false; + } + + return ExecAnserBloomFilterConsumeFinish(state, payload, payload_len); +} + +/* + * Turn a merged payload into this consumer's filter. + * + * The coordinator unions every segment's part into one before delivery, so a + * single chunk is deserialized rather than N; the merged header's total_parts + * records how many were folded, which we surface as the received count. + */ +static bool +ExecAnserBloomFilterConsumeFinish(AnserBloomFilterConsumeState *state, + void *payload, Size payload_len) +{ + uint32 part_index = 0; + uint32 folded = 0; + + state->filter = AnserBloomDeserializePart(payload, payload_len, + state->total_elems, + state->max_payload_bytes, + state->seed, + &part_index, &folded); + state->received_parts = (state->filter != NULL) ? folded : 0; + + if (payload != NULL) + pfree(payload); + state->consumed = true; + return state->filter != NULL; +} + +bloom_filter * +ExecAnserBloomFilterConsumerGetFilter(AnserBloomFilterConsumeState *state) +{ + return state != NULL ? state->filter : NULL; +} + +uint32 +ExecAnserBloomFilterConsumerReceivedParts(AnserBloomFilterConsumeState *state) +{ + return state != NULL ? state->received_parts : 0; +} + +bool +ExecAnserBloomFilterConsumerWasCancelled(AnserBloomFilterConsumeState *state) +{ + return state != NULL && state->cancelled; +} + +void +ExecEndAnserBloomFilterConsume(AnserBloomFilterConsumeState *state) +{ + if (state == NULL) + return; + + if (state->filter != NULL) + bloom_free(state->filter); + pfree(state); +} diff --git a/gpcontrib/anser/src/anserbloomproduce.c b/gpcontrib/anser/src/anserbloomproduce.c new file mode 100644 index 00000000000..1bf5d584439 --- /dev/null +++ b/gpcontrib/anser/src/anserbloomproduce.c @@ -0,0 +1,219 @@ +/*------------------------------------------------------------------------- + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * anserbloomproduce.c + * Standalone Anser Bloom filter producer executor helper. + * + * IDENTIFICATION + * gpcontrib/anser/src/anserbloomproduce.c + * + *------------------------------------------------------------------------- + */ +#include "postgres.h" + +#include "anser.h" +#include "anserbloom.h" +#include "anserfilter.h" +#include "anserpayload.h" +#include "ansersideband.h" +#include "cdb/cdbvars.h" + +/* + * State for a single bloom filter producer: the target channel, the filter + * being built, and this producer's identity within total_parts. published + * and cancelled guard against double publication and drive teardown. + */ +struct AnserBloomFilterProduceState +{ + AnserChannelKey channel_key; + bloom_filter *filter; + uint32 part_index; + uint32 total_parts; + bool published; + bool cancelled; +}; + +/* + * Publish one part. + * + * A segment sends it to the coordinator over the dispatch connection; a + * coordinator-local producer hands it to the same per-query channel table the + * coordinator merges into, since it has no connection to itself. total_parts + * doubles as expected_producers: each producer contributes exactly one part. + */ +static bool +AnserProducePublishPart(AnserBloomFilterProduceState *state, + const void *payload, Size payload_len, bool cancelled) +{ + if (Gp_role == GP_ROLE_EXECUTE) + return AnserSidebandPublish(&state->channel_key, ANSER_PAYLOAD_BLOOM, + state->part_index, state->total_parts, + payload, payload_len, cancelled); + + return AnserDispatchLocalPublish(&state->channel_key, ANSER_PAYLOAD_BLOOM, + state->part_index, state->total_parts, + payload, payload_len, cancelled); +} + +AnserBloomFilterProduceState * +ExecInitAnserBloomFilterProduce(const AnserChannelKey *channel_key, + int64 total_elems, + Size max_payload_bytes, + uint32 part_index, + uint32 total_parts) +{ + AnserBloomFilterProduceState *state; + uint64 seed; + + if (channel_key == NULL || total_parts == 0 || part_index >= total_parts) + return NULL; + + state = palloc0(sizeof(AnserBloomFilterProduceState)); + state->channel_key = *channel_key; + state->part_index = part_index; + state->total_parts = total_parts; + seed = AnserBloomSeed(channel_key->condition_key); + state->filter = AnserBloomCreate(total_elems, max_payload_bytes, seed); + + return state; +} + +void +ExecAnserBloomFilterProduceAddDatum(AnserBloomFilterProduceState *state, + Datum value, bool isnull) +{ + /* + * A NULL filter means AnserBloomCreate declined to build one -- too many + * keys for the payload cap. There is nothing to add to, and the node has + * already published the cancel. + */ + if (state == NULL || state->filter == NULL || state->published || isnull) + return; + + bloom_add_element(state->filter, (unsigned char *) &value, sizeof(Datum)); +} + +/* Does this producer have a filter to fill? False means "already hopeless". */ +bool +ExecAnserBloomFilterProduceHasFilter(AnserBloomFilterProduceState *state) +{ + return state != NULL && state->filter != NULL; +} + +bool +ExecAnserBloomFilterProducePublish(AnserBloomFilterProduceState *state) +{ + Size payload_size; + Size payload_len = 0; + void *payload; + bool ok; + + if (state == NULL || state->published) + { + ANSER_DEBUG("anser: publish skipped (%s)", + state == NULL ? "no producer state" : "already published"); + return false; + } + + if (state->cancelled || state->filter == NULL) + { + ANSER_DEBUG("anser: publishing a cancel (%s)", + state->cancelled ? "producer cancelled" : "no filter"); + state->published = true; + return AnserProducePublishPart(state, NULL, 0, true); + } + + /* + * Too saturated to be worth anything? Cancel rather than send. + * + * This is the check the planner cannot make: it decided the filter was + * worth building from a row estimate, and estimates are wrong. A filter + * whose bits are nearly all set matches nearly every probe row, so shipping + * it would buy the consumers a hash and k probes per row in exchange for + * almost no rows eliminated -- pure overhead on both sides. + */ + if (bloom_false_positive_rate(state->filter) > ANSER_BLOOM_MAX_FPR) + { + ANSER_DEBUG("anser: publishing a cancel: filter is %.1f%% full, est. FPR %.1f%% above the %.0f%% limit", + bloom_prop_bits_set(state->filter) * 100.0, + bloom_false_positive_rate(state->filter) * 100.0, + ANSER_BLOOM_MAX_FPR * 100.0); + state->published = true; + return AnserProducePublishPart(state, NULL, 0, true); + } + + ANSER_DEBUG("anser: filter is %.1f%% full, est. FPR %.2f%%", + bloom_prop_bits_set(state->filter) * 100.0, + bloom_false_positive_rate(state->filter) * 100.0); + + /* + * Serialize as a self-contained single part (index 0 of 1). The coordinator + * stores the first part verbatim and OR-folds each later part, bumping the + * merged header's fold count, so the final count reflects how many parts were + * unioned. state->part_index / state->total_parts identify this producer to + * the channel (expected_producers), not the on-wire part layout. + */ + payload_size = AnserBloomSerializedSize(state->filter); + payload = palloc(payload_size); + ok = AnserBloomSerializePart(state->filter, + 0, + 1, + payload, + payload_size, + &payload_len); + if (ok) + ok = AnserProducePublishPart(state, payload, payload_len, false); + else + ANSER_DEBUG("anser: publish skipped: could not serialize part (size=%zu)", + payload_size); + + pfree(payload); + state->published = true; + return ok; +} + +bool +ExecAnserBloomFilterProduceCancel(AnserBloomFilterProduceState *state) +{ + if (state == NULL || state->published) + return false; + + state->cancelled = true; + state->published = true; + return AnserProducePublishPart(state, NULL, 0, true); +} + +/* + * Free the producer state. + * + * Deliberately does NOT publish a cancel for an unpublished producer: whether + * silence means "ran and was abandoned" or "never ran at all" is knowable only + * to the node, and the two need opposite handling (see anser_produce_end in + * anserplanexec.c). + */ +void +ExecEndAnserBloomFilterProduce(AnserBloomFilterProduceState *state) +{ + if (state == NULL) + return; + + if (state->filter != NULL) + bloom_free(state->filter); + pfree(state); +} diff --git a/gpcontrib/anser/src/anserdispatch.c b/gpcontrib/anser/src/anserdispatch.c new file mode 100644 index 00000000000..a48e3a0d1f7 --- /dev/null +++ b/gpcontrib/anser/src/anserdispatch.c @@ -0,0 +1,713 @@ +/*------------------------------------------------------------------------- + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * anserdispatch.c + * Coordinator side of the dispatch-connection transport. + * + * Everything here runs in the QD backend, reached from cdbdisp_notify_hook + * while the dispatcher drains QE messages -- which happens from inside the + * interconnect wait loop, so this code is on the query's critical path. It + * must stay cheap and must not throw for anything recoverable: an error here + * lands in a running query, whereas losing a filter only costs us an + * unfiltered scan. + * + * Because producer merge and consumer delivery both happen in this one + * process, the accumulator is an ordinary palloc'd buffer. There is no shared + * memory, no DSM segment to attach, and no separate worker to hand data to; + * parts are folded in place as they arrive, so only the final fold is on the + * critical path. + * + * Nothing here knows what a part contains. Merging is delegated to the payload + * type's fold() (anserpayload.h), and the only other thing this file asks about + * a type is whether its body is covered by the message checksum -- which is why + * it does not include anserfilter.h at all. + * + * A note on libpq linkage. The connections we write to were created by the + * copy of libpq that is statically linked into the postgres binary, and every + * symbol listed in libpq's exports.txt is deliberately made *local* in that + * binary (see the version-script hack in src/backend/Makefile) precisely so a + * module cannot end up driving one connection through two copies of libpq. + * So this file must not call the public PQ* API: linking libpq.so to obtain it + * would create exactly the mixture that hack exists to prevent. The internal + * pq* writers are not in exports.txt and so remain global in the backend, + * which is how pqPutMsgStart() and friends resolve here; the two accessors we + * would otherwise want are inlined below, straight off the struct. + * + * IDENTIFICATION + * gpcontrib/anser/src/anserdispatch.c + * + *------------------------------------------------------------------------- + */ +#include "postgres.h" + +#include "libpq-fe.h" +#include "libpq-int.h" + +#include "anser.h" +#include "anserpayload.h" +#include "ansersideband.h" +#include "cdb/cdbconn.h" +#include "cdb/cdbdisp.h" +#include "cdb/cdbdispatchresult.h" +#include "cdb/cdbvars.h" +#include "common/base64.h" +#include "lib/stringinfo.h" +#include "nodes/pg_list.h" +#include "utils/hsearch.h" +#include "utils/memutils.h" + +/* + * One channel's merge state, living for the duration of the query that created + * it. Keyed by AnserChannelKey, which must be the first field. + */ +typedef struct AnserDispChannel +{ + AnserChannelKey key; + const AnserPayloadOps *ops; /* what this carries; NULL until a part says */ + char *payload; /* merged part, or NULL before the first one */ + Size payload_len; + int parts_received; + int expected_parts; /* 0 until a part tells us; from total_parts */ + bool cancelled; + bool complete; /* every expected part folded, or cancelled */ + List *subscribers; /* PGconn * of QEs awaiting delivery */ +} AnserDispChannel; + +static HTAB *AnserDispChannels = NULL; +static MemoryContext AnserDispContext = NULL; + +static AnserDispChannel *anser_disp_lookup(const AnserChannelKey *key, bool create); +static void anser_disp_apply_part(AnserDispChannel *chan, + const AnserPayloadOps *ops, + const void *payload, Size payload_len, + int total_parts, bool cancelled); +static void anser_disp_deliver(AnserDispChannel *chan); +static bool anser_disp_push(PGconn *conn, AnserDispChannel *chan); + +/* + * PQstatus() / PQerrorMessage() equivalents. See the linkage note above for + * why these are not the real thing; libpq-int.h gives us the full struct. + */ +static inline bool +anser_conn_ok(const PGconn *conn) +{ + return conn != NULL && conn->status == CONNECTION_OK; +} + +static inline const char * +anser_conn_error(const PGconn *conn) +{ + if (conn == NULL) + return "connection pointer is NULL"; + if (PQExpBufferBroken(&conn->errorMessage)) + return "out of memory"; + return conn->errorMessage.data; +} + +/* + * Per-query state lives in its own context so it can be dropped wholesale. + * Channels are keyed by (session, command, condition), so entries from an + * earlier command in the same transaction are distinct and simply unused + * until the reset. + */ +static void +anser_disp_init(void) +{ + HASHCTL hctl; + + if (AnserDispChannels != NULL) + return; + + AnserDispContext = AllocSetContextCreate(TopMemoryContext, + "Anser dispatch transport", + ALLOCSET_DEFAULT_SIZES); + + MemSet(&hctl, 0, sizeof(hctl)); + hctl.keysize = sizeof(AnserChannelKey); + hctl.entrysize = sizeof(AnserDispChannel); + hctl.hcxt = AnserDispContext; + + AnserDispChannels = hash_create("Anser dispatch channels", 32, &hctl, + HASH_ELEM | HASH_BLOBS | HASH_CONTEXT); +} + +void +AnserDispatchReset(void) +{ + if (AnserDispChannels == NULL) + return; + + hash_destroy(AnserDispChannels); + AnserDispChannels = NULL; + MemoryContextDelete(AnserDispContext); + AnserDispContext = NULL; +} + +static AnserDispChannel * +anser_disp_lookup(const AnserChannelKey *key, bool create) +{ + AnserDispChannel *chan; + bool found; + + anser_disp_init(); + + chan = (AnserDispChannel *) hash_search(AnserDispChannels, key, + create ? HASH_ENTER : HASH_FIND, + &found); + if (chan == NULL) + return NULL; + + if (create && !found) + { + /* hash_search only fills the key; initialize the rest. */ + chan->ops = NULL; + chan->payload = NULL; + chan->payload_len = 0; + chan->parts_received = 0; + chan->expected_parts = 0; + chan->cancelled = false; + chan->complete = false; + chan->subscribers = NIL; + } + + return chan; +} + +/* + * cdbdisp_notify_hook: is this one of ours, and if so, handle it. + * + * Returns false for any notify on another channel so the dispatcher can carry + * on with its own handling. A malformed payload is consumed (it is addressed + * to us) but otherwise ignored: the affected consumers will time out and run + * unfiltered. + */ +bool +AnserDispatchNotifyHandler(struct CdbDispatchResult *dispatchResult, + struct pgNotify *notify) +{ + PGnotify *n = (PGnotify *) notify; + CdbDispatchResult *dr = (CdbDispatchResult *) dispatchResult; + int sender = (dr != NULL && dr->segdbDesc != NULL) + ? dr->segdbDesc->segindex : -99; + AnserWireMsg msg; + const AnserPayloadOps *ops; + AnserDispChannel *chan; + MemoryContext oldcxt; + + if (n == NULL || n->relname == NULL || + strcmp(n->relname, ANSER_NOTIFY_CHANNEL) != 0) + return false; + + if (n->extra == NULL || !AnserWireParse(n->extra, &msg)) + { + elog(LOG, "anser: ignoring malformed message from a segment (len=%zu)", + n->extra != NULL ? strlen(n->extra) : (size_t) 0); + return true; + } + + /* + * Resolve what the message carries before doing anything with it. An + * unregistered type means a producer was taught to send something this + * coordinator does not know how to merge -- almost certainly a payload type + * added to anserpayload.h but not to AnserPayloadTable. + */ + ops = AnserPayloadLookup(msg.payload_type); + if (ops == NULL) + { + elog(LOG, "anser: unknown payload type '%c' from seg%d (cond=%u); ignoring", + msg.payload_type, sender, msg.key.condition_id); + return true; + } + + /* + * A message with no body can be checked here, before anything is + * allocated; one with a body is checked in the PART branch below, once it + * has been decoded. + */ + if (msg.body_len == 0 && !AnserWireCheckCrc(&msg, NULL, 0)) + { + elog(LOG, "anser: ignoring corrupted message from seg%d (cond=%u kind=%c)", + sender, msg.key.condition_id, msg.kind); + return true; + } + + anser_disp_init(); + oldcxt = MemoryContextSwitchTo(AnserDispContext); + + chan = anser_disp_lookup(&msg.key, true); + if (chan == NULL) + { + MemoryContextSwitchTo(oldcxt); + return true; + } + + if (msg.kind == ANSER_WIRE_KIND_SUBSCRIBE) + { + PGconn *conn = dr->segdbDesc->conn; + + /* + * A consumer can subscribe after the channel is already complete -- + * producers on other segments may well have finished first -- so + * deliver immediately in that case rather than recording interest. + */ + ANSER_DEBUG("anser: QD subscribe cond=%u from seg%d (channel %s)", + msg.key.condition_id, sender, + chan->complete ? "complete, delivering now" : "still collecting"); + if (chan->complete) + (void) anser_disp_push(conn, chan); + else + chan->subscribers = lappend(chan->subscribers, conn); + } + else if (msg.kind == ANSER_WIRE_KIND_PART) + { + char *raw = NULL; + int raw_len = 0; + + if (!(msg.flags & ANSER_WIRE_F_CANCELLED) && msg.body_len > 0) + { + int maxlen = pg_b64_dec_len(msg.body_len); + + raw = palloc(maxlen); + raw_len = pg_b64_decode(msg.body, msg.body_len, raw, maxlen); + + /* + * Undecodable, oversized or corrupted: cancel the channel rather + * than guess. Consumers then run unfiltered, which is slower but + * correct -- folding in a part we cannot vouch for risks clearing a + * bit that should be set, and a filter missing a key silently drops + * joinable rows. + */ + if (raw_len < 0 || raw_len > gp_anser_max_info_size || + !AnserWireCheckCrc(&msg, raw, (Size) raw_len)) + { + elog(LOG, "anser: unusable part for condition %u from seg%d; cancelling channel", + msg.key.condition_id, sender); + pfree(raw); + raw = NULL; + raw_len = 0; + msg.flags |= ANSER_WIRE_F_CANCELLED; + } + } + + anser_disp_apply_part(chan, ops, raw, (Size) raw_len, msg.total_parts, + (msg.flags & ANSER_WIRE_F_CANCELLED) != 0); + ANSER_DEBUG("anser: QD %s part cond=%u from seg%d (says part %d of %d) %d/%d bytes=%d -> %s", + ops->name, msg.key.condition_id, sender, msg.part_index, + msg.total_parts, chan->parts_received, + chan->expected_parts, raw_len, + chan->cancelled ? "cancelled" : + chan->complete ? "complete" : "collecting"); + if (raw != NULL) + pfree(raw); + } + + if (chan->complete) + anser_disp_deliver(chan); + + MemoryContextSwitchTo(oldcxt); + return true; +} + +/* + * Fold one part into the channel's accumulator. + * + * The first part is kept verbatim and becomes the accumulator; later parts are + * merged into it in place by the payload type's fold(), so no part is ever + * copied twice and the accumulator is never reallocated. Nothing here knows + * what the bytes are -- for a bloom filter the fold is a bitwise OR, for a row + * count it would be a sum, and this function reads the same either way. + */ +static void +anser_disp_apply_part(AnserDispChannel *chan, const AnserPayloadOps *ops, + const void *payload, Size payload_len, int total_parts, + bool cancelled) +{ + if (chan->cancelled) + return; /* already dead; nothing to do */ + + if (chan->ops == NULL) + chan->ops = ops; + else if (chan->ops != ops) + { + /* + * Two producers disagree about what this channel carries. Merging + * across types is meaningless, so give up on the channel: consumers run + * unfiltered, which is always correct. + */ + elog(LOG, "anser: cond=%u carries both '%s' and '%s'; cancelling channel", + chan->key.condition_id, chan->ops->name, ops->name); + chan->cancelled = true; + chan->complete = true; + chan->payload = NULL; + chan->payload_len = 0; + return; + } + + if (total_parts > chan->expected_parts) + { + /* + * Take the largest count any producer claims. Letting a later part + * lower it would complete the channel early and deliver a filter + * missing another segment's keys -- a false negative, which drops + * joinable rows. Disagreement means the producers computed their + * slice width differently and is worth seeing. + */ + if (chan->expected_parts != 0) + elog(LOG, "anser: cond=%u producer count changed %d -> %d", + chan->key.condition_id, chan->expected_parts, total_parts); + chan->expected_parts = total_parts; + } + + if (cancelled) + { + chan->cancelled = true; + chan->complete = true; + chan->payload = NULL; + chan->payload_len = 0; + return; + } + + if (payload == NULL || payload_len == 0) + { + /* An empty part still counts toward completion. */ + chan->parts_received++; + } + else if (chan->payload == NULL) + { + chan->payload = palloc(payload_len); + memcpy(chan->payload, payload, payload_len); + chan->payload_len = payload_len; + chan->parts_received++; + } + else if (ops->fold != NULL && + ops->fold(chan->payload, chan->payload_len, payload, payload_len)) + { + chan->parts_received++; + } + else + { + /* + * The parts cannot be merged -- for a bloom filter, sizes or filter + * parameters disagree. That should not happen (every part on a channel + * is built from the same plan parameters), but if it does the only safe + * answer is to give up on the channel. A type with no fold() reaches + * here too, which is right: it should never have carried a body. + */ + elog(LOG, "anser: incompatible '%s' part for condition %u; cancelling channel", + ops->name, chan->key.condition_id); + chan->cancelled = true; + chan->complete = true; + chan->payload = NULL; + chan->payload_len = 0; + return; + } + + if (chan->expected_parts > 0 && chan->parts_received >= chan->expected_parts) + { + chan->complete = true; + + /* + * Every part is in, so this is the first and last chance to judge the + * merged result. A type that declines it here saves each consumer both + * the delivery and the per-row probing it would have paid for. + */ + if (chan->payload != NULL && ops->worth_delivering != NULL && + !ops->worth_delivering(chan->payload, chan->payload_len)) + { + elog(LOG, "anser: merged '%s' payload for condition %u is not worth delivering; cancelling channel", + ops->name, chan->key.condition_id); + chan->cancelled = true; + chan->payload = NULL; + chan->payload_len = 0; + } + } +} + +/* Push the finished channel to everyone waiting, then forget them. */ +static void +anser_disp_deliver(AnserDispChannel *chan) +{ + ListCell *lc; + + ANSER_DEBUG("anser: QD delivering cond=%u to %d subscriber(s)", + chan->key.condition_id, list_length(chan->subscribers)); + foreach(lc, chan->subscribers) + (void) anser_disp_push((PGconn *) lfirst(lc), chan); + + list_free(chan->subscribers); + chan->subscribers = NIL; +} + +/* + * Write one merged payload to a QE as a GP_SIDEBAND_MESSAGE. + * + * Delivery is per consumer: a write that fails costs that one segment its + * filter (it will time out and run unfiltered) and leaves the others alone. + * This is the same "try to reach every consumer" rule the shared-memory send + * service followed. + */ +static bool +anser_disp_push(PGconn *conn, AnserDispChannel *chan) +{ + int flags = chan->cancelled ? ANSER_WIRE_F_CANCELLED : 0; + int keylen = (int) strlen(chan->key.condition_key); + int paylen = chan->cancelled ? 0 : (int) chan->payload_len; + pg_crc32c crc; + char paytype; + + if (!anser_conn_ok(conn)) + return false; + + /* + * A channel that was cancelled before any part arrived has no type. That + * is fine: the delivery carries no body, and a consumer only checks the + * type of a body it actually received. + */ + paytype = chan->ops != NULL ? chan->ops->code : ANSER_PAYLOAD_NONE; + + crc = AnserWirePushCrc(paytype, chan->key.condition_id, (uint32) flags, + chan->key.condition_key, keylen, + chan->payload, paylen); + + /* + * Raw binary: pqPutnchar performs no encoding conversion, so unlike the + * QE -> QD direction this needs no base64. + */ + if (pqPutMsgStart(GP_SIDEBAND_MESSAGE, conn) < 0 || + pqPutInt((int) paytype, 4, conn) < 0 || + pqPutInt((int) crc, 4, conn) < 0 || + pqPutInt((int) chan->key.condition_id, 4, conn) < 0 || + pqPutInt(flags, 4, conn) < 0 || + pqPutInt(keylen, 4, conn) < 0 || + pqPutnchar(chan->key.condition_key, keylen, conn) < 0 || + pqPutInt(paylen, 4, conn) < 0 || + (paylen > 0 && pqPutnchar(chan->payload, paylen, conn) < 0) || + pqPutMsgEnd(conn) < 0 || + pqFlush(conn) < 0) + { + elog(LOG, "anser: could not deliver filter for condition %u: %s", + chan->key.condition_id, anser_conn_error(conn)); + return false; + } + + ANSER_DEBUG("anser: QD pushed cond=%u type=%c bytes=%d cancelled=%d", + chan->key.condition_id, paytype, paylen, + chan->cancelled ? 1 : 0); + return true; +} + +/* + * Parse a QE -> QD payload: fixed-width header, then the condition key, then + * the base64 body. ansersideband.h documents the layout and the reasoning; the + * checks here are what make it trustworthy: + * + * - the tag must match, so a message from a different format is not + * misinterpreted as this one; + * - the header is a constant length, so the key and the body start at offsets + * that no payload byte can influence; + * - the two lengths must account for the message exactly, which catches a + * truncated or over-long message before any of it is used; + * - the CRC is verified once the body has been decoded, in the caller. + * + * The payload type is carried through as the raw byte; resolving it against the + * registry is the caller's job, so that an unregistered type is reported as + * exactly that rather than as a framing error. + */ +bool +AnserWireParse(const char *msg, AnserWireMsg *out) +{ + Size msglen = strlen(msg); + char kind; + char paytype; + uint32 ssid, + ccnt, + condid, + part, + total, + flags, + keylen, + bodylen, + crc; + + if (msglen < ANSER_WIRE_HDR_LEN) + return false; + if (strncmp(msg, ANSER_WIRE_TAG " ", sizeof(ANSER_WIRE_TAG)) != 0) + return false; + + if (sscanf(msg, ANSER_WIRE_HDR_SCANF, + &kind, &paytype, &ssid, &ccnt, &condid, &part, &total, &flags, + &keylen, &bodylen, &crc) != 11) + return false; + + if (kind != ANSER_WIRE_KIND_PART && kind != ANSER_WIRE_KIND_SUBSCRIBE) + return false; + if (keylen >= ANSER_CONDITION_KEY_SIZE) + return false; + if (msglen != ANSER_WIRE_HDR_LEN + (Size) keylen + (Size) bodylen) + return false; + + MemSet(out, 0, sizeof(*out)); + out->wire = msg; + out->kind = kind; + out->payload_type = paytype; + out->key.gp_session_id = (int) ssid; + out->key.gp_command_count = (int) ccnt; + out->key.condition_id = condid; + memcpy(out->key.condition_key, msg + ANSER_WIRE_HDR_LEN, keylen); + out->key.condition_key[keylen] = '\0'; + out->key_len = (int) keylen; + out->part_index = (int) part; + out->total_parts = (int) total; + out->flags = (int) flags; + out->body = msg + ANSER_WIRE_HDR_LEN + keylen; + out->body_len = (int) bodylen; + out->crc = crc; + + return true; +} + +/* + * Verify a parsed message against its checksum. + * + * 'body' is the decoded body, which is what the producer checksummed -- so + * where it is covered this validates the base64 round trip as well. Pass + * NULL/0 for a message that has no body, and note that a type which opts out of + * checksumming its body still has its header and key checked. + */ +bool +AnserWireCheckCrc(const AnserWireMsg *msg, const void *body, Size body_len) +{ + pg_crc32c crc; + + INIT_CRC32C(crc); + COMP_CRC32C(crc, msg->wire, ANSER_WIRE_CRC_OFFSET); + COMP_CRC32C(crc, msg->key.condition_key, msg->key_len); + if (body != NULL && body_len > 0 && + AnserPayloadChecksumsBody(msg->payload_type)) + COMP_CRC32C(crc, body, body_len); + FIN_CRC32C(crc); + + return (uint32) crc == msg->crc; +} + +/* + * Coordinator-local producer. + * + * A producer running on the QD has no dispatch connection to itself, so it + * folds straight into the same channel table the hook uses. + */ +bool +AnserDispatchLocalPublish(const AnserChannelKey *channel_key, char payload_type, + uint32 part_index, uint32 total_parts, + const void *payload, Size payload_len, + bool cancelled) +{ + const AnserPayloadOps *ops; + AnserDispChannel *chan; + MemoryContext oldcxt; + + if (channel_key == NULL) + return false; + if (!cancelled && payload_len > (Size) gp_anser_max_info_size) + cancelled = true; + + ops = AnserPayloadLookup(payload_type); + if (ops == NULL) + { + elog(LOG, "anser: unknown payload type '%c' for condition %u; ignoring", + payload_type, channel_key->condition_id); + return false; + } + + anser_disp_init(); + oldcxt = MemoryContextSwitchTo(AnserDispContext); + + chan = anser_disp_lookup(channel_key, true); + if (chan != NULL) + { + anser_disp_apply_part(chan, ops, payload, payload_len, + (int) total_parts, cancelled); + ANSER_DEBUG("anser: QD local %s part cond=%u (part %u of %u) %d/%d bytes=%zu -> %s", + ops->name, channel_key->condition_id, part_index, total_parts, + chan->parts_received, chan->expected_parts, payload_len, + chan->cancelled ? "cancelled" : + chan->complete ? "complete" : "collecting"); + if (chan->complete) + anser_disp_deliver(chan); + } + + MemoryContextSwitchTo(oldcxt); + return chan != NULL; +} + +/* + * Coordinator-local consumer. + * + * Does not wait: on the coordinator the producer side of the join has already + * run by the time the probe side asks for the filter, so either the channel is + * complete or it never will be (a squelched producer, say) and we fail open. + */ +bool +AnserDispatchLocalConsume(const AnserChannelKey *channel_key, char payload_type, + void **payload, Size *payload_len, bool *cancelled) +{ + AnserDispChannel *chan; + + if (payload != NULL) + *payload = NULL; + if (payload_len != NULL) + *payload_len = 0; + if (cancelled != NULL) + *cancelled = false; + + if (channel_key == NULL || AnserDispChannels == NULL) + return false; + + chan = anser_disp_lookup(channel_key, false); + if (chan == NULL || !chan->complete) + return false; + + if (chan->cancelled || chan->payload == NULL) + { + if (cancelled != NULL) + *cancelled = true; + return false; + } + + /* Same check the segment path makes on delivery; see anser_inbox_take. */ + if (chan->ops == NULL || chan->ops->code != payload_type) + { + elog(WARNING, "anser: condition %u holds payload type '%c', expected '%c'", + channel_key->condition_id, + chan->ops != NULL ? chan->ops->code : '?', payload_type); + if (cancelled != NULL) + *cancelled = true; + return false; + } + + if (payload != NULL) + { + *payload = palloc(chan->payload_len); + memcpy(*payload, chan->payload, chan->payload_len); + } + if (payload_len != NULL) + *payload_len = chan->payload_len; + + return true; +} diff --git a/gpcontrib/anser/src/anserfilter.c b/gpcontrib/anser/src/anserfilter.c new file mode 100644 index 00000000000..88933861748 --- /dev/null +++ b/gpcontrib/anser/src/anserfilter.c @@ -0,0 +1,376 @@ +/*------------------------------------------------------------------------- + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * anserfilter.c + * Bloom-filter payload helpers for Anser channels. + * + * IDENTIFICATION + * gpcontrib/anser/src/anserfilter.c + * + *------------------------------------------------------------------------- + */ +#include "postgres.h" + +#include "anser.h" +#include "anserfilter.h" +#include "common/hashfn.h" +#include "port/pg_bitutils.h" + +static bool AnserBloomValidateHeader(const AnserBloomPartHeader *header, + Size payload_len); + +/* + * Map Anser's payload budget to bloom_create's work_mem (KB): the space left for + * the bitset after the part header. Callers must have checked that + * max_payload_bytes leaves room for a header. + */ +static int +AnserBloomWorkMemKb(Size max_payload_bytes) +{ + return (int) ((max_payload_bytes - sizeof(AnserBloomPartHeader)) / 1024); +} + +uint64 +AnserBloomSeed(const char *condition_key) +{ + if (condition_key == NULL) + return 0; + + return hash_bytes_extended((const unsigned char *) condition_key, + strlen(condition_key), 0); +} + +/* + * Build an empty bloom filter for a channel from the caller's parameters. + * + * The producer builds its filter here; the consumer rebuilds an identical one + * from the received bitset (AnserBloomDeserializePart -> bloom_create_from_bitset) + * using the SAME (total_elems, max_payload_bytes, seed) -- carried in the plan + * node's custom_private and derived from the shared condition key -- so every + * segment and the consumer realize a byte-for-byte identical filter shape. This + * is why the serialized part header does not need to carry the bitset parameters: + * the reconstructing side already knows them. + * + * Sizing is bloom_create's, but the decision to keep the result is ours. Two + * ways it can come back unusable: + * + * - Too large to send. bloom_create ends with Max(1 MB, bitset), a floor + * that overrides the work_mem cap, so a payload cap below 1 MB yields a + * bitset that cannot be shipped. + * - Too thin to help. bloom_create has no minimum density (unlike + * bloom_create_aggresive, which refuses below 1.6 bits/key), so a large + * enough total_elems against a fixed cap gets you a filter that matches + * almost everything. + * + * Both are judged from the filter's own accessors rather than by re-deriving + * bloom_create's arithmetic here: my_bloom_power() and the floor are private to + * lib/bloomfilter.c, and a copy of them would go quietly out of date on a + * kernel rebase. The cost of learning the answer this way is one palloc0 that + * is immediately freed -- against a build-side scan, which is what returning + * NULL here avoids, that is nothing. + */ +bloom_filter * +AnserBloomCreate(int64 total_elems, Size max_payload_bytes, uint64 seed) +{ + bloom_filter *filter; + Size serialized; + double bits_per_key; + + /* + * These were an Assert on the grounds that internal callers always pass a + * payload cap with room for a header. They are checks because the caller + * is a plan node, and its parameters arrived from the coordinator in + * custom_private -- not somewhere an assertion belongs. The cap also + * cannot merely be >= the header: AnserBloomWorkMemKb subtracts the header + * from a Size, so a smaller cap would underflow to an enormous work_mem. + */ + if (total_elems <= 0 || max_payload_bytes <= sizeof(AnserBloomPartHeader)) + return NULL; + + filter = bloom_create(total_elems, AnserBloomWorkMemKb(max_payload_bytes), + seed); + serialized = AnserBloomSerializedSize(filter); + bits_per_key = (double) bloom_total_bits(filter) / (double) total_elems; + + if (serialized > max_payload_bytes) + { + ANSER_DEBUG("anser: not building a filter: smallest bitset serializes to %zu bytes, cap is %zu", + serialized, max_payload_bytes); + bloom_free(filter); + return NULL; + } + + if (bits_per_key < ANSER_BLOOM_MIN_BITS_PER_KEY) + { + ANSER_DEBUG("anser: not building a filter for %ld key(s) in %zu bytes: %.2f bits/key, below the %.1f floor", + (long) total_elems, max_payload_bytes, bits_per_key, + ANSER_BLOOM_MIN_BITS_PER_KEY); + bloom_free(filter); + return NULL; + } + + return filter; +} + +/* + * Is this serialized payload still selective enough to be worth delivering? + * + * Counts the set bits in the wire form, so the coordinator can ask it of a + * merged accumulator without rebuilding a filter -- which is the point, because + * the union of N parts is denser than any one of them. Three parts at 60% fill + * OR together to as much as 94%, so the producers' own checks do not protect + * consumers from the merged result; this is the check that does. + * + * Fill, not false positive rate: the serialized part carries no hash count, so + * fill is all there is to go on here. ANSER_BLOOM_MERGED_MAX_FILL is set + * accordingly -- high enough that it cannot reject a filter that is still good + * at a large k, which makes it a backstop rather than a tuning knob. + */ +bool +AnserBloomPartWorthSending(const void *payload, Size payload_len) +{ + const char *bits; + Size bitset_bytes; + uint64 bits_set; + double fill; + + if (!AnserBloomLooksLikePart(payload, payload_len)) + return false; + + bitset_bytes = payload_len - sizeof(AnserBloomPartHeader); + if (bitset_bytes == 0) + return false; + + bits = (const char *) payload + sizeof(AnserBloomPartHeader); + bits_set = pg_popcount(bits, (int) bitset_bytes); + fill = (double) bits_set / (double) (bitset_bytes * BITS_PER_BYTE); + + if (fill > ANSER_BLOOM_MERGED_MAX_FILL) + { + ANSER_DEBUG("anser: merged filter is %.1f%% full, above the %.0f%% limit; not worth delivering", + fill * 100.0, ANSER_BLOOM_MERGED_MAX_FILL * 100.0); + return false; + } + + return true; +} + +Size +AnserBloomSerializedSize(const bloom_filter *filter) +{ + if (filter == NULL) + return 0; + + return sizeof(AnserBloomPartHeader) + bloom_bitset_bytes(filter); +} + +/* + * Does this payload look like a serialized bloom part? Used by the in-place fold + * to confirm both the accumulator and the incoming payload are well-formed parts + * before OR-ing their bitsets. A false positive is effectively impossible: a + * part must carry the ABF1 magic, a known version, and sane part counts. + */ +bool +AnserBloomLooksLikePart(const void *payload, Size payload_len) +{ + if (payload == NULL || payload_len < sizeof(AnserBloomPartHeader)) + return false; + + return AnserBloomValidateHeader((const AnserBloomPartHeader *) payload, + payload_len); +} + +/* + * Fold an incoming part into an accumulator part IN PLACE. + * + * When a merged part and the incoming part are the same serialized size (they + * share bitset params derived from the condition key), the union is a pure + * bitwise OR of the two bitsets plus a bump of the merged header's fold count -- + * no reallocation. This mutates `acc` directly, so the caller must hold whatever + * lock guards the buffer (AnserChannelLock, for the channel payload). + * + * Returns true only when the in-place union applied. It returns false -- and + * leaves `acc` untouched (all checks run before any write) -- when the union + * cannot be done by raw OR: sizes differ, either side is not a valid part, or + * the filter parameters disagree. Since every part on a channel shares the same + * (condition-key-derived) parameters and therefore the same serialized size, the + * first part is stored verbatim and every later part folds in here; a false + * return means a malformed/mismatched payload and the caller cancels the channel. + */ +bool +AnserBloomFoldPartInPlace(void *acc, Size acc_len, + const void *part, Size part_len) +{ + AnserBloomPartHeader *ah; + unsigned char *abits; + const unsigned char *pbits; + Size bitset_bytes; + Size i; + + /* Only a same-sized, valid part-vs-part union can be done by raw OR. */ + if (acc_len != part_len || + !AnserBloomLooksLikePart(acc, acc_len) || + !AnserBloomLooksLikePart(part, part_len)) + return false; + + ah = (AnserBloomPartHeader *) acc; + + /* + * Equal serialized size is sufficient: every part on a channel is built by + * bloom_create from the same (condition-key-derived) parameters, so equal + * length implies an identical bitset shape. The bitset is whatever follows + * the header, so its length is the payload length minus the header. + */ + bitset_bytes = acc_len - sizeof(AnserBloomPartHeader); + abits = (unsigned char *) acc + sizeof(AnserBloomPartHeader); + pbits = (const unsigned char *) part + sizeof(AnserBloomPartHeader); + + for (i = 0; i < bitset_bytes; i++) + abits[i] |= pbits[i]; + + /* One more segment part folded into the running merged part. */ + ah->total_parts += 1; + + return true; +} + +/* + * Serialize one filter as a wire part: an AnserBloomPartHeader followed by the + * raw bitset. Fails (returns false) on bogus arguments or a too-small buffer. + */ +bool +AnserBloomSerializePart(const bloom_filter *filter, uint32 part_index, + uint32 total_parts, void *buffer, Size buffer_size, + Size *payload_len) +{ + AnserBloomPartHeader header; + Size bitset_bytes; + Size total_len; + + if (payload_len != NULL) + *payload_len = 0; + + if (filter == NULL || buffer == NULL || total_parts == 0 || + part_index >= total_parts) + return false; + + bitset_bytes = bloom_bitset_bytes(filter); + total_len = sizeof(AnserBloomPartHeader) + bitset_bytes; + if (buffer_size < total_len) + return false; + + MemSet(&header, 0, sizeof(header)); + header.magic = ANSER_BLOOM_PART_MAGIC; + header.version = ANSER_BLOOM_PART_VERSION; + header.part_index = part_index; + header.total_parts = total_parts; + + memcpy(buffer, &header, sizeof(header)); + memcpy((char *) buffer + sizeof(header), bloom_bitset_data(filter), + bitset_bytes); + + if (payload_len != NULL) + *payload_len = total_len; + return true; +} + +/* + * Rebuild the filter from a received merged part. + * + * The bitset parameters are NOT taken from the wire header: the caller passes + * the same (total_elems, max_payload_bytes, seed) used to produce the filter -- + * it holds them in the plan node, so both ends agree by construction. We rebuild + * the empty filter from those, then load the received bitset into it. The wire + * header is still validated (magic/version/counts) and, crucially, the received + * bitset length must exactly match the size the local parameters imply; any + * mismatch (version/parameter skew, truncation) returns NULL so the consumer + * fails open rather than loading a wrongly-shaped bitset. part_index/total_parts + * are surfaced from the header for diagnostics. + */ +bloom_filter * +AnserBloomDeserializePart(const void *payload, Size payload_len, + int64 total_elems, Size max_payload_bytes, uint64 seed, + uint32 *part_index, uint32 *total_parts) +{ + const AnserBloomPartHeader *header; + bloom_filter *filter; + + if (part_index != NULL) + *part_index = 0; + if (total_parts != NULL) + *total_parts = 0; + + if (payload == NULL || payload_len < sizeof(AnserBloomPartHeader)) + return NULL; + + header = (const AnserBloomPartHeader *) payload; + if (!AnserBloomValidateHeader(header, payload_len)) + return NULL; + + if (max_payload_bytes <= sizeof(AnserBloomPartHeader)) + return NULL; + if (total_elems < 1) + total_elems = 1; + + /* + * Build the filter straight from the received bitset, sized by our own + * parameters. bloom_create_from_bitset returns NULL unless the received + * length is exactly the size those parameters imply, so the fail-open + * described above happens here. A filter is thus only ever populated at + * construction and, from then on, only grown by add/union -- never re-set. + */ + filter = bloom_create_from_bitset(total_elems, + AnserBloomWorkMemKb(max_payload_bytes), + seed, + (const unsigned char *) payload + + sizeof(AnserBloomPartHeader), + payload_len - sizeof(AnserBloomPartHeader)); + if (filter == NULL) + return NULL; + + if (part_index != NULL) + *part_index = header->part_index; + if (total_parts != NULL) + *total_parts = header->total_parts; + return filter; +} + +/* + * Validate the wire framing of a part header. The bitset parameters are not + * carried on the wire (both ends rebuild the filter from the shared plan + * parameters), so this only checks the framing: magic/version, a sane fold + * count, and that the payload carries a header plus at least some bitset. The + * authoritative size check -- that the received bitset matches the size the local + * parameters imply -- is done in AnserBloomDeserializePart. + */ +static bool +AnserBloomValidateHeader(const AnserBloomPartHeader *header, Size payload_len) +{ + if (header == NULL) + return false; + + if (header->magic != ANSER_BLOOM_PART_MAGIC || + header->version != ANSER_BLOOM_PART_VERSION) + return false; + + if (header->total_parts == 0 || header->part_index >= header->total_parts) + return false; + + return payload_len > sizeof(AnserBloomPartHeader); +} diff --git a/gpcontrib/anser/src/anserinit.c b/gpcontrib/anser/src/anserinit.c new file mode 100644 index 00000000000..10b0a28498f --- /dev/null +++ b/gpcontrib/anser/src/anserinit.c @@ -0,0 +1,235 @@ +/*------------------------------------------------------------------------- + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * anserinit.c + * Module entry point: GUCs and the core hooks Anser hangs off. + * + * Anser is a shared_preload_libraries extension. Everything it needs from the + * server is reached through an existing extensibility point: + * + * planner_hook runtime-filter injection + * RegisterCustomScanMethods the injected plan nodes + * cdbdisp_notify_hook parts arriving from segments + * ExecutorEnd_hook dropping a query's channels + * + * It must be preloaded, because a segment backend deserializing a dispatched + * plan has no opportunity to load the library first. + * + * IDENTIFICATION + * gpcontrib/anser/src/anserinit.c + * + *------------------------------------------------------------------------- + */ +#include "postgres.h" + +#include "access/xact.h" +#include "anser.h" +#include "anserplan.h" +#include "ansersideband.h" +#include "cdb/cdbdisp.h" +#include "cdb/cdbvars.h" +#include "executor/executor.h" +#include "miscadmin.h" +#include "optimizer/planner.h" +#include "utils/guc.h" + +PG_MODULE_MAGIC; + +void _PG_init(void); + +bool gp_anser_enable = false; +bool gp_anser_runtime_filter = false; +bool gp_anser_debug = false; +int gp_anser_max_info_size = 64 * 1024 * 1024 + 1024 * 1024; +int gp_anser_timeout_ms = 100000; + +static void anser_define_gucs(void); +static PlannedStmt *anser_planner(Query *parse, const char *query_string, + int cursorOptions, ParamListInfo boundParams, + OptimizerOptions *optimizer_options); + +static planner_hook_type prev_planner_hook = NULL; +static ExecutorEnd_hook_type prev_ExecutorEnd_hook = NULL; + +static void anser_executor_end(QueryDesc *queryDesc); +static void anser_xact_callback(XactEvent event, void *arg); + +void +_PG_init(void) +{ + anser_define_gucs(); + + /* + * Only a preloaded library can be relied on to have installed its hooks in + * every backend. Loaded any other way, Anser stays inert: the GUCs exist + * (so a stray setting is not an error) but nothing is wired up. + */ + if (!process_shared_preload_libraries_in_progress) + return; + + prev_planner_hook = planner_hook; + planner_hook = anser_planner; + + /* + * Channels live in backend memory on both ends, so they need a point to be + * dropped. ExecutorEnd covers the normal path; the transaction callback + * catches queries that end by erroring. + */ + prev_ExecutorEnd_hook = ExecutorEnd_hook; + ExecutorEnd_hook = anser_executor_end; + RegisterXactCallback(anser_xact_callback, NULL); + + /* + * Handle Anser notifies arriving from QEs on the dispatch connections. + * Installed unconditionally: it is inert until a segment sends one, and a + * QE that never dispatches never calls it. + */ + cdbdisp_notify_hook = AnserDispatchNotifyHandler; + + /* + * The producer and consumer nodes travel to the segments inside dispatched + * plans, so every backend must be able to resolve their CustomScan methods + * by name. Registering here covers QD and QE alike. + */ + AnserRegisterRuntimeFilterMethods(); +} + +/* + * The subsystem's GUCs. All are "anser.*"-qualified because they belong to a + * loadable module; the C variables keep their gp_anser_ names. + */ +static void +anser_define_gucs(void) +{ + DefineCustomBoolVariable("anser.enable", + "Enables the Anser adaptive information sharing subsystem.", + "When disabled, the plan pass never injects anything and no filters are exchanged.", + &gp_anser_enable, + false, + PGC_SIGHUP, + 0, + NULL, NULL, NULL); + + DefineCustomBoolVariable("anser.runtime_filter", + "Enables injection of Anser runtime bloom filters into plans.", + "Requires anser.enable; the plan pass is a no-op otherwise.", + &gp_anser_runtime_filter, + false, + PGC_USERSET, + GUC_EXPLAIN, + NULL, NULL, NULL); + + /* + * GUC_GPDB_NEED_SYNC on everything a QE reads. A custom GUC gets + * GUC_GPDB_NO_SYNC by default -- gpdb_assign_sync_flag() says so in as many + * words for "the third-part libraries gucs introduced by customer" + * (guc_gp.c) -- and a QE created after the SET then never receives the + * value. The QEs alive at the time do get it, because the SET itself is + * dispatched to them as a command, so the setting appears to work right up + * until a query needs a wider gang and the new processes fall back to the + * compiled-in default. That is not a setting anyone can reason about. + */ + DefineCustomBoolVariable("anser.debug", + "Logs each step of the Anser filter exchange.", + "Traces publish, merge, delivery and receive in the log of the process each happens in.", + &gp_anser_debug, + false, + PGC_USERSET, + GUC_GPDB_NEED_SYNC, + NULL, NULL, NULL); + + DefineCustomIntVariable("anser.max_info_size", + "Sets the maximum byte size of one Anser information record.", + "Caps the serialized payload a channel may carry, and with it the effective bloom-filter size. The default holds a full 64 MB bitset plus its serialized-part header.", + &gp_anser_max_info_size, + 64 * 1024 * 1024 + 1024 * 1024, 1, INT_MAX, + PGC_USERSET, + 0, + NULL, NULL, NULL); + + DefineCustomIntVariable("anser.timeout_ms", + "Sets how long an Anser consumer waits for its filter.", + "On expiry the consumer runs unfiltered. The deadline matters because a producer that gets squelched never publishes at all. The default is generous because delivery is serial: the coordinator writes the merged filter to one subscriber at a time, so the last of a wide slice waits for all the writes before it.", + &gp_anser_timeout_ms, + 100000, 0, INT_MAX, + PGC_USERSET, + GUC_UNIT_MS | GUC_GPDB_NEED_SYNC, + NULL, NULL, NULL); + + MarkGUCPrefixReserved("anser"); +} + +/* + * Plan the query as usual, then hand the finished tree to the runtime-filter + * pass. Wrapping the hook this way covers both optimizers, because ORCA is + * dispatched from inside standard_planner(). + */ +static PlannedStmt * +anser_planner(Query *parse, const char *query_string, int cursorOptions, + ParamListInfo boundParams, OptimizerOptions *optimizer_options) +{ + PlannedStmt *result; + + if (prev_planner_hook) + result = prev_planner_hook(parse, query_string, cursorOptions, + boundParams, optimizer_options); + else + result = standard_planner(parse, query_string, cursorOptions, + boundParams, optimizer_options); + + AnserApplyRuntimeFilters(result); + + return result; +} + +/* + * Drop the transport's per-query state. + * + * Nested executor runs (a function body, say) must not clear state the outer + * query is still using, so only the outermost end resets. + */ +static void +anser_executor_end(QueryDesc *queryDesc) +{ + static int nesting_level = 0; + + nesting_level++; + PG_TRY(); + { + if (prev_ExecutorEnd_hook) + prev_ExecutorEnd_hook(queryDesc); + else + standard_ExecutorEnd(queryDesc); + } + PG_FINALLY(); + { + nesting_level--; + } + PG_END_TRY(); + + if (nesting_level == 0) + AnserSidebandResetAll(); +} + +static void +anser_xact_callback(XactEvent event, void *arg) +{ + if (event == XACT_EVENT_ABORT || event == XACT_EVENT_PARALLEL_ABORT) + AnserSidebandResetAll(); +} diff --git a/gpcontrib/anser/src/anserpayload.c b/gpcontrib/anser/src/anserpayload.c new file mode 100644 index 00000000000..2926a15a3e4 --- /dev/null +++ b/gpcontrib/anser/src/anserpayload.c @@ -0,0 +1,108 @@ +/*------------------------------------------------------------------------- + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * anserpayload.c + * The registry of payload types. + * + * One row per kind of runtime information a channel can carry. See + * anserpayload.h for what a row means and how to add one. + * + * IDENTIFICATION + * gpcontrib/anser/src/anserpayload.c + * + *------------------------------------------------------------------------- + */ +#include "postgres.h" + +#include "anserfilter.h" +#include "anserpayload.h" + +/* + * Indexed by the wire byte itself, so a lookup is one load and registering a + * type is one line (the pattern syscache.c:154 uses for its cache + * descriptors). Unregistered slots are all-zero, which is the answer we want + * for every question asked of them: no fold, no checksum, no name. + * + * Only two of the 256 slots are used, so this costs 8 KB of read-only data to + * hold mostly zeroes. That is the whole point: the alternative ways to reach a + * descriptor in constant time -- a second array mapping code to row, or a + * switch -- are extra code that has to be kept in step with the table, and the + * 8 KB buys them all away. A linear search over a two-row table would be just + * as fast in practice; what it would not be is impossible to get wrong. + */ +static const AnserPayloadOps AnserPayloadTable[PG_UINT8_MAX + 1] = +{ + /* + * A subscription: header and key only. It has no body to fold or to + * checksum, but its header still is checksummed -- that is what protects + * the condition key it is asking to be served on. + */ + [(unsigned char) ANSER_PAYLOAD_NONE] = { + ANSER_PAYLOAD_NONE, "none", false, NULL, NULL + }, + + /* + * A bloom filter part. AnserBloomFoldPartInPlace already has exactly the + * fold contract -- it validates both sides before touching the accumulator + * and ORs the bitsets -- so it is used directly rather than wrapped, as is + * the fill check for worth_delivering. + */ + [(unsigned char) ANSER_PAYLOAD_BLOOM] = { + ANSER_PAYLOAD_BLOOM, "bloom", true, AnserBloomFoldPartInPlace, + AnserBloomPartWorthSending + } +}; + +/* + * Resolve a code, or NULL if nothing has registered it. + * + * The NULL matters: 'code' reaches us straight off the wire, so "no such type" + * is a case that has to be reportable rather than silently defaulted. It is + * also the diagnostic that catches a payload type given a code in the header + * but no row in the table. + */ +const AnserPayloadOps * +AnserPayloadLookup(char code) +{ + /* + * Cast before indexing. 'char' is signed on x86, and the code is an + * untrusted byte: without this, a 0x80-0xff code from a corrupted or + * hostile message would index the table at a negative offset. + */ + const AnserPayloadOps *ops = &AnserPayloadTable[(unsigned char) code]; + + if (ops->code == '\0') + return NULL; + + /* The index is authoritative; a row filed under the wrong code is a typo. */ + Assert(ops->code == code); + + return ops; +} + +bool +AnserPayloadChecksumsBody(char code) +{ + /* + * No presence check: an unregistered code lands on a zeroed slot, and + * "false" is exactly right for it -- there is nothing to checksum that we + * would know how to use anyway. + */ + return AnserPayloadTable[(unsigned char) code].checksum_body; +} diff --git a/gpcontrib/anser/src/anserplan.c b/gpcontrib/anser/src/anserplan.c new file mode 100644 index 00000000000..7fa5aae5189 --- /dev/null +++ b/gpcontrib/anser/src/anserplan.c @@ -0,0 +1,637 @@ +/*------------------------------------------------------------------------- + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * anserplan.c + * Post-planning transformation that injects Anser runtime bloom-filter + * producer/consumer CustomScan nodes into a finished plan tree. + * + * The pass runs once from planner() (after both the Postgres planner and ORCA, + * and after set_plan_references / the cdbllize slice passes), recognizes one + * supported join shape, and inserts a producer above the base scan the join's + * build key comes from and a consumer above the base scan its probe key comes + * from. Either side may be reached through the joins before it, so a chain of + * joins gets a filter per join, and two joins reaching the same relation stack + * their filters on it. See anserplan.h for the rationale. + * + * IDENTIFICATION + * gpcontrib/anser/src/anserplan.c + * + *------------------------------------------------------------------------- + */ +#include "postgres.h" + +#include "anser.h" +#include "anserfilter.h" +#include "anserplan.h" +#include "cdb/cdbvars.h" +#include "catalog/pg_type.h" +#include "nodes/nodeFuncs.h" +#include "nodes/pg_list.h" +#include "utils/acl.h" +#include "utils/fmgroids.h" +#include "utils/lsyscache.h" + +/* Runtime-filter bloom size bounds (realized bitset bytes). */ +#define ANSER_RF_MIN_BYTES (1024 * 1024) /* bloom_create's 1 MB floor */ +#define ANSER_RF_MAX_BYTES (64 * 1024 * 1024) +#define ANSER_RF_HEADER_ROOM 64 + +/* Per-statement state for the injection pass. */ +typedef struct AnserInjectCtx +{ + PlannedStmt *stmt; /* for slices[]: see anser_slice_producers */ + int slice_index; /* slice the subtree being walked runs in */ + uint32 next_condition_id; + int next_plan_node_id; + List *consumer_keys; /* condition_keys already given a consumer node; + * enforces one consumer per channel (see + * anser_try_inject) */ +} AnserInjectCtx; + +static int anser_max_plan_node_id(Plan *plan); +static int anser_slice_producers(AnserInjectCtx *ctx, int slice_index); +static bool anser_hashjoin_keys(HashJoin *hj, AttrNumber *inner_attno, + AttrNumber *outer_attno); +static bool anser_resolve_key_scan(Plan *top, AttrNumber key_attno, + int slice_index, Plan **parent_out, + Plan **scan_out, AttrNumber *attno_out, + int *slice_out); +static void anser_try_inject(HashJoin *hj, AnserInjectCtx *ctx); +static void anser_inject_walk(Plan *plan, AnserInjectCtx *ctx); + +void +AnserApplyRuntimeFilters(PlannedStmt *stmt) +{ + /* + * Opt-in and coordinator-only: the pass runs on the QD where the whole + * PlannedStmt is available, and only when the operator has enabled the + * Anser subsystem and the runtime-filter feature. Anything else is left + * completely untouched. + */ + if (!gp_anser_enable || !gp_anser_runtime_filter) + return; + if (Gp_role != GP_ROLE_DISPATCH) + return; + if (stmt == NULL || stmt->commandType != CMD_SELECT || stmt->planTree == NULL) + return; + + { + AnserInjectCtx ctx; + ListCell *lc; + int maxid; + + /* + * Injected nodes need plan_node_ids unique across the whole statement. + * set_plan_references already numbered every existing node, so continue + * past the current maximum (planTree + subplans). + * + * We walk the tree because neither planner's id counter survives to + * this hook (the Postgres planner counts in a standard_planner() + * local; ORCA counts inside its DXL translation context) and + * PlannedStmt carries no max-id field -- walking is the only + * planner-agnostic option, and cheap at this hook point. + * + * We take the max, not the node count: "count == next free id" + * assumes dense numbering, which ORCA's CIdGenerator and third-party + * planner_hooks do not promise. max + 1 is correct under any + * assignment scheme. + */ + maxid = anser_max_plan_node_id(stmt->planTree); + foreach(lc, stmt->subplans) + maxid = Max(maxid, anser_max_plan_node_id((Plan *) lfirst(lc))); + + ctx.stmt = stmt; + ctx.slice_index = 0; /* the root runs in the coordinator's slice */ + ctx.next_condition_id = 0; + ctx.next_plan_node_id = maxid + 1; + ctx.consumer_keys = NIL; + + anser_inject_walk(stmt->planTree, &ctx); + } +} + +/* + * Largest plan_node_id in a plan subtree. Recurses the spine plus CustomScan + * children; sufficient for the supported (simple) plan shape. + */ +static int +anser_max_plan_node_id(Plan *plan) +{ + int m; + + if (plan == NULL) + return 0; + + m = plan->plan_node_id; + m = Max(m, anser_max_plan_node_id(outerPlan(plan))); + m = Max(m, anser_max_plan_node_id(innerPlan(plan))); + if (IsA(plan, CustomScan)) + { + ListCell *lc; + + foreach(lc, ((CustomScan *) plan)->custom_plans) + m = Max(m, anser_max_plan_node_id((Plan *) lfirst(lc))); + } + + return m; +} + +/* + * Compute the bloom sizing to hand both the producer and consumer helpers, from + * the estimated build cardinality. Both call AnserBloomCreate (== bloom_create) + * with the SAME (total_elems, max_payload) so they realize an identical filter; + * we mirror bloom_create's own math here so `planned_bytes` (shown in EXPLAIN) + * equals the realized bitset: target ~2 bytes/element, floor at 1 MB, cap at the + * server payload budget, round DOWN to a power of two. + */ +bool +AnserRuntimeFilterSize(double est_rows, int64 *total_elems, int64 *max_payload, + int64 *planned_bytes) +{ + int64 cap_bytes; + int64 elems; + int64 target_bytes; + int64 realized; + + /* Largest bitset that fits the server payload cap, and our own ceiling. */ + cap_bytes = Min((int64) ANSER_RF_MAX_BYTES, + (int64) gp_anser_max_info_size - ANSER_RF_HEADER_ROOM); + if (cap_bytes < ANSER_RF_MIN_BYTES) + return false; /* cap too small to hold even a floor-sized filter */ + + /* + * The element count stays the honest estimate; only the *size* is clamped. + * + * It is tempting to clamp elems instead -- it is the number that drives the + * size, so bounding it bounds the bitset in one step. That is a trap. + * total_elems is also what optimal_k() sizes the hash count from, so + * understating it understates k: a 128M-row build side reported as 33.5M + * gets k=10, when the optimum for 128M keys in a 512 Mbit filter is k=3. + * Since the producer inserts all 128M keys regardless of what we wrote + * down, the result is a filter with a 38% false positive rate where 15% was + * available. Lowering the declared count never makes a filter fit; it only + * relabels it, and then misconfigures it. + * + * INT32 is the real bound on elems: custom_private carries it as an Integer + * node. The density check below refuses anything remotely near that. + */ + elems = (est_rows > 0.0) ? (int64) est_rows : 1; + if (elems < 1) + elems = 1; + if (elems > PG_INT32_MAX) + elems = PG_INT32_MAX; + + target_bytes = Min(cap_bytes, Max((int64) ANSER_RF_MIN_BYTES, elems * 2)); + realized = ANSER_RF_MIN_BYTES; + while ((realized << 1) <= target_bytes) + realized <<= 1; + + /* + * Refuse a join whose build side cannot be usefully summarized within the + * payload cap. + * + * Refusing here is the cheapest possible outcome: no producer, no consumer, + * no channel, and nothing for a consumer to wait on and time out against. + * The runtime checks in anserfilter.c exist for when this estimate turns + * out to be wrong, not instead of this one. + */ + if (realized * (double) BITS_PER_BYTE / (double) elems + < ANSER_BLOOM_MIN_BITS_PER_KEY) + return false; + + *total_elems = elems; + *max_payload = cap_bytes + ANSER_RF_HEADER_ROOM; + *planned_bytes = realized; + return true; +} + +/* + * Match a single-column equijoin over plain (by-value) Vars and return the + * inner (build) and outer (probe) key attnos. After set_plan_references the + * operands are INNER_VAR / OUTER_VAR references into the join's child tlists. + */ +static bool +anser_hashjoin_keys(HashJoin *hj, AttrNumber *inner_attno, AttrNumber *outer_attno) +{ + OpExpr *op; + Node *l; + Node *r; + Var *outer_var; + Var *inner_var; + + if (list_length(hj->hashclauses) != 1) + return false; + op = (OpExpr *) linitial(hj->hashclauses); + if (!IsA(op, OpExpr) || list_length(op->args) != 2) + return false; + + l = (Node *) linitial(op->args); + r = (Node *) lsecond(op->args); + while (l != NULL && IsA(l, RelabelType)) + l = (Node *) ((RelabelType *) l)->arg; + while (r != NULL && IsA(r, RelabelType)) + r = (Node *) ((RelabelType *) r)->arg; + if (l == NULL || r == NULL || !IsA(l, Var) || !IsA(r, Var)) + return false; + + if (((Var *) l)->varno == OUTER_VAR && ((Var *) r)->varno == INNER_VAR) + { + outer_var = (Var *) l; + inner_var = (Var *) r; + } + else if (((Var *) l)->varno == INNER_VAR && ((Var *) r)->varno == OUTER_VAR) + { + outer_var = (Var *) r; + inner_var = (Var *) l; + } + else + return false; + + /* + * Producer and consumer hash the raw Datum bytes, which is only correct + * when SQL equality coincides with bitwise Datum equality of the key. + * That requires the SAME by-value type on both sides: cross-type equijoins + * (e.g. float4 = float8, date = timestamp) hash different bit patterns for + * equal values, and floats are excluded even same-typed because -0.0 and + * 0.0 compare equal but are not bitwise equal. Anything looser could + * prune rows that actually join. + */ + if (inner_var->vartype != outer_var->vartype) + return false; + if (!get_typbyval(inner_var->vartype)) + return false; + if (inner_var->vartype == FLOAT4OID || inner_var->vartype == FLOAT8OID) + return false; + + *inner_attno = inner_var->varattno; + *outer_attno = outer_var->varattno; + return true; +} + +/* + * Follow one side of a join down to the base SeqScan its key column comes + * from, mapping the key attno through each targetlist on the way. Used for + * both sides: the producer goes above the scan the build key comes from, the + * consumer above the scan the probe key comes from. + * + * Wrapping the base scan (rather than an intermediate node) keeps the injected + * CustomScan's custom_scan_tlist made of base-relation Vars, which (a) deparses + * cleanly in EXPLAIN and (b) is the proven-safe "leaf child" case for MPP + * slice/gang setup. (a) is not a preference: after set_plan_references an + * intermediate node's targetlist holds OUTER_VAR/INNER_VAR Vars, and a + * CustomScan keeps its child in custom_plans rather than in lefttree, so + * copying such a targetlist into custom_scan_tlist leaves EXPLAIN resolving an + * OUTER_VAR against a node with no outer plan -- get_variable() raises "bogus + * varno" (ruleutils.c) and the plan cannot be printed at all. + * + * The descent goes through HashJoins as well as the single-child passthroughs + * (Hash, Motion), which is what lets a chain of joins be filtered: by the third + * join one side is the result of the first two, and the key column still traces + * back to one base relation through it. A filter built that way holds the + * relation's whole key column rather than the subset the earlier joins would + * have left -- a superset, so no row that could join is ever rejected, but less + * selective than filtering on the intermediate result itself. Filtering on the + * intermediate would mean wrapping it, which (a) above rules out. + * + * It also goes through our own nodes, so the second join to reach a relation + * stacks its filter under the first one's rather than replacing it. + * + * On success *parent_out is the node holding the base scan -- pass it to + * anser_relink_child(), which knows that ours hold it in custom_plans and + * everything else in the outer link. A SeqScan is only accepted through one of + * those two: the inner child of a HashJoin is always a Hash. + */ +static bool +anser_resolve_key_scan(Plan *top, AttrNumber key_attno, int slice_index, + Plan **parent_out, Plan **scan_out, + AttrNumber *attno_out, int *slice_out) +{ + Plan *node = top; + AttrNumber attno = key_attno; + + for (;;) + { + TargetEntry *tle; + Var *var; + Plan *child; + bool via_outer; + + if (node == NULL || + attno < 1 || attno > list_length(node->targetlist)) + return false; + + tle = (TargetEntry *) list_nth(node->targetlist, attno - 1); + if (tle == NULL || !IsA(tle->expr, Var)) + return false; + var = (Var *) tle->expr; + + /* + * Which child supplies this column. A join is the only node here with + * two of them, and only a HashJoin is descended through -- an Append or + * a SetOp would make the column come from several relations at once, + * and then no single scan holds the keys the filter would claim. + * + * One of our own nodes is a passthrough with an identity targetlist, so + * its columns are its child's columns in the same order and the attno + * carries over unchanged. Descending through it is what lets a second + * join filter a relation a first join already filters. + */ + if (var->varno == OUTER_VAR) + { + child = outerPlan(node); + via_outer = true; + } + else if (var->varno == INNER_VAR && IsA(node, HashJoin)) + { + child = innerPlan(node); + via_outer = false; + } + else if (var->varno == INDEX_VAR && AnserIsRuntimeFilterScan(node)) + { + child = (Plan *) linitial(((CustomScan *) node)->custom_plans); + via_outer = true; + } + else + return false; + + if (child == NULL) + return false; + + if (via_outer && IsA(child, SeqScan)) + { + *parent_out = node; + *scan_out = child; + *attno_out = var->varattno; + *slice_out = slice_index; + return true; + } + if (!IsA(child, Hash) && !IsA(child, Motion) && !IsA(child, HashJoin) && + !AnserIsRuntimeFilterScan(child)) + return false; + + /* + * Descending through a Motion moves us into its sending slice, which + * is where the producer will end up -- and whose width decides how + * many parts the coordinator has to wait for. + */ + if (IsA(child, Motion)) + slice_index = ((Motion *) child)->motionID; + + node = child; + attno = var->varattno; + } +} + +/* + * Put `child` where the scan the descent ended on used to be. The link is the + * outer one for every node the descent walks except one of ours, which holds + * its child in custom_plans -- that is the case where a second join filters a + * relation a first join already filters, and the new node goes below the old + * one so that both see every row. + */ +static void +anser_relink_child(Plan *parent, Plan *child) +{ + if (AnserIsRuntimeFilterScan(parent)) + linitial(((CustomScan *) parent)->custom_plans) = child; + else + outerPlan(parent) = child; +} + +/* + * If this HashJoin is the supported shape, inject a producer above the base + * scan its build key comes from and a consumer above the base scan its probe + * key comes from. + */ +static void +anser_try_inject(HashJoin *hj, AnserInjectCtx *ctx) +{ + Plan *hash = innerPlan(hj); /* build side */ + Plan *probe = outerPlan(hj); /* probe side */ + Plan *build_parent; + Plan *build_scan; + Plan *probe_parent; + Plan *probe_scan; + AttrNumber inner_attno; + AttrNumber outer_attno; + AttrNumber build_attno; + AttrNumber probe_attno; + int build_slice; + int probe_slice; + int n_producers; + int64 total_elems; + int64 max_payload; + int64 planned_bytes; + uint32 condition_id; + char condition_key[ANSER_CONDITION_KEY_SIZE]; + CustomScan *producer; + CustomScan *consumer; + ListCell *lc; + + if (hj->join.jointype != JOIN_INNER && hj->join.jointype != JOIN_RIGHT) + return; + if (hash == NULL || !IsA(hash, Hash)) + return; + if (probe == NULL) + return; + + if (!anser_hashjoin_keys(hj, &inner_attno, &outer_attno)) + return; + if (!anser_resolve_key_scan(hash, inner_attno, ctx->slice_index, + &build_parent, &build_scan, &build_attno, + &build_slice)) + return; + + /* + * Where the filter is applied. A probe side that is already a scan is the + * simple case; anything else -- a Motion over the result of the joins + * before this one -- is descended the same way the build side is, and the + * consumer lands on the base scan the probe key comes from. + * + * Filtering that scan is right for the same reason filtering the probe + * side is: every row the probe subtree produces carries that relation's + * key column unchanged, so a row of it whose key the build side does not + * have cannot reach the join's output through any path. A row dropped + * under an outer join inside the subtree comes back NULL-extended instead, + * and a NULL key joins nothing either. + */ + if (IsA(probe, SeqScan)) + { + probe_parent = (Plan *) hj; + probe_scan = probe; + probe_attno = outer_attno; + } + else if (!anser_resolve_key_scan(probe, outer_attno, ctx->slice_index, + &probe_parent, &probe_scan, &probe_attno, + &probe_slice)) + return; + + /* + * Nothing to gain from filtering a relation with its own keys, and the + * producer would have to finish before the consumer under it could start. + */ + if (probe_scan == build_scan) + return; + + /* + * How many producers will there be? One per process of the build scan's + * slice. Declining when we cannot tell is the safe direction: a count that + * is too low makes the coordinator complete the channel early and ship a + * filter missing keys, which drops joinable rows. + */ + n_producers = anser_slice_producers(ctx, build_slice); + if (n_producers <= 0) + return; + /* + * Sized from the scan the producer wraps, not from the Hash: those are the + * rows whose keys actually go into the filter. They are the same thing + * when the build side is one scan under a Hash, and they are not when the + * descent came through a join -- there the Hash holds the joined result and + * the scan holds the whole key column, which is what gets inserted. + */ + if (!AnserRuntimeFilterSize(build_scan->plan_rows, &total_elems, &max_payload, + &planned_bytes)) + return; + + condition_id = ctx->next_condition_id++; + snprintf(condition_key, sizeof(condition_key), "anser_rf_%u", condition_id); + + /* + * One consumer per channel. Two consumer nodes sharing a channel would + * both be served -- the coordinator pushes to every subscriber -- but they + * would also both count toward nothing: the channel's part count comes from + * the producers, so a second consumer only multiplies deliveries. More to + * the point, the two would be indistinguishable in the debug trace and in + * any future per-channel accounting, so keep the invariant. + * + * Minting a unique condition_id per injection makes this hold by + * construction, so the check never fires. It stays as a guard against + * channel-key collisions if the key derivation ever changes (e.g. keys + * derived from the build's semantic identity, where two joins could share + * one channel). + */ + foreach(lc, ctx->consumer_keys) + { + if (strcmp((const char *) lfirst(lc), condition_key) == 0) + return; + } + + /* Producer wraps the build base scan; keyed by the mapped build attno. */ + producer = AnserBuildBloomProducerScan(build_scan, build_attno, condition_id, + condition_key, total_elems, max_payload, + planned_bytes, n_producers); + producer->scan.plan.plan_node_id = ctx->next_plan_node_id++; + anser_relink_child(build_parent, (Plan *) producer); + + /* + * Consumer wraps the probe base scan; keyed by the mapped probe attno. + * + * Whether it must let the join's first outer tuple through unfiltered + * comes down to whether ExecHashJoin prefetches one before building its + * hash table. It skips the prefetch when it null-fills the inner side -- + * HJ_FILL_INNER, which for the join types injected into means a right join + * -- and otherwise decides on a cost comparison we are not going to + * reproduce here. So: rule it out only where the join type rules it out, + * and defer everywhere else. Wrongly deferring gives up pushdown for that + * consumer; wrongly not deferring deadlocks it against its own producer. + */ + consumer = AnserBuildBloomConsumerScan(probe_scan, probe_attno, condition_id, + condition_key, total_elems, max_payload, + planned_bytes, n_producers, + hj->join.jointype != JOIN_RIGHT); + consumer->scan.plan.plan_node_id = ctx->next_plan_node_id++; + anser_relink_child(probe_parent, (Plan *) consumer); + + /* Record the channel so no later join can add a second consumer on it. */ + ctx->consumer_keys = lappend(ctx->consumer_keys, pstrdup(condition_key)); +} + +/* + * Manual in-place traversal (recurse spine + custom_plans). We do not use + * plan_tree_mutator, whose CustomScan arm does not descend into custom_plans. + */ +static void +anser_inject_walk(Plan *plan, AnserInjectCtx *ctx) +{ + int save_slice = ctx->slice_index; + + if (plan == NULL) + return; + + /* + * A Motion is the boundary between slices, and everything below one runs + * in the sending slice. cdbllize stamps that slice's index into motionID + * (cdbllize.c: "motion->motionID = sendSlice->sliceIndex") and then clears + * senderSliceInfo, so motionID is the only durable way to get here from a + * finished plan. + */ + if (IsA(plan, Motion)) + ctx->slice_index = ((Motion *) plan)->motionID; + + if (IsA(plan, HashJoin)) + anser_try_inject((HashJoin *) plan, ctx); + + anser_inject_walk(outerPlan(plan), ctx); + anser_inject_walk(innerPlan(plan), ctx); + if (IsA(plan, CustomScan)) + { + ListCell *lc; + + foreach(lc, ((CustomScan *) plan)->custom_plans) + anser_inject_walk((Plan *) lfirst(lc), ctx); + } + + ctx->slice_index = save_slice; +} + +/* + * How many processes execute a slice -- which is how many producers will + * publish a part, and therefore how many the coordinator must wait for. + * + * This mirrors FillSliceGangInfo (execUtils.c): a slice runs + * numsegments * parallel_workers processes, with parallel_workers == 0 meaning + * one. Both optimizers are covered, and they do differ: for the same query the + * Postgres planner may give a serial build slice (Broadcast Motion 2:8 over a + * plain Seq Scan, so 2 producers) where GPORCA gives a parallel one (Broadcast + * Motion 16:16 over a Parallel Seq Scan, so 16). Reading it from the slice + * means neither case needs special handling. + */ +static int +anser_slice_producers(AnserInjectCtx *ctx, int slice_index) +{ + PlanSlice *slice; + int factor; + + if (ctx->stmt->slices == NULL || + slice_index < 0 || slice_index >= ctx->stmt->numSlices) + return 0; /* unknown: caller declines to inject */ + + slice = &ctx->stmt->slices[slice_index]; + + /* The coordinator's own slice is one process and has no gang. */ + if (slice->gangType == GANGTYPE_UNALLOCATED || + slice->gangType == GANGTYPE_ENTRYDB_READER) + return 1; + + factor = slice->parallel_workers > 0 ? slice->parallel_workers : 1; + if (slice->numsegments <= 0) + return 0; + + return slice->numsegments * factor; +} diff --git a/gpcontrib/anser/src/anserplanexec.c b/gpcontrib/anser/src/anserplanexec.c new file mode 100644 index 00000000000..3680b9215c8 --- /dev/null +++ b/gpcontrib/anser/src/anserplanexec.c @@ -0,0 +1,803 @@ +/*------------------------------------------------------------------------- + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * anserplanexec.c + * CustomScan providers that carry the Anser runtime bloom filter through + * the executor: a pass-through "producer" above the hash build input that + * observes the build join key and publishes a bloom filter, and a + * pass-through "consumer" above the probe scan that prunes rows whose key is + * definitely absent from the received (unioned) filter. + * + * Both are thin drivers over the helper library in + * anserbloom.h, which selects transport by role + * (segment -> libpq to QD; coordinator -> direct shmem) and unions parts. The + * plan-injection pass (anserplan.c) builds the nodes and stashes the parameters + * in CustomScan.custom_private using the layout in AnserRfPrivateIndex below. + * + * IDENTIFICATION + * gpcontrib/anser/src/anserplanexec.c + * + *------------------------------------------------------------------------- + */ +#include "postgres.h" + +#include "access/skey.h" +#include "anser.h" +#include "anserbloom.h" +#include "anserfilter.h" +#include "anserplan.h" +#include "cdb/cdbutil.h" +#include "cdb/cdbvars.h" +#include "commands/explain.h" +#include "executor/executor.h" +#include "lib/bloomfilter.h" +#include "nodes/extensible.h" +#include "nodes/makefuncs.h" +#include "nodes/nodeFuncs.h" +#include "nodes/pg_list.h" +#include "nodes/value.h" +#include "utils/guc.h" + +/* + * Positional layout of CustomScan.custom_private shared with anserplan.c. + * Integer nodes except the last two, which are Strings. Both providers read + * the same list (each ignores fields it does not need). + */ +typedef enum AnserRfPrivateIndex +{ + ANSER_RF_PRIV_CONDITION_ID = 0, /* Integer: channel condition_id */ + ANSER_RF_PRIV_KEY_ATTNO, /* Integer: build/probe key attno */ + ANSER_RF_PRIV_TOTAL_ELEMS, /* Integer: bloom sizing (producer) */ + ANSER_RF_PRIV_MAX_PAYLOAD, /* Integer: bloom sizing (producer) */ + ANSER_RF_PRIV_PLANNED_BYTES, /* Integer: planned bitset bytes (EXPLAIN) */ + ANSER_RF_PRIV_N_PRODUCERS, /* Integer: processes publishing a part */ + ANSER_RF_PRIV_DEFER_FIRST, /* Integer: consumer must let the join's + * empty-outer prefetch through */ + ANSER_RF_PRIV_CONDITION_KEY, /* String: channel condition_key */ + ANSER_RF_PRIV__COUNT +} AnserRfPrivateIndex; + +/* Registration-timeout for the consumer's wait-for-registration phase. */ +#define ANSER_RF_REGISTRATION_TIMEOUT_MS ((long) gp_anser_timeout_ms) + +/* + * Producer scan state: feeds the build key of every child tuple into the + * filter and publishes this backend's part once the child is exhausted. + */ +typedef struct AnserBloomProduceScanState +{ + CustomScanState csstate; + AnserBloomFilterProduceState *produce; + AttrNumber key_attno; + int64 planned_bytes; + bool started; /* this process actually executed the node */ + bool published; +} AnserBloomProduceScanState; + +/* + * Consumer scan state: receives and unions the bloom parts on first use, + * then prunes probe rows whose key is definitely absent from the filter. + */ +typedef struct AnserBloomConsumeScanState +{ + CustomScanState csstate; + AnserBloomFilterConsumeState *consume; + bloom_filter *filter; /* NULL => fail open (pass everything) */ + AttrNumber key_attno; + int64 planned_bytes; + bool received; /* have we run the receive/union yet? */ + bool defer_first; /* this join prefetches one outer tuple before + * building its hash; see anser_consume_receive() */ + bool probed; /* has the join pulled a tuple from us before? */ + bool pushed_down; /* filter handed to the child scan as an + * SK_BLOOM_FILTER scan key (the scan filters, + * we pass through) */ +} AnserBloomConsumeScanState; + +/* Provider callbacks. */ +static Node *anser_produce_create_state(CustomScan *cscan); +static void anser_produce_begin(CustomScanState *node, EState *estate, int eflags); +static TupleTableSlot *anser_produce_exec(CustomScanState *node); +static void anser_produce_end(CustomScanState *node); +static void anser_produce_rescan(CustomScanState *node); +static void anser_produce_explain(CustomScanState *node, List *ancestors, + ExplainState *es); + +static Node *anser_consume_create_state(CustomScan *cscan); +static void anser_consume_begin(CustomScanState *node, EState *estate, int eflags); +static TupleTableSlot *anser_consume_exec(CustomScanState *node); +static void anser_consume_end(CustomScanState *node); +static void anser_consume_rescan(CustomScanState *node); +static void anser_consume_explain(CustomScanState *node, List *ancestors, + ExplainState *es); + +static const CustomScanMethods anser_produce_scan_methods = +{ + .CustomName = "Anser Bloom Producer", + .CreateCustomScanState = anser_produce_create_state, +}; + +static const CustomScanMethods anser_consume_scan_methods = +{ + .CustomName = "Anser Bloom Consumer", + .CreateCustomScanState = anser_consume_create_state, +}; + +static const CustomExecMethods anser_produce_exec_methods = +{ + .CustomName = "Anser Bloom Producer", + .BeginCustomScan = anser_produce_begin, + .ExecCustomScan = anser_produce_exec, + .EndCustomScan = anser_produce_end, + .ReScanCustomScan = anser_produce_rescan, + .ExplainCustomScan = anser_produce_explain, +}; + +static const CustomExecMethods anser_consume_exec_methods = +{ + .CustomName = "Anser Bloom Consumer", + .BeginCustomScan = anser_consume_begin, + .ExecCustomScan = anser_consume_exec, + .EndCustomScan = anser_consume_end, + .ReScanCustomScan = anser_consume_rescan, + .ExplainCustomScan = anser_consume_explain, +}; + +void +AnserRegisterRuntimeFilterMethods(void) +{ + RegisterCustomScanMethods(&anser_produce_scan_methods); + RegisterCustomScanMethods(&anser_consume_scan_methods); +} + +/* ---- node builders (called from the injection pass in anserplan.c) ---- */ + +/* + * Identity output targetlist for a pass-through CustomScan: a Var per child + * column referencing the scan tuple via INDEX_VAR, so ExecScan projects the + * child tuple through unchanged. (Post-setrefs we build this by hand rather + * than relying on set_customscan_references.) + */ +static List * +anser_rf_identity_tlist(List *child_tlist) +{ + List *tlist = NIL; + ListCell *lc; + AttrNumber attno = 0; + + foreach(lc, child_tlist) + { + TargetEntry *tle = (TargetEntry *) lfirst(lc); + Var *var; + + attno++; + var = makeVar(INDEX_VAR, attno, + exprType((Node *) tle->expr), + exprTypmod((Node *) tle->expr), + exprCollation((Node *) tle->expr), + 0); + tlist = lappend(tlist, + makeTargetEntry((Expr *) var, attno, + tle->resname ? pstrdup(tle->resname) : NULL, + tle->resjunk)); + } + + return tlist; +} + +static CustomScan * +anser_build_rf_scan(const CustomScanMethods *methods, Plan *child, + AttrNumber key_attno, uint32 condition_id, + const char *condition_key, int64 total_elems, + Size max_payload_bytes, int64 planned_bytes, + int n_producers, bool defer_first) +{ + CustomScan *cs = makeNode(CustomScan); + List *priv = NIL; + + /* custom_private, in AnserRfPrivateIndex order. */ + priv = lappend(priv, makeInteger((int) condition_id)); + priv = lappend(priv, makeInteger((int) key_attno)); + priv = lappend(priv, makeInteger((int) total_elems)); + priv = lappend(priv, makeInteger((int) max_payload_bytes)); + priv = lappend(priv, makeInteger((int) planned_bytes)); + priv = lappend(priv, makeInteger(n_producers)); + priv = lappend(priv, makeInteger(defer_first ? 1 : 0)); + priv = lappend(priv, makeString(pstrdup(condition_key))); + + cs->scan.plan.targetlist = anser_rf_identity_tlist(child->targetlist); + cs->scan.plan.qual = NIL; + cs->scan.plan.lefttree = NULL; + cs->scan.plan.righttree = NULL; + cs->scan.plan.startup_cost = child->startup_cost; + cs->scan.plan.total_cost = child->total_cost; + cs->scan.plan.plan_rows = child->plan_rows; + cs->scan.plan.plan_width = child->plan_width; + /* + * Not parallel-aware, and that is not an omission. parallel_aware only + * drives PostgreSQL's DSM callbacks, which are reached from Gather; an MPP + * parallel slice has no Gather and no ParallelContext, so the callbacks + * would never fire. Each process of the slice is a full QE with its own + * dispatch connection, so each publishes and subscribes for itself and the + * coordinator folds the lot -- which is why n_producers above has to be the + * slice's process count rather than the segment count. + */ + cs->scan.plan.parallel_aware = false; + cs->scan.plan.parallel_safe = child->parallel_safe; + cs->scan.plan.flow = (Flow *) copyObject(child->flow); + cs->scan.scanrelid = 0; + cs->flags = 0; + cs->custom_plans = list_make1(child); + cs->custom_exprs = NIL; + cs->custom_private = priv; + cs->custom_scan_tlist = copyObject(child->targetlist); + cs->methods = methods; + + return cs; +} + +/* + * Is this one of our own nodes? + * + * The injection pass has to recognise them because it can meet one where it + * expects a scan: two joins whose keys trace to the same base relation both + * want to filter it, and the second arrives to find the first already there. + * Compared by method table rather than by name, and only sound on the + * coordinator -- a QE resolves these pointers by name when it deserializes the + * plan, which is long after the only pass that asks. + */ +bool +AnserIsRuntimeFilterScan(const Plan *plan) +{ + const CustomScan *cscan; + + if (plan == NULL || !IsA(plan, CustomScan)) + return false; + + cscan = (const CustomScan *) plan; + return cscan->methods == &anser_produce_scan_methods || + cscan->methods == &anser_consume_scan_methods; +} + +CustomScan * +AnserBuildBloomProducerScan(Plan *child, AttrNumber key_attno, + uint32 condition_id, const char *condition_key, + int64 total_elems, Size max_payload_bytes, + int64 planned_bytes, int n_producers) +{ + return anser_build_rf_scan(&anser_produce_scan_methods, child, key_attno, + condition_id, condition_key, total_elems, + max_payload_bytes, planned_bytes, n_producers, + false); +} + +CustomScan * +AnserBuildBloomConsumerScan(Plan *child, AttrNumber key_attno, + uint32 condition_id, const char *condition_key, + int64 total_elems, Size max_payload_bytes, + int64 planned_bytes, int n_producers, + bool defer_first) +{ + return anser_build_rf_scan(&anser_consume_scan_methods, child, key_attno, + condition_id, condition_key, total_elems, + max_payload_bytes, planned_bytes, n_producers, + defer_first); +} + +/* ---- shared helpers ---- */ + +static void +anser_rf_build_key(CustomScan *cscan, AnserChannelKey *key) +{ + List *priv = cscan->custom_private; + int condition_id = intVal(list_nth(priv, ANSER_RF_PRIV_CONDITION_ID)); + char *condition_key = strVal(list_nth(priv, ANSER_RF_PRIV_CONDITION_KEY)); + + MemSet(key, 0, sizeof(*key)); + key->gp_session_id = gp_session_id; + key->gp_command_count = gp_command_count; + key->condition_id = (uint32) condition_id; + strlcpy(key->condition_key, condition_key, ANSER_CONDITION_KEY_SIZE); +} + +/* + * How many parts the coordinator must fold, and this backend's part index. + * + * The count comes from the plan, not from getgpsegmentCount(), because a + * parallel slice runs numsegments * parallel_workers processes and every one + * of them publishes its own part. Counting segments instead would make the + * coordinator declare the channel complete after the first nsegments parts + * and hand consumers a filter missing most of the build keys -- a bloom filter + * short of keys gives false negatives, which silently drops joinable rows. + * Only the planner knows the slice width, so it stamps it into custom_private + * (see AnserRuntimeFilterSize's caller in anserplan.c). + * + * part_index is diagnostics only: completion is by count, and the serialized + * part carries its own (0, 1). Under parallel execution the workers of one + * segment therefore share an index, which is why the trace prints the pid too. + */ +static void +anser_rf_part_info(CustomScan *cscan, uint32 *part_index, uint32 *total_parts) +{ + int n_producers = intVal(list_nth(cscan->custom_private, + ANSER_RF_PRIV_N_PRODUCERS)); + + if (Gp_role == GP_ROLE_EXECUTE) + { + *part_index = (uint32) GpIdentity.segindex; + *total_parts = (uint32) Max(n_producers, 1); + } + else + { + /* + * The coordinator produces locally as a single part. This is the + * whole-plan-on-the-QD case (no gang), not the parallel one. + */ + *part_index = 0; + *total_parts = 1; + } +} + +/* Move the child tuple into our scan slot (positional pass-through). */ +static TupleTableSlot * +anser_rf_child_slot(CustomScanState *node, TupleTableSlot *childslot) +{ + ExecCopySlot(node->ss.ss_ScanTupleSlot, childslot); + return node->ss.ss_ScanTupleSlot; +} + +static bool +anser_rf_recheck(CustomScanState *node, TupleTableSlot *slot) +{ + return true; +} + +/* ---- producer ---- */ + +static Node * +anser_produce_create_state(CustomScan *cscan) +{ + AnserBloomProduceScanState *st = (AnserBloomProduceScanState *) + newNode(sizeof(AnserBloomProduceScanState), T_CustomScanState); + + st->csstate.methods = &anser_produce_exec_methods; + return (Node *) st; +} + +static void +anser_produce_begin(CustomScanState *node, EState *estate, int eflags) +{ + AnserBloomProduceScanState *st = (AnserBloomProduceScanState *) node; + CustomScan *cscan = (CustomScan *) node->ss.ps.plan; + List *priv = cscan->custom_private; + Plan *child = (Plan *) linitial(cscan->custom_plans); + AnserChannelKey key; + uint32 part_index; + uint32 total_parts; + int64 total_elems = intVal(list_nth(priv, ANSER_RF_PRIV_TOTAL_ELEMS)); + Size max_payload = (Size) intVal(list_nth(priv, ANSER_RF_PRIV_MAX_PAYLOAD)); + + st->key_attno = (AttrNumber) intVal(list_nth(priv, ANSER_RF_PRIV_KEY_ATTNO)); + st->planned_bytes = intVal(list_nth(priv, ANSER_RF_PRIV_PLANNED_BYTES)); + st->started = false; + st->published = false; + + anser_rf_build_key(cscan, &key); + anser_rf_part_info(cscan, &part_index, &total_parts); + + st->produce = ExecInitAnserBloomFilterProduce(&key, total_elems, max_payload, + part_index, total_parts); + ANSER_DEBUG("anser: producer init cond=%u part=%u/%u elems=" INT64_FORMAT " payload=%zu state=%s", + key.condition_id, part_index, total_parts, total_elems, + max_payload, st->produce != NULL ? "ok" : "NULL"); + + node->custom_ps = list_make1(ExecInitNode(child, estate, eflags)); +} + +/* + * Pass-through that feeds the build key of every child tuple into the filter, + * and publishes once the child is exhausted. + */ +static TupleTableSlot * +anser_produce_next(CustomScanState *node) +{ + AnserBloomProduceScanState *st = (AnserBloomProduceScanState *) node; + PlanState *child = (PlanState *) linitial(node->custom_ps); + TupleTableSlot *slot; + + /* + * Every process that deserializes the plan builds this node, but only the + * ones running its slice ever execute it; the rest must stay silent. + */ + if (!st->started) + { + st->started = true; + + /* + * No filter means the estimate that justified this node was wrong by + * enough that one cannot usefully be built. Say so now, on the first + * tuple, rather than after scanning the whole build side: the consumers + * are sitting on their timeout, and every millisecond of it is wasted. + * + * This is also the earliest point at which cancelling is safe. Doing + * it at ExecInit time would cancel channels from the coordinator, which + * builds this node for every slice but executes none of them. + */ + if (st->produce != NULL && !st->published && + !ExecAnserBloomFilterProduceHasFilter(st->produce)) + { + ANSER_DEBUG("anser: producer has no usable filter; cancelling before the scan"); + (void) ExecAnserBloomFilterProduceCancel(st->produce); + st->published = true; + } + } + + slot = ExecProcNode(child); + + if (TupIsNull(slot)) + { + if (!st->published) + { + ANSER_DEBUG("anser: producer child exhausted, publishing (state=%s)", + st->produce != NULL ? "ok" : "NULL"); + (void) ExecAnserBloomFilterProducePublish(st->produce); + st->published = true; + } + return NULL; + } + + if (st->produce != NULL) + { + bool isnull; + Datum value = slot_getattr(slot, st->key_attno, &isnull); + + ExecAnserBloomFilterProduceAddDatum(st->produce, value, isnull); + } + + return anser_rf_child_slot(node, slot); +} + +static TupleTableSlot * +anser_produce_exec(CustomScanState *node) +{ + return ExecScan(&node->ss, + (ExecScanAccessMtd) anser_produce_next, + (ExecScanRecheckMtd) anser_rf_recheck); +} + +static void +anser_produce_end(CustomScanState *node) +{ + AnserBloomProduceScanState *st = (AnserBloomProduceScanState *) node; + + /* + * A producer that ran but never published was abandoned mid-scan (squelch, + * or an error upstream), and consumers must be told so rather than left + * waiting for a part that will never come. A producer that never ran at + * all belongs to another slice: cancelling here would destroy a channel + * this process has no part in -- which is exactly what the coordinator + * used to do to every runtime filter. + */ + if (st->started && !st->published && st->produce != NULL) + { + (void) ExecAnserBloomFilterProduceCancel(st->produce); + st->published = true; + } + + if (st->produce != NULL) + ExecEndAnserBloomFilterProduce(st->produce); + st->produce = NULL; + if (node->custom_ps != NIL) + ExecEndNode((PlanState *) linitial(node->custom_ps)); +} + +static void +anser_produce_rescan(CustomScanState *node) +{ + if (node->custom_ps != NIL) + ExecReScan((PlanState *) linitial(node->custom_ps)); +} + +static void +anser_produce_explain(CustomScanState *node, List *ancestors, ExplainState *es) +{ + AnserBloomProduceScanState *st = (AnserBloomProduceScanState *) node; + char buf[64]; + + ExplainPropertyInteger("Bloom Filter Size", "bytes", st->planned_bytes, es); + + /* + * The realized filter size is planned_bytes by construction: producer and + * consumer build identical filters from the same plan parameters (see + * anser_rf_size in anserplan.c), so no execution stats are needed. + */ + snprintf(buf, sizeof(buf), "memory=" INT64_FORMAT "kB", + st->planned_bytes / 1024); + ExplainPropertyText("Bloom Filter Stats", buf, es); +} + +/* ---- consumer ---- */ + +static Node * +anser_consume_create_state(CustomScan *cscan) +{ + AnserBloomConsumeScanState *st = (AnserBloomConsumeScanState *) + newNode(sizeof(AnserBloomConsumeScanState), T_CustomScanState); + + st->csstate.methods = &anser_consume_exec_methods; + return (Node *) st; +} + +static void +anser_consume_begin(CustomScanState *node, EState *estate, int eflags) +{ + AnserBloomConsumeScanState *st = (AnserBloomConsumeScanState *) node; + CustomScan *cscan = (CustomScan *) node->ss.ps.plan; + List *priv = cscan->custom_private; + Plan *child = (Plan *) linitial(cscan->custom_plans); + AnserChannelKey key; + uint32 part_index; + uint32 expected_parts; + int64 total_elems = intVal(list_nth(priv, ANSER_RF_PRIV_TOTAL_ELEMS)); + Size max_payload = (Size) intVal(list_nth(priv, ANSER_RF_PRIV_MAX_PAYLOAD)); + + st->key_attno = (AttrNumber) intVal(list_nth(priv, ANSER_RF_PRIV_KEY_ATTNO)); + st->planned_bytes = intVal(list_nth(priv, ANSER_RF_PRIV_PLANNED_BYTES)); + st->filter = NULL; + st->received = false; + st->defer_first = (intVal(list_nth(priv, ANSER_RF_PRIV_DEFER_FIRST)) != 0); + st->probed = false; + st->pushed_down = false; + + anser_rf_build_key(cscan, &key); + anser_rf_part_info(cscan, &part_index, &expected_parts); + + st->consume = ExecInitAnserBloomFilterConsume(&key, total_elems, max_payload, + expected_parts); + + node->custom_ps = list_make1(ExecInitNode(child, estate, eflags)); +} + +/* + * On first call, block to receive and union the bloom parts. A NULL filter + * (channel cancelled / unreachable / feature degraded) means fail open: pass + * every row through unfiltered. + */ +static void +anser_consume_receive(AnserBloomConsumeScanState *st) +{ + if (st->received) + return; + + /* + * Not on the first tuple, when the join is one that prefetches. + * + * ExecHashJoin pulls one tuple from its outer side -- us -- before it + * builds the hash table, so that it can skip the build when the outer side + * turns out to be empty (nodeHashjoin.c, the hj_FirstOuterTupleSlot fetch + * in HJ_BUILD_HASHTABLE). Waiting there waits for the inner side the same + * join has not started; when the join is co-located -- no Motion between + * the two, producer and consumer in one slice -- that is this very + * process, and nothing but the deadline ends it. + * + * Letting that one tuple through unfiltered costs a wasted probe and can + * never cost a row: the join still applies the real condition, and passing + * rows a filter would have rejected is the safe direction. By the next + * call the hash table is built, so the producer's scan has been drained + * and its part published. + * + * It is not free, which is why it is conditional: the scan has started by + * then, and anser_consume_pushdown() has to run before that to get its key + * into the scan descriptor. So a deferred consumer keeps the filter and + * probes it itself. The planner sets the flag whenever it cannot rule the + * prefetch out, which is the conservative direction -- guessing "no + * prefetch" wrongly deadlocks, guessing "prefetch" wrongly only gives up + * pushdown. + */ + if (st->defer_first && !st->probed) + { + st->probed = true; + return; + } + + st->received = true; + + if (st->consume == NULL) + return; + + if (ExecAnserBloomFilterConsume(st->consume, ANSER_RF_REGISTRATION_TIMEOUT_MS)) + st->filter = ExecAnserBloomFilterConsumerGetFilter(st->consume); + else + st->filter = NULL; + + /* + * Diagnostic: a NULL filter means we fail open (no pruning). Log why -- + * cancelled delivery vs. too few bloom parts unioned -- so runtime-filter + * misbehavior is visible in the server log. + */ + if (st->filter == NULL) + elog(LOG, + "anser bloom consumer: no filter, failing open (cancelled=%d, received_parts=%u)", + ExecAnserBloomFilterConsumerWasCancelled(st->consume), + ExecAnserBloomFilterConsumerReceivedParts(st->consume)); +} + +/* + * Hand the received filter to the child SeqScan as an SK_BLOOM_FILTER scan + * key -- the exact structure PassByBloomFilter consumes (nodeSeqscan.c), so + * the scan does the pruning itself and a table AM that supports + * SCAN_SUPPORT_RUNTIME_FILTER may additionally use the key for block-level + * pruning. Runs before the child's first ExecProcNode, so the key is in + * node->filters when the scan descriptor is created. The filter stays owned + * by us; anser_consume_end ends the child before freeing it. + */ +static void +anser_consume_pushdown(CustomScanState *node) +{ + AnserBloomConsumeScanState *st = (AnserBloomConsumeScanState *) node; + PlanState *child = (PlanState *) linitial(node->custom_ps); + SeqScanState *sss; + ScanKey sk; + MemoryContext oldcxt; + + if (!gp_enable_runtime_filter_pushdown || !IsA(child, SeqScanState)) + return; + sss = (SeqScanState *) child; + if (!sss->filter_in_seqscan) + return; + + oldcxt = MemoryContextSwitchTo(node->ss.ps.state->es_query_cxt); + sk = (ScanKey) palloc0(sizeof(ScanKeyData)); + sk->sk_attno = st->key_attno; + sk->sk_flags = SK_BLOOM_FILTER; + sk->sk_argument = PointerGetDatum(st->filter); + sss->filters = lappend(sss->filters, sk); + MemoryContextSwitchTo(oldcxt); + + st->pushed_down = true; +} + +static TupleTableSlot * +anser_consume_next(CustomScanState *node) +{ + AnserBloomConsumeScanState *st = (AnserBloomConsumeScanState *) node; + PlanState *child = (PlanState *) linitial(node->custom_ps); + + anser_consume_receive(st); + + /* + * When the pushdown machinery is enabled, hand the filter to the scan and + * pass tuples through untouched; otherwise probe the filter here. + */ + if (st->filter != NULL && !st->pushed_down) + anser_consume_pushdown(node); + + for (;;) + { + TupleTableSlot *slot = ExecProcNode(child); + bool isnull; + Datum value; + + if (TupIsNull(slot)) + return NULL; + + /* + * Fail open (no usable filter) or pushed down (the scan filters via + * PassByBloomFilter): pass everything. + */ + if (st->filter == NULL || st->pushed_down) + return anser_rf_child_slot(node, slot); + + value = slot_getattr(slot, st->key_attno, &isnull); + + /* NULLs never match an equijoin key; let the join handle them. */ + if (!isnull) + { + /* + * Count lookups/prunes via instrumentation (nfiltered1/nfiltered2), + * not local state: these fields ride the CdbExplain_StatInst wire + * format back to the QD, so EXPLAIN ANALYZE shows them for + * segment-executed nodes too. + */ + InstrCountFiltered1(node, 1); + if (bloom_lacks_element(st->filter, (unsigned char *) &value, + sizeof(Datum))) + { + /* Definitely absent from the build side -> prune. */ + InstrCountFiltered2(node, 1); + continue; + } + } + + return anser_rf_child_slot(node, slot); + } +} + +static TupleTableSlot * +anser_consume_exec(CustomScanState *node) +{ + return ExecScan(&node->ss, + (ExecScanAccessMtd) anser_consume_next, + (ExecScanRecheckMtd) anser_rf_recheck); +} + +static void +anser_consume_end(CustomScanState *node) +{ + AnserBloomConsumeScanState *st = (AnserBloomConsumeScanState *) node; + + /* + * End the child first: when the filter was pushed down, the child's scan + * key references it, so the filter must outlive the child node. + */ + if (node->custom_ps != NIL) + ExecEndNode((PlanState *) linitial(node->custom_ps)); + if (st->consume != NULL) + ExecEndAnserBloomFilterConsume(st->consume); + st->consume = NULL; + st->filter = NULL; +} + +static void +anser_consume_rescan(CustomScanState *node) +{ + if (node->custom_ps != NIL) + ExecReScan((PlanState *) linitial(node->custom_ps)); +} + +static void +anser_consume_explain(CustomScanState *node, List *ancestors, ExplainState *es) +{ + AnserBloomConsumeScanState *st = (AnserBloomConsumeScanState *) node; + + ExplainPropertyInteger("Bloom Filter Size", "bytes", st->planned_bytes, es); + + if (es->analyze && node->ss.ps.instrument != NULL) + { + char buf[128]; + double nloops = node->ss.ps.instrument->nloops; + double nfiltered = node->ss.ps.instrument->nfiltered2; + double nchecked = node->ss.ps.instrument->nfiltered1; + + /* + * checked/rejected come from nfiltered1/nfiltered2, which cdbexplain + * transports from the segments and deposits into this node's + * instrument. As with all per-node EXPLAIN ANALYZE stats in MPP, + * these are the winning segment's values (max ntuples/nloops), not a + * cluster-wide sum. memory is planned_bytes: the unioned filter's + * size is identical on every segment by construction (anser_rf_size). + * When the filter was pushed into the child scan, pruning happens + * there instead (its "Rows Removed by Pushdown Runtime Filter" line), + * so our counters stay zero. + */ + if (st->pushed_down) + snprintf(buf, sizeof(buf), + "memory=" INT64_FORMAT "kB (pushed down to Seq Scan)", + st->planned_bytes / 1024); + else + snprintf(buf, sizeof(buf), + "memory=" INT64_FORMAT "kB checked=%.0f rejected=%.0f", + st->planned_bytes / 1024, nchecked, nfiltered); + ExplainPropertyText("Bloom Filter Stats", buf, es); + + if (!st->pushed_down && nloops > 0) + ExplainPropertyFloat("Rows Removed by Bloom Filter", NULL, + nfiltered / nloops, 0, es); + } +} diff --git a/gpcontrib/anser/src/ansersideband.c b/gpcontrib/anser/src/ansersideband.c new file mode 100644 index 00000000000..babc7bbaba8 --- /dev/null +++ b/gpcontrib/anser/src/ansersideband.c @@ -0,0 +1,641 @@ +/*------------------------------------------------------------------------- + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * ansersideband.c + * Segment side of the dispatch-connection transport. + * + * A producer sends its part as a NOTIFY and moves on; a consumer subscribes, + * then blocks on its own dispatch socket until the coordinator pushes the + * merged filter back. Both directions reuse the connection the dispatcher + * already owns, so there is no second connection to open and nothing to + * authenticate. + * + * Reading the frontend socket in the middle of executing a query is the + * pattern cdb_sequence_nextval_qe() established (commands/sequence.c); the + * loop below mirrors its use of pq_startmsgread/pq_getbyte_if_available, but + * sleeps on the socket instead of spinning. + * + * IDENTIFICATION + * gpcontrib/anser/src/ansersideband.c + * + *------------------------------------------------------------------------- + */ +#include "postgres.h" + +#include "anser.h" +#include "ansersideband.h" +#include "cdb/cdbvars.h" +#include "commands/async.h" +#include "common/base64.h" +#include "libpq/libpq-be.h" +#include "libpq/libpq.h" +#include "libpq/pqformat.h" +#include "miscadmin.h" +#include "nodes/pg_list.h" +#include "port/pg_bswap.h" +#include "port/pg_crc32c.h" +#include "storage/latch.h" +#include "tcop/dest.h" +#include "tcop/tcopprot.h" +#include "utils/memutils.h" +#include "utils/timestamp.h" +#include "utils/wait_event.h" + +/* How long to sleep between wakeups while waiting for a delivery. */ +#define ANSER_SIDEBAND_POLL_MS 100L + +/* + * A delivery that arrived while we were waiting for a different channel. + * + * One slice can host consumers for more than one condition, and the + * coordinator pushes each channel as soon as it completes, so messages can + * arrive in an order we did not ask for. Rather than discard them (which + * would cost that consumer its filter), park them here and check the inbox + * before touching the socket. + */ +typedef struct AnserInboxEntry +{ + AnserChannelKey key; + char payload_type; + char *payload; /* NULL when cancelled */ + Size payload_len; + bool cancelled; +} AnserInboxEntry; + +static List *AnserInbox = NIL; + +static bool anser_sideband_send(const char *payload); +static bool anser_inbox_take(const AnserChannelKey *key, char payload_type, + void **payload, Size *payload_len, bool *cancelled); +static bool anser_sideband_read_one(long timeout_ms); + +/* + * Publish one part. Fire-and-forget: the coordinator does not acknowledge, + * because nothing on this side needs to wait for it. + */ +bool +AnserSidebandPublish(const AnserChannelKey *channel_key, char payload_type, + uint32 part_index, uint32 total_parts, + const void *payload, Size payload_len, bool cancelled) +{ + char *msg; + int flags = cancelled ? ANSER_WIRE_F_CANCELLED : 0; + bool ok; + + if (channel_key == NULL) + return false; + + if (!cancelled && payload_len > (Size) gp_anser_max_info_size) + { + /* Too large to ship; tell the coordinator so consumers stop waiting. */ + flags = ANSER_WIRE_F_CANCELLED; + payload = NULL; + payload_len = 0; + } + + msg = AnserWireFormat(channel_key, ANSER_WIRE_KIND_PART, payload_type, + part_index, total_parts, flags, + (flags & ANSER_WIRE_F_CANCELLED) ? NULL : payload, + (flags & ANSER_WIRE_F_CANCELLED) ? 0 : payload_len); + ok = anser_sideband_send(msg); + ANSER_DEBUG("anser: seg%d published cond=%u type=%c part=%u/%u bytes=%zu cancelled=%d sent=%d", + GpIdentity.segindex, channel_key->condition_id, payload_type, + part_index, total_parts, payload_len, + (flags & ANSER_WIRE_F_CANCELLED) ? 1 : 0, ok ? 1 : 0); + pfree(msg); + + return ok; +} + +/* + * Subscribe, then wait for the merged payload. + * + * Returns true with *payload set, or false for "run unfiltered" -- including + * on timeout. The deadline exists because a producer that gets squelched + * never publishes anything: ExecSquelchNode does not call CustomScan + * callbacks, it only marks the node (execAmi.c), so without a deadline this + * wait could outlive the reason for it. + */ +bool +AnserSidebandConsumeWait(const AnserChannelKey *channel_key, char payload_type, + void **payload, Size *payload_len, + bool *cancelled, long timeout_ms) +{ + char *msg; + TimestampTz start; + int reads = 0; + + if (payload != NULL) + *payload = NULL; + if (payload_len != NULL) + *payload_len = 0; + if (cancelled != NULL) + *cancelled = false; + + if (channel_key == NULL || MyProcPort == NULL || + MyProcPort->sock == PGINVALID_SOCKET) + { + /* + * No way to reach the coordinator from here, so this consumer will run + * unfiltered without ever having asked for anything. Which of the + * three reasons it was matters: a process with no Port is one the + * sideband was never designed for, while a bad socket on a process + * that has one is a different problem entirely. + */ + ANSER_DEBUG("anser: seg%d cannot consume cond=%u: key=%s port=%s sock=%d role=%d writer=%d dest=%d", + GpIdentity.segindex, + channel_key != NULL ? channel_key->condition_id : 0, + channel_key != NULL ? "ok" : "NULL", + MyProcPort != NULL ? "ok" : "NULL", + MyProcPort != NULL ? (int) MyProcPort->sock : -1, + (int) Gp_role, Gp_is_writer ? 1 : 0, + (int) whereToSendOutput); + return false; + } + + /* It may already be here: the coordinator pushes as soon as it can. */ + if (anser_inbox_take(channel_key, payload_type, payload, payload_len, cancelled)) + return payload != NULL && *payload != NULL; + + /* + * A subscription carries nothing, so its payload type is NONE -- what this + * consumer expects to receive is checked on delivery, not announced here. + */ + msg = AnserWireFormat(channel_key, ANSER_WIRE_KIND_SUBSCRIBE, + ANSER_PAYLOAD_NONE, 0, 0, 0, NULL, 0); + if (!anser_sideband_send(msg)) + { + ANSER_DEBUG("anser: seg%d could not subscribe to cond=%u (role=%d writer=%d dest=%d)", + GpIdentity.segindex, channel_key->condition_id, + (int) Gp_role, Gp_is_writer ? 1 : 0, + (int) whereToSendOutput); + pfree(msg); + return false; + } + pfree(msg); + ANSER_DEBUG("anser: seg%d subscribed cond=%u, waiting up to %ld ms", + GpIdentity.segindex, channel_key->condition_id, timeout_ms); + + start = GetCurrentTimestamp(); + for (;;) + { + CHECK_FOR_INTERRUPTS(); + + if (anser_inbox_take(channel_key, payload_type, payload, payload_len, + cancelled)) + return payload != NULL && *payload != NULL; + + if (timeout_ms >= 0 && + TimestampDifferenceExceeds(start, GetCurrentTimestamp(), timeout_ms)) + { + /* + * Report what we saw, not just that we waited: "nothing arrived" + * and "something arrived for another channel" are different bugs. + */ + ANSER_DEBUG("anser: seg%d gave up on cond=%u after %ld ms (read %d message(s), %d unclaimed)", + GpIdentity.segindex, channel_key->condition_id, + timeout_ms, reads, list_length(AnserInbox)); + return false; + } + + /* + * Any message we read lands in the inbox; the loop then rechecks + * whether it was the one we wanted. A read failure means the + * connection is gone, which the interconnect will report far more + * usefully than we can -- stop waiting and let the query run + * unfiltered. + */ + if (!anser_sideband_read_one(ANSER_SIDEBAND_POLL_MS)) + return false; + reads = list_length(AnserInbox); + } +} + +/* + * Wait briefly for one sideband message and stash it in the inbox. + * + * Returns false only when the connection is unusable; a timeout with nothing + * to read is a normal true. + */ +static bool +anser_sideband_read_one(long timeout_ms) +{ + unsigned char qtype; + int retval; + StringInfoData buf; + AnserInboxEntry *entry; + MemoryContext oldcxt; + char paytype; + uint32 crc; + int condid; + int flags; + int keylen; + int paylen; + const char *keyptr; + const char *payptr; + + pq_startmsgread(); + retval = pq_getbyte_if_available(&qtype); + if (retval == 0) + { + /* Nothing buffered: sleep on the socket rather than spinning. */ + pq_endmsgread(); + + ResetLatch(MyLatch); + (void) WaitLatchOrSocket(MyLatch, + WL_LATCH_SET | WL_SOCKET_READABLE | + WL_TIMEOUT | WL_EXIT_ON_PM_DEATH, + MyProcPort->sock, + timeout_ms, + PG_WAIT_EXTENSION); + return true; + } + + if (retval == EOF) + { + pq_endmsgread(); + elog(LOG, "anser: dispatch connection closed while awaiting a filter"); + return false; + } + + if (qtype != GP_SIDEBAND_MESSAGE) + { + /* + * Nothing else should reach us here, and if it does there is no way + * back: pq_getbyte_if_available() has already taken the byte and the + * backend has no means of pushing one in front of the stream, so this + * message can neither be handed to the command loop nor skipped + * safely. The connection is one byte short from here on. + * + * Terminating is therefore the only honest outcome. Returning and + * running unfiltered -- the instinct everywhere else in this code -- + * would be much worse than losing the filter: every later read lands + * at the wrong offset, and the failure surfaces somewhere else + * entirely as an absurd allocation request or "invalid frontend + * message type". It has to be FATAL rather than ERROR because the + * connection cannot be reused: an ERROR would return this QE to the + * gang pool with a desynchronised stream, poisoning a later query. + * + * cdb_sequence_nextval_qe() reaches the same conclusion at + * sequence.c:438, one level milder, because it runs where the gang is + * about to be torn down anyway. + */ + pq_endmsgread(); + ereport(FATAL, + (errcode(ERRCODE_PROTOCOL_VIOLATION), + errmsg("anser: unexpected message type '%c' while awaiting a filter", + (char) qtype), + errdetail("The message boundary was lost; this connection cannot be used further."))); + } + + initStringInfo(&buf); + if (pq_getmessage(&buf, gp_anser_max_info_size + ANSER_CONDITION_KEY_SIZE + 64) != 0) + { + /* + * pq_getmessage clears the reading-message flag when it succeeds, but + * not on its EOF paths; clear it by hand so we do not trip the + * assertion in a later pq_startmsgread. + */ + pq_endmsgread(); + pfree(buf.data); + elog(LOG, "anser: could not read filter message"); + return false; + } + + paytype = (char) pq_getmsgint(&buf, 4); + crc = (uint32) pq_getmsgint(&buf, 4); + condid = pq_getmsgint(&buf, 4); + flags = pq_getmsgint(&buf, 4); + keylen = pq_getmsgint(&buf, 4); + if (keylen < 0 || keylen >= ANSER_CONDITION_KEY_SIZE) + { + pfree(buf.data); + elog(LOG, "anser: filter message has a bad condition key"); + return false; + } + keyptr = pq_getmsgbytes(&buf, keylen); + paylen = pq_getmsgint(&buf, 4); + if (paylen < 0 || paylen > gp_anser_max_info_size) + { + pfree(buf.data); + elog(LOG, "anser: filter message has a bad length"); + return false; + } + payptr = paylen > 0 ? pq_getmsgbytes(&buf, paylen) : NULL; + + if (crc != (uint32) AnserWirePushCrc(paytype, (uint32) condid, + (uint32) flags, keyptr, keylen, + payptr, paylen)) + { + /* + * Drop it and keep waiting: the deadline will expire and this consumer + * will run unfiltered. Using the filter anyway is the one thing we must + * not do -- a bit corrupted 1 -> 0 silently drops joinable rows. + */ + pfree(buf.data); + elog(LOG, "anser: checksum mismatch on filter for condition %u; discarding", + (uint32) condid); + return true; + } + + /* + * The inbox outlives this call and the memory context it was reached in, + * so anchor it somewhere stable; AnserSidebandResetAll drops it. + */ + oldcxt = MemoryContextSwitchTo(TopMemoryContext); + entry = palloc0(sizeof(AnserInboxEntry)); + entry->key.gp_session_id = gp_session_id; + entry->key.gp_command_count = gp_command_count; + entry->key.condition_id = (uint32) condid; + memcpy(entry->key.condition_key, keyptr, keylen); + entry->key.condition_key[keylen] = '\0'; + entry->payload_type = paytype; + entry->cancelled = (flags & ANSER_WIRE_F_CANCELLED) != 0; + if (!entry->cancelled && paylen > 0) + { + entry->payload = palloc(paylen); + memcpy(entry->payload, payptr, paylen); + entry->payload_len = paylen; + } + AnserInbox = lappend(AnserInbox, entry); + MemoryContextSwitchTo(oldcxt); + ANSER_DEBUG("anser: seg%d received cond=%u type=%c bytes=%zu cancelled=%d", + GpIdentity.segindex, (uint32) condid, paytype, + entry->payload_len, entry->cancelled ? 1 : 0); + + pfree(buf.data); + return true; +} + +/* Claim a delivery for this channel, if one has arrived. */ +static bool +anser_inbox_take(const AnserChannelKey *key, char payload_type, void **payload, + Size *payload_len, bool *cancelled) +{ + ListCell *lc; + + foreach(lc, AnserInbox) + { + AnserInboxEntry *entry = (AnserInboxEntry *) lfirst(lc); + + if (entry->key.condition_id != key->condition_id || + strncmp(entry->key.condition_key, key->condition_key, + ANSER_CONDITION_KEY_SIZE) != 0) + continue; + + /* + * Right channel, wrong kind of information. The coordinator stamps + * the type from the parts it folded, so this means a producer and a + * consumer disagree about what the channel carries -- claim the entry + * to stop waiting on it, and treat it as a cancellation so this + * consumer runs unfiltered. An empty delivery carries no type to + * check. + */ + if (!entry->cancelled && entry->payload != NULL && + entry->payload_type != payload_type) + { + elog(WARNING, "anser: condition %u delivered payload type '%c', expected '%c'", + key->condition_id, entry->payload_type, payload_type); + entry->cancelled = true; + } + + if (cancelled != NULL) + *cancelled = entry->cancelled; + if (!entry->cancelled && entry->payload != NULL) + { + if (payload != NULL) + { + *payload = palloc(entry->payload_len); + memcpy(*payload, entry->payload, entry->payload_len); + } + if (payload_len != NULL) + *payload_len = entry->payload_len; + } + + AnserInbox = foreach_delete_current(AnserInbox, lc); + if (entry->payload != NULL) + pfree(entry->payload); + pfree(entry); + return true; + } + + return false; +} + +/* Drop any deliveries nobody claimed. */ +void +AnserSidebandResetInbox(void) +{ + ListCell *lc; + + foreach(lc, AnserInbox) + { + AnserInboxEntry *entry = (AnserInboxEntry *) lfirst(lc); + + if (entry->payload != NULL) + pfree(entry->payload); + pfree(entry); + } + list_free(AnserInbox); + AnserInbox = NIL; +} + +void +AnserSidebandResetAll(void) +{ + AnserSidebandResetInbox(); + AnserDispatchReset(); +} + +/* + * Build a QE -> QD payload: fixed-width header, then the key, then the base64 + * body. See ansersideband.h for the layout and AnserWireParse() for the + * reader. + */ +char * +AnserWireFormat(const AnserChannelKey *channel_key, char kind, + char payload_type, uint32 part_index, uint32 total_parts, + int flags, const void *payload, Size payload_len) +{ + StringInfoData buf; + char hdr[ANSER_WIRE_HDR_LEN + 1]; + int hdrlen; + int keylen = (int) strlen(channel_key->condition_key); + int bodylen = 0; + char *body = NULL; + pg_crc32c crc; + + /* + * What the checksum covers has to be exactly what goes on the wire, so the + * bytes it will cover are recorded here, as the body is encoded, rather + * than re-derived from the arguments afterwards. Deriving them again would + * mean covering a payload that was suppressed -- a cancelled message + * carries no body however it was called -- and the reader, seeing no body, + * would compute a different checksum and discard a perfectly good message. + */ + const void *crc_body = NULL; + Size crc_body_len = 0; + + if (!(flags & ANSER_WIRE_F_CANCELLED) && payload != NULL && payload_len > 0) + { + int maxlen = pg_b64_enc_len((int) payload_len); + + if (maxlen > ANSER_WIRE_MAX_BODYLEN) + { + /* + * No header can describe a body this large. gp_anser_max_info_size + * keeps us far away from this, so it is a belt-and-braces check on + * the field width rather than a reachable path. + */ + bodylen = 0; + flags |= ANSER_WIRE_F_CANCELLED; + } + else + { + body = palloc(maxlen + 1); + bodylen = pg_b64_encode((const char *) payload, (int) payload_len, + body, maxlen); + if (bodylen < 0) + { + pfree(body); + body = NULL; + bodylen = 0; + flags |= ANSER_WIRE_F_CANCELLED; + } + else + { + crc_body = payload; + crc_body_len = payload_len; + } + } + } + + Assert(keylen <= ANSER_WIRE_MAX_KEYLEN); + + /* + * Format the header with a zero CRC, checksum it together with the key and + * the raw body, then overwrite the CRC field in place -- it sits at a fixed + * offset, so one pass suffices and the checksum still covers every header + * field. Checksumming the body before base64 rather than after means a + * mangled encoding is caught too, and whether the body is covered at all is + * the payload type's call (anserpayload.h). + */ + hdrlen = snprintf(hdr, sizeof(hdr), ANSER_WIRE_HDR_FORMAT, + kind, payload_type, + (uint32) channel_key->gp_session_id, + (uint32) channel_key->gp_command_count, + channel_key->condition_id, + part_index, total_parts, + (uint32) flags, (uint32) keylen, (uint32) bodylen, 0U); + if (hdrlen != ANSER_WIRE_HDR_LEN) + { + /* + * Cannot happen -- every field is width-limited -- but complain rather + * than throw: this is reached from producer teardown, and the + * coordinator's length cross-check will reject the message anyway, + * which costs the filter and not the query. + */ + elog(WARNING, "anser: formatted a %d byte header, expected %d", + hdrlen, ANSER_WIRE_HDR_LEN); + } + + INIT_CRC32C(crc); + COMP_CRC32C(crc, hdr, ANSER_WIRE_CRC_OFFSET); + COMP_CRC32C(crc, channel_key->condition_key, keylen); + if (crc_body != NULL && AnserPayloadChecksumsBody(payload_type)) + COMP_CRC32C(crc, crc_body, crc_body_len); + FIN_CRC32C(crc); + snprintf(hdr + ANSER_WIRE_CRC_OFFSET, 9, "%08x", (uint32) crc); + + initStringInfo(&buf); + appendBinaryStringInfo(&buf, hdr, ANSER_WIRE_HDR_LEN); + appendBinaryStringInfo(&buf, channel_key->condition_key, keylen); + if (bodylen > 0) + appendBinaryStringInfo(&buf, body, bodylen); + + if (body != NULL) + pfree(body); + + return buf.data; +} + +/* + * Checksum of a QD -> QE push. Lives here, on the reading side, but is called + * from anserdispatch.c too: one definition means the writer and the reader + * cannot disagree about what is covered -- including whether the body is, which + * follows from the payload type. Integers go in network byte order so the + * value does not depend on the host. + */ +pg_crc32c +AnserWirePushCrc(char payload_type, uint32 condition_id, uint32 flags, + const char *key, int keylen, const void *body, int bodylen) +{ + pg_crc32c crc; + uint32 fields[4]; + uint32 belen; + + fields[0] = pg_hton32((uint32) (unsigned char) payload_type); + fields[1] = pg_hton32(condition_id); + fields[2] = pg_hton32(flags); + fields[3] = pg_hton32((uint32) keylen); + + INIT_CRC32C(crc); + COMP_CRC32C(crc, fields, sizeof(fields)); + if (keylen > 0) + COMP_CRC32C(crc, key, keylen); + belen = pg_hton32((uint32) bodylen); + COMP_CRC32C(crc, &belen, sizeof(belen)); + if (bodylen > 0 && AnserPayloadChecksumsBody(payload_type)) + COMP_CRC32C(crc, body, bodylen); + FIN_CRC32C(crc); + + return crc; +} + +/* + * Hand a payload to the coordinator. + * + * NotifyMyFrontEnd enforces no length limit of its own -- the ~8 KB cap + * applies to the SQL-level NOTIFY, which has to fit its queue page -- so a + * multi-megabyte part is fine here. The payload is base64, hence free of the + * NUL that would truncate it in pq_sendstring(). + */ +static bool +anser_sideband_send(const char *payload) +{ + if (whereToSendOutput != DestRemote) + { + /* + * Reached by any process that runs the plan without a frontend to + * answer to. Both directions of the protocol come through here, so + * this is the single place that says "this process is not on the + * sideband at all" -- for a publisher as much as for a subscriber. + */ + ANSER_DEBUG("anser: seg%d has no frontend to send on (role=%d writer=%d dest=%d port=%s sock=%d)", + GpIdentity.segindex, (int) Gp_role, Gp_is_writer ? 1 : 0, + (int) whereToSendOutput, + MyProcPort != NULL ? "ok" : "NULL", + MyProcPort != NULL ? (int) MyProcPort->sock : -1); + return false; + } + + NotifyMyFrontEnd(ANSER_NOTIFY_CHANNEL, payload, gp_session_id); + pq_flush(); + return true; +} diff --git a/pom.xml b/pom.xml index 5db6c10b684..21365d09782 100644 --- a/pom.xml +++ b/pom.xml @@ -1428,6 +1428,10 @@ code or new licensing patterns. gpcontrib/reject_partition_fullscan/Makefile gpcontrib/reject_partition_fullscan/reject_partition_fullscan.control + gpcontrib/anser/anser_test.control + gpcontrib/anser/**/*.sql + gpcontrib/anser/**/*.out + diff --git a/src/Makefile.global.in b/src/Makefile.global.in index bb9d4474de0..23d9e9080f8 100644 --- a/src/Makefile.global.in +++ b/src/Makefile.global.in @@ -266,6 +266,7 @@ BITCODE_CXXFLAGS = @BITCODE_CXXFLAGS@ have_yaml = @have_yaml@ YAML_LIBS = @YAML_LIBS@ with_zstd = @with_zstd@ +with_anser = @with_anser@ ZSTD_CFLAGS = @ZSTD_CFLAGS@ ZSTD_LIBS = @ZSTD_LIBS@ EVENT_LIBS = @EVENT_LIBS@ From b4d32b8ea9ff1d868c7832eef30f18634f036fb7 Mon Sep 17 00:00:00 2001 From: Leonid Borchuk Date: Thu, 15 Oct 2026 13:59:40 +0300 Subject: [PATCH 13/27] Export Anser's C API and rework the filter pushdown Anser builds as a MODULE_big with no SHLIB_EXPORTS, so the module is compiled -fvisibility=hidden and every extern in its headers lands in .symtab but not .dynsym. A second extension cannot reach any of it: RTLD_GLOBAL does not help, because a hidden symbol never reaches the dynamic symbol table, and load_external_function() fails for the same reason. Mark the public declarations PGDLLEXPORT, which is the convention the tree already uses for a cross-module C API -- see contrib/hstore and src/pl/plpython. Two plan-resolution helpers are promoted into that API rather than left for a caller to reimplement. Both encode which plan shapes are safe to inject into, so a second copy would be a second place to get that wrong. The pushdown itself is split so the decision can be tested on its own. A scan key is numbered by the table's attributes while the consumer is numbered by the scan's output targetlist, and the two coincide only when the scan emits every column in order -- so the filter was handed a position where an attribute number was wanted, and a projecting scan tested the wrong column and discarded every row. The decision now maps one to the other, and refuses the shapes that have no mapping: a computed column, a whole-row or system Var, a Var of another relation. A second function walks from the consumer to the deepest node that accepts the key, so stacked consumers over one relation all push into the same scan instead of probing tuple by tuple. Co-Authored-By: Claude Opus 5 --- gpcontrib/anser/anser_test--1.0.sql | 32 +++ .../anser/expected/anser_runtime_filter.out | 86 +++++++ gpcontrib/anser/expected/anser_test.out | 66 ++++++ gpcontrib/anser/include/anser.h | 10 +- gpcontrib/anser/include/anserbloom.h | 24 +- gpcontrib/anser/include/anserfilter.h | 16 +- gpcontrib/anser/include/anserpayload.h | 4 +- gpcontrib/anser/include/anserplan.h | 106 +++++++-- gpcontrib/anser/include/ansersideband.h | 24 +- gpcontrib/anser/sql/anser_runtime_filter.sql | 59 +++++ gpcontrib/anser/sql/anser_test.sql | 43 ++++ gpcontrib/anser/src/anser_test.c | 200 +++++++++++++++++ gpcontrib/anser/src/anserinit.c | 29 ++- gpcontrib/anser/src/anserplan.c | 89 +++++--- gpcontrib/anser/src/anserplanexec.c | 212 ++++++++++++++++-- 15 files changed, 895 insertions(+), 105 deletions(-) diff --git a/gpcontrib/anser/anser_test--1.0.sql b/gpcontrib/anser/anser_test--1.0.sql index 3c2f8d14cfd..0f1c02560ca 100644 --- a/gpcontrib/anser/anser_test--1.0.sql +++ b/gpcontrib/anser/anser_test--1.0.sql @@ -97,3 +97,35 @@ CREATE FUNCTION anser_test_push_crc( RETURNS int8 AS 'MODULE_PATHNAME' LANGUAGE C STRICT; + +-- The runtime-filter pushdown decision, over a synthetic scan. `tlist` +-- describes the scan's output targetlist, one element per entry: a positive +-- value is a plain Var on that table attno, and the rest are the shapes that +-- must be refused -- 0 a whole-row Var, -1 a system column, -2 a computed +-- column, -3 a Var of another relation, -4 a cast wrapping a Var. +-- +-- sk_attno is the attribute number the scan key would carry, and is NULL when +-- the shape is refused. +CREATE FUNCTION anser_test_pushdown_accepts( + tlist int4[], + attno int4, + filter_in_seqscan bool, + as_seqscan bool, + OUT accepts bool, + OUT sk_attno int4) +RETURNS record +AS 'MODULE_PATHNAME' +LANGUAGE C STRICT; + +-- The same scan put through AnserPushdownTarget, which must agree and name the +-- node itself when there is nothing to descend through. +CREATE FUNCTION anser_test_pushdown_target( + tlist int4[], + attno int4, + filter_in_seqscan bool, + as_seqscan bool, + OUT found_self bool, + OUT sk_attno int4) +RETURNS record +AS 'MODULE_PATHNAME' +LANGUAGE C STRICT; diff --git a/gpcontrib/anser/expected/anser_runtime_filter.out b/gpcontrib/anser/expected/anser_runtime_filter.out index cbc5fbceee8..9293817d4c9 100644 --- a/gpcontrib/anser/expected/anser_runtime_filter.out +++ b/gpcontrib/anser/expected/anser_runtime_filter.out @@ -144,6 +144,92 @@ SELECT count(*) AS only_off_push (1 row) RESET gp_enable_runtime_filter_pushdown; +-- Pushdown where the key's table attribute number and its position in the +-- scan's output targetlist disagree. The case above cannot see the +-- difference: anser_rf_probe's key is its first column and also the first +-- thing the scan emits, so the two numbers coincide. Here the key is the +-- probe table's *second* column and its *only* projected one, so a scan key +-- carrying the output position makes the scan test `payload` against a filter +-- built on `id` -- and since no payload is ever a build key, every row is +-- discarded. Both counts must be 200. +SET optimizer = on; +CREATE TABLE anser_rf_proj_build (id int) DISTRIBUTED BY (id); +CREATE TABLE anser_rf_proj_probe (payload int, id int) DISTRIBUTED BY (id); +INSERT INTO anser_rf_proj_build SELECT g FROM generate_series(1, 200) g; +INSERT INTO anser_rf_proj_probe SELECT -g, g FROM generate_series(1, 2000) g; +ANALYZE anser_rf_proj_build; +ANALYZE anser_rf_proj_probe; +SET gp_enable_runtime_filter_pushdown = on; +SET anser.runtime_filter = off; +SELECT count(p.id) AS proj_push_off + FROM anser_rf_proj_build b LEFT JOIN anser_rf_proj_probe p ON b.id = p.id; + proj_push_off +--------------- + 200 +(1 row) + +SET anser.runtime_filter = on; +SELECT count(p.id) AS proj_push_on + FROM anser_rf_proj_build b LEFT JOIN anser_rf_proj_probe p ON b.id = p.id; + proj_push_on +-------------- + 200 +(1 row) + +-- The alias is not the column. This subquery renames `payload` to the name +-- `id` and the real `id` to `id2`, and joins on id2 -- so resolving the key by +-- name would land on `payload`, the one column guaranteed never to match a +-- filter built from the build side. Resolution runs position -> TargetEntry +-- -> Var -> varattno and never reads a name, so the rename is invisible to it. +-- Both counts must be 200. +SET anser.runtime_filter = off; +SELECT count(p.id2) AS rename_push_off + FROM anser_rf_proj_build b + LEFT JOIN (SELECT payload AS id, id AS id2 FROM anser_rf_proj_probe) p + ON b.id = p.id2; + rename_push_off +----------------- + 200 +(1 row) + +SET anser.runtime_filter = on; +SELECT count(p.id2) AS rename_push_on + FROM anser_rf_proj_build b + LEFT JOIN (SELECT payload AS id, id AS id2 FROM anser_rf_proj_probe) p + ON b.id = p.id2; + rename_push_on +---------------- + 200 +(1 row) + +-- A computed key has no table attribute number to push down: the scan would +-- have to evaluate `id * 2`, which PassByBloomFilter cannot do. Note the join +-- clause itself still looks like a plain Var to the planner (it references the +-- scan's output position), so the key resolves and the nodes are injected -- +-- it is the scan's own targetlist entry that is an expression. The consumer +-- must decline the pushdown and probe the projected slot itself. Only the +-- 100 even build ids match, so both counts must be 100. +SET anser.runtime_filter = off; +SELECT count(p.k) AS expr_push_off + FROM anser_rf_proj_build b + LEFT JOIN (SELECT id * 2 AS k FROM anser_rf_proj_probe) p ON b.id = p.k; + expr_push_off +--------------- + 100 +(1 row) + +SET anser.runtime_filter = on; +SELECT count(p.k) AS expr_push_on + FROM anser_rf_proj_build b + LEFT JOIN (SELECT id * 2 AS k FROM anser_rf_proj_probe) p ON b.id = p.k; + expr_push_on +-------------- + 100 +(1 row) + +RESET gp_enable_runtime_filter_pushdown; +DROP TABLE anser_rf_proj_build, anser_rf_proj_probe; +SET optimizer = off; -- Everything above checks that the nodes were injected and that the results did -- not change. Both of those also hold when the filter never arrives and every -- consumer fails open -- which is how a broken exchange passes a green test. diff --git a/gpcontrib/anser/expected/anser_test.out b/gpcontrib/anser/expected/anser_test.out index 9e663672a02..13e1413eecc 100644 --- a/gpcontrib/anser/expected/anser_test.out +++ b/gpcontrib/anser/expected/anser_test.out @@ -283,4 +283,70 @@ SELECT t | t | t | t | t | t (1 row) +-- 5. The runtime-filter pushdown decision. A scan key is numbered by the +-- table's attributes, while the consumer is numbered by the scan's output +-- targetlist -- the slot it probes has already been projected. So the whole +-- job of the decision is that mapping, plus refusing every shape where there +-- is no mapping to make. sk_attno is what the key would carry; NULL means +-- refused, and the consumer then probes the slot itself (always correct, just +-- slower). In the tlist spec a positive value is a plain Var on that table +-- attno; 0 is a whole-row Var, -1 a system column, -2 a computed column, -3 a +-- Var of another relation, -4 a cast wrapping a Var. +SELECT t.what, t.tlist, t.attno, s.accepts, s.sk_attno +FROM (VALUES + ('all columns in order', '{1,2,3}'::int4[], 2, true, true), + ('projecting scan, one column', '{2}', 1, true, true), + ('reordered, first of two', '{3,1}', 1, true, true), + ('reordered, second of two', '{3,1}', 2, true, true), + ('position past the targetlist', '{2}', 2, true, true), + ('position zero', '{2}', 0, true, true), + ('negative position', '{2}', -1, true, true), + ('computed column', '{-2}', 1, true, true), + ('cast wrapping a column', '{-4}', 1, true, true), + ('whole-row reference', '{0}', 1, true, true), + ('system column', '{-1}', 1, true, true), + ('column of another relation', '{-3}', 1, true, true), + ('filter_in_seqscan off', '{2}', 1, false, true), + ('not a SeqScan', '{2}', 1, true, false) + ) AS t(what, tlist, attno, filter_in_seqscan, as_seqscan), + LATERAL anser_test_pushdown_accepts(t.tlist, t.attno, t.filter_in_seqscan, + t.as_seqscan) AS s; + what | tlist | attno | accepts | sk_attno +------------------------------+---------+-------+---------+---------- + all columns in order | {1,2,3} | 2 | t | 2 + projecting scan, one column | {2} | 1 | t | 2 + reordered, first of two | {3,1} | 1 | t | 3 + reordered, second of two | {3,1} | 2 | t | 1 + position past the targetlist | {2} | 2 | f | + position zero | {2} | 0 | f | + negative position | {2} | -1 | f | + computed column | {-2} | 1 | f | + cast wrapping a column | {-4} | 1 | f | + whole-row reference | {0} | 1 | f | + system column | {-1} | 1 | f | + column of another relation | {-3} | 1 | f | + filter_in_seqscan off | {2} | 1 | f | + not a SeqScan | {2} | 1 | f | +(14 rows) + +-- 6. The walk that finds where to push. With nothing to descend through it +-- must agree with the decision and name the node itself; a node that refuses +-- yields no target at all rather than a target with a meaningless attno. The +-- descent through stacked nodes needs live CustomScanStates, so it is covered +-- end to end by anser_runtime_filter instead. +SELECT t.what, s.found_self, s.sk_attno +FROM (VALUES + ('accepting scan is its own target', '{2}'::int4[], 1, true), + ('refused scan has no target', '{-2}', 1, true), + ('non-scan has no target', '{2}', 1, false) + ) AS t(what, tlist, attno, as_seqscan), + LATERAL anser_test_pushdown_target(t.tlist, t.attno, true, + t.as_seqscan) AS s; + what | found_self | sk_attno +----------------------------------+------------+---------- + accepting scan is its own target | t | 2 + refused scan has no target | f | + non-scan has no target | f | +(3 rows) + DROP EXTENSION anser_test; diff --git a/gpcontrib/anser/include/anser.h b/gpcontrib/anser/include/anser.h index 5bfa5d772e8..d7967e59070 100644 --- a/gpcontrib/anser/include/anser.h +++ b/gpcontrib/anser/include/anser.h @@ -60,11 +60,11 @@ typedef struct AnserChannelKey } AnserChannelKey; /* GUCs (defined in anserinit.c). */ -extern bool gp_anser_enable; -extern bool gp_anser_runtime_filter; -extern bool gp_anser_debug; -extern int gp_anser_max_info_size; -extern int gp_anser_timeout_ms; +extern PGDLLEXPORT bool gp_anser_enable; +extern PGDLLEXPORT bool gp_anser_runtime_filter; +extern PGDLLEXPORT bool gp_anser_debug; +extern PGDLLEXPORT int gp_anser_max_info_size; +extern PGDLLEXPORT int gp_anser_timeout_ms; /* * Trace the handoff between producers, the coordinator and consumers. diff --git a/gpcontrib/anser/include/anserbloom.h b/gpcontrib/anser/include/anserbloom.h index 24c7eaba7f6..946b35db5f8 100644 --- a/gpcontrib/anser/include/anserbloom.h +++ b/gpcontrib/anser/include/anserbloom.h @@ -47,33 +47,33 @@ typedef struct AnserBloomFilterConsumeState AnserBloomFilterConsumeState; * libpq connection (parallel-retrieve-cursor model); NULL means connect * without it and rely on pg_hba. Ignored on the coordinator-local path. */ -extern AnserBloomFilterProduceState *ExecInitAnserBloomFilterProduce( +extern PGDLLEXPORT AnserBloomFilterProduceState *ExecInitAnserBloomFilterProduce( const AnserChannelKey *channel_key, int64 total_elems, Size max_payload_bytes, uint32 part_index, uint32 total_parts); -extern void ExecAnserBloomFilterProduceAddDatum(AnserBloomFilterProduceState *state, +extern PGDLLEXPORT void ExecAnserBloomFilterProduceAddDatum(AnserBloomFilterProduceState *state, Datum value, bool isnull); -extern bool ExecAnserBloomFilterProduceHasFilter(AnserBloomFilterProduceState *state); -extern bool ExecAnserBloomFilterProducePublish(AnserBloomFilterProduceState *state); -extern bool ExecAnserBloomFilterProduceCancel(AnserBloomFilterProduceState *state); -extern void ExecEndAnserBloomFilterProduce(AnserBloomFilterProduceState *state); +extern PGDLLEXPORT bool ExecAnserBloomFilterProduceHasFilter(AnserBloomFilterProduceState *state); +extern PGDLLEXPORT bool ExecAnserBloomFilterProducePublish(AnserBloomFilterProduceState *state); +extern PGDLLEXPORT bool ExecAnserBloomFilterProduceCancel(AnserBloomFilterProduceState *state); +extern PGDLLEXPORT void ExecEndAnserBloomFilterProduce(AnserBloomFilterProduceState *state); /* Consumer side: gather all parts of a channel into one Bloom filter. */ -extern AnserBloomFilterConsumeState *ExecInitAnserBloomFilterConsume( +extern PGDLLEXPORT AnserBloomFilterConsumeState *ExecInitAnserBloomFilterConsume( const AnserChannelKey *channel_key, int64 total_elems, Size max_payload_bytes, uint32 expected_parts); -extern bool ExecAnserBloomFilterConsume(AnserBloomFilterConsumeState *state, +extern PGDLLEXPORT bool ExecAnserBloomFilterConsume(AnserBloomFilterConsumeState *state, long registration_timeout_ms); -extern bloom_filter *ExecAnserBloomFilterConsumerGetFilter( +extern PGDLLEXPORT bloom_filter *ExecAnserBloomFilterConsumerGetFilter( AnserBloomFilterConsumeState *state); -extern uint32 ExecAnserBloomFilterConsumerReceivedParts( +extern PGDLLEXPORT uint32 ExecAnserBloomFilterConsumerReceivedParts( AnserBloomFilterConsumeState *state); -extern bool ExecAnserBloomFilterConsumerWasCancelled( +extern PGDLLEXPORT bool ExecAnserBloomFilterConsumerWasCancelled( AnserBloomFilterConsumeState *state); -extern void ExecEndAnserBloomFilterConsume(AnserBloomFilterConsumeState *state); +extern PGDLLEXPORT void ExecEndAnserBloomFilterConsume(AnserBloomFilterConsumeState *state); #endif /* ANSERBLOOM_H */ diff --git a/gpcontrib/anser/include/anserfilter.h b/gpcontrib/anser/include/anserfilter.h index 7e50e25c042..dd802a4b6f6 100644 --- a/gpcontrib/anser/include/anserfilter.h +++ b/gpcontrib/anser/include/anserfilter.h @@ -94,34 +94,34 @@ typedef struct AnserBloomPartHeader * anything useful. Works on the wire form, so the coordinator can ask this of * a merged payload without rebuilding a filter. */ -extern bool AnserBloomPartWorthSending(const void *payload, Size payload_len); +extern PGDLLEXPORT bool AnserBloomPartWorthSending(const void *payload, Size payload_len); -extern uint64 AnserBloomSeed(const char *condition_key); +extern PGDLLEXPORT uint64 AnserBloomSeed(const char *condition_key); /* * Build an empty filter, or return NULL when AnserBloomShapeFor says it is not * worth building. A NULL return is not an error: the producer turns it into an * immediate cancel, so consumers stop waiting instead of timing out. */ -extern bloom_filter *AnserBloomCreate(int64 total_elems, +extern PGDLLEXPORT bloom_filter *AnserBloomCreate(int64 total_elems, Size max_payload_bytes, uint64 seed); -extern Size AnserBloomSerializedSize(const bloom_filter *filter); -extern bool AnserBloomSerializePart(const bloom_filter *filter, +extern PGDLLEXPORT Size AnserBloomSerializedSize(const bloom_filter *filter); +extern PGDLLEXPORT bool AnserBloomSerializePart(const bloom_filter *filter, uint32 part_index, uint32 total_parts, void *buffer, Size buffer_size, Size *payload_len); -extern bloom_filter *AnserBloomDeserializePart(const void *payload, +extern PGDLLEXPORT bloom_filter *AnserBloomDeserializePart(const void *payload, Size payload_len, int64 total_elems, Size max_payload_bytes, uint64 seed, uint32 *part_index, uint32 *total_parts); -extern bool AnserBloomLooksLikePart(const void *payload, Size payload_len); -extern bool AnserBloomFoldPartInPlace(void *acc, Size acc_len, +extern PGDLLEXPORT bool AnserBloomLooksLikePart(const void *payload, Size payload_len); +extern PGDLLEXPORT bool AnserBloomFoldPartInPlace(void *acc, Size acc_len, const void *part, Size part_len); #endif /* ANSERFILTER_H */ diff --git a/gpcontrib/anser/include/anserpayload.h b/gpcontrib/anser/include/anserpayload.h index cb5f30dffcf..18ab6bab108 100644 --- a/gpcontrib/anser/include/anserpayload.h +++ b/gpcontrib/anser/include/anserpayload.h @@ -137,7 +137,7 @@ typedef struct AnserPayloadOps * unregistered one is a real case, and defaulting it silently would turn a * forgotten table row into a mystery rather than a log line. */ -extern const AnserPayloadOps *AnserPayloadLookup(char code); +extern PGDLLEXPORT const AnserPayloadOps *AnserPayloadLookup(char code); /* * Whether a type's body is covered by the message checksum. Takes a code @@ -145,6 +145,6 @@ extern const AnserPayloadOps *AnserPayloadLookup(char code); * before it has resolved anything; an unregistered code answers false, which * is why this one needs no error handling. */ -extern bool AnserPayloadChecksumsBody(char code); +extern PGDLLEXPORT bool AnserPayloadChecksumsBody(char code); #endif /* ANSERPAYLOAD_H */ diff --git a/gpcontrib/anser/include/anserplan.h b/gpcontrib/anser/include/anserplan.h index 4e59453210d..26c8169424d 100644 --- a/gpcontrib/anser/include/anserplan.h +++ b/gpcontrib/anser/include/anserplan.h @@ -29,6 +29,7 @@ #ifndef ANSERPLAN_H #define ANSERPLAN_H +#include "nodes/execnodes.h" /* PlanState, for the pushdown helpers */ #include "nodes/plannodes.h" /* @@ -38,14 +39,14 @@ * both the Postgres planner and ORCA. A no-op unless the Anser runtime-filter * GUCs are on and this is a coordinator SELECT. */ -extern void AnserApplyRuntimeFilters(PlannedStmt *stmt); +extern PGDLLEXPORT void AnserApplyRuntimeFilters(PlannedStmt *stmt); /* * Register the two CustomScan providers (producer, consumer) so their methods * resolve by name when a dispatched plan is deserialized. Must run once per * backend (QD and every QE) before any plan execution. */ -extern void AnserRegisterRuntimeFilterMethods(void); +extern PGDLLEXPORT void AnserRegisterRuntimeFilterMethods(void); /* * Node builders (implemented in anserplanexec.c, where the CustomScan method @@ -61,15 +62,17 @@ extern void AnserRegisterRuntimeFilterMethods(void); * pass meets them where it expects a scan, because two joins can want to filter * the same base relation; coordinator-only, see the implementation. */ -extern bool AnserIsRuntimeFilterScan(const Plan *plan); +extern PGDLLEXPORT bool AnserIsRuntimeFilterScan(const Plan *plan); -extern CustomScan *AnserBuildBloomProducerScan(Plan *child, AttrNumber key_attno, - uint32 condition_id, - const char *condition_key, - int64 total_elems, - Size max_payload_bytes, - int64 planned_bytes, - int n_producers); +extern PGDLLEXPORT CustomScan *AnserBuildBloomProducerScan( + Plan *child, + AttrNumber key_attno, + uint32 condition_id, + const char *condition_key, + int64 total_elems, + Size max_payload_bytes, + int64 planned_bytes, + int n_producers); /* * `defer_first` tells the consumer to pass its first tuple through unfiltered. * ExecHashJoin pulls one outer tuple before building its hash table, and a @@ -78,14 +81,16 @@ extern CustomScan *AnserBuildBloomProducerScan(Plan *child, AttrNumber key_attno * cannot be ruled out; the cost is that such a consumer cannot push its filter * into the scan and probes it itself instead. */ -extern CustomScan *AnserBuildBloomConsumerScan(Plan *child, AttrNumber key_attno, - uint32 condition_id, - const char *condition_key, - int64 total_elems, - Size max_payload_bytes, - int64 planned_bytes, - int n_producers, - bool defer_first); +extern PGDLLEXPORT CustomScan *AnserBuildBloomConsumerScan( + Plan *child, + AttrNumber key_attno, + uint32 condition_id, + const char *condition_key, + int64 total_elems, + Size max_payload_bytes, + int64 planned_bytes, + int n_producers, + bool defer_first); /* * Bloom sizing for one join, from its estimated build cardinality. False means @@ -95,7 +100,68 @@ extern CustomScan *AnserBuildBloomConsumerScan(Plan *child, AttrNumber key_attno * decisions and therefore the one most worth testing directly at its boundary * (anser_test.c); nothing but the injection pass and the tests should call it. */ -extern bool AnserRuntimeFilterSize(double est_rows, int64 *total_elems, - int64 *max_payload, int64 *planned_bytes); +extern PGDLLEXPORT bool AnserRuntimeFilterSize(double est_rows, + int64 *total_elems, + int64 *max_payload, + int64 *planned_bytes); + +/* + * Follow a key column of `top` down to the base scan that originates it, + * through the nodes that pass a column through unchanged (Hash, Motion), the + * joins that carry one side's columns up (HashJoin), and our own nodes. True + * on success, with *scan_out the scan, *attno_out the key's attno in it, + * *parent_out the node holding it (ours keep it in custom_plans, everything + * else in the outer link) and *slice_out the slice the scan runs in. + * + * False means no single base scan holds the whole key column -- an Append or a + * SetOp, say, where the column comes from several relations at once and a + * filter built on any one of them would reject rows that can join. A false + * return is therefore a correctness gate, not a missed optimization: callers + * must decline rather than guess. + * + * Only meaningful after set_plan_references, which is also what makes the + * descent necessary: before it the Vars still name their base relations. + */ +extern PGDLLEXPORT bool AnserResolveKeyScan(Plan *top, + AttrNumber key_attno, + int slice_index, + Plan **parent_out, + Plan **scan_out, + AttrNumber *attno_out, + int *slice_out); + +/* + * How many processes execute `slice_index` of `stmt` -- the number of parts a + * channel fed from that slice must wait for. Zero means the slice could not be + * identified, and the caller must decline rather than assume: a channel that + * expects too few parts completes early and hands out a filter missing some + * segment's keys, which drops rows that can join. + * + * Not the segment count. A parallel slice runs numsegments * parallel_workers + * full QEs, each publishing for itself, and the two optimizers choose different + * widths for the same query -- see the implementation. + */ +extern PGDLLEXPORT int AnserSliceProducers(PlannedStmt *stmt, int slice_index); + +/* + * Runtime-filter pushdown, split so the decision can be tested on its own. + * + * AnserPushdownAccepts answers "would this node take a scan key for the column + * it emits at output position `attno`?", and on yes reports the attribute + * number the key must carry -- which is the table's attno, not `attno`, because + * the scan evaluates the key against an unprojected tuple. + * + * AnserPushdownTarget walks down from `top` following that column and returns + * the deepest accepting node, so a stack of consumers over one relation all + * push into the same scan instead of probing tuple by tuple. + * + * Both return false/NULL rather than failing when the shape is not supported; + * the caller's fallback is to probe the filter itself, which is always correct. + */ +extern PGDLLEXPORT bool AnserPushdownAccepts(PlanState *ps, AttrNumber attno, + AttrNumber *sk_attno_out); +extern PGDLLEXPORT PlanState *AnserPushdownTarget(PlanState *top, + AttrNumber attno, + AttrNumber *sk_attno_out); #endif /* ANSERPLAN_H */ diff --git a/gpcontrib/anser/include/ansersideband.h b/gpcontrib/anser/include/ansersideband.h index 2cbea9336cd..995c16670ad 100644 --- a/gpcontrib/anser/include/ansersideband.h +++ b/gpcontrib/anser/include/ansersideband.h @@ -165,12 +165,12 @@ typedef struct AnserWireMsg uint32 crc; /* of the header, key and decoded body */ } AnserWireMsg; -extern char *AnserWireFormat(const AnserChannelKey *channel_key, char kind, +extern PGDLLEXPORT char *AnserWireFormat(const AnserChannelKey *channel_key, char kind, char payload_type, uint32 part_index, uint32 total_parts, int flags, const void *payload, Size payload_len); -extern bool AnserWireParse(const char *msg, AnserWireMsg *out); -extern bool AnserWireCheckCrc(const AnserWireMsg *msg, const void *body, +extern PGDLLEXPORT bool AnserWireParse(const char *msg, AnserWireMsg *out); +extern PGDLLEXPORT bool AnserWireCheckCrc(const AnserWireMsg *msg, const void *body, Size body_len); /* @@ -185,7 +185,7 @@ extern bool AnserWireCheckCrc(const AnserWireMsg *msg, const void *body, * this one function so the two cannot drift apart. It is implemented in * ansersideband.c, beside the reader that verifies it. */ -extern pg_crc32c AnserWirePushCrc(char payload_type, uint32 condition_id, +extern PGDLLEXPORT pg_crc32c AnserWirePushCrc(char payload_type, uint32 condition_id, uint32 flags, const char *key, int keylen, const void *body, int bodylen); @@ -204,12 +204,12 @@ extern pg_crc32c AnserWirePushCrc(char payload_type, uint32 condition_id, * is not of 'payload_type', which would mean two different kinds of information * had collided on one channel. */ -extern bool AnserSidebandPublish(const AnserChannelKey *channel_key, +extern PGDLLEXPORT bool AnserSidebandPublish(const AnserChannelKey *channel_key, char payload_type, uint32 part_index, uint32 total_parts, const void *payload, Size payload_len, bool cancelled); -extern bool AnserSidebandConsumeWait(const AnserChannelKey *channel_key, +extern PGDLLEXPORT bool AnserSidebandConsumeWait(const AnserChannelKey *channel_key, char payload_type, void **payload, Size *payload_len, bool *cancelled, long timeout_ms); @@ -225,14 +225,14 @@ extern bool AnserSidebandConsumeWait(const AnserChannelKey *channel_key, * same reasons, so that a coordinator-local producer and a segment producer are * interchangeable on one channel. */ -extern bool AnserDispatchNotifyHandler(struct CdbDispatchResult *dispatchResult, +extern PGDLLEXPORT bool AnserDispatchNotifyHandler(struct CdbDispatchResult *dispatchResult, struct pgNotify *notify); -extern bool AnserDispatchLocalPublish(const AnserChannelKey *channel_key, +extern PGDLLEXPORT bool AnserDispatchLocalPublish(const AnserChannelKey *channel_key, char payload_type, uint32 part_index, uint32 total_parts, const void *payload, Size payload_len, bool cancelled); -extern bool AnserDispatchLocalConsume(const AnserChannelKey *channel_key, +extern PGDLLEXPORT bool AnserDispatchLocalConsume(const AnserChannelKey *channel_key, char payload_type, void **payload, Size *payload_len, bool *cancelled); @@ -242,8 +242,8 @@ extern bool AnserDispatchLocalConsume(const AnserChannelKey *channel_key, * the executor/transaction-end callbacks call; the halves are exposed because * each lives with the state it owns. */ -extern void AnserDispatchReset(void); -extern void AnserSidebandResetInbox(void); -extern void AnserSidebandResetAll(void); +extern PGDLLEXPORT void AnserDispatchReset(void); +extern PGDLLEXPORT void AnserSidebandResetInbox(void); +extern PGDLLEXPORT void AnserSidebandResetAll(void); #endif /* ANSERSIDEBAND_H */ diff --git a/gpcontrib/anser/sql/anser_runtime_filter.sql b/gpcontrib/anser/sql/anser_runtime_filter.sql index 06e12ac2f2f..ec46b6e0596 100644 --- a/gpcontrib/anser/sql/anser_runtime_filter.sql +++ b/gpcontrib/anser/sql/anser_runtime_filter.sql @@ -85,6 +85,65 @@ SELECT count(*) AS only_off_push FROM (SELECT * FROM anser_rf_r_off EXCEPT ALL SELECT * FROM anser_rf_r_push) d; RESET gp_enable_runtime_filter_pushdown; +-- Pushdown where the key's table attribute number and its position in the +-- scan's output targetlist disagree. The case above cannot see the +-- difference: anser_rf_probe's key is its first column and also the first +-- thing the scan emits, so the two numbers coincide. Here the key is the +-- probe table's *second* column and its *only* projected one, so a scan key +-- carrying the output position makes the scan test `payload` against a filter +-- built on `id` -- and since no payload is ever a build key, every row is +-- discarded. Both counts must be 200. +SET optimizer = on; +CREATE TABLE anser_rf_proj_build (id int) DISTRIBUTED BY (id); +CREATE TABLE anser_rf_proj_probe (payload int, id int) DISTRIBUTED BY (id); +INSERT INTO anser_rf_proj_build SELECT g FROM generate_series(1, 200) g; +INSERT INTO anser_rf_proj_probe SELECT -g, g FROM generate_series(1, 2000) g; +ANALYZE anser_rf_proj_build; +ANALYZE anser_rf_proj_probe; +SET gp_enable_runtime_filter_pushdown = on; +SET anser.runtime_filter = off; +SELECT count(p.id) AS proj_push_off + FROM anser_rf_proj_build b LEFT JOIN anser_rf_proj_probe p ON b.id = p.id; +SET anser.runtime_filter = on; +SELECT count(p.id) AS proj_push_on + FROM anser_rf_proj_build b LEFT JOIN anser_rf_proj_probe p ON b.id = p.id; + +-- The alias is not the column. This subquery renames `payload` to the name +-- `id` and the real `id` to `id2`, and joins on id2 -- so resolving the key by +-- name would land on `payload`, the one column guaranteed never to match a +-- filter built from the build side. Resolution runs position -> TargetEntry +-- -> Var -> varattno and never reads a name, so the rename is invisible to it. +-- Both counts must be 200. +SET anser.runtime_filter = off; +SELECT count(p.id2) AS rename_push_off + FROM anser_rf_proj_build b + LEFT JOIN (SELECT payload AS id, id AS id2 FROM anser_rf_proj_probe) p + ON b.id = p.id2; +SET anser.runtime_filter = on; +SELECT count(p.id2) AS rename_push_on + FROM anser_rf_proj_build b + LEFT JOIN (SELECT payload AS id, id AS id2 FROM anser_rf_proj_probe) p + ON b.id = p.id2; + +-- A computed key has no table attribute number to push down: the scan would +-- have to evaluate `id * 2`, which PassByBloomFilter cannot do. Note the join +-- clause itself still looks like a plain Var to the planner (it references the +-- scan's output position), so the key resolves and the nodes are injected -- +-- it is the scan's own targetlist entry that is an expression. The consumer +-- must decline the pushdown and probe the projected slot itself. Only the +-- 100 even build ids match, so both counts must be 100. +SET anser.runtime_filter = off; +SELECT count(p.k) AS expr_push_off + FROM anser_rf_proj_build b + LEFT JOIN (SELECT id * 2 AS k FROM anser_rf_proj_probe) p ON b.id = p.k; +SET anser.runtime_filter = on; +SELECT count(p.k) AS expr_push_on + FROM anser_rf_proj_build b + LEFT JOIN (SELECT id * 2 AS k FROM anser_rf_proj_probe) p ON b.id = p.k; +RESET gp_enable_runtime_filter_pushdown; +DROP TABLE anser_rf_proj_build, anser_rf_proj_probe; +SET optimizer = off; + -- Everything above checks that the nodes were injected and that the results did -- not change. Both of those also hold when the filter never arrives and every diff --git a/gpcontrib/anser/sql/anser_test.sql b/gpcontrib/anser/sql/anser_test.sql index 0c6e3585c89..dfdd08fb8c8 100644 --- a/gpcontrib/anser/sql/anser_test.sql +++ b/gpcontrib/anser/sql/anser_test.sql @@ -181,4 +181,47 @@ SELECT anser_test_push_crc('B'::"char", 1, 0, 'k', '\x01') <> anser_test_push_crc('-'::"char", 1, 0, 'k', '\x01') AS type_covered; +-- 5. The runtime-filter pushdown decision. A scan key is numbered by the +-- table's attributes, while the consumer is numbered by the scan's output +-- targetlist -- the slot it probes has already been projected. So the whole +-- job of the decision is that mapping, plus refusing every shape where there +-- is no mapping to make. sk_attno is what the key would carry; NULL means +-- refused, and the consumer then probes the slot itself (always correct, just +-- slower). In the tlist spec a positive value is a plain Var on that table +-- attno; 0 is a whole-row Var, -1 a system column, -2 a computed column, -3 a +-- Var of another relation, -4 a cast wrapping a Var. +SELECT t.what, t.tlist, t.attno, s.accepts, s.sk_attno +FROM (VALUES + ('all columns in order', '{1,2,3}'::int4[], 2, true, true), + ('projecting scan, one column', '{2}', 1, true, true), + ('reordered, first of two', '{3,1}', 1, true, true), + ('reordered, second of two', '{3,1}', 2, true, true), + ('position past the targetlist', '{2}', 2, true, true), + ('position zero', '{2}', 0, true, true), + ('negative position', '{2}', -1, true, true), + ('computed column', '{-2}', 1, true, true), + ('cast wrapping a column', '{-4}', 1, true, true), + ('whole-row reference', '{0}', 1, true, true), + ('system column', '{-1}', 1, true, true), + ('column of another relation', '{-3}', 1, true, true), + ('filter_in_seqscan off', '{2}', 1, false, true), + ('not a SeqScan', '{2}', 1, true, false) + ) AS t(what, tlist, attno, filter_in_seqscan, as_seqscan), + LATERAL anser_test_pushdown_accepts(t.tlist, t.attno, t.filter_in_seqscan, + t.as_seqscan) AS s; + +-- 6. The walk that finds where to push. With nothing to descend through it +-- must agree with the decision and name the node itself; a node that refuses +-- yields no target at all rather than a target with a meaningless attno. The +-- descent through stacked nodes needs live CustomScanStates, so it is covered +-- end to end by anser_runtime_filter instead. +SELECT t.what, s.found_self, s.sk_attno +FROM (VALUES + ('accepting scan is its own target', '{2}'::int4[], 1, true), + ('refused scan has no target', '{-2}', 1, true), + ('non-scan has no target', '{2}', 1, false) + ) AS t(what, tlist, attno, as_seqscan), + LATERAL anser_test_pushdown_target(t.tlist, t.attno, true, + t.as_seqscan) AS s; + DROP EXTENSION anser_test; diff --git a/gpcontrib/anser/src/anser_test.c b/gpcontrib/anser/src/anser_test.c index 456d1a4a004..4ef17765d89 100644 --- a/gpcontrib/anser/src/anser_test.c +++ b/gpcontrib/anser/src/anser_test.c @@ -28,6 +28,7 @@ #include "postgres.h" #include "access/htup_details.h" +#include "catalog/pg_type.h" #include "anser.h" #include "anserbloom.h" #include "anserfilter.h" @@ -39,7 +40,9 @@ #include "fmgr.h" #include "funcapi.h" #include "lib/bloomfilter.h" +#include "nodes/makefuncs.h" #include "miscadmin.h" +#include "utils/array.h" #include "utils/builtins.h" #include "varatt.h" @@ -763,3 +766,200 @@ anser_test_push_crc(PG_FUNCTION_ARGS) VARDATA_ANY(body), (int) VARSIZE_ANY_EXHDR(body))); } + +/* + * --------------------------------------------------------------------------- + * Runtime-filter pushdown: the decision, on its own + * --------------------------------------------------------------------------- + */ + +PG_FUNCTION_INFO_V1(anser_test_pushdown_accepts); +PG_FUNCTION_INFO_V1(anser_test_pushdown_target); + +/* The relation the synthetic scan below claims to be scanning. */ +#define ANSER_TEST_SCANRELID 1 + +/* + * Targetlist entry kinds, as the SQL-visible spec encodes them. A positive + * value is the table attno of a plain Var; the rest are the shapes that must + * be refused. + */ +#define ANSER_TEST_TL_WHOLEROW 0 /* Var, varattno 0 */ +#define ANSER_TEST_TL_SYSCOL (-1) /* Var, negative varattno */ +#define ANSER_TEST_TL_EXPR (-2) /* not a Var at all */ +#define ANSER_TEST_TL_OTHERREL (-3) /* Var of a different RTE */ +#define ANSER_TEST_TL_RELABEL (-4) /* a cast wrapping a Var */ + +/* + * Build the targetlist `spec` describes. Each element becomes one entry, in + * order, so the array index doubles as the output position the decision is + * asked about. + */ +static List * +anser_test_make_tlist(ArrayType *spec) +{ + Datum *elems; + bool *nulls; + int nelems; + List *tlist = NIL; + int i; + + if (ARR_NDIM(spec) > 1) + elog(ERROR, "anser_test: targetlist spec must be one-dimensional"); + + deconstruct_array(spec, INT4OID, sizeof(int32), true, TYPALIGN_INT, + &elems, &nulls, &nelems); + + for (i = 0; i < nelems; i++) + { + int32 kind; + Expr *expr; + + if (nulls[i]) + elog(ERROR, "anser_test: targetlist spec may not contain NULLs"); + kind = DatumGetInt32(elems[i]); + + switch (kind) + { + case ANSER_TEST_TL_EXPR: + + /* + * The decision only asks whether the entry is a Var, so any + * non-Var exercises the same branch. An OpExpr is what a + * computed column really looks like; nothing evaluates it. + */ + expr = (Expr *) makeNode(OpExpr); + break; + case ANSER_TEST_TL_OTHERREL: + expr = (Expr *) makeVar(ANSER_TEST_SCANRELID + 1, 1, INT4OID, + -1, InvalidOid, 0); + break; + case ANSER_TEST_TL_RELABEL: + { + RelabelType *rt = makeNode(RelabelType); + + rt->arg = (Expr *) makeVar(ANSER_TEST_SCANRELID, 1, + INT4OID, -1, InvalidOid, 0); + rt->resulttype = OIDOID; + rt->relabelformat = COERCE_IMPLICIT_CAST; + expr = (Expr *) rt; + } + break; + default: + /* whole-row (0), system column (negative), or a real column */ + expr = (Expr *) makeVar(ANSER_TEST_SCANRELID, + (AttrNumber) kind, INT4OID, -1, + InvalidOid, 0); + break; + } + + tlist = lappend(tlist, makeTargetEntry(expr, (AttrNumber) (i + 1), + NULL, false)); + } + + return tlist; +} + +/* + * A SeqScanState over that targetlist. Nothing is executed, so only the + * fields the decision reads are set: the node tag, filter_in_seqscan, and the + * plan's targetlist and scanrelid. Passing as_seqscan = false substitutes a + * node of another type, which must be refused whatever its targetlist says. + */ +static PlanState * +anser_test_make_scanstate(ArrayType *spec, bool filter_in_seqscan, + bool as_seqscan) +{ + SeqScan *scan = makeNode(SeqScan); + + scan->scan.scanrelid = ANSER_TEST_SCANRELID; + scan->scan.plan.targetlist = anser_test_make_tlist(spec); + + if (!as_seqscan) + { + MaterialState *ms = makeNode(MaterialState); + + ms->ss.ps.plan = (Plan *) scan; + return (PlanState *) ms; + } + + { + SeqScanState *sss = makeNode(SeqScanState); + + sss->ss.ps.plan = (Plan *) scan; + sss->filter_in_seqscan = filter_in_seqscan; + return (PlanState *) sss; + } +} + +/* + * AnserPushdownAccepts on a synthetic scan. Returns the scan key's attribute + * number, or NULL when the shape is refused -- so the expected output records + * the mapping itself and not merely a verdict on it. + */ +Datum +anser_test_pushdown_accepts(PG_FUNCTION_ARGS) +{ + ArrayType *spec = PG_GETARG_ARRAYTYPE_P(0); + AttrNumber attno = (AttrNumber) PG_GETARG_INT32(1); + bool filter_in_seqscan = PG_GETARG_BOOL(2); + bool as_seqscan = PG_GETARG_BOOL(3); + PlanState *ps; + AttrNumber sk_attno = InvalidAttrNumber; + bool accepts; + Datum values[2] = {0, 0}; + bool nulls[2] = {false, false}; + TupleDesc tupdesc; + + if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE) + elog(ERROR, "anser_test_pushdown_accepts: expected a composite return type"); + tupdesc = BlessTupleDesc(tupdesc); + + ps = anser_test_make_scanstate(spec, filter_in_seqscan, as_seqscan); + accepts = AnserPushdownAccepts(ps, attno, &sk_attno); + + values[0] = BoolGetDatum(accepts); + values[1] = Int32GetDatum((int32) sk_attno); + nulls[1] = !accepts; + + PG_RETURN_DATUM(HeapTupleGetDatum(heap_form_tuple(tupdesc, values, nulls))); +} + +/* + * AnserPushdownTarget over the same synthetic scan, with no stacking: it must + * agree with AnserPushdownAccepts and report the node itself as the target. + * + * The descent through stacked nodes cannot be built here -- that needs real + * CustomScanStates, which need a live EState -- so it is covered end to end by + * the multi-join case in anser_runtime_filter instead. What this pins down is + * the part that is easy to get wrong without noticing: that a found target + * carries the mapped attno through, and that nothing is reported when the + * bottom node refuses. + */ +Datum +anser_test_pushdown_target(PG_FUNCTION_ARGS) +{ + ArrayType *spec = PG_GETARG_ARRAYTYPE_P(0); + AttrNumber attno = (AttrNumber) PG_GETARG_INT32(1); + bool filter_in_seqscan = PG_GETARG_BOOL(2); + bool as_seqscan = PG_GETARG_BOOL(3); + PlanState *ps; + PlanState *target; + AttrNumber sk_attno = InvalidAttrNumber; + Datum values[2] = {0, 0}; + bool nulls[2] = {false, false}; + TupleDesc tupdesc; + + if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE) + elog(ERROR, "anser_test_pushdown_target: expected a composite return type"); + tupdesc = BlessTupleDesc(tupdesc); + + ps = anser_test_make_scanstate(spec, filter_in_seqscan, as_seqscan); + target = AnserPushdownTarget(ps, attno, &sk_attno); + + values[0] = BoolGetDatum(target == ps); + values[1] = Int32GetDatum((int32) sk_attno); + nulls[1] = (target == NULL); + + PG_RETURN_DATUM(HeapTupleGetDatum(heap_form_tuple(tupdesc, values, nulls))); +} diff --git a/gpcontrib/anser/src/anserinit.c b/gpcontrib/anser/src/anserinit.c index 10b0a28498f..1ab12e3db09 100644 --- a/gpcontrib/anser/src/anserinit.c +++ b/gpcontrib/anser/src/anserinit.c @@ -66,7 +66,10 @@ static PlannedStmt *anser_planner(Query *parse, const char *query_string, static planner_hook_type prev_planner_hook = NULL; static ExecutorEnd_hook_type prev_ExecutorEnd_hook = NULL; +static cdbdisp_notify_hook_type prev_cdbdisp_notify_hook = NULL; +static bool anser_notify_hook(struct CdbDispatchResult *dispatchResult, + struct pgNotify *notify); static void anser_executor_end(QueryDesc *queryDesc); static void anser_xact_callback(XactEvent event, void *arg); @@ -100,7 +103,8 @@ _PG_init(void) * Installed unconditionally: it is inert until a segment sends one, and a * QE that never dispatches never calls it. */ - cdbdisp_notify_hook = AnserDispatchNotifyHandler; + prev_cdbdisp_notify_hook = cdbdisp_notify_hook; + cdbdisp_notify_hook = anser_notify_hook; /* * The producer and consumer nodes travel to the segments inside dispatched @@ -198,6 +202,29 @@ anser_planner(Query *parse, const char *query_string, int cursorOptions, return result; } +/* + * Dispatch a QE notify to Anser, then to whoever held the hook before us. + * + * cdbdisp_notify_hook is a single pointer, so an extension that overwrites it + * silently swallows every notify the other one was waiting for. Anser's + * handler already declines anything that is not addressed to it -- it returns + * false for any channel other than "anser_rf" -- so all that is missing is the + * fallthrough. Returning false from here means nobody claimed the notify and + * the dispatcher should handle it itself. + */ +static bool +anser_notify_hook(struct CdbDispatchResult *dispatchResult, + struct pgNotify *notify) +{ + if (AnserDispatchNotifyHandler(dispatchResult, notify)) + return true; + + if (prev_cdbdisp_notify_hook) + return prev_cdbdisp_notify_hook(dispatchResult, notify); + + return false; +} + /* * Drop the transport's per-query state. * diff --git a/gpcontrib/anser/src/anserplan.c b/gpcontrib/anser/src/anserplan.c index 7fa5aae5189..75c08a48fcd 100644 --- a/gpcontrib/anser/src/anserplan.c +++ b/gpcontrib/anser/src/anserplan.c @@ -55,7 +55,7 @@ /* Per-statement state for the injection pass. */ typedef struct AnserInjectCtx { - PlannedStmt *stmt; /* for slices[]: see anser_slice_producers */ + PlannedStmt *stmt; /* for slices[]: see AnserSliceProducers */ int slice_index; /* slice the subtree being walked runs in */ uint32 next_condition_id; int next_plan_node_id; @@ -65,13 +65,8 @@ typedef struct AnserInjectCtx } AnserInjectCtx; static int anser_max_plan_node_id(Plan *plan); -static int anser_slice_producers(AnserInjectCtx *ctx, int slice_index); static bool anser_hashjoin_keys(HashJoin *hj, AttrNumber *inner_attno, AttrNumber *outer_attno); -static bool anser_resolve_key_scan(Plan *top, AttrNumber key_attno, - int slice_index, Plan **parent_out, - Plan **scan_out, AttrNumber *attno_out, - int *slice_out); static void anser_try_inject(HashJoin *hj, AnserInjectCtx *ctx); static void anser_inject_walk(Plan *plan, AnserInjectCtx *ctx); @@ -127,13 +122,23 @@ AnserApplyRuntimeFilters(PlannedStmt *stmt) } /* - * Largest plan_node_id in a plan subtree. Recurses the spine plus CustomScan - * children; sufficient for the supported (simple) plan shape. + * Largest plan_node_id in a plan subtree. + * + * This has to see every node in the subtree, because the result is the + * high-water mark injected nodes are numbered from: an id it misses is an id we + * hand out a second time. So unlike anser_inject_walk, which deliberately only + * follows the shapes it injects into, this descends every child list there is. + * Getting that wrong corrupts a plan the pass would otherwise have left alone -- + * an Append (a partitioned table, a UNION) is enough to trigger it. + * + * InitPlans need no arm here: a SubPlan only references stmt->subplans by index, + * and AnserApplyRuntimeFilters walks that list separately. */ static int anser_max_plan_node_id(Plan *plan) { int m; + ListCell *lc; if (plan == NULL) return 0; @@ -141,12 +146,38 @@ anser_max_plan_node_id(Plan *plan) m = plan->plan_node_id; m = Max(m, anser_max_plan_node_id(outerPlan(plan))); m = Max(m, anser_max_plan_node_id(innerPlan(plan))); - if (IsA(plan, CustomScan)) - { - ListCell *lc; - foreach(lc, ((CustomScan *) plan)->custom_plans) - m = Max(m, anser_max_plan_node_id((Plan *) lfirst(lc))); + switch (nodeTag(plan)) + { + case T_Append: + foreach(lc, ((Append *) plan)->appendplans) + m = Max(m, anser_max_plan_node_id((Plan *) lfirst(lc))); + break; + case T_MergeAppend: + foreach(lc, ((MergeAppend *) plan)->mergeplans) + m = Max(m, anser_max_plan_node_id((Plan *) lfirst(lc))); + break; + case T_Sequence: + foreach(lc, ((Sequence *) plan)->subplans) + m = Max(m, anser_max_plan_node_id((Plan *) lfirst(lc))); + break; + case T_BitmapAnd: + foreach(lc, ((BitmapAnd *) plan)->bitmapplans) + m = Max(m, anser_max_plan_node_id((Plan *) lfirst(lc))); + break; + case T_BitmapOr: + foreach(lc, ((BitmapOr *) plan)->bitmapplans) + m = Max(m, anser_max_plan_node_id((Plan *) lfirst(lc))); + break; + case T_SubqueryScan: + m = Max(m, anser_max_plan_node_id(((SubqueryScan *) plan)->subplan)); + break; + case T_CustomScan: + foreach(lc, ((CustomScan *) plan)->custom_plans) + m = Max(m, anser_max_plan_node_id((Plan *) lfirst(lc))); + break; + default: + break; } return m; @@ -317,10 +348,10 @@ anser_hashjoin_keys(HashJoin *hj, AttrNumber *inner_attno, AttrNumber *outer_att * everything else in the outer link. A SeqScan is only accepted through one of * those two: the inner child of a HashJoin is always a Hash. */ -static bool -anser_resolve_key_scan(Plan *top, AttrNumber key_attno, int slice_index, - Plan **parent_out, Plan **scan_out, - AttrNumber *attno_out, int *slice_out) +bool +AnserResolveKeyScan(Plan *top, AttrNumber key_attno, int slice_index, + Plan **parent_out, Plan **scan_out, + AttrNumber *attno_out, int *slice_out) { Plan *node = top; AttrNumber attno = key_attno; @@ -453,9 +484,9 @@ anser_try_inject(HashJoin *hj, AnserInjectCtx *ctx) if (!anser_hashjoin_keys(hj, &inner_attno, &outer_attno)) return; - if (!anser_resolve_key_scan(hash, inner_attno, ctx->slice_index, - &build_parent, &build_scan, &build_attno, - &build_slice)) + if (!AnserResolveKeyScan(hash, inner_attno, ctx->slice_index, + &build_parent, &build_scan, &build_attno, + &build_slice)) return; /* @@ -477,9 +508,9 @@ anser_try_inject(HashJoin *hj, AnserInjectCtx *ctx) probe_scan = probe; probe_attno = outer_attno; } - else if (!anser_resolve_key_scan(probe, outer_attno, ctx->slice_index, - &probe_parent, &probe_scan, &probe_attno, - &probe_slice)) + else if (!AnserResolveKeyScan(probe, outer_attno, ctx->slice_index, + &probe_parent, &probe_scan, &probe_attno, + &probe_slice)) return; /* @@ -495,7 +526,7 @@ anser_try_inject(HashJoin *hj, AnserInjectCtx *ctx) * is too low makes the coordinator complete the channel early and ship a * filter missing keys, which drops joinable rows. */ - n_producers = anser_slice_producers(ctx, build_slice); + n_producers = AnserSliceProducers(ctx->stmt, build_slice); if (n_producers <= 0) return; /* @@ -612,17 +643,17 @@ anser_inject_walk(Plan *plan, AnserInjectCtx *ctx) * Motion 16:16 over a Parallel Seq Scan, so 16). Reading it from the slice * means neither case needs special handling. */ -static int -anser_slice_producers(AnserInjectCtx *ctx, int slice_index) +int +AnserSliceProducers(PlannedStmt *stmt, int slice_index) { PlanSlice *slice; int factor; - if (ctx->stmt->slices == NULL || - slice_index < 0 || slice_index >= ctx->stmt->numSlices) + if (stmt == NULL || stmt->slices == NULL || + slice_index < 0 || slice_index >= stmt->numSlices) return 0; /* unknown: caller declines to inject */ - slice = &ctx->stmt->slices[slice_index]; + slice = &stmt->slices[slice_index]; /* The coordinator's own slice is one process and has no gang. */ if (slice->gangType == GANGTYPE_UNALLOCATED || diff --git a/gpcontrib/anser/src/anserplanexec.c b/gpcontrib/anser/src/anserplanexec.c index 3680b9215c8..a6c256c6960 100644 --- a/gpcontrib/anser/src/anserplanexec.c +++ b/gpcontrib/anser/src/anserplanexec.c @@ -258,9 +258,13 @@ anser_build_rf_scan(const CustomScanMethods *methods, Plan *child, * The injection pass has to recognise them because it can meet one where it * expects a scan: two joins whose keys trace to the same base relation both * want to filter it, and the second arrives to find the first already there. - * Compared by method table rather than by name, and only sound on the - * coordinator -- a QE resolves these pointers by name when it deserializes the - * plan, which is long after the only pass that asks. + * + * Compared by method table rather than by name, which holds in any backend and + * not just the one that built the plan: a CustomScan travels as its + * methods->CustomName, and the reader resolves that name through + * GetCustomScanMethods() back to the very table RegisterCustomScanMethods() + * stored -- ours, because _PG_init registers it in every backend. A process + * that had not registered it could not deserialize the plan at all. */ bool AnserIsRuntimeFilterScan(const Plan *plan) @@ -639,35 +643,205 @@ anser_consume_receive(AnserBloomConsumeScanState *st) } /* - * Hand the received filter to the child SeqScan as an SK_BLOOM_FILTER scan - * key -- the exact structure PassByBloomFilter consumes (nodeSeqscan.c), so - * the scan does the pruning itself and a table AM that supports - * SCAN_SUPPORT_RUNTIME_FILTER may additionally use the key for block-level - * pruning. Runs before the child's first ExecProcNode, so the key is in - * node->filters when the scan descriptor is created. The filter stays owned - * by us; anser_consume_end ends the child before freeing it. + * Is this PlanState one of our own nodes? + * + * The exec method table is the thing to compare, because our own + * CreateCustomScanState callback assigns it (anser_consume_create_state) in + * the process that is about to run the node. It is a plain in-process pointer + * with nothing in between, so the comparison means the same thing wherever it + * runs -- which matters, because a consumer executes on the coordinator as + * well as on a segment: a slice with GANGTYPE_UNALLOCATED has no gang, and + * ExecAnserBloomFilterConsume takes the AnserDispatchLocalConsume path for it. + * + * AnserIsRuntimeFilterScan answers the same question about a Plan, and is what + * the injection pass uses. Either would work here; this walk has the PlanState + * in hand, so it asks the shorter question. + */ +static bool +anser_is_rf_scan_state(const PlanState *ps) +{ + const CustomScanState *css; + + if (ps == NULL || !IsA(ps, CustomScanState)) + return false; + + css = (const CustomScanState *) ps; + return css->methods == &anser_produce_exec_methods || + css->methods == &anser_consume_exec_methods; +} + +/* + * Can `ps` take an SK_BLOOM_FILTER scan key for the column it emits at output + * targetlist position `attno`? On true, *sk_attno_out is the attribute number + * the key must carry; on false it is InvalidAttrNumber. + * + * `attno` is a position in the node's *output* targetlist, which is how + * anser_consume_next numbers things, because the slot it probes has already + * been projected. A scan key is numbered differently: PassByBloomFilter reads + * ss_ScanTupleSlot -- the table tuple, before projection -- so the key has to + * carry the table's own attribute number. The two coincide only when the scan + * emits every column in order; for a projecting scan they do not, and + * filtering the wrong column drops rows that can join. On probe(payload, id) + * with a targetlist of just [id], `attno` is 1 while the table attno is 2: + * a key carrying 1 tests `payload` against a filter built on `id` and discards + * every row. + * + * Only a SeqScan is accepted, because nodeSeqscan.c is the only consumer of + * this scan-key shape that sees an unprojected table tuple. DynamicSeqScan + * also carries a `filters` list, but it applies the filter to its child + * SeqScan's *projected* slot and remaps varattnos per partition, so it would + * need different numbering -- and the injection pass cannot reach one anyway, + * since AnserResolveKeyScan only terminates on a plain SeqScan. + */ +bool +AnserPushdownAccepts(PlanState *ps, AttrNumber attno, AttrNumber *sk_attno_out) +{ + SeqScanState *sss; + List *tlist; + TargetEntry *tle; + Var *var; + + if (sk_attno_out != NULL) + *sk_attno_out = InvalidAttrNumber; + + if (ps == NULL || !IsA(ps, SeqScanState)) + return false; + sss = (SeqScanState *) ps; + + /* + * With filter_in_seqscan off the scan hands its keys to the table AM + * instead of evaluating them itself, and nothing guarantees the AM + * understands SK_BLOOM_FILTER. + */ + if (!sss->filter_in_seqscan) + return false; + + tlist = ps->plan->targetlist; + if (attno < 1 || attno > list_length(tlist)) + return false; + tle = (TargetEntry *) list_nth(tlist, attno - 1); + if (tle == NULL || !IsA(tle->expr, Var)) + return false; /* a computed column has no table attno */ + var = (Var *) tle->expr; + + /* + * Only a plain user column of this very relation can be pushed. A + * whole-row reference (varattno 0) or a system column (negative) is not + * something PassByBloomFilter can hash the way the producer did, and a Var + * belonging to another range table entry would send the scan looking at an + * attno that means nothing to it. + */ + if (var->varno != ((Scan *) ps->plan)->scanrelid || var->varattno < 1) + return false; + + if (sk_attno_out != NULL) + *sk_attno_out = var->varattno; + return true; +} + +/* + * Walk down from `top` following the column at output position `attno`, and + * return the deepest node that would accept a scan key for it -- or NULL if + * none would. *sk_attno_out is the attribute number for that node. + * + * Deepest, because a filter applied further down rejects the row before more + * of the plan has touched it; this is the same reason RPT+ pushes its min-max + * filters "to the deepest possible operators". The walk matters because our + * nodes stack: when two joins trace their probe key to the same relation, the + * second consumer is inserted *under* the first, so the upper one's child is a + * CustomScanState rather than the scan. Before this walk existed the upper + * consumer simply declined and probed every tuple itself. + * + * Descent follows the column's provenance, so it is a path rather than a tree + * sweep: at each step exactly one child supplies the column. It continues + * only through our own nodes, which is the complete set of things that can sit + * between a consumer and its scan -- the injection pass puts the consumer + * directly above the scan, and the only thing that later gets between them is + * another one of ours. + */ +PlanState * +AnserPushdownTarget(PlanState *top, AttrNumber attno, AttrNumber *sk_attno_out) +{ + PlanState *best = NULL; + AttrNumber best_sk = InvalidAttrNumber; + PlanState *cur = top; + AttrNumber cur_attno = attno; + + while (cur != NULL) + { + AttrNumber sk_attno; + List *tlist; + TargetEntry *tle; + Var *var; + + if (AnserPushdownAccepts(cur, cur_attno, &sk_attno)) + { + best = cur; + best_sk = sk_attno; + } + + if (!anser_is_rf_scan_state(cur)) + break; + + /* Follow the column into our child, remapping the position. */ + tlist = cur->plan->targetlist; + if (cur_attno < 1 || cur_attno > list_length(tlist)) + break; + tle = (TargetEntry *) list_nth(tlist, cur_attno - 1); + if (tle == NULL || !IsA(tle->expr, Var)) + break; + var = (Var *) tle->expr; + if (var->varno != INDEX_VAR || var->varattno < 1) + break; /* not the identity tlist we build */ + + cur_attno = var->varattno; + cur = (PlanState *) linitial(((CustomScanState *) cur)->custom_ps); + } + + if (sk_attno_out != NULL) + *sk_attno_out = best_sk; + return best; +} + +/* + * Hand the received filter to the deepest scan that will take it, as an + * SK_BLOOM_FILTER scan key -- the exact structure PassByBloomFilter consumes + * (nodeSeqscan.c), so the scan does the pruning itself and a table AM that + * supports SCAN_SUPPORT_RUNTIME_FILTER may additionally use the key for + * block-level pruning. Runs before the child's first ExecProcNode, so the key + * is in node->filters when the scan descriptor is created. The filter stays + * owned by us; anser_consume_end ends the child before freeing it. + * + * Declining (leaving pushed_down false) costs nothing but the pushdown: the + * caller then probes the filter itself in anser_consume_next. */ static void anser_consume_pushdown(CustomScanState *node) { AnserBloomConsumeScanState *st = (AnserBloomConsumeScanState *) node; PlanState *child = (PlanState *) linitial(node->custom_ps); - SeqScanState *sss; + PlanState *target; + AttrNumber sk_attno; ScanKey sk; MemoryContext oldcxt; - if (!gp_enable_runtime_filter_pushdown || !IsA(child, SeqScanState)) + if (!gp_enable_runtime_filter_pushdown) return; - sss = (SeqScanState *) child; - if (!sss->filter_in_seqscan) + + target = AnserPushdownTarget(child, st->key_attno, &sk_attno); + if (target == NULL) return; + /* AnserPushdownAccepts admits nothing else, and the cast below needs it. */ + Assert(IsA(target, SeqScanState)); + oldcxt = MemoryContextSwitchTo(node->ss.ps.state->es_query_cxt); sk = (ScanKey) palloc0(sizeof(ScanKeyData)); - sk->sk_attno = st->key_attno; + sk->sk_attno = sk_attno; sk->sk_flags = SK_BLOOM_FILTER; sk->sk_argument = PointerGetDatum(st->filter); - sss->filters = lappend(sss->filters, sk); + ((SeqScanState *) target)->filters = + lappend(((SeqScanState *) target)->filters, sk); MemoryContextSwitchTo(oldcxt); st->pushed_down = true; @@ -704,6 +878,12 @@ anser_consume_next(CustomScanState *node) if (st->filter == NULL || st->pushed_down) return anser_rf_child_slot(node, slot); + /* + * key_attno is an output-targetlist position, which is how this slot + * is numbered: it has already been projected. The pushdown path sees + * the table tuple instead and needs the table attno -- see + * anser_consume_pushdown. + */ value = slot_getattr(slot, st->key_attno, &isnull); /* NULLs never match an equijoin key; let the join handle them. */ From c6e64615c4dc6dc3e31d71759ba0cd323911dfbe Mon Sep 17 00:00:00 2001 From: Leonid Borchuk Date: Thu, 15 Oct 2026 14:00:05 +0300 Subject: [PATCH 14/27] Fix four ways Anser could drop rows or hang a session A filter that is a subset of the build side's keys rejects rows that join, so every one of these is a wrong answer rather than a slow one. A build side under a nested loop yields different keys per outer tuple, but neither node rebuilds on rescan: the producer stays published and the consumer keeps the filter it has, so the second iteration is probed against the first iteration's keys. Refuse such a join at injection time, by intersecting the loop's nestParams with the build subtree's extParam -- the same test UpdateChangedParamSet makes at runtime. Cleanup was scoped by counting ExecutorEnd, which only ever measures recursion into ExecutorEnd itself. A statement run through SPI from a function body goes through its whole Start/Run/End inside the outer query's Run, so its End looked outermost and took the running query's channels with it. Count over execution instead, as auto_explain does. Two readers share the QD->QE connection, and the nextval one died on a sideband message: it took the type byte, then errored without the body, leaving the stream desynchronised and the session unusable. It now skips one whole and waits on, and Anser no longer subscribes when it has no deadline to wait out. The coordinator could also leave a QE's notification unread for the whole lifetime of the slice that sent it, because PQisBusy() reports the command rather than the queued message, and the bytes may already have been absorbed by a flush. Drain before deciding to wait. Co-Authored-By: Claude Opus 5 --- .../anser/expected/anser_runtime_filter.out | 157 +++++++++++++++++ gpcontrib/anser/sql/anser_runtime_filter.sql | 121 +++++++++++++ gpcontrib/anser/src/anserinit.c | 94 ++++++++-- gpcontrib/anser/src/anserplan.c | 67 +++++++- gpcontrib/anser/src/anserplanexec.c | 16 ++ gpcontrib/anser/src/ansersideband.c | 43 ++++- src/backend/cdb/dispatcher/cdbdisp_async.c | 68 ++++++++ src/backend/commands/sequence.c | 47 ++++- src/test/modules/Makefile | 1 + .../modules/libpq_isbusy_notify/.gitignore | 5 + src/test/modules/libpq_isbusy_notify/Makefile | 27 +++ .../libpq_isbusy_notify/libpq_isbusy_notify.c | 162 ++++++++++++++++++ .../modules/libpq_isbusy_notify/meson.build | 47 +++++ .../t/001_isbusy_notify.pl | 33 ++++ src/test/modules/meson.build | 1 + 15 files changed, 856 insertions(+), 33 deletions(-) create mode 100644 src/test/modules/libpq_isbusy_notify/.gitignore create mode 100644 src/test/modules/libpq_isbusy_notify/Makefile create mode 100644 src/test/modules/libpq_isbusy_notify/libpq_isbusy_notify.c create mode 100644 src/test/modules/libpq_isbusy_notify/meson.build create mode 100644 src/test/modules/libpq_isbusy_notify/t/001_isbusy_notify.pl diff --git a/gpcontrib/anser/expected/anser_runtime_filter.out b/gpcontrib/anser/expected/anser_runtime_filter.out index 9293817d4c9..11603d91ddf 100644 --- a/gpcontrib/anser/expected/anser_runtime_filter.out +++ b/gpcontrib/anser/expected/anser_runtime_filter.out @@ -259,6 +259,31 @@ SELECT 'the filter pruned nothing: it never arrived, or it arrived empty' AS pro --------- (0 rows) +-- Nested statement guard: a statement run through SPI from a function body +-- goes through its whole Start/Run/End inside the *outer* query's Run, so the +-- inner ExecutorEnd fires while the outer query is still executing. Dropping +-- channels there takes the running query's filters with it, and every consumer +-- that has not read yet fails open -- the exchange still happens, the filter is +-- just never used. +-- +-- This needs an inner join, not the left join above: the consumer defers its +-- first tuple only for INNER (see defer_first), which is what lets the join +-- emit a row -- and so evaluate the function -- before the consumer reads. +CREATE FUNCTION anser_rf_nested(x int) RETURNS int +LANGUAGE plpgsql VOLATILE AS $$ +BEGIN + PERFORM 1; + RETURN x; +END; +$$; +SELECT 'an inner statement dropped the running query''s filter' AS problem + WHERE anser_rf_pruned('SELECT anser_rf_nested(p.id) FROM anser_rf_build b + JOIN anser_rf_probe p ON b.id = p.id') = 0; + problem +--------- +(0 rows) + +DROP FUNCTION anser_rf_nested(int); DROP FUNCTION anser_rf_pruned(text); DROP TABLE anser_rf_r_push; DROP TABLE anser_rf_r_on, anser_rf_r_off, anser_rf_r_orca; @@ -339,6 +364,138 @@ SELECT count(*) AS only_off (1 row) DROP TABLE anser_rf_z_on, anser_rf_z_off, anser_rf_build_z, anser_rf_probe_z; +-- Parameterized rescan guard: a build side whose rows depend on a parameter an +-- enclosing nested loop reassigns yields a different key set per outer tuple, +-- and neither node rebuilds on rescan -- the producer stays published, the +-- consumer keeps the filter it already has. The second iteration would be +-- probed against the first iteration's keys, dropping every row that joins. +-- So the plan pass must refuse the join outright. +-- +-- OFFSET 0 keeps the subquery from being pulled up, so it stays a nested loop +-- with a correlated inner side. Each group's ids are disjoint, which is what +-- makes a stale filter visible: with the first group's filter still in place +-- the second group matches nothing. +CREATE TABLE anser_rf_nl_outer (g int) DISTRIBUTED REPLICATED; +CREATE TABLE anser_rf_nl_build (g int, id int) DISTRIBUTED BY (id); +CREATE TABLE anser_rf_nl_probe (id int) DISTRIBUTED BY (id); +INSERT INTO anser_rf_nl_outer VALUES (1), (2); +INSERT INTO anser_rf_nl_build SELECT 1, g FROM generate_series(1, 100) g; +INSERT INTO anser_rf_nl_build SELECT 2, g FROM generate_series(101, 200) g; +INSERT INTO anser_rf_nl_probe SELECT g FROM generate_series(1, 20000) g; +ANALYZE anser_rf_nl_outer; +ANALYZE anser_rf_nl_build; +ANALYZE anser_rf_nl_probe; +CREATE FUNCTION anser_rf_nodes(q text) RETURNS bigint +LANGUAGE plpgsql AS $$ +DECLARE + line text; + nodes bigint := 0; +BEGIN + FOR line IN EXECUTE 'EXPLAIN (COSTS OFF) ' || q LOOP + IF line ~ 'Anser Bloom' THEN + nodes := nodes + 1; + END IF; + END LOOP; + RETURN nodes; +END; +$$; +-- Both groups must report 100, with the filter on and off alike, and the plan +-- must carry no Anser node at all. +SET anser.runtime_filter = off; +SELECT o.g, s.n AS nl_off +FROM anser_rf_nl_outer o +CROSS JOIN LATERAL (SELECT count(*) n FROM anser_rf_nl_probe p + JOIN anser_rf_nl_build b ON p.id = b.id + WHERE b.g = o.g OFFSET 0) s +ORDER BY o.g; + g | nl_off +---+-------- + 1 | 100 + 2 | 100 +(2 rows) + +SET anser.runtime_filter = on; +SELECT o.g, s.n AS nl_on +FROM anser_rf_nl_outer o +CROSS JOIN LATERAL (SELECT count(*) n FROM anser_rf_nl_probe p + JOIN anser_rf_nl_build b ON p.id = b.id + WHERE b.g = o.g OFFSET 0) s +ORDER BY o.g; + g | nl_on +---+------- + 1 | 100 + 2 | 100 +(2 rows) + +SELECT anser_rf_nodes('SELECT o.g, s.n FROM anser_rf_nl_outer o +CROSS JOIN LATERAL (SELECT count(*) n FROM anser_rf_nl_probe p + JOIN anser_rf_nl_build b ON p.id = b.id + WHERE b.g = o.g OFFSET 0) s') AS nodes_in_parameterized_plan; + nodes_in_parameterized_plan +----------------------------- + 0 +(1 row) + +-- The same two tables joined without the correlation are still injected into: +-- the guard must refuse a parameterized build side, not every build side. +SELECT anser_rf_nodes('SELECT count(*) FROM anser_rf_nl_probe p + JOIN anser_rf_nl_build b ON p.id = b.id + WHERE b.g = 1') AS nodes_when_not_parameterized; + nodes_when_not_parameterized +------------------------------ + 2 +(1 row) + +DROP FUNCTION anser_rf_nodes(text); +DROP TABLE anser_rf_nl_outer, anser_rf_nl_build, anser_rf_nl_probe; +-- nextval guard: the QD sends two kinds of message down a QE's dispatch +-- connection -- the nextval reply and a sideband message -- and each has its +-- own reader. A reader that meets the other's message has to skip it whole: +-- taking the type byte and erroring leaves the body in the stream, and the +-- session then dies somewhere else with "protocol synchronization was lost". +-- +-- In the field this needs an Anser consumer to give up on a filter the +-- coordinator goes on to deliver anyway, which is a race and cannot be asked +-- for from SQL. The fault puts a sideband message in front of one nextval +-- reply instead, so the ordering is certain rather than likely; the +-- three-argument form is occurrence 1..1, so it fires once. +-- +-- Nothing here is Anser's -- the fault supplies the message and the fix is in +-- cdb_sequence_nextval_qe -- but Anser is why a second message type exists on +-- that connection at all, so the test lives with it. +-- +-- The extension may or may not already be there, and saying so is not part of +-- what is being tested; it is never dropped, for the same reason -- it can +-- predate this test, and other tests in the same database rely on it. +-- start_ignore +CREATE EXTENSION IF NOT EXISTS gp_inject_fault; +NOTICE: extension "gp_inject_fault" already exists, skipping +-- end_ignore +CREATE SEQUENCE anser_rf_seq; +CREATE TABLE anser_rf_seq_t (id int) DISTRIBUTED BY (id); +INSERT INTO anser_rf_seq_t SELECT g FROM generate_series(1, 10) g; +SELECT gp_inject_fault('nextval_sideband_before_response', 'skip', dbid) + FROM gp_segment_configuration WHERE role = 'p' AND content = -1; + gp_inject_fault +----------------- + Success: +(1 row) + +SELECT count(nextval('anser_rf_seq')) AS seq_rows FROM anser_rf_seq_t; + seq_rows +---------- + 10 +(1 row) + +SELECT gp_inject_fault('nextval_sideband_before_response', 'reset', dbid) + FROM gp_segment_configuration WHERE role = 'p' AND content = -1; + gp_inject_fault +----------------- + Success: +(1 row) + +DROP SEQUENCE anser_rf_seq; +DROP TABLE anser_rf_seq_t; -- --------------------------------------------------------------------------- -- Parallel execution. -- diff --git a/gpcontrib/anser/sql/anser_runtime_filter.sql b/gpcontrib/anser/sql/anser_runtime_filter.sql index ec46b6e0596..736823fbd57 100644 --- a/gpcontrib/anser/sql/anser_runtime_filter.sql +++ b/gpcontrib/anser/sql/anser_runtime_filter.sql @@ -170,6 +170,27 @@ SET optimizer = off; SET anser.runtime_filter = on; SELECT 'the filter pruned nothing: it never arrived, or it arrived empty' AS problem WHERE anser_rf_pruned('SELECT b.name, p.payload FROM anser_rf_build b LEFT JOIN anser_rf_probe p ON b.id = p.id') = 0; +-- Nested statement guard: a statement run through SPI from a function body +-- goes through its whole Start/Run/End inside the *outer* query's Run, so the +-- inner ExecutorEnd fires while the outer query is still executing. Dropping +-- channels there takes the running query's filters with it, and every consumer +-- that has not read yet fails open -- the exchange still happens, the filter is +-- just never used. +-- +-- This needs an inner join, not the left join above: the consumer defers its +-- first tuple only for INNER (see defer_first), which is what lets the join +-- emit a row -- and so evaluate the function -- before the consumer reads. +CREATE FUNCTION anser_rf_nested(x int) RETURNS int +LANGUAGE plpgsql VOLATILE AS $$ +BEGIN + PERFORM 1; + RETURN x; +END; +$$; +SELECT 'an inner statement dropped the running query''s filter' AS problem + WHERE anser_rf_pruned('SELECT anser_rf_nested(p.id) FROM anser_rf_build b + JOIN anser_rf_probe p ON b.id = p.id') = 0; +DROP FUNCTION anser_rf_nested(int); DROP FUNCTION anser_rf_pruned(text); DROP TABLE anser_rf_r_push; @@ -230,6 +251,106 @@ SELECT count(*) AS only_off DROP TABLE anser_rf_z_on, anser_rf_z_off, anser_rf_build_z, anser_rf_probe_z; +-- Parameterized rescan guard: a build side whose rows depend on a parameter an +-- enclosing nested loop reassigns yields a different key set per outer tuple, +-- and neither node rebuilds on rescan -- the producer stays published, the +-- consumer keeps the filter it already has. The second iteration would be +-- probed against the first iteration's keys, dropping every row that joins. +-- So the plan pass must refuse the join outright. +-- +-- OFFSET 0 keeps the subquery from being pulled up, so it stays a nested loop +-- with a correlated inner side. Each group's ids are disjoint, which is what +-- makes a stale filter visible: with the first group's filter still in place +-- the second group matches nothing. +CREATE TABLE anser_rf_nl_outer (g int) DISTRIBUTED REPLICATED; +CREATE TABLE anser_rf_nl_build (g int, id int) DISTRIBUTED BY (id); +CREATE TABLE anser_rf_nl_probe (id int) DISTRIBUTED BY (id); +INSERT INTO anser_rf_nl_outer VALUES (1), (2); +INSERT INTO anser_rf_nl_build SELECT 1, g FROM generate_series(1, 100) g; +INSERT INTO anser_rf_nl_build SELECT 2, g FROM generate_series(101, 200) g; +INSERT INTO anser_rf_nl_probe SELECT g FROM generate_series(1, 20000) g; +ANALYZE anser_rf_nl_outer; +ANALYZE anser_rf_nl_build; +ANALYZE anser_rf_nl_probe; + +CREATE FUNCTION anser_rf_nodes(q text) RETURNS bigint +LANGUAGE plpgsql AS $$ +DECLARE + line text; + nodes bigint := 0; +BEGIN + FOR line IN EXECUTE 'EXPLAIN (COSTS OFF) ' || q LOOP + IF line ~ 'Anser Bloom' THEN + nodes := nodes + 1; + END IF; + END LOOP; + RETURN nodes; +END; +$$; + +-- Both groups must report 100, with the filter on and off alike, and the plan +-- must carry no Anser node at all. +SET anser.runtime_filter = off; +SELECT o.g, s.n AS nl_off +FROM anser_rf_nl_outer o +CROSS JOIN LATERAL (SELECT count(*) n FROM anser_rf_nl_probe p + JOIN anser_rf_nl_build b ON p.id = b.id + WHERE b.g = o.g OFFSET 0) s +ORDER BY o.g; +SET anser.runtime_filter = on; +SELECT o.g, s.n AS nl_on +FROM anser_rf_nl_outer o +CROSS JOIN LATERAL (SELECT count(*) n FROM anser_rf_nl_probe p + JOIN anser_rf_nl_build b ON p.id = b.id + WHERE b.g = o.g OFFSET 0) s +ORDER BY o.g; +SELECT anser_rf_nodes('SELECT o.g, s.n FROM anser_rf_nl_outer o +CROSS JOIN LATERAL (SELECT count(*) n FROM anser_rf_nl_probe p + JOIN anser_rf_nl_build b ON p.id = b.id + WHERE b.g = o.g OFFSET 0) s') AS nodes_in_parameterized_plan; + +-- The same two tables joined without the correlation are still injected into: +-- the guard must refuse a parameterized build side, not every build side. +SELECT anser_rf_nodes('SELECT count(*) FROM anser_rf_nl_probe p + JOIN anser_rf_nl_build b ON p.id = b.id + WHERE b.g = 1') AS nodes_when_not_parameterized; + +DROP FUNCTION anser_rf_nodes(text); +DROP TABLE anser_rf_nl_outer, anser_rf_nl_build, anser_rf_nl_probe; + +-- nextval guard: the QD sends two kinds of message down a QE's dispatch +-- connection -- the nextval reply and a sideband message -- and each has its +-- own reader. A reader that meets the other's message has to skip it whole: +-- taking the type byte and erroring leaves the body in the stream, and the +-- session then dies somewhere else with "protocol synchronization was lost". +-- +-- In the field this needs an Anser consumer to give up on a filter the +-- coordinator goes on to deliver anyway, which is a race and cannot be asked +-- for from SQL. The fault puts a sideband message in front of one nextval +-- reply instead, so the ordering is certain rather than likely; the +-- three-argument form is occurrence 1..1, so it fires once. +-- +-- Nothing here is Anser's -- the fault supplies the message and the fix is in +-- cdb_sequence_nextval_qe -- but Anser is why a second message type exists on +-- that connection at all, so the test lives with it. +-- +-- The extension may or may not already be there, and saying so is not part of +-- what is being tested; it is never dropped, for the same reason -- it can +-- predate this test, and other tests in the same database rely on it. +-- start_ignore +CREATE EXTENSION IF NOT EXISTS gp_inject_fault; +-- end_ignore +CREATE SEQUENCE anser_rf_seq; +CREATE TABLE anser_rf_seq_t (id int) DISTRIBUTED BY (id); +INSERT INTO anser_rf_seq_t SELECT g FROM generate_series(1, 10) g; +SELECT gp_inject_fault('nextval_sideband_before_response', 'skip', dbid) + FROM gp_segment_configuration WHERE role = 'p' AND content = -1; +SELECT count(nextval('anser_rf_seq')) AS seq_rows FROM anser_rf_seq_t; +SELECT gp_inject_fault('nextval_sideband_before_response', 'reset', dbid) + FROM gp_segment_configuration WHERE role = 'p' AND content = -1; +DROP SEQUENCE anser_rf_seq; +DROP TABLE anser_rf_seq_t; + -- --------------------------------------------------------------------------- -- Parallel execution. -- diff --git a/gpcontrib/anser/src/anserinit.c b/gpcontrib/anser/src/anserinit.c index 1ab12e3db09..da6cb30e96d 100644 --- a/gpcontrib/anser/src/anserinit.c +++ b/gpcontrib/anser/src/anserinit.c @@ -26,6 +26,7 @@ * planner_hook runtime-filter injection * RegisterCustomScanMethods the injected plan nodes * cdbdisp_notify_hook parts arriving from segments + * ExecutorRun/Finish_hook knowing whether a query is still executing * ExecutorEnd_hook dropping a query's channels * * It must be preloaded, because a segment backend deserializing a dispatched @@ -65,11 +66,33 @@ static PlannedStmt *anser_planner(Query *parse, const char *query_string, OptimizerOptions *optimizer_options); static planner_hook_type prev_planner_hook = NULL; +static ExecutorRun_hook_type prev_ExecutorRun_hook = NULL; +static ExecutorFinish_hook_type prev_ExecutorFinish_hook = NULL; static ExecutorEnd_hook_type prev_ExecutorEnd_hook = NULL; static cdbdisp_notify_hook_type prev_cdbdisp_notify_hook = NULL; +/* + * How many queries are executing right now. A query's channels may only be + * dropped when nothing is left running, and the count has to be taken over + * execution rather than over ExecutorEnd: a statement run through SPI from a + * function body goes through its whole Start/Run/End inside the *outer* + * query's Run, so by the time the inner ExecutorEnd fires the outer one has + * not entered its own End yet. Counting ends alone makes that inner End look + * outermost, and dropping channels there takes the running query's filters + * with it -- the exchange still happens, but every consumer then fails open. + * + * Nested execution is nested inside Run (and inside Finish, where AFTER + * triggers run), which is why those are the two that count. ExecutorEnd only + * reads the result: a query's own End always runs after its own Run returned, + * so an outermost query still sees zero. + */ +static int anser_exec_nesting = 0; + static bool anser_notify_hook(struct CdbDispatchResult *dispatchResult, struct pgNotify *notify); +static void anser_executor_run(QueryDesc *queryDesc, ScanDirection direction, + uint64 count, bool execute_once); +static void anser_executor_finish(QueryDesc *queryDesc); static void anser_executor_end(QueryDesc *queryDesc); static void anser_xact_callback(XactEvent event, void *arg); @@ -94,6 +117,10 @@ _PG_init(void) * dropped. ExecutorEnd covers the normal path; the transaction callback * catches queries that end by erroring. */ + prev_ExecutorRun_hook = ExecutorRun_hook; + ExecutorRun_hook = anser_executor_run; + prev_ExecutorFinish_hook = ExecutorFinish_hook; + ExecutorFinish_hook = anser_executor_finish; prev_ExecutorEnd_hook = ExecutorEnd_hook; ExecutorEnd_hook = anser_executor_end; RegisterXactCallback(anser_xact_callback, NULL); @@ -225,38 +252,77 @@ anser_notify_hook(struct CdbDispatchResult *dispatchResult, return false; } -/* - * Drop the transport's per-query state. - * - * Nested executor runs (a function body, say) must not clear state the outer - * query is still using, so only the outermost end resets. - */ +/* Count a query as executing for as long as it is running. */ static void -anser_executor_end(QueryDesc *queryDesc) +anser_executor_run(QueryDesc *queryDesc, ScanDirection direction, + uint64 count, bool execute_once) { - static int nesting_level = 0; + anser_exec_nesting++; + PG_TRY(); + { + if (prev_ExecutorRun_hook) + prev_ExecutorRun_hook(queryDesc, direction, count, execute_once); + else + standard_ExecutorRun(queryDesc, direction, count, execute_once); + } + PG_FINALLY(); + { + anser_exec_nesting--; + } + PG_END_TRY(); +} - nesting_level++; +/* The same, for the phase that runs AFTER triggers. */ +static void +anser_executor_finish(QueryDesc *queryDesc) +{ + anser_exec_nesting++; PG_TRY(); { - if (prev_ExecutorEnd_hook) - prev_ExecutorEnd_hook(queryDesc); + if (prev_ExecutorFinish_hook) + prev_ExecutorFinish_hook(queryDesc); else - standard_ExecutorEnd(queryDesc); + standard_ExecutorFinish(queryDesc); } PG_FINALLY(); { - nesting_level--; + anser_exec_nesting--; } PG_END_TRY(); +} - if (nesting_level == 0) +/* + * Drop the transport's per-query state, once nothing is executing. + * + * See anser_exec_nesting: a query ending while another is still running is an + * inner statement of that query, and its channels are not the ones to drop. + */ +static void +anser_executor_end(QueryDesc *queryDesc) +{ + if (prev_ExecutorEnd_hook) + prev_ExecutorEnd_hook(queryDesc); + else + standard_ExecutorEnd(queryDesc); + + if (anser_exec_nesting == 0) AnserSidebandResetAll(); } static void anser_xact_callback(XactEvent event, void *arg) { + /* + * Deliberately does not touch anser_exec_nesting. PG_FINALLY unwinds it + * on the error path, and XACT_EVENT_ABORT is only reached from + * AbortTransaction() at a statement boundary, by which point every + * executor frame is gone -- so there is nothing here to correct. Forcing + * it to zero would instead risk driving it negative if a frame ever did + * outlive the callback, and a negative count never compares equal to zero + * again: the resets below would stop happening for the rest of the + * session. auto_explain and pg_stat_statements keep their counters the + * same way, on PG_FINALLY alone. + */ if (event == XACT_EVENT_ABORT || event == XACT_EVENT_PARALLEL_ABORT) AnserSidebandResetAll(); } diff --git a/gpcontrib/anser/src/anserplan.c b/gpcontrib/anser/src/anserplan.c index 75c08a48fcd..3391db24461 100644 --- a/gpcontrib/anser/src/anserplan.c +++ b/gpcontrib/anser/src/anserplan.c @@ -57,6 +57,9 @@ typedef struct AnserInjectCtx { PlannedStmt *stmt; /* for slices[]: see AnserSliceProducers */ int slice_index; /* slice the subtree being walked runs in */ + Bitmapset *nestloop_params; /* PARAM_EXECs an enclosing nested loop + * reassigns per outer tuple; see + * anser_inject_walk */ uint32 next_condition_id; int next_plan_node_id; List *consumer_keys; /* condition_keys already given a consumer node; @@ -87,7 +90,14 @@ AnserApplyRuntimeFilters(PlannedStmt *stmt) return; { - AnserInjectCtx ctx; + /* + * Zeroed rather than filled field by field. The fields below say what + * they mean and are kept for that, but a pointer field added later and + * missed here would hold whatever was on the stack -- and the first + * thing done with nestloop_params is bms_copy(), which reads a word + * count out of it and allocates that much. + */ + AnserInjectCtx ctx = {0}; ListCell *lc; int maxid; @@ -116,6 +126,7 @@ AnserApplyRuntimeFilters(PlannedStmt *stmt) ctx.next_condition_id = 0; ctx.next_plan_node_id = maxid + 1; ctx.consumer_keys = NIL; + ctx.nestloop_params = NULL; /* no loop encloses the root */ anser_inject_walk(stmt->planTree, &ctx); } @@ -482,6 +493,34 @@ anser_try_inject(HashJoin *hj, AnserInjectCtx *ctx) if (probe == NULL) return; + /* + * Refuse a build side that depends on a parameter an enclosing nested loop + * reassigns. Such a side yields a different set of keys per outer tuple, + * and nothing here rebuilds: the rescan callbacks rescan their child and + * keep everything else, so the producer stays published and the consumer + * keeps the filter it already received. The second iteration would then be + * probed against the first iteration's keys -- a filter that is no longer a + * superset, which is the one way this feature can drop a row that joins. + * + * extParam is the whole build subtree's dependency on parameters set + * outside it (SS_finalize_plan fills it in before set_plan_references, so + * it is still here), and ctx->nestloop_params is what the nested loops we + * are nested inside actually reassign. An InitPlan's output parameter + * appears in extParam too but is evaluated once, so intersecting against + * the nestloop set rather than testing extParam for emptiness keeps those + * plans eligible. + * + * Only the build side is tested. A parameterized probe side is rescanned + * too, but the filter it probes was built from keys that did not change, so + * reusing it stays correct -- and skipping the rebuild is then a saving. + * + * The alternative is to re-exchange per iteration, which needs a generation + * in the channel key and a barrier across every segment per outer tuple. + * Not worth it for a shape this narrow. + */ + if (bms_overlap(hash->extParam, ctx->nestloop_params)) + return; + if (!anser_hashjoin_keys(hj, &inner_attno, &outer_attno)) return; if (!AnserResolveKeyScan(hash, inner_attno, ctx->slice_index, @@ -619,7 +658,31 @@ anser_inject_walk(Plan *plan, AnserInjectCtx *ctx) anser_try_inject((HashJoin *) plan, ctx); anser_inject_walk(outerPlan(plan), ctx); - anser_inject_walk(innerPlan(plan), ctx); + + /* + * A nested loop re-executes its inner side once per outer tuple, after + * assigning that tuple's values to the PARAM_EXECs in nestParams. Carry + * those paramids down the inner side so anser_try_inject can tell whether a + * build side it is looking at will be rebuilt with different values. + */ + if (IsA(plan, NestLoop) && ((NestLoop *) plan)->nestParams != NIL) + { + Bitmapset *saved = ctx->nestloop_params; + Bitmapset *inner = bms_copy(saved); + ListCell *lc; + + foreach(lc, ((NestLoop *) plan)->nestParams) + inner = bms_add_member(inner, + ((NestLoopParam *) lfirst(lc))->paramno); + + ctx->nestloop_params = inner; + anser_inject_walk(innerPlan(plan), ctx); + ctx->nestloop_params = saved; + bms_free(inner); + } + else + anser_inject_walk(innerPlan(plan), ctx); + if (IsA(plan, CustomScan)) { ListCell *lc; diff --git a/gpcontrib/anser/src/anserplanexec.c b/gpcontrib/anser/src/anserplanexec.c index a6c256c6960..8b8f93a4a8e 100644 --- a/gpcontrib/anser/src/anserplanexec.c +++ b/gpcontrib/anser/src/anserplanexec.c @@ -511,6 +511,21 @@ anser_produce_end(CustomScanState *node) ExecEndNode((PlanState *) linitial(node->custom_ps)); } +/* + * Rescan the child and keep every piece of filter state: the producer stays + * published, the consumer keeps the filter it received, and no second exchange + * happens. + * + * That is only correct because the build side cannot produce different keys on + * a second pass, which the injection pass guarantees by refusing any join whose + * build subtree depends on a parameter an enclosing nested loop reassigns (see + * the extParam test in anser_try_inject). Given that, every iteration wants + * the same filter, so rebuilding it would be pure cost -- and a channel is + * keyed per statement, so a second exchange could not be told apart from the + * first anyway. + * + * If that gate is ever loosened, this is the code that has to change with it. + */ static void anser_produce_rescan(CustomScanState *node) { @@ -934,6 +949,7 @@ anser_consume_end(CustomScanState *node) st->filter = NULL; } +/* Keeps the received filter; see anser_produce_rescan for why that is sound. */ static void anser_consume_rescan(CustomScanState *node) { diff --git a/gpcontrib/anser/src/ansersideband.c b/gpcontrib/anser/src/ansersideband.c index babc7bbaba8..c0e01700c43 100644 --- a/gpcontrib/anser/src/ansersideband.c +++ b/gpcontrib/anser/src/ansersideband.c @@ -173,6 +173,26 @@ AnserSidebandConsumeWait(const AnserChannelKey *channel_key, char payload_type, if (anser_inbox_take(channel_key, payload_type, payload, payload_len, cancelled)) return payload != NULL && *payload != NULL; + /* + * With no deadline to wait out there is nothing to subscribe for: we would + * give up on the next line anyway, and the only lasting effect would be the + * coordinator later pushing a message to a process that has stopped reading + * for it. Such a delivery is not free -- it arrives while this QE is + * somewhere else in the plan, and whatever reads the connection then has to + * recognise and skip it. So do not ask in the first place. + * + * This only narrows the window. Any deadline can expire with the + * subscription already registered, and there is no unsubscribe, so every + * reader of this connection still has to tolerate a message that is not its + * own (see cdb_sequence_nextval_qe). + */ + if (timeout_ms == 0) + { + ANSER_DEBUG("anser: seg%d not subscribing to cond=%u: no deadline to wait out", + GpIdentity.segindex, channel_key->condition_id); + return false; + } + /* * A subscription carries nothing, so its payload type is NONE -- what this * consumer expects to receive is checked on delivery, not announced here. @@ -277,11 +297,18 @@ anser_sideband_read_one(long timeout_ms) if (qtype != GP_SIDEBAND_MESSAGE) { /* - * Nothing else should reach us here, and if it does there is no way - * back: pq_getbyte_if_available() has already taken the byte and the - * backend has no means of pushing one in front of the stream, so this - * message can neither be handed to the command loop nor skipped - * safely. The connection is one byte short from here on. + * The one type that can legitimately turn up is '?', the nextval + * response: the QD sends it and cancels the QEs when it fails to get a + * value, so it can arrive with nobody waiting for it. We do not skip + * it, because that would consume a reply a later nextval might be + * about to wait for, and we are not the owner of that exchange. Other + * types should not reach us at all. + * + * Either way there is no way back by the time we are here: + * pq_getbyte_if_available() has already taken the byte, and the + * backend cannot push one in front of the stream, so the message can + * neither be handed to the command loop nor left for its owner. The + * connection is one byte short from here on. * * Terminating is therefore the only honest outcome. Returning and * running unfiltered -- the instinct everywhere else in this code -- @@ -292,9 +319,9 @@ anser_sideband_read_one(long timeout_ms) * connection cannot be reused: an ERROR would return this QE to the * gang pool with a desynchronised stream, poisoning a later query. * - * cdb_sequence_nextval_qe() reaches the same conclusion at - * sequence.c:438, one level milder, because it runs where the gang is - * about to be torn down anyway. + * The mirror case -- a sideband message reaching the nextval reader -- + * is the common one, and that reader skips it rather than dying: it + * knows our message is unowned by then, which we cannot say of its. */ pq_endmsgread(); ereport(FATAL, diff --git a/src/backend/cdb/dispatcher/cdbdisp_async.c b/src/backend/cdb/dispatcher/cdbdisp_async.c index 536ef3c15ae..62417d2e643 100644 --- a/src/backend/cdb/dispatcher/cdbdisp_async.c +++ b/src/backend/cdb/dispatcher/cdbdisp_async.c @@ -43,6 +43,7 @@ #include "miscadmin.h" #include "commands/sequence.h" #include "access/xact.h" +#include "utils/faultinjector.h" #include "utils/timestamp.h" #define DISPATCH_WAIT_TIMEOUT_MSEC 2000 @@ -564,6 +565,34 @@ checkDispatchResult(CdbDispatcherState *ds, int timeout_sec) segdbDesc->whoami, PQerrorMessage(conn)); } + /* + * Take any notifications off this connection before deciding + * whether to wait on it. + * + * The buffered[] check below cannot see them. PQisBusy() parses + * whatever is already in the buffer, and libpq's parser handles a + * NOTIFY by queueing it on the connection and carrying on + * (pqParseInput3's 'A' arm calls getNotify(), then breaks); it + * never touches asyncStatus. So a notification can be complete + * and ready while PQisBusy() still answers true for the command + * that has not finished, leaving buffered[] false. + * + * That would only be survivable while the socket stayed readable, + * and it need not be: the flush just above can have absorbed those + * very bytes, because a write that cannot complete in one go reads + * the peer's pending output to avoid deadlocking against it. The + * notification is then in our own memory with nothing left in the + * socket, and poll() sleeps until the QE happens to send something + * else -- which, for a sender that does not wait for a reply, is + * not until its command completes, since tuples travel over the + * interconnect rather than over this connection. + * + * Draining here costs a pointer check when nothing is queued. + * PQnotifies() runs the parser itself, so this also does the + * parsing PQisBusy() would have done a few lines below. + */ + processNotifies(dispatchResult); + /* * Add socket to fd_set if still connected. */ @@ -1039,6 +1068,31 @@ checkSegmentAlive(CdbDispatchCmdAsync *pParms) } } +/* + * Write a sideband message nobody asked for, to order the stream a particular + * way under the nextval_sideband_before_response fault. + * + * Only the framing matters, so the body is a marker rather than anything a + * subscriber could use: every reader of this connection has to get past a + * message that is not its own, and what is inside it never comes into that. + * A QE that happens to be waiting for a real filter logs that it could not + * read one and carries on unfiltered, which is the same fail-open path as a + * truncated delivery. + */ +static inline void +send_fake_sideband_message(PGconn *conn) +{ + static const char marker[] = "fault"; + + if (pqPutMsgStart(GP_SIDEBAND_MESSAGE, conn) < 0) + elog(ERROR, "Failed to send sideband message: %s", PQerrorMessage(conn)); + pqPutnchar(marker, sizeof(marker), conn); + if (pqPutMsgEnd(conn) < 0) + elog(ERROR, "Failed to send sideband message: %s", PQerrorMessage(conn)); + if (pqFlush(conn) < 0) + elog(ERROR, "Failed to send sideband message: %s", PQerrorMessage(conn)); +} + static inline void send_sequence_response(PGconn *conn, Oid oid, int64 last, int64 cached, int64 increment, bool overflow, bool error) { @@ -1117,6 +1171,20 @@ processNotifies(CdbDispatchResult *dispatchResult) PG_RE_THROW(); } PG_END_TRY(); + + /* + * Put a sideband message in front of the reply, so the QE meets + * one while it is reading for something else. That ordering is a + * race in the field -- it needs a consumer to have given up on a + * filter the coordinator then delivers anyway -- and this makes it + * certain instead. The QE must skip the message and go on waiting + * for its reply; taking the type byte and erroring would leave the + * body in the stream and desynchronise the connection. + */ + if (SIMPLE_FAULT_INJECTOR("nextval_sideband_before_response") == + FaultInjectorTypeSkip) + send_fake_sideband_message(segdbDesc->conn); + /* respond back on this libpq connection with the next value */ send_sequence_response(segdbDesc->conn, seq_oid, last, cached, increment, overflow, false /* error */); } diff --git a/src/backend/commands/sequence.c b/src/backend/commands/sequence.c index 29992eb02fd..bf49e3d758d 100644 --- a/src/backend/commands/sequence.c +++ b/src/backend/commands/sequence.c @@ -2149,22 +2149,51 @@ cdb_sequence_nextval_qe(Relation seqrel, /* * Read nextval response from QD. + * + * The loop is here because this is not the only thing the QD sends down + * this connection: a sideband message may arrive for a waiter that has + * already stopped waiting for it, and it can land in the middle of this + * exchange. Skipping one and carrying on is the only safe answer, since + * the type byte has been taken by then and there is no way to put it back + * for someone else to read. */ - do + for (;;) { - pq_startmsgread(); - retval = pq_getbyte_if_available(&qtype); - if (retval == 0) + StringInfoData sideband; + + do { - pq_endmsgread(); - CHECK_FOR_INTERRUPTS(); - } + pq_startmsgread(); + retval = pq_getbyte_if_available(&qtype); + if (retval == 0) + { + pq_endmsgread(); + CHECK_FOR_INTERRUPTS(); + } - if (retval == EOF) + if (retval == EOF) + ereport(ERROR, + (errcode(ERRCODE_INTERNAL_ERROR), + errmsg("nextval: connection is gone unexpectedly"))); + } while (retval != 1); + + if (qtype != GP_SIDEBAND_MESSAGE) + break; + + /* + * Not ours. Read the body out whole and drop it: whoever it was meant + * for is no longer reading, and leaving it in the stream would put + * every later read at the wrong offset -- which does not surface here + * but as an absurd allocation or an invalid message type somewhere + * else entirely, after the connection has gone back to the gang pool. + */ + initStringInfo(&sideband); + if (pq_getmessage(&sideband, PQ_LARGE_MESSAGE_LIMIT) != 0) ereport(ERROR, (errcode(ERRCODE_INTERNAL_ERROR), errmsg("nextval: connection is gone unexpectedly"))); - } while (retval != 1); + pfree(sideband.data); + } if (qtype == 'X') ereport(ERROR, (errcode(ERRCODE_INTERNAL_ERROR), errmsg("nextval: QD closed the connection"))); diff --git a/src/test/modules/Makefile b/src/test/modules/Makefile index 77af107b55e..8b67ea905c7 100644 --- a/src/test/modules/Makefile +++ b/src/test/modules/Makefile @@ -10,6 +10,7 @@ SUBDIRS = \ delay_execution \ dummy_index_am \ dummy_seclabel \ + libpq_isbusy_notify \ libpq_pipeline \ plsample \ snapshot_too_old \ diff --git a/src/test/modules/libpq_isbusy_notify/.gitignore b/src/test/modules/libpq_isbusy_notify/.gitignore new file mode 100644 index 00000000000..5dd57c4b4f5 --- /dev/null +++ b/src/test/modules/libpq_isbusy_notify/.gitignore @@ -0,0 +1,5 @@ +# Generated subdirectories +/log/ +/results/ +/tmp_check/ +/libpq_isbusy_notify diff --git a/src/test/modules/libpq_isbusy_notify/Makefile b/src/test/modules/libpq_isbusy_notify/Makefile new file mode 100644 index 00000000000..7ea27d27d95 --- /dev/null +++ b/src/test/modules/libpq_isbusy_notify/Makefile @@ -0,0 +1,27 @@ +# src/test/modules/libpq_isbusy_notify/Makefile + +PGFILEDESC = "libpq_isbusy_notify - test program for PQisBusy and queued notifications" +PGAPPICON = win32 + +PROGRAM = libpq_isbusy_notify +OBJS = $(WIN32RES) libpq_isbusy_notify.o + +NO_INSTALL = 1 + +# libpq-int.h: the test builds a connection state by hand rather than reading +# one off a socket, which is the only way to reach the case without a server. +PG_CPPFLAGS = -I$(libpq_srcdir) +PG_LIBS_INTERNAL += $(libpq_pgport) + +TAP_TESTS = 1 + +ifdef USE_PGXS +PG_CONFIG = pg_config +PGXS := $(shell $(PG_CONFIG) --pgxs) +include $(PGXS) +else +subdir = src/test/modules/libpq_isbusy_notify +top_builddir = ../../../.. +include $(top_builddir)/src/Makefile.global +include $(top_srcdir)/contrib/contrib-global.mk +endif diff --git a/src/test/modules/libpq_isbusy_notify/libpq_isbusy_notify.c b/src/test/modules/libpq_isbusy_notify/libpq_isbusy_notify.c new file mode 100644 index 00000000000..f11d0435d46 --- /dev/null +++ b/src/test/modules/libpq_isbusy_notify/libpq_isbusy_notify.c @@ -0,0 +1,162 @@ +/*------------------------------------------------------------------------- + * + * libpq_isbusy_notify.c + * PQisBusy() does not report a notification that is ready to collect. + * + * libpq's parser handles a NOTIFY by queueing it on the connection and + * carrying on: pqParseInput3's 'A' arm calls getNotify() and then breaks, + * never touching asyncStatus. PQisBusy() reports asyncStatus, so while the + * command that is in flight has not finished it keeps answering true -- even + * though a complete notification is sitting in conn->notifyHead with nothing + * left unread in the connection's buffer. + * + * Anything that treats !PQisBusy() as "nothing is ready for me" therefore has + * a blind spot, and the blind spot bites when the bytes reached the buffer + * without the socket staying readable. That is not exotic: a write that + * cannot complete in one go reads the peer's pending output to avoid + * deadlocking against it (pqSendSome -> pqReadData, fe-misc.c), so the + * notification can already be in our memory while poll() has nothing to + * report. The coordinator's dispatcher hit exactly this, which is why it now + * drains notifications before deciding whether to wait + * (checkDispatchResult, cdbdisp_async.c). + * + * This test pins the libpq behaviour that reasoning depends on. No server is + * involved and nothing is read from a socket: the input buffer is filled by + * hand with one complete NOTIFY message, which models the state after the + * bytes have already been absorbed. If libpq ever changes so that a queued + * notification stops PQisBusy() returning true, this fails -- and the drain in + * the dispatcher has become unnecessary. + * + * IDENTIFICATION + * src/test/modules/libpq_isbusy_notify/libpq_isbusy_notify.c + * + *------------------------------------------------------------------------- + */ +#include "postgres_fe.h" + +#include + +#include "libpq-fe.h" +#include "libpq-int.h" + +/* A NOTIFY as it appears on the wire, minus the leading message type byte. */ +static const char notify_channel[] = "anser"; +static const char notify_payload[] = "part"; + +int +main(void) +{ + PGconn *conn; + uint32 netlen; + uint32 netpid; + int msglen; + int pos = 0; + int busy; + int unread; + PGnotify *notify; + int failures = 0; + + /* + * PQconnectStart() allocates the buffers and returns without waiting for + * the connection, which is all we need -- the path is deliberately one + * that cannot exist, and the resulting status is overwritten below. We + * never read or write the socket, so no server is required. + */ + conn = PQconnectStart("host=/nonexistent-libpq-isbusy-notify dbname=postgres"); + if (conn == NULL) + { + fprintf(stderr, "could not allocate a connection\n"); + return 2; + } + + /* 4-byte length, 4-byte sender pid, then two NUL-terminated strings. */ + msglen = 4 + 4 + sizeof(notify_channel) + sizeof(notify_payload); + if (conn->inBufSize < msglen + 1) + { + fprintf(stderr, "input buffer is too small for the test message\n"); + PQfinish(conn); + return 2; + } + + /* + * Put the connection in the state it would be in with a command still in + * flight and one complete notification already parsed off the socket. + */ + conn->status = CONNECTION_OK; + conn->asyncStatus = PGASYNC_BUSY; + conn->inStart = conn->inCursor = 0; + + conn->inBuffer[pos++] = 'A'; + netlen = htonl(msglen); + memcpy(conn->inBuffer + pos, &netlen, 4); + pos += 4; + netpid = htonl(123); + memcpy(conn->inBuffer + pos, &netpid, 4); + pos += 4; + memcpy(conn->inBuffer + pos, notify_channel, sizeof(notify_channel)); + pos += sizeof(notify_channel); + memcpy(conn->inBuffer + pos, notify_payload, sizeof(notify_payload)); + pos += sizeof(notify_payload); + conn->inEnd = pos; + + /* Parses the buffer; never touches the socket. */ + busy = PQisBusy(conn); + unread = conn->inEnd - conn->inStart; + + printf("PQisBusy=%d notify_queued=%d unread_bytes=%d\n", + busy, conn->notifyHead != NULL, unread); + + /* + * The three halves of the trap, each worth failing on separately: the + * notification is ready, the connection still reports itself busy, and + * there is nothing left that would make the socket readable again. + */ + if (conn->notifyHead == NULL) + { + fprintf(stderr, "FAIL: the notification was not queued\n"); + failures++; + } + if (!busy) + { + fprintf(stderr, + "FAIL: PQisBusy() reported the notification, so a caller " + "watching it alone would not miss one\n"); + failures++; + } + if (unread != 0) + { + fprintf(stderr, + "FAIL: %d byte(s) left unparsed, so the message was not " + "complete and the test proved nothing\n", unread); + failures++; + } + + /* And it really is collectable, by the call that does look for it. */ + notify = PQnotifies(conn); + if (notify == NULL) + { + fprintf(stderr, "FAIL: PQnotifies() returned nothing\n"); + failures++; + } + else + { + printf("PQnotifies: channel=%s payload=%s\n", + notify->relname, notify->extra); + if (strcmp(notify->relname, notify_channel) != 0 || + strcmp(notify->extra, notify_payload) != 0) + { + fprintf(stderr, "FAIL: the notification did not round-trip\n"); + failures++; + } + PQfreemem(notify); + } + + /* + * Nothing was ever connected, so stop PQfinish() from trying to send a + * terminate message down a socket we invented a state for. + */ + conn->status = CONNECTION_BAD; + PQfinish(conn); + + return failures == 0 ? 0 : 1; +} diff --git a/src/test/modules/libpq_isbusy_notify/meson.build b/src/test/modules/libpq_isbusy_notify/meson.build new file mode 100644 index 00000000000..c13e1dfcdbd --- /dev/null +++ b/src/test/modules/libpq_isbusy_notify/meson.build @@ -0,0 +1,47 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +libpq_isbusy_notify_sources = files( + 'libpq_isbusy_notify.c', +) + +if host_system == 'windows' + libpq_isbusy_notify_sources += rc_bin_gen.process(win32ver_rc, extra_args: [ + '--NAME', 'libpq_isbusy_notify', + '--FILEDESC', 'libpq_isbusy_notify - test program for PQisBusy and queued notifications',]) +endif + +libpq_isbusy_notify = executable('libpq_isbusy_notify', + libpq_isbusy_notify_sources, + dependencies: [frontend_code, libpq], + kwargs: default_bin_args + { + 'install': false, + }, +) +testprep_targets += libpq_isbusy_notify + +tests += { + 'name': 'libpq_isbusy_notify', + 'sd': meson.current_source_dir(), + 'bd': meson.current_build_dir(), + 'tap': { + 'tests': [ + 't/001_isbusy_notify.pl', + ], + 'deps': [libpq_isbusy_notify], + }, +} diff --git a/src/test/modules/libpq_isbusy_notify/t/001_isbusy_notify.pl b/src/test/modules/libpq_isbusy_notify/t/001_isbusy_notify.pl new file mode 100644 index 00000000000..1b29f773e0f --- /dev/null +++ b/src/test/modules/libpq_isbusy_notify/t/001_isbusy_notify.pl @@ -0,0 +1,33 @@ + +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +use strict; +use warnings; + +use PostgreSQL::Test::Utils; +use Test::More; + +# No cluster is started. The program constructs the connection state it needs +# by hand and never reads or writes a socket, which is the point: the state +# under test is "the bytes are already in our buffer and the socket has gone +# quiet", awkward to provoke against a live server and trivial to build. +command_ok( + ['libpq_isbusy_notify'], + 'PQisBusy() stays true while a complete notification is queued'); + +done_testing(); diff --git a/src/test/modules/meson.build b/src/test/modules/meson.build index 9556bac4ad4..e2f8387fe33 100644 --- a/src/test/modules/meson.build +++ b/src/test/modules/meson.build @@ -6,6 +6,7 @@ subdir('delay_execution') subdir('dummy_index_am') subdir('dummy_seclabel') subdir('ldap_password_func') +subdir('libpq_isbusy_notify') subdir('libpq_pipeline') subdir('plsample') subdir('snapshot_too_old') From 1f9ab152d2294abc3b28e33d7cb5f26610d7d358 Mon Sep 17 00:00:00 2001 From: Leonid Borchuk Date: Fri, 16 Oct 2026 17:21:04 +0300 Subject: [PATCH 15/27] Fix Anser defects found in review and CI A blocking push stranded the dispatcher uncancellably when a consumer had timed out: deliver without blocking, and let a consumer withdraw. AnserSliceProducers counted a singleton reader and direct dispatch as the whole cluster, so the channel never completed. A producer whose slice ran but whose node never did stayed silent, leaving consumers to wait out the deadline. A consumer ignored QueryFinishPending, so a query the coordinator had already finished -- a LIMIT satisfied early -- still sat out the full deadline. CHECK_FOR_INTERRUPTS does not cover that flag. Injection is now refused when the build and probe sides share a slice. Slices are divided by Motions only, so a co-located join offers no synchronisation point to hang the exchange on and no network cost to save against the filter's own. probe_slice already carried the answer but was never read, and was left uninitialized on one path. Channel cleanup is wholesale, and inside SPI the executor nesting level never returns to zero, so a PL/pgSQL body held one merged filter per statement until the outermost one ended -- about 1 MB a query, and up to the full anser.max_info_size. A query now takes its own channels with it, keyed by the command count the channel already carries. Only finished ones: gp_command_count names the newest query descriptor rather than the one that is ending, and the two differ whenever a portal has outlived the statements that ran after it. Planning joins that nesting level too. Constant folding can run a query through SPI, and that query reached ExecutorEnd believing it was the outermost one -- the split counter PostgreSQL merged in 76db9cb6368. The anser.timeout_ms default drops from 100 s to 10 s. Its rationale had outlived itself: delivery no longer blocks, so a wide slice no longer needs the headroom it was sized for. The help text said otherwise, in four places. Hash keys are now checked on the base type and against the type's own equality operator. A domain inherits typlen and typbyval, so a domain over float8 passed both the by-value test and a float exclusion that names only the built-in OIDs -- and -0.0 would have stopped matching 0.0, dropping rows that join. The anser_test harness moves to a library of its own. It was linked into anser.so, which every user preloads, and its extension shipped with the subsystem; it now builds only for installcheck. It resolves the C API against the preloaded library at load time, which is what the PGDLLEXPORT markers on the public headers were for. Tests move to redistributed joins to match the Motion rule. The parallel section had also been running on the 1 ms deadline left over from the preceding block, which let it pass without a filter ever arriving; that block has since moved out of the file altogether. It keeps its coverage under GPORCA only, since the Postgres planner marks Motion paths parallel-unsafe. The catversion bump is withdrawn. The CustomScan serialization fix reaches only outfast/readfast, the binary format the dispatcher uses; the text branch that nodeToString writes into catalogs is untouched, and a CustomScan is a Plan node, which catalogs never store anyway. configure-cloudberry.sh no longer passes --with-anser unconditionally, which had put the extension into every CI and release build despite the configure default of no. The ic-anser job does not need it: the gpcontrib/anser target builds, installs and preloads the extension itself, so the matrix entry no longer arms the cluster either -- and the postgres_conf_addons hook added for that is gone with it. anser_test carries its own PG_MODULE_MAGIC. It had been relying on the one in anserinit.c, which stopped covering it when it became a library of its own. anser.max_info_size is synced to the segments. Both ends read it, and a segment hands it to pq_getmessage() as the cap on the message it is about to read, so a disagreement does not merely size things differently -- it frames the wire differently. Without the flag a gang created after the SET fell back to the compiled-in default. Syncing narrows that window rather than closing it, since a gang busy with an open cursor is never sent the SET, so a read that overruns the cap now terminates the connection instead of going on: pq_getmessage takes the length word and leaves the body behind, and a message boundary cannot be recovered from there. The README said POSTMASTER where the code said USERSET, and now says what the trade-off is. SHLIB_PREREQS is dropped. Anser links nothing and only reads libpq's headers, and under PGXS the prerequisite expands to "make -C $(libdir) all", for which there is no such target. preload-anser no longer mistakes tr's exit status for psql's: a psql that failed left the list empty, and the recipe then wrote shared_preload_libraries back as just anser, dropping every other library. It also leaves the cluster alone when it is already armed instead of restarting on every installcheck. The cases that need gp_inject_fault move to anser_faults. The regression driver already excludes a file that calls one when the debug extensions are off -- faultinjector_prep in src/Makefile.global.in -- so sharing a file with them took the rest of anser_runtime_filter down as well. The TAP helper uses pg_hton32 rather than htonl and so no longer needs , which MSVC does not have. The fold no longer touches the part header. It incremented total_parts once per fold, but a producer already writes the channel's producer count there, so a merged header carried 2N-1 -- which is N only when N is 1, and that is the shape the one test covering it pinned. The number is what a consumer reports and what the log prints when a filter goes missing, so it was wrong exactly where someone would be reading it. A unit test now folds three parts, where N and 2N-1 differ; it needs no cluster and runs from installcheck. anser.max_info_size stops at what the protocol can carry. The bound was INT_MAX, so a large setting overflowed the pq_getmessage() cap computed from it into a negative int and every delivery was then refused -- and anything past PQ_LARGE_MESSAGE_LIMIT could never have been read in the first place. The frame around the payload has a name now rather than being spelled out at the single place that needed it. The outer-join case in anser_multijoin produces NULL keys at last. Its right side held every key the left could offer, and the join above it is strict in that key, so reduce_outer_joins() made the LEFT JOIN an inner one before any path was considered. It now joins a table with gaps, through a subquery that keeps the reduction from applying. The tables the chains rest on are left alone: thinning one of those would have cost eighteen assertions to fix one. No filter reaches that join, and none can. Keeping the outer join costs a SubqueryScan, which the key walk does not descend through, and there is no way around it: a NULL-keyed row survives only under an outer join, an outer join survives only behind a subquery boundary. The block says so now, and checks what it can -- that such a plan returns the same rows with the filter on and off. The pushdown cases say whether anything was injected. They compared a filtered count against an unfiltered one, which also agree when no filter was ever built -- and the computed-key case turns out to be one of those: its subquery is pulled up, the hash clause becomes an expression, and anser_hashjoin_keys takes plain Vars only. The comment claiming otherwise is corrected rather than the behaviour. Every GUC a test pins is reset at the end of it. Three files out of four left one behind, and anser_faults all five. Smaller things: send_fake_sideband_message() checks what pqPutnchar() returns; the nextval reply no longer serializes uninitialized locals on its error path, where a QE reads overflow before error and would report reaching a sequence's maximum rather than the real failure; the condition key's zero-padding is written down beside the struct that requires it; and the comments naming AnserBloomShapeFor, AnserChannelLock and anser_rf_size now name things that exist. configure is regenerated for --with-anser. Co-Authored-By: Claude Opus 5 --- .github/workflows/build-cloudberry.yml | 13 +- .../scripts/configure-cloudberry.sh | 14 +- gpcontrib/anser/Makefile | 90 +++- gpcontrib/anser/README.md | 94 +++- gpcontrib/anser/anser_test/Makefile | 58 ++ .../{ => anser_test}/anser_test--1.0.sql | 24 + .../anser/{src => anser_test}/anser_test.c | 129 ++++- .../anser/{ => anser_test}/anser_test.control | 2 +- gpcontrib/anser/expected/anser_faults.out | 284 ++++++++++ gpcontrib/anser/expected/anser_multijoin.out | 64 ++- .../anser/expected/anser_runtime_filter.out | 509 +++++++++++++++--- gpcontrib/anser/expected/anser_test.out | 113 +++- gpcontrib/anser/include/anser.h | 22 + gpcontrib/anser/include/anserbloom.h | 12 +- gpcontrib/anser/include/anserfilter.h | 2 +- gpcontrib/anser/include/anserplan.h | 9 + gpcontrib/anser/include/ansersideband.h | 13 +- gpcontrib/anser/sql/anser_faults.sql | 207 +++++++ gpcontrib/anser/sql/anser_multijoin.sql | 64 ++- gpcontrib/anser/sql/anser_runtime_filter.sql | 350 +++++++++--- gpcontrib/anser/sql/anser_test.sql | 76 ++- gpcontrib/anser/src/anserbloomconsume.c | 24 +- gpcontrib/anser/src/anserdispatch.c | 269 ++++++++- gpcontrib/anser/src/anserfilter.c | 18 +- gpcontrib/anser/src/anserinit.c | 135 ++++- gpcontrib/anser/src/anserplan.c | 275 +++++++++- gpcontrib/anser/src/anserplanexec.c | 79 ++- gpcontrib/anser/src/ansersideband.c | 143 ++++- gpcontrib/anser/test/.gitignore | 1 + gpcontrib/anser/test/Makefile | 66 +++ gpcontrib/anser/test/anserfold_test.c | 258 +++++++++ pom.xml | 1 - src/backend/cdb/dispatcher/cdbdisp_async.c | 34 +- src/include/catalog/catversion.h | 2 +- .../libpq_isbusy_notify/libpq_isbusy_notify.c | 7 +- 35 files changed, 3066 insertions(+), 395 deletions(-) create mode 100644 gpcontrib/anser/anser_test/Makefile rename gpcontrib/anser/{ => anser_test}/anser_test--1.0.sql (80%) rename gpcontrib/anser/{src => anser_test}/anser_test.c (88%) rename gpcontrib/anser/{ => anser_test}/anser_test.control (95%) create mode 100644 gpcontrib/anser/expected/anser_faults.out create mode 100644 gpcontrib/anser/sql/anser_faults.sql create mode 100644 gpcontrib/anser/test/.gitignore create mode 100644 gpcontrib/anser/test/Makefile create mode 100644 gpcontrib/anser/test/anserfold_test.c diff --git a/.github/workflows/build-cloudberry.yml b/.github/workflows/build-cloudberry.yml index 3dc5f38e5c6..d2f1428f823 100644 --- a/.github/workflows/build-cloudberry.yml +++ b/.github/workflows/build-cloudberry.yml @@ -244,9 +244,7 @@ jobs: "pg_settings":{"optimizer":"on"} }, {"test":"ic-anser", - "make_configs":["gpcontrib/anser:installcheck-with-install"], - "shared_preload_libraries":"anser", - "postgres_conf_addons":"anser.enable=on" + "make_configs":["gpcontrib/anser:installcheck-with-install"] }, {"test":"pax-ic-good-opt-off", "make_configs":[ @@ -1575,15 +1573,6 @@ jobs: echo "Adding shared_preload_libraries: ${{ matrix.shared_preload_libraries }}" fi - # Append extra postgresql.conf settings requested by the matrix - # entry (e.g. postmaster-context GUCs a test module requires). - # BLDWRAP_POSTGRES_CONF_ADDONS is a pipe-separated list of - # key=value pairs (see gpAux/gpdemo/demo_cluster.sh). - if [[ -n "${{ matrix.postgres_conf_addons }}" ]]; then - EXTRA_CONF="${EXTRA_CONF}${EXTRA_CONF:+|}${{ matrix.postgres_conf_addons }}" - echo "Adding postgres_conf_addons: ${{ matrix.postgres_conf_addons }}" - fi - if ! time su - gpadmin -c "cd ${SRC_DIR} && NUM_PRIMARY_MIRROR_PAIRS='${{ matrix.num_primary_mirror_pairs }}' BLDWRAP_POSTGRES_CONF_ADDONS=\"${EXTRA_CONF}\" SRC_DIR=${SRC_DIR} ${SRC_DIR}/devops/build/automation/cloudberry/scripts/create-cloudberry-demo-cluster.sh"; then echo "::error::Demo cluster creation failed" exit 1 diff --git a/devops/build/automation/cloudberry/scripts/configure-cloudberry.sh b/devops/build/automation/cloudberry/scripts/configure-cloudberry.sh index 370c97703e6..267fe2ce3cf 100755 --- a/devops/build/automation/cloudberry/scripts/configure-cloudberry.sh +++ b/devops/build/automation/cloudberry/scripts/configure-cloudberry.sh @@ -32,7 +32,7 @@ # - IC Proxy Support # - Oracle Compatibility (orafce) # - ORCA Query Optimizer -# - Anser Runtime Filters +# - Anser Runtime Filters (opt-in, ENABLE_ANSER=true) # - PAX Access Method # - Test Automation Support (tap-tests) # @@ -148,6 +148,16 @@ if [ "${ENABLE_MDBLOCALES:-false}" = "true" ]; then CONFIGURE_MDBLOCALES_OPTS="--with-mdblocales" fi +# Anser is opt-in at configure time, and stays opt-in here: passing +# --with-anser unconditionally would put it into every release artifact, which +# is the opposite of what the configure default says. The ic-anser CI job does +# not need it -- gpcontrib/anser's installcheck-with-install builds, installs +# and preloads the extension itself. +CONFIGURE_ANSER_OPTS="" +if [ "${ENABLE_ANSER:-false}" = "true" ]; then + CONFIGURE_ANSER_OPTS="--with-anser" +fi + # Configure build log_section "Configure" execute_cmd ./configure --prefix=${BUILD_DESTINATION} \ @@ -159,7 +169,7 @@ execute_cmd ./configure --prefix=${BUILD_DESTINATION} \ --enable-pax \ --enable-tap-tests \ ${CONFIGURE_DEBUG_OPTS} \ - --with-anser \ + ${CONFIGURE_ANSER_OPTS} \ --with-diskquota \ --with-gp-stats-collector \ --with-gssapi \ diff --git a/gpcontrib/anser/Makefile b/gpcontrib/anser/Makefile index 42c93ac0ff7..dbad6d5a823 100644 --- a/gpcontrib/anser/Makefile +++ b/gpcontrib/anser/Makefile @@ -35,19 +35,21 @@ OBJS = \ src/anserpayload.o \ src/anserplan.o \ src/anserplanexec.o \ - src/ansersideband.o \ - src/anser_test.o + src/ansersideband.o PGFILEDESC = "anser - adaptive information sharing runtime filters" -# The subsystem itself needs no catalog objects -- it is configured entirely by -# GUCs and travels over the dispatch connection. The only extension here is -# anser_test, which exposes the internal C API to the regression tests from -# inside the same library. -EXTENSION = anser_test -DATA = anser_test--1.0.sql - -REGRESS = anser_test anser_runtime_filter anser_multijoin +# No catalog objects: the subsystem is configured entirely by GUCs and travels +# over the dispatch connection, so there is no extension to install here. The +# harness the tests use to reach the C API from SQL is a library of its own in +# anser_test/ -- this one is preloaded into every backend and has no business +# carrying test code. +# anser_faults is split out rather than gated: everything in it calls +# gp_inject_fault, which exists only in a build with the debug extensions, and +# the regression driver drops any file that calls one (faultinjector_prep in +# src/Makefile.global.in). Keeping them in anser_runtime_filter would take the +# rest of that file down with them wherever the extensions are off. +REGRESS = anser_test anser_runtime_filter anser_faults anser_multijoin # src/anserdispatch.c writes sideband messages onto the dispatcher's own libpq # connections, so it needs libpq's internal headers (as the dispatcher itself @@ -55,8 +57,12 @@ REGRESS = anser_test anser_runtime_filter anser_multijoin # global in the postgres binary, while the public PQ* API is made local there # on purpose (src/backend/Makefile), so linking libpq.so would put a second # copy of libpq on connections the backend's copy created. +# +# Headers only means no SHLIB_PREREQS either. submake-libpq is there to build +# what a module links against, and under PGXS libpq_builddir is $(libdir), so +# it would run "make -C $(libdir) all" and fail outright; in tree it stopped +# the build on a libpq that had not been built yet, before compiling anything. PG_CPPFLAGS = -I$(srcdir)/include -I$(libpq_srcdir) -SHLIB_PREREQS = submake-libpq ifdef USE_PGXS PG_CONFIG ?= pg_config @@ -72,9 +78,44 @@ endif # The tests need the live gather/send services, which only exist when the # library is preloaded and anser.enable is on -- both postmaster-context # settings. Ensure them (and restart) before installcheck, as -# gp_relsizes_stats does for its worker. -installcheck: preload-anser +# gp_relsizes_stats does for its worker. They also need the anser_test +# extension, which install deliberately leaves out: it is a test tool, so it is +# put in place here rather than shipped with the subsystem. +installcheck: unitcheck install-anser-test preload-anser + +.PHONY: install-anser-test +install-anser-test: + $(MAKE) -C anser_test install + +# The unit tests need no cluster of their own. Hanging them off installcheck +# is what gets them run at all: a failure here stops make before the cluster is +# touched. (Prerequisites are unordered under -j; what matters is that a +# broken one fails the target, not which runs first.) +.PHONY: unitcheck +unitcheck: + $(MAKE) -C test unitcheck + +# Neither the harness nor the unit tests are part of this library's build, so +# they are not part of its clean either unless said so. +clean: clean-anser-test +.PHONY: clean-anser-test +clean-anser-test: + $(MAKE) -C test clean + $(MAKE) -C anser_test clean + +# Arm the cluster, and only disturb it when that is actually needed. +# +# The decision takes one shell: make runs each recipe line in its own, and +# whether a restart is called for is only known after the first half. That +# gives up make's per-line status check, so every command below ends in +# "|| exit 1". +# +# SHOW reports what the postmaster really loaded rather than what +# postgresql.conf says, which is the question being asked -- a library +# configured but never restarted into still needs the restart. anser.enable is +# only worth asking about once anser is loaded: until then the GUC does not +# exist and SHOW errors out instead of answering "off". .PHONY: preload-anser preload-anser: @if [ -z "$$COORDINATOR_DATA_DIRECTORY$$MASTER_DATA_DIRECTORY" ]; then \ @@ -82,18 +123,29 @@ preload-anser: echo " source cloudberry-env.sh and gpdemo-env.sh before running installcheck." >&2; \ exit 1; \ fi - @current=`psql -d postgres -At -c "SHOW shared_preload_libraries;" | tr -d ' '`; \ + @current=`psql -d postgres -At -c "SHOW shared_preload_libraries;"` || { \ + echo "ERROR: cannot read shared_preload_libraries; is the cluster up?" >&2; \ + exit 1; \ + }; \ + current=`echo "$$current" | tr -d ' '`; \ + need_restart=; \ case ",$$current," in \ *,anser,*) \ echo "==> shared_preload_libraries already contains anser" ;; \ *) \ if [ -z "$$current" ]; then new=anser; else new="$$current,anser"; fi; \ echo "==> Adding anser to shared_preload_libraries ($$new)"; \ - gpconfig -c shared_preload_libraries -v "'$$new'" --skipvalidation ;; \ - esac - gpconfig -c anser.enable -v on --skipvalidation - gpstop -ra - psql -d postgres -c "SHOW shared_preload_libraries;" + gpconfig -c shared_preload_libraries -v "'$$new'" --skipvalidation || exit 1; \ + need_restart=1 ;; \ + esac; \ + if [ -z "$$need_restart" ] && \ + [ "`psql -d postgres -At -c 'SHOW anser.enable' 2>/dev/null`" = on ]; then \ + echo "==> anser.enable is already on; leaving the cluster alone"; \ + else \ + gpconfig -c anser.enable -v on --skipvalidation || exit 1; \ + gpstop -ra || exit 1; \ + fi; \ + psql -d postgres -c "SHOW shared_preload_libraries;" || exit 1 # CI runs a single make target per test config, so offer one target that # installs the extension, arms the cluster, and runs the tests. diff --git a/gpcontrib/anser/README.md b/gpcontrib/anser/README.md index 7d6e9305581..5872cf97be0 100644 --- a/gpcontrib/anser/README.md +++ b/gpcontrib/anser/README.md @@ -35,9 +35,12 @@ to be registered before that happens. Then turn the filter on where you want it — `anser.runtime_filter` is `PGC_USERSET`, so per session, per role, or cluster-wide. -`anser_test` is a separate control file over the same library, exposing the -internal C API to the regression tests. It is test-only; do not create it in -production databases. +`anser_test` is a separate extension, built from `anser_test/` as a library of +its own, that exposes the internal C API to the regression tests. It resolves +that API at load time against the preloaded `anser.so`, which is what the +`PGDLLEXPORT` markers on the public headers are for. `make install` does not +build or install it — `make installcheck` does — so a cluster never carries it +unless someone puts it there deliberately. ## Architecture @@ -94,6 +97,27 @@ yet reading — and that producer only publishes once its subtree is drained, which it cannot be. That is a deadlock, not a slow path, and it is why the intermediate result cannot filter the table it is being joined to. +### Only across a Motion + +A join is injected into only when its build and probe scans run in **different +slices**. Slices are divided by Motions and nothing else, so this says: filter +only where the rows already travel over the network. + +It is a safety rule first. An exchange is a synchronisation point — the +consumer stops until the coordinator has heard from every producer — and +within one slice producer and consumer are the same process, run one after the +other. A consumer placed under an intervening hash join then waits for a +producer in that join's inner side, which cannot start until the outer side +yields the very tuple the consumer is holding. `defer_first` covers exactly +one tuple of that, and no more. Across a Motion they are separate processes +and the question does not arise. + +It is also what makes the exchange worth its cost. Where a Motion already +carries the rows, the barrier is paid for by work the query was doing anyway. +In a co-located join it is new cost against a join that was already local, and +whether it pays for itself is a measurement nobody has made — so until someone +does, those joins are left alone. + ### Giving up early A bloom filter that is too small for its key count matches almost everything: it @@ -140,9 +164,16 @@ injected into. Fan-in is parallel and fan-out is not. Every producer uploads its part at the same time while the coordinator reads as it goes, but `anser_disp_deliver()` -then walks its subscribers and writes the merged filter to each in turn, -blocking until that peer has taken it. Delivery latency is therefore the *sum* -of the writes, not the slowest one, and it grows with the width of the slice. +then walks its subscribers and writes the merged filter to each in turn. +Delivery latency is therefore the *sum* of the writes, not the slowest one, and +it grows with the width of the slice. + +That is only the tail of what a consumer waits for, and which part dominates +depends on the plan. Where the probe side is local to the join, `prefetch_inner` +has already drained the build side before the consumer runs — every producer has +published, and only the fold and the fan-out are left. Where the probe side +travels through its own Motion, the consumer sits in that scan's slice and runs +alongside the build, so it waits the build side out as well. Measured on 4 segments, 5000 build rows, a 1 MB filter (`bloom_create` floors every bitset at 1 MB, whatever the cardinality): @@ -152,11 +183,20 @@ every bitset at 1 MB, whatever the cardinality): | 4 | 4 | ~40 ms | ~520 ms | 549 ms | | 16 | 16 | ~100 ms | ~1140 ms | 1005 ms, then gave up | -The second row is why the default deadline is 100 s rather than 1 s: at 16 -subscribers the fan-out outlived a 1 s deadline, so every consumer ran -unfiltered. Raising it makes the filter arrive, but the wait is not free — the -first subscriber is served in under a millisecond and the last over a second -later, and none of them can tell where in the queue they are. +These numbers predate the switch to non-blocking delivery. Each write above +blocked until that peer had taken the filter, which is what made a wide slice so +expensive — and what could strand the dispatcher outright when nobody was +reading. Delivery now queues and flushes without blocking, so the fan-out should +be well under this; it has not been re-measured. + +The second row is still what sets the floor for the deadline: at 16 subscribers +the fan-out outlived a 1 s deadline, so every consumer ran unfiltered. The 10 s +default clears even that worst measured case by most of an order of magnitude, +while keeping a filter that never arrives from dominating the query. It is a +budget for an exchange that has gone wrong, not a measurement of a healthy one. +The wait is not free either — the first subscriber is served in under a +millisecond and the last much later, and none of them can tell where in the +queue they are. **A filter can cost more than it saves.** In the 16-wide plan the consumer waits 160 ms to skip a probe scan that takes 40 ms. Anser is worth enabling when the @@ -328,10 +368,38 @@ by up to that long. | --- | --- | --- | --- | | `anser.enable` | `off` | SIGHUP | Master switch. With it off the plan pass never injects anything and no filters are exchanged. | | `anser.runtime_filter` | `off` | USERSET | Enables the post-planning pass that injects bloom-filter producer/consumer nodes into a matching plan. Requires `anser.enable`. | -| `anser.max_info_size` | `65 MB` | POSTMASTER | Maximum serialized payload (merged bloom filter + part header) a channel may hold; caps the effective bloom-filter size. The default is `64 MB + 1 MB` so a full 64 MB power-of-two bitset fits with its header; `bloom_create` also floors every bitset at 1 MB. | -| `anser.timeout_ms` | `100 s` | USERSET | How long a consumer waits for its filter before running unfiltered. The deadline matters because a producer that gets squelched never publishes at all: `ExecSquelchNode` only marks a `CustomScanState`, it does not call the node back. The default is generous because delivery is serial — see [Delivery cost](#delivery-cost). | +| `anser.max_info_size` | `65 MB` | USERSET | Maximum serialized payload (merged bloom filter + part header) a channel may hold; caps the effective bloom-filter size. The default is `64 MB + 1 MB` so a full 64 MB power-of-two bitset fits with its header; `bloom_create` also floors every bitset at 1 MB. The ceiling is a little under 1 GB: the payload has to arrive as a single protocol message, and `pq_getmessage()` will not read one past `PQ_LARGE_MESSAGE_LIMIT`. Both ends read it — see [Changing max_info_size](#changing-max_info_size) before touching it on a live session. | +| `anser.timeout_ms` | `10 s` | USERSET | How long a consumer waits for its filter before running unfiltered. What the wait covers depends on the plan — see [Delivery cost](#delivery-cost). The deadline matters because a producer that gets squelched never publishes at all: `ExecSquelchNode` only marks a `CustomScanState`, it does not call the node back. | | `anser.debug` | `off` | USERSET | Traces the exchange — publish, merge, delivery, receive — in the log of the process each step happens in. See below. | +### Changing max_info_size + +`anser.max_info_size` is the one GUC both ends of the exchange read, and they +read it for different things. The coordinator sizes the filter against it when +it plans, and bounds the parts it will accept; a segment bounds what it +publishes, and passes it to `pq_getmessage()` as the cap on the sideband message +it is about to read. A coordinator and a segment that disagree therefore do not +merely compute different sizes — they frame the wire differently. + +It is `USERSET` rather than `POSTMASTER` because that is how the rest of Anser's +knobs behave, and because being able to try a different filter size without a +cluster restart is worth something. That is a trade-off, not a free choice: +**do not change it on a live session.** `GUC_GPDB_NEED_SYNC` gets the new value +to the segments — synchronously for every idle one, and in the connection +options of every gang created afterwards — but a gang that is busy with an open +cursor is not reached. Core does not update such a gang; it marks it as not +reusable and moves on (`cdbdisp_markNamedPortalGangsDestroyed()`), so its +segments keep the old value until the cursor is closed. + +While that lasts, the two failure directions differ. A part sized by the old +ceiling and rejected by the new one simply never completes its channel, and the +consumers run unfiltered after `anser.timeout_ms` — the usual fail-open. But a +delivery that overruns a segment's `pq_getmessage()` cap takes the length word +and leaves the body in the stream, and there is no recovering a message boundary +from there: the consumer terminates the connection rather than return it to the +gang pool poisoned. Set it in `postgresql.conf`, or set it before the session +does any work. + ### Tracing an exchange Until a filter is either used or timed out, none of the handoff is visible in diff --git a/gpcontrib/anser/anser_test/Makefile b/gpcontrib/anser/anser_test/Makefile new file mode 100644 index 00000000000..71b1c4f22ac --- /dev/null +++ b/gpcontrib/anser/anser_test/Makefile @@ -0,0 +1,58 @@ +#------------------------------------------------------------------------- +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# +# Makefile for anser_test, the harness the anser regression tests use to +# reach Anser's C API from SQL. +# +# A library of its own on purpose: linked into anser.so it would ride along +# into every backend, since anser is preloaded. Nothing here belongs in a +# running cluster, so the parent's install does not build it -- see the +# install-anser-test target there. +# +# It links against nothing: anser.so is preloaded and dfmgr.c loads modules +# RTLD_GLOBAL, so the AnserXxx entry points resolve at load time. That is +# what the PGDLLEXPORT markers on the public headers are for. +# +# IDENTIFICATION +# gpcontrib/anser/anser_test/Makefile +# +#------------------------------------------------------------------------- + +MODULE_big = anser_test +OBJS = \ + $(WIN32RES) \ + anser_test.o + +PGFILEDESC = "anser_test - test helpers for the anser extension" + +EXTENSION = anser_test +DATA = anser_test--1.0.sql + +PG_CPPFLAGS = -I$(srcdir)/../include + +ifdef USE_PGXS +PG_CONFIG ?= pg_config +PGXS := $(shell $(PG_CONFIG) --pgxs) +include $(PGXS) +else +subdir = gpcontrib/anser/anser_test +top_builddir = ../../.. +include $(top_builddir)/src/Makefile.global +include $(top_srcdir)/contrib/contrib-global.mk +endif diff --git a/gpcontrib/anser/anser_test--1.0.sql b/gpcontrib/anser/anser_test/anser_test--1.0.sql similarity index 80% rename from gpcontrib/anser/anser_test--1.0.sql rename to gpcontrib/anser/anser_test/anser_test--1.0.sql index 0f1c02560ca..42e75341ed9 100644 --- a/gpcontrib/anser/anser_test--1.0.sql +++ b/gpcontrib/anser/anser_test/anser_test--1.0.sql @@ -91,6 +91,8 @@ LANGUAGE C STRICT; CREATE FUNCTION anser_test_push_crc( payload_type "char", condition_id int4, + session_id int4, + command_count int4, flags int4, condition_key text, body bytea) @@ -129,3 +131,25 @@ CREATE FUNCTION anser_test_pushdown_target( RETURNS record AS 'MODULE_PATHNAME' LANGUAGE C STRICT; + +-- How many producers a slice runs, which is what a channel waits for. +-- FillSliceGangInfo() has one formula per gang type, so `gang` selects it: +-- 'unallocated', 'entrydb', 'singleton', 'reader' or 'writer'. `direct` is +-- the direct-dispatch content list, or NULL for none. `slice_index` lets a +-- caller ask about a slice outside the array. +CREATE FUNCTION anser_test_slice_producers( + gang text, + numsegments int4, + parallel_workers int4, + direct int4[], + slice_index int4) +RETURNS int4 +AS 'MODULE_PATHNAME' +LANGUAGE C CALLED ON NULL INPUT; + +-- Withdrawals the coordinator has honoured in this backend, so a test can see +-- that a consumer which ran out of time actually said so. +CREATE FUNCTION anser_test_unsubscribes() +RETURNS int8 +AS 'MODULE_PATHNAME' +LANGUAGE C STRICT; diff --git a/gpcontrib/anser/src/anser_test.c b/gpcontrib/anser/anser_test/anser_test.c similarity index 88% rename from gpcontrib/anser/src/anser_test.c rename to gpcontrib/anser/anser_test/anser_test.c index 4ef17765d89..2c2dfe8e9c1 100644 --- a/gpcontrib/anser/src/anser_test.c +++ b/gpcontrib/anser/anser_test/anser_test.c @@ -21,7 +21,7 @@ * SQL-callable test helpers for the Anser subsystem. * * IDENTIFICATION - * gpcontrib/anser/src/anser_test.c + * gpcontrib/anser/anser_test/anser_test.c * *------------------------------------------------------------------------- */ @@ -46,6 +46,12 @@ #include "utils/builtins.h" #include "varatt.h" +/* + * Its own library, so its own magic block: this used to be compiled into + * anser.so and covered by the one in anserinit.c. + */ +PG_MODULE_MAGIC; + /* * Bloom sizing used by the test helpers. bloom_create floors every filter at * 1 MB, so these are the smallest filters we can build; producer and consumer @@ -96,8 +102,8 @@ anser_test_bloom_roundtrip(PG_FUNCTION_ARGS) PG_RETURN_BOOL(false); roundtrip = AnserBloomDeserializePart(payload, payload_len, - 32, 1024 * 1024, seed, - &part_index, &total_parts); + ANSER_TEST_ELEMS, ANSER_TEST_MAX_PAYLOAD, + seed, &part_index, &total_parts); if (roundtrip == NULL) PG_RETURN_BOOL(false); @@ -110,10 +116,12 @@ anser_test_bloom_roundtrip(PG_FUNCTION_ARGS) /* * In-place fold: folding an equally-sized part into a merged part is a bitwise - * OR of the bitset plus a fold-count bump, mutating the buffer without realloc. - * This is the coordinator's only combine path: the first part is stored - * verbatim, every later part folds in here. A differently-sized part is - * rejected and leaves the accumulator untouched. + * OR of the bitset, mutating the buffer without realloc. This is the + * coordinator's only combine path: the first part is stored verbatim, every + * later part folds in here. The header is not touched -- total_parts names the + * channel's producer count, which every part agrees on and folding does not + * change. A differently-sized part is rejected and leaves the accumulator + * untouched. */ Datum anser_test_bloom_fold_inplace(PG_FUNCTION_ARGS) @@ -164,13 +172,14 @@ anser_test_bloom_fold_inplace(PG_FUNCTION_ARGS) same_ok = AnserBloomFoldPartInPlace(acc, acc_len, part, part_len); merged = same_ok ? - AnserBloomDeserializePart(acc, acc_len, 32, 1024 * 1024, seed, + AnserBloomDeserializePart(acc, acc_len, ANSER_TEST_ELEMS, + ANSER_TEST_MAX_PAYLOAD, seed, &part_index, &total_parts) : NULL; same_ok = same_ok && acc_len == acc_size && /* size unchanged, folded in place */ merged != NULL && part_index == 0 && - total_parts == 2 && /* one more part folded */ + total_parts == 1 && /* the fold leaves the header alone */ !bloom_lacks_element(merged, (unsigned char *) &left_value, sizeof(Datum)) && !bloom_lacks_element(merged, (unsigned char *) &right_value, @@ -755,12 +764,15 @@ anser_test_push_crc(PG_FUNCTION_ARGS) { char payload_type = PG_GETARG_CHAR(0); int32 condition_id = PG_GETARG_INT32(1); - int32 flags = PG_GETARG_INT32(2); - char *condition_key = text_to_cstring(PG_GETARG_TEXT_PP(3)); - bytea *body = PG_GETARG_BYTEA_PP(4); + int32 session_id = PG_GETARG_INT32(2); + int32 command_count = PG_GETARG_INT32(3); + int32 flags = PG_GETARG_INT32(4); + char *condition_key = text_to_cstring(PG_GETARG_TEXT_PP(5)); + bytea *body = PG_GETARG_BYTEA_PP(6); PG_RETURN_INT64((int64) (uint32) AnserWirePushCrc(payload_type, (uint32) condition_id, + (uint32) session_id, (uint32) command_count, (uint32) flags, condition_key, (int) strlen(condition_key), VARDATA_ANY(body), @@ -963,3 +975,96 @@ anser_test_pushdown_target(PG_FUNCTION_ARGS) PG_RETURN_DATUM(HeapTupleGetDatum(heap_form_tuple(tupdesc, values, nulls))); } + +/* + * --------------------------------------------------------------------------- + * How many producers a slice runs + * --------------------------------------------------------------------------- + */ + +PG_FUNCTION_INFO_V1(anser_test_slice_producers); + +/* + * AnserSliceProducers() over a hand-built PlanSlice. + * + * The count is the channel's completion criterion, and FillSliceGangInfo gives + * a different answer per gang type -- so the cases below are the four formulas + * it uses, plus the boundaries where the slice cannot be identified at all. + * Nothing is executed: the function reads the slice array and nothing else. + * + * `gang` names the gang type; `direct` is the direct-dispatch content list, + * or NULL for no direct dispatch. `slice_index` is what to ask about, so a + * caller can also probe outside the array. + */ +Datum +anser_test_slice_producers(PG_FUNCTION_ARGS) +{ + char *gang = text_to_cstring(PG_GETARG_TEXT_PP(0)); + int32 numsegments = PG_GETARG_INT32(1); + int32 parallel_workers = PG_GETARG_INT32(2); + int32 slice_index = PG_GETARG_INT32(4); + PlannedStmt *stmt = makeNode(PlannedStmt); + PlanSlice slice; + + MemSet(&slice, 0, sizeof(slice)); + + if (strcmp(gang, "unallocated") == 0) + slice.gangType = GANGTYPE_UNALLOCATED; + else if (strcmp(gang, "entrydb") == 0) + slice.gangType = GANGTYPE_ENTRYDB_READER; + else if (strcmp(gang, "singleton") == 0) + slice.gangType = GANGTYPE_SINGLETON_READER; + else if (strcmp(gang, "reader") == 0) + slice.gangType = GANGTYPE_PRIMARY_READER; + else if (strcmp(gang, "writer") == 0) + slice.gangType = GANGTYPE_PRIMARY_WRITER; + else + elog(ERROR, "anser_test: unknown gang type \"%s\"", gang); + + slice.numsegments = numsegments; + slice.parallel_workers = parallel_workers; + + if (!PG_ARGISNULL(3)) + { + ArrayType *direct = PG_GETARG_ARRAYTYPE_P(3); + Datum *elems; + bool *nulls; + int nelems; + int i; + + deconstruct_array(direct, INT4OID, sizeof(int32), true, TYPALIGN_INT, + &elems, &nulls, &nelems); + + slice.directDispatch.isDirectDispatch = true; + for (i = 0; i < nelems; i++) + { + if (nulls[i]) + elog(ERROR, "anser_test: direct dispatch list may not contain NULLs"); + slice.directDispatch.contentIds = + lappend_int(slice.directDispatch.contentIds, + DatumGetInt32(elems[i])); + } + } + + stmt->numSlices = 1; + stmt->slices = &slice; + + PG_RETURN_INT32(AnserSliceProducers(stmt, slice_index)); +} + +PG_FUNCTION_INFO_V1(anser_test_unsubscribes); + +/* + * Withdrawals the coordinator has honoured in this backend. + * + * A consumer that runs out of time says so, and the coordinator drops it from + * the channel rather than delivering 1-64 MB to a process that has moved on. + * Nothing else in the system records that it happened: the channel is gone by + * the time the query returns, and a filter that simply arrived late looks the + * same from SQL. A test reads the difference across a query. + */ +Datum +anser_test_unsubscribes(PG_FUNCTION_ARGS) +{ + PG_RETURN_INT64(AnserDispatchUnsubscribeCount()); +} diff --git a/gpcontrib/anser/anser_test.control b/gpcontrib/anser/anser_test/anser_test.control similarity index 95% rename from gpcontrib/anser/anser_test.control rename to gpcontrib/anser/anser_test/anser_test.control index 8a4f941a49b..e4d7a9e5ac5 100644 --- a/gpcontrib/anser/anser_test.control +++ b/gpcontrib/anser/anser_test/anser_test.control @@ -18,5 +18,5 @@ # anser_test extension comment = 'test helpers for the anser extension' default_version = '1.0' -module_pathname = '$libdir/anser' +module_pathname = '$libdir/anser_test' relocatable = true diff --git a/gpcontrib/anser/expected/anser_faults.out b/gpcontrib/anser/expected/anser_faults.out new file mode 100644 index 00000000000..05df2ab394b --- /dev/null +++ b/gpcontrib/anser/expected/anser_faults.out @@ -0,0 +1,284 @@ +-- Anser runtime bloom filter: the cases that need gp_inject_fault. +-- +-- Kept apart from anser_runtime_filter so that a build configured with +-- --disable-debug-extensions loses only these. gp_inject_fault is built only +-- with the debug extensions, and the regression driver excludes any file that +-- calls one -- see faultinjector_prep in src/Makefile.global.in -- so whatever +-- shares a file with a fault is skipped along with it. Everything that can be +-- asked for with data alone therefore stays in the other file. +-- +-- Requires the cluster to run with anser.enable=on, like the rest of the suite. +-- +-- The extension may or may not already be there, and saying so is not part of +-- what is being tested; it is never dropped, for the same reason -- it can +-- predate this test, and other tests in the same database rely on it. +-- start_ignore +CREATE EXTENSION IF NOT EXISTS gp_inject_fault; +NOTICE: extension "gp_inject_fault" already exists, skipping +-- end_ignore +-- Deterministic plan shape: a hash join under the Postgres planner, as in +-- anser_runtime_filter. A fresh session inherits none of its settings. +SET enable_nestloop = off; +SET enable_mergejoin = off; +SET optimizer = off; +-- nextval guard: the QD sends two kinds of message down a QE's dispatch +-- connection -- the nextval reply and a sideband message -- and each has its +-- own reader. A reader that meets the other's message has to skip it whole: +-- taking the type byte and erroring leaves the body in the stream, and the +-- session then dies somewhere else with "protocol synchronization was lost". +-- +-- In the field this needs an Anser consumer to give up on a filter the +-- coordinator goes on to deliver anyway, which is a race and cannot be asked +-- for from SQL. The fault puts a sideband message in front of one nextval +-- reply instead, so the ordering is certain rather than likely; the +-- three-argument form is occurrence 1..1, so it fires once. +-- +-- Nothing here is Anser's -- the fault supplies the message and the fix is in +-- cdb_sequence_nextval_qe -- but Anser is why a second message type exists on +-- that connection at all, so the test lives with it. +-- +CREATE SEQUENCE anser_rf_seq; +CREATE TABLE anser_rf_seq_t (id int) DISTRIBUTED BY (id); +INSERT INTO anser_rf_seq_t SELECT g FROM generate_series(1, 10) g; +SELECT gp_inject_fault('nextval_sideband_before_response', 'skip', dbid) + FROM gp_segment_configuration WHERE role = 'p' AND content = -1; + gp_inject_fault +----------------- + Success: +(1 row) + +SELECT count(nextval('anser_rf_seq')) AS seq_rows FROM anser_rf_seq_t; + seq_rows +---------- + 10 +(1 row) + +SELECT gp_inject_fault('nextval_sideband_before_response', 'reset', dbid) + FROM gp_segment_configuration WHERE role = 'p' AND content = -1; + gp_inject_fault +----------------- + Success: +(1 row) + +DROP SEQUENCE anser_rf_seq; +DROP TABLE anser_rf_seq_t; +-- Delivery to a consumer that stopped reading must not block the coordinator. +-- +-- A merged filter is 1-64 MB and the dispatch connections are blocking, so +-- pushing to a QE that is no longer reading used to wait in pqWait() with no +-- deadline and no CHECK_FOR_INTERRUPTS -- inside the dispatcher loop, which +-- then served nobody. Neither cancel nor statement_timeout could end it. +-- +-- The ordering cannot be arranged with data: the channel completes when the +-- last part lands, which is normally while the consumers are still waiting. +-- Holding the delivery back is what lets every consumer reach its deadline +-- and move on to its probe side -- and it is while they are busy there that +-- the coordinator finally writes to all of them. +-- +-- Note this one hangs rather than fails if the fix is reverted, for the same +-- reason the bug was unkillable. It is here because the alternative is a +-- regression nobody notices until a cluster wedges. +SET anser.timeout_ms = 1; +CREATE TABLE anser_rf_push_build (id int, pad text) DISTRIBUTED BY (pad); +CREATE TABLE anser_rf_push_probe (id int) DISTRIBUTED BY (id); +INSERT INTO anser_rf_push_build SELECT g, 'b' || g FROM generate_series(1, 1000) g; +INSERT INTO anser_rf_push_probe SELECT g % 1000 + 1 FROM generate_series(1, 1000000) g; +ANALYZE anser_rf_push_build; +ANALYZE anser_rf_push_probe; +-- One segment publishes a second late; every other consumer gives up at 1 ms. +SELECT gp_inject_fault('anser_before_deliver', 'sleep', '', '', '', 1, 1, 1, dbid) + FROM gp_segment_configuration WHERE role = 'p' AND content = -1; + gp_inject_fault +----------------- + Success: +(1 row) + +SET anser.runtime_filter = on; +SELECT count(*) AS push_rows + FROM anser_rf_push_probe p JOIN anser_rf_push_build b ON p.id = b.id; + push_rows +----------- + 1000000 +(1 row) + +SELECT gp_inject_fault('anser_before_deliver', 'reset', dbid) + FROM gp_segment_configuration WHERE role = 'p' AND content = -1; + gp_inject_fault +----------------- + Success: +(1 row) + +DROP TABLE anser_rf_push_build, anser_rf_push_probe; +-- A consumer that runs out of time withdraws, so the coordinator stops +-- counting it as a subscriber. Without that it would keep delivering to a +-- process that has moved on -- which is what the push above has to survive, +-- and what this keeps from quietly regressing into. +-- +-- The withdrawal leaves no trace a query can see: the channel is gone by the +-- time the query returns, and a filter that merely arrived late looks the +-- same. So the coordinator counts them, and this reads the difference. +CREATE TABLE anser_rf_unsub_build (id int, pad text) DISTRIBUTED BY (pad); +CREATE TABLE anser_rf_unsub_probe (id int) DISTRIBUTED BY (id); +INSERT INTO anser_rf_unsub_build SELECT g, 'b' || g FROM generate_series(1, 1000) g; +INSERT INTO anser_rf_unsub_probe SELECT g % 1000 + 1 FROM generate_series(1, 200000) g; +ANALYZE anser_rf_unsub_build; +ANALYZE anser_rf_unsub_probe; +-- start_ignore +CREATE EXTENSION IF NOT EXISTS anser_test; +NOTICE: extension "anser_test" already exists, skipping +-- end_ignore +SELECT anser_test_unsubscribes() AS unsub_before \gset +SELECT gp_inject_fault('anser_before_deliver', 'sleep', '', '', '', 1, 1, 1, dbid) + FROM gp_segment_configuration WHERE role = 'p' AND content = -1; + gp_inject_fault +----------------- + Success: +(1 row) + +SELECT count(*) AS unsub_rows + FROM anser_rf_unsub_probe p JOIN anser_rf_unsub_build b ON p.id = b.id; + unsub_rows +------------ + 200000 +(1 row) + +SELECT gp_inject_fault('anser_before_deliver', 'reset', dbid) + FROM gp_segment_configuration WHERE role = 'p' AND content = -1; + gp_inject_fault +----------------- + Success: +(1 row) + +SELECT anser_test_unsubscribes() > :unsub_before AS consumers_withdrew; + consumers_withdrew +-------------------- + t +(1 row) + +DROP EXTENSION anser_test; +DROP TABLE anser_rf_unsub_build, anser_rf_unsub_probe; +-- A partially delivered filter must not wedge the next command. +-- +-- The push above is torn when the filter is bigger than the connection +-- buffers: the consumers give up and finish, the coordinator's non-blocking +-- write leaves the tail queued, and the poll loop stops flushing once the +-- peer's command completes. The next dispatch on the same gang would then +-- overwrite the queued bytes (PQsendGpQuery_shared), leaving the QE waiting +-- forever for a body that never comes -- the session only ended with the +-- cluster. ExecutorEnd now completes the flush, or poisons the connection. +-- +-- The filter is forced to 64 MB (anser.debug_min_filter_bytes) so the push +-- cannot fit the buffers even on loopback. This test hangs rather than fails +-- if the fix is reverted, the same warning as the push test above. +SET anser.timeout_ms = 100; +SET anser.debug_min_filter_bytes = 67108864; +CREATE TABLE anser_rf_stale_build (id int, pad text) DISTRIBUTED BY (pad); +CREATE TABLE anser_rf_stale_probe (id int) DISTRIBUTED BY (id); +INSERT INTO anser_rf_stale_build SELECT g, 'b' || g FROM generate_series(1, 1000) g; +INSERT INTO anser_rf_stale_probe SELECT g % 1000 + 1 FROM generate_series(1, 1000000) g; +ANALYZE anser_rf_stale_build; +ANALYZE anser_rf_stale_probe; +-- Hold the delivery for 1 s so every consumer gives up at 100 ms and the +-- 64 MB filter is pushed to connections nobody is reading. +SELECT gp_inject_fault('anser_before_deliver', 'sleep', '', '', '', 1, 1, 1, dbid) + FROM gp_segment_configuration WHERE role = 'p' AND content = -1; + gp_inject_fault +----------------- + Success: +(1 row) + +SET anser.runtime_filter = on; +SELECT count(*) AS stale_rows + FROM anser_rf_stale_probe p JOIN anser_rf_stale_build b ON p.id = b.id; + stale_rows +------------ + 1000000 +(1 row) + +SELECT gp_inject_fault('anser_before_deliver', 'reset', dbid) + FROM gp_segment_configuration WHERE role = 'p' AND content = -1; + gp_inject_fault +----------------- + Success: +(1 row) + +-- The torn delivery is completed (or its connection poisoned) at ExecutorEnd, +-- so the next command on the same gang must answer. +SELECT 'next command' AS marker, count(*) AS build_rows FROM anser_rf_stale_build; + marker | build_rows +--------------+------------ + next command | 1000 +(1 row) + +DROP TABLE anser_rf_stale_build, anser_rf_stale_probe; +RESET anser.debug_min_filter_bytes; +-- A filter parked by one statement must not be claimed by the next. +-- +-- A consumer that waits while a delivery for another channel arrives parks it +-- in the inbox. If the statement then errors inside a savepoint, there is no +-- ExecutorEnd for it (a failed portal skips it), so the parked entry used to +-- survive -- and the next statement, whose condition keys restart at the same +-- values, claimed it: a filter built from the old build side pruned the new +-- one. Deliveries now carry the session id and command count and a consumer +-- matches on them exactly, and the subtransaction abort drops the inbox. +-- +-- The LEFT JOIN pins j2 as the build side, so its consumer waits first: it +-- gives up at 700 ms while the coordinator sleeps for 1 s, its late delivery +-- is then read -- and parked -- by j1's consumer, and the division errors +-- before anything claims it. The second run re-executes the same plan shape +-- with a build side disjoint from the parked filter: claiming it would drop +-- every joinable row, so count(j2.id) must be 1000, not 0. +SET anser.timeout_ms = 700; +CREATE TABLE anser_sf_b1 (id int, pad text) DISTRIBUTED BY (pad); +CREATE TABLE anser_sf_p1 (id int) DISTRIBUTED BY (id); +CREATE TABLE anser_sf_b2 (id int, pad text) DISTRIBUTED BY (pad); +CREATE TABLE anser_sf_p2 (id int) DISTRIBUTED BY (id); +INSERT INTO anser_sf_b1 SELECT g, 'b' || g FROM generate_series(1, 2000) g; +INSERT INTO anser_sf_p1 SELECT g FROM generate_series(1, 2000) g; +INSERT INTO anser_sf_b2 SELECT g, 'b' || g FROM generate_series(1, 1000) g; +INSERT INTO anser_sf_p2 SELECT g FROM generate_series(1, 2000) g; +ANALYZE anser_sf_b1; +ANALYZE anser_sf_p1; +ANALYZE anser_sf_b2; +ANALYZE anser_sf_p2; +SELECT gp_inject_fault('anser_before_deliver', 'sleep', '', '', '', 1, 1, 1, dbid) + FROM gp_segment_configuration WHERE role = 'p' AND content = -1; + gp_inject_fault +----------------- + Success: +(1 row) + +SET anser.runtime_filter = on; +BEGIN; +SAVEPOINT anser_sf; +SELECT j1.id / (j1.id - j1.id) AS boom +FROM (SELECT p1.id FROM anser_sf_p1 p1 JOIN anser_sf_b1 b1 ON p1.id = b1.id) j1 +LEFT JOIN (SELECT p2.id FROM anser_sf_p2 p2 JOIN anser_sf_b2 b2 ON p2.id = b2.id) j2 + ON j1.id = j2.id; +ERROR: division by zero +ROLLBACK TO SAVEPOINT anser_sf; +DELETE FROM anser_sf_b2; +INSERT INTO anser_sf_b2 SELECT 1000 + g, 'b' || g FROM generate_series(1, 1000) g; +SELECT count(j2.id) AS reused_rows +FROM (SELECT p1.id FROM anser_sf_p1 p1 JOIN anser_sf_b1 b1 ON p1.id = b1.id) j1 +LEFT JOIN (SELECT p2.id FROM anser_sf_p2 p2 JOIN anser_sf_b2 b2 ON p2.id = b2.id) j2 + ON j1.id = j2.id; + reused_rows +------------- + 1000 +(1 row) + +COMMIT; +SELECT gp_inject_fault('anser_before_deliver', 'reset', dbid) + FROM gp_segment_configuration WHERE role = 'p' AND content = -1; + gp_inject_fault +----------------- + Success: +(1 row) + +DROP TABLE anser_sf_b1, anser_sf_p1, anser_sf_b2, anser_sf_p2; +RESET anser.runtime_filter; +RESET anser.timeout_ms; +RESET optimizer; +RESET enable_nestloop; +RESET enable_mergejoin; diff --git a/gpcontrib/anser/expected/anser_multijoin.out b/gpcontrib/anser/expected/anser_multijoin.out index 0740fef3f15..37cbfe7453a 100644 --- a/gpcontrib/anser/expected/anser_multijoin.out +++ b/gpcontrib/anser/expected/anser_multijoin.out @@ -26,10 +26,10 @@ SET enable_nestloop = off; SET enable_mergejoin = off; -- A regress test must not inherit the production deadline. anser.timeout_ms --- defaults to 100 s because delivery is serial and a wide slice needs that --- long to be served; a test that waits it out has turned a delivery failure --- into a slow pass. Pinned short here, so a filter that never arrives fails --- the run instead of hiding in the clock. +-- defaults to 10 s, which is a budget for an exchange that has gone wrong; a +-- test that waits it out has turned a delivery failure into a slow pass. +-- Pinned shorter here, so a filter that never arrives fails the run instead of +-- hiding in the clock. SET anser.timeout_ms = 5000; -- own is what the previous table joins to, nxt is what this table joins to the -- next one with. They hold the same value, which keeps the chain 1:1 and the @@ -78,6 +78,14 @@ ANALYZE anser_mj_t8; ANALYZE anser_mj_t9; ANALYZE anser_mj_t10; ANALYZE anser_mj_t11; +-- The same shape, minus every tenth key. The outer-join case below needs a +-- right side that does not cover its left, and none of the tables above can be +-- it: they are joined 1:1 on purpose, and thinning one would break the "1000 +-- rows summing to 500500 whatever the join count" invariant the chains rest on. +CREATE TABLE anser_mj_gap (own int, nxt int, payload text) DISTRIBUTED BY (own); +INSERT INTO anser_mj_gap SELECT g, g, 'gap_' || g + FROM generate_series(1, 3000) g WHERE g % 10 <> 0; +ANALYZE anser_mj_gap; -- Runs one chain twice, with the filter on and off, and reports whether the two -- agree. The checksum is what makes the comparison mean something: equal row -- counts would also come out of two different sets of rows, and a filter that @@ -128,28 +136,47 @@ SELECT joins, nrows, own_sum, agrees SET optimizer = off; -- A chain whose middle join is an outer join, and whose next join reads the --- nullable side's key. The joined result there holds NULLs the base relation --- does not, and a NULL must not be taken for a key the filter is missing. +-- nullable side's key -- so the joined result carries NULLs the base relation +-- does not. +-- +-- Two things make that true of the plan and not just of the text. The right +-- side has to leave keys unmatched, which is what anser_mj_gap is for. And the +-- outer join has to survive planning: the join above it is strict in c.nxt, so +-- reduce_outer_joins() would otherwise turn this LEFT JOIN into an inner one +-- before any path is considered. OFFSET 0 keeps the subquery from being pulled +-- up, which keeps that qual a level away. +-- +-- No filter is injected here, and none can be. The price of keeping the outer +-- join is a SubqueryScan, and AnserResolveKeyScan descends through Hash, Motion +-- and HashJoin only. Nor is there a way around it: a NULL-keyed row reaches +-- the output only under an outer join, an outer join survives only behind a +-- subquery boundary, and Anser does not inject across one. So a filter meeting +-- a NULL key is not something this suite can arrange -- what is checked here is +-- that such a plan returns the same rows either way. SET anser.runtime_filter = on; CREATE TEMP TABLE anser_mj_lj_on AS - SELECT a.own AS aown, c.own AS cown, d.own AS down - FROM anser_mj_t1 a - JOIN anser_mj_t2 b ON a.nxt = b.own - LEFT JOIN anser_mj_t3 c ON b.nxt = c.own - JOIN anser_mj_t4 d ON c.nxt = d.own + SELECT s.aown, s.cown, d.own AS down + FROM (SELECT a.own AS aown, c.own AS cown, c.nxt AS cnxt + FROM anser_mj_t1 a + JOIN anser_mj_t2 b ON a.nxt = b.own + LEFT JOIN anser_mj_gap c ON b.nxt = c.own + OFFSET 0) s + JOIN anser_mj_t4 d ON s.cnxt = d.own DISTRIBUTED BY (aown); SET anser.runtime_filter = off; CREATE TEMP TABLE anser_mj_lj_off AS - SELECT a.own AS aown, c.own AS cown, d.own AS down - FROM anser_mj_t1 a - JOIN anser_mj_t2 b ON a.nxt = b.own - LEFT JOIN anser_mj_t3 c ON b.nxt = c.own - JOIN anser_mj_t4 d ON c.nxt = d.own + SELECT s.aown, s.cown, d.own AS down + FROM (SELECT a.own AS aown, c.own AS cown, c.nxt AS cnxt + FROM anser_mj_t1 a + JOIN anser_mj_t2 b ON a.nxt = b.own + LEFT JOIN anser_mj_gap c ON b.nxt = c.own + OFFSET 0) s + JOIN anser_mj_t4 d ON s.cnxt = d.own DISTRIBUTED BY (aown); SELECT count(*) AS lj_rows FROM anser_mj_lj_on; lj_rows --------- - 1000 + 900 (1 row) SELECT count(*) AS lj_only_on @@ -251,8 +278,9 @@ DROP FUNCTION anser_mj_pruned(text); DROP FUNCTION anser_mj_check(int); DROP TABLE anser_mj_t1, anser_mj_t2, anser_mj_t3, anser_mj_t4, anser_mj_t5, anser_mj_t6, anser_mj_t7, anser_mj_t8, anser_mj_t9, anser_mj_t10, - anser_mj_t11; + anser_mj_t11, anser_mj_gap; RESET anser.runtime_filter; +RESET anser.timeout_ms; RESET optimizer; RESET enable_nestloop; RESET enable_mergejoin; diff --git a/gpcontrib/anser/expected/anser_runtime_filter.out b/gpcontrib/anser/expected/anser_runtime_filter.out index 11603d91ddf..2cd24b12ff1 100644 --- a/gpcontrib/anser/expected/anser_runtime_filter.out +++ b/gpcontrib/anser/expected/anser_runtime_filter.out @@ -9,10 +9,10 @@ SET enable_nestloop = off; SET enable_mergejoin = off; -- A regress test must not inherit the production deadline. anser.timeout_ms --- defaults to 100 s because delivery is serial and a wide slice needs that --- long to be served; a test that waits it out has turned a delivery failure --- into a slow pass. Pinned short here, so a filter that never arrives fails --- the run instead of hiding in the clock. +-- defaults to 10 s, which is a budget for an exchange that has gone wrong; a +-- test that waits it out has turned a delivery failure into a slow pass. +-- Pinned shorter here, so a filter that never arrives fails the run instead of +-- hiding in the clock. SET anser.timeout_ms = 5000; -- build is the smaller (hashed / preserved) side, distributed so the join key -- must be redistributed; probe is the larger side distributed by the join key. @@ -144,6 +144,24 @@ SELECT count(*) AS only_off_push (1 row) RESET gp_enable_runtime_filter_pushdown; +-- Counts the Anser nodes a plan carries. Comparing a filtered count with an +-- unfiltered one says nothing when no filter was injected: both runs then do +-- the same thing and agree. Several blocks below ask this as well, which is +-- why it is defined here rather than beside its first caller. +CREATE FUNCTION anser_rf_nodes(q text) RETURNS bigint +LANGUAGE plpgsql AS $$ +DECLARE + line text; + nodes bigint := 0; +BEGIN + FOR line IN EXECUTE 'EXPLAIN (COSTS OFF) ' || q LOOP + IF line ~ 'Anser Bloom' THEN + nodes := nodes + 1; + END IF; + END LOOP; + RETURN nodes; +END; +$$; -- Pushdown where the key's table attribute number and its position in the -- scan's output targetlist disagree. The case above cannot see the -- difference: anser_rf_probe's key is its first column and also the first @@ -153,9 +171,9 @@ RESET gp_enable_runtime_filter_pushdown; -- built on `id` -- and since no payload is ever a build key, every row is -- discarded. Both counts must be 200. SET optimizer = on; -CREATE TABLE anser_rf_proj_build (id int) DISTRIBUTED BY (id); +CREATE TABLE anser_rf_proj_build (id int, pad text) DISTRIBUTED BY (pad); CREATE TABLE anser_rf_proj_probe (payload int, id int) DISTRIBUTED BY (id); -INSERT INTO anser_rf_proj_build SELECT g FROM generate_series(1, 200) g; +INSERT INTO anser_rf_proj_build SELECT g, 'b' || g FROM generate_series(1, 200) g; INSERT INTO anser_rf_proj_probe SELECT -g, g FROM generate_series(1, 2000) g; ANALYZE anser_rf_proj_build; ANALYZE anser_rf_proj_probe; @@ -176,6 +194,13 @@ SELECT count(p.id) AS proj_push_on 200 (1 row) +SELECT anser_rf_nodes('SELECT count(p.id) + FROM anser_rf_proj_build b LEFT JOIN anser_rf_proj_probe p ON b.id = p.id') > 0 AS proj_injected; + proj_injected +--------------- + t +(1 row) + -- The alias is not the column. This subquery renames `payload` to the name -- `id` and the real `id` to `id2`, and joins on id2 -- so resolving the key by -- name would land on `payload`, the one column guaranteed never to match a @@ -202,13 +227,28 @@ SELECT count(p.id2) AS rename_push_on 200 (1 row) --- A computed key has no table attribute number to push down: the scan would --- have to evaluate `id * 2`, which PassByBloomFilter cannot do. Note the join --- clause itself still looks like a plain Var to the planner (it references the --- scan's output position), so the key resolves and the nodes are injected -- --- it is the scan's own targetlist entry that is an expression. The consumer --- must decline the pushdown and probe the projected slot itself. Only the --- 100 even build ids match, so both counts must be 100. +SELECT anser_rf_nodes('SELECT count(p.id2) + FROM anser_rf_proj_build b + LEFT JOIN (SELECT payload AS id, id AS id2 FROM anser_rf_proj_probe) p + ON b.id = p.id2') > 0 AS rename_injected; + rename_injected +----------------- + t +(1 row) + +-- A computed key, which Anser declines outright. The subquery has no ORDER BY, +-- LIMIT or OFFSET, so is_simple_subquery() pulls it up and the hash clause +-- becomes `b.id = p.id * 2`; anser_hashjoin_keys takes two plain Vars and +-- nothing else, so no nodes are injected at all. +-- +-- The comment here used to claim the opposite -- that the clause still reads as +-- a Var into the scan's output and only the pushdown is declined. That would +-- need the subquery to survive as a scan of its own, which asking for it +-- (OFFSET 0) does not buy either: AnserResolveKeyScan does not descend through +-- a SubqueryScan. The decision for a computed column is covered directly by +-- anser_test_pushdown_accepts; what is left to check here is that declining is +-- clean, so expr_injected is f and both counts are 100 -- only the 100 even +-- build ids match. SET anser.runtime_filter = off; SELECT count(p.k) AS expr_push_off FROM anser_rf_proj_build b @@ -227,6 +267,14 @@ SELECT count(p.k) AS expr_push_on 100 (1 row) +SELECT anser_rf_nodes('SELECT count(p.k) + FROM anser_rf_proj_build b + LEFT JOIN (SELECT id * 2 AS k FROM anser_rf_proj_probe) p ON b.id = p.k') > 0 AS expr_injected; + expr_injected +--------------- + f +(1 row) + RESET gp_enable_runtime_filter_pushdown; DROP TABLE anser_rf_proj_build, anser_rf_proj_probe; SET optimizer = off; @@ -283,8 +331,214 @@ SELECT 'an inner statement dropped the running query''s filter' AS problem --------- (0 rows) +-- Narrow build slices must still publish. part_index is only diagnostic, so +-- a slice narrower than the cluster must not reject the only producer there +-- is: direct dispatch to a non-zero segment (segindex >= total_parts) or the +-- entry-DB (segindex = -1) both used to leave the channel a part short and +-- every consumer waiting out anser.timeout_ms. The block also asserts the +-- count, but the load-bearing assertion is that rows were pruned at all. +SET anser.timeout_ms = 100; +CREATE TABLE anser_rf_dd_build (id int, dk text, pad text) DISTRIBUTED BY (dk); +CREATE TABLE anser_rf_dd_probe (id int) DISTRIBUTED BY (id); +INSERT INTO anser_rf_dd_build SELECT g, 'k' || (g % 6), 'b' || g FROM generate_series(1, 1000) g; +INSERT INTO anser_rf_dd_probe SELECT g % 1000 + 1 FROM generate_series(1, 100000) g; +ANALYZE anser_rf_dd_build; +ANALYZE anser_rf_dd_probe; +-- Check every distribution key, not a chosen one: on any cluster with more +-- than one segment at least one of them lands with segindex >= 1, which is +-- the shape that used to stall -- the direct-dispatched producer was refused +-- (segindex >= total_parts) and every consumer waited out anser.timeout_ms. +-- The discriminating observable is that rows were pruned at all; result +-- counts are identical either way, since a broken exchange fails open. +SET anser.runtime_filter = on; +SELECT anser_rf_pruned('SELECT count(*) FROM anser_rf_dd_probe p JOIN anser_rf_dd_build b ON p.id = b.id WHERE b.dk = ''k0''') > 0 AS k0_pruned; + k0_pruned +----------- + t +(1 row) + +SELECT anser_rf_pruned('SELECT count(*) FROM anser_rf_dd_probe p JOIN anser_rf_dd_build b ON p.id = b.id WHERE b.dk = ''k1''') > 0 AS k1_pruned; + k1_pruned +----------- + t +(1 row) + +SELECT anser_rf_pruned('SELECT count(*) FROM anser_rf_dd_probe p JOIN anser_rf_dd_build b ON p.id = b.id WHERE b.dk = ''k2''') > 0 AS k2_pruned; + k2_pruned +----------- + t +(1 row) + +SELECT anser_rf_pruned('SELECT count(*) FROM anser_rf_dd_probe p JOIN anser_rf_dd_build b ON p.id = b.id WHERE b.dk = ''k3''') > 0 AS k3_pruned; + k3_pruned +----------- + t +(1 row) + +SELECT anser_rf_pruned('SELECT count(*) FROM anser_rf_dd_probe p JOIN anser_rf_dd_build b ON p.id = b.id WHERE b.dk = ''k4''') > 0 AS k4_pruned; + k4_pruned +----------- + t +(1 row) + +SELECT anser_rf_pruned('SELECT count(*) FROM anser_rf_dd_probe p JOIN anser_rf_dd_build b ON p.id = b.id WHERE b.dk = ''k5''') > 0 AS k5_pruned; + k5_pruned +----------- + t +(1 row) + +-- Entry-DB build side: a catalog scan runs on the entry-DB QE, whose +-- segindex is -1 -- 0xFFFFFFFF as a part index, the same rejection. +CREATE TABLE anser_rf_eb_probe (o oid) DISTRIBUTED BY (o); +INSERT INTO anser_rf_eb_probe + SELECT 'anser_rf_eb_probe'::regclass::oid FROM generate_series(1, 10) g + UNION ALL + SELECT 4000000000 + g FROM generate_series(1, 100000) g; +ANALYZE anser_rf_eb_probe; +-- The count uses only the matching rows, so it is exact: the table's own oid +-- exists in pg_class, ten times. +SELECT count(*) AS eb_rows + FROM (SELECT 'anser_rf_eb_probe'::regclass::oid AS o FROM generate_series(1, 10) g) p + JOIN pg_class c ON p.o = c.oid; + eb_rows +--------- + 10 +(1 row) + +SELECT 'entry-DB producer did not publish (consumer failed open)' AS problem + WHERE anser_rf_pruned('SELECT count(*) FROM anser_rf_eb_probe p JOIN pg_class c ON p.o = c.oid') = 0; + problem +--------- +(0 rows) + +DROP TABLE anser_rf_dd_build, anser_rf_dd_probe, anser_rf_eb_probe; +RESET anser.timeout_ms; DROP FUNCTION anser_rf_nested(int); DROP FUNCTION anser_rf_pruned(text); +-- Filter sizing follows the estimate, so exercising a production-size payload +-- with test-size data goes through a debug knob instead: force a 64 MB bitset +-- for a 200-row build side, and the full-size filter has to actually exchange +-- (the serialized part rides the wire to every consumer). +SET anser.debug_min_filter_bytes = 67108864; +SET anser.runtime_filter = on; +EXPLAIN (COSTS OFF) +SELECT b.name, p.payload FROM anser_rf_build b LEFT JOIN anser_rf_probe p ON b.id = p.id; + QUERY PLAN +------------------------------------------------------------------ + Gather Motion 3:1 (slice1; segments: 3) + -> Hash Right Join + Hash Cond: (p.id = b.id) + -> Custom Scan (Anser Bloom Consumer) + Bloom Filter Size: 67108864 bytes + -> Seq Scan on anser_rf_probe p + -> Hash + -> Redistribute Motion 3:3 (slice2; segments: 3) + Hash Key: b.id + -> Custom Scan (Anser Bloom Producer) + Bloom Filter Size: 67108864 bytes + Bloom Filter Stats: memory=65536kB + -> Seq Scan on anser_rf_build b + Optimizer: Postgres query optimizer +(14 rows) + +-- A plain EXPLAIN must not allocate the filter: the coordinator initializes +-- every slice with EXEC_FLAG_EXPLAIN_ONLY, so the producer must skip building +-- the bitset (state=NULL) while the plan still shows the planned size. The +-- debug log makes the decision observable. +SET anser.debug = on; +SET client_min_messages = log; +EXPLAIN (COSTS OFF) +SELECT b.name, p.payload FROM anser_rf_build b LEFT JOIN anser_rf_probe p ON b.id = p.id; +LOG: statement: EXPLAIN (COSTS OFF) +SELECT b.name, p.payload FROM anser_rf_build b LEFT JOIN anser_rf_probe p ON b.id = p.id; +LOG: anser: producer init cond=0 part=0/1 elems=33554432 payload=67108928 state=NULL + QUERY PLAN +------------------------------------------------------------------ + Gather Motion 3:1 (slice1; segments: 3) + -> Hash Right Join + Hash Cond: (p.id = b.id) + -> Custom Scan (Anser Bloom Consumer) + Bloom Filter Size: 67108864 bytes + -> Seq Scan on anser_rf_probe p + -> Hash + -> Redistribute Motion 3:3 (slice2; segments: 3) + Hash Key: b.id + -> Custom Scan (Anser Bloom Producer) + Bloom Filter Size: 67108864 bytes + Bloom Filter Stats: memory=65536kB + -> Seq Scan on anser_rf_build b + Optimizer: Postgres query optimizer +(14 rows) + +RESET client_min_messages; +LOG: statement: RESET client_min_messages; +RESET anser.debug; +-- And the filter must still be correct: 200 build rows, each matching one +-- probe row. +SELECT count(*) AS rows_joined + FROM anser_rf_build b LEFT JOIN anser_rf_probe p ON b.id = p.id; + rows_joined +------------- + 200 +(1 row) + +-- The floor must change the real filter, not just the plan: sizing raises the +-- declared element count to match the forced bitset size, so producer and +-- consumer really build a 64 MB filter instead of displaying one. +CREATE EXTENSION IF NOT EXISTS anser_test; +SELECT injected, total_elems, planned_bytes FROM anser_test_rf_size(200); + injected | total_elems | planned_bytes +----------+-------------+--------------- + t | 33554432 | 67108864 +(1 row) + +DROP EXTENSION anser_test; +-- A LIMIT can finish the query while a consumer is still waiting for its +-- filter: the producer is squelched before it publishes, the consumer must +-- bail out via QueryFinishPending and withdraw its subscription, and the +-- result must match the unfiltered run. +SELECT count(*) AS limited_rows FROM ( + SELECT b.name, p.payload FROM anser_rf_build b LEFT JOIN anser_rf_probe p ON b.id = p.id + LIMIT 10) q; + limited_rows +-------------- + 10 +(1 row) + +-- An open cursor must be undisturbed by statements between its FETCHes: the +-- filter exchange completes inside the FETCH's ExecutorRun window, so a +-- sibling statement's ExecutorEnd sweep has nothing of the cursor's to wipe. +-- The sibling runs the same filtered join, so it really does create and sweep +-- its own channels while the cursor's gang sits suspended. (A wiped exchange +-- would fail open with the same rows, so this guards the behavior, not the +-- timing: that path is a wait, not a wrong answer.) +SET optimizer = off; +SET anser.runtime_filter = on; +BEGIN; +DECLARE anser_rf_cur CURSOR FOR + SELECT b.name, p.payload FROM anser_rf_build b LEFT JOIN anser_rf_probe p ON b.id = p.id + WHERE b.id <= 3 ORDER BY b.name; +FETCH 1 anser_rf_cur; + name | payload +------+--------- + b1 | p1 +(1 row) + +-- The ExecutorEnd of this statement must leave the open cursor's state alone. +SELECT count(*) AS sibling_stmt FROM anser_rf_build b LEFT JOIN anser_rf_probe p ON b.id = p.id; + sibling_stmt +-------------- + 200 +(1 row) + +FETCH ALL anser_rf_cur; + name | payload +------+--------- + b2 | p2 + b3 | p3 +(2 rows) + +COMMIT; DROP TABLE anser_rf_r_push; DROP TABLE anser_rf_r_on, anser_rf_r_off, anser_rf_r_orca; DROP TABLE anser_rf_build, anser_rf_probe; @@ -364,6 +618,49 @@ SELECT count(*) AS only_off (1 row) DROP TABLE anser_rf_z_on, anser_rf_z_off, anser_rf_build_z, anser_rf_probe_z; +-- The exclusion has to hold through a domain. A domain inherits typlen and +-- typbyval from its base type, so it looks by-value, and its OID is not one of +-- the float OIDs the exclusion names -- a check on the declared type alone lets +-- it straight through and -0.0 would stop matching 0.0. Both sides are +-- distributed on their text column so the join needs a Motion and is a +-- candidate for injection; the key type is what has to refuse it. +CREATE DOMAIN anser_rf_f8 AS float8; +CREATE TABLE anser_rf_build_d (id anser_rf_f8, name text) DISTRIBUTED BY (name); +CREATE TABLE anser_rf_probe_d (id anser_rf_f8, payload text) DISTRIBUTED BY (payload); +INSERT INTO anser_rf_build_d VALUES (1, 'b1'), (-0.0, 'bz'); +INSERT INTO anser_rf_probe_d VALUES (1, 'p1'), (0.0, 'pz'), (2, 'p2'); +ANALYZE anser_rf_build_d; +ANALYZE anser_rf_probe_d; +SET anser.runtime_filter = on; +CREATE TEMP TABLE anser_rf_d_on AS + SELECT b.name, p.payload FROM anser_rf_build_d b JOIN anser_rf_probe_d p ON b.id = p.id + DISTRIBUTED BY (name); +SET anser.runtime_filter = off; +CREATE TEMP TABLE anser_rf_d_off AS + SELECT b.name, p.payload FROM anser_rf_build_d b JOIN anser_rf_probe_d p ON b.id = p.id + DISTRIBUTED BY (name); +SELECT count(*) AS rows_on FROM anser_rf_d_on; + rows_on +--------- + 2 +(1 row) + +SELECT count(*) AS only_on + FROM (SELECT * FROM anser_rf_d_on EXCEPT ALL SELECT * FROM anser_rf_d_off) d; + only_on +--------- + 0 +(1 row) + +SELECT count(*) AS only_off + FROM (SELECT * FROM anser_rf_d_off EXCEPT ALL SELECT * FROM anser_rf_d_on) d; + only_off +---------- + 0 +(1 row) + +DROP TABLE anser_rf_d_on, anser_rf_d_off, anser_rf_build_d, anser_rf_probe_d; +DROP DOMAIN anser_rf_f8; -- Parameterized rescan guard: a build side whose rows depend on a parameter an -- enclosing nested loop reassigns yields a different key set per outer tuple, -- and neither node rebuilds on rescan -- the producer stays published, the @@ -376,7 +673,7 @@ DROP TABLE anser_rf_z_on, anser_rf_z_off, anser_rf_build_z, anser_rf_probe_z; -- makes a stale filter visible: with the first group's filter still in place -- the second group matches nothing. CREATE TABLE anser_rf_nl_outer (g int) DISTRIBUTED REPLICATED; -CREATE TABLE anser_rf_nl_build (g int, id int) DISTRIBUTED BY (id); +CREATE TABLE anser_rf_nl_build (g int, id int) DISTRIBUTED BY (g); CREATE TABLE anser_rf_nl_probe (id int) DISTRIBUTED BY (id); INSERT INTO anser_rf_nl_outer VALUES (1), (2); INSERT INTO anser_rf_nl_build SELECT 1, g FROM generate_series(1, 100) g; @@ -385,20 +682,6 @@ INSERT INTO anser_rf_nl_probe SELECT g FROM generate_series(1, 20000) g; ANALYZE anser_rf_nl_outer; ANALYZE anser_rf_nl_build; ANALYZE anser_rf_nl_probe; -CREATE FUNCTION anser_rf_nodes(q text) RETURNS bigint -LANGUAGE plpgsql AS $$ -DECLARE - line text; - nodes bigint := 0; -BEGIN - FOR line IN EXECUTE 'EXPLAIN (COSTS OFF) ' || q LOOP - IF line ~ 'Anser Bloom' THEN - nodes := nodes + 1; - END IF; - END LOOP; - RETURN nodes; -END; -$$; -- Both groups must report 100, with the filter on and off alike, and the plan -- must carry no Anser node at all. SET anser.runtime_filter = off; @@ -448,54 +731,6 @@ SELECT anser_rf_nodes('SELECT count(*) FROM anser_rf_nl_probe p DROP FUNCTION anser_rf_nodes(text); DROP TABLE anser_rf_nl_outer, anser_rf_nl_build, anser_rf_nl_probe; --- nextval guard: the QD sends two kinds of message down a QE's dispatch --- connection -- the nextval reply and a sideband message -- and each has its --- own reader. A reader that meets the other's message has to skip it whole: --- taking the type byte and erroring leaves the body in the stream, and the --- session then dies somewhere else with "protocol synchronization was lost". --- --- In the field this needs an Anser consumer to give up on a filter the --- coordinator goes on to deliver anyway, which is a race and cannot be asked --- for from SQL. The fault puts a sideband message in front of one nextval --- reply instead, so the ordering is certain rather than likely; the --- three-argument form is occurrence 1..1, so it fires once. --- --- Nothing here is Anser's -- the fault supplies the message and the fix is in --- cdb_sequence_nextval_qe -- but Anser is why a second message type exists on --- that connection at all, so the test lives with it. --- --- The extension may or may not already be there, and saying so is not part of --- what is being tested; it is never dropped, for the same reason -- it can --- predate this test, and other tests in the same database rely on it. --- start_ignore -CREATE EXTENSION IF NOT EXISTS gp_inject_fault; -NOTICE: extension "gp_inject_fault" already exists, skipping --- end_ignore -CREATE SEQUENCE anser_rf_seq; -CREATE TABLE anser_rf_seq_t (id int) DISTRIBUTED BY (id); -INSERT INTO anser_rf_seq_t SELECT g FROM generate_series(1, 10) g; -SELECT gp_inject_fault('nextval_sideband_before_response', 'skip', dbid) - FROM gp_segment_configuration WHERE role = 'p' AND content = -1; - gp_inject_fault ------------------ - Success: -(1 row) - -SELECT count(nextval('anser_rf_seq')) AS seq_rows FROM anser_rf_seq_t; - seq_rows ----------- - 10 -(1 row) - -SELECT gp_inject_fault('nextval_sideband_before_response', 'reset', dbid) - FROM gp_segment_configuration WHERE role = 'p' AND content = -1; - gp_inject_fault ------------------ - Success: -(1 row) - -DROP SEQUENCE anser_rf_seq; -DROP TABLE anser_rf_seq_t; -- --------------------------------------------------------------------------- -- Parallel execution. -- @@ -509,12 +744,15 @@ DROP TABLE anser_rf_seq_t; -- which disagree here: for one query the Postgres planner may give a serial -- build slice where GPORCA gives a parallel one. -- --- The join key is the distribution key on both sides, so no Motion is needed --- to co-locate the join. That is the only shape either planner parallelises: --- a redistributed or broadcast build side is fine for GPORCA but the Postgres --- planner marks Motion paths parallel-unsafe. +-- The build side is distributed by v and joined on id, so it is redistributed: +-- Anser only injects across a Motion, and a co-located join -- which is what +-- this used to use -- is now skipped outright, making the whole section +-- vacuous. The cost is that only GPORCA still parallelises it; the Postgres +-- planner marks Motion paths parallel-unsafe, so its two blocks below are +-- reduced to a filter-on/filter-off equivalence check on a serial slice. +-- They are kept because that equivalence is worth asserting on its own. -- --------------------------------------------------------------------------- -CREATE TABLE anser_rf_par_build (id int, v text) DISTRIBUTED BY (id); +CREATE TABLE anser_rf_par_build (id int, v text) DISTRIBUTED BY (v); CREATE TABLE anser_rf_par_probe (id int, pad text) DISTRIBUTED BY (id); INSERT INTO anser_rf_par_build SELECT g, 'b' || g FROM generate_series(1, 500) g; INSERT INTO anser_rf_par_probe SELECT g, 'p' || g FROM generate_series(1, 20000) g; @@ -636,7 +874,116 @@ RESET min_parallel_table_scan_size; RESET parallel_setup_cost; RESET parallel_tuple_cost; RESET max_parallel_workers_per_gather; +-- A probe side under a WindowAgg must not be filtered: the window is computed +-- over the whole input, so a consumer on the base scan below it would change +-- the result. The resolver must refuse the descent (the node whitelist +-- applies to the probe-side top too) and inject nothing. +SET optimizer = off; +SET anser.runtime_filter = on; +CREATE TABLE anser_rf_win (k int8, pad int) DISTRIBUTED BY (pad); +INSERT INTO anser_rf_win SELECT i % 1000, i FROM generate_series(1, 100000) i; +ANALYZE anser_rf_win; +-- A temp table is scanned on the coordinator, so the build side runs in the +-- coordinator's slice while the probe side runs on the segments: the slices +-- differ and the join would inject if the WindowAgg did not refuse it. k=5 +-- exists in the table (k = i % 1000, a hundred times), so the row matches and +-- the window total must be the full 100000 -- the windowed side returns every +-- row with the total attached, hence the max(). +CREATE TEMP TABLE anser_rf_dim (k int8) DISTRIBUTED BY (k); +INSERT INTO anser_rf_dim VALUES (5); +SELECT max(s.total) AS window_total +FROM anser_rf_dim d +LEFT JOIN (SELECT k, count(*) OVER () AS total FROM anser_rf_win) s + ON s.k = d.k; + window_total +-------------- + 100000 +(1 row) + +DROP TABLE anser_rf_dim; +DROP TABLE anser_rf_win; +-- IS NOT DISTINCT FROM below an outer join: ORCA puts a plain `=` in +-- hashclauses and the null-safety test in hashqualclauses, so the join passes +-- the equality gate, and the consumer used to land on the nullable side of +-- the lower LEFT JOIN. Every row it dropped came back NULL-extended and then +-- matched the build side's NULL under INDF -- 99997 rows instead of 0. The +-- resolver must now refuse the descent into the nullable side, so no filter +-- is injected and the count stays 0. +SET optimizer = on; +SET gp_enable_runtime_filter = off; +SET anser.runtime_filter = on; +CREATE TABLE anser_rf_s (x int, y int) DISTRIBUTED BY (x); +CREATE TABLE anser_rf_r (x int, k int) DISTRIBUTED BY (x); +CREATE TABLE anser_rf_b (k int) DISTRIBUTED RANDOMLY; +INSERT INTO anser_rf_s SELECT g, g FROM generate_series(1, 100000) g; +INSERT INTO anser_rf_r SELECT g, g + 1000000 FROM generate_series(1, 100000) g; +INSERT INTO anser_rf_b VALUES (NULL); +INSERT INTO anser_rf_b SELECT g FROM generate_series(1, 10) g; +ANALYZE anser_rf_s; +ANALYZE anser_rf_r; +ANALYZE anser_rf_b; +SELECT count(*) AS indf_rows FROM (anser_rf_s s LEFT JOIN anser_rf_r r ON s.x = r.x) + JOIN anser_rf_b b ON r.k IS NOT DISTINCT FROM b.k; + indf_rows +----------- + 0 +(1 row) + +-- Plain two-table INDF remains sound to filter: the bloom holds the build +-- side's non-NULL keys and NULL probe keys pass through, so matches and the +-- NULL row must all survive -- 1 and 2 are in b (1..10), NULL matches b's. +CREATE TABLE anser_rf_m (k int) DISTRIBUTED RANDOMLY; +INSERT INTO anser_rf_m VALUES (1), (2), (NULL); +SELECT count(*) AS indf_simple_rows FROM anser_rf_m m + JOIN anser_rf_b b ON m.k IS NOT DISTINCT FROM b.k; + indf_simple_rows +------------------ + 3 +(1 row) + +DROP TABLE anser_rf_m, anser_rf_s, anser_rf_r, anser_rf_b; +-- A computed probe key must refuse the filter: ORCA evaluates the cast in +-- the scan's projection (Output: (big)::oid), so the consumer would hash the +-- zero-extended computed oid while the producer hashes the sign-extended +-- stored oid -- every oid >= 2^31 mismatches, and only false positives pass +-- (3 rows instead of 50). The SeqScan shortcut must apply the same +-- plain-Var check as the resolver, so no consumer is injected. +CREATE TABLE anser_rf_p (id int, big int8) DISTRIBUTED BY (id); +CREATE TABLE anser_rf_o (o oid) DISTRIBUTED RANDOMLY; +INSERT INTO anser_rf_p SELECT g, 3000000000 + g FROM generate_series(1, 100000) g; +INSERT INTO anser_rf_o SELECT (3000000000 + g)::oid FROM generate_series(1, 50) g; +ANALYZE anser_rf_p; +ANALYZE anser_rf_o; +SELECT count(*) AS cast_rows + FROM (SELECT id, big::oid AS o FROM anser_rf_p) p + JOIN anser_rf_o b ON b.o = p.o; + cast_rows +----------- + 50 +(1 row) + +DROP TABLE anser_rf_p, anser_rf_o; +RESET gp_enable_runtime_filter; +-- Resource and log knobs are superuser-only: an unprivileged role must not +-- force 64 MB filters on every segment or fill the server log. runtime_filter +-- and timeout_ms only shape the caller's own queries, so they stay +-- user-settable. +CREATE ROLE anser_nopriv LOGIN; +NOTICE: resource queue required -- using default resource queue "pg_default" +SET ROLE anser_nopriv; +SET anser.debug_min_filter_bytes = 67108864; +ERROR: permission denied to set parameter "anser.debug_min_filter_bytes" +SET anser.max_info_size = 100000000; +ERROR: permission denied to set parameter "anser.max_info_size" +SET anser.debug = on; +ERROR: permission denied to set parameter "anser.debug" +SET anser.runtime_filter = on; +SET anser.timeout_ms = 100; +RESET ROLE; +DROP ROLE anser_nopriv; RESET anser.runtime_filter; +RESET anser.debug_min_filter_bytes; +RESET anser.timeout_ms; RESET optimizer; RESET enable_nestloop; RESET enable_mergejoin; diff --git a/gpcontrib/anser/expected/anser_test.out b/gpcontrib/anser/expected/anser_test.out index 13e1413eecc..ff3da23d1ea 100644 --- a/gpcontrib/anser/expected/anser_test.out +++ b/gpcontrib/anser/expected/anser_test.out @@ -44,7 +44,9 @@ SET anser.max_info_size = 68157440; -- 1. The planner's gate -- the only one that costs nothing, since nothing is -- injected and no consumer is left waiting. A 64 MB bitset is 536870912 bits, -- so 134217728 estimated keys is exactly 4 bits/key and one more key is not. --- A zero or negative estimate is treated as one key, not as "no filter". +-- A zero or negative estimate is treated as one key, not as "no filter"; the +-- 1 MB bitset floor then raises the declared count to match, so every estimate +-- the floor covers shows the same 524288 elements. SELECT t.est_rows, s.injected, s.total_elems, s.planned_bytes FROM (VALUES (0), (-5), (1), (1000), (1000000), (10000000), (134217728), (134217729), (150000000), (1000000000)) @@ -52,10 +54,10 @@ FROM (VALUES (0), (-5), (1), (1000), (1000000), (10000000), LATERAL anser_test_rf_size(t.est_rows) AS s; est_rows | injected | total_elems | planned_bytes ------------+----------+-------------+--------------- - 0 | t | 1 | 1048576 - -5 | t | 1 | 1048576 - 1 | t | 1 | 1048576 - 1000 | t | 1000 | 1048576 + 0 | t | 524288 | 1048576 + -5 | t | 524288 | 1048576 + 1 | t | 524288 | 1048576 + 1000 | t | 524288 | 1048576 1000000 | t | 1000000 | 1048576 10000000 | t | 10000000 | 16777216 134217728 | t | 134217728 | 67108864 @@ -263,24 +265,28 @@ FROM (VALUES -- The QD -> QE checksum. The value itself does not matter; which inputs -- change it does. Every routing field and the key are always covered, and the --- body only for a payload type that asks for it -- the fifth column is that --- deliberate exemption, not an oversight. +-- body only for a payload type that asks for it -- the other_body_exempt column +-- is that deliberate exemption, not an oversight. SELECT - anser_test_push_crc('B'::"char", 1, 0, 'k', '\x01') - <> anser_test_push_crc('B'::"char", 2, 0, 'k', '\x01') AS condition_covered, - anser_test_push_crc('B'::"char", 1, 0, 'k', '\x01') - <> anser_test_push_crc('B'::"char", 1, 1, 'k', '\x01') AS flags_covered, - anser_test_push_crc('B'::"char", 1, 0, 'k', '\x01') - <> anser_test_push_crc('B'::"char", 1, 0, 'j', '\x01') AS key_covered, - anser_test_push_crc('B'::"char", 1, 0, 'k', '\x01') - <> anser_test_push_crc('B'::"char", 1, 0, 'k', '\x02') AS bloom_body_covered, - anser_test_push_crc('-'::"char", 1, 0, 'k', '\x01') - = anser_test_push_crc('-'::"char", 1, 0, 'k', '\x02') AS other_body_exempt, - anser_test_push_crc('B'::"char", 1, 0, 'k', '\x01') - <> anser_test_push_crc('-'::"char", 1, 0, 'k', '\x01') AS type_covered; - condition_covered | flags_covered | key_covered | bloom_body_covered | other_body_exempt | type_covered --------------------+---------------+-------------+--------------------+-------------------+-------------- - t | t | t | t | t | t + anser_test_push_crc('B'::"char", 1, 7, 3, 0, 'k', '\x01') + <> anser_test_push_crc('B'::"char", 2, 7, 3, 0, 'k', '\x01') AS condition_covered, + anser_test_push_crc('B'::"char", 1, 7, 3, 0, 'k', '\x01') + <> anser_test_push_crc('B'::"char", 1, 8, 3, 0, 'k', '\x01') AS session_covered, + anser_test_push_crc('B'::"char", 1, 7, 3, 0, 'k', '\x01') + <> anser_test_push_crc('B'::"char", 1, 7, 4, 0, 'k', '\x01') AS command_covered, + anser_test_push_crc('B'::"char", 1, 7, 3, 0, 'k', '\x01') + <> anser_test_push_crc('B'::"char", 1, 7, 3, 1, 'k', '\x01') AS flags_covered, + anser_test_push_crc('B'::"char", 1, 7, 3, 0, 'k', '\x01') + <> anser_test_push_crc('B'::"char", 1, 7, 3, 0, 'j', '\x01') AS key_covered, + anser_test_push_crc('B'::"char", 1, 7, 3, 0, 'k', '\x01') + <> anser_test_push_crc('B'::"char", 1, 7, 3, 0, 'k', '\x02') AS bloom_body_covered, + anser_test_push_crc('-'::"char", 1, 7, 3, 0, 'k', '\x01') + = anser_test_push_crc('-'::"char", 1, 7, 3, 0, 'k', '\x02') AS other_body_exempt, + anser_test_push_crc('B'::"char", 1, 7, 3, 0, 'k', '\x01') + <> anser_test_push_crc('-'::"char", 1, 7, 3, 0, 'k', '\x01') AS type_covered; + condition_covered | session_covered | command_covered | flags_covered | key_covered | bloom_body_covered | other_body_exempt | type_covered +-------------------+-----------------+-----------------+---------------+-------------+--------------------+-------------------+-------------- + t | t | t | t | t | t | t | t (1 row) -- 5. The runtime-filter pushdown decision. A scan key is numbered by the @@ -349,4 +355,67 @@ FROM (VALUES non-scan has no target | f | (3 rows) +-- 7. How many producers a slice runs. This number is the channel's +-- completion criterion, so over-counting costs every consumer the full +-- anser.timeout_ms -- the channel never reaches the count it waits for and +-- nobody is told -- while under-counting completes it early and hands out a +-- filter missing a segment's keys, which drops rows that join. So each of +-- FillSliceGangInfo's four formulas is pinned here, not inferred from one. +-- +-- numsegments is only meaningful for a reader or writer gang; the columns are +-- still filled for the other types to show they are ignored. +SELECT t.what, t.gang, t.nseg, t.workers, t.direct, + anser_test_slice_producers(t.gang, t.nseg, t.workers, t.direct, 0) + AS producers +FROM (VALUES + ('coordinator slice', 'unallocated', 3, 0, NULL::int4[]), + ('coordinator slice, parallel', 'unallocated', 3, 4, NULL), + ('entry db reader', 'entrydb', 3, 0, NULL), + ('singleton reader', 'singleton', 3, 0, NULL), + ('singleton reader, parallel', 'singleton', 3, 4, NULL), + ('reader, serial', 'reader', 3, 0, NULL), + ('reader, parallel', 'reader', 3, 4, NULL), + ('writer, serial', 'writer', 3, 0, NULL), + ('direct dispatch, one segment', 'reader', 3, 0, '{1}'), + ('direct dispatch, two, serial', 'reader', 3, 0, '{0,2}'), + ('direct dispatch, two, par 4', 'reader', 3, 4, '{0,2}'), + ('direct dispatch, nowhere', 'reader', 3, 4, '{}'), + ('no segments', 'reader', 0, 4, NULL), + ('negative segments', 'reader', -1, 4, NULL) + ) AS t(what, gang, nseg, workers, direct); + what | gang | nseg | workers | direct | producers +------------------------------+-------------+------+---------+--------+----------- + coordinator slice | unallocated | 3 | 0 | | 1 + coordinator slice, parallel | unallocated | 3 | 4 | | 1 + entry db reader | entrydb | 3 | 0 | | 1 + singleton reader | singleton | 3 | 0 | | 1 + singleton reader, parallel | singleton | 3 | 4 | | 4 + reader, serial | reader | 3 | 0 | | 3 + reader, parallel | reader | 3 | 4 | | 12 + writer, serial | writer | 3 | 0 | | 3 + direct dispatch, one segment | reader | 3 | 0 | {1} | 1 + direct dispatch, two, serial | reader | 3 | 0 | {0,2} | 2 + direct dispatch, two, par 4 | reader | 3 | 4 | {0,2} | 8 + direct dispatch, nowhere | reader | 3 | 4 | {} | 0 + no segments | reader | 0 | 4 | | 0 + negative segments | reader | -1 | 4 | | 0 +(14 rows) + +-- The slice has to be identifiable at all; anything else is zero, which the +-- injection pass reads as "unknown" and declines on. +SELECT t.what, anser_test_slice_producers('reader', 3, 4, NULL, t.idx) + AS producers +FROM (VALUES + ('slice 0 of 1 exists', 0), + ('slice 1 of 1 does not', 1), + ('negative slice index', -1) + ) AS t(what, idx); + what | producers +-----------------------+----------- + slice 0 of 1 exists | 12 + slice 1 of 1 does not | 0 + negative slice index | 0 +(3 rows) + +RESET anser.max_info_size; DROP EXTENSION anser_test; diff --git a/gpcontrib/anser/include/anser.h b/gpcontrib/anser/include/anser.h index d7967e59070..d1862f93082 100644 --- a/gpcontrib/anser/include/anser.h +++ b/gpcontrib/anser/include/anser.h @@ -44,18 +44,39 @@ #define ANSER_CONDITION_KEY_SIZE 64 +/* + * What a sideband delivery costs on the wire besides the payload: the fixed + * header fields plus the condition key, rounded up. Not to be confused with + * the room a serialized bloom part keeps for its own header -- that one is + * about the payload's contents, this one about the frame around it. + * + * anser.max_info_size caps the payload, so payload + this is the largest + * message a consumer can be asked to read, and the reason that GUC cannot be + * set as high as an int goes. + */ +#define ANSER_SIDEBAND_FRAME_ROOM (ANSER_CONDITION_KEY_SIZE + 64) + /* * Identifies one channel: a single condition within one running command. * * condition_key is an opaque symbol naming the condition -- the * optimizer-generated equivalence-class symbol described in the Anser paper. * Channels are only ever compared for equality, never ordered. + * + * Zero the whole struct before filling it in. The coordinator keeps channels + * in a HASH_BLOBS table keyed by sizeof(AnserChannelKey), so every byte of it + * is hashed and compared, the bytes past the key's terminating NUL included -- + * and neither strlcpy() nor a memcpy() of a shorter key clears those. Two + * sides naming the same condition would then land on different channels, and + * what that looks like from the outside is consumers timing out for no + * visible reason. */ typedef struct AnserChannelKey { int gp_session_id; int gp_command_count; uint32 condition_id; + /* NUL-terminated, and zero-padded to the end -- see above. */ char condition_key[ANSER_CONDITION_KEY_SIZE]; } AnserChannelKey; @@ -65,6 +86,7 @@ extern PGDLLEXPORT bool gp_anser_runtime_filter; extern PGDLLEXPORT bool gp_anser_debug; extern PGDLLEXPORT int gp_anser_max_info_size; extern PGDLLEXPORT int gp_anser_timeout_ms; +extern PGDLLEXPORT int gp_anser_debug_min_filter_bytes; /* * Trace the handoff between producers, the coordinator and consumers. diff --git a/gpcontrib/anser/include/anserbloom.h b/gpcontrib/anser/include/anserbloom.h index 946b35db5f8..68d5c904d08 100644 --- a/gpcontrib/anser/include/anserbloom.h +++ b/gpcontrib/anser/include/anserbloom.h @@ -42,11 +42,6 @@ typedef struct AnserBloomFilterProduceState AnserBloomFilterProduceState; typedef struct AnserBloomFilterConsumeState AnserBloomFilterConsumeState; /* Producer side: build one Bloom filter part and publish it to the channel. */ - -/* - * libpq connection (parallel-retrieve-cursor model); NULL means connect - * without it and rely on pg_hba. Ignored on the coordinator-local path. - */ extern PGDLLEXPORT AnserBloomFilterProduceState *ExecInitAnserBloomFilterProduce( const AnserChannelKey *channel_key, int64 total_elems, @@ -70,6 +65,13 @@ extern PGDLLEXPORT bool ExecAnserBloomFilterConsume(AnserBloomFilterConsumeState long registration_timeout_ms); extern PGDLLEXPORT bloom_filter *ExecAnserBloomFilterConsumerGetFilter( AnserBloomFilterConsumeState *state); +/* + * The producer count the delivered payload declares, or 0 if no filter was + * built. Every part of a channel carries that count and folding does not + * change it, so this is a cross-check on the payload, not a tally of arrivals: + * the coordinator delivers only once it holds every part, so a filter that + * reaches a consumer at all is a complete one. + */ extern PGDLLEXPORT uint32 ExecAnserBloomFilterConsumerReceivedParts( AnserBloomFilterConsumeState *state); extern PGDLLEXPORT bool ExecAnserBloomFilterConsumerWasCancelled( diff --git a/gpcontrib/anser/include/anserfilter.h b/gpcontrib/anser/include/anserfilter.h index dd802a4b6f6..4db4d0563e4 100644 --- a/gpcontrib/anser/include/anserfilter.h +++ b/gpcontrib/anser/include/anserfilter.h @@ -99,7 +99,7 @@ extern PGDLLEXPORT bool AnserBloomPartWorthSending(const void *payload, Size pay extern PGDLLEXPORT uint64 AnserBloomSeed(const char *condition_key); /* - * Build an empty filter, or return NULL when AnserBloomShapeFor says it is not + * Build an empty filter, or return NULL when the sizing rules say it is not * worth building. A NULL return is not an error: the producer turns it into an * immediate cancel, so consumers stop waiting instead of timing out. */ diff --git a/gpcontrib/anser/include/anserplan.h b/gpcontrib/anser/include/anserplan.h index 26c8169424d..6dd9adffd87 100644 --- a/gpcontrib/anser/include/anserplan.h +++ b/gpcontrib/anser/include/anserplan.h @@ -119,12 +119,21 @@ extern PGDLLEXPORT bool AnserRuntimeFilterSize(double est_rows, * return is therefore a correctness gate, not a missed optimization: callers * must decline rather than guess. * + * `for_consumer` is true when resolving the consumer's (probe) key and false + * for the producer's (build) key: a consumer removes rows, so the descent + * refuses an outer join's nullable side, where pruning rewrites a dropped row + * into a NULL-extended one that a downstream condition can distinguish from + * "no row" (IS NOT DISTINCT FROM matches NULL to NULL). A producer only + * summarizes the column, and a superset of the outer join's emitted keys is + * all it needs, so it may descend anywhere. + * * Only meaningful after set_plan_references, which is also what makes the * descent necessary: before it the Vars still name their base relations. */ extern PGDLLEXPORT bool AnserResolveKeyScan(Plan *top, AttrNumber key_attno, int slice_index, + bool for_consumer, Plan **parent_out, Plan **scan_out, AttrNumber *attno_out, diff --git a/gpcontrib/anser/include/ansersideband.h b/gpcontrib/anser/include/ansersideband.h index 995c16670ad..4c1f773635c 100644 --- a/gpcontrib/anser/include/ansersideband.h +++ b/gpcontrib/anser/include/ansersideband.h @@ -76,6 +76,7 @@ struct pgNotify; /* #include "libpq-fe.h" */ */ #define ANSER_WIRE_KIND_PART 'P' /* a producer's serialized part */ #define ANSER_WIRE_KIND_SUBSCRIBE 'S' /* a consumer registering interest */ +#define ANSER_WIRE_KIND_UNSUBSCRIBE 'U' /* ... and withdrawing it again */ /* Flag bits, shared by both directions. */ #define ANSER_WIRE_F_CANCELLED 0x0001 @@ -176,7 +177,13 @@ extern PGDLLEXPORT bool AnserWireCheckCrc(const AnserWireMsg *msg, const void *b /* * QD -> QE push: raw binary, written with pqPutInt/pqPutnchar. * - * payload_type | crc32c | condition_id | flags | keylen | key | bodylen | body + * payload_type | crc32c | condition_id | session_id | command_count | + * flags | keylen | key | bodylen | body + * + * The session and command ids are what lets a parked delivery be told apart + * from the current command's: condition_key restarts at the same value for + * every statement, so the consumer must match the whole identity, not the key + * alone. * * with each field a 4-byte integer except the key and body. The CRC covers all * of the other fields in that order, the integers in network byte order, so it @@ -186,6 +193,7 @@ extern PGDLLEXPORT bool AnserWireCheckCrc(const AnserWireMsg *msg, const void *b * ansersideband.c, beside the reader that verifies it. */ extern PGDLLEXPORT pg_crc32c AnserWirePushCrc(char payload_type, uint32 condition_id, + uint32 session_id, uint32 command_count, uint32 flags, const char *key, int keylen, const void *body, int bodylen); @@ -242,7 +250,10 @@ extern PGDLLEXPORT bool AnserDispatchLocalConsume(const AnserChannelKey *channel * the executor/transaction-end callbacks call; the halves are exposed because * each lives with the state it owns. */ +extern PGDLLEXPORT int64 AnserDispatchUnsubscribeCount(void); +extern PGDLLEXPORT void AnserDispatchFlushPending(long timeout_ms); extern PGDLLEXPORT void AnserDispatchReset(void); +extern PGDLLEXPORT void AnserDispatchResetCommand(int command_count); extern PGDLLEXPORT void AnserSidebandResetInbox(void); extern PGDLLEXPORT void AnserSidebandResetAll(void); diff --git a/gpcontrib/anser/sql/anser_faults.sql b/gpcontrib/anser/sql/anser_faults.sql new file mode 100644 index 00000000000..0cc5f4a39b1 --- /dev/null +++ b/gpcontrib/anser/sql/anser_faults.sql @@ -0,0 +1,207 @@ +-- Anser runtime bloom filter: the cases that need gp_inject_fault. +-- +-- Kept apart from anser_runtime_filter so that a build configured with +-- --disable-debug-extensions loses only these. gp_inject_fault is built only +-- with the debug extensions, and the regression driver excludes any file that +-- calls one -- see faultinjector_prep in src/Makefile.global.in -- so whatever +-- shares a file with a fault is skipped along with it. Everything that can be +-- asked for with data alone therefore stays in the other file. +-- +-- Requires the cluster to run with anser.enable=on, like the rest of the suite. +-- +-- The extension may or may not already be there, and saying so is not part of +-- what is being tested; it is never dropped, for the same reason -- it can +-- predate this test, and other tests in the same database rely on it. +-- start_ignore +CREATE EXTENSION IF NOT EXISTS gp_inject_fault; +-- end_ignore + +-- Deterministic plan shape: a hash join under the Postgres planner, as in +-- anser_runtime_filter. A fresh session inherits none of its settings. +SET enable_nestloop = off; +SET enable_mergejoin = off; +SET optimizer = off; + +-- nextval guard: the QD sends two kinds of message down a QE's dispatch +-- connection -- the nextval reply and a sideband message -- and each has its +-- own reader. A reader that meets the other's message has to skip it whole: +-- taking the type byte and erroring leaves the body in the stream, and the +-- session then dies somewhere else with "protocol synchronization was lost". +-- +-- In the field this needs an Anser consumer to give up on a filter the +-- coordinator goes on to deliver anyway, which is a race and cannot be asked +-- for from SQL. The fault puts a sideband message in front of one nextval +-- reply instead, so the ordering is certain rather than likely; the +-- three-argument form is occurrence 1..1, so it fires once. +-- +-- Nothing here is Anser's -- the fault supplies the message and the fix is in +-- cdb_sequence_nextval_qe -- but Anser is why a second message type exists on +-- that connection at all, so the test lives with it. +-- +CREATE SEQUENCE anser_rf_seq; +CREATE TABLE anser_rf_seq_t (id int) DISTRIBUTED BY (id); +INSERT INTO anser_rf_seq_t SELECT g FROM generate_series(1, 10) g; +SELECT gp_inject_fault('nextval_sideband_before_response', 'skip', dbid) + FROM gp_segment_configuration WHERE role = 'p' AND content = -1; +SELECT count(nextval('anser_rf_seq')) AS seq_rows FROM anser_rf_seq_t; +SELECT gp_inject_fault('nextval_sideband_before_response', 'reset', dbid) + FROM gp_segment_configuration WHERE role = 'p' AND content = -1; +DROP SEQUENCE anser_rf_seq; +DROP TABLE anser_rf_seq_t; + +-- Delivery to a consumer that stopped reading must not block the coordinator. +-- +-- A merged filter is 1-64 MB and the dispatch connections are blocking, so +-- pushing to a QE that is no longer reading used to wait in pqWait() with no +-- deadline and no CHECK_FOR_INTERRUPTS -- inside the dispatcher loop, which +-- then served nobody. Neither cancel nor statement_timeout could end it. +-- +-- The ordering cannot be arranged with data: the channel completes when the +-- last part lands, which is normally while the consumers are still waiting. +-- Holding the delivery back is what lets every consumer reach its deadline +-- and move on to its probe side -- and it is while they are busy there that +-- the coordinator finally writes to all of them. +-- +-- Note this one hangs rather than fails if the fix is reverted, for the same +-- reason the bug was unkillable. It is here because the alternative is a +-- regression nobody notices until a cluster wedges. +SET anser.timeout_ms = 1; +CREATE TABLE anser_rf_push_build (id int, pad text) DISTRIBUTED BY (pad); +CREATE TABLE anser_rf_push_probe (id int) DISTRIBUTED BY (id); +INSERT INTO anser_rf_push_build SELECT g, 'b' || g FROM generate_series(1, 1000) g; +INSERT INTO anser_rf_push_probe SELECT g % 1000 + 1 FROM generate_series(1, 1000000) g; +ANALYZE anser_rf_push_build; +ANALYZE anser_rf_push_probe; + +-- One segment publishes a second late; every other consumer gives up at 1 ms. +SELECT gp_inject_fault('anser_before_deliver', 'sleep', '', '', '', 1, 1, 1, dbid) + FROM gp_segment_configuration WHERE role = 'p' AND content = -1; +SET anser.runtime_filter = on; +SELECT count(*) AS push_rows + FROM anser_rf_push_probe p JOIN anser_rf_push_build b ON p.id = b.id; +SELECT gp_inject_fault('anser_before_deliver', 'reset', dbid) + FROM gp_segment_configuration WHERE role = 'p' AND content = -1; +DROP TABLE anser_rf_push_build, anser_rf_push_probe; + +-- A consumer that runs out of time withdraws, so the coordinator stops +-- counting it as a subscriber. Without that it would keep delivering to a +-- process that has moved on -- which is what the push above has to survive, +-- and what this keeps from quietly regressing into. +-- +-- The withdrawal leaves no trace a query can see: the channel is gone by the +-- time the query returns, and a filter that merely arrived late looks the +-- same. So the coordinator counts them, and this reads the difference. +CREATE TABLE anser_rf_unsub_build (id int, pad text) DISTRIBUTED BY (pad); +CREATE TABLE anser_rf_unsub_probe (id int) DISTRIBUTED BY (id); +INSERT INTO anser_rf_unsub_build SELECT g, 'b' || g FROM generate_series(1, 1000) g; +INSERT INTO anser_rf_unsub_probe SELECT g % 1000 + 1 FROM generate_series(1, 200000) g; +ANALYZE anser_rf_unsub_build; +ANALYZE anser_rf_unsub_probe; +-- start_ignore +CREATE EXTENSION IF NOT EXISTS anser_test; +-- end_ignore +SELECT anser_test_unsubscribes() AS unsub_before \gset +SELECT gp_inject_fault('anser_before_deliver', 'sleep', '', '', '', 1, 1, 1, dbid) + FROM gp_segment_configuration WHERE role = 'p' AND content = -1; +SELECT count(*) AS unsub_rows + FROM anser_rf_unsub_probe p JOIN anser_rf_unsub_build b ON p.id = b.id; +SELECT gp_inject_fault('anser_before_deliver', 'reset', dbid) + FROM gp_segment_configuration WHERE role = 'p' AND content = -1; +SELECT anser_test_unsubscribes() > :unsub_before AS consumers_withdrew; +DROP EXTENSION anser_test; +DROP TABLE anser_rf_unsub_build, anser_rf_unsub_probe; + +-- A partially delivered filter must not wedge the next command. +-- +-- The push above is torn when the filter is bigger than the connection +-- buffers: the consumers give up and finish, the coordinator's non-blocking +-- write leaves the tail queued, and the poll loop stops flushing once the +-- peer's command completes. The next dispatch on the same gang would then +-- overwrite the queued bytes (PQsendGpQuery_shared), leaving the QE waiting +-- forever for a body that never comes -- the session only ended with the +-- cluster. ExecutorEnd now completes the flush, or poisons the connection. +-- +-- The filter is forced to 64 MB (anser.debug_min_filter_bytes) so the push +-- cannot fit the buffers even on loopback. This test hangs rather than fails +-- if the fix is reverted, the same warning as the push test above. +SET anser.timeout_ms = 100; +SET anser.debug_min_filter_bytes = 67108864; +CREATE TABLE anser_rf_stale_build (id int, pad text) DISTRIBUTED BY (pad); +CREATE TABLE anser_rf_stale_probe (id int) DISTRIBUTED BY (id); +INSERT INTO anser_rf_stale_build SELECT g, 'b' || g FROM generate_series(1, 1000) g; +INSERT INTO anser_rf_stale_probe SELECT g % 1000 + 1 FROM generate_series(1, 1000000) g; +ANALYZE anser_rf_stale_build; +ANALYZE anser_rf_stale_probe; + +-- Hold the delivery for 1 s so every consumer gives up at 100 ms and the +-- 64 MB filter is pushed to connections nobody is reading. +SELECT gp_inject_fault('anser_before_deliver', 'sleep', '', '', '', 1, 1, 1, dbid) + FROM gp_segment_configuration WHERE role = 'p' AND content = -1; +SET anser.runtime_filter = on; +SELECT count(*) AS stale_rows + FROM anser_rf_stale_probe p JOIN anser_rf_stale_build b ON p.id = b.id; +SELECT gp_inject_fault('anser_before_deliver', 'reset', dbid) + FROM gp_segment_configuration WHERE role = 'p' AND content = -1; + +-- The torn delivery is completed (or its connection poisoned) at ExecutorEnd, +-- so the next command on the same gang must answer. +SELECT 'next command' AS marker, count(*) AS build_rows FROM anser_rf_stale_build; +DROP TABLE anser_rf_stale_build, anser_rf_stale_probe; +RESET anser.debug_min_filter_bytes; + +-- A filter parked by one statement must not be claimed by the next. +-- +-- A consumer that waits while a delivery for another channel arrives parks it +-- in the inbox. If the statement then errors inside a savepoint, there is no +-- ExecutorEnd for it (a failed portal skips it), so the parked entry used to +-- survive -- and the next statement, whose condition keys restart at the same +-- values, claimed it: a filter built from the old build side pruned the new +-- one. Deliveries now carry the session id and command count and a consumer +-- matches on them exactly, and the subtransaction abort drops the inbox. +-- +-- The LEFT JOIN pins j2 as the build side, so its consumer waits first: it +-- gives up at 700 ms while the coordinator sleeps for 1 s, its late delivery +-- is then read -- and parked -- by j1's consumer, and the division errors +-- before anything claims it. The second run re-executes the same plan shape +-- with a build side disjoint from the parked filter: claiming it would drop +-- every joinable row, so count(j2.id) must be 1000, not 0. +SET anser.timeout_ms = 700; +CREATE TABLE anser_sf_b1 (id int, pad text) DISTRIBUTED BY (pad); +CREATE TABLE anser_sf_p1 (id int) DISTRIBUTED BY (id); +CREATE TABLE anser_sf_b2 (id int, pad text) DISTRIBUTED BY (pad); +CREATE TABLE anser_sf_p2 (id int) DISTRIBUTED BY (id); +INSERT INTO anser_sf_b1 SELECT g, 'b' || g FROM generate_series(1, 2000) g; +INSERT INTO anser_sf_p1 SELECT g FROM generate_series(1, 2000) g; +INSERT INTO anser_sf_b2 SELECT g, 'b' || g FROM generate_series(1, 1000) g; +INSERT INTO anser_sf_p2 SELECT g FROM generate_series(1, 2000) g; +ANALYZE anser_sf_b1; +ANALYZE anser_sf_p1; +ANALYZE anser_sf_b2; +ANALYZE anser_sf_p2; + +SELECT gp_inject_fault('anser_before_deliver', 'sleep', '', '', '', 1, 1, 1, dbid) + FROM gp_segment_configuration WHERE role = 'p' AND content = -1; +SET anser.runtime_filter = on; +BEGIN; +SAVEPOINT anser_sf; +SELECT j1.id / (j1.id - j1.id) AS boom +FROM (SELECT p1.id FROM anser_sf_p1 p1 JOIN anser_sf_b1 b1 ON p1.id = b1.id) j1 +LEFT JOIN (SELECT p2.id FROM anser_sf_p2 p2 JOIN anser_sf_b2 b2 ON p2.id = b2.id) j2 + ON j1.id = j2.id; +ROLLBACK TO SAVEPOINT anser_sf; +DELETE FROM anser_sf_b2; +INSERT INTO anser_sf_b2 SELECT 1000 + g, 'b' || g FROM generate_series(1, 1000) g; +SELECT count(j2.id) AS reused_rows +FROM (SELECT p1.id FROM anser_sf_p1 p1 JOIN anser_sf_b1 b1 ON p1.id = b1.id) j1 +LEFT JOIN (SELECT p2.id FROM anser_sf_p2 p2 JOIN anser_sf_b2 b2 ON p2.id = b2.id) j2 + ON j1.id = j2.id; +COMMIT; +SELECT gp_inject_fault('anser_before_deliver', 'reset', dbid) + FROM gp_segment_configuration WHERE role = 'p' AND content = -1; +DROP TABLE anser_sf_b1, anser_sf_p1, anser_sf_b2, anser_sf_p2; + +RESET anser.runtime_filter; +RESET anser.timeout_ms; +RESET optimizer; +RESET enable_nestloop; +RESET enable_mergejoin; diff --git a/gpcontrib/anser/sql/anser_multijoin.sql b/gpcontrib/anser/sql/anser_multijoin.sql index e10247295d6..fcfda3e550c 100644 --- a/gpcontrib/anser/sql/anser_multijoin.sql +++ b/gpcontrib/anser/sql/anser_multijoin.sql @@ -28,10 +28,10 @@ SET enable_nestloop = off; SET enable_mergejoin = off; -- A regress test must not inherit the production deadline. anser.timeout_ms --- defaults to 100 s because delivery is serial and a wide slice needs that --- long to be served; a test that waits it out has turned a delivery failure --- into a slow pass. Pinned short here, so a filter that never arrives fails --- the run instead of hiding in the clock. +-- defaults to 10 s, which is a budget for an exchange that has gone wrong; a +-- test that waits it out has turned a delivery failure into a slow pass. +-- Pinned shorter here, so a filter that never arrives fails the run instead of +-- hiding in the clock. SET anser.timeout_ms = 5000; -- own is what the previous table joins to, nxt is what this table joins to the @@ -84,6 +84,15 @@ ANALYZE anser_mj_t9; ANALYZE anser_mj_t10; ANALYZE anser_mj_t11; +-- The same shape, minus every tenth key. The outer-join case below needs a +-- right side that does not cover its left, and none of the tables above can be +-- it: they are joined 1:1 on purpose, and thinning one would break the "1000 +-- rows summing to 500500 whatever the join count" invariant the chains rest on. +CREATE TABLE anser_mj_gap (own int, nxt int, payload text) DISTRIBUTED BY (own); +INSERT INTO anser_mj_gap SELECT g, g, 'gap_' || g + FROM generate_series(1, 3000) g WHERE g % 10 <> 0; +ANALYZE anser_mj_gap; + -- Runs one chain twice, with the filter on and off, and reports whether the two -- agree. The checksum is what makes the comparison mean something: equal row -- counts would also come out of two different sets of rows, and a filter that @@ -130,23 +139,42 @@ SELECT joins, nrows, own_sum, agrees SET optimizer = off; -- A chain whose middle join is an outer join, and whose next join reads the --- nullable side's key. The joined result there holds NULLs the base relation --- does not, and a NULL must not be taken for a key the filter is missing. +-- nullable side's key -- so the joined result carries NULLs the base relation +-- does not. +-- +-- Two things make that true of the plan and not just of the text. The right +-- side has to leave keys unmatched, which is what anser_mj_gap is for. And the +-- outer join has to survive planning: the join above it is strict in c.nxt, so +-- reduce_outer_joins() would otherwise turn this LEFT JOIN into an inner one +-- before any path is considered. OFFSET 0 keeps the subquery from being pulled +-- up, which keeps that qual a level away. +-- +-- No filter is injected here, and none can be. The price of keeping the outer +-- join is a SubqueryScan, and AnserResolveKeyScan descends through Hash, Motion +-- and HashJoin only. Nor is there a way around it: a NULL-keyed row reaches +-- the output only under an outer join, an outer join survives only behind a +-- subquery boundary, and Anser does not inject across one. So a filter meeting +-- a NULL key is not something this suite can arrange -- what is checked here is +-- that such a plan returns the same rows either way. SET anser.runtime_filter = on; CREATE TEMP TABLE anser_mj_lj_on AS - SELECT a.own AS aown, c.own AS cown, d.own AS down - FROM anser_mj_t1 a - JOIN anser_mj_t2 b ON a.nxt = b.own - LEFT JOIN anser_mj_t3 c ON b.nxt = c.own - JOIN anser_mj_t4 d ON c.nxt = d.own + SELECT s.aown, s.cown, d.own AS down + FROM (SELECT a.own AS aown, c.own AS cown, c.nxt AS cnxt + FROM anser_mj_t1 a + JOIN anser_mj_t2 b ON a.nxt = b.own + LEFT JOIN anser_mj_gap c ON b.nxt = c.own + OFFSET 0) s + JOIN anser_mj_t4 d ON s.cnxt = d.own DISTRIBUTED BY (aown); SET anser.runtime_filter = off; CREATE TEMP TABLE anser_mj_lj_off AS - SELECT a.own AS aown, c.own AS cown, d.own AS down - FROM anser_mj_t1 a - JOIN anser_mj_t2 b ON a.nxt = b.own - LEFT JOIN anser_mj_t3 c ON b.nxt = c.own - JOIN anser_mj_t4 d ON c.nxt = d.own + SELECT s.aown, s.cown, d.own AS down + FROM (SELECT a.own AS aown, c.own AS cown, c.nxt AS cnxt + FROM anser_mj_t1 a + JOIN anser_mj_t2 b ON a.nxt = b.own + LEFT JOIN anser_mj_gap c ON b.nxt = c.own + OFFSET 0) s + JOIN anser_mj_t4 d ON s.cnxt = d.own DISTRIBUTED BY (aown); SELECT count(*) AS lj_rows FROM anser_mj_lj_on; @@ -215,14 +243,16 @@ $$; SET anser.runtime_filter = on; SELECT 'the chain pruned nothing: no filter arrived, or none was injected' AS problem WHERE anser_mj_pruned('SELECT count(*) FROM anser_mj_t1 a JOIN anser_mj_t2 b ON a.nxt = b.own JOIN anser_mj_t3 c ON b.nxt = c.own') = 0; + DROP FUNCTION anser_mj_pruned(text); DROP FUNCTION anser_mj_check(int); DROP TABLE anser_mj_t1, anser_mj_t2, anser_mj_t3, anser_mj_t4, anser_mj_t5, anser_mj_t6, anser_mj_t7, anser_mj_t8, anser_mj_t9, anser_mj_t10, - anser_mj_t11; + anser_mj_t11, anser_mj_gap; RESET anser.runtime_filter; +RESET anser.timeout_ms; RESET optimizer; RESET enable_nestloop; RESET enable_mergejoin; diff --git a/gpcontrib/anser/sql/anser_runtime_filter.sql b/gpcontrib/anser/sql/anser_runtime_filter.sql index 736823fbd57..c444f4e3dbb 100644 --- a/gpcontrib/anser/sql/anser_runtime_filter.sql +++ b/gpcontrib/anser/sql/anser_runtime_filter.sql @@ -12,10 +12,10 @@ SET enable_nestloop = off; SET enable_mergejoin = off; -- A regress test must not inherit the production deadline. anser.timeout_ms --- defaults to 100 s because delivery is serial and a wide slice needs that --- long to be served; a test that waits it out has turned a delivery failure --- into a slow pass. Pinned short here, so a filter that never arrives fails --- the run instead of hiding in the clock. +-- defaults to 10 s, which is a budget for an exchange that has gone wrong; a +-- test that waits it out has turned a delivery failure into a slow pass. +-- Pinned shorter here, so a filter that never arrives fails the run instead of +-- hiding in the clock. SET anser.timeout_ms = 5000; -- build is the smaller (hashed / preserved) side, distributed so the join key @@ -85,6 +85,25 @@ SELECT count(*) AS only_off_push FROM (SELECT * FROM anser_rf_r_off EXCEPT ALL SELECT * FROM anser_rf_r_push) d; RESET gp_enable_runtime_filter_pushdown; +-- Counts the Anser nodes a plan carries. Comparing a filtered count with an +-- unfiltered one says nothing when no filter was injected: both runs then do +-- the same thing and agree. Several blocks below ask this as well, which is +-- why it is defined here rather than beside its first caller. +CREATE FUNCTION anser_rf_nodes(q text) RETURNS bigint +LANGUAGE plpgsql AS $$ +DECLARE + line text; + nodes bigint := 0; +BEGIN + FOR line IN EXECUTE 'EXPLAIN (COSTS OFF) ' || q LOOP + IF line ~ 'Anser Bloom' THEN + nodes := nodes + 1; + END IF; + END LOOP; + RETURN nodes; +END; +$$; + -- Pushdown where the key's table attribute number and its position in the -- scan's output targetlist disagree. The case above cannot see the -- difference: anser_rf_probe's key is its first column and also the first @@ -94,9 +113,9 @@ RESET gp_enable_runtime_filter_pushdown; -- built on `id` -- and since no payload is ever a build key, every row is -- discarded. Both counts must be 200. SET optimizer = on; -CREATE TABLE anser_rf_proj_build (id int) DISTRIBUTED BY (id); +CREATE TABLE anser_rf_proj_build (id int, pad text) DISTRIBUTED BY (pad); CREATE TABLE anser_rf_proj_probe (payload int, id int) DISTRIBUTED BY (id); -INSERT INTO anser_rf_proj_build SELECT g FROM generate_series(1, 200) g; +INSERT INTO anser_rf_proj_build SELECT g, 'b' || g FROM generate_series(1, 200) g; INSERT INTO anser_rf_proj_probe SELECT -g, g FROM generate_series(1, 2000) g; ANALYZE anser_rf_proj_build; ANALYZE anser_rf_proj_probe; @@ -107,6 +126,8 @@ SELECT count(p.id) AS proj_push_off SET anser.runtime_filter = on; SELECT count(p.id) AS proj_push_on FROM anser_rf_proj_build b LEFT JOIN anser_rf_proj_probe p ON b.id = p.id; +SELECT anser_rf_nodes('SELECT count(p.id) + FROM anser_rf_proj_build b LEFT JOIN anser_rf_proj_probe p ON b.id = p.id') > 0 AS proj_injected; -- The alias is not the column. This subquery renames `payload` to the name -- `id` and the real `id` to `id2`, and joins on id2 -- so resolving the key by @@ -124,14 +145,24 @@ SELECT count(p.id2) AS rename_push_on FROM anser_rf_proj_build b LEFT JOIN (SELECT payload AS id, id AS id2 FROM anser_rf_proj_probe) p ON b.id = p.id2; +SELECT anser_rf_nodes('SELECT count(p.id2) + FROM anser_rf_proj_build b + LEFT JOIN (SELECT payload AS id, id AS id2 FROM anser_rf_proj_probe) p + ON b.id = p.id2') > 0 AS rename_injected; --- A computed key has no table attribute number to push down: the scan would --- have to evaluate `id * 2`, which PassByBloomFilter cannot do. Note the join --- clause itself still looks like a plain Var to the planner (it references the --- scan's output position), so the key resolves and the nodes are injected -- --- it is the scan's own targetlist entry that is an expression. The consumer --- must decline the pushdown and probe the projected slot itself. Only the --- 100 even build ids match, so both counts must be 100. +-- A computed key, which Anser declines outright. The subquery has no ORDER BY, +-- LIMIT or OFFSET, so is_simple_subquery() pulls it up and the hash clause +-- becomes `b.id = p.id * 2`; anser_hashjoin_keys takes two plain Vars and +-- nothing else, so no nodes are injected at all. +-- +-- The comment here used to claim the opposite -- that the clause still reads as +-- a Var into the scan's output and only the pushdown is declined. That would +-- need the subquery to survive as a scan of its own, which asking for it +-- (OFFSET 0) does not buy either: AnserResolveKeyScan does not descend through +-- a SubqueryScan. The decision for a computed column is covered directly by +-- anser_test_pushdown_accepts; what is left to check here is that declining is +-- clean, so expr_injected is f and both counts are 100 -- only the 100 even +-- build ids match. SET anser.runtime_filter = off; SELECT count(p.k) AS expr_push_off FROM anser_rf_proj_build b @@ -140,6 +171,9 @@ SET anser.runtime_filter = on; SELECT count(p.k) AS expr_push_on FROM anser_rf_proj_build b LEFT JOIN (SELECT id * 2 AS k FROM anser_rf_proj_probe) p ON b.id = p.k; +SELECT anser_rf_nodes('SELECT count(p.k) + FROM anser_rf_proj_build b + LEFT JOIN (SELECT id * 2 AS k FROM anser_rf_proj_probe) p ON b.id = p.k') > 0 AS expr_injected; RESET gp_enable_runtime_filter_pushdown; DROP TABLE anser_rf_proj_build, anser_rf_proj_probe; SET optimizer = off; @@ -190,9 +224,115 @@ $$; SELECT 'an inner statement dropped the running query''s filter' AS problem WHERE anser_rf_pruned('SELECT anser_rf_nested(p.id) FROM anser_rf_build b JOIN anser_rf_probe p ON b.id = p.id') = 0; + +-- Narrow build slices must still publish. part_index is only diagnostic, so +-- a slice narrower than the cluster must not reject the only producer there +-- is: direct dispatch to a non-zero segment (segindex >= total_parts) or the +-- entry-DB (segindex = -1) both used to leave the channel a part short and +-- every consumer waiting out anser.timeout_ms. The block also asserts the +-- count, but the load-bearing assertion is that rows were pruned at all. +SET anser.timeout_ms = 100; +CREATE TABLE anser_rf_dd_build (id int, dk text, pad text) DISTRIBUTED BY (dk); +CREATE TABLE anser_rf_dd_probe (id int) DISTRIBUTED BY (id); +INSERT INTO anser_rf_dd_build SELECT g, 'k' || (g % 6), 'b' || g FROM generate_series(1, 1000) g; +INSERT INTO anser_rf_dd_probe SELECT g % 1000 + 1 FROM generate_series(1, 100000) g; +ANALYZE anser_rf_dd_build; +ANALYZE anser_rf_dd_probe; + +-- Check every distribution key, not a chosen one: on any cluster with more +-- than one segment at least one of them lands with segindex >= 1, which is +-- the shape that used to stall -- the direct-dispatched producer was refused +-- (segindex >= total_parts) and every consumer waited out anser.timeout_ms. +-- The discriminating observable is that rows were pruned at all; result +-- counts are identical either way, since a broken exchange fails open. +SET anser.runtime_filter = on; +SELECT anser_rf_pruned('SELECT count(*) FROM anser_rf_dd_probe p JOIN anser_rf_dd_build b ON p.id = b.id WHERE b.dk = ''k0''') > 0 AS k0_pruned; +SELECT anser_rf_pruned('SELECT count(*) FROM anser_rf_dd_probe p JOIN anser_rf_dd_build b ON p.id = b.id WHERE b.dk = ''k1''') > 0 AS k1_pruned; +SELECT anser_rf_pruned('SELECT count(*) FROM anser_rf_dd_probe p JOIN anser_rf_dd_build b ON p.id = b.id WHERE b.dk = ''k2''') > 0 AS k2_pruned; +SELECT anser_rf_pruned('SELECT count(*) FROM anser_rf_dd_probe p JOIN anser_rf_dd_build b ON p.id = b.id WHERE b.dk = ''k3''') > 0 AS k3_pruned; +SELECT anser_rf_pruned('SELECT count(*) FROM anser_rf_dd_probe p JOIN anser_rf_dd_build b ON p.id = b.id WHERE b.dk = ''k4''') > 0 AS k4_pruned; +SELECT anser_rf_pruned('SELECT count(*) FROM anser_rf_dd_probe p JOIN anser_rf_dd_build b ON p.id = b.id WHERE b.dk = ''k5''') > 0 AS k5_pruned; + +-- Entry-DB build side: a catalog scan runs on the entry-DB QE, whose +-- segindex is -1 -- 0xFFFFFFFF as a part index, the same rejection. +CREATE TABLE anser_rf_eb_probe (o oid) DISTRIBUTED BY (o); +INSERT INTO anser_rf_eb_probe + SELECT 'anser_rf_eb_probe'::regclass::oid FROM generate_series(1, 10) g + UNION ALL + SELECT 4000000000 + g FROM generate_series(1, 100000) g; +ANALYZE anser_rf_eb_probe; +-- The count uses only the matching rows, so it is exact: the table's own oid +-- exists in pg_class, ten times. +SELECT count(*) AS eb_rows + FROM (SELECT 'anser_rf_eb_probe'::regclass::oid AS o FROM generate_series(1, 10) g) p + JOIN pg_class c ON p.o = c.oid; +SELECT 'entry-DB producer did not publish (consumer failed open)' AS problem + WHERE anser_rf_pruned('SELECT count(*) FROM anser_rf_eb_probe p JOIN pg_class c ON p.o = c.oid') = 0; +DROP TABLE anser_rf_dd_build, anser_rf_dd_probe, anser_rf_eb_probe; +RESET anser.timeout_ms; + DROP FUNCTION anser_rf_nested(int); DROP FUNCTION anser_rf_pruned(text); +-- Filter sizing follows the estimate, so exercising a production-size payload +-- with test-size data goes through a debug knob instead: force a 64 MB bitset +-- for a 200-row build side, and the full-size filter has to actually exchange +-- (the serialized part rides the wire to every consumer). +SET anser.debug_min_filter_bytes = 67108864; +SET anser.runtime_filter = on; +EXPLAIN (COSTS OFF) +SELECT b.name, p.payload FROM anser_rf_build b LEFT JOIN anser_rf_probe p ON b.id = p.id; + +-- A plain EXPLAIN must not allocate the filter: the coordinator initializes +-- every slice with EXEC_FLAG_EXPLAIN_ONLY, so the producer must skip building +-- the bitset (state=NULL) while the plan still shows the planned size. The +-- debug log makes the decision observable. +SET anser.debug = on; +SET client_min_messages = log; +EXPLAIN (COSTS OFF) +SELECT b.name, p.payload FROM anser_rf_build b LEFT JOIN anser_rf_probe p ON b.id = p.id; +RESET client_min_messages; +RESET anser.debug; + +-- And the filter must still be correct: 200 build rows, each matching one +-- probe row. +SELECT count(*) AS rows_joined + FROM anser_rf_build b LEFT JOIN anser_rf_probe p ON b.id = p.id; + +-- The floor must change the real filter, not just the plan: sizing raises the +-- declared element count to match the forced bitset size, so producer and +-- consumer really build a 64 MB filter instead of displaying one. +CREATE EXTENSION IF NOT EXISTS anser_test; +SELECT injected, total_elems, planned_bytes FROM anser_test_rf_size(200); +DROP EXTENSION anser_test; + +-- A LIMIT can finish the query while a consumer is still waiting for its +-- filter: the producer is squelched before it publishes, the consumer must +-- bail out via QueryFinishPending and withdraw its subscription, and the +-- result must match the unfiltered run. +SELECT count(*) AS limited_rows FROM ( + SELECT b.name, p.payload FROM anser_rf_build b LEFT JOIN anser_rf_probe p ON b.id = p.id + LIMIT 10) q; + +-- An open cursor must be undisturbed by statements between its FETCHes: the +-- filter exchange completes inside the FETCH's ExecutorRun window, so a +-- sibling statement's ExecutorEnd sweep has nothing of the cursor's to wipe. +-- The sibling runs the same filtered join, so it really does create and sweep +-- its own channels while the cursor's gang sits suspended. (A wiped exchange +-- would fail open with the same rows, so this guards the behavior, not the +-- timing: that path is a wait, not a wrong answer.) +SET optimizer = off; +SET anser.runtime_filter = on; +BEGIN; +DECLARE anser_rf_cur CURSOR FOR + SELECT b.name, p.payload FROM anser_rf_build b LEFT JOIN anser_rf_probe p ON b.id = p.id + WHERE b.id <= 3 ORDER BY b.name; +FETCH 1 anser_rf_cur; +-- The ExecutorEnd of this statement must leave the open cursor's state alone. +SELECT count(*) AS sibling_stmt FROM anser_rf_build b LEFT JOIN anser_rf_probe p ON b.id = p.id; +FETCH ALL anser_rf_cur; +COMMIT; + DROP TABLE anser_rf_r_push; DROP TABLE anser_rf_r_on, anser_rf_r_off, anser_rf_r_orca; DROP TABLE anser_rf_build, anser_rf_probe; @@ -251,6 +391,38 @@ SELECT count(*) AS only_off DROP TABLE anser_rf_z_on, anser_rf_z_off, anser_rf_build_z, anser_rf_probe_z; +-- The exclusion has to hold through a domain. A domain inherits typlen and +-- typbyval from its base type, so it looks by-value, and its OID is not one of +-- the float OIDs the exclusion names -- a check on the declared type alone lets +-- it straight through and -0.0 would stop matching 0.0. Both sides are +-- distributed on their text column so the join needs a Motion and is a +-- candidate for injection; the key type is what has to refuse it. +CREATE DOMAIN anser_rf_f8 AS float8; +CREATE TABLE anser_rf_build_d (id anser_rf_f8, name text) DISTRIBUTED BY (name); +CREATE TABLE anser_rf_probe_d (id anser_rf_f8, payload text) DISTRIBUTED BY (payload); +INSERT INTO anser_rf_build_d VALUES (1, 'b1'), (-0.0, 'bz'); +INSERT INTO anser_rf_probe_d VALUES (1, 'p1'), (0.0, 'pz'), (2, 'p2'); +ANALYZE anser_rf_build_d; +ANALYZE anser_rf_probe_d; + +SET anser.runtime_filter = on; +CREATE TEMP TABLE anser_rf_d_on AS + SELECT b.name, p.payload FROM anser_rf_build_d b JOIN anser_rf_probe_d p ON b.id = p.id + DISTRIBUTED BY (name); +SET anser.runtime_filter = off; +CREATE TEMP TABLE anser_rf_d_off AS + SELECT b.name, p.payload FROM anser_rf_build_d b JOIN anser_rf_probe_d p ON b.id = p.id + DISTRIBUTED BY (name); + +SELECT count(*) AS rows_on FROM anser_rf_d_on; +SELECT count(*) AS only_on + FROM (SELECT * FROM anser_rf_d_on EXCEPT ALL SELECT * FROM anser_rf_d_off) d; +SELECT count(*) AS only_off + FROM (SELECT * FROM anser_rf_d_off EXCEPT ALL SELECT * FROM anser_rf_d_on) d; + +DROP TABLE anser_rf_d_on, anser_rf_d_off, anser_rf_build_d, anser_rf_probe_d; +DROP DOMAIN anser_rf_f8; + -- Parameterized rescan guard: a build side whose rows depend on a parameter an -- enclosing nested loop reassigns yields a different key set per outer tuple, -- and neither node rebuilds on rescan -- the producer stays published, the @@ -263,7 +435,7 @@ DROP TABLE anser_rf_z_on, anser_rf_z_off, anser_rf_build_z, anser_rf_probe_z; -- makes a stale filter visible: with the first group's filter still in place -- the second group matches nothing. CREATE TABLE anser_rf_nl_outer (g int) DISTRIBUTED REPLICATED; -CREATE TABLE anser_rf_nl_build (g int, id int) DISTRIBUTED BY (id); +CREATE TABLE anser_rf_nl_build (g int, id int) DISTRIBUTED BY (g); CREATE TABLE anser_rf_nl_probe (id int) DISTRIBUTED BY (id); INSERT INTO anser_rf_nl_outer VALUES (1), (2); INSERT INTO anser_rf_nl_build SELECT 1, g FROM generate_series(1, 100) g; @@ -273,21 +445,6 @@ ANALYZE anser_rf_nl_outer; ANALYZE anser_rf_nl_build; ANALYZE anser_rf_nl_probe; -CREATE FUNCTION anser_rf_nodes(q text) RETURNS bigint -LANGUAGE plpgsql AS $$ -DECLARE - line text; - nodes bigint := 0; -BEGIN - FOR line IN EXECUTE 'EXPLAIN (COSTS OFF) ' || q LOOP - IF line ~ 'Anser Bloom' THEN - nodes := nodes + 1; - END IF; - END LOOP; - RETURN nodes; -END; -$$; - -- Both groups must report 100, with the filter on and off alike, and the plan -- must carry no Anser node at all. SET anser.runtime_filter = off; @@ -318,38 +475,7 @@ SELECT anser_rf_nodes('SELECT count(*) FROM anser_rf_nl_probe p DROP FUNCTION anser_rf_nodes(text); DROP TABLE anser_rf_nl_outer, anser_rf_nl_build, anser_rf_nl_probe; --- nextval guard: the QD sends two kinds of message down a QE's dispatch --- connection -- the nextval reply and a sideband message -- and each has its --- own reader. A reader that meets the other's message has to skip it whole: --- taking the type byte and erroring leaves the body in the stream, and the --- session then dies somewhere else with "protocol synchronization was lost". --- --- In the field this needs an Anser consumer to give up on a filter the --- coordinator goes on to deliver anyway, which is a race and cannot be asked --- for from SQL. The fault puts a sideband message in front of one nextval --- reply instead, so the ordering is certain rather than likely; the --- three-argument form is occurrence 1..1, so it fires once. --- --- Nothing here is Anser's -- the fault supplies the message and the fix is in --- cdb_sequence_nextval_qe -- but Anser is why a second message type exists on --- that connection at all, so the test lives with it. --- --- The extension may or may not already be there, and saying so is not part of --- what is being tested; it is never dropped, for the same reason -- it can --- predate this test, and other tests in the same database rely on it. --- start_ignore -CREATE EXTENSION IF NOT EXISTS gp_inject_fault; --- end_ignore -CREATE SEQUENCE anser_rf_seq; -CREATE TABLE anser_rf_seq_t (id int) DISTRIBUTED BY (id); -INSERT INTO anser_rf_seq_t SELECT g FROM generate_series(1, 10) g; -SELECT gp_inject_fault('nextval_sideband_before_response', 'skip', dbid) - FROM gp_segment_configuration WHERE role = 'p' AND content = -1; -SELECT count(nextval('anser_rf_seq')) AS seq_rows FROM anser_rf_seq_t; -SELECT gp_inject_fault('nextval_sideband_before_response', 'reset', dbid) - FROM gp_segment_configuration WHERE role = 'p' AND content = -1; -DROP SEQUENCE anser_rf_seq; -DROP TABLE anser_rf_seq_t; + -- --------------------------------------------------------------------------- -- Parallel execution. @@ -364,12 +490,15 @@ DROP TABLE anser_rf_seq_t; -- which disagree here: for one query the Postgres planner may give a serial -- build slice where GPORCA gives a parallel one. -- --- The join key is the distribution key on both sides, so no Motion is needed --- to co-locate the join. That is the only shape either planner parallelises: --- a redistributed or broadcast build side is fine for GPORCA but the Postgres --- planner marks Motion paths parallel-unsafe. +-- The build side is distributed by v and joined on id, so it is redistributed: +-- Anser only injects across a Motion, and a co-located join -- which is what +-- this used to use -- is now skipped outright, making the whole section +-- vacuous. The cost is that only GPORCA still parallelises it; the Postgres +-- planner marks Motion paths parallel-unsafe, so its two blocks below are +-- reduced to a filter-on/filter-off equivalence check on a serial slice. +-- They are kept because that equivalence is worth asserting on its own. -- --------------------------------------------------------------------------- -CREATE TABLE anser_rf_par_build (id int, v text) DISTRIBUTED BY (id); +CREATE TABLE anser_rf_par_build (id int, v text) DISTRIBUTED BY (v); CREATE TABLE anser_rf_par_probe (id int, pad text) DISTRIBUTED BY (id); INSERT INTO anser_rf_par_build SELECT g, 'b' || g FROM generate_series(1, 500) g; INSERT INTO anser_rf_par_probe SELECT g, 'p' || g FROM generate_series(1, 20000) g; @@ -478,7 +607,96 @@ RESET parallel_setup_cost; RESET parallel_tuple_cost; RESET max_parallel_workers_per_gather; +-- A probe side under a WindowAgg must not be filtered: the window is computed +-- over the whole input, so a consumer on the base scan below it would change +-- the result. The resolver must refuse the descent (the node whitelist +-- applies to the probe-side top too) and inject nothing. +SET optimizer = off; +SET anser.runtime_filter = on; +CREATE TABLE anser_rf_win (k int8, pad int) DISTRIBUTED BY (pad); +INSERT INTO anser_rf_win SELECT i % 1000, i FROM generate_series(1, 100000) i; +ANALYZE anser_rf_win; +-- A temp table is scanned on the coordinator, so the build side runs in the +-- coordinator's slice while the probe side runs on the segments: the slices +-- differ and the join would inject if the WindowAgg did not refuse it. k=5 +-- exists in the table (k = i % 1000, a hundred times), so the row matches and +-- the window total must be the full 100000 -- the windowed side returns every +-- row with the total attached, hence the max(). +CREATE TEMP TABLE anser_rf_dim (k int8) DISTRIBUTED BY (k); +INSERT INTO anser_rf_dim VALUES (5); +SELECT max(s.total) AS window_total +FROM anser_rf_dim d +LEFT JOIN (SELECT k, count(*) OVER () AS total FROM anser_rf_win) s + ON s.k = d.k; +DROP TABLE anser_rf_dim; +DROP TABLE anser_rf_win; + +-- IS NOT DISTINCT FROM below an outer join: ORCA puts a plain `=` in +-- hashclauses and the null-safety test in hashqualclauses, so the join passes +-- the equality gate, and the consumer used to land on the nullable side of +-- the lower LEFT JOIN. Every row it dropped came back NULL-extended and then +-- matched the build side's NULL under INDF -- 99997 rows instead of 0. The +-- resolver must now refuse the descent into the nullable side, so no filter +-- is injected and the count stays 0. +SET optimizer = on; +SET gp_enable_runtime_filter = off; +SET anser.runtime_filter = on; +CREATE TABLE anser_rf_s (x int, y int) DISTRIBUTED BY (x); +CREATE TABLE anser_rf_r (x int, k int) DISTRIBUTED BY (x); +CREATE TABLE anser_rf_b (k int) DISTRIBUTED RANDOMLY; +INSERT INTO anser_rf_s SELECT g, g FROM generate_series(1, 100000) g; +INSERT INTO anser_rf_r SELECT g, g + 1000000 FROM generate_series(1, 100000) g; +INSERT INTO anser_rf_b VALUES (NULL); +INSERT INTO anser_rf_b SELECT g FROM generate_series(1, 10) g; +ANALYZE anser_rf_s; +ANALYZE anser_rf_r; +ANALYZE anser_rf_b; +SELECT count(*) AS indf_rows FROM (anser_rf_s s LEFT JOIN anser_rf_r r ON s.x = r.x) + JOIN anser_rf_b b ON r.k IS NOT DISTINCT FROM b.k; + +-- Plain two-table INDF remains sound to filter: the bloom holds the build +-- side's non-NULL keys and NULL probe keys pass through, so matches and the +-- NULL row must all survive -- 1 and 2 are in b (1..10), NULL matches b's. +CREATE TABLE anser_rf_m (k int) DISTRIBUTED RANDOMLY; +INSERT INTO anser_rf_m VALUES (1), (2), (NULL); +SELECT count(*) AS indf_simple_rows FROM anser_rf_m m + JOIN anser_rf_b b ON m.k IS NOT DISTINCT FROM b.k; +DROP TABLE anser_rf_m, anser_rf_s, anser_rf_r, anser_rf_b; + +-- A computed probe key must refuse the filter: ORCA evaluates the cast in +-- the scan's projection (Output: (big)::oid), so the consumer would hash the +-- zero-extended computed oid while the producer hashes the sign-extended +-- stored oid -- every oid >= 2^31 mismatches, and only false positives pass +-- (3 rows instead of 50). The SeqScan shortcut must apply the same +-- plain-Var check as the resolver, so no consumer is injected. +CREATE TABLE anser_rf_p (id int, big int8) DISTRIBUTED BY (id); +CREATE TABLE anser_rf_o (o oid) DISTRIBUTED RANDOMLY; +INSERT INTO anser_rf_p SELECT g, 3000000000 + g FROM generate_series(1, 100000) g; +INSERT INTO anser_rf_o SELECT (3000000000 + g)::oid FROM generate_series(1, 50) g; +ANALYZE anser_rf_p; +ANALYZE anser_rf_o; +SELECT count(*) AS cast_rows + FROM (SELECT id, big::oid AS o FROM anser_rf_p) p + JOIN anser_rf_o b ON b.o = p.o; +DROP TABLE anser_rf_p, anser_rf_o; +RESET gp_enable_runtime_filter; +-- Resource and log knobs are superuser-only: an unprivileged role must not +-- force 64 MB filters on every segment or fill the server log. runtime_filter +-- and timeout_ms only shape the caller's own queries, so they stay +-- user-settable. +CREATE ROLE anser_nopriv LOGIN; +SET ROLE anser_nopriv; +SET anser.debug_min_filter_bytes = 67108864; +SET anser.max_info_size = 100000000; +SET anser.debug = on; +SET anser.runtime_filter = on; +SET anser.timeout_ms = 100; +RESET ROLE; +DROP ROLE anser_nopriv; + RESET anser.runtime_filter; +RESET anser.debug_min_filter_bytes; +RESET anser.timeout_ms; RESET optimizer; RESET enable_nestloop; RESET enable_mergejoin; diff --git a/gpcontrib/anser/sql/anser_test.sql b/gpcontrib/anser/sql/anser_test.sql index dfdd08fb8c8..37878e5a946 100644 --- a/gpcontrib/anser/sql/anser_test.sql +++ b/gpcontrib/anser/sql/anser_test.sql @@ -30,7 +30,9 @@ SET anser.max_info_size = 68157440; -- 1. The planner's gate -- the only one that costs nothing, since nothing is -- injected and no consumer is left waiting. A 64 MB bitset is 536870912 bits, -- so 134217728 estimated keys is exactly 4 bits/key and one more key is not. --- A zero or negative estimate is treated as one key, not as "no filter". +-- A zero or negative estimate is treated as one key, not as "no filter"; the +-- 1 MB bitset floor then raises the declared count to match, so every estimate +-- the floor covers shows the same 524288 elements. SELECT t.est_rows, s.injected, s.total_elems, s.planned_bytes FROM (VALUES (0), (-5), (1), (1000), (1000000), (10000000), (134217728), (134217729), (150000000), (1000000000)) @@ -165,21 +167,25 @@ FROM (VALUES -- The QD -> QE checksum. The value itself does not matter; which inputs -- change it does. Every routing field and the key are always covered, and the --- body only for a payload type that asks for it -- the fifth column is that --- deliberate exemption, not an oversight. +-- body only for a payload type that asks for it -- the other_body_exempt column +-- is that deliberate exemption, not an oversight. SELECT - anser_test_push_crc('B'::"char", 1, 0, 'k', '\x01') - <> anser_test_push_crc('B'::"char", 2, 0, 'k', '\x01') AS condition_covered, - anser_test_push_crc('B'::"char", 1, 0, 'k', '\x01') - <> anser_test_push_crc('B'::"char", 1, 1, 'k', '\x01') AS flags_covered, - anser_test_push_crc('B'::"char", 1, 0, 'k', '\x01') - <> anser_test_push_crc('B'::"char", 1, 0, 'j', '\x01') AS key_covered, - anser_test_push_crc('B'::"char", 1, 0, 'k', '\x01') - <> anser_test_push_crc('B'::"char", 1, 0, 'k', '\x02') AS bloom_body_covered, - anser_test_push_crc('-'::"char", 1, 0, 'k', '\x01') - = anser_test_push_crc('-'::"char", 1, 0, 'k', '\x02') AS other_body_exempt, - anser_test_push_crc('B'::"char", 1, 0, 'k', '\x01') - <> anser_test_push_crc('-'::"char", 1, 0, 'k', '\x01') AS type_covered; + anser_test_push_crc('B'::"char", 1, 7, 3, 0, 'k', '\x01') + <> anser_test_push_crc('B'::"char", 2, 7, 3, 0, 'k', '\x01') AS condition_covered, + anser_test_push_crc('B'::"char", 1, 7, 3, 0, 'k', '\x01') + <> anser_test_push_crc('B'::"char", 1, 8, 3, 0, 'k', '\x01') AS session_covered, + anser_test_push_crc('B'::"char", 1, 7, 3, 0, 'k', '\x01') + <> anser_test_push_crc('B'::"char", 1, 7, 4, 0, 'k', '\x01') AS command_covered, + anser_test_push_crc('B'::"char", 1, 7, 3, 0, 'k', '\x01') + <> anser_test_push_crc('B'::"char", 1, 7, 3, 1, 'k', '\x01') AS flags_covered, + anser_test_push_crc('B'::"char", 1, 7, 3, 0, 'k', '\x01') + <> anser_test_push_crc('B'::"char", 1, 7, 3, 0, 'j', '\x01') AS key_covered, + anser_test_push_crc('B'::"char", 1, 7, 3, 0, 'k', '\x01') + <> anser_test_push_crc('B'::"char", 1, 7, 3, 0, 'k', '\x02') AS bloom_body_covered, + anser_test_push_crc('-'::"char", 1, 7, 3, 0, 'k', '\x01') + = anser_test_push_crc('-'::"char", 1, 7, 3, 0, 'k', '\x02') AS other_body_exempt, + anser_test_push_crc('B'::"char", 1, 7, 3, 0, 'k', '\x01') + <> anser_test_push_crc('-'::"char", 1, 7, 3, 0, 'k', '\x01') AS type_covered; -- 5. The runtime-filter pushdown decision. A scan key is numbered by the -- table's attributes, while the consumer is numbered by the scan's output @@ -224,4 +230,44 @@ FROM (VALUES LATERAL anser_test_pushdown_target(t.tlist, t.attno, true, t.as_seqscan) AS s; +-- 7. How many producers a slice runs. This number is the channel's +-- completion criterion, so over-counting costs every consumer the full +-- anser.timeout_ms -- the channel never reaches the count it waits for and +-- nobody is told -- while under-counting completes it early and hands out a +-- filter missing a segment's keys, which drops rows that join. So each of +-- FillSliceGangInfo's four formulas is pinned here, not inferred from one. +-- +-- numsegments is only meaningful for a reader or writer gang; the columns are +-- still filled for the other types to show they are ignored. +SELECT t.what, t.gang, t.nseg, t.workers, t.direct, + anser_test_slice_producers(t.gang, t.nseg, t.workers, t.direct, 0) + AS producers +FROM (VALUES + ('coordinator slice', 'unallocated', 3, 0, NULL::int4[]), + ('coordinator slice, parallel', 'unallocated', 3, 4, NULL), + ('entry db reader', 'entrydb', 3, 0, NULL), + ('singleton reader', 'singleton', 3, 0, NULL), + ('singleton reader, parallel', 'singleton', 3, 4, NULL), + ('reader, serial', 'reader', 3, 0, NULL), + ('reader, parallel', 'reader', 3, 4, NULL), + ('writer, serial', 'writer', 3, 0, NULL), + ('direct dispatch, one segment', 'reader', 3, 0, '{1}'), + ('direct dispatch, two, serial', 'reader', 3, 0, '{0,2}'), + ('direct dispatch, two, par 4', 'reader', 3, 4, '{0,2}'), + ('direct dispatch, nowhere', 'reader', 3, 4, '{}'), + ('no segments', 'reader', 0, 4, NULL), + ('negative segments', 'reader', -1, 4, NULL) + ) AS t(what, gang, nseg, workers, direct); + +-- The slice has to be identifiable at all; anything else is zero, which the +-- injection pass reads as "unknown" and declines on. +SELECT t.what, anser_test_slice_producers('reader', 3, 4, NULL, t.idx) + AS producers +FROM (VALUES + ('slice 0 of 1 exists', 0), + ('slice 1 of 1 does not', 1), + ('negative slice index', -1) + ) AS t(what, idx); + +RESET anser.max_info_size; DROP EXTENSION anser_test; diff --git a/gpcontrib/anser/src/anserbloomconsume.c b/gpcontrib/anser/src/anserbloomconsume.c index 694959efd2f..1f3706f27e9 100644 --- a/gpcontrib/anser/src/anserbloomconsume.c +++ b/gpcontrib/anser/src/anserbloomconsume.c @@ -39,9 +39,9 @@ /* * State for one Bloom filter consumer. Consumes the merged payload for a - * channel exactly once (consumed), either from the coordinator's shared - * coordinator. cancelled records that the producer side aborted instead of - * delivering the payload. + * channel exactly once (consumed), either from the coordinator's channel table + * (local path) or from the dispatch sideband connection. cancelled records + * that the producer side aborted instead of delivering the payload. */ struct AnserBloomFilterConsumeState { @@ -50,8 +50,8 @@ struct AnserBloomFilterConsumeState int64 total_elems; /* filter sizing, shared with the producer */ Size max_payload_bytes; uint64 seed; - uint32 expected_parts; - uint32 received_parts; + uint32 expected_parts; /* producers the plan says to wait for */ + uint32 received_parts; /* the same count, as the payload declares it */ bool consumed; bool cancelled; }; @@ -149,22 +149,26 @@ ExecAnserBloomFilterConsumeSideband(AnserBloomFilterConsumeState *state, * Turn a merged payload into this consumer's filter. * * The coordinator unions every segment's part into one before delivery, so a - * single chunk is deserialized rather than N; the merged header's total_parts - * records how many were folded, which we surface as the received count. + * single chunk is deserialized rather than N. The header's total_parts is how + * many producers the channel was waiting for -- every part carries the same + * number and folding leaves it alone -- so it is the payload's own account of + * the count this consumer was planned with, not a tally of what arrived. How + * many parts were folded is known only to the coordinator, which does not + * deliver until it has them all. */ static bool ExecAnserBloomFilterConsumeFinish(AnserBloomFilterConsumeState *state, void *payload, Size payload_len) { uint32 part_index = 0; - uint32 folded = 0; + uint32 declared_parts = 0; state->filter = AnserBloomDeserializePart(payload, payload_len, state->total_elems, state->max_payload_bytes, state->seed, - &part_index, &folded); - state->received_parts = (state->filter != NULL) ? folded : 0; + &part_index, &declared_parts); + state->received_parts = (state->filter != NULL) ? declared_parts : 0; if (payload != NULL) pfree(payload); diff --git a/gpcontrib/anser/src/anserdispatch.c b/gpcontrib/anser/src/anserdispatch.c index a48e3a0d1f7..a7e9a4fb4a6 100644 --- a/gpcontrib/anser/src/anserdispatch.c +++ b/gpcontrib/anser/src/anserdispatch.c @@ -66,11 +66,15 @@ #include "cdb/cdbdisp.h" #include "cdb/cdbdispatchresult.h" #include "cdb/cdbvars.h" +#include "storage/latch.h" +#include "utils/faultinjector.h" #include "common/base64.h" #include "lib/stringinfo.h" #include "nodes/pg_list.h" #include "utils/hsearch.h" #include "utils/memutils.h" +#include "utils/timestamp.h" +#include "utils/wait_event.h" /* * One channel's merge state, living for the duration of the query that created @@ -90,6 +94,36 @@ typedef struct AnserDispChannel } AnserDispChannel; static HTAB *AnserDispChannels = NULL; + +/* + * Connections holding a partially written delivery. A merged filter is + * 1-64 MB and a consumer that has stopped reading cannot take it in one + * flush, so a push can leave bytes queued (anser_disp_push). Nothing more + * can be queued once the query ends -- deliveries only happen mid-query, and + * the channels are dropped at ExecutorEnd -- so the set is final then, and + * AnserDispatchFlushPending() completes or poisons it before the gang is + * reused. List nodes live in TopMemoryContext: the list must survive until + * ExecutorEnd regardless of the context a delivery ran in. + * + * Invariant the safety of this list stands on: its connections are + * dereferenced ONLY in AnserDispatchFlushPending(), and that call runs before + * any gang recycling in every teardown order (standard ExecutorEnd recycles + * after it; AtAbort_Portals reaches ExecutorEnd before + * AtAbort_DispatcherState). The only other place it is touched is + * AnserDispatchReset(), which frees the list nodes without dereferencing the + * connections. Any future path that frees a QE connection before ExecutorEnd + * invalidates this comment and must be handled here instead. + */ +static List *AnserDispPendingPush = NIL; + +/* + * Withdrawals honoured since the backend started. Only a test reads it: an + * unsubscribe leaves no other trace, because the channel it touches is gone + * by the time the query returns. Counting them is how we tell "the consumer + * withdrew and the coordinator acted on it" from "the filter happened to + * arrive in time", which look identical from SQL. + */ +static int64 AnserDispUnsubscribes = 0; static MemoryContext AnserDispContext = NULL; static AnserDispChannel *anser_disp_lookup(const AnserChannelKey *key, bool create); @@ -120,6 +154,22 @@ anser_conn_error(const PGconn *conn) return conn->errorMessage.data; } +/* PQsocket() equivalent; same linkage note as above. */ +static inline int +anser_conn_socket(const PGconn *conn) +{ + return conn->sock; +} + +/* + * How many withdrawals this backend has honoured. For tests; see the counter. + */ +int64 +AnserDispatchUnsubscribeCount(void) +{ + return AnserDispUnsubscribes; +} + /* * Per-query state lives in its own context so it can be dropped wholesale. * Channels are keyed by (session, command, condition), so entries from an @@ -150,6 +200,16 @@ anser_disp_init(void) void AnserDispatchReset(void) { + /* + * Drop the pending-delivery list too, but only the list nodes: the + * connections it names may already be dead on the abort path, and + * dereferencing them is reserved to AnserDispatchFlushPending (see the + * invariant at the declaration). A delivery dropped here unflushed is + * the cleanupQE guard's problem, which poisons the connection at recycle. + */ + list_free(AnserDispPendingPush); + AnserDispPendingPush = NIL; + if (AnserDispChannels == NULL) return; @@ -159,6 +219,128 @@ AnserDispatchReset(void) AnserDispContext = NULL; } +/* + * Drop the finished channels of one command, leaving every other query's + * alone. + * + * AnserDispatchReset() is wholesale, so it can only run once nothing else is + * executing. Inside SPI that is not until the outermost statement ends, which + * holds every inner query's merged filter -- at least anser.max_info_size each + * -- for the whole call. The key carries the command count, so a query can + * take its own channels with it instead of waiting for that sweep. + * + * Only channels that are complete and have no subscriber left are taken, + * because the caller cannot always prove the count is its own: gp_command_count + * names the newest query descriptor, not the one that is ending, and the two + * differ whenever a portal outlives the statements that ran after it. Closing + * such a cursor from inside a live query would otherwise hand us that live + * query's count. A channel that is complete and already delivered costs at + * most a consumer that had not subscribed yet, which fails open; an exchange + * still in flight is never touched. + * + * The flip side is that a channel that never completed -- a squelched producer, + * say -- still waits for the sweep. + */ +void +AnserDispatchResetCommand(int command_count) +{ + HASH_SEQ_STATUS status; + AnserDispChannel *chan; + + if (AnserDispChannels == NULL) + return; + + hash_seq_init(&status, AnserDispChannels); + while ((chan = (AnserDispChannel *) hash_seq_search(&status)) != NULL) + { + if (chan->key.gp_command_count != command_count) + continue; + if (!chan->complete || chan->subscribers != NIL) + continue; + + /* + * AnserDispContext outlives this, so free what the entry owns rather + * than the context. Deleting the entry the scan just handed back is + * explicitly allowed; see hash_seq_search(). + */ + if (chan->payload != NULL) + pfree(chan->payload); + + if (hash_search(AnserDispChannels, &chan->key, + HASH_REMOVE, NULL) == NULL) + elog(ERROR, "anser: dispatch channel hash table corrupted"); + } +} + +/* + * Complete or poison partially written deliveries before the query's gang is + * reused. + * + * A connection on the pending list holds a torn sideband message: part of it + * was sent, the rest is still queued, and the dispatcher's poll() stopped + * flushing when the peer's command completed. Left alone, the next dispatch + * on the gang would overwrite the queued bytes (PQsendGpQuery_shared) and the + * QE -- which has the message header already -- would wait forever for a body + * that never comes. + * + * Nothing more can be queued once the query ends, so the set is final and + * each connection gets one bounded attempt: the peer in this situation is a + * live idle reader (it is blocked mid-message or in ReadCommand), so the + * flush normally completes and the QE absorbs the stale delivery. A peer + * that stays unwritable past the deadline is gone or stuck; its connection is + * marked bad so the dispatcher discards it rather than reusing it -- the QE + * sees EOF and exits, and the next gang build reconnects (cdbgang_async.c + * does not treat a bad connection as cached). + */ +void +AnserDispatchFlushPending(long timeout_ms) +{ + ListCell *lc; + + foreach(lc, AnserDispPendingPush) + { + PGconn *conn = (PGconn *) lfirst(lc); + TimestampTz start = GetCurrentTimestamp(); + + while (conn->outCount > 0) + { + /* + * Bail on cancel or finish: teardown must not linger. No + * CHECK_FOR_INTERRUPTS throw -- an interrupted flush degrades to + * poisoning the connection below, never to an error raised this + * late in the query's life. + */ + if (QueryCancelPending || QueryFinishPending) + break; + + if (pqFlushNonBlocking(conn) < 0) + break; /* libpq already logged; marked bad below */ + if (conn->outCount == 0) + break; + if (TimestampDifferenceExceeds(start, GetCurrentTimestamp(), + timeout_ms)) + break; + + (void) WaitLatchOrSocket(MyLatch, + WL_LATCH_SET | WL_SOCKET_WRITEABLE | + WL_TIMEOUT | WL_EXIT_ON_PM_DEATH, + anser_conn_socket(conn), 100L /* ms */ , + PG_WAIT_EXTENSION); + ResetLatch(MyLatch); + } + + if (conn->outCount > 0) + { + elog(LOG, "anser: dropping a connection with %d unsent bytes of a delivered filter", + conn->outCount); + conn->status = CONNECTION_BAD; + } + } + + list_free(AnserDispPendingPush); + AnserDispPendingPush = NIL; +} + static AnserDispChannel * anser_disp_lookup(const AnserChannelKey *key, bool create) { @@ -274,6 +456,27 @@ AnserDispatchNotifyHandler(struct CdbDispatchResult *dispatchResult, else chan->subscribers = lappend(chan->subscribers, conn); } + else if (msg.kind == ANSER_WIRE_KIND_UNSUBSCRIBE) + { + PGconn *conn = dr->segdbDesc->conn; + + /* + * The consumer ran out of time and is no longer reading for us. + * Delivering anyway would put 1-64 MB on a connection whose other end + * is busy elsewhere, and the message would then have to be recognised + * and skipped by whatever reads it next. + * + * This narrows the window rather than closing it: the withdrawal can + * arrive while delivery is already under way, which is why the push + * never blocks and why every reader of this connection still has to + * tolerate a message that is not its own. + */ + ANSER_DEBUG("anser: QD unsubscribe cond=%u from seg%d (%d subscriber(s) before)", + msg.key.condition_id, sender, + list_length(chan->subscribers)); + chan->subscribers = list_delete_ptr(chan->subscribers, conn); + AnserDispUnsubscribes++; + } else if (msg.kind == ANSER_WIRE_KIND_PART) { char *raw = NULL; @@ -445,6 +648,15 @@ anser_disp_deliver(AnserDispChannel *chan) { ListCell *lc; + /* + * Hold the delivery back, so a test can let every consumer reach its + * deadline first and then be delivered to anyway. That ordering is what + * puts a filter on a connection nobody is reading, and it cannot be + * arranged with data: the channel completes when the last part lands, + * which is normally while the consumers are still waiting for it. + */ + SIMPLE_FAULT_INJECTOR("anser_before_deliver"); + ANSER_DEBUG("anser: QD delivering cond=%u to %d subscriber(s)", chan->key.condition_id, list_length(chan->subscribers)); foreach(lc, chan->subscribers) @@ -481,31 +693,70 @@ anser_disp_push(PGconn *conn, AnserDispChannel *chan) */ paytype = chan->ops != NULL ? chan->ops->code : ANSER_PAYLOAD_NONE; - crc = AnserWirePushCrc(paytype, chan->key.condition_id, (uint32) flags, - chan->key.condition_key, keylen, - chan->payload, paylen); + crc = AnserWirePushCrc(paytype, chan->key.condition_id, + chan->key.gp_session_id, chan->key.gp_command_count, + (uint32) flags, chan->key.condition_key, keylen, + chan->payload, paylen); /* * Raw binary: pqPutnchar performs no encoding conversion, so unlike the - * QE -> QD direction this needs no base64. + * QE -> QD direction this needs no base64. The session and command ids go + * out with every delivery: condition_key restarts at the same value for + * every statement, so a parked delivery is only claimable by the command + * it was pushed for. */ if (pqPutMsgStart(GP_SIDEBAND_MESSAGE, conn) < 0 || pqPutInt((int) paytype, 4, conn) < 0 || pqPutInt((int) crc, 4, conn) < 0 || pqPutInt((int) chan->key.condition_id, 4, conn) < 0 || + pqPutInt((int) chan->key.gp_session_id, 4, conn) < 0 || + pqPutInt((int) chan->key.gp_command_count, 4, conn) < 0 || pqPutInt(flags, 4, conn) < 0 || pqPutInt(keylen, 4, conn) < 0 || pqPutnchar(chan->key.condition_key, keylen, conn) < 0 || pqPutInt(paylen, 4, conn) < 0 || - (paylen > 0 && pqPutnchar(chan->payload, paylen, conn) < 0) || - pqPutMsgEnd(conn) < 0 || - pqFlush(conn) < 0) + (paylen > 0 && pqPutnchar(chan->payload, paylen, conn) < 0)) + { + elog(LOG, "anser: could not deliver filter for condition %u: %s", + chan->key.condition_id, anser_conn_error(conn)); + return false; + } + + /* + * Nothing above this point can block -- those calls only grow the output + * buffer. The two that can are avoided deliberately, because this runs + * inside the dispatcher loop and a merged filter is 1-64 MB headed for a + * peer that may have stopped reading: a consumer that gave up on its + * deadline stays subscribed until it can say otherwise. + * + * pqPutMsgEnd() flushes by itself once the buffer passes 8 kB, and a + * blocking pqFlush() then waits in pqWait() with no deadline and no + * CHECK_FOR_INTERRUPTS. Either one strands the coordinator: it stops + * serving every other segment, and the query cannot even be cancelled. + * + * So queue the bytes and let the dispatcher's own poll() place them while + * the peer's command runs. When it ends with bytes still queued the + * message is torn, and the next dispatch would overwrite the buffer, so + * record the connection for AnserDispatchFlushPending() to complete or + * poison before the gang is reused. + */ + pqPutMsgEndNoAutoFlush(conn); + if (pqFlushNonBlocking(conn) < 0) { elog(LOG, "anser: could not deliver filter for condition %u: %s", chan->key.condition_id, anser_conn_error(conn)); return false; } + if (conn->outCount > 0 && + !list_member_ptr(AnserDispPendingPush, conn)) + { + MemoryContext oldcxt = MemoryContextSwitchTo(TopMemoryContext); + + AnserDispPendingPush = lappend(AnserDispPendingPush, conn); + MemoryContextSwitchTo(oldcxt); + } + ANSER_DEBUG("anser: QD pushed cond=%u type=%c bytes=%d cancelled=%d", chan->key.condition_id, paytype, paylen, chan->cancelled ? 1 : 0); @@ -555,7 +806,9 @@ AnserWireParse(const char *msg, AnserWireMsg *out) &keylen, &bodylen, &crc) != 11) return false; - if (kind != ANSER_WIRE_KIND_PART && kind != ANSER_WIRE_KIND_SUBSCRIBE) + if (kind != ANSER_WIRE_KIND_PART && + kind != ANSER_WIRE_KIND_SUBSCRIBE && + kind != ANSER_WIRE_KIND_UNSUBSCRIBE) return false; if (keylen >= ANSER_CONDITION_KEY_SIZE) return false; diff --git a/gpcontrib/anser/src/anserfilter.c b/gpcontrib/anser/src/anserfilter.c index 88933861748..1441bb0275a 100644 --- a/gpcontrib/anser/src/anserfilter.c +++ b/gpcontrib/anser/src/anserfilter.c @@ -201,9 +201,15 @@ AnserBloomLooksLikePart(const void *payload, Size payload_len) * * When a merged part and the incoming part are the same serialized size (they * share bitset params derived from the condition key), the union is a pure - * bitwise OR of the two bitsets plus a bump of the merged header's fold count -- - * no reallocation. This mutates `acc` directly, so the caller must hold whatever - * lock guards the buffer (AnserChannelLock, for the channel payload). + * bitwise OR of the two bitsets -- no reallocation. This mutates `acc` + * directly, so the buffer must be one the caller owns; the coordinator's is + * the channel payload, which lives in its own backend-private memory and is + * only ever touched from the dispatcher loop. + * + * The header is left alone, total_parts included. It names how many producers + * the channel is waiting for, which every part on the channel agrees on and + * folding does not change; the count of parts already folded is the + * coordinator's business and it keeps that in the channel, not in the payload. * * Returns true only when the in-place union applied. It returns false -- and * leaves `acc` untouched (all checks run before any write) -- when the union @@ -217,7 +223,6 @@ bool AnserBloomFoldPartInPlace(void *acc, Size acc_len, const void *part, Size part_len) { - AnserBloomPartHeader *ah; unsigned char *abits; const unsigned char *pbits; Size bitset_bytes; @@ -229,8 +234,6 @@ AnserBloomFoldPartInPlace(void *acc, Size acc_len, !AnserBloomLooksLikePart(part, part_len)) return false; - ah = (AnserBloomPartHeader *) acc; - /* * Equal serialized size is sufficient: every part on a channel is built by * bloom_create from the same (condition-key-derived) parameters, so equal @@ -244,9 +247,6 @@ AnserBloomFoldPartInPlace(void *acc, Size acc_len, for (i = 0; i < bitset_bytes; i++) abits[i] |= pbits[i]; - /* One more segment part folded into the running merged part. */ - ah->total_parts += 1; - return true; } diff --git a/gpcontrib/anser/src/anserinit.c b/gpcontrib/anser/src/anserinit.c index da6cb30e96d..0667179ce68 100644 --- a/gpcontrib/anser/src/anserinit.c +++ b/gpcontrib/anser/src/anserinit.c @@ -46,6 +46,7 @@ #include "cdb/cdbdisp.h" #include "cdb/cdbvars.h" #include "executor/executor.h" +#include "libpq/libpq.h" #include "miscadmin.h" #include "optimizer/planner.h" #include "utils/guc.h" @@ -58,7 +59,8 @@ bool gp_anser_enable = false; bool gp_anser_runtime_filter = false; bool gp_anser_debug = false; int gp_anser_max_info_size = 64 * 1024 * 1024 + 1024 * 1024; -int gp_anser_timeout_ms = 100000; +int gp_anser_timeout_ms = 10000; +int gp_anser_debug_min_filter_bytes = 0; static void anser_define_gucs(void); static PlannedStmt *anser_planner(Query *parse, const char *query_string, @@ -95,6 +97,8 @@ static void anser_executor_run(QueryDesc *queryDesc, ScanDirection direction, static void anser_executor_finish(QueryDesc *queryDesc); static void anser_executor_end(QueryDesc *queryDesc); static void anser_xact_callback(XactEvent event, void *arg); +static void anser_subxact_callback(SubXactEvent event, SubTransactionId mySubid, + SubTransactionId parentSubid, void *arg); void _PG_init(void) @@ -114,8 +118,11 @@ _PG_init(void) /* * Channels live in backend memory on both ends, so they need a point to be - * dropped. ExecutorEnd covers the normal path; the transaction callback - * catches queries that end by erroring. + * dropped. ExecutorEnd covers the normal path; the transaction callbacks + * catch queries that end by erroring -- the subtransaction one because a + * statement aborted inside a savepoint gets no ExecutorEnd (PortalCleanup + * skips it for a failed portal), and its parked deliveries would otherwise + * sit in the inbox until the top-level statement ends. */ prev_ExecutorRun_hook = ExecutorRun_hook; ExecutorRun_hook = anser_executor_run; @@ -124,6 +131,7 @@ _PG_init(void) prev_ExecutorEnd_hook = ExecutorEnd_hook; ExecutorEnd_hook = anser_executor_end; RegisterXactCallback(anser_xact_callback, NULL); + RegisterSubXactCallback(anser_subxact_callback, NULL); /* * Handle Anser notifies arriving from QEs on the dispatch connections. @@ -176,33 +184,62 @@ anser_define_gucs(void) * until a query needs a wider gang and the new processes fall back to the * compiled-in default. That is not a setting anyone can reason about. */ + /* + * Resource and log-noise knobs are superuser-only: an unprivileged role + * must not be able to force large allocations on every segment or fill the + * server log. runtime_filter and timeout_ms stay PGC_USERSET -- they only + * shape the caller's own queries, like any other planner GUC. + */ DefineCustomBoolVariable("anser.debug", "Logs each step of the Anser filter exchange.", "Traces publish, merge, delivery and receive in the log of the process each happens in.", &gp_anser_debug, false, - PGC_USERSET, + PGC_SUSET, GUC_GPDB_NEED_SYNC, NULL, NULL, NULL); DefineCustomIntVariable("anser.max_info_size", "Sets the maximum byte size of one Anser information record.", - "Caps the serialized payload a channel may carry, and with it the effective bloom-filter size. The default holds a full 64 MB bitset plus its serialized-part header.", + "Caps the serialized payload a channel may carry, and with it the effective bloom-filter size. The default holds a full 64 MB bitset plus its serialized-part header. Both ends read it -- the coordinator to size the filter and to bound what it accepts, a segment to bound what it sends and receives -- so it is synced to the segments; changing it on a live session is still best avoided, because a gang busy with an open cursor does not receive the new value.", &gp_anser_max_info_size, - 64 * 1024 * 1024 + 1024 * 1024, 1, INT_MAX, - PGC_USERSET, - 0, + 64 * 1024 * 1024 + 1024 * 1024, 1, + /* + * Not INT_MAX: the payload has to arrive as one + * protocol message, and pq_getmessage refuses + * anything past PQ_LARGE_MESSAGE_LIMIT -- so a + * larger setting buys a configuration that cannot + * work, and used to overflow the cap computed from + * it into a negative int. Refusing it at SET is + * the honest place to say so. + */ + (int) (PQ_LARGE_MESSAGE_LIMIT - ANSER_SIDEBAND_FRAME_ROOM), + PGC_SUSET, + GUC_GPDB_NEED_SYNC, NULL, NULL, NULL); DefineCustomIntVariable("anser.timeout_ms", "Sets how long an Anser consumer waits for its filter.", - "On expiry the consumer runs unfiltered. The deadline matters because a producer that gets squelched never publishes at all. The default is generous because delivery is serial: the coordinator writes the merged filter to one subscriber at a time, so the last of a wide slice waits for all the writes before it.", + "On expiry the consumer runs unfiltered. What the wait covers depends on the plan: where the probe side also travels through a Motion the consumer sits in the probe scan's own slice and runs alongside the build, so it waits out the whole build side; where the probe side is local the hash is already built by the time the consumer runs, leaving only the coordinator's fold and delivery. The default is a budget for an exchange that has gone wrong -- a squelched producer never publishes at all -- rather than a measurement of a healthy one.", &gp_anser_timeout_ms, - 100000, 0, INT_MAX, + 10000, 0, INT_MAX, PGC_USERSET, GUC_UNIT_MS | GUC_GPDB_NEED_SYNC, NULL, NULL, NULL); + /* + * Read on the coordinator at plan time only, and the size it forces + * travels in the plan -- so no NEED_SYNC: a QE never consults it. + */ + DefineCustomIntVariable("anser.debug_min_filter_bytes", + "Sets a floor on the bloom-filter bitset size, for testing.", + "0 (the default) sizes filters from the plan estimate; anything larger forces at least that many bytes, still capped by anser.max_info_size. A bigger filter over the same keys stays a superset of them, so this can only cost memory and wire time, never correctness.", + &gp_anser_debug_min_filter_bytes, + 0, 0, INT_MAX, + PGC_SUSET, + 0, + NULL, NULL, NULL); + MarkGUCPrefixReserved("anser"); } @@ -210,6 +247,12 @@ anser_define_gucs(void) * Plan the query as usual, then hand the finished tree to the runtime-filter * pass. Wrapping the hook this way covers both optimizers, because ORCA is * dispatched from inside standard_planner(). + * + * Planning counts towards anser_exec_nesting for the same reason execution + * does: constant folding an immutable function can run a query through SPI, + * and that query would otherwise reach anser_executor_end believing it is the + * outermost one -- and take the wholesale reset with it. One counter covering + * both phases is what PostgreSQL settled on for the same problem in 76db9cb6368. */ static PlannedStmt * anser_planner(Query *parse, const char *query_string, int cursorOptions, @@ -217,14 +260,23 @@ anser_planner(Query *parse, const char *query_string, int cursorOptions, { PlannedStmt *result; - if (prev_planner_hook) - result = prev_planner_hook(parse, query_string, cursorOptions, - boundParams, optimizer_options); - else - result = standard_planner(parse, query_string, cursorOptions, - boundParams, optimizer_options); + anser_exec_nesting++; + PG_TRY(); + { + if (prev_planner_hook) + result = prev_planner_hook(parse, query_string, cursorOptions, + boundParams, optimizer_options); + else + result = standard_planner(parse, query_string, cursorOptions, + boundParams, optimizer_options); - AnserApplyRuntimeFilters(result); + AnserApplyRuntimeFilters(result); + } + PG_FINALLY(); + { + anser_exec_nesting--; + } + PG_END_TRY(); return result; } @@ -292,14 +344,38 @@ anser_executor_finish(QueryDesc *queryDesc) } /* - * Drop the transport's per-query state, once nothing is executing. + * Drop the transport's per-query state. + * + * The channel key carries gp_command_count, so a query can take its own + * channels with it. That is what keeps a PL/pgSQL body from accumulating one + * merged filter per statement until the outermost one ends: inside SPI, + * anser_exec_nesting never comes back to zero, so the sweep below does not run. * - * See anser_exec_nesting: a query ending while another is still running is an - * inner statement of that query, and its channels are not the ones to drop. + * The global is this query's own count whenever nothing was planned or + * executed since CreateQueryDesc() assigned it -- true for the inner queries + * that do the accumulating. Where it has moved on (a query that itself ran + * nested ones, a PL/pgSQL cursor whose portal outlived other statements) the + * count handed over belongs to somebody else, possibly to a query that is + * still running: AnserDispatchResetCommand() therefore takes only channels + * whose exchange is over, and leaves the rest to the sweep. + * + * See anser_exec_nesting for the sweep: it is wholesale, so a query ending + * while another is still running must not trigger it. */ static void anser_executor_end(QueryDesc *queryDesc) { + /* + * Flush or poison partially written deliveries BEFORE anything can + * recycle the gang: standard_ExecutorEnd's teardown recycles it + * (cleanupQE destroys connections with unflushed output -- the very ones + * the pending list holds), and on the abort path AtAbort_Portals reaches + * this hook strictly before AtAbort_DispatcherState recycles + * (xact.c:3640-3641). So no connection the list names can be freed yet. + */ + AnserDispatchResetCommand(gp_command_count); + AnserDispatchFlushPending(gp_anser_timeout_ms); + if (prev_ExecutorEnd_hook) prev_ExecutorEnd_hook(queryDesc); else @@ -326,3 +402,22 @@ anser_xact_callback(XactEvent event, void *arg) if (event == XACT_EVENT_ABORT || event == XACT_EVENT_PARALLEL_ABORT) AnserSidebandResetAll(); } + +/* + * The same, one transaction level down. A statement that errors inside a + * savepoint or a PL/pgSQL exception block is unwound without an ExecutorEnd: + * AtSubAbort_Portals runs PortalCleanup, which skips the executor for a failed + * portal (portalcmds.c). Anything it parked in the QE inbox -- up to + * gp_anser_max_info_size a piece -- would otherwise survive until the + * top-level statement ends, and a PL/pgSQL loop with an exception block could + * pile them up without bound. The command-count match makes such an entry + * unclaimable, so this is memory hygiene, not correctness; only the inbox is + * dropped, the dispatch channels belong to the top-level statement. + */ +static void +anser_subxact_callback(SubXactEvent event, SubTransactionId mySubid, + SubTransactionId parentSubid, void *arg) +{ + if (event == SUBXACT_EVENT_ABORT_SUB) + AnserSidebandResetInbox(); +} diff --git a/gpcontrib/anser/src/anserplan.c b/gpcontrib/anser/src/anserplan.c index 3391db24461..767d6141831 100644 --- a/gpcontrib/anser/src/anserplan.c +++ b/gpcontrib/anser/src/anserplan.c @@ -46,6 +46,7 @@ #include "utils/acl.h" #include "utils/fmgroids.h" #include "utils/lsyscache.h" +#include "utils/typcache.h" /* Runtime-filter bloom size bounds (realized bitset bytes). */ #define ANSER_RF_MIN_BYTES (1024 * 1024) /* bloom_create's 1 MB floor */ @@ -244,6 +245,35 @@ AnserRuntimeFilterSize(double est_rows, int64 *total_elems, int64 *max_payload, while ((realized << 1) <= target_bytes) realized <<= 1; + /* + * Debug/test knob: force a filter of at least this size regardless of the + * estimate. 0 (the default) means the estimate rules. A bigger filter + * built from the same rows stays a superset of the key set, so this can + * only cost memory and wire time, never correctness. Still capped by the + * payload budget: a floor above it cannot ship. + */ + if (gp_anser_debug_min_filter_bytes > 0 && + realized < (int64) gp_anser_debug_min_filter_bytes) + { + while (realized < (int64) gp_anser_debug_min_filter_bytes && + (realized << 1) <= cap_bytes) + realized <<= 1; + } + + /* + * The filter is built from total_elems (bloom_create sizes from the + * element count, not from planned_bytes), so a floor on realized alone + * only inflates the EXPLAIN number: a 200-row build still produces a 1 MB + * filter whatever the knob says. Raise the element count to match the + * forced size, so producer and consumer really build it. The extra hash + * functions cost only probe time, and the larger bitset only lowers the + * false-positive rate -- a 64 MB filter over 200 keys is overkill, never + * wrong. realized is a power of two, so elems = realized / 2 sizes the + * bitset to exactly realized. + */ + if (realized > elems * 2) + elems = realized / 2; + /* * Refuse a join whose build side cannot be usefully summarized within the * payload cap. @@ -263,6 +293,43 @@ AnserRuntimeFilterSize(double est_rows, int64 *total_elems, int64 *max_payload, return true; } +/* + * The key types we may filter on: by-value types for which SQL equality is + * provably bitwise equality of the stored bytes, so that hashing the raw + * Datum on both sides agrees with the equality operator. Producer and + * consumer hash raw bytes, so a type with a non-canonical representation + * would hash two different ways under one equality operator -- a false + * negative that drops a joinable row. Floats are the canonical + * counterexample (-0.0 and 0.0 compare equal but are not bitwise equal); + * everything else that does not make this guarantee is refused by omission. + * Domains resolve through getBaseType() before landing here. + */ +static bool +anser_rf_key_type_supported(Oid keytype) +{ + switch (keytype) + { + case BOOLOID: + case CHAROID: + case INT2OID: + case INT4OID: + case INT8OID: + case OIDOID: + case XIDOID: + case CIDOID: + case DATEOID: + case TIMEOID: + case TIMESTAMPOID: + case TIMESTAMPTZOID: + case MONEYOID: + case PG_LSNOID: + case XID8OID: + return true; + default: + return false; + } +} + /* * Match a single-column equijoin over plain (by-value) Vars and return the * inner (build) and outer (probe) key attnos. After set_plan_references the @@ -276,6 +343,8 @@ anser_hashjoin_keys(HashJoin *hj, AttrNumber *inner_attno, AttrNumber *outer_att Node *r; Var *outer_var; Var *inner_var; + Oid keytype; + TypeCacheEntry *tce; if (list_length(hj->hashclauses) != 1) return false; @@ -308,17 +377,29 @@ anser_hashjoin_keys(HashJoin *hj, AttrNumber *inner_attno, AttrNumber *outer_att /* * Producer and consumer hash the raw Datum bytes, which is only correct * when SQL equality coincides with bitwise Datum equality of the key. - * That requires the SAME by-value type on both sides: cross-type equijoins - * (e.g. float4 = float8, date = timestamp) hash different bit patterns for - * equal values, and floats are excluded even same-typed because -0.0 and - * 0.0 compare equal but are not bitwise equal. Anything looser could - * prune rows that actually join. + * That requires the same type on both sides -- cross-type equijoins (e.g. + * float4 = float8, date = timestamp) hash different bit patterns for equal + * values -- and the type on the whitelist (see + * anser_rf_key_type_supported), which is where the bitwise-equality claim + * is made. Anything looser could prune rows that actually join -- a wrong + * answer, not a slow one. */ if (inner_var->vartype != outer_var->vartype) return false; - if (!get_typbyval(inner_var->vartype)) + + keytype = getBaseType(inner_var->vartype); + if (!anser_rf_key_type_supported(keytype)) return false; - if (inner_var->vartype == FLOAT4OID || inner_var->vartype == FLOAT8OID) + + /* + * Finally, the clause has to be the type's own equality rather than just + * some hashable operator spelled "=". This does not prove bitwise + * equality -- float8's default equality is float8eq, which is why floats + * need the exclusion above -- but it keeps an operator with semantics of + * its own from being read as if it were the canonical one. + */ + tce = lookup_type_cache(keytype, TYPECACHE_EQ_OPR); + if (!OidIsValid(tce->eq_opr) || op->opno != tce->eq_opr) return false; *inner_attno = inner_var->varattno; @@ -326,6 +407,75 @@ anser_hashjoin_keys(HashJoin *hj, AttrNumber *inner_attno, AttrNumber *outer_att return true; } +/* + * Node types the descent may pass through: the single-child passthroughs + * (Hash, Motion), the joins whose sides carry the column up (HashJoin), and + * our own filter nodes. Anything else -- WindowAgg, Limit, Agg, Result, an + * Append or SetOp whose column comes from several relations at once -- ends + * the descent: the superset argument for landing the filter on the base scan + * (anser_try_inject) only holds for joins and passthroughs; an operator whose + * output depends on the whole input (a window over it, an aggregate of it, + * its first N rows) would be computed over the filtered rows instead. + */ +static bool +anser_rf_passthrough_node(const Plan *plan) +{ + return IsA(plan, Hash) || IsA(plan, Motion) || IsA(plan, HashJoin) || + AnserIsRuntimeFilterScan(plan); +} + +/* + * Is this side of the join the nullable one? On that side an outer join + * extends non-matching rows with NULLs, so a pruned row comes back as a + * NULL-extended row rather than disappearing -- and a downstream condition + * can tell the difference (IS NOT DISTINCT FROM matches NULL to NULL, so + * what was a no-match becomes a match). The preserved side is safe to + * filter: its rows survive identically either way. + */ +static bool +anser_rf_nullable_side(JoinType jointype, bool inner_side) +{ + switch (jointype) + { + case JOIN_INNER: + return false; + case JOIN_LEFT: + return inner_side; + case JOIN_RIGHT: + return !inner_side; + default: + /* FULL, ANTI, LASJ variants and anything future: block both. */ + return true; + } +} + +/* + * Require the scan's own targetlist entry for the column to be a plain Var + * of the scanned relation, never a computed expression (ORCA emits those in + * a Seq Scan's Output). Producer and consumer hash the raw Datum, and a + * tuple-fetched value and an expression-computed one need not agree in the + * upper bytes for 4-byte types: fetch_att sign-extends (Int32GetDatum, + * tupmacs.h) while e.g. ObjectIdGetDatum zero-extends, so an oid >= 2^31 + * would hash two different ways under one equality operator -- a false + * negative that drops a joinable row. A plain Var guarantees both sides + * read the column through fetch_att, where the representation is fixed. + */ +static bool +anser_rf_scan_column_is_var(Plan *scan, AttrNumber attno) +{ + TargetEntry *tle; + Var *var; + + if (scan == NULL || !IsA(scan, SeqScan) || + attno < 1 || attno > list_length(scan->targetlist)) + return false; + tle = (TargetEntry *) list_nth(scan->targetlist, attno - 1); + if (tle == NULL || !IsA(tle->expr, Var)) + return false; + var = (Var *) tle->expr; + return var->varno == ((Scan *) scan)->scanrelid; +} + /* * Follow one side of a join down to the base SeqScan its key column comes * from, mapping the key attno through each targetlist on the way. Used for @@ -361,12 +511,22 @@ anser_hashjoin_keys(HashJoin *hj, AttrNumber *inner_attno, AttrNumber *outer_att */ bool AnserResolveKeyScan(Plan *top, AttrNumber key_attno, int slice_index, + bool for_consumer, Plan **parent_out, Plan **scan_out, AttrNumber *attno_out, int *slice_out) { Plan *node = top; AttrNumber attno = key_attno; + /* + * top itself must be descendable: the loop below checks only child, and + * on the first iteration node IS top, which without this a WindowAgg, + * Limit, Agg or Result above the probe scan would be walked straight + * past. + */ + if (top == NULL || !anser_rf_passthrough_node(top)) + return false; + for (;;) { TargetEntry *tle; @@ -415,16 +575,36 @@ AnserResolveKeyScan(Plan *top, AttrNumber key_attno, int slice_index, if (child == NULL) return false; + /* + * The consumer must never land below an outer join's nullable side: + * pruning there does not just remove the row, it NULL-extends it, and + * a downstream condition can tell that from "no row" (IS NOT DISTINCT + * FROM matches NULL to NULL, so a no-match becomes a match). The + * producer may descend there: a bloom built over the whole base + * column is a superset of what the outer join emits, and a superset + * is all it needs to be. + */ + if (for_consumer && IsA(node, HashJoin) && + anser_rf_nullable_side(((HashJoin *) node)->join.jointype, + !via_outer)) + return false; + if (via_outer && IsA(child, SeqScan)) { + /* + * The scan's own entry for the column must be a plain Var, never + * a computed expression (see anser_rf_scan_column_is_var). + */ + if (!anser_rf_scan_column_is_var(child, var->varattno)) + return false; + *parent_out = node; *scan_out = child; *attno_out = var->varattno; *slice_out = slice_index; return true; } - if (!IsA(child, Hash) && !IsA(child, Motion) && !IsA(child, HashJoin) && - !AnserIsRuntimeFilterScan(child)) + if (!anser_rf_passthrough_node(child)) return false; /* @@ -524,6 +704,7 @@ anser_try_inject(HashJoin *hj, AnserInjectCtx *ctx) if (!anser_hashjoin_keys(hj, &inner_attno, &outer_attno)) return; if (!AnserResolveKeyScan(hash, inner_attno, ctx->slice_index, + false /* for_consumer */ , &build_parent, &build_scan, &build_attno, &build_slice)) return; @@ -535,19 +716,21 @@ anser_try_inject(HashJoin *hj, AnserInjectCtx *ctx) * consumer lands on the base scan the probe key comes from. * * Filtering that scan is right for the same reason filtering the probe - * side is: every row the probe subtree produces carries that relation's - * key column unchanged, so a row of it whose key the build side does not - * have cannot reach the join's output through any path. A row dropped - * under an outer join inside the subtree comes back NULL-extended instead, - * and a NULL key joins nothing either. + * side is: the descent never crosses an outer join's nullable side (see + * AnserResolveKeyScan), so every row the probe subtree produces carries + * that relation's key column unchanged, and a row whose key the build side + * does not have cannot reach the join's output through any path. */ - if (IsA(probe, SeqScan)) + if (IsA(probe, SeqScan) && + anser_rf_scan_column_is_var(probe, outer_attno)) { probe_parent = (Plan *) hj; probe_scan = probe; probe_attno = outer_attno; + probe_slice = ctx->slice_index; /* nothing descended through */ } else if (!AnserResolveKeyScan(probe, outer_attno, ctx->slice_index, + true /* for_consumer */ , &probe_parent, &probe_scan, &probe_attno, &probe_slice)) return; @@ -559,6 +742,30 @@ anser_try_inject(HashJoin *hj, AnserInjectCtx *ctx) if (probe_scan == build_scan) return; + /* + * Only filter across a Motion. Slices are divided by Motions and nothing + * else, so two scans in one slice are a co-located join, and that is a + * different proposition in both directions. + * + * It is what makes the exchange safe. An Anser exchange is a + * synchronisation point: the consumer stops until the coordinator has + * heard from every producer. Within one slice those are the same process, + * run one after another, so the wait can be on something that has not been + * reached yet -- a consumer placed under an intervening HashJoin waits for + * a producer sitting in that join's inner side, which cannot start until + * the outer side yields the tuple the consumer is holding. defer_first + * covers exactly one tuple of that. Across a Motion the two are separate + * processes and the question does not arise. + * + * And it is what makes it worth doing. Where a Motion already carries the + * rows, the barrier is paid for by work the query was doing anyway; in a + * co-located join it is new cost against a join that was already local, + * and whether it pays for itself is a measurement nobody has made. Until + * someone does, decline. + */ + if (build_slice == probe_slice) + return; + /* * How many producers will there be? One per process of the build scan's * slice. Declining when we cannot tell is the safe direction: a count that @@ -698,9 +905,17 @@ anser_inject_walk(Plan *plan, AnserInjectCtx *ctx) * How many processes execute a slice -- which is how many producers will * publish a part, and therefore how many the coordinator must wait for. * - * This mirrors FillSliceGangInfo (execUtils.c): a slice runs - * numsegments * parallel_workers processes, with parallel_workers == 0 meaning - * one. Both optimizers are covered, and they do differ: for the same query the + * This mirrors FillSliceGangInfo (execUtils.c), which has a different answer + * per gang type: numsegments * parallel_workers for a plain reader or writer, + * but one process for the coordinator's own slice, parallel_workers for a + * singleton reader, and one set per targeted segment under direct dispatch. + * parallel_workers == 0 means one. Each is handled below, because over- + * counting costs every consumer the full anser.timeout_ms: the channel never + * reaches the part count it is waiting for, so it never completes and nobody + * is ever told. Under-counting would be worse -- a filter short of one + * segment's keys rejects rows that join -- which is why none of these guesses. + * + * Both optimizers are covered, and they do differ: for the same query the * Postgres planner may give a serial build slice (Broadcast Motion 2:8 over a * plain Seq Scan, so 2 producers) where GPORCA gives a parallel one (Broadcast * Motion 16:16 over a Parallel Seq Scan, so 16). Reading it from the slice @@ -724,6 +939,30 @@ AnserSliceProducers(PlannedStmt *stmt, int slice_index) return 1; factor = slice->parallel_workers > 0 ? slice->parallel_workers : 1; + + /* + * A singleton reader runs on one segment, whatever the cluster's width: + * FillSliceGangInfo gives it planNumSegments = 1 * factor and fills its + * segment list with factor copies of one segindex. numsegments is not + * even meaningful here -- plannodes.h scopes that field to the + * PRIMARY_READER/WRITER cases. + */ + if (slice->gangType == GANGTYPE_SINGLETON_READER) + return factor; + + /* + * Direct dispatch narrows a reader gang to the segments it actually + * needs, and FillSliceGangInfo builds the segment list from contentIds -- + * factor processes for each. planNumSegments keeps saying + * numsegments * factor, so the segment list is what to count. + * + * An empty list yields zero, which the caller reads as "unknown" and + * declines on, and that is the right answer: a slice dispatched nowhere + * publishes nothing. + */ + if (slice->directDispatch.isDirectDispatch) + return list_length(slice->directDispatch.contentIds) * factor; + if (slice->numsegments <= 0) return 0; diff --git a/gpcontrib/anser/src/anserplanexec.c b/gpcontrib/anser/src/anserplanexec.c index 8b8f93a4a8e..a42a5b3013c 100644 --- a/gpcontrib/anser/src/anserplanexec.c +++ b/gpcontrib/anser/src/anserplanexec.c @@ -87,6 +87,8 @@ typedef struct AnserBloomProduceScanState AttrNumber key_attno; int64 planned_bytes; bool started; /* this process actually executed the node */ + bool my_slice; /* ... and was always going to: the node's + * slice is the one this process runs */ bool published; } AnserBloomProduceScanState; @@ -335,6 +337,12 @@ anser_rf_build_key(CustomScan *cscan, AnserChannelKey *key) * part_index is diagnostics only: completion is by count, and the serialized * part carries its own (0, 1). Under parallel execution the workers of one * segment therefore share an index, which is why the trace prints the pid too. + * Because it is only diagnostic it must never be allowed to reject a real + * producer: a slice narrower than the cluster (direct dispatch to one + * segment, a singleton reader, or the entry-DB where segindex is -1) would + * otherwise put segindex >= total_parts and refuse the only producer there + * is, leaving the channel a part short and every consumer waiting out + * anser.timeout_ms. Clamp it instead. */ static void anser_rf_part_info(CustomScan *cscan, uint32 *part_index, uint32 *total_parts) @@ -344,8 +352,12 @@ anser_rf_part_info(CustomScan *cscan, uint32 *part_index, uint32 *total_parts) if (Gp_role == GP_ROLE_EXECUTE) { - *part_index = (uint32) GpIdentity.segindex; *total_parts = (uint32) Max(n_producers, 1); + if (GpIdentity.segindex < 0) + *part_index = 0; /* entry-DB: single local producer */ + else + *part_index = Min((uint32) GpIdentity.segindex, + *total_parts - 1); } else { @@ -402,11 +414,35 @@ anser_produce_begin(CustomScanState *node, EState *estate, int eflags) st->started = false; st->published = false; + /* + * Whether this process is the one that runs the node's slice -- which is + * not the same question as whether the node ends up executing, and has to + * be asked here rather than at end time. During initialization + * currentSliceId names the slice being initialized, alien or not; once + * execution starts it always names the local one, so the distinction is + * gone by then. + */ + st->my_slice = (estate->currentSliceId == LocallyExecutingSliceIndex(estate)); + anser_rf_build_key(cscan, &key); anser_rf_part_info(cscan, &part_index, &total_parts); - st->produce = ExecInitAnserBloomFilterProduce(&key, total_elems, max_payload, - part_index, total_parts); + /* + * Build the filter only in the process that will execute this node, and + * never for EXPLAIN alone. ExecutorStart runs even then + * (EXEC_FLAG_EXPLAIN_ONLY), and on the coordinator it runs over every + * slice (eliminateAliens is false there), so without the gate a plain + * EXPLAIN allocates a full bitset per producer on the coordinator -- + * memory no work_mem/statement_mem accounting ever sees. A NULL produce + * is a first-class state downstream: the node passes tuples through, and + * publish/cancel are NULL-safe. + */ + if (st->my_slice && (eflags & EXEC_FLAG_EXPLAIN_ONLY) == 0) + st->produce = ExecInitAnserBloomFilterProduce(&key, total_elems, + max_payload, part_index, + total_parts); + else + st->produce = NULL; ANSER_DEBUG("anser: producer init cond=%u part=%u/%u elems=" INT64_FORMAT " payload=%zu state=%s", key.condition_id, part_index, total_parts, total_elems, max_payload, st->produce != NULL ? "ok" : "NULL"); @@ -460,6 +496,7 @@ anser_produce_next(CustomScanState *node) { ANSER_DEBUG("anser: producer child exhausted, publishing (state=%s)", st->produce != NULL ? "ok" : "NULL"); + (void) ExecAnserBloomFilterProducePublish(st->produce); st->published = true; } @@ -491,14 +528,23 @@ anser_produce_end(CustomScanState *node) AnserBloomProduceScanState *st = (AnserBloomProduceScanState *) node; /* - * A producer that ran but never published was abandoned mid-scan (squelch, - * or an error upstream), and consumers must be told so rather than left - * waiting for a part that will never come. A producer that never ran at - * all belongs to another slice: cancelling here would destroy a channel - * this process has no part in -- which is exactly what the coordinator - * used to do to every runtime filter. + * Consumers must be told, one way or another, by every process the + * channel is counting on. Two cases reach here without having published: + * + * The producer ran and was abandoned mid-scan (squelch, or an error + * upstream), so the part it promised will never come. + * + * Or it never ran at all although this process owns its slice -- which + * happens in a co-located join, where ExecHashJoin skips building the + * hash table on a segment whose outer side turned out to be empty. The + * node is initialized, never executed, and used to leave silently; the + * channel then sat one part short of complete and every consumer waited + * out the whole of anser.timeout_ms for it. + * + * What must still stay silent is a node belonging to another slice. + * Cancelling there would destroy a channel this process has no part in. */ - if (st->started && !st->published && st->produce != NULL) + if (!st->published && st->produce != NULL && (st->started || st->my_slice)) { (void) ExecAnserBloomFilterProduceCancel(st->produce); st->published = true; @@ -544,7 +590,7 @@ anser_produce_explain(CustomScanState *node, List *ancestors, ExplainState *es) /* * The realized filter size is planned_bytes by construction: producer and * consumer build identical filters from the same plan parameters (see - * anser_rf_size in anserplan.c), so no execution stats are needed. + * AnserRuntimeFilterSize in anserplan.c), so no execution stats are needed. */ snprintf(buf, sizeof(buf), "memory=" INT64_FORMAT "kB", st->planned_bytes / 1024); @@ -647,12 +693,15 @@ anser_consume_receive(AnserBloomConsumeScanState *st) /* * Diagnostic: a NULL filter means we fail open (no pruning). Log why -- - * cancelled delivery vs. too few bloom parts unioned -- so runtime-filter - * misbehavior is visible in the server log. + * cancelled delivery vs. nothing arriving at all -- so runtime-filter + * misbehavior is visible in the server log. The count below is what the + * payload declared the channel's producer count to be, not a tally of + * arrivals: the coordinator never delivers a channel short of its parts, + * so there is no such tally to report here. */ if (st->filter == NULL) elog(LOG, - "anser bloom consumer: no filter, failing open (cancelled=%d, received_parts=%u)", + "anser bloom consumer: no filter, failing open (cancelled=%d, declared_parts=%u)", ExecAnserBloomFilterConsumerWasCancelled(st->consume), ExecAnserBloomFilterConsumerReceivedParts(st->consume)); } @@ -977,7 +1026,7 @@ anser_consume_explain(CustomScanState *node, List *ancestors, ExplainState *es) * instrument. As with all per-node EXPLAIN ANALYZE stats in MPP, * these are the winning segment's values (max ntuples/nloops), not a * cluster-wide sum. memory is planned_bytes: the unioned filter's - * size is identical on every segment by construction (anser_rf_size). + * size is identical on every segment by construction (AnserRuntimeFilterSize). * When the filter was pushed into the child scan, pruning happens * there instead (its "Rows Removed by Pushdown Runtime Filter" line), * so our counters stay zero. diff --git a/gpcontrib/anser/src/ansersideband.c b/gpcontrib/anser/src/ansersideband.c index c0e01700c43..9e72cfea600 100644 --- a/gpcontrib/anser/src/ansersideband.c +++ b/gpcontrib/anser/src/ansersideband.c @@ -123,6 +123,30 @@ AnserSidebandPublish(const AnserChannelKey *channel_key, char payload_type, return ok; } +/* + * Withdraw a subscription: best effort, so a send failure is only logged. + * Called on every path out of AnserSidebandConsumeWait that leaves a + * subscription behind (deadline expiry, query finishing early): from here on + * this process is somewhere else in the plan and no longer reading for a + * filter, so a delivery would put up to 64 MB on a connection whose reader + * has moved on -- and whatever reads it next has to recognise and skip it. + * + * A narrowing rather than a guarantee: the coordinator may already be inside + * its delivery when this arrives. That is why the push never blocks and why + * every reader still tolerates a message that is not its own. + */ +static void +anser_sideband_unsubscribe(const AnserChannelKey *channel_key) +{ + char *msg = AnserWireFormat(channel_key, ANSER_WIRE_KIND_UNSUBSCRIBE, + ANSER_PAYLOAD_NONE, 0, 0, 0, NULL, 0); + + if (!anser_sideband_send(msg)) + ANSER_DEBUG("anser: seg%d could not unsubscribe from cond=%u", + GpIdentity.segindex, channel_key->condition_id); + pfree(msg); +} + /* * Subscribe, then wait for the merged payload. * @@ -182,9 +206,10 @@ AnserSidebandConsumeWait(const AnserChannelKey *channel_key, char payload_type, * recognise and skip it. So do not ask in the first place. * * This only narrows the window. Any deadline can expire with the - * subscription already registered, and there is no unsubscribe, so every - * reader of this connection still has to tolerate a message that is not its - * own (see cdb_sequence_nextval_qe). + * subscription already registered, so even though we unsubscribe on the way + * out (anser_sideband_unsubscribe), every reader of this connection still + * has to tolerate a message that is not its own (see + * cdb_sequence_nextval_qe). */ if (timeout_ms == 0) { @@ -217,6 +242,28 @@ AnserSidebandConsumeWait(const AnserChannelKey *channel_key, char payload_type, { CHECK_FOR_INTERRUPTS(); + /* + * The coordinator has what it needs and told us to wrap up -- a LIMIT + * that filled, typically. CHECK_FOR_INTERRUPTS does not cover this: + * it is a flag the executor is expected to notice, which is why + * nodeHashjoin.c tests it beside the same call. Without that, a + * finished query is held open by a consumer still waiting on a filter + * nobody will use, for up to anser.timeout_ms. + */ + if (QueryFinishPending) + { + ANSER_DEBUG("anser: seg%d abandoning cond=%u: the query is finishing", + GpIdentity.segindex, channel_key->condition_id); + + /* + * Same reason the deadline path withdraws: the query is wrapping + * up, so nobody here will read the filter the coordinator is + * about to push. + */ + anser_sideband_unsubscribe(channel_key); + return false; + } + if (anser_inbox_take(channel_key, payload_type, payload, payload_len, cancelled)) return payload != NULL && *payload != NULL; @@ -224,6 +271,15 @@ AnserSidebandConsumeWait(const AnserChannelKey *channel_key, char payload_type, if (timeout_ms >= 0 && TimestampDifferenceExceeds(start, GetCurrentTimestamp(), timeout_ms)) { + /* + * Withdraw the subscription on the way out. From here on this + * process is somewhere else in the plan and no longer reading for + * a filter, so a delivery would put 1-64 MB on a connection whose + * reader has moved on -- and whatever reads it next has to + * recognise and skip it. + */ + anser_sideband_unsubscribe(channel_key); + /* * Report what we saw, not just that we waited: "nothing arrived" * and "something arrived for another channel" are different bugs. @@ -258,12 +314,15 @@ anser_sideband_read_one(long timeout_ms) { unsigned char qtype; int retval; + int64 maxlen; StringInfoData buf; AnserInboxEntry *entry; MemoryContext oldcxt; char paytype; uint32 crc; int condid; + uint32 session_id; + uint32 command_count; int flags; int keylen; int paylen; @@ -331,23 +390,49 @@ anser_sideband_read_one(long timeout_ms) errdetail("The message boundary was lost; this connection cannot be used further."))); } + /* + * In int64 and clamped, though anser.max_info_size is bounded so that it + * cannot overflow this on its own. The bound lives in another file; this + * costs nothing and does not depend on the two staying in step. + */ + maxlen = (int64) gp_anser_max_info_size + ANSER_SIDEBAND_FRAME_ROOM; + if (maxlen > (int64) PQ_LARGE_MESSAGE_LIMIT) + maxlen = (int64) PQ_LARGE_MESSAGE_LIMIT; + initStringInfo(&buf); - if (pq_getmessage(&buf, gp_anser_max_info_size + ANSER_CONDITION_KEY_SIZE + 64) != 0) + if (pq_getmessage(&buf, (int) maxlen) != 0) { /* - * pq_getmessage clears the reading-message flag when it succeeds, but - * not on its EOF paths; clear it by hand so we do not trip the - * assertion in a later pq_startmsgread. + * Same reasoning as the message type above, and the same outcome. + * Every path on which pq_getmessage returns EOF leaves us unable to go + * on: either the peer is gone, or -- when the length word exceeds the + * cap -- it has taken those four bytes and left the body in the stream + * (pqcomm.c discards the body only on the paths where it already read + * it). Running unfiltered from here would hand the gang pool a + * connection whose next read lands mid-message. + * + * The cap can be exceeded for real: gp_anser_max_info_size is USERSET, + * and a gang busy with a named portal does not receive a dispatched + * SET -- see cdbdisp_markNamedPortalGangsDestroyed(). So this is a + * reachable disagreement, not a corrupt-stream-only case. + * + * pq_getmessage clears the reading-message flag when it succeeds but + * not on its EOF paths; clear it by hand so the error path does not + * also trip the assertion in a later pq_startmsgread. */ pq_endmsgread(); pfree(buf.data); - elog(LOG, "anser: could not read filter message"); - return false; + ereport(FATAL, + (errcode(ERRCODE_PROTOCOL_VIOLATION), + errmsg("anser: could not read the filter message"), + errdetail("The message boundary was lost; this connection cannot be used further."))); } paytype = (char) pq_getmsgint(&buf, 4); crc = (uint32) pq_getmsgint(&buf, 4); condid = pq_getmsgint(&buf, 4); + session_id = (uint32) pq_getmsgint(&buf, 4); + command_count = (uint32) pq_getmsgint(&buf, 4); flags = pq_getmsgint(&buf, 4); keylen = pq_getmsgint(&buf, 4); if (keylen < 0 || keylen >= ANSER_CONDITION_KEY_SIZE) @@ -367,8 +452,9 @@ anser_sideband_read_one(long timeout_ms) payptr = paylen > 0 ? pq_getmsgbytes(&buf, paylen) : NULL; if (crc != (uint32) AnserWirePushCrc(paytype, (uint32) condid, - (uint32) flags, keyptr, keylen, - payptr, paylen)) + session_id, command_count, + (uint32) flags, keyptr, keylen, + payptr, paylen)) { /* * Drop it and keep waiting: the deadline will expire and this consumer @@ -387,8 +473,14 @@ anser_sideband_read_one(long timeout_ms) */ oldcxt = MemoryContextSwitchTo(TopMemoryContext); entry = palloc0(sizeof(AnserInboxEntry)); - entry->key.gp_session_id = gp_session_id; - entry->key.gp_command_count = gp_command_count; + + /* + * The identity comes from the wire, never from our own globals: a message + * parked from an earlier command keeps that command's ids, so a later + * statement can never claim it by matching its own. + */ + entry->key.gp_session_id = session_id; + entry->key.gp_command_count = command_count; entry->key.condition_id = (uint32) condid; memcpy(entry->key.condition_key, keyptr, keylen); entry->key.condition_key[keylen] = '\0'; @@ -421,7 +513,19 @@ anser_inbox_take(const AnserChannelKey *key, char payload_type, void **payload, { AnserInboxEntry *entry = (AnserInboxEntry *) lfirst(lc); + /* + * The condition key alone does not identify a delivery: it is built + * from plan-shape names and repeats across statements, so a filter + * parked by a late delivery of statement A would otherwise be + * claimed by statement B. The coordinator stamps every push with + * its session id and command count, and we demand an exact match + * on both integers (no hashing -- collisions are unaffordable). + * gp_session_id is unique per postmaster lifetime, so an entry from + * an older session can never match either. + */ if (entry->key.condition_id != key->condition_id || + entry->key.gp_session_id != key->gp_session_id || + entry->key.gp_command_count != key->gp_command_count || strncmp(entry->key.condition_key, key->condition_key, ANSER_CONDITION_KEY_SIZE) != 0) continue; @@ -610,17 +714,20 @@ AnserWireFormat(const AnserChannelKey *channel_key, char kind, * value does not depend on the host. */ pg_crc32c -AnserWirePushCrc(char payload_type, uint32 condition_id, uint32 flags, - const char *key, int keylen, const void *body, int bodylen) +AnserWirePushCrc(char payload_type, uint32 condition_id, uint32 session_id, + uint32 command_count, uint32 flags, const char *key, + int keylen, const void *body, int bodylen) { pg_crc32c crc; - uint32 fields[4]; + uint32 fields[6]; uint32 belen; fields[0] = pg_hton32((uint32) (unsigned char) payload_type); fields[1] = pg_hton32(condition_id); - fields[2] = pg_hton32(flags); - fields[3] = pg_hton32((uint32) keylen); + fields[2] = pg_hton32(session_id); + fields[3] = pg_hton32(command_count); + fields[4] = pg_hton32(flags); + fields[5] = pg_hton32((uint32) keylen); INIT_CRC32C(crc); COMP_CRC32C(crc, fields, sizeof(fields)); diff --git a/gpcontrib/anser/test/.gitignore b/gpcontrib/anser/test/.gitignore new file mode 100644 index 00000000000..847bef2ecd3 --- /dev/null +++ b/gpcontrib/anser/test/.gitignore @@ -0,0 +1 @@ +/anserfold_test diff --git a/gpcontrib/anser/test/Makefile b/gpcontrib/anser/test/Makefile new file mode 100644 index 00000000000..7edeaa7a8a9 --- /dev/null +++ b/gpcontrib/anser/test/Makefile @@ -0,0 +1,66 @@ +#------------------------------------------------------------------------- +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# +# Unit tests for anser: plain programs, no cluster. +# +# The backend's own unit tests live in src/backend/*/test and go through +# mock.mk, which links a mock of every backend file; an extension is not +# backend code and does not fit that. What is tested here does not need it +# either -- it works on caller-supplied memory -- so the few backend symbols +# anserfilter.c refers to from its other functions are stubbed in the test +# itself, where a wrong signature is a compile error. +# +# Built and run by the parent's installcheck; never installed. +# +# IDENTIFICATION +# gpcontrib/anser/test/Makefile +# +#------------------------------------------------------------------------- + +PROGRAM = anserfold_test +OBJS = anserfold_test.o anserfilter.o + +PGFILEDESC = "anserfold_test - unit test for the Anser bloom part fold" +NO_INSTALL = 1 + +PG_CPPFLAGS = -I$(srcdir)/../include +PG_LIBS_INTERNAL += $(libpgport) + +ifdef USE_PGXS +PG_CONFIG ?= pg_config +PGXS := $(shell $(PG_CONFIG) --pgxs) +include $(PGXS) +else +subdir = gpcontrib/anser/test +top_builddir = ../../.. +include $(top_builddir)/src/Makefile.global +include $(top_srcdir)/contrib/contrib-global.mk +endif + +# Its own copy rather than the one the extension builds: that one is built for +# a shared library, and depending on it would tie this to the parent's build +# order for no gain. +anserfilter.o: $(srcdir)/../src/anserfilter.c + $(CC) $(CFLAGS) $(CPPFLAGS) -c -o $@ $< + +# Not called "check": pgxs.mk already defines that target, and overriding it +# here would only earn a warning from make. +.PHONY: unitcheck +unitcheck: $(PROGRAM) + ./$(PROGRAM) diff --git a/gpcontrib/anser/test/anserfold_test.c b/gpcontrib/anser/test/anserfold_test.c new file mode 100644 index 00000000000..bf753105072 --- /dev/null +++ b/gpcontrib/anser/test/anserfold_test.c @@ -0,0 +1,258 @@ +/*------------------------------------------------------------------------- + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * anserfold_test.c + * What AnserBloomFoldPartInPlace() does to a part header. + * + * The coordinator folds every later part of a channel into the first one, and + * total_parts in that header is how many producers the channel is waiting for: + * every part carries the same number, and folding must not change it. It used + * to be incremented per fold, which left the merged header holding + * n_producers + (folds) -- 2N-1 for a complete channel, and N only when N is 1. + * That number is what a consumer reports and what the log prints when a filter + * goes missing, so it was wrong exactly where someone would be reading it. + * + * A regression test rather than a demonstration: the SQL-level fold test could + * not see this, because it folds one part into one and 2N-1 is N there. This + * one folds three, where the two differ. + * + * Runs without a cluster -- the fold is a bitwise OR over caller-supplied + * memory, so the parts here are built by hand rather than by bloom_create(). + * The backend symbols anserfilter.c refers to from its other functions are + * stubbed below; none of them is on this path, and each aborts if that ever + * stops being true. + * + * IDENTIFICATION + * gpcontrib/anser/test/anserfold_test.c + * + *------------------------------------------------------------------------- + */ +#include "postgres.h" + +#include +#include + +#include "anserfilter.h" +#include "common/hashfn.h" +#include "lib/bloomfilter.h" +#include "port/pg_bitutils.h" + +#define BITSET_BYTES 64 +#define PART_BYTES (sizeof(AnserBloomPartHeader) + BITSET_BYTES) + +#define PRODUCERS 3 /* so that N and 2N-1 differ */ + +static int failures = 0; + +static void +check(bool ok, const char *what) +{ + if (!ok) + { + fprintf(stderr, "FAIL: %s\n", what); + failures++; + } +} + +/* + * One part as it travels: the header every producer writes, then a bitset with + * a single byte set, so a union of several is easy to recognise. + */ +static void +make_part(unsigned char *buf, uint32 part_index, uint32 total_parts, + int set_byte) +{ + AnserBloomPartHeader *h = (AnserBloomPartHeader *) buf; + + memset(buf, 0, PART_BYTES); + h->magic = ANSER_BLOOM_PART_MAGIC; + h->version = ANSER_BLOOM_PART_VERSION; + h->part_index = part_index; + h->total_parts = total_parts; + buf[sizeof(AnserBloomPartHeader) + set_byte] = 0x01; +} + +static bool +header_is(const unsigned char *buf, uint32 part_index, uint32 total_parts) +{ + const AnserBloomPartHeader *h = (const AnserBloomPartHeader *) buf; + + return h->magic == ANSER_BLOOM_PART_MAGIC && + h->version == ANSER_BLOOM_PART_VERSION && + h->part_index == part_index && + h->total_parts == total_parts; +} + +static bool +bit_set(const unsigned char *buf, int byte) +{ + return buf[sizeof(AnserBloomPartHeader) + byte] == 0x01; +} + +int +main(void) +{ + unsigned char acc[PART_BYTES]; + unsigned char second[PART_BYTES]; + unsigned char third[PART_BYTES]; + unsigned char before[PART_BYTES]; + unsigned char bigger[PART_BYTES * 2]; + unsigned char bad[PART_BYTES]; + + /* + * A three-producer channel: the first part is kept verbatim, the other two + * fold into it. This is the whole point of the file -- with one fold the + * old behaviour was indistinguishable from the right one. + */ + make_part(acc, 0, PRODUCERS, 0); + make_part(second, 1, PRODUCERS, 1); + make_part(third, 2, PRODUCERS, 2); + + check(AnserBloomFoldPartInPlace(acc, PART_BYTES, second, PART_BYTES), + "folding a same-sized part is accepted"); + check(header_is(acc, 0, PRODUCERS), + "one fold leaves the header alone"); + + check(AnserBloomFoldPartInPlace(acc, PART_BYTES, third, PART_BYTES), + "folding a third part is accepted"); + check(header_is(acc, 0, PRODUCERS), + "two folds leave the header alone (was 2N-1)"); + + check(bit_set(acc, 0) && bit_set(acc, 1) && bit_set(acc, 2), + "the union carries every part's bits"); + + /* The parts folded in are read-only. */ + check(header_is(second, 1, PRODUCERS) && header_is(third, 2, PRODUCERS), + "the folded-in parts are untouched"); + + /* + * A part of another size cannot be OR'd, and the refusal has to leave the + * accumulator exactly as it was -- the caller cancels the channel on it. + */ + memcpy(before, acc, PART_BYTES); + make_part(bigger, 0, PRODUCERS, 0); + check(!AnserBloomFoldPartInPlace(acc, PART_BYTES, bigger, PART_BYTES * 2), + "a differently-sized part is refused"); + check(memcmp(acc, before, PART_BYTES) == 0, + "a refused size leaves the accumulator byte-identical"); + + /* The same for a part whose header does not pass validation. */ + make_part(bad, 0, PRODUCERS, 0); + ((AnserBloomPartHeader *) bad)->magic = 0xDEADBEEFU; + check(!AnserBloomFoldPartInPlace(acc, PART_BYTES, bad, PART_BYTES), + "a part with a bad magic is refused"); + check(memcmp(acc, before, PART_BYTES) == 0, + "a refused part leaves the accumulator byte-identical"); + + if (failures > 0) + { + fprintf(stderr, "%d check(s) failed\n", failures); + return 1; + } + printf("all checks passed\n"); + return 0; +} + +/* + * Stubs for what anserfilter.c's other functions refer to. The fold path + * touches none of them; reaching one means this test is no longer testing what + * it says it is, so they abort rather than return something plausible. + * + * Signatures are copied from the declarations the compiler has already seen, + * so a mismatch is a build error here rather than a surprise at link time. + */ +#define NOT_ON_THIS_PATH(fn) \ + do { \ + fprintf(stderr, "%s called: the fold path is not supposed to\n", fn); \ + abort(); \ + } while (0) + +bool gp_anser_debug = false; + +bloom_filter * +bloom_create(int64 total_elems, int bloom_work_mem, uint64 seed) +{ + NOT_ON_THIS_PATH("bloom_create"); +} + +bloom_filter * +bloom_create_from_bitset(int64 total_elems, int bloom_work_mem, uint64 seed, + const unsigned char *bitset, Size bitset_bytes) +{ + NOT_ON_THIS_PATH("bloom_create_from_bitset"); +} + +void +bloom_free(bloom_filter *filter) +{ + NOT_ON_THIS_PATH("bloom_free"); +} + +Size +bloom_bitset_bytes(const bloom_filter *filter) +{ + NOT_ON_THIS_PATH("bloom_bitset_bytes"); +} + +const unsigned char * +bloom_bitset_data(const bloom_filter *filter) +{ + NOT_ON_THIS_PATH("bloom_bitset_data"); +} + +uint64 +bloom_total_bits(bloom_filter *filter) +{ + NOT_ON_THIS_PATH("bloom_total_bits"); +} + +uint64 +hash_bytes_extended(const unsigned char *k, int keylen, uint64 seed) +{ + NOT_ON_THIS_PATH("hash_bytes_extended"); +} + +uint64 +pg_popcount(const char *buf, int bytes) +{ + NOT_ON_THIS_PATH("pg_popcount"); +} + +/* ANSER_DEBUG expands to elog(); gp_anser_debug is off, so it never fires. */ +bool +errstart(int elevel, const char *domain) +{ + return false; +} + +bool +errstart_cold(int elevel, const char *domain) +{ + return false; +} + +void +errfinish(const char *filename, int lineno, const char *funcname) +{ +} + +void +errmsg_internal(const char *fmt,...) +{ +} diff --git a/pom.xml b/pom.xml index 21365d09782..b1970df21f9 100644 --- a/pom.xml +++ b/pom.xml @@ -1428,7 +1428,6 @@ code or new licensing patterns. gpcontrib/reject_partition_fullscan/Makefile gpcontrib/reject_partition_fullscan/reject_partition_fullscan.control - gpcontrib/anser/anser_test.control gpcontrib/anser/**/*.sql gpcontrib/anser/**/*.out diff --git a/src/backend/cdb/dispatcher/cdbdisp_async.c b/src/backend/cdb/dispatcher/cdbdisp_async.c index 62417d2e643..5b123a94c65 100644 --- a/src/backend/cdb/dispatcher/cdbdisp_async.c +++ b/src/backend/cdb/dispatcher/cdbdisp_async.c @@ -557,10 +557,20 @@ checkDispatchResult(CdbDispatcherState *ds, int timeout_sec) if (conn->outCount > 0) { /* + * Non-blocking, and the POLLOUT below is what finishes the + * job. A blocking flush here waits in pqWait() with no + * deadline and no CHECK_FOR_INTERRUPTS, so a peer that has + * stopped reading its end takes the whole dispatcher with it + * -- including this function's other role, which is to notice + * a cancel for the caller (cdbdisp_checkForCancel_async + * arrives here with timeout_sec = 0 precisely so as not to + * wait). cdbdisp_waitDispatchFinish_async already drains its + * output this way, so this is the same shape, not a new one. + * * Don't error out here, let following poll() routine to * handle it. */ - if (pqFlush(conn) < 0) + if (pqFlushNonBlocking(conn) < 0) elog(LOG, "Failed flushing outbound data to %s:%s", segdbDesc->whoami, PQerrorMessage(conn)); } @@ -601,6 +611,17 @@ checkDispatchResult(CdbDispatcherState *ds, int timeout_sec) fds[nfds].fd = sock; fds[nfds].events = POLLIN; + /* + * Anything the flush above could not place goes out when the peer + * makes room, which is what this asks to be woken for. Only the + * events mask grows: the array keeps one entry per connection in + * the same order, so handlePollSuccess() -- which walks it with + * its own counter and asserts the socket matches -- is unaffected, + * and it tests POLLIN alone. + */ + if (conn->outCount > 0) + fds[nfds].events |= POLLOUT; + /* * poll() reports what is in the socket, which is only the same * thing as "what is left to read" while we are the only thing @@ -1086,7 +1107,8 @@ send_fake_sideband_message(PGconn *conn) if (pqPutMsgStart(GP_SIDEBAND_MESSAGE, conn) < 0) elog(ERROR, "Failed to send sideband message: %s", PQerrorMessage(conn)); - pqPutnchar(marker, sizeof(marker), conn); + if (pqPutnchar(marker, sizeof(marker), conn) < 0) + elog(ERROR, "Failed to send sideband message: %s", PQerrorMessage(conn)); if (pqPutMsgEnd(conn) < 0) elog(ERROR, "Failed to send sideband message: %s", PQerrorMessage(conn)); if (pqFlush(conn) < 0) @@ -1147,10 +1169,10 @@ processNotifies(CdbDispatchResult *dispatchResult) CHECK_FOR_INTERRUPTS(); - int64 last; - int64 cached; - int64 increment; - bool overflow; + int64 last = 0; + int64 cached = 0; + int64 increment = 0; + bool overflow = false; Oid dbid; Oid seq_oid; diff --git a/src/include/catalog/catversion.h b/src/include/catalog/catversion.h index 8ec2c11f0e9..c9c2fd83275 100644 --- a/src/include/catalog/catversion.h +++ b/src/include/catalog/catversion.h @@ -60,6 +60,6 @@ */ /* 3yyymmddN */ -#define CATALOG_VERSION_NO 302609151 +#define CATALOG_VERSION_NO 302608261 #endif diff --git a/src/test/modules/libpq_isbusy_notify/libpq_isbusy_notify.c b/src/test/modules/libpq_isbusy_notify/libpq_isbusy_notify.c index f11d0435d46..ec2da133e15 100644 --- a/src/test/modules/libpq_isbusy_notify/libpq_isbusy_notify.c +++ b/src/test/modules/libpq_isbusy_notify/libpq_isbusy_notify.c @@ -34,10 +34,9 @@ */ #include "postgres_fe.h" -#include - #include "libpq-fe.h" #include "libpq-int.h" +#include "port/pg_bswap.h" /* A NOTIFY as it appears on the wire, minus the leading message type byte. */ static const char notify_channel[] = "anser"; @@ -87,10 +86,10 @@ main(void) conn->inStart = conn->inCursor = 0; conn->inBuffer[pos++] = 'A'; - netlen = htonl(msglen); + netlen = pg_hton32(msglen); memcpy(conn->inBuffer + pos, &netlen, 4); pos += 4; - netpid = htonl(123); + netpid = pg_hton32(123); memcpy(conn->inBuffer + pos, &netpid, 4); pos += 4; memcpy(conn->inBuffer + pos, notify_channel, sizeof(notify_channel)); From da37b64e78a65966ab7a7e9231838e59e6b34c08 Mon Sep 17 00:00:00 2001 From: Leonid Borchuk Date: Mon, 2 Nov 2026 19:04:25 +0300 Subject: [PATCH 16/27] Fix recycling connections with unflushed output A QE connection whose output buffer still holds bytes at cleanup time carries a partially delivered asynchronous payload (an Anser sideband message; normal command traffic is fully flushed before cleanup). The peer may already have read a prefix of it, and nobody flushes the rest once the command is over -- checkDispatchResult only flushes connections whose command is still running. Reusing such a connection makes the next dispatch overwrite the remainder of the buffer (PQsendGpQuery_shared), and the peer then misreads its stream. Have cleanupQE decline to recycle any connection with pending output, so the next dispatch builds a fresh one. --- src/backend/cdb/cdbutil.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/backend/cdb/cdbutil.c b/src/backend/cdb/cdbutil.c index f286f2d1450..82d0e1d0c09 100644 --- a/src/backend/cdb/cdbutil.c +++ b/src/backend/cdb/cdbutil.c @@ -954,6 +954,20 @@ cleanupQE(SegmentDatabaseDescriptor *segdbDesc) if (cdbconn_isBadConnection(segdbDesc)) return false; + /* + * Unflushed output bytes at cleanup time can only be a partially + * delivered asynchronous payload (an Anser sideband message; command + * traffic is fully flushed before cleanup runs). The peer may already + * have read a prefix of it, and nobody will flush the rest now that the + * command is over (checkDispatchResult only flushes connections whose + * command is still running), so the connection is poisoned for reuse: + * the next dispatch would overwrite the remainder of the buffer + * (PQsendGpQuery_shared) and the peer would misread its stream. Decline + * to recycle it; a fresh connection is created on the next dispatch. + */ + if (segdbDesc->conn->outCount > 0) + return false; + /* if segment is down, the gang can not be reused */ if (FtsIsSegmentDown(segdbDesc->segment_database_info)) return false; @@ -3361,6 +3375,20 @@ cleanupQE(SegmentDatabaseDescriptor *segdbDesc) if (cdbconn_isBadConnection(segdbDesc)) return false; + /* + * Unflushed output bytes at cleanup time can only be a partially + * delivered asynchronous payload (an Anser sideband message; command + * traffic is fully flushed before cleanup runs). The peer may already + * have read a prefix of it, and nobody will flush the rest now that the + * command is over (checkDispatchResult only flushes connections whose + * command is still running), so the connection is poisoned for reuse: + * the next dispatch would overwrite the remainder of the buffer + * (PQsendGpQuery_shared) and the peer would misread its stream. Decline + * to recycle it; a fresh connection is created on the next dispatch. + */ + if (segdbDesc->conn->outCount > 0) + return false; + /* if segment is down, the gang can not be reused */ if (FtsIsSegmentDown(segdbDesc->segment_database_info)) return false; From c7c58aceac190d219be01f60492602ce331fdc59 Mon Sep 17 00:00:00 2001 From: Leonid Borchuk Date: Mon, 2 Nov 2026 19:06:59 +0300 Subject: [PATCH 17/27] Rework Anser delivery into NEXT-driven chunks Squashes the review-round commits: the delivery-protocol rework, the debug tracing used to diagnose it, and the single-slot QE assembly. A whole-filter push left tens of MB queued in the QD's output buffer, and on the UDP interconnect it was flushed only while a dispatch socket was readable. Once parts and subscribes were consumed, inbound traffic stopped, flushing stopped with it, and the consumer sat in pq_getmessage() with no deadline of its own -- an uncancellable deadlock, seen as a cursor's FETCH 1 never returning. Deliver the merged payload in chunks instead, one GP_SIDEBAND_MESSAGE each, sending the next only when the consumer asks with a NEXT (kind 'N', its part field carrying the offset wanted). At most one chunk -- sized to fit the connection's send buffer -- is ever in flight, so a consumer walking away leaves nothing torn behind, and every NEXT keeps waking the coordinator: the delivery drives its own servicing. Chunk coordinates (total_len, offset) join the CRC, the QE assembles into one slot and drops chunks for keys it has unsubscribed from, and the wait deadline runs from the last progress, with a hard cap from the subscribe. All waits use the monotonic clock. Wire tag is anser4. Also zero-initialises the dispatcher's buffered[] array (defensive). Track delivery connections with libpq connection-destroy/reset events so InitPlan and error teardown cannot leave dangling PGconn pointers. Register through the existing internal libpq ABI; no new core hook or exported symbol is required. Refuse subscriptions if registration cannot allocate. Make torn-delivery cleanup allocation-free and nonblocking, poisoning connections instead of waiting during error handling. Add a standalone connection-event test covering destruction, reset, registration failure, other event users, and preservation of merged payloads. Validation: connection-event tests pass with -O3 -DNDEBUG and ASan/UBSan; changed C sources pass syntax checks. Cluster regressions remain pending. --- gpcontrib/anser/README.md | 95 ++- .../anser/anser_test/anser_test--1.0.sql | 12 +- gpcontrib/anser/anser_test/anser_test.c | 30 +- gpcontrib/anser/expected/anser_faults.out | 118 +++- .../anser/expected/anser_runtime_filter.out | 119 +++- gpcontrib/anser/expected/anser_test.out | 80 ++- gpcontrib/anser/include/anser.h | 1 + gpcontrib/anser/include/anserplan.h | 2 + gpcontrib/anser/include/ansersideband.h | 47 +- gpcontrib/anser/include/ansertlist.h | 64 ++ gpcontrib/anser/sql/anser_faults.sql | 91 ++- gpcontrib/anser/sql/anser_runtime_filter.sql | 96 ++- gpcontrib/anser/sql/anser_test.sql | 59 +- gpcontrib/anser/src/anserdispatch.c | 599 +++++++++++++----- gpcontrib/anser/src/anserinit.c | 42 +- gpcontrib/anser/src/anserplan.c | 17 +- gpcontrib/anser/src/anserplanexec.c | 3 +- gpcontrib/anser/src/ansersideband.c | 548 +++++++++++----- gpcontrib/anser/test/.gitignore | 1 + gpcontrib/anser/test/Makefile | 19 +- gpcontrib/anser/test/anserdetach_test.c | 175 +++++ src/backend/cdb/dispatcher/cdbdisp_async.c | 4 +- 22 files changed, 1720 insertions(+), 502 deletions(-) create mode 100644 gpcontrib/anser/include/ansertlist.h create mode 100644 gpcontrib/anser/test/anserdetach_test.c diff --git a/gpcontrib/anser/README.md b/gpcontrib/anser/README.md index 5872cf97be0..34367c23eb9 100644 --- a/gpcontrib/anser/README.md +++ b/gpcontrib/anser/README.md @@ -183,11 +183,11 @@ every bitset at 1 MB, whatever the cardinality): | 4 | 4 | ~40 ms | ~520 ms | 549 ms | | 16 | 16 | ~100 ms | ~1140 ms | 1005 ms, then gave up | -These numbers predate the switch to non-blocking delivery. Each write above +These numbers predate the switch to chunked delivery. Each write above blocked until that peer had taken the filter, which is what made a wide slice so expensive — and what could strand the dispatcher outright when nobody was -reading. Delivery now queues and flushes without blocking, so the fan-out should -be well under this; it has not been re-measured. +reading. Delivery now goes one chunk per NEXT request, never blocking, so the +fan-out should be well under this; it has not been re-measured. The second row is still what sets the floor for the deadline: at 16 subscribers the fan-out outlived a 1 s deadline, so every consumer ran unfiltered. The 10 s @@ -324,43 +324,71 @@ applies to the SQL-level `NOTIFY`, which must fit a queue page — but it delive through `pq_sendstring`, so the payload must be a NUL-free string: ``` -anser3 K T SSSSSSSSSS CCCCCCCCCC DDDDDDDDDD PPPPPPPPPP TTTTTTTTTT FFFF KKKK BBBBBBBBBB XXXXXXXX +anser4 K T SSSSSSSSSS CCCCCCCCCC DDDDDDDDDD PPPPPPPPPP TTTTTTTTTT FFFF KKKK BBBBBBBBBB XXXXXXXX ^tag ^ ^ session command condition part total flags keylen bodylen crc | \ payload type \ kind ``` -`kind` is `P` (a producer's part) or `S` (a consumer subscribing) — what the -message *does*, as opposed to the payload type, which is what it *carries*. The -header is **95 bytes of fixed-width ASCII**, so the key and the body begin at -offsets that nothing in their own contents can shift, and the two lengths must -account for the message exactly — a short, long or misaligned message is rejected -before any of it is used. There is deliberately no delimiter anywhere in the -format: the earlier version ended its header with a newline and found it with -`strchr()`, which was correct only while the key could not itself contain a -newline, an invariant nothing enforced. +`kind` is `P` (a producer's part), `S` (a consumer subscribing), `U` +(withdrawing), or `N` (a NEXT: ask for the next delivery chunk — see below; +its part field carries the offset the consumer wants, `total` is 0, and there +is no body) — what the message *does*, as opposed to the payload type, which +is what it *carries*. The header is **95 bytes of fixed-width ASCII**, so the +key and the body begin at offsets that nothing in their own contents can +shift, and the two lengths must account for the message exactly — a short, +long or misaligned message is rejected before any of it is used. There is +deliberately no delimiter anywhere in the format: the earlier version ended +its header with a newline and found it with `strchr()`, which was correct only +while the key could not itself contain a newline, an invariant nothing +enforced. **Coordinator → segment** is a `GP_SIDEBAND_MESSAGE`, written with `pqPutnchar`, which performs no conversion — so the merged filter travels as **raw binary**, with no base64 tax. That is the direction that matters most, since the merged payload is sent once *per consumer* while each part is sent once. +The merged payload travels **in chunks, one message per chunk**, and the +coordinator sends a chunk only when the consumer asks for it with a NEXT — +starting with chunk 0 on subscribe (or on channel completion, whichever is +later). The chunk size is chosen once per connection from its send buffer +(capped at 1 MB, floored at 8 kB), so one flush places a chunk whole and +nothing is left queued. The invariant this buys: **at most one whole chunk is +ever in flight**, so a consumer that walks away mid-delivery — deadline +expired, query finishing — never leaves a torn message behind, and its +withdrawal stops the flow at the chunk boundary. + +``` +payload_type | crc32c | condition_id | session_id | command_count | +flags | total_len | offset | keylen | key | chunk_len | chunk +``` + +`total_len` is the full merged payload size and `offset` is where the chunk +starts in it, so the consumer allocates the assembly buffer once, when chunk 0 +arrives, and every later chunk must name the offset that continues it. A +cancelled delivery is a single chunk with `F_CANCELLED`, `total_len = 0`, +`offset = 0`. Both chunk coordinates are inside the CRC, so a chunk's place in +the filter is as protected as its bytes. + Both directions carry a **CRC32C**, and both discard a message that fails it. The transport is TCP, not the UDP interconnect, so this is not about a lossy link — TCP's 16-bit checksum is simply thin cover for a megabyte, and hardware CRC32C costs about 0.05 ms/MB. The header and key are always covered; whether the body is covered too is the payload type's decision, for the reasons in [Payload types](#payload-types) above. Where it applies, the segment → -coordinator CRC covers the pre-base64 bytes, so it validates the decode as well. +coordinator CRC covers the pre-base64 bytes, so it validates the decode as +well. The coordinator services these while it is blocked receiving tuples: the interconnect adds every dispatch socket to its wait set (`ic_udpifc.c`, `ic_tcp.c`), and a readable one leads to `checkForCancelFromQD` → `cdbdisp_checkForCancel` → `processResults`, which is -where the notify handler runs. Under `gp_interconnect_type=proxy` that check is -driven by a 2 s timer instead (`ic_proxy_backend.c`), so filter delivery can lag -by up to that long. +where the notify handler runs. Chunked delivery is what keeps that loop fed: +every NEXT is inbound traffic, so an in-flight delivery keeps waking the +coordinator until it is done — the consumer drives the flow. Under +`gp_interconnect_type=proxy` that check is driven by a 2 s timer instead +(`ic_proxy_backend.c`), so filter delivery can lag by up to that long. ## GUCs @@ -368,9 +396,11 @@ by up to that long. | --- | --- | --- | --- | | `anser.enable` | `off` | SIGHUP | Master switch. With it off the plan pass never injects anything and no filters are exchanged. | | `anser.runtime_filter` | `off` | USERSET | Enables the post-planning pass that injects bloom-filter producer/consumer nodes into a matching plan. Requires `anser.enable`. | -| `anser.max_info_size` | `65 MB` | USERSET | Maximum serialized payload (merged bloom filter + part header) a channel may hold; caps the effective bloom-filter size. The default is `64 MB + 1 MB` so a full 64 MB power-of-two bitset fits with its header; `bloom_create` also floors every bitset at 1 MB. The ceiling is a little under 1 GB: the payload has to arrive as a single protocol message, and `pq_getmessage()` will not read one past `PQ_LARGE_MESSAGE_LIMIT`. Both ends read it — see [Changing max_info_size](#changing-max_info_size) before touching it on a live session. | +| `anser.max_info_size` | `65 MB` | SUSET | Maximum serialized payload (merged bloom filter + part header) a channel may hold; caps the effective bloom-filter size. The default is `64 MB + 1 MB` so a full 64 MB power-of-two bitset fits with its header; `bloom_create` also floors every bitset at 1 MB. The ceiling is a little under 1 GB: every payload is held whole in a single palloc on each end — the coordinator's merged payload and the consumer's assembly buffer, plus the base64 copy of a part on a producer — so it must stay inside `MaxAllocSize`. Both ends read it — see [Changing max_info_size](#changing-max_info_size) before touching it on a live session. | | `anser.timeout_ms` | `10 s` | USERSET | How long a consumer waits for its filter before running unfiltered. What the wait covers depends on the plan — see [Delivery cost](#delivery-cost). The deadline matters because a producer that gets squelched never publishes at all: `ExecSquelchNode` only marks a `CustomScanState`, it does not call the node back. | -| `anser.debug` | `off` | USERSET | Traces the exchange — publish, merge, delivery, receive — in the log of the process each step happens in. See below. | +| `anser.debug` | `off` | SUSET | Traces the exchange — publish, merge, delivery, receive — in the log of the process each step happens in. See below. | +| `anser.debug_min_filter_bytes` | `0` | SUSET | Test knob: a floor on the bitset size, so a small table still produces a full-size filter. | +| `anser.debug_delivery_chunk_bytes` | `0` | SUSET | Test knob: force the delivery chunk size instead of deriving it from the connection's send buffer, so a test can turn one delivery into hundreds of NEXT round trips. | ### Changing max_info_size @@ -381,9 +411,9 @@ publishes, and passes it to `pq_getmessage()` as the cap on the sideband message it is about to read. A coordinator and a segment that disagree therefore do not merely compute different sizes — they frame the wire differently. -It is `USERSET` rather than `POSTMASTER` because that is how the rest of Anser's -knobs behave, and because being able to try a different filter size without a -cluster restart is worth something. That is a trade-off, not a free choice: +It is `SUSET` rather than `POSTMASTER` because a filter size is worth trying +without a cluster restart, but not worth handing to every role: it drives +per-segment memory and wire traffic. That is a trade-off, not a free choice: **do not change it on a live session.** `GUC_GPDB_NEED_SYNC` gets the new value to the segments — synchronously for every idle one, and in the connection options of every gang created afterwards — but a gang that is busy with an open @@ -408,24 +438,27 @@ producers never published. `anser.debug` makes each step log where it happened, which is normally the fastest way to find where an exchange broke: ``` -seg0 producer init cond=0 part=0/3 elems=3334 payload=67108928 state=ok +seg0 producer init cond=0 part=0/3 elems=3334 payload=1048592 state=ok seg0 producer child exhausted, publishing (state=ok) seg0 published cond=0 part=0/3 bytes=1048592 cancelled=0 sent=1 QD part cond=0 from seg0 (says part 0 of 3) 1/3 bytes=1048592 -> collecting QD part cond=0 from seg1 (says part 1 of 3) 2/3 bytes=1048592 -> collecting QD part cond=0 from seg2 (says part 2 of 3) 3/3 bytes=1048592 -> complete QD delivering cond=0 to 3 subscriber(s) -QD pushed cond=0 bytes=1048592 cancelled=0 -seg0 received cond=0 bytes=1048592 cancelled=0 +QD pushed cond=0 to seg0 chunk=[0,1048592) of 1048592 cancelled=0 +seg0 received cond=0 chunk=[0,1048592) of 1048592 cancelled=0 ``` +(That delivery fits one chunk; anything past the chunk size repeats the last +two lines per NEXT.) + Set it in `postgresql.conf` (`gpconfig -c anser.debug -v on`) rather than with `SET` if you need to see the producer gang: a session-level `SET` does not reliably reach every gang, and the producers are the half you usually want. -A consumer that gives up reports what it saw — `read 0 message(s), 0 -unclaimed` means nothing arrived at all, whereas unclaimed messages mean -something arrived for a channel it was not waiting on. +A consumer that gives up reports what it saw — `received 0 of 0 bytes` means +nothing arrived at all, whereas bytes in the middle mean the delivery stalled +part-way. ## Data flow: producer → merge (bitwise union) → consumer @@ -505,8 +538,10 @@ never in an error raised into the query: - a broken connection → the query is failing anyway, and the interconnect reports it far more usefully than the filter path could; - query cancellation → the consumer's wait is a `CHECK_FOR_INTERRUPTS` loop, and - a delivery that arrives after nobody is waiting is discarded by the QE command - loop (`GP_SIDEBAND_MESSAGE` is accepted and ignored there). + a consumer that walks away mid-delivery withdraws its subscription: the + coordinator stops at the chunk boundary, and the one chunk that may still be + in flight is discarded at the door (not subscribed) or skipped whole by the + QE command loop (`GP_SIDEBAND_MESSAGE` is accepted and ignored there). ## Tests diff --git a/gpcontrib/anser/anser_test/anser_test--1.0.sql b/gpcontrib/anser/anser_test/anser_test--1.0.sql index 42e75341ed9..d94957e7ccf 100644 --- a/gpcontrib/anser/anser_test/anser_test--1.0.sql +++ b/gpcontrib/anser/anser_test/anser_test--1.0.sql @@ -82,7 +82,8 @@ CREATE FUNCTION anser_test_wire_roundtrip( condition_key text, body bytea, tamper text, - payload_type "char") + payload_type "char", + kind "char") RETURNS text AS 'MODULE_PATHNAME' LANGUAGE C STRICT; @@ -94,6 +95,8 @@ CREATE FUNCTION anser_test_push_crc( session_id int4, command_count int4, flags int4, + total_len int4, + chunk_offset int4, condition_key text, body bytea) RETURNS int8 @@ -153,3 +156,10 @@ CREATE FUNCTION anser_test_unsubscribes() RETURNS int8 AS 'MODULE_PATHNAME' LANGUAGE C STRICT; + +-- Deliveries the coordinator has completed in this backend, so a test can tell +-- "the filter arrived" apart from "the consumer gave up and ran unfiltered". +CREATE FUNCTION anser_test_deliveries_completed() +RETURNS int8 +AS 'MODULE_PATHNAME' +LANGUAGE C STRICT; diff --git a/gpcontrib/anser/anser_test/anser_test.c b/gpcontrib/anser/anser_test/anser_test.c index 2c2dfe8e9c1..e69ce6d3e1b 100644 --- a/gpcontrib/anser/anser_test/anser_test.c +++ b/gpcontrib/anser/anser_test/anser_test.c @@ -644,6 +644,7 @@ anser_test_wire_roundtrip(PG_FUNCTION_ARGS) bytea *body = PG_GETARG_BYTEA_PP(1); char *tamper = text_to_cstring(PG_GETARG_TEXT_PP(2)); char payload_type = PG_GETARG_CHAR(3); + char kind = PG_GETARG_CHAR(4); AnserChannelKey key; AnserWireMsg parsed; char *msg; @@ -654,7 +655,7 @@ anser_test_wire_roundtrip(PG_FUNCTION_ARGS) int decoded_len = 0; anser_test_wire_key(&key, condition_key); - msg = AnserWireFormat(&key, ANSER_WIRE_KIND_PART, payload_type, + msg = AnserWireFormat(&key, kind, payload_type, ANSER_TEST_WIRE_PART, ANSER_TEST_WIRE_TOTAL, 0, raw_len > 0 ? raw_body : NULL, (Size) raw_len); msg_len = strlen(msg); @@ -730,7 +731,7 @@ anser_test_wire_roundtrip(PG_FUNCTION_ARGS) PG_RETURN_TEXT_P(cstring_to_text("checksum-mismatch")); /* Framed and vouched for: now every field must have survived the trip. */ - if (parsed.kind != ANSER_WIRE_KIND_PART) + if (parsed.kind != kind) PG_RETURN_TEXT_P(cstring_to_text("mismatch: kind")); if (parsed.payload_type != payload_type) PG_RETURN_TEXT_P(cstring_to_text("mismatch: payload_type")); @@ -767,13 +768,16 @@ anser_test_push_crc(PG_FUNCTION_ARGS) int32 session_id = PG_GETARG_INT32(2); int32 command_count = PG_GETARG_INT32(3); int32 flags = PG_GETARG_INT32(4); - char *condition_key = text_to_cstring(PG_GETARG_TEXT_PP(5)); - bytea *body = PG_GETARG_BYTEA_PP(6); + int32 total_len = PG_GETARG_INT32(5); + int32 offset = PG_GETARG_INT32(6); + char *condition_key = text_to_cstring(PG_GETARG_TEXT_PP(7)); + bytea *body = PG_GETARG_BYTEA_PP(8); PG_RETURN_INT64((int64) (uint32) AnserWirePushCrc(payload_type, (uint32) condition_id, (uint32) session_id, (uint32) command_count, - (uint32) flags, condition_key, + (uint32) flags, (uint32) total_len, + (uint32) offset, condition_key, (int) strlen(condition_key), VARDATA_ANY(body), (int) VARSIZE_ANY_EXHDR(body))); @@ -1068,3 +1072,19 @@ anser_test_unsubscribes(PG_FUNCTION_ARGS) { PG_RETURN_INT64(AnserDispatchUnsubscribeCount()); } + +PG_FUNCTION_INFO_V1(anser_test_deliveries_completed); + +/* + * Deliveries that reached their last chunk in this backend. + * + * The counterpart of anser_test_unsubscribes: a test that wants to know the + * filter really arrived -- rather than the consumer quietly running + * unfiltered after its deadline, which produces the same rows -- reads the + * difference across a query, together with the unsubscribe count staying put. + */ +Datum +anser_test_deliveries_completed(PG_FUNCTION_ARGS) +{ + PG_RETURN_INT64(AnserDispatchDeliveriesCompleted()); +} diff --git a/gpcontrib/anser/expected/anser_faults.out b/gpcontrib/anser/expected/anser_faults.out index 05df2ab394b..43f41d49dce 100644 --- a/gpcontrib/anser/expected/anser_faults.out +++ b/gpcontrib/anser/expected/anser_faults.out @@ -21,6 +21,9 @@ NOTICE: extension "gp_inject_fault" already exists, skipping SET enable_nestloop = off; SET enable_mergejoin = off; SET optimizer = off; +-- Trace every exchange step into the server logs (never reaches the client), +-- so a CI run that goes wrong can be diagnosed from the log artifacts. +SET anser.debug = on; -- nextval guard: the QD sends two kinds of message down a QE's dispatch -- connection -- the nextval reply and a sideband message -- and each has its -- own reader. A reader that meets the other's message has to skip it whole: @@ -159,17 +162,12 @@ DROP EXTENSION anser_test; DROP TABLE anser_rf_unsub_build, anser_rf_unsub_probe; -- A partially delivered filter must not wedge the next command. -- --- The push above is torn when the filter is bigger than the connection --- buffers: the consumers give up and finish, the coordinator's non-blocking --- write leaves the tail queued, and the poll loop stops flushing once the --- peer's command completes. The next dispatch on the same gang would then --- overwrite the queued bytes (PQsendGpQuery_shared), leaving the QE waiting --- forever for a body that never comes -- the session only ended with the --- cluster. ExecutorEnd now completes the flush, or poisons the connection. --- --- The filter is forced to 64 MB (anser.debug_min_filter_bytes) so the push --- cannot fit the buffers even on loopback. This test hangs rather than fails --- if the fix is reverted, the same warning as the push test above. +-- With chunked delivery there is no torn 64 MB push any more: the coordinator +-- sends one chunk and waits to be asked for the next. What can still be in +-- flight when every consumer has given up is exactly one chunk -- the QE is +-- busy on its probe side by then, so the chunk sits in the stream until the +-- command loop reads and skips it whole (postgres.c, GP_SIDEBAND_MESSAGE). +-- The next command on the same gang must answer. SET anser.timeout_ms = 100; SET anser.debug_min_filter_bytes = 67108864; CREATE TABLE anser_rf_stale_build (id int, pad text) DISTRIBUTED BY (pad); @@ -179,7 +177,7 @@ INSERT INTO anser_rf_stale_probe SELECT g % 1000 + 1 FROM generate_series(1, 100 ANALYZE anser_rf_stale_build; ANALYZE anser_rf_stale_probe; -- Hold the delivery for 1 s so every consumer gives up at 100 ms and the --- 64 MB filter is pushed to connections nobody is reading. +-- first chunk goes out to connections nobody is reading. SELECT gp_inject_fault('anser_before_deliver', 'sleep', '', '', '', 1, 1, 1, dbid) FROM gp_segment_configuration WHERE role = 'p' AND content = -1; gp_inject_fault @@ -202,8 +200,8 @@ SELECT gp_inject_fault('anser_before_deliver', 'reset', dbid) Success: (1 row) --- The torn delivery is completed (or its connection poisoned) at ExecutorEnd, --- so the next command on the same gang must answer. +-- The leftover chunk is skipped whole, so the next command on the same gang +-- must answer. SELECT 'next command' AS marker, count(*) AS build_rows FROM anser_rf_stale_build; marker | build_rows --------------+------------ @@ -212,22 +210,87 @@ SELECT 'next command' AS marker, count(*) AS build_rows FROM anser_rf_stale_buil DROP TABLE anser_rf_stale_build, anser_rf_stale_probe; RESET anser.debug_min_filter_bytes; --- A filter parked by one statement must not be claimed by the next. +-- Walking away mid-delivery must end it cleanly. The fault pauses the +-- consumer after its first chunk for longer than its deadline -- as a skip +-- fault: the consumer itself waits out anser.timeout_ms plus a margin, on the +-- monotonic clock, because a 'sleep' fault is a bare nanosleep that any +-- signal cuts short. The consumer withdraws, the coordinator's delivery +-- stalls at the chunk boundary, and the one chunk in flight is discarded at +-- the door: the key is no longer subscribed, so read_one drops it without a +-- NEXT. Results must still be exact, the coordinator must record the +-- withdrawals, and the next command on the same gang must answer. +SET anser.timeout_ms = 500; +SET anser.debug_delivery_chunk_bytes = 8192; +SET anser.debug_min_filter_bytes = 67108864; +CREATE TABLE anser_rf_ab_build (id int, pad text) DISTRIBUTED BY (pad); +CREATE TABLE anser_rf_ab_probe (id int) DISTRIBUTED BY (id); +INSERT INTO anser_rf_ab_build SELECT g, 'b' || g FROM generate_series(1, 1000) g; +INSERT INTO anser_rf_ab_probe SELECT g % 1000 + 1 FROM generate_series(1, 200000) g; +ANALYZE anser_rf_ab_build; +ANALYZE anser_rf_ab_probe; +-- start_ignore +CREATE EXTENSION IF NOT EXISTS anser_test; +NOTICE: extension "anser_test" already exists, skipping +-- end_ignore +SELECT anser_test_unsubscribes() AS unsub_before \gset +-- Pause every consumer after its first chunk for longer than its deadline. +SELECT gp_inject_fault('anser_after_chunk', 'skip', '', '', '', 1, 1, 0, dbid) + FROM gp_segment_configuration WHERE role = 'p' AND content >= 0; + gp_inject_fault +----------------- + Success: + Success: + Success: +(3 rows) + +SET anser.runtime_filter = on; +SELECT count(*) AS abort_rows + FROM anser_rf_ab_probe p JOIN anser_rf_ab_build b ON p.id = b.id; + abort_rows +------------ + 200000 +(1 row) + +SELECT gp_inject_fault('anser_after_chunk', 'reset', dbid) + FROM gp_segment_configuration WHERE role = 'p' AND content >= 0; + gp_inject_fault +----------------- + Success: + Success: + Success: +(3 rows) + +SELECT anser_test_unsubscribes() > :unsub_before AS consumers_withdrew; + consumers_withdrew +-------------------- + t +(1 row) + +SELECT 'next command' AS marker, count(*) AS build_rows FROM anser_rf_ab_build; + marker | build_rows +--------------+------------ + next command | 1000 +(1 row) + +DROP EXTENSION anser_test; +DROP TABLE anser_rf_ab_build, anser_rf_ab_probe; +RESET anser.debug_min_filter_bytes; +RESET anser.debug_delivery_chunk_bytes; +-- A filter from one statement must not be claimed by the next. -- --- A consumer that waits while a delivery for another channel arrives parks it --- in the inbox. If the statement then errors inside a savepoint, there is no --- ExecutorEnd for it (a failed portal skips it), so the parked entry used to --- survive -- and the next statement, whose condition keys restart at the same --- values, claimed it: a filter built from the old build side pruned the new --- one. Deliveries now carry the session id and command count and a consumer --- matches on them exactly, and the subtransaction abort drops the inbox. +-- A consumer that runs out of time withdraws, and with chunked delivery the +-- coordinator stops at the chunk boundary: the one chunk in flight is dropped +-- at the door, because the key is no longer subscribed. And even a filter +-- that somehow got parked whole could not be claimed by the next statement: +-- condition keys restart at the same values, but the session id and command +-- count stamped on the wire must match exactly. -- -- The LEFT JOIN pins j2 as the build side, so its consumer waits first: it --- gives up at 700 ms while the coordinator sleeps for 1 s, its late delivery --- is then read -- and parked -- by j1's consumer, and the division errors --- before anything claims it. The second run re-executes the same plan shape --- with a build side disjoint from the parked filter: claiming it would drop --- every joinable row, so count(j2.id) must be 1000, not 0. +-- gives up at 700 ms while the coordinator sleeps for 1 s, its late first +-- chunk is then read -- and dropped -- while j1's consumer waits, and the +-- division errors. The second run re-executes the same plan shape with a +-- build side disjoint from the parked filter: claiming anything stale would +-- drop every joinable row, so count(j2.id) must be 1000, not 0. SET anser.timeout_ms = 700; CREATE TABLE anser_sf_b1 (id int, pad text) DISTRIBUTED BY (pad); CREATE TABLE anser_sf_p1 (id int) DISTRIBUTED BY (id); @@ -278,6 +341,7 @@ SELECT gp_inject_fault('anser_before_deliver', 'reset', dbid) DROP TABLE anser_sf_b1, anser_sf_p1, anser_sf_b2, anser_sf_p2; RESET anser.runtime_filter; +RESET anser.debug; RESET anser.timeout_ms; RESET optimizer; RESET enable_nestloop; diff --git a/gpcontrib/anser/expected/anser_runtime_filter.out b/gpcontrib/anser/expected/anser_runtime_filter.out index 2cd24b12ff1..e8092995007 100644 --- a/gpcontrib/anser/expected/anser_runtime_filter.out +++ b/gpcontrib/anser/expected/anser_runtime_filter.out @@ -14,6 +14,10 @@ SET enable_mergejoin = off; -- Pinned shorter here, so a filter that never arrives fails the run instead of -- hiding in the clock. SET anser.timeout_ms = 5000; +-- Trace every exchange step into the server logs for the whole file. LOG +-- level never reaches the client, so the expected output is unaffected; the +-- traces are for the CI logs when a run goes wrong. +SET anser.debug = on; -- build is the smaller (hashed / preserved) side, distributed so the join key -- must be redistributed; probe is the larger side distributed by the join key. CREATE TABLE anser_rf_build (id int, name text) DISTRIBUTED BY (name); @@ -444,8 +448,8 @@ SELECT b.name, p.payload FROM anser_rf_build b LEFT JOIN anser_rf_probe p ON b.i -- A plain EXPLAIN must not allocate the filter: the coordinator initializes -- every slice with EXEC_FLAG_EXPLAIN_ONLY, so the producer must skip building -- the bitset (state=NULL) while the plan still shows the planned size. The --- debug log makes the decision observable. -SET anser.debug = on; +-- debug log makes the decision observable. anser.debug is already on for the +-- whole file; client_min_messages is what brings the line to the client here. SET client_min_messages = log; EXPLAIN (COSTS OFF) SELECT b.name, p.payload FROM anser_rf_build b LEFT JOIN anser_rf_probe p ON b.id = p.id; @@ -472,7 +476,6 @@ LOG: anser: producer init cond=0 part=0/1 elems=33554432 payload=67108928 state RESET client_min_messages; LOG: statement: RESET client_min_messages; -RESET anser.debug; -- And the filter must still be correct: 200 build rows, each matching one -- probe row. SELECT count(*) AS rows_joined @@ -493,6 +496,52 @@ SELECT injected, total_elems, planned_bytes FROM anser_test_rf_size(200); (1 row) DROP EXTENSION anser_test; +-- The same exchange in chunks: an 8 kB chunk turns the 64 MB filter into +-- thousands of NEXT round trips. The result must match the unfiltered run and +-- the query must simply finish -- a whole-filter push that outgrew the +-- connection buffers is exactly what used to wedge the session. Matching +-- results alone would not prove it: a consumer that gives up produces the +-- same rows, so the coordinator's counters must say every consumer was +-- served and none withdrew. +-- start_ignore +CREATE EXTENSION IF NOT EXISTS anser_test; +-- end_ignore +SELECT anser_test_deliveries_completed() AS d_before \gset +SELECT anser_test_unsubscribes() AS u_before \gset +SET anser.debug_delivery_chunk_bytes = 8192; +SET anser.runtime_filter = on; +CREATE TEMP TABLE anser_rf_r_chunk AS + SELECT b.name, p.payload FROM anser_rf_build b LEFT JOIN anser_rf_probe p ON b.id = p.id + DISTRIBUTED BY (name); +SELECT anser_test_deliveries_completed() = :d_before + 3 AS all_delivered; + all_delivered +--------------- + t +(1 row) + +SELECT anser_test_unsubscribes() = :u_before AS nobody_gave_up; + nobody_gave_up +---------------- + t +(1 row) + +SELECT count(*) AS only_chunk + FROM (SELECT * FROM anser_rf_r_chunk EXCEPT ALL SELECT * FROM anser_rf_r_off) d; + only_chunk +------------ + 0 +(1 row) + +SELECT count(*) AS only_off_chunk + FROM (SELECT * FROM anser_rf_r_off EXCEPT ALL SELECT * FROM anser_rf_r_chunk) d; + only_off_chunk +---------------- + 0 +(1 row) + +DROP TABLE anser_rf_r_chunk; +DROP EXTENSION anser_test; +RESET anser.debug_delivery_chunk_bytes; -- A LIMIT can finish the query while a consumer is still waiting for its -- filter: the producer is squelched before it publishes, the consumer must -- bail out via QueryFinishPending and withdraw its subscription, and the @@ -509,9 +558,20 @@ SELECT count(*) AS limited_rows FROM ( -- filter exchange completes inside the FETCH's ExecutorRun window, so a -- sibling statement's ExecutorEnd sweep has nothing of the cursor's to wipe. -- The sibling runs the same filtered join, so it really does create and sweep --- its own channels while the cursor's gang sits suspended. (A wiped exchange --- would fail open with the same rows, so this guards the behavior, not the --- timing: that path is a wait, not a wrong answer.) +-- its own channels while the cursor's gang sits suspended. The delivery +-- counter must also say every consumer was served -- because "came back +-- unfiltered" returns the same rows and would hide a broken exchange. (The +-- sweep's failure mode itself is a wait, not a wrong answer.) The cursor +-- query filters the build side to ids 1..3, and on a segment none of them +-- land on the hash is empty, the join skips the probe side, and that +-- consumer never runs -- so its count comes from the data, not from the +-- cluster width. The sibling has no such filter, so it serves all three. +-- start_ignore +CREATE EXTENSION IF NOT EXISTS anser_test; +-- end_ignore +SELECT anser_test_deliveries_completed() AS d_before \gset +SELECT count(DISTINCT gp_segment_id) AS cursor_consumers + FROM anser_rf_probe WHERE id <= 3 \gset SET optimizer = off; SET anser.runtime_filter = on; BEGIN; @@ -539,6 +599,14 @@ FETCH ALL anser_rf_cur; (2 rows) COMMIT; +SELECT anser_test_deliveries_completed() + = :d_before + 3 + :cursor_consumers AS cursor_delivered; + cursor_delivered +------------------ + t +(1 row) + +DROP EXTENSION anser_test; DROP TABLE anser_rf_r_push; DROP TABLE anser_rf_r_on, anser_rf_r_off, anser_rf_r_orca; DROP TABLE anser_rf_build, anser_rf_probe; @@ -964,6 +1032,44 @@ SELECT count(*) AS cast_rows DROP TABLE anser_rf_p, anser_rf_o; RESET gp_enable_runtime_filter; +-- A filter over another filter must resolve the child's INDEX_VAR outputs +-- in the child's namespace, not copy them into a self-referencing namespace. +CREATE TABLE anser_stack_probe (k1 int, k2 int) DISTRIBUTED RANDOMLY; +CREATE TABLE anser_stack_b1 (k int) DISTRIBUTED RANDOMLY; +CREATE TABLE anser_stack_b2 (k int) DISTRIBUTED RANDOMLY; +INSERT INTO anser_stack_probe SELECT i % 100, i % 50 FROM generate_series(1, 10000) i; +INSERT INTO anser_stack_b1 SELECT i FROM generate_series(0, 99) i; +INSERT INTO anser_stack_b2 SELECT i FROM generate_series(0, 49) i; +ANALYZE anser_stack_probe; +ANALYZE anser_stack_b1; +ANALYZE anser_stack_b2; +DO $$ +DECLARE + opt text; + plan jsonb; +BEGIN + PERFORM set_config('anser.runtime_filter', 'on', true); + FOREACH opt IN ARRAY ARRAY['off', 'on'] LOOP + PERFORM set_config('optimizer', opt, true); + EXECUTE 'EXPLAIN (VERBOSE, COSTS OFF, FORMAT JSON) + SELECT p.k1, p.k2 FROM anser_stack_probe p + JOIN anser_stack_b1 b1 ON p.k1 = b1.k + JOIN anser_stack_b2 b2 ON p.k2 = b2.k' INTO plan; + IF NOT EXISTS ( + WITH RECURSIVE walk(node) AS ( + SELECT plan->0->'Plan' + UNION ALL + SELECT child FROM walk CROSS JOIN LATERAL + jsonb_array_elements(coalesce(node->'Plans', '[]')) child + ) SELECT FROM walk + WHERE node->>'Custom Plan Provider' = 'Anser Bloom Consumer' + AND node->'Plans'->0->>'Custom Plan Provider' = 'Anser Bloom Consumer') THEN + RAISE EXCEPTION 'stacked-consumer EXPLAIN fixture changed (optimizer=%): %', opt, plan; + END IF; + END LOOP; +END +$$; +DROP TABLE anser_stack_probe, anser_stack_b1, anser_stack_b2; -- Resource and log knobs are superuser-only: an unprivileged role must not -- force 64 MB filters on every segment or fill the server log. runtime_filter -- and timeout_ms only shape the caller's own queries, so they stay @@ -983,6 +1089,7 @@ RESET ROLE; DROP ROLE anser_nopriv; RESET anser.runtime_filter; RESET anser.debug_min_filter_bytes; +RESET anser.debug; RESET anser.timeout_ms; RESET optimizer; RESET enable_nestloop; diff --git a/gpcontrib/anser/expected/anser_test.out b/gpcontrib/anser/expected/anser_test.out index ff3da23d1ea..70f8f574489 100644 --- a/gpcontrib/anser/expected/anser_test.out +++ b/gpcontrib/anser/expected/anser_test.out @@ -180,16 +180,17 @@ FROM (VALUES -- --------------------------------------------------------------------------- -- The bytes, exactly. Session/command/condition are 42/7/3 and part is 1 of 3, -- so these are golden values: any change to the layout, the field widths or --- what the checksum covers shows up here as a diff. Note the third row -- a +-- what the checksum covers shows up here as a diff. Note the fourth row -- a -- cancelled message carries no body even though one was passed, and its -- checksum must be computed over what is actually sent. -- -- That the message comes back as text is itself the check that it is NUL-free, --- which a NOTIFY payload has to be: the fourth row's body is 00 ff 00. +-- which a NOTIFY payload has to be: the fifth row's body is 00 ff 00. SELECT t.what, length(m.msg) AS len, m.msg FROM (VALUES ('part with a 3-byte body', 'P', 'B', 0, '\x616263'::bytea), ('subscription, no body', 'S', '-', 0, '\x'::bytea), + ('next request, no body', 'N', '-', 0, '\x'::bytea), ('cancelled: body suppressed', 'P', 'B', 1, '\x616263'::bytea), ('body of NUL and 0xff bytes', 'P', 'B', 0, '\x00ff00'::bytea)) AS t(what, kind, ptype, flags, body), @@ -198,11 +199,12 @@ FROM (VALUES AS msg) m; what | len | msg ----------------------------+-----+--------------------------------------------------------------------------------------------------------------- - part with a 3-byte body | 109 | anser3 P B 0000000042 0000000007 0000000003 0000000001 0000000003 0000 0010 0000000004 f7b3c0d5anser_rf_3YWJj - subscription, no body | 105 | anser3 S - 0000000042 0000000007 0000000003 0000000001 0000000003 0000 0010 0000000000 ecd98662anser_rf_3 - cancelled: body suppressed | 105 | anser3 P B 0000000042 0000000007 0000000003 0000000001 0000000003 0001 0010 0000000000 e88148e9anser_rf_3 - body of NUL and 0xff bytes | 109 | anser3 P B 0000000042 0000000007 0000000003 0000000001 0000000003 0000 0010 0000000004 027f789banser_rf_3AP8A -(4 rows) + part with a 3-byte body | 109 | anser4 P B 0000000042 0000000007 0000000003 0000000001 0000000003 0000 0010 0000000004 c43569b0anser_rf_3YWJj + subscription, no body | 105 | anser4 S - 0000000042 0000000007 0000000003 0000000001 0000000003 0000 0010 0000000000 74e20b74anser_rf_3 + next request, no body | 105 | anser4 N - 0000000042 0000000007 0000000003 0000000001 0000000003 0000 0010 0000000000 d4c4eb99anser_rf_3 + cancelled: body suppressed | 105 | anser4 P B 0000000042 0000000007 0000000003 0000000001 0000000003 0001 0010 0000000000 70bac5ffanser_rf_3 + body of NUL and 0xff bytes | 109 | anser4 P B 0000000042 0000000007 0000000003 0000000001 0000000003 0000 0010 0000000004 31f9d1feanser_rf_3AP8A +(5 rows) -- Alter exactly one byte (or one length) and see what the reader does. -- @@ -213,7 +215,7 @@ FROM (VALUES -- which is why the checksum covers the header and not just the payload. SELECT t.tamper, anser_test_wire_roundtrip('anser_rf_3', '\x616263'::bytea, t.tamper, - 'B'::"char") AS outcome + 'B'::"char", 'P'::"char") AS outcome FROM (VALUES (''), ('truncate'), ('append'), ('bodylen'), ('tag'), ('kind'), ('type'), ('crc'), ('key'), ('body')) AS t(tamper); tamper | outcome @@ -230,17 +232,27 @@ FROM (VALUES (''), ('truncate'), ('append'), ('bodylen'), ('tag'), ('kind'), body | checksum-mismatch (10 rows) +-- A NEXT request is a message of its own kind: no body, and part_index carries +-- the offset the consumer wants next. The parser must accept it and every +-- field must survive. +SELECT anser_test_wire_roundtrip('anser_rf_3', '\x'::bytea, '', '-'::"char", + 'N'::"char") AS next_roundtrip; + next_roundtrip +---------------- + ok +(1 row) + -- Keys and bodies that must survive the trip unchanged. "ok" means every -- field and the decoded body came back identical, so each of these is a case -- where a naive framing would have gone wrong: the key travels as raw bytes -- and is taken by length, which is what lets it contain a newline, a space, or -- the protocol's own tag without any escaping. SELECT t.what, - anser_test_wire_roundtrip(t.key, t.body, '', 'B'::"char") AS outcome + anser_test_wire_roundtrip(t.key, t.body, '', 'B'::"char", 'P'::"char") AS outcome FROM (VALUES ('plain key', 'anser_rf_3' , '\x616263'::bytea), ('key containing a newline', E'rf:a\nb' , '\x616263'::bytea), - ('key that looks like a header', 'anser3 P B 0000000000', '\x616263'::bytea), + ('key that looks like a header', 'anser4 P B 0000000000', '\x616263'::bytea), ('key with spaces and quotes', 'rf:"a b".c' , '\x616263'::bytea), ('key at the 63-byte limit', repeat('k', 63) , '\x616263'::bytea), ('empty key', '' , '\x616263'::bytea), @@ -264,29 +276,33 @@ FROM (VALUES (10 rows) -- The QD -> QE checksum. The value itself does not matter; which inputs --- change it does. Every routing field and the key are always covered, and the --- body only for a payload type that asks for it -- the other_body_exempt column --- is that deliberate exemption, not an oversight. +-- change it does. Every routing field, the chunk's coordinates and the key +-- are always covered, and the body only for a payload type that asks for it -- +-- the other_body_exempt column is that deliberate exemption, not an oversight. SELECT - anser_test_push_crc('B'::"char", 1, 7, 3, 0, 'k', '\x01') - <> anser_test_push_crc('B'::"char", 2, 7, 3, 0, 'k', '\x01') AS condition_covered, - anser_test_push_crc('B'::"char", 1, 7, 3, 0, 'k', '\x01') - <> anser_test_push_crc('B'::"char", 1, 8, 3, 0, 'k', '\x01') AS session_covered, - anser_test_push_crc('B'::"char", 1, 7, 3, 0, 'k', '\x01') - <> anser_test_push_crc('B'::"char", 1, 7, 4, 0, 'k', '\x01') AS command_covered, - anser_test_push_crc('B'::"char", 1, 7, 3, 0, 'k', '\x01') - <> anser_test_push_crc('B'::"char", 1, 7, 3, 1, 'k', '\x01') AS flags_covered, - anser_test_push_crc('B'::"char", 1, 7, 3, 0, 'k', '\x01') - <> anser_test_push_crc('B'::"char", 1, 7, 3, 0, 'j', '\x01') AS key_covered, - anser_test_push_crc('B'::"char", 1, 7, 3, 0, 'k', '\x01') - <> anser_test_push_crc('B'::"char", 1, 7, 3, 0, 'k', '\x02') AS bloom_body_covered, - anser_test_push_crc('-'::"char", 1, 7, 3, 0, 'k', '\x01') - = anser_test_push_crc('-'::"char", 1, 7, 3, 0, 'k', '\x02') AS other_body_exempt, - anser_test_push_crc('B'::"char", 1, 7, 3, 0, 'k', '\x01') - <> anser_test_push_crc('-'::"char", 1, 7, 3, 0, 'k', '\x01') AS type_covered; - condition_covered | session_covered | command_covered | flags_covered | key_covered | bloom_body_covered | other_body_exempt | type_covered --------------------+-----------------+-----------------+---------------+-------------+--------------------+-------------------+-------------- - t | t | t | t | t | t | t | t + anser_test_push_crc('B'::"char", 1, 7, 3, 0, 1000, 0, 'k', '\x01') + <> anser_test_push_crc('B'::"char", 2, 7, 3, 0, 1000, 0, 'k', '\x01') AS condition_covered, + anser_test_push_crc('B'::"char", 1, 7, 3, 0, 1000, 0, 'k', '\x01') + <> anser_test_push_crc('B'::"char", 1, 8, 3, 0, 1000, 0, 'k', '\x01') AS session_covered, + anser_test_push_crc('B'::"char", 1, 7, 3, 0, 1000, 0, 'k', '\x01') + <> anser_test_push_crc('B'::"char", 1, 7, 4, 0, 1000, 0, 'k', '\x01') AS command_covered, + anser_test_push_crc('B'::"char", 1, 7, 3, 0, 1000, 0, 'k', '\x01') + <> anser_test_push_crc('B'::"char", 1, 7, 3, 1, 1000, 0, 'k', '\x01') AS flags_covered, + anser_test_push_crc('B'::"char", 1, 7, 3, 0, 1000, 0, 'k', '\x01') + <> anser_test_push_crc('B'::"char", 1, 7, 3, 0, 2000, 0, 'k', '\x01') AS total_covered, + anser_test_push_crc('B'::"char", 1, 7, 3, 0, 1000, 0, 'k', '\x01') + <> anser_test_push_crc('B'::"char", 1, 7, 3, 0, 1000, 64, 'k', '\x01') AS offset_covered, + anser_test_push_crc('B'::"char", 1, 7, 3, 0, 1000, 0, 'k', '\x01') + <> anser_test_push_crc('B'::"char", 1, 7, 3, 0, 1000, 0, 'j', '\x01') AS key_covered, + anser_test_push_crc('B'::"char", 1, 7, 3, 0, 1000, 0, 'k', '\x01') + <> anser_test_push_crc('B'::"char", 1, 7, 3, 0, 1000, 0, 'k', '\x02') AS bloom_body_covered, + anser_test_push_crc('-'::"char", 1, 7, 3, 0, 1000, 0, 'k', '\x01') + = anser_test_push_crc('-'::"char", 1, 7, 3, 0, 1000, 0, 'k', '\x02') AS other_body_exempt, + anser_test_push_crc('B'::"char", 1, 7, 3, 0, 1000, 0, 'k', '\x01') + <> anser_test_push_crc('-'::"char", 1, 7, 3, 0, 1000, 0, 'k', '\x01') AS type_covered; + condition_covered | session_covered | command_covered | flags_covered | total_covered | offset_covered | key_covered | bloom_body_covered | other_body_exempt | type_covered +-------------------+-----------------+-----------------+---------------+---------------+----------------+-------------+--------------------+-------------------+-------------- + t | t | t | t | t | t | t | t | t | t (1 row) -- 5. The runtime-filter pushdown decision. A scan key is numbered by the diff --git a/gpcontrib/anser/include/anser.h b/gpcontrib/anser/include/anser.h index d1862f93082..5c87f3295d4 100644 --- a/gpcontrib/anser/include/anser.h +++ b/gpcontrib/anser/include/anser.h @@ -87,6 +87,7 @@ extern PGDLLEXPORT bool gp_anser_debug; extern PGDLLEXPORT int gp_anser_max_info_size; extern PGDLLEXPORT int gp_anser_timeout_ms; extern PGDLLEXPORT int gp_anser_debug_min_filter_bytes; +extern PGDLLEXPORT int gp_anser_debug_delivery_chunk_bytes; /* * Trace the handoff between producers, the coordinator and consumers. diff --git a/gpcontrib/anser/include/anserplan.h b/gpcontrib/anser/include/anserplan.h index 6dd9adffd87..35362c0ee78 100644 --- a/gpcontrib/anser/include/anserplan.h +++ b/gpcontrib/anser/include/anserplan.h @@ -150,6 +150,8 @@ extern PGDLLEXPORT bool AnserResolveKeyScan(Plan *top, * full QEs, each publishing for itself, and the two optimizers choose different * widths for the same query -- see the implementation. */ +extern PGDLLEXPORT int AnserMaxPlanNodeId(PlannedStmt *stmt); + extern PGDLLEXPORT int AnserSliceProducers(PlannedStmt *stmt, int slice_index); /* diff --git a/gpcontrib/anser/include/ansersideband.h b/gpcontrib/anser/include/ansersideband.h index 4c1f773635c..fc820b050b6 100644 --- a/gpcontrib/anser/include/ansersideband.h +++ b/gpcontrib/anser/include/ansersideband.h @@ -68,15 +68,23 @@ struct pgNotify; /* #include "libpq-fe.h" */ #define ANSER_NOTIFY_CHANNEL "anser_rf" /* First token of every QE -> QD payload; bump when the format changes. */ -#define ANSER_WIRE_TAG "anser3" +#define ANSER_WIRE_TAG "anser4" /* * Message kinds (QE -> QD): what the message does. What it carries is a * separate field, the payload type -- see anserpayload.h. + * + * NEXT is the flow-control request of the chunked delivery: the coordinator + * pushes one chunk per GP_SIDEBAND_MESSAGE and waits for the consumer to ask + * for the following one, so no more than one chunk -- always smaller than the + * connection's send buffer -- is ever in flight. In a NEXT, part_index is + * the offset the consumer wants next (what it has received so far), + * total_parts is 0, and there is no body. */ #define ANSER_WIRE_KIND_PART 'P' /* a producer's serialized part */ #define ANSER_WIRE_KIND_SUBSCRIBE 'S' /* a consumer registering interest */ #define ANSER_WIRE_KIND_UNSUBSCRIBE 'U' /* ... and withdrawing it again */ +#define ANSER_WIRE_KIND_NEXT 'N' /* request the next chunk; see above */ /* Flag bits, shared by both directions. */ #define ANSER_WIRE_F_CANCELLED 0x0001 @@ -85,7 +93,7 @@ struct pgNotify; /* #include "libpq-fe.h" */ * QE -> QD header: fixed width, so the key and body start at known offsets and * nothing about their contents can affect parsing. * - * anser3 K T SSSSSSSSSS CCCCCCCCCC DDDDDDDDDD PPPPPPPPPP TTTTTTTTTT FFFF KKKK BBBBBBBBBB XXXXXXXX + * anser4 K T SSSSSSSSSS CCCCCCCCCC DDDDDDDDDD PPPPPPPPPP TTTTTTTTTT FFFF KKKK BBBBBBBBBB XXXXXXXX * ^tag ^ ^ session command condition part total ^ ^ bodylen crc * | \ payload type flags keylen * \ kind @@ -175,26 +183,34 @@ extern PGDLLEXPORT bool AnserWireCheckCrc(const AnserWireMsg *msg, const void *b Size body_len); /* - * QD -> QE push: raw binary, written with pqPutInt/pqPutnchar. + * QD -> QE push: raw binary, written with pqPutInt/pqPutnchar. The payload + * travels in chunks, one message each, and the coordinator sends the next one + * only when the consumer asks with a NEXT -- so a delivery is never more than + * one whole chunk deep on the connection, and the reader can walk away between + * chunks without leaving a torn message behind. * * payload_type | crc32c | condition_id | session_id | command_count | - * flags | keylen | key | bodylen | body + * flags | total_len | offset | keylen | key | chunk_len | chunk * - * The session and command ids are what lets a parked delivery be told apart - * from the current command's: condition_key restarts at the same value for - * every statement, so the consumer must match the whole identity, not the key - * alone. + * total_len is the full size of the merged payload and offset is where this + * chunk starts in it; a cancelled delivery is a single chunk with + * F_CANCELLED, total_len = 0 and offset = 0. The session and command ids are + * what lets a parked delivery be told apart from the current command's: + * condition_key restarts at the same value for every statement, so the + * consumer must match the whole identity, not the key alone. * - * with each field a 4-byte integer except the key and body. The CRC covers all - * of the other fields in that order, the integers in network byte order, so it - * validates the routing information as well as the payload; the body is - * included only when the payload type says so. Both ends compute it through - * this one function so the two cannot drift apart. It is implemented in - * ansersideband.c, beside the reader that verifies it. + * Each field is a 4-byte integer except the key and the chunk. The CRC covers + * all of the other fields in that order, the integers in network byte order, + * so it validates the routing information, the chunk's place in the payload, + * and the payload bytes themselves; the chunk is included only when the + * payload type says so. Both ends compute it through this one function so the + * two cannot drift apart. It is implemented in ansersideband.c, beside the + * reader that verifies it. */ extern PGDLLEXPORT pg_crc32c AnserWirePushCrc(char payload_type, uint32 condition_id, uint32 session_id, uint32 command_count, - uint32 flags, const char *key, int keylen, + uint32 flags, uint32 total_len, uint32 offset, + const char *key, int keylen, const void *body, int bodylen); /* @@ -251,6 +267,7 @@ extern PGDLLEXPORT bool AnserDispatchLocalConsume(const AnserChannelKey *channel * each lives with the state it owns. */ extern PGDLLEXPORT int64 AnserDispatchUnsubscribeCount(void); +extern PGDLLEXPORT int64 AnserDispatchDeliveriesCompleted(void); extern PGDLLEXPORT void AnserDispatchFlushPending(long timeout_ms); extern PGDLLEXPORT void AnserDispatchReset(void); extern PGDLLEXPORT void AnserDispatchResetCommand(int command_count); diff --git a/gpcontrib/anser/include/ansertlist.h b/gpcontrib/anser/include/ansertlist.h new file mode 100644 index 00000000000..0e7cc3352bc --- /dev/null +++ b/gpcontrib/anser/include/ansertlist.h @@ -0,0 +1,64 @@ +/*------------------------------------------------------------------------- + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * ansertlist.h + * Deparse expressions for post-planning pass-through filter scans. + * + *------------------------------------------------------------------------- + */ +#ifndef ANSERTLIST_H +#define ANSERTLIST_H + +#include "nodes/nodeFuncs.h" +#include "nodes/plannodes.h" +#include "parser/parsetree.h" + +/* INDEX_VAR in a child's output belongs to the child's deparse namespace. + * Resolve it before copying the expression into a new CustomScan namespace; + * otherwise stacked filters can make ruleutils resolve a Var to itself. + * Builders using this helper keep their custom_scan_tlist resolved, so one + * substitution also handles arbitrarily deep stacks and reordered outputs. */ +static inline Node * +anser_filter_tlist_mutator(Node *node, void *context) +{ + CustomScan *child = (CustomScan *) context; + + if (node == NULL) + return NULL; + if (IsA(node, Var) && ((Var *) node)->varno == INDEX_VAR) + { + TargetEntry *tle = get_tle_by_resno(child->custom_scan_tlist, + ((Var *) node)->varattno); + if (tle == NULL) + elog(ERROR, "missing child filter output column %d", ((Var *) node)->varattno); + return (Node *) copyObjectImpl(tle->expr); + } + return expression_tree_mutator(node, anser_filter_tlist_mutator, context); +} + +static inline List * +AnserFilterScanTlist(Plan *child) +{ + if (IsA(child, CustomScan)) + return (List *) expression_tree_mutator((Node *) child->targetlist, + anser_filter_tlist_mutator, child); + return (List *) copyObjectImpl(child->targetlist); +} + +#endif /* ANSERTLIST_H */ diff --git a/gpcontrib/anser/sql/anser_faults.sql b/gpcontrib/anser/sql/anser_faults.sql index 0cc5f4a39b1..4114b4b83d3 100644 --- a/gpcontrib/anser/sql/anser_faults.sql +++ b/gpcontrib/anser/sql/anser_faults.sql @@ -22,6 +22,10 @@ SET enable_nestloop = off; SET enable_mergejoin = off; SET optimizer = off; +-- Trace every exchange step into the server logs (never reaches the client), +-- so a CI run that goes wrong can be diagnosed from the log artifacts. +SET anser.debug = on; + -- nextval guard: the QD sends two kinds of message down a QE's dispatch -- connection -- the nextval reply and a sideband message -- and each has its -- own reader. A reader that meets the other's message has to skip it whole: @@ -113,17 +117,12 @@ DROP TABLE anser_rf_unsub_build, anser_rf_unsub_probe; -- A partially delivered filter must not wedge the next command. -- --- The push above is torn when the filter is bigger than the connection --- buffers: the consumers give up and finish, the coordinator's non-blocking --- write leaves the tail queued, and the poll loop stops flushing once the --- peer's command completes. The next dispatch on the same gang would then --- overwrite the queued bytes (PQsendGpQuery_shared), leaving the QE waiting --- forever for a body that never comes -- the session only ended with the --- cluster. ExecutorEnd now completes the flush, or poisons the connection. --- --- The filter is forced to 64 MB (anser.debug_min_filter_bytes) so the push --- cannot fit the buffers even on loopback. This test hangs rather than fails --- if the fix is reverted, the same warning as the push test above. +-- With chunked delivery there is no torn 64 MB push any more: the coordinator +-- sends one chunk and waits to be asked for the next. What can still be in +-- flight when every consumer has given up is exactly one chunk -- the QE is +-- busy on its probe side by then, so the chunk sits in the stream until the +-- command loop reads and skips it whole (postgres.c, GP_SIDEBAND_MESSAGE). +-- The next command on the same gang must answer. SET anser.timeout_ms = 100; SET anser.debug_min_filter_bytes = 67108864; CREATE TABLE anser_rf_stale_build (id int, pad text) DISTRIBUTED BY (pad); @@ -134,7 +133,7 @@ ANALYZE anser_rf_stale_build; ANALYZE anser_rf_stale_probe; -- Hold the delivery for 1 s so every consumer gives up at 100 ms and the --- 64 MB filter is pushed to connections nobody is reading. +-- first chunk goes out to connections nobody is reading. SELECT gp_inject_fault('anser_before_deliver', 'sleep', '', '', '', 1, 1, 1, dbid) FROM gp_segment_configuration WHERE role = 'p' AND content = -1; SET anser.runtime_filter = on; @@ -143,28 +142,65 @@ SELECT count(*) AS stale_rows SELECT gp_inject_fault('anser_before_deliver', 'reset', dbid) FROM gp_segment_configuration WHERE role = 'p' AND content = -1; --- The torn delivery is completed (or its connection poisoned) at ExecutorEnd, --- so the next command on the same gang must answer. +-- The leftover chunk is skipped whole, so the next command on the same gang +-- must answer. SELECT 'next command' AS marker, count(*) AS build_rows FROM anser_rf_stale_build; DROP TABLE anser_rf_stale_build, anser_rf_stale_probe; RESET anser.debug_min_filter_bytes; --- A filter parked by one statement must not be claimed by the next. +-- Walking away mid-delivery must end it cleanly. The fault pauses the +-- consumer after its first chunk for longer than its deadline -- as a skip +-- fault: the consumer itself waits out anser.timeout_ms plus a margin, on the +-- monotonic clock, because a 'sleep' fault is a bare nanosleep that any +-- signal cuts short. The consumer withdraws, the coordinator's delivery +-- stalls at the chunk boundary, and the one chunk in flight is discarded at +-- the door: the key is no longer subscribed, so read_one drops it without a +-- NEXT. Results must still be exact, the coordinator must record the +-- withdrawals, and the next command on the same gang must answer. +SET anser.timeout_ms = 500; +SET anser.debug_delivery_chunk_bytes = 8192; +SET anser.debug_min_filter_bytes = 67108864; +CREATE TABLE anser_rf_ab_build (id int, pad text) DISTRIBUTED BY (pad); +CREATE TABLE anser_rf_ab_probe (id int) DISTRIBUTED BY (id); +INSERT INTO anser_rf_ab_build SELECT g, 'b' || g FROM generate_series(1, 1000) g; +INSERT INTO anser_rf_ab_probe SELECT g % 1000 + 1 FROM generate_series(1, 200000) g; +ANALYZE anser_rf_ab_build; +ANALYZE anser_rf_ab_probe; + +-- start_ignore +CREATE EXTENSION IF NOT EXISTS anser_test; +-- end_ignore +SELECT anser_test_unsubscribes() AS unsub_before \gset +-- Pause every consumer after its first chunk for longer than its deadline. +SELECT gp_inject_fault('anser_after_chunk', 'skip', '', '', '', 1, 1, 0, dbid) + FROM gp_segment_configuration WHERE role = 'p' AND content >= 0; +SET anser.runtime_filter = on; +SELECT count(*) AS abort_rows + FROM anser_rf_ab_probe p JOIN anser_rf_ab_build b ON p.id = b.id; +SELECT gp_inject_fault('anser_after_chunk', 'reset', dbid) + FROM gp_segment_configuration WHERE role = 'p' AND content >= 0; +SELECT anser_test_unsubscribes() > :unsub_before AS consumers_withdrew; +SELECT 'next command' AS marker, count(*) AS build_rows FROM anser_rf_ab_build; +DROP EXTENSION anser_test; +DROP TABLE anser_rf_ab_build, anser_rf_ab_probe; +RESET anser.debug_min_filter_bytes; +RESET anser.debug_delivery_chunk_bytes; + +-- A filter from one statement must not be claimed by the next. -- --- A consumer that waits while a delivery for another channel arrives parks it --- in the inbox. If the statement then errors inside a savepoint, there is no --- ExecutorEnd for it (a failed portal skips it), so the parked entry used to --- survive -- and the next statement, whose condition keys restart at the same --- values, claimed it: a filter built from the old build side pruned the new --- one. Deliveries now carry the session id and command count and a consumer --- matches on them exactly, and the subtransaction abort drops the inbox. +-- A consumer that runs out of time withdraws, and with chunked delivery the +-- coordinator stops at the chunk boundary: the one chunk in flight is dropped +-- at the door, because the key is no longer subscribed. And even a filter +-- that somehow got parked whole could not be claimed by the next statement: +-- condition keys restart at the same values, but the session id and command +-- count stamped on the wire must match exactly. -- -- The LEFT JOIN pins j2 as the build side, so its consumer waits first: it --- gives up at 700 ms while the coordinator sleeps for 1 s, its late delivery --- is then read -- and parked -- by j1's consumer, and the division errors --- before anything claims it. The second run re-executes the same plan shape --- with a build side disjoint from the parked filter: claiming it would drop --- every joinable row, so count(j2.id) must be 1000, not 0. +-- gives up at 700 ms while the coordinator sleeps for 1 s, its late first +-- chunk is then read -- and dropped -- while j1's consumer waits, and the +-- division errors. The second run re-executes the same plan shape with a +-- build side disjoint from the parked filter: claiming anything stale would +-- drop every joinable row, so count(j2.id) must be 1000, not 0. SET anser.timeout_ms = 700; CREATE TABLE anser_sf_b1 (id int, pad text) DISTRIBUTED BY (pad); CREATE TABLE anser_sf_p1 (id int) DISTRIBUTED BY (id); @@ -201,6 +237,7 @@ SELECT gp_inject_fault('anser_before_deliver', 'reset', dbid) DROP TABLE anser_sf_b1, anser_sf_p1, anser_sf_b2, anser_sf_p2; RESET anser.runtime_filter; +RESET anser.debug; RESET anser.timeout_ms; RESET optimizer; RESET enable_nestloop; diff --git a/gpcontrib/anser/sql/anser_runtime_filter.sql b/gpcontrib/anser/sql/anser_runtime_filter.sql index c444f4e3dbb..88633aedb3b 100644 --- a/gpcontrib/anser/sql/anser_runtime_filter.sql +++ b/gpcontrib/anser/sql/anser_runtime_filter.sql @@ -18,6 +18,11 @@ SET enable_mergejoin = off; -- hiding in the clock. SET anser.timeout_ms = 5000; +-- Trace every exchange step into the server logs for the whole file. LOG +-- level never reaches the client, so the expected output is unaffected; the +-- traces are for the CI logs when a run goes wrong. +SET anser.debug = on; + -- build is the smaller (hashed / preserved) side, distributed so the join key -- must be redistributed; probe is the larger side distributed by the join key. CREATE TABLE anser_rf_build (id int, name text) DISTRIBUTED BY (name); @@ -286,13 +291,12 @@ SELECT b.name, p.payload FROM anser_rf_build b LEFT JOIN anser_rf_probe p ON b.i -- A plain EXPLAIN must not allocate the filter: the coordinator initializes -- every slice with EXEC_FLAG_EXPLAIN_ONLY, so the producer must skip building -- the bitset (state=NULL) while the plan still shows the planned size. The --- debug log makes the decision observable. -SET anser.debug = on; +-- debug log makes the decision observable. anser.debug is already on for the +-- whole file; client_min_messages is what brings the line to the client here. SET client_min_messages = log; EXPLAIN (COSTS OFF) SELECT b.name, p.payload FROM anser_rf_build b LEFT JOIN anser_rf_probe p ON b.id = p.id; RESET client_min_messages; -RESET anser.debug; -- And the filter must still be correct: 200 build rows, each matching one -- probe row. @@ -306,6 +310,33 @@ CREATE EXTENSION IF NOT EXISTS anser_test; SELECT injected, total_elems, planned_bytes FROM anser_test_rf_size(200); DROP EXTENSION anser_test; +-- The same exchange in chunks: an 8 kB chunk turns the 64 MB filter into +-- thousands of NEXT round trips. The result must match the unfiltered run and +-- the query must simply finish -- a whole-filter push that outgrew the +-- connection buffers is exactly what used to wedge the session. Matching +-- results alone would not prove it: a consumer that gives up produces the +-- same rows, so the coordinator's counters must say every consumer was +-- served and none withdrew. +-- start_ignore +CREATE EXTENSION IF NOT EXISTS anser_test; +-- end_ignore +SELECT anser_test_deliveries_completed() AS d_before \gset +SELECT anser_test_unsubscribes() AS u_before \gset +SET anser.debug_delivery_chunk_bytes = 8192; +SET anser.runtime_filter = on; +CREATE TEMP TABLE anser_rf_r_chunk AS + SELECT b.name, p.payload FROM anser_rf_build b LEFT JOIN anser_rf_probe p ON b.id = p.id + DISTRIBUTED BY (name); +SELECT anser_test_deliveries_completed() = :d_before + 3 AS all_delivered; +SELECT anser_test_unsubscribes() = :u_before AS nobody_gave_up; +SELECT count(*) AS only_chunk + FROM (SELECT * FROM anser_rf_r_chunk EXCEPT ALL SELECT * FROM anser_rf_r_off) d; +SELECT count(*) AS only_off_chunk + FROM (SELECT * FROM anser_rf_r_off EXCEPT ALL SELECT * FROM anser_rf_r_chunk) d; +DROP TABLE anser_rf_r_chunk; +DROP EXTENSION anser_test; +RESET anser.debug_delivery_chunk_bytes; + -- A LIMIT can finish the query while a consumer is still waiting for its -- filter: the producer is squelched before it publishes, the consumer must -- bail out via QueryFinishPending and withdraw its subscription, and the @@ -318,9 +349,20 @@ SELECT count(*) AS limited_rows FROM ( -- filter exchange completes inside the FETCH's ExecutorRun window, so a -- sibling statement's ExecutorEnd sweep has nothing of the cursor's to wipe. -- The sibling runs the same filtered join, so it really does create and sweep --- its own channels while the cursor's gang sits suspended. (A wiped exchange --- would fail open with the same rows, so this guards the behavior, not the --- timing: that path is a wait, not a wrong answer.) +-- its own channels while the cursor's gang sits suspended. The delivery +-- counter must also say every consumer was served -- because "came back +-- unfiltered" returns the same rows and would hide a broken exchange. (The +-- sweep's failure mode itself is a wait, not a wrong answer.) The cursor +-- query filters the build side to ids 1..3, and on a segment none of them +-- land on the hash is empty, the join skips the probe side, and that +-- consumer never runs -- so its count comes from the data, not from the +-- cluster width. The sibling has no such filter, so it serves all three. +-- start_ignore +CREATE EXTENSION IF NOT EXISTS anser_test; +-- end_ignore +SELECT anser_test_deliveries_completed() AS d_before \gset +SELECT count(DISTINCT gp_segment_id) AS cursor_consumers + FROM anser_rf_probe WHERE id <= 3 \gset SET optimizer = off; SET anser.runtime_filter = on; BEGIN; @@ -332,6 +374,9 @@ FETCH 1 anser_rf_cur; SELECT count(*) AS sibling_stmt FROM anser_rf_build b LEFT JOIN anser_rf_probe p ON b.id = p.id; FETCH ALL anser_rf_cur; COMMIT; +SELECT anser_test_deliveries_completed() + = :d_before + 3 + :cursor_consumers AS cursor_delivered; +DROP EXTENSION anser_test; DROP TABLE anser_rf_r_push; DROP TABLE anser_rf_r_on, anser_rf_r_off, anser_rf_r_orca; @@ -680,6 +725,44 @@ SELECT count(*) AS cast_rows JOIN anser_rf_o b ON b.o = p.o; DROP TABLE anser_rf_p, anser_rf_o; RESET gp_enable_runtime_filter; +-- A filter over another filter must resolve the child's INDEX_VAR outputs +-- in the child's namespace, not copy them into a self-referencing namespace. +CREATE TABLE anser_stack_probe (k1 int, k2 int) DISTRIBUTED RANDOMLY; +CREATE TABLE anser_stack_b1 (k int) DISTRIBUTED RANDOMLY; +CREATE TABLE anser_stack_b2 (k int) DISTRIBUTED RANDOMLY; +INSERT INTO anser_stack_probe SELECT i % 100, i % 50 FROM generate_series(1, 10000) i; +INSERT INTO anser_stack_b1 SELECT i FROM generate_series(0, 99) i; +INSERT INTO anser_stack_b2 SELECT i FROM generate_series(0, 49) i; +ANALYZE anser_stack_probe; +ANALYZE anser_stack_b1; +ANALYZE anser_stack_b2; +DO $$ +DECLARE + opt text; + plan jsonb; +BEGIN + PERFORM set_config('anser.runtime_filter', 'on', true); + FOREACH opt IN ARRAY ARRAY['off', 'on'] LOOP + PERFORM set_config('optimizer', opt, true); + EXECUTE 'EXPLAIN (VERBOSE, COSTS OFF, FORMAT JSON) + SELECT p.k1, p.k2 FROM anser_stack_probe p + JOIN anser_stack_b1 b1 ON p.k1 = b1.k + JOIN anser_stack_b2 b2 ON p.k2 = b2.k' INTO plan; + IF NOT EXISTS ( + WITH RECURSIVE walk(node) AS ( + SELECT plan->0->'Plan' + UNION ALL + SELECT child FROM walk CROSS JOIN LATERAL + jsonb_array_elements(coalesce(node->'Plans', '[]')) child + ) SELECT FROM walk + WHERE node->>'Custom Plan Provider' = 'Anser Bloom Consumer' + AND node->'Plans'->0->>'Custom Plan Provider' = 'Anser Bloom Consumer') THEN + RAISE EXCEPTION 'stacked-consumer EXPLAIN fixture changed (optimizer=%): %', opt, plan; + END IF; + END LOOP; +END +$$; +DROP TABLE anser_stack_probe, anser_stack_b1, anser_stack_b2; -- Resource and log knobs are superuser-only: an unprivileged role must not -- force 64 MB filters on every segment or fill the server log. runtime_filter -- and timeout_ms only shape the caller's own queries, so they stay @@ -696,6 +779,7 @@ DROP ROLE anser_nopriv; RESET anser.runtime_filter; RESET anser.debug_min_filter_bytes; +RESET anser.debug; RESET anser.timeout_ms; RESET optimizer; RESET enable_nestloop; diff --git a/gpcontrib/anser/sql/anser_test.sql b/gpcontrib/anser/sql/anser_test.sql index 37878e5a946..de3bca72922 100644 --- a/gpcontrib/anser/sql/anser_test.sql +++ b/gpcontrib/anser/sql/anser_test.sql @@ -115,16 +115,17 @@ FROM (VALUES -- The bytes, exactly. Session/command/condition are 42/7/3 and part is 1 of 3, -- so these are golden values: any change to the layout, the field widths or --- what the checksum covers shows up here as a diff. Note the third row -- a +-- what the checksum covers shows up here as a diff. Note the fourth row -- a -- cancelled message carries no body even though one was passed, and its -- checksum must be computed over what is actually sent. -- -- That the message comes back as text is itself the check that it is NUL-free, --- which a NOTIFY payload has to be: the fourth row's body is 00 ff 00. +-- which a NOTIFY payload has to be: the fifth row's body is 00 ff 00. SELECT t.what, length(m.msg) AS len, m.msg FROM (VALUES ('part with a 3-byte body', 'P', 'B', 0, '\x616263'::bytea), ('subscription, no body', 'S', '-', 0, '\x'::bytea), + ('next request, no body', 'N', '-', 0, '\x'::bytea), ('cancelled: body suppressed', 'P', 'B', 1, '\x616263'::bytea), ('body of NUL and 0xff bytes', 'P', 'B', 0, '\x00ff00'::bytea)) AS t(what, kind, ptype, flags, body), @@ -141,21 +142,27 @@ FROM (VALUES -- which is why the checksum covers the header and not just the payload. SELECT t.tamper, anser_test_wire_roundtrip('anser_rf_3', '\x616263'::bytea, t.tamper, - 'B'::"char") AS outcome + 'B'::"char", 'P'::"char") AS outcome FROM (VALUES (''), ('truncate'), ('append'), ('bodylen'), ('tag'), ('kind'), ('type'), ('crc'), ('key'), ('body')) AS t(tamper); +-- A NEXT request is a message of its own kind: no body, and part_index carries +-- the offset the consumer wants next. The parser must accept it and every +-- field must survive. +SELECT anser_test_wire_roundtrip('anser_rf_3', '\x'::bytea, '', '-'::"char", + 'N'::"char") AS next_roundtrip; + -- Keys and bodies that must survive the trip unchanged. "ok" means every -- field and the decoded body came back identical, so each of these is a case -- where a naive framing would have gone wrong: the key travels as raw bytes -- and is taken by length, which is what lets it contain a newline, a space, or -- the protocol's own tag without any escaping. SELECT t.what, - anser_test_wire_roundtrip(t.key, t.body, '', 'B'::"char") AS outcome + anser_test_wire_roundtrip(t.key, t.body, '', 'B'::"char", 'P'::"char") AS outcome FROM (VALUES ('plain key', 'anser_rf_3' , '\x616263'::bytea), ('key containing a newline', E'rf:a\nb' , '\x616263'::bytea), - ('key that looks like a header', 'anser3 P B 0000000000', '\x616263'::bytea), + ('key that looks like a header', 'anser4 P B 0000000000', '\x616263'::bytea), ('key with spaces and quotes', 'rf:"a b".c' , '\x616263'::bytea), ('key at the 63-byte limit', repeat('k', 63) , '\x616263'::bytea), ('empty key', '' , '\x616263'::bytea), @@ -166,26 +173,30 @@ FROM (VALUES AS t(what, key, body); -- The QD -> QE checksum. The value itself does not matter; which inputs --- change it does. Every routing field and the key are always covered, and the --- body only for a payload type that asks for it -- the other_body_exempt column --- is that deliberate exemption, not an oversight. +-- change it does. Every routing field, the chunk's coordinates and the key +-- are always covered, and the body only for a payload type that asks for it -- +-- the other_body_exempt column is that deliberate exemption, not an oversight. SELECT - anser_test_push_crc('B'::"char", 1, 7, 3, 0, 'k', '\x01') - <> anser_test_push_crc('B'::"char", 2, 7, 3, 0, 'k', '\x01') AS condition_covered, - anser_test_push_crc('B'::"char", 1, 7, 3, 0, 'k', '\x01') - <> anser_test_push_crc('B'::"char", 1, 8, 3, 0, 'k', '\x01') AS session_covered, - anser_test_push_crc('B'::"char", 1, 7, 3, 0, 'k', '\x01') - <> anser_test_push_crc('B'::"char", 1, 7, 4, 0, 'k', '\x01') AS command_covered, - anser_test_push_crc('B'::"char", 1, 7, 3, 0, 'k', '\x01') - <> anser_test_push_crc('B'::"char", 1, 7, 3, 1, 'k', '\x01') AS flags_covered, - anser_test_push_crc('B'::"char", 1, 7, 3, 0, 'k', '\x01') - <> anser_test_push_crc('B'::"char", 1, 7, 3, 0, 'j', '\x01') AS key_covered, - anser_test_push_crc('B'::"char", 1, 7, 3, 0, 'k', '\x01') - <> anser_test_push_crc('B'::"char", 1, 7, 3, 0, 'k', '\x02') AS bloom_body_covered, - anser_test_push_crc('-'::"char", 1, 7, 3, 0, 'k', '\x01') - = anser_test_push_crc('-'::"char", 1, 7, 3, 0, 'k', '\x02') AS other_body_exempt, - anser_test_push_crc('B'::"char", 1, 7, 3, 0, 'k', '\x01') - <> anser_test_push_crc('-'::"char", 1, 7, 3, 0, 'k', '\x01') AS type_covered; + anser_test_push_crc('B'::"char", 1, 7, 3, 0, 1000, 0, 'k', '\x01') + <> anser_test_push_crc('B'::"char", 2, 7, 3, 0, 1000, 0, 'k', '\x01') AS condition_covered, + anser_test_push_crc('B'::"char", 1, 7, 3, 0, 1000, 0, 'k', '\x01') + <> anser_test_push_crc('B'::"char", 1, 8, 3, 0, 1000, 0, 'k', '\x01') AS session_covered, + anser_test_push_crc('B'::"char", 1, 7, 3, 0, 1000, 0, 'k', '\x01') + <> anser_test_push_crc('B'::"char", 1, 7, 4, 0, 1000, 0, 'k', '\x01') AS command_covered, + anser_test_push_crc('B'::"char", 1, 7, 3, 0, 1000, 0, 'k', '\x01') + <> anser_test_push_crc('B'::"char", 1, 7, 3, 1, 1000, 0, 'k', '\x01') AS flags_covered, + anser_test_push_crc('B'::"char", 1, 7, 3, 0, 1000, 0, 'k', '\x01') + <> anser_test_push_crc('B'::"char", 1, 7, 3, 0, 2000, 0, 'k', '\x01') AS total_covered, + anser_test_push_crc('B'::"char", 1, 7, 3, 0, 1000, 0, 'k', '\x01') + <> anser_test_push_crc('B'::"char", 1, 7, 3, 0, 1000, 64, 'k', '\x01') AS offset_covered, + anser_test_push_crc('B'::"char", 1, 7, 3, 0, 1000, 0, 'k', '\x01') + <> anser_test_push_crc('B'::"char", 1, 7, 3, 0, 1000, 0, 'j', '\x01') AS key_covered, + anser_test_push_crc('B'::"char", 1, 7, 3, 0, 1000, 0, 'k', '\x01') + <> anser_test_push_crc('B'::"char", 1, 7, 3, 0, 1000, 0, 'k', '\x02') AS bloom_body_covered, + anser_test_push_crc('-'::"char", 1, 7, 3, 0, 1000, 0, 'k', '\x01') + = anser_test_push_crc('-'::"char", 1, 7, 3, 0, 1000, 0, 'k', '\x02') AS other_body_exempt, + anser_test_push_crc('B'::"char", 1, 7, 3, 0, 1000, 0, 'k', '\x01') + <> anser_test_push_crc('-'::"char", 1, 7, 3, 0, 1000, 0, 'k', '\x01') AS type_covered; -- 5. The runtime-filter pushdown decision. A scan key is numbered by the -- table's attributes, while the consumer is numbered by the scan's output diff --git a/gpcontrib/anser/src/anserdispatch.c b/gpcontrib/anser/src/anserdispatch.c index a7e9a4fb4a6..d6479cec871 100644 --- a/gpcontrib/anser/src/anserdispatch.c +++ b/gpcontrib/anser/src/anserdispatch.c @@ -56,7 +56,10 @@ */ #include "postgres.h" +#include + #include "libpq-fe.h" +#include "libpq-events.h" #include "libpq-int.h" #include "anser.h" @@ -70,12 +73,31 @@ #include "utils/faultinjector.h" #include "common/base64.h" #include "lib/stringinfo.h" +#include "lib/ilist.h" #include "nodes/pg_list.h" +#include "portability/instr_time.h" #include "utils/hsearch.h" #include "utils/memutils.h" -#include "utils/timestamp.h" #include "utils/wait_event.h" +/* + * One in-flight chunked delivery to one consumer. The merged payload is sent + * one chunk per GP_SIDEBAND_MESSAGE, and the next chunk goes out only when the + * consumer asks for it with a NEXT -- so no more than one chunk, always + * smaller than the connection's send buffer, is ever in flight, and a consumer + * that walks away between chunks leaves nothing torn behind. + */ +typedef struct AnserDispDelivery +{ + dlist_node active_node; + struct AnserDispChannel *channel; + PGconn *conn; + int segindex; /* for logs */ + int chunk_size; /* chosen once; see anser_disp_chunk_size() */ + uint32 next_offset; /* sent so far; a NEXT must ask for exactly this */ + bool started; /* chunk 0 has been sent */ +} AnserDispDelivery; + /* * One channel's merge state, living for the duration of the query that created * it. Keyed by AnserChannelKey, which must be the first field. @@ -90,32 +112,42 @@ typedef struct AnserDispChannel int expected_parts; /* 0 until a part tells us; from total_parts */ bool cancelled; bool complete; /* every expected part folded, or cancelled */ - List *subscribers; /* PGconn * of QEs awaiting delivery */ + List *deliveries; /* AnserDispDelivery, one per subscribed QE */ } AnserDispChannel; static HTAB *AnserDispChannels = NULL; /* - * Connections holding a partially written delivery. A merged filter is - * 1-64 MB and a consumer that has stopped reading cannot take it in one - * flush, so a push can leave bytes queued (anser_disp_push). Nothing more - * can be queued once the query ends -- deliveries only happen mid-query, and - * the channels are dropped at ExecutorEnd -- so the set is final then, and - * AnserDispatchFlushPending() completes or poisons it before the gang is - * reused. List nodes live in TopMemoryContext: the list must survive until - * ExecutorEnd regardless of the context a delivery ran in. + * The chunk must fit an empty send buffer, so one pqFlushNonBlocking() places + * it whole and outCount returns to zero. SO_SNDBUF is halved because Linux + * reports it doubled (the kernel's bookkeeping share included); the floor + * keeps a tiny or unreadable buffer from turning the delivery into thousands + * of round trips. anser.debug_delivery_chunk_bytes overrides the choice, for + * tests that want exactly that. + */ +#define ANSER_CHUNK_MAX (1024 * 1024) +#define ANSER_CHUNK_MIN (8 * 1024) + +/* + * Connections holding a partially written chunk. With chunked delivery this + * is only the insurance case: a chunk is sized to fit an empty send buffer, + * and anser_disp_send_chunk() gives the socket a bounded moment to drain + * before recording the connection here. Reaching this list therefore means + * the peer has not read for a full second -- it is gone or wedged, and + * AnserDispatchFlushPending() poisons the connection before the + * gang is reused. List nodes live in TopMemoryContext: the list must survive + * until ExecutorEnd regardless of the context a delivery ran in. * - * Invariant the safety of this list stands on: its connections are - * dereferenced ONLY in AnserDispatchFlushPending(), and that call runs before - * any gang recycling in every teardown order (standard ExecutorEnd recycles - * after it; AtAbort_Portals reaches ExecutorEnd before - * AtAbort_DispatcherState). The only other place it is touched is - * AnserDispatchReset(), which frees the list nodes without dereferencing the - * connections. Any future path that frees a QE connection before ExecutorEnd - * invalidates this comment and must be handled here instead. + * Connections are registered with libpq before Anser retains any pointer. + * PGEVT_CONNDESTROY removes every reference before PQfinish frees a PGconn, + * including when an InitPlan releases its gangs before ExecutorEnd. A reset + * drops the old stream's deliveries too. No dispatcher/core hook is needed. */ static List *AnserDispPendingPush = NIL; +/* Intrusive index: teardown must not allocate or start a dynahash scan. */ +static dlist_head AnserDispDeliveries = DLIST_STATIC_INIT(AnserDispDeliveries); + /* * Withdrawals honoured since the backend started. Only a test reads it: an * unsubscribe leaves no other trace, because the channel it touches is gone @@ -124,6 +156,14 @@ static List *AnserDispPendingPush = NIL; * arrive in time", which look identical from SQL. */ static int64 AnserDispUnsubscribes = 0; + +/* + * Deliveries that reached their last chunk. Only a test reads it: a + * completed delivery leaves no other trace, and a test that wants to know the + * filter really arrived -- rather than the consumer quietly giving up and + * running unfiltered, which produces the same rows -- reads the difference. + */ +static int64 AnserDispDeliveriesCompleted = 0; static MemoryContext AnserDispContext = NULL; static AnserDispChannel *anser_disp_lookup(const AnserChannelKey *key, bool create); @@ -132,7 +172,12 @@ static void anser_disp_apply_part(AnserDispChannel *chan, const void *payload, Size payload_len, int total_parts, bool cancelled); static void anser_disp_deliver(AnserDispChannel *chan); -static bool anser_disp_push(PGconn *conn, AnserDispChannel *chan); +static int anser_disp_chunk_size(PGconn *conn); +static AnserDispDelivery *anser_disp_delivery_find(AnserDispChannel *chan, + PGconn *conn); +static void anser_disp_delivery_drop(AnserDispChannel *chan, + AnserDispDelivery *d); +static void anser_disp_send_chunk(AnserDispChannel *chan, AnserDispDelivery *d); /* * PQstatus() / PQerrorMessage() equivalents. See the linkage note above for @@ -170,6 +215,15 @@ AnserDispatchUnsubscribeCount(void) return AnserDispUnsubscribes; } +/* + * How many deliveries this backend has completed. For tests; see the counter. + */ +int64 +AnserDispatchDeliveriesCompleted(void) +{ + return AnserDispDeliveriesCompleted; +} + /* * Per-query state lives in its own context so it can be dropped wholesale. * Channels are keyed by (session, command, condition), so entries from an @@ -203,7 +257,7 @@ AnserDispatchReset(void) /* * Drop the pending-delivery list too, but only the list nodes: the * connections it names may already be dead on the abort path, and - * dereferencing them is reserved to AnserDispatchFlushPending (see the + * dereferencing them is reserved to cleanup while their gang is live (see the * invariant at the declaration). A delivery dropped here unflushed is * the cleanupQE guard's problem, which poisons the connection at recycle. */ @@ -213,10 +267,14 @@ AnserDispatchReset(void) if (AnserDispChannels == NULL) return; + ANSER_DEBUG("anser: QD reset drops %ld channel(s)", + hash_get_num_entries(AnserDispChannels)); + hash_destroy(AnserDispChannels); AnserDispChannels = NULL; MemoryContextDelete(AnserDispContext); AnserDispContext = NULL; + dlist_init(&AnserDispDeliveries); } /* @@ -229,14 +287,15 @@ AnserDispatchReset(void) * -- for the whole call. The key carries the command count, so a query can * take its own channels with it instead of waiting for that sweep. * - * Only channels that are complete and have no subscriber left are taken, + * Only channels that are complete and have no delivery in flight are taken, * because the caller cannot always prove the count is its own: gp_command_count * names the newest query descriptor, not the one that is ending, and the two * differ whenever a portal outlives the statements that ran after it. Closing * such a cursor from inside a live query would otherwise hand us that live * query's count. A channel that is complete and already delivered costs at * most a consumer that had not subscribed yet, which fails open; an exchange - * still in flight is never touched. + * still in flight is never touched, and a channel with a delivery in flight + * keeps its payload until the sweep. * * The flip side is that a channel that never completed -- a squelched producer, * say -- still waits for the sweep. @@ -255,9 +314,12 @@ AnserDispatchResetCommand(int command_count) { if (chan->key.gp_command_count != command_count) continue; - if (!chan->complete || chan->subscribers != NIL) + if (!chan->complete || chan->deliveries != NIL) continue; + ANSER_DEBUG("anser: QD dropping finished channel cond=%u ccnt=%d", + chan->key.condition_id, chan->key.gp_command_count); + /* * AnserDispContext outlives this, so free what the entry owns rather * than the context. Deleting the entry the scan just handed back is @@ -273,72 +335,137 @@ AnserDispatchResetCommand(int command_count) } /* - * Complete or poison partially written deliveries before the query's gang is - * reused. - * - * A connection on the pending list holds a torn sideband message: part of it - * was sent, the rest is still queued, and the dispatcher's poll() stopped - * flushing when the peer's command completed. Left alone, the next dispatch - * on the gang would overwrite the queued bytes (PQsendGpQuery_shared) and the - * QE -- which has the message header already -- would wait forever for a body - * that never comes. - * - * Nothing more can be queued once the query ends, so the set is final and - * each connection gets one bounded attempt: the peer in this situation is a - * live idle reader (it is blocked mid-message or in ReadCommand), so the - * flush normally completes and the QE absorbs the stale delivery. A peer - * that stays unwritable past the deadline is gone or stuck; its connection is - * marked bad so the dispatcher discards it rather than reusing it -- the QE - * sees EOF and exits, and the next gang build reconnects (cdbgang_async.c - * does not treat a bad connection as cached). + * Poison torn deliveries before the connections can be reused. Teardown may + * run while handling another error, so it must not wait, allocate or report + * another error. timeout_ms remains in the public API for compatibility. */ void AnserDispatchFlushPending(long timeout_ms) { - ListCell *lc; + ListCell *lc; + + (void) timeout_ms; foreach(lc, AnserDispPendingPush) { - PGconn *conn = (PGconn *) lfirst(lc); - TimestampTz start = GetCurrentTimestamp(); + PGconn *conn = (PGconn *) lfirst(lc); - while (conn->outCount > 0) - { - /* - * Bail on cancel or finish: teardown must not linger. No - * CHECK_FOR_INTERRUPTS throw -- an interrupted flush degrades to - * poisoning the connection below, never to an error raised this - * late in the query's life. - */ - if (QueryCancelPending || QueryFinishPending) - break; + if (conn->outCount > 0) + conn->status = CONNECTION_BAD; + } + list_free(AnserDispPendingPush); + AnserDispPendingPush = NIL; +} - if (pqFlushNonBlocking(conn) < 0) - break; /* libpq already logged; marked bad below */ - if (conn->outCount == 0) - break; - if (TimestampDifferenceExceeds(start, GetCurrentTimestamp(), - timeout_ms)) - break; +/* list_delete_ptr can allocate in DEBUG_LIST_MEMORY_USAGE builds. Teardown + * needs the ordinary in-place deletion even in those builds. */ +static List * +anser_disp_list_forget(List *list, void *value) +{ + int i; - (void) WaitLatchOrSocket(MyLatch, - WL_LATCH_SET | WL_SOCKET_WRITEABLE | - WL_TIMEOUT | WL_EXIT_ON_PM_DEATH, - anser_conn_socket(conn), 100L /* ms */ , - PG_WAIT_EXTENSION); - ResetLatch(MyLatch); + for (i = 0; i < list_length(list); ++i) + { + if (lfirst(&list->elements[i]) != value) + continue; + if (list->length == 1) + { + list_free(list); + return NIL; } + memmove(&list->elements[i], &list->elements[i + 1], + (list->length - i - 1) * sizeof(ListCell)); + --list->length; + break; + } + return list; +} - if (conn->outCount > 0) +/* libpq invokes this while conn is still alive, including on error cleanup. + * Never allocate, log, wait, or traverse dynahash in the destruction path. */ +static void +anser_disp_forget_conn(PGconn *conn) +{ + dlist_mutable_iter iter; + + AnserDispPendingPush = anser_disp_list_forget(AnserDispPendingPush, conn); + dlist_foreach_modify(iter, &AnserDispDeliveries) + { + AnserDispDelivery *d = dlist_container(AnserDispDelivery, active_node, iter.cur); + + if (d->conn == conn) + anser_disp_delivery_drop(d->channel, d); + } +} + +static int +anser_disp_conn_event(PGEventId event, void *info, void *pass_through) +{ + switch (event) + { + case PGEVT_CONNDESTROY: + anser_disp_forget_conn(((PGEventConnDestroy *) info)->conn); + break; + case PGEVT_CONNRESET: + anser_disp_forget_conn(((PGEventConnReset *) info)->conn); + break; + default: + break; + } + return 1; +} + +/* + * Register using the same PGEvent layout and libc allocation as + * PQregisterEventProc. Cloudberry hides public PQ* symbols in postgres; + * linking a second libpq to operate on the backend's connections is unsafe. + * Anser already requires the matching libpq-int.h ABI for its message writer. + * Registration happens during normal notify processing, never cleanup. On + * allocation failure refuse the subscription, which then times out unfiltered. + */ +static bool +anser_disp_track_conn(PGconn *conn) +{ + int i; + char *name; + PGEvent *event; + + if (conn == NULL) + return false; + for (i = 0; i < conn->nEvents; ++i) + if (conn->events[i].proc == anser_disp_conn_event) + return true; + + name = strdup("Anser dispatch delivery"); + if (name == NULL) + return false; + if (conn->nEvents >= conn->eventArraySize) + { + int capacity; + PGEvent *events; + + if (conn->eventArraySize > INT_MAX / 2) { - elog(LOG, "anser: dropping a connection with %d unsent bytes of a delivered filter", - conn->outCount); - conn->status = CONNECTION_BAD; + free(name); + return false; + } + capacity = conn->eventArraySize ? conn->eventArraySize * 2 : 8; + events = realloc(conn->events, (size_t) capacity * sizeof(PGEvent)); + if (events == NULL) + { + free(name); + return false; } + conn->events = events; + conn->eventArraySize = capacity; } - - list_free(AnserDispPendingPush); - AnserDispPendingPush = NIL; + event = &conn->events[conn->nEvents++]; + event->proc = anser_disp_conn_event; + event->name = name; + event->passThrough = NULL; + event->data = NULL; + event->resultInitialized = false; + return true; } static AnserDispChannel * @@ -365,7 +492,7 @@ anser_disp_lookup(const AnserChannelKey *key, bool create) chan->expected_parts = 0; chan->cancelled = false; chan->complete = false; - chan->subscribers = NIL; + chan->deliveries = NIL; } return chan; @@ -432,6 +559,37 @@ AnserDispatchNotifyHandler(struct CdbDispatchResult *dispatchResult, anser_disp_init(); oldcxt = MemoryContextSwitchTo(AnserDispContext); + /* + * NEXT names a channel that must already exist: one that does not was + * dropped with its query, and the request is simply late. + */ + if (msg.kind == ANSER_WIRE_KIND_NEXT) + { + PGconn *conn = dr->segdbDesc->conn; + AnserDispDelivery *d; + + chan = anser_disp_lookup(&msg.key, false); + d = chan != NULL ? anser_disp_delivery_find(chan, conn) : NULL; + + /* + * A NEXT for a delivery we do not have, or for an offset we already + * passed, is a late duplicate: the delivery it belongs to is over or + * never started. Skipping it costs nothing -- the consumer's deadline + * handles the rest. + */ + if (d == NULL || (uint32) msg.part_index != d->next_offset) + { + ANSER_DEBUG("anser: QD ignoring NEXT cond=%u from seg%d (offset %d, channel %s)", + msg.key.condition_id, sender, msg.part_index, + chan == NULL ? "gone" : "mismatched"); + } + else + anser_disp_send_chunk(chan, d); + + MemoryContextSwitchTo(oldcxt); + return true; + } + chan = anser_disp_lookup(&msg.key, true); if (chan == NULL) { @@ -442,39 +600,59 @@ AnserDispatchNotifyHandler(struct CdbDispatchResult *dispatchResult, if (msg.kind == ANSER_WIRE_KIND_SUBSCRIBE) { PGconn *conn = dr->segdbDesc->conn; + AnserDispDelivery *d; + + if (!anser_disp_track_conn(conn)) + { + MemoryContextSwitchTo(oldcxt); + return true; + } + + /* + * A second subscribe from the same connection restarts its delivery + * from the beginning -- it cannot mean anything else, because a + * consumer subscribes once per wait. + */ + d = anser_disp_delivery_find(chan, conn); + if (d != NULL) + anser_disp_delivery_drop(chan, d); + + d = (AnserDispDelivery *) palloc0(sizeof(AnserDispDelivery)); + d->conn = conn; + d->segindex = sender; + d->chunk_size = anser_disp_chunk_size(conn); + d->next_offset = 0; + d->started = false; + chan->deliveries = lappend(chan->deliveries, d); + d->channel = chan; + dlist_push_head(&AnserDispDeliveries, &d->active_node); /* * A consumer can subscribe after the channel is already complete -- * producers on other segments may well have finished first -- so - * deliver immediately in that case rather than recording interest. + * start its delivery immediately in that case. */ ANSER_DEBUG("anser: QD subscribe cond=%u from seg%d (channel %s)", msg.key.condition_id, sender, chan->complete ? "complete, delivering now" : "still collecting"); if (chan->complete) - (void) anser_disp_push(conn, chan); - else - chan->subscribers = lappend(chan->subscribers, conn); + anser_disp_send_chunk(chan, d); } else if (msg.kind == ANSER_WIRE_KIND_UNSUBSCRIBE) { PGconn *conn = dr->segdbDesc->conn; + AnserDispDelivery *d = anser_disp_delivery_find(chan, conn); /* * The consumer ran out of time and is no longer reading for us. - * Delivering anyway would put 1-64 MB on a connection whose other end - * is busy elsewhere, and the message would then have to be recognised - * and skipped by whatever reads it next. - * - * This narrows the window rather than closing it: the withdrawal can - * arrive while delivery is already under way, which is why the push - * never blocks and why every reader of this connection still has to - * tolerate a message that is not its own. + * Dropping the delivery stops the flow at the chunk boundary: at most + * one whole chunk is in flight, and the reader discards it. */ - ANSER_DEBUG("anser: QD unsubscribe cond=%u from seg%d (%d subscriber(s) before)", + ANSER_DEBUG("anser: QD unsubscribe cond=%u from seg%d (%d deliver(ies) before)", msg.key.condition_id, sender, - list_length(chan->subscribers)); - chan->subscribers = list_delete_ptr(chan->subscribers, conn); + list_length(chan->deliveries)); + if (d != NULL) + anser_disp_delivery_drop(chan, d); AnserDispUnsubscribes++; } else if (msg.kind == ANSER_WIRE_KIND_PART) @@ -642,12 +820,11 @@ anser_disp_apply_part(AnserDispChannel *chan, const AnserPayloadOps *ops, } } -/* Push the finished channel to everyone waiting, then forget them. */ +/* Start every delivery that has not started yet; deliveries stay on the + * channel until their last chunk. */ static void anser_disp_deliver(AnserDispChannel *chan) { - ListCell *lc; - /* * Hold the delivery back, so a test can let every consumer reach its * deadline first and then be delivered to anyway. That ordering is what @@ -658,33 +835,121 @@ anser_disp_deliver(AnserDispChannel *chan) SIMPLE_FAULT_INJECTOR("anser_before_deliver"); ANSER_DEBUG("anser: QD delivering cond=%u to %d subscriber(s)", - chan->key.condition_id, list_length(chan->subscribers)); - foreach(lc, chan->subscribers) - (void) anser_disp_push((PGconn *) lfirst(lc), chan); + chan->key.condition_id, list_length(chan->deliveries)); + + /* + * send_chunk() can drop the delivery it is given (write failure), so this + * cannot be a foreach(): restart from the first unstarted delivery each + * time. Every call either starts or removes one, so it terminates. + */ + for (;;) + { + ListCell *lc; + AnserDispDelivery *d = NULL; + + foreach(lc, chan->deliveries) + { + AnserDispDelivery *e = (AnserDispDelivery *) lfirst(lc); + + if (!e->started) + { + d = e; + break; + } + } + if (d == NULL) + break; + anser_disp_send_chunk(chan, d); + } +} - list_free(chan->subscribers); - chan->subscribers = NIL; +/* The delivery of one connection, or NULL. */ +static AnserDispDelivery * +anser_disp_delivery_find(AnserDispChannel *chan, PGconn *conn) +{ + ListCell *lc; + + foreach(lc, chan->deliveries) + { + AnserDispDelivery *d = (AnserDispDelivery *) lfirst(lc); + + if (d->conn == conn) + return d; + } + return NULL; +} + +/* Forget one delivery: the consumer is gone or the delivery is done. */ +static void +anser_disp_delivery_drop(AnserDispChannel *chan, AnserDispDelivery *d) +{ + chan->deliveries = anser_disp_list_forget(chan->deliveries, d); + dlist_delete(&d->active_node); + pfree(d); } /* - * Write one merged payload to a QE as a GP_SIDEBAND_MESSAGE. + * The chunk size for one connection, chosen once when the delivery starts. + * The debug GUC wins so a test can force hundreds of round trips; otherwise + * halve the send buffer (see the constants' comment) and clamp into range. + */ +static int +anser_disp_chunk_size(PGconn *conn) +{ + int sndbuf = 0; + socklen_t len = sizeof(sndbuf); + int size; + + if (gp_anser_debug_delivery_chunk_bytes > 0) + return Max(gp_anser_debug_delivery_chunk_bytes, ANSER_CHUNK_MIN); + + /* + * conn can be NULL here: the dispatcher NULLs it after PQfinish on the + * error/FTS paths and treats that as a dead connection + * (cdbconn_isBadConnection), so do not rely on the notify handler only + * seeing live ones. + */ + if (conn != NULL && conn->sock != PGINVALID_SOCKET && + getsockopt(conn->sock, SOL_SOCKET, SO_SNDBUF, &sndbuf, &len) == 0 && + sndbuf > 0) + size = sndbuf / 2; + else + size = ANSER_CHUNK_MIN; + + return Min(ANSER_CHUNK_MAX, Max(ANSER_CHUNK_MIN, size)); +} + +/* + * Write the next chunk of one delivery as one GP_SIDEBAND_MESSAGE. + * + * The chunk is sized to fit an empty send buffer, so the flush below places + * it whole and nothing is left queued. The insurance for a wrong estimate is + * a bounded wait -- one second, no CHECK_FOR_INTERRUPTS and no dispatcher + * calls, because this runs inside the dispatcher's notify processing -- after + * which the connection goes on the pending list for AnserDispatchFlushPending() + * and the delivery is over: the consumer's own deadline ends its wait. * * Delivery is per consumer: a write that fails costs that one segment its - * filter (it will time out and run unfiltered) and leaves the others alone. - * This is the same "try to reach every consumer" rule the shared-memory send - * service followed. + * filter and leaves the others alone. */ -static bool -anser_disp_push(PGconn *conn, AnserDispChannel *chan) +static void +anser_disp_send_chunk(AnserDispChannel *chan, AnserDispDelivery *d) { + PGconn *conn = d->conn; int flags = chan->cancelled ? ANSER_WIRE_F_CANCELLED : 0; + uint32 total_len = chan->cancelled ? 0 : (uint32) chan->payload_len; + uint32 offset = chan->cancelled ? 0 : d->next_offset; + int chunk_len = chan->cancelled ? 0 : + (int) Min((uint32) d->chunk_size, total_len - offset); int keylen = (int) strlen(chan->key.condition_key); - int paylen = chan->cancelled ? 0 : (int) chan->payload_len; - pg_crc32c crc; + const char *chunk = chunk_len > 0 ? chan->payload + offset : NULL; char paytype; + pg_crc32c crc; + bool first = !d->started; + bool last; if (!anser_conn_ok(conn)) - return false; + goto out; /* * A channel that was cancelled before any part arrived has no type. That @@ -695,15 +960,14 @@ anser_disp_push(PGconn *conn, AnserDispChannel *chan) crc = AnserWirePushCrc(paytype, chan->key.condition_id, chan->key.gp_session_id, chan->key.gp_command_count, - (uint32) flags, chan->key.condition_key, keylen, - chan->payload, paylen); + (uint32) flags, total_len, offset, + chan->key.condition_key, keylen, + chunk, chunk_len); /* * Raw binary: pqPutnchar performs no encoding conversion, so unlike the - * QE -> QD direction this needs no base64. The session and command ids go - * out with every delivery: condition_key restarts at the same value for - * every statement, so a parked delivery is only claimable by the command - * it was pushed for. + * QE -> QD direction this needs no base64. These calls only grow the + * output buffer, so nothing here can block. */ if (pqPutMsgStart(GP_SIDEBAND_MESSAGE, conn) < 0 || pqPutInt((int) paytype, 4, conn) < 0 || @@ -712,55 +976,99 @@ anser_disp_push(PGconn *conn, AnserDispChannel *chan) pqPutInt((int) chan->key.gp_session_id, 4, conn) < 0 || pqPutInt((int) chan->key.gp_command_count, 4, conn) < 0 || pqPutInt(flags, 4, conn) < 0 || + pqPutInt((int) total_len, 4, conn) < 0 || + pqPutInt((int) offset, 4, conn) < 0 || pqPutInt(keylen, 4, conn) < 0 || pqPutnchar(chan->key.condition_key, keylen, conn) < 0 || - pqPutInt(paylen, 4, conn) < 0 || - (paylen > 0 && pqPutnchar(chan->payload, paylen, conn) < 0)) + pqPutInt(chunk_len, 4, conn) < 0 || + (chunk_len > 0 && pqPutnchar(chunk, chunk_len, conn) < 0)) { elog(LOG, "anser: could not deliver filter for condition %u: %s", chan->key.condition_id, anser_conn_error(conn)); - return false; + goto out; } - /* - * Nothing above this point can block -- those calls only grow the output - * buffer. The two that can are avoided deliberately, because this runs - * inside the dispatcher loop and a merged filter is 1-64 MB headed for a - * peer that may have stopped reading: a consumer that gave up on its - * deadline stays subscribed until it can say otherwise. - * - * pqPutMsgEnd() flushes by itself once the buffer passes 8 kB, and a - * blocking pqFlush() then waits in pqWait() with no deadline and no - * CHECK_FOR_INTERRUPTS. Either one strands the coordinator: it stops - * serving every other segment, and the query cannot even be cancelled. - * - * So queue the bytes and let the dispatcher's own poll() place them while - * the peer's command runs. When it ends with bytes still queued the - * message is torn, and the next dispatch would overwrite the buffer, so - * record the connection for AnserDispatchFlushPending() to complete or - * poison before the gang is reused. - */ pqPutMsgEndNoAutoFlush(conn); if (pqFlushNonBlocking(conn) < 0) { elog(LOG, "anser: could not deliver filter for condition %u: %s", chan->key.condition_id, anser_conn_error(conn)); - return false; + goto out; } - if (conn->outCount > 0 && - !list_member_ptr(AnserDispPendingPush, conn)) + if (conn->outCount > 0) { - MemoryContext oldcxt = MemoryContextSwitchTo(TopMemoryContext); + /* + * The chunk was meant to fit the buffer whole. It did not, so give + * the peer a bounded moment to drain: it just asked for this chunk, + * so it is alive and reading. Over the deadline the connection is + * torn and goes on the pending list. + */ + instr_time start; + instr_time now; - AnserDispPendingPush = lappend(AnserDispPendingPush, conn); - MemoryContextSwitchTo(oldcxt); + INSTR_TIME_SET_CURRENT(start); + + while (conn->outCount > 0) + { + if (pqFlushNonBlocking(conn) < 0) + break; + if (conn->outCount == 0) + break; + INSTR_TIME_SET_CURRENT(now); + INSTR_TIME_SUBTRACT(now, start); + if (INSTR_TIME_GET_MILLISEC(now) >= 1000) + break; + + (void) WaitLatchOrSocket(MyLatch, + WL_LATCH_SET | WL_SOCKET_WRITEABLE | + WL_TIMEOUT | WL_EXIT_ON_PM_DEATH, + anser_conn_socket(conn), 100L /* ms */, + PG_WAIT_EXTENSION); + ResetLatch(MyLatch); + } + + if (conn->outCount > 0 && + !list_member_ptr(AnserDispPendingPush, conn)) + { + MemoryContext savecxt = MemoryContextSwitchTo(TopMemoryContext); + + AnserDispPendingPush = lappend(AnserDispPendingPush, conn); + MemoryContextSwitchTo(savecxt); + } + if (conn->outCount > 0) + { + elog(LOG, "anser: cond=%u chunk did not flush in 1 s, %d bytes queued; ending the delivery", + chan->key.condition_id, conn->outCount); + goto out; + } } - ANSER_DEBUG("anser: QD pushed cond=%u type=%c bytes=%d cancelled=%d", - chan->key.condition_id, paytype, paylen, - chan->cancelled ? 1 : 0); - return true; + d->started = true; + d->next_offset = offset + (uint32) chunk_len; + + /* Log only the first and last chunk; a delivery is hundreds of them. */ + last = chan->cancelled || d->next_offset >= total_len; + if (first || last) + ANSER_DEBUG("anser: QD pushed cond=%u type=%c to seg%d chunk=[%u,%u) of %u cancelled=%d chunk_size=%d", + chan->key.condition_id, paytype, d->segindex, + offset, d->next_offset, total_len, + chan->cancelled ? 1 : 0, d->chunk_size); + if (last) + { + /* + * Counted only when a filter really travelled: a cancelled delivery + * tells the consumer to run unfiltered, so it must not look like an + * arrival to a test. + */ + if (!chan->cancelled) + AnserDispDeliveriesCompleted++; + goto out; + } + return; + +out: + anser_disp_delivery_drop(chan, d); } /* @@ -808,7 +1116,8 @@ AnserWireParse(const char *msg, AnserWireMsg *out) if (kind != ANSER_WIRE_KIND_PART && kind != ANSER_WIRE_KIND_SUBSCRIBE && - kind != ANSER_WIRE_KIND_UNSUBSCRIBE) + kind != ANSER_WIRE_KIND_UNSUBSCRIBE && + kind != ANSER_WIRE_KIND_NEXT) return false; if (keylen >= ANSER_CONDITION_KEY_SIZE) return false; diff --git a/gpcontrib/anser/src/anserinit.c b/gpcontrib/anser/src/anserinit.c index 0667179ce68..c738e22bb75 100644 --- a/gpcontrib/anser/src/anserinit.c +++ b/gpcontrib/anser/src/anserinit.c @@ -61,6 +61,7 @@ bool gp_anser_debug = false; int gp_anser_max_info_size = 64 * 1024 * 1024 + 1024 * 1024; int gp_anser_timeout_ms = 10000; int gp_anser_debug_min_filter_bytes = 0; +int gp_anser_debug_delivery_chunk_bytes = 0; static void anser_define_gucs(void); static PlannedStmt *anser_planner(Query *parse, const char *query_string, @@ -205,13 +206,16 @@ anser_define_gucs(void) &gp_anser_max_info_size, 64 * 1024 * 1024 + 1024 * 1024, 1, /* - * Not INT_MAX: the payload has to arrive as one - * protocol message, and pq_getmessage refuses - * anything past PQ_LARGE_MESSAGE_LIMIT -- so a - * larger setting buys a configuration that cannot - * work, and used to overflow the cap computed from - * it into a negative int. Refusing it at SET is - * the honest place to say so. + * Not INT_MAX: every payload is held whole in a + * single palloc on each end -- the coordinator's + * merged payload and the consumer's assembly + * buffer, plus the base64 copy of a part on the + * producer -- so it must stay inside + * MaxAllocSize. A larger setting buys a + * configuration that cannot work, and used to + * overflow the cap computed from it into a + * negative int. Refusing it at SET is the honest + * place to say so. */ (int) (PQ_LARGE_MESSAGE_LIMIT - ANSER_SIDEBAND_FRAME_ROOM), PGC_SUSET, @@ -240,6 +244,20 @@ anser_define_gucs(void) 0, NULL, NULL, NULL); + /* + * Read on the coordinator at delivery time only, and the choice never + * travels -- a QE reassembles whatever chunk sizes arrive -- so no + * NEED_SYNC: a QE never consults it. + */ + DefineCustomIntVariable("anser.debug_delivery_chunk_bytes", + "Sets the delivery chunk size, for testing.", + "0 (the default) sizes a chunk from the connection's send buffer; anything larger forces exactly that chunk size (the built-in floor still applies). Small values turn one delivery into hundreds of NEXT round trips.", + &gp_anser_debug_delivery_chunk_bytes, + 0, 0, INT_MAX, + PGC_SUSET, + 0, + NULL, NULL, NULL); + MarkGUCPrefixReserved("anser"); } @@ -366,12 +384,10 @@ static void anser_executor_end(QueryDesc *queryDesc) { /* - * Flush or poison partially written deliveries BEFORE anything can - * recycle the gang: standard_ExecutorEnd's teardown recycles it - * (cleanupQE destroys connections with unflushed output -- the very ones - * the pending list holds), and on the abort path AtAbort_Portals reaches - * this hook strictly before AtAbort_DispatcherState recycles - * (xact.c:3640-3641). So no connection the list names can be freed yet. + * Poison whatever is still pending. Connection destruction already + * forgets its own references through libpq events, so this only sees + * live connections -- normally the main plan's, which standard_ExecutorEnd + * releases after this returns. */ AnserDispatchResetCommand(gp_command_count); AnserDispatchFlushPending(gp_anser_timeout_ms); diff --git a/gpcontrib/anser/src/anserplan.c b/gpcontrib/anser/src/anserplan.c index 767d6141831..80c5c74d812 100644 --- a/gpcontrib/anser/src/anserplan.c +++ b/gpcontrib/anser/src/anserplan.c @@ -99,7 +99,6 @@ AnserApplyRuntimeFilters(PlannedStmt *stmt) * count out of it and allocates that much. */ AnserInjectCtx ctx = {0}; - ListCell *lc; int maxid; /* @@ -118,9 +117,7 @@ AnserApplyRuntimeFilters(PlannedStmt *stmt) * planner_hooks do not promise. max + 1 is correct under any * assignment scheme. */ - maxid = anser_max_plan_node_id(stmt->planTree); - foreach(lc, stmt->subplans) - maxid = Max(maxid, anser_max_plan_node_id((Plan *) lfirst(lc))); + maxid = AnserMaxPlanNodeId(stmt); ctx.stmt = stmt; ctx.slice_index = 0; /* the root runs in the coordinator's slice */ @@ -133,6 +130,18 @@ AnserApplyRuntimeFilters(PlannedStmt *stmt) } } +/* Include separately stored SubPlans as well as the main tree. */ +int +AnserMaxPlanNodeId(PlannedStmt *stmt) +{ + int maxid = anser_max_plan_node_id(stmt->planTree); + ListCell *lc; + + foreach(lc, stmt->subplans) + maxid = Max(maxid, anser_max_plan_node_id((Plan *) lfirst(lc))); + return maxid; +} + /* * Largest plan_node_id in a plan subtree. * diff --git a/gpcontrib/anser/src/anserplanexec.c b/gpcontrib/anser/src/anserplanexec.c index a42a5b3013c..7ac6373e434 100644 --- a/gpcontrib/anser/src/anserplanexec.c +++ b/gpcontrib/anser/src/anserplanexec.c @@ -42,6 +42,7 @@ #include "anserbloom.h" #include "anserfilter.h" #include "anserplan.h" +#include "ansertlist.h" #include "cdb/cdbutil.h" #include "cdb/cdbvars.h" #include "commands/explain.h" @@ -248,7 +249,7 @@ anser_build_rf_scan(const CustomScanMethods *methods, Plan *child, cs->custom_plans = list_make1(child); cs->custom_exprs = NIL; cs->custom_private = priv; - cs->custom_scan_tlist = copyObject(child->targetlist); + cs->custom_scan_tlist = AnserFilterScanTlist(child); cs->methods = methods; return cs; diff --git a/gpcontrib/anser/src/ansersideband.c b/gpcontrib/anser/src/ansersideband.c index 9e72cfea600..24161df52da 100644 --- a/gpcontrib/anser/src/ansersideband.c +++ b/gpcontrib/anser/src/ansersideband.c @@ -50,40 +50,84 @@ #include "nodes/pg_list.h" #include "port/pg_bswap.h" #include "port/pg_crc32c.h" +#include "portability/instr_time.h" #include "storage/latch.h" #include "tcop/dest.h" #include "tcop/tcopprot.h" +#include "utils/faultinjector.h" #include "utils/memutils.h" -#include "utils/timestamp.h" #include "utils/wait_event.h" /* How long to sleep between wakeups while waiting for a delivery. */ #define ANSER_SIDEBAND_POLL_MS 100L /* - * A delivery that arrived while we were waiting for a different channel. + * Delivery state: one live subscription, one assembly slot. * - * One slice can host consumers for more than one condition, and the - * coordinator pushes each channel as soon as it completes, so messages can - * arrive in an order we did not ask for. Rather than discard them (which - * would cost that consumer its filter), park them here and check the inbox - * before touching the socket. + * With pull-based chunking there is at most one live subscription per + * process: AnserSidebandConsumeWait() is called from one place + * (anserbloomconsume.c), its loop never runs the executor, so waits cannot + * nest, and every exit path clears the flag. And nothing arrives without a + * subscription: the coordinator sends a chunk only after a SUBSCRIBE or a + * NEXT. So the push model's inbox -- several parked deliveries, one per + * channel -- collapses to a single slot, and the cases "several entries" and + * "an entry for someone else" are impossible by construction rather than by + * argument. + * + * The full-key match is still what rejects a late chunk from an earlier + * command: AnserWaitKey carries this command's session and command ids, so a + * chunk from an older one never enters the slot. */ typedef struct AnserInboxEntry { - AnserChannelKey key; + AnserChannelKey key; /* for the trace lines; always AnserWaitKey */ char payload_type; - char *payload; /* NULL when cancelled */ - Size payload_len; + char *payload; /* the assembly buffer; NULL when cancelled */ + Size total_len; /* declared full size; 0 when cancelled */ + Size received; /* bytes assembled so far */ + instr_time last_chunk_at; /* when the latest chunk was accepted */ + bool complete; /* received == total_len, or cancelled */ bool cancelled; } AnserInboxEntry; -static List *AnserInbox = NIL; +static bool AnserWaiting = false; /* a subscription is live... */ +static AnserChannelKey AnserWaitKey; /* ...for this key */ +static AnserInboxEntry *AnserSlot = NULL; /* its assembly, or NULL */ static bool anser_sideband_send(const char *payload); static bool anser_inbox_take(const AnserChannelKey *key, char payload_type, void **payload, Size *payload_len, bool *cancelled); static bool anser_sideband_read_one(long timeout_ms); +static bool anser_key_equal(const AnserChannelKey *a, const AnserChannelKey *b); +static void anser_slot_free(void); + +static bool +anser_key_equal(const AnserChannelKey *a, const AnserChannelKey *b) +{ + return a->condition_id == b->condition_id && + a->gp_session_id == b->gp_session_id && + a->gp_command_count == b->gp_command_count && + strncmp(a->condition_key, b->condition_key, + ANSER_CONDITION_KEY_SIZE) == 0; +} + +/* Free the assembly slot, whatever state the delivery is in. */ +static void +anser_slot_free(void) +{ + if (AnserSlot != NULL) + { + ANSER_DEBUG("anser: seg%d dropping delivery cond=%u pushed for ssid=%d ccnt=%d (%zu of %zu bytes, complete=%d)", + GpIdentity.segindex, AnserSlot->key.condition_id, + AnserSlot->key.gp_session_id, AnserSlot->key.gp_command_count, + AnserSlot->received, AnserSlot->total_len, + AnserSlot->complete ? 1 : 0); + if (AnserSlot->payload != NULL) + pfree(AnserSlot->payload); + pfree(AnserSlot); + AnserSlot = NULL; + } +} /* * Publish one part. Fire-and-forget: the coordinator does not acknowledge, @@ -124,19 +168,22 @@ AnserSidebandPublish(const AnserChannelKey *channel_key, char payload_type, } /* - * Withdraw a subscription: best effort, so a send failure is only logged. - * Called on every path out of AnserSidebandConsumeWait that leaves a - * subscription behind (deadline expiry, query finishing early): from here on - * this process is somewhere else in the plan and no longer reading for a - * filter, so a delivery would put up to 64 MB on a connection whose reader - * has moved on -- and whatever reads it next has to recognise and skip it. + * Withdraw a subscription, in full: tell the coordinator, forget our own + * record of it, and drop the delivery slot whatever state it is in -- a + * completed but unclaimed payload is just as dead once its consumer walks + * away. Called on every path out of AnserSidebandConsumeWait that leaves a + * subscription behind (deadline expiry, query finishing early). * - * A narrowing rather than a guarantee: the coordinator may already be inside - * its delivery when this arrives. That is why the push never blocks and why - * every reader still tolerates a message that is not its own. + * With chunked delivery this closes the problem rather than narrowing a + * window: the coordinator stops at the chunk boundary, at most one whole + * chunk can still be in flight, and read_one drops it because the slot has no + * live subscription -- or, if we have already left the wait, the command loop + * skips the message whole (postgres.c, GP_SIDEBAND_MESSAGE). A send failure + * is only logged: the coordinator then keeps a stalled delivery until the + * channel is reset, which costs it nothing further. */ static void -anser_sideband_unsubscribe(const AnserChannelKey *channel_key) +anser_sideband_withdraw(const AnserChannelKey *channel_key) { char *msg = AnserWireFormat(channel_key, ANSER_WIRE_KIND_UNSUBSCRIBE, ANSER_PAYLOAD_NONE, 0, 0, 0, NULL, 0); @@ -145,6 +192,9 @@ anser_sideband_unsubscribe(const AnserChannelKey *channel_key) ANSER_DEBUG("anser: seg%d could not unsubscribe from cond=%u", GpIdentity.segindex, channel_key->condition_id); pfree(msg); + + AnserWaiting = false; + anser_slot_free(); } /* @@ -162,8 +212,8 @@ AnserSidebandConsumeWait(const AnserChannelKey *channel_key, char payload_type, bool *cancelled, long timeout_ms) { char *msg; - TimestampTz start; - int reads = 0; + instr_time subscribe_start; + instr_time now; if (payload != NULL) *payload = NULL; @@ -193,23 +243,10 @@ AnserSidebandConsumeWait(const AnserChannelKey *channel_key, char payload_type, return false; } - /* It may already be here: the coordinator pushes as soon as it can. */ - if (anser_inbox_take(channel_key, payload_type, payload, payload_len, cancelled)) - return payload != NULL && *payload != NULL; - /* * With no deadline to wait out there is nothing to subscribe for: we would - * give up on the next line anyway, and the only lasting effect would be the - * coordinator later pushing a message to a process that has stopped reading - * for it. Such a delivery is not free -- it arrives while this QE is - * somewhere else in the plan, and whatever reads the connection then has to - * recognise and skip it. So do not ask in the first place. - * - * This only narrows the window. Any deadline can expire with the - * subscription already registered, so even though we unsubscribe on the way - * out (anser_sideband_unsubscribe), every reader of this connection still - * has to tolerate a message that is not its own (see - * cdb_sequence_nextval_qe). + * give up on the next line anyway, and the only lasting effect would be a + * stalled delivery on the coordinator. So do not ask in the first place. */ if (timeout_ms == 0) { @@ -218,9 +255,21 @@ AnserSidebandConsumeWait(const AnserChannelKey *channel_key, char payload_type, return false; } + /* + * One subscription at a time, by construction -- see the slot's comment. + * A slot left over from an error exit (PG error unwinding passes none of + * the exits below) is always stale: its command count is not ours. + */ + Assert(!AnserWaiting); + if (AnserSlot != NULL) + anser_slot_free(); + /* * A subscription carries nothing, so its payload type is NONE -- what this * consumer expects to receive is checked on delivery, not announced here. + * The key is marked live before anything can arrive: read_one drops chunks + * unless the subscription is, which is how a delivery whose consumer has + * given up ends at its current chunk. */ msg = AnserWireFormat(channel_key, ANSER_WIRE_KIND_SUBSCRIBE, ANSER_PAYLOAD_NONE, 0, 0, 0, NULL, 0); @@ -234,10 +283,12 @@ AnserSidebandConsumeWait(const AnserChannelKey *channel_key, char payload_type, return false; } pfree(msg); + AnserWaitKey = *channel_key; + AnserWaiting = true; ANSER_DEBUG("anser: seg%d subscribed cond=%u, waiting up to %ld ms", GpIdentity.segindex, channel_key->condition_id, timeout_ms); - start = GetCurrentTimestamp(); + INSTR_TIME_SET_CURRENT(subscribe_start); for (;;) { CHECK_FOR_INTERRUPTS(); @@ -260,46 +311,77 @@ AnserSidebandConsumeWait(const AnserChannelKey *channel_key, char payload_type, * up, so nobody here will read the filter the coordinator is * about to push. */ - anser_sideband_unsubscribe(channel_key); + anser_sideband_withdraw(channel_key); return false; } if (anser_inbox_take(channel_key, payload_type, payload, payload_len, cancelled)) + { + AnserWaiting = false; return payload != NULL && *payload != NULL; + } - if (timeout_ms >= 0 && - TimestampDifferenceExceeds(start, GetCurrentTimestamp(), timeout_ms)) + /* + * The deadline guards against a delivery that stalls: it counts from + * the last accepted chunk (or from the subscribe while none has + * arrived), not from the subscribe alone -- aborting a healthy + * delivery mid-flight for being large would only waste the work + * already done. A delivery that trickles just under the deadline + * forever must still end, so a hard cap a few times the deadline + * counts from the subscribe. + */ + if (timeout_ms >= 0) { - /* - * Withdraw the subscription on the way out. From here on this - * process is somewhere else in the plan and no longer reading for - * a filter, so a delivery would put 1-64 MB on a connection whose - * reader has moved on -- and whatever reads it next has to - * recognise and skip it. - */ - anser_sideband_unsubscribe(channel_key); + double since_progress; + double since_subscribe; + instr_time *last = (AnserSlot != NULL) ? + &AnserSlot->last_chunk_at : &subscribe_start; - /* - * Report what we saw, not just that we waited: "nothing arrived" - * and "something arrived for another channel" are different bugs. - */ - ANSER_DEBUG("anser: seg%d gave up on cond=%u after %ld ms (read %d message(s), %d unclaimed)", - GpIdentity.segindex, channel_key->condition_id, - timeout_ms, reads, list_length(AnserInbox)); - return false; + INSTR_TIME_SET_CURRENT(now); + INSTR_TIME_SUBTRACT(now, *last); + since_progress = INSTR_TIME_GET_MILLISEC(now); + + INSTR_TIME_SET_CURRENT(now); + INSTR_TIME_SUBTRACT(now, subscribe_start); + since_subscribe = INSTR_TIME_GET_MILLISEC(now); + + if (since_progress >= timeout_ms || + since_subscribe >= (double) timeout_ms * 4) + { + /* + * Withdraw the subscription on the way out. From here on + * this process is somewhere else in the plan, and the + * chunked delivery stops at its current chunk once the + * withdrawal lands. + * + * Withdraw frees the slot, so read the totals first: "gave up + * having received 0 of 0" and "never started" should not look + * alike in the log. + */ + Size got = AnserSlot != NULL ? AnserSlot->received : 0; + Size total = AnserSlot != NULL ? AnserSlot->total_len : 0; + + anser_sideband_withdraw(channel_key); + + ANSER_DEBUG("anser: seg%d gave up on cond=%u after %ld ms (received %zu of %zu bytes)", + GpIdentity.segindex, channel_key->condition_id, + timeout_ms, got, total); + return false; + } } /* - * Any message we read lands in the inbox; the loop then rechecks - * whether it was the one we wanted. A read failure means the - * connection is gone, which the interconnect will report far more - * usefully than we can -- stop waiting and let the query run - * unfiltered. + * The chunk we read lands in the slot; the loop then rechecks whether + * the delivery is complete. A read failure means the connection is + * gone, which the interconnect will report far more usefully than we + * can -- stop waiting and let the query run unfiltered. */ if (!anser_sideband_read_one(ANSER_SIDEBAND_POLL_MS)) + { + anser_sideband_withdraw(channel_key); return false; - reads = list_length(AnserInbox); + } } } @@ -316,7 +398,7 @@ anser_sideband_read_one(long timeout_ms) int retval; int64 maxlen; StringInfoData buf; - AnserInboxEntry *entry; + AnserChannelKey key; MemoryContext oldcxt; char paytype; uint32 crc; @@ -324,8 +406,10 @@ anser_sideband_read_one(long timeout_ms) uint32 session_id; uint32 command_count; int flags; + uint32 total_len; + uint32 offset; int keylen; - int paylen; + int chunklen; const char *keyptr; const char *payptr; @@ -434,6 +518,8 @@ anser_sideband_read_one(long timeout_ms) session_id = (uint32) pq_getmsgint(&buf, 4); command_count = (uint32) pq_getmsgint(&buf, 4); flags = pq_getmsgint(&buf, 4); + total_len = (uint32) pq_getmsgint(&buf, 4); + offset = (uint32) pq_getmsgint(&buf, 4); keylen = pq_getmsgint(&buf, 4); if (keylen < 0 || keylen >= ANSER_CONDITION_KEY_SIZE) { @@ -442,19 +528,22 @@ anser_sideband_read_one(long timeout_ms) return false; } keyptr = pq_getmsgbytes(&buf, keylen); - paylen = pq_getmsgint(&buf, 4); - if (paylen < 0 || paylen > gp_anser_max_info_size) + chunklen = pq_getmsgint(&buf, 4); + if (chunklen < 0 || (uint32) chunklen > total_len || + total_len > (uint32) gp_anser_max_info_size || + offset > total_len || (uint32) chunklen > total_len - offset) { pfree(buf.data); - elog(LOG, "anser: filter message has a bad length"); + elog(LOG, "anser: filter message has a bad chunk frame"); return false; } - payptr = paylen > 0 ? pq_getmsgbytes(&buf, paylen) : NULL; + payptr = chunklen > 0 ? pq_getmsgbytes(&buf, chunklen) : NULL; if (crc != (uint32) AnserWirePushCrc(paytype, (uint32) condid, session_id, command_count, - (uint32) flags, keyptr, keylen, - payptr, paylen)) + (uint32) flags, total_len, offset, + keyptr, keylen, + payptr, chunklen)) { /* * Drop it and keep waiting: the deadline will expire and this consumer @@ -467,124 +556,252 @@ anser_sideband_read_one(long timeout_ms) return true; } - /* - * The inbox outlives this call and the memory context it was reached in, - * so anchor it somewhere stable; AnserSidebandResetAll drops it. - */ - oldcxt = MemoryContextSwitchTo(TopMemoryContext); - entry = palloc0(sizeof(AnserInboxEntry)); - /* * The identity comes from the wire, never from our own globals: a message * parked from an earlier command keeps that command's ids, so a later * statement can never claim it by matching its own. */ - entry->key.gp_session_id = session_id; - entry->key.gp_command_count = command_count; - entry->key.condition_id = (uint32) condid; - memcpy(entry->key.condition_key, keyptr, keylen); - entry->key.condition_key[keylen] = '\0'; - entry->payload_type = paytype; - entry->cancelled = (flags & ANSER_WIRE_F_CANCELLED) != 0; - if (!entry->cancelled && paylen > 0) + key.gp_session_id = (int) session_id; + key.gp_command_count = (int) command_count; + key.condition_id = (uint32) condid; + memcpy(key.condition_key, keyptr, keylen); + key.condition_key[keylen] = '\0'; + + /* + * A chunk for anything but the live subscription belongs to a delivery + * whose consumer has already given up. Drop it without answering: with no + * NEXT the delivery ends at its current chunk, and nothing is left in + * flight to break the stream. + */ + if (!AnserWaiting || !anser_key_equal(&AnserWaitKey, &key)) { - entry->payload = palloc(paylen); - memcpy(entry->payload, payptr, paylen); - entry->payload_len = paylen; + ANSER_DEBUG("anser: seg%d dropping chunk cond=%u off=%u len=%d: not subscribed", + GpIdentity.segindex, (uint32) condid, offset, chunklen); + pfree(buf.data); + return true; } - AnserInbox = lappend(AnserInbox, entry); - MemoryContextSwitchTo(oldcxt); - ANSER_DEBUG("anser: seg%d received cond=%u type=%c bytes=%zu cancelled=%d", - GpIdentity.segindex, (uint32) condid, paytype, - entry->payload_len, entry->cancelled ? 1 : 0); - - pfree(buf.data); - return true; -} -/* Claim a delivery for this channel, if one has arrived. */ -static bool -anser_inbox_take(const AnserChannelKey *key, char payload_type, void **payload, - Size *payload_len, bool *cancelled) -{ - ListCell *lc; + if (flags & ANSER_WIRE_F_CANCELLED) + { + /* + * A cancelled delivery is a single chunk with total_len = 0. It + * completes the slot at once; any partial assembly is replaced. + */ + if (AnserSlot == NULL) + { + oldcxt = MemoryContextSwitchTo(TopMemoryContext); + AnserSlot = palloc0(sizeof(AnserInboxEntry)); + MemoryContextSwitchTo(oldcxt); + } + else if (AnserSlot->payload != NULL) + { + pfree(AnserSlot->payload); + } + AnserSlot->key = key; + AnserSlot->payload_type = paytype; + AnserSlot->payload = NULL; + AnserSlot->cancelled = true; + AnserSlot->complete = true; + AnserSlot->total_len = 0; + AnserSlot->received = 0; + INSTR_TIME_SET_CURRENT(AnserSlot->last_chunk_at); + pfree(buf.data); + return true; + } - foreach(lc, AnserInbox) + if (AnserSlot == NULL) { - AnserInboxEntry *entry = (AnserInboxEntry *) lfirst(lc); + if (offset != 0) + { + /* The window never reopens: out of order means desync. */ + elog(LOG, "anser: chunk for condition %u starts at %u, expected 0; discarding", + (uint32) condid, offset); + pfree(buf.data); + return true; + } /* - * The condition key alone does not identify a delivery: it is built - * from plan-shape names and repeats across statements, so a filter - * parked by a late delivery of statement A would otherwise be - * claimed by statement B. The coordinator stamps every push with - * its session id and command count, and we demand an exact match - * on both integers (no hashing -- collisions are unaffordable). - * gp_session_id is unique per postmaster lifetime, so an entry from - * an older session can never match either. + * The slot outlives this call and the memory context it was reached + * in, so anchor it somewhere stable; AnserSidebandResetInbox drops it. + * The assembly buffer is sized by the declared total_len, already + * checked against anser.max_info_size above. */ - if (entry->key.condition_id != key->condition_id || - entry->key.gp_session_id != key->gp_session_id || - entry->key.gp_command_count != key->gp_command_count || - strncmp(entry->key.condition_key, key->condition_key, - ANSER_CONDITION_KEY_SIZE) != 0) - continue; - + oldcxt = MemoryContextSwitchTo(TopMemoryContext); + AnserSlot = palloc0(sizeof(AnserInboxEntry)); + AnserSlot->key = key; + AnserSlot->payload_type = paytype; + AnserSlot->total_len = total_len; + AnserSlot->payload = total_len > 0 ? palloc(total_len) : NULL; + MemoryContextSwitchTo(oldcxt); + } + else if (AnserSlot->complete || AnserSlot->received != offset || + AnserSlot->total_len != total_len || + AnserSlot->payload_type != paytype) + { /* - * Right channel, wrong kind of information. The coordinator stamps - * the type from the parts it folded, so this means a producer and a - * consumer disagree about what the channel carries -- claim the entry - * to stop waiting on it, and treat it as a cancellation so this - * consumer runs unfiltered. An empty delivery carries no type to - * check. + * A duplicate, a gap, or a frame that disagrees with the first chunk: + * the delivery is desynced, the stream is not. The total_len and + * payload_type checks are what keep the memcpy below inside the + * buffer the first chunk sized. */ - if (!entry->cancelled && entry->payload != NULL && - entry->payload_type != payload_type) - { - elog(WARNING, "anser: condition %u delivered payload type '%c', expected '%c'", - key->condition_id, entry->payload_type, payload_type); - entry->cancelled = true; - } + elog(LOG, "anser: chunk for condition %u at %u (total %u), have %zu of %zu; discarding", + (uint32) condid, offset, total_len, + AnserSlot->received, AnserSlot->total_len); + pfree(buf.data); + return true; + } - if (cancelled != NULL) - *cancelled = entry->cancelled; - if (!entry->cancelled && entry->payload != NULL) + /* + * Ask for the following chunk before copying this one, so the coordinator + * works while we copy. A NEXT names the offset we will hold after it. + */ + if (AnserSlot->received + (uint32) chunklen < total_len) + { + char *next = AnserWireFormat(&key, ANSER_WIRE_KIND_NEXT, + ANSER_PAYLOAD_NONE, + AnserSlot->received + (uint32) chunklen, + 0, 0, NULL, 0); + + if (!anser_sideband_send(next)) + ANSER_DEBUG("anser: seg%d could not ask for the next chunk of cond=%u", + GpIdentity.segindex, (uint32) condid); + pfree(next); + } + + if (chunklen > 0) + memcpy(AnserSlot->payload + offset, payptr, chunklen); + AnserSlot->received += (uint32) chunklen; + if (AnserSlot->received == total_len) + AnserSlot->complete = true; + INSTR_TIME_SET_CURRENT(AnserSlot->last_chunk_at); + + /* Log only the first and last chunk; a delivery is hundreds of them. */ + if (offset == 0 || AnserSlot->complete) + ANSER_DEBUG("anser: seg%d received cond=%u type=%c chunk=[%u,%zu) of %u cancelled=0", + GpIdentity.segindex, (uint32) condid, paytype, + offset, AnserSlot->received, total_len); + +#ifdef FAULT_INJECTOR + /* + * Let a test pause a consumer mid-delivery, past its deadline: the next + * loop iteration finds it expired and withdraws -- the walk-away path. + * + * Not a 'sleep' fault: that is a bare nanosleep() (pgsleep.c), which any + * signal cuts short, and a QE does get signals mid-query, so the pause + * ends well inside the deadline and the whole delivery completes. So the + * fault only says "pause", and this waits out the pause on the monotonic + * clock, re-arming after every early wakeup. It stays cancellable, and + * it does not touch the socket: the chunk it fires for stays queued, to + * be dropped as unsubscribed after the withdrawal. + */ + if (SIMPLE_FAULT_INJECTOR("anser_after_chunk") == FaultInjectorTypeSkip) + { + double pause_ms = (double) gp_anser_timeout_ms + 100; + instr_time pause_start; + instr_time now; + + INSTR_TIME_SET_CURRENT(pause_start); + for (;;) { - if (payload != NULL) - { - *payload = palloc(entry->payload_len); - memcpy(*payload, entry->payload, entry->payload_len); - } - if (payload_len != NULL) - *payload_len = entry->payload_len; + double left; + + CHECK_FOR_INTERRUPTS(); + + INSTR_TIME_SET_CURRENT(now); + INSTR_TIME_SUBTRACT(now, pause_start); + left = pause_ms - INSTR_TIME_GET_MILLISEC(now); + if (left <= 0) + break; + + (void) WaitLatch(MyLatch, + WL_LATCH_SET | WL_TIMEOUT | WL_EXIT_ON_PM_DEATH, + (long) left + 1, + PG_WAIT_EXTENSION); + ResetLatch(MyLatch); } + } +#endif + + pfree(buf.data); + return true; +} - AnserInbox = foreach_delete_current(AnserInbox, lc); +/* Claim the delivery if it is complete. */ +static bool +anser_inbox_take(const AnserChannelKey *key, char payload_type, void **payload, + Size *payload_len, bool *cancelled) +{ + AnserInboxEntry *entry = AnserSlot; + + if (entry == NULL || !entry->complete) + return false; + + /* + * The slot can only hold the live subscription's delivery, so a mismatch + * here is a bug, not a late message: assert in assert builds, and end the + * wait without the filter rather than log it every 100 ms until the + * deadline. + */ + if (!anser_key_equal(&entry->key, key)) + { + Assert(false); + elog(LOG, "anser: inbox slot for condition %u does not match the waiter %u", + entry->key.condition_id, key->condition_id); + if (cancelled != NULL) + *cancelled = true; + AnserSlot = NULL; if (entry->payload != NULL) pfree(entry->payload); pfree(entry); return true; } - return false; + /* + * Right channel, wrong kind of information. The coordinator stamps + * the type from the parts it folded, so this means a producer and a + * consumer disagree about what the channel carries -- claim the entry + * to stop waiting on it, and treat it as a cancellation so this + * consumer runs unfiltered. An empty delivery carries no type to + * check. + */ + if (!entry->cancelled && entry->payload != NULL && + entry->payload_type != payload_type) + { + elog(WARNING, "anser: condition %u delivered payload type '%c', expected '%c'", + key->condition_id, entry->payload_type, payload_type); + entry->cancelled = true; + } + + if (cancelled != NULL) + *cancelled = entry->cancelled; + if (!entry->cancelled && entry->payload != NULL) + { + if (payload != NULL) + { + *payload = palloc(entry->total_len); + memcpy(*payload, entry->payload, entry->total_len); + } + if (payload_len != NULL) + *payload_len = entry->total_len; + } + + AnserSlot = NULL; + ANSER_DEBUG("anser: seg%d claimed cond=%u (pushed for ssid=%d ccnt=%d, cancelled=%d)", + GpIdentity.segindex, key->condition_id, + entry->key.gp_session_id, entry->key.gp_command_count, + entry->cancelled ? 1 : 0); + if (entry->payload != NULL) + pfree(entry->payload); + pfree(entry); + return true; } -/* Drop any deliveries nobody claimed. */ +/* Drop a delivery nobody claimed, whatever state it is in. */ void AnserSidebandResetInbox(void) { - ListCell *lc; - - foreach(lc, AnserInbox) - { - AnserInboxEntry *entry = (AnserInboxEntry *) lfirst(lc); - - if (entry->payload != NULL) - pfree(entry->payload); - pfree(entry); - } - list_free(AnserInbox); - AnserInbox = NIL; + AnserWaiting = false; + anser_slot_free(); } void @@ -715,11 +932,12 @@ AnserWireFormat(const AnserChannelKey *channel_key, char kind, */ pg_crc32c AnserWirePushCrc(char payload_type, uint32 condition_id, uint32 session_id, - uint32 command_count, uint32 flags, const char *key, - int keylen, const void *body, int bodylen) + uint32 command_count, uint32 flags, uint32 total_len, + uint32 offset, const char *key, int keylen, + const void *body, int bodylen) { pg_crc32c crc; - uint32 fields[6]; + uint32 fields[8]; uint32 belen; fields[0] = pg_hton32((uint32) (unsigned char) payload_type); @@ -727,7 +945,9 @@ AnserWirePushCrc(char payload_type, uint32 condition_id, uint32 session_id, fields[2] = pg_hton32(session_id); fields[3] = pg_hton32(command_count); fields[4] = pg_hton32(flags); - fields[5] = pg_hton32((uint32) keylen); + fields[5] = pg_hton32(total_len); + fields[6] = pg_hton32(offset); + fields[7] = pg_hton32((uint32) keylen); INIT_CRC32C(crc); COMP_CRC32C(crc, fields, sizeof(fields)); diff --git a/gpcontrib/anser/test/.gitignore b/gpcontrib/anser/test/.gitignore index 847bef2ecd3..d65b9b5dd8d 100644 --- a/gpcontrib/anser/test/.gitignore +++ b/gpcontrib/anser/test/.gitignore @@ -1 +1,2 @@ /anserfold_test +/anserdetach_test diff --git a/gpcontrib/anser/test/Makefile b/gpcontrib/anser/test/Makefile index 7edeaa7a8a9..15bc250fa60 100644 --- a/gpcontrib/anser/test/Makefile +++ b/gpcontrib/anser/test/Makefile @@ -62,5 +62,22 @@ anserfilter.o: $(srcdir)/../src/anserfilter.c # Not called "check": pgxs.mk already defines that target, and overriding it # here would only earn a warning from make. .PHONY: unitcheck -unitcheck: $(PROGRAM) +unitcheck: $(PROGRAM) anserdetach_test ./$(PROGRAM) + ./anserdetach_test + +# Only teardown code from the included transport source is linked. Any +# allocation, wait or error-reporting dependency in that path is unresolved, +# so the test also checks the abort-cleanup contract at link time. +ifeq ($(PORTNAME),darwin) +DETACH_TEST_LDFLAGS = -Wl,-dead_strip +else +DETACH_TEST_LDFLAGS = -Wl,--gc-sections +endif + +anserdetach_test: $(srcdir)/anserdetach_test.c $(srcdir)/../src/anserdispatch.c $(top_srcdir)/src/interfaces/libpq/libpq-events.c + $(CC) $(CFLAGS) $(CPPFLAGS) $(PG_CPPFLAGS) -ffunction-sections -fdata-sections \ + -I$(top_srcdir)/src/interfaces/libpq -I$(top_srcdir)/src/backend \ + $(DETACH_TEST_LDFLAGS) $< $(top_srcdir)/src/interfaces/libpq/libpq-events.c -o $@ + +EXTRA_CLEAN += anserdetach_test diff --git a/gpcontrib/anser/test/anserdetach_test.c b/gpcontrib/anser/test/anserdetach_test.c new file mode 100644 index 00000000000..03c2bd66a5b --- /dev/null +++ b/gpcontrib/anser/test/anserdetach_test.c @@ -0,0 +1,175 @@ +/*------------------------------------------------------------------------- + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * anserdetach_test.c + * Connection-event cleanup of Anser deliveries and queued chunks. + * + * Runs the production callback without a cluster. PostgreSQL allocation, + * wait and error-reporting functions are deliberately unavailable: adding a + * dependency on them to cleanup makes the test fail to link. + * + * IDENTIFICATION + * gpcontrib/anser/test/anserdetach_test.c + * + *------------------------------------------------------------------------- + */ +/* Test the production teardown with allocation, waiting and error-reporting + * entry points deliberately unavailable. Unused transport code is discarded + * by the linker; any such call reachable from teardown breaks this test. */ +#include "postgres.h" + +static bool fail_name; +static bool fail_events; +static int allocation_attempts; +static char *fixture_strdup(const char *text); +static void *fixture_realloc(void *memory, size_t bytes); +#define strdup fixture_strdup +#define realloc fixture_realloc +#include "../src/anserdispatch.c" +#undef strdup +#undef realloc + +static char *fixture_strdup(const char *text) +{ + ++allocation_attempts; + return fail_name ? NULL : strdup(text); +} +static void *fixture_realloc(void *memory, size_t bytes) +{ + ++allocation_attempts; + return fail_events ? NULL : realloc(memory, bytes); +} +#include + +#define CHECK(c) do { if (!(c)) abort(); } while (0) + +void pfree(void *p) { free(p); } +void list_free(List *list) +{ + if (list == NIL) return; + free(list->elements); + free(list); +} +void ExceptionalCondition(const char *a, const char *b, int c) { abort(); } + +static List * +fixture_list(void *a, void *b) +{ + List *list = calloc(1, sizeof(List)); + CHECK(list); + list->type = T_List; + list->length = list->max_length = b == NULL ? 1 : 2; + list->elements = calloc(list->length, sizeof(ListCell)); + CHECK(list->elements); + list->elements[0].ptr_value = a; + if (b) list->elements[1].ptr_value = b; + return list; +} + +/* Mirror libpq's documented callback-before-free ordering. */ +static void +finish_fixture_conn(PGconn *conn) +{ + int i; + + for (i = 0; i < conn->nEvents; ++i) + { + PGEventConnDestroy event = {conn}; + + CHECK(conn->events[i].proc(PGEVT_CONNDESTROY, &event, + conn->events[i].passThrough)); + free(conn->events[i].name); + } + free(conn->events); + free(conn); +} + +static int other_event(PGEventId event, void *info, void *pass_through) +{ + return 1; +} + +int main(void) +{ + PGconn *own = calloc(1, sizeof(*own)); + PGconn *other = calloc(1, sizeof(*other)); + AnserDispChannel channel = {0}; + char payload[] = "completed payload"; + AnserDispDelivery *a = calloc(1, sizeof(*a)); + AnserDispDelivery *b = calloc(1, sizeof(*b)); + CHECK(own && other && a && b); + CHECK(!anser_disp_track_conn(NULL)); + fail_name = true; + CHECK(!anser_disp_track_conn(own) && own->nEvents == 0); + fail_name = false; + fail_events = true; + CHECK(!anser_disp_track_conn(own) && own->nEvents == 0); + fail_events = false; + /* Existing libpq users must keep their event entries and instance data. */ + CHECK(PQregisterEventProc(own, other_event, "other user", NULL)); + CHECK(PQsetInstanceData(own, other_event, &channel)); + CHECK(anser_disp_track_conn(own) && anser_disp_track_conn(other)); + CHECK(anser_disp_track_conn(own) && own->nEvents == 2); + CHECK(PQinstanceData(own, other_event) == &channel); + CHECK(!PQregisterEventProc(own, anser_disp_conn_event, "duplicate", NULL)); + own->outCount = other->outCount = 12; + own->status = other->status = CONNECTION_OK; + AnserDispPendingPush = fixture_list(own, other); + a->channel = b->channel = &channel; + a->conn = own; b->conn = other; + channel.deliveries = fixture_list(a, b); + channel.complete = true; + channel.payload = payload; + dlist_push_head(&AnserDispDeliveries, &a->active_node); + dlist_push_head(&AnserDispDeliveries, &b->active_node); + + /* Destroy one connection: its pointers disappear before the memory does. + * Other dispatches and the merged payload remain available. */ + { + int attempts = allocation_attempts; + finish_fixture_conn(own); + CHECK(allocation_attempts == attempts); + } + CHECK(list_length(AnserDispPendingPush) == 1); + CHECK(linitial(AnserDispPendingPush) == other); + CHECK(list_length(channel.deliveries) == 1 && linitial(channel.deliveries) == b); + CHECK(dlist_head_element(AnserDispDelivery, active_node, &AnserDispDeliveries) == b); + CHECK(other->status == CONNECTION_OK); + + /* A reset starts another stream on the same PGconn. Forget old state, + * but keep registration so later subscriptions remain protected. */ + { + PGEventConnReset reset = {other}; + CHECK(anser_disp_conn_event(PGEVT_CONNRESET, &reset, NULL)); + CHECK(anser_disp_conn_event(PGEVT_CONNRESET, &reset, NULL)); + } + CHECK(AnserDispPendingPush == NIL && channel.deliveries == NIL); + CHECK(dlist_is_empty(&AnserDispDeliveries)); + CHECK(channel.complete && channel.payload == payload); + CHECK(anser_disp_track_conn(other) && other->nEvents == 1); + + /* Query-end cleanup must not dereference the connection already freed. */ + AnserDispatchFlushPending(10000); + AnserDispPendingPush = fixture_list(other, NULL); + AnserDispatchFlushPending(10000); + CHECK(other->status == CONNECTION_BAD && AnserDispPendingPush == NIL); + finish_fixture_conn(other); + CHECK(dlist_is_empty(&AnserDispDeliveries)); + return 0; +} diff --git a/src/backend/cdb/dispatcher/cdbdisp_async.c b/src/backend/cdb/dispatcher/cdbdisp_async.c index 5b123a94c65..c1440693901 100644 --- a/src/backend/cdb/dispatcher/cdbdisp_async.c +++ b/src/backend/cdb/dispatcher/cdbdisp_async.c @@ -481,7 +481,7 @@ checkDispatchResult(CdbDispatcherState *ds, int timeout_sec) * Parallel to fds[]: whether that connection already holds a complete * message in libpq's buffer rather than in its socket. See the loop below. */ - buffered = (bool *) palloc(db_count * sizeof(bool)); + buffered = (bool *) palloc0(db_count * sizeof(bool)); /* * OK, we are finished submitting the command to the segdbs. Now, we have @@ -1203,9 +1203,11 @@ processNotifies(CdbDispatchResult *dispatchResult) * for its reply; taking the type byte and erroring would leave the * body in the stream and desynchronise the connection. */ +#ifdef FAULT_INJECTOR if (SIMPLE_FAULT_INJECTOR("nextval_sideband_before_response") == FaultInjectorTypeSkip) send_fake_sideband_message(segdbDesc->conn); +#endif /* respond back on this libpq connection with the next value */ send_sequence_response(segdbDesc->conn, seq_oid, last, cached, increment, overflow, false /* error */); From 6592d6406ec95f03b541a473ec0f1197dd6b4e01 Mon Sep 17 00:00:00 2001 From: Leonid Borchuk Date: Fri, 23 Oct 2026 18:33:14 +0300 Subject: [PATCH 18/27] Feature: RPT+ transfer-plan analysis extension Add gpcontrib/robust_predicate_transfer, Phase I of robust predicate transfer over Anser: a pure analysis pass over a finished PlannedStmt that discovers transfer regions (connected components of the join graph over base scans), builds equivalence classes of join keys by union-find, and computes the asymmetric transfer plan (chaining forward stage, leader-broadcast backward stage) ported from the RPT+ artifact (MIT, Stichting DuckDB Foundation; see licenses/LICENSE-mit-duckdb-rpt.txt). Adds a plan-order consumer-eligibility check for the MPP deadlock rule. SQL-visible via rpt_transfer_plan(text); planner hook logs the dump at DEBUG1 under rpt.debug. Nothing is injected or executed. Tests are EXPLAIN-free and property-based where estimates or tie-breaks could vary. --- .github/workflows/build-cloudberry.yml | 3 + configure | 33 + configure.ac | 7 + gpcontrib/Makefile | 5 + gpcontrib/robust_predicate_transfer/Makefile | 97 +++ gpcontrib/robust_predicate_transfer/README.md | 20 + .../robust_predicate_transfer/docs/design.md | 135 ++++ .../expected/rpt_graph.out | 118 +++ .../expected/rpt_regions.out | 104 +++ .../robust_predicate_transfer/include/rpt.h | 204 +++++ .../robust_predicate_transfer--1.0.sql | 9 + .../robust_predicate_transfer.control | 5 + .../sql/rpt_graph.sql | 80 ++ .../sql/rpt_regions.sql | 53 ++ .../src/rptgraph.cpp | 595 ++++++++++++++ .../robust_predicate_transfer/src/rptinit.cpp | 230 ++++++ .../robust_predicate_transfer/src/rptplan.cpp | 730 ++++++++++++++++++ .../src/rptregion.cpp | 568 ++++++++++++++ licenses/LICENSE-mit-duckdb-rpt.txt | 24 + 19 files changed, 3020 insertions(+) create mode 100644 gpcontrib/robust_predicate_transfer/Makefile create mode 100644 gpcontrib/robust_predicate_transfer/README.md create mode 100644 gpcontrib/robust_predicate_transfer/docs/design.md create mode 100644 gpcontrib/robust_predicate_transfer/expected/rpt_graph.out create mode 100644 gpcontrib/robust_predicate_transfer/expected/rpt_regions.out create mode 100644 gpcontrib/robust_predicate_transfer/include/rpt.h create mode 100644 gpcontrib/robust_predicate_transfer/robust_predicate_transfer--1.0.sql create mode 100644 gpcontrib/robust_predicate_transfer/robust_predicate_transfer.control create mode 100644 gpcontrib/robust_predicate_transfer/sql/rpt_graph.sql create mode 100644 gpcontrib/robust_predicate_transfer/sql/rpt_regions.sql create mode 100644 gpcontrib/robust_predicate_transfer/src/rptgraph.cpp create mode 100644 gpcontrib/robust_predicate_transfer/src/rptinit.cpp create mode 100644 gpcontrib/robust_predicate_transfer/src/rptplan.cpp create mode 100644 gpcontrib/robust_predicate_transfer/src/rptregion.cpp create mode 100644 licenses/LICENSE-mit-duckdb-rpt.txt diff --git a/.github/workflows/build-cloudberry.yml b/.github/workflows/build-cloudberry.yml index d2f1428f823..cd314a2a4d2 100644 --- a/.github/workflows/build-cloudberry.yml +++ b/.github/workflows/build-cloudberry.yml @@ -246,6 +246,9 @@ jobs: {"test":"ic-anser", "make_configs":["gpcontrib/anser:installcheck-with-install"] }, + {"test":"ic-rpt", + "make_configs":["gpcontrib/robust_predicate_transfer:installcheck-with-install"] + }, {"test":"pax-ic-good-opt-off", "make_configs":[ "contrib/pax_storage/:pax-test", diff --git a/configure b/configure index b21834c7d66..f07cd27cea0 100755 --- a/configure +++ b/configure @@ -818,6 +818,7 @@ GCOV enable_debug enable_orafce enable_faultinjector +with_rpt with_anser enable_debug_extensions enable_gpfdist @@ -891,6 +892,7 @@ enable_strong_random enable_gpfdist enable_debug_extensions with_anser +with_rpt enable_faultinjector enable_orafce enable_debug @@ -1660,6 +1662,8 @@ Optional Packages: --with-libs=DIRS alternative spelling of --with-libraries --with-pgport=PORTNUM set default port number [5432] --with-anser build the Anser runtime-filter extension + --with-rpt build the RPT+ transfer-plan extension (requires + --with-anser) --with-blocksize=BLOCKSIZE set table block size in kB [8] --with-segsize=SEGSIZE set table segment size in GB [1] @@ -3629,6 +3633,35 @@ fi +# +# Robust predicate transfer (RPT+ over Anser) +# + + + +# Check whether --with-rpt was given. +if test "${with_rpt+set}" = set; then : + withval=$with_rpt; + case $withval in + yes) + : + ;; + no) + : + ;; + *) + as_fn_error $? "no argument expected for --with-rpt option" "$LINENO" 5 + ;; + esac + +else + with_rpt=no + +fi + + + + # # Enable fault injector # diff --git a/configure.ac b/configure.ac index 9be6ba62ffc..70ed54930c5 100644 --- a/configure.ac +++ b/configure.ac @@ -241,6 +241,13 @@ PGAC_ARG_BOOL(with, anser, no, [build the Anser runtime-filter extension]) AC_SUBST(with_anser) +# +# Robust predicate transfer (RPT+ over Anser) +# +PGAC_ARG_BOOL(with, rpt, no, + [build the RPT+ transfer-plan extension (requires --with-anser)]) +AC_SUBST(with_rpt) + # # Enable fault injector # diff --git a/gpcontrib/Makefile b/gpcontrib/Makefile index c5af15e5e12..e379616fee5 100755 --- a/gpcontrib/Makefile +++ b/gpcontrib/Makefile @@ -57,6 +57,11 @@ ifeq "$(with_anser)" "yes" recurse_targets += anser endif +# RPT+ rides on Anser: --with-rpt (off by default, needs --with-anser). +ifeq "$(with_rpt)" "yes" + recurse_targets += robust_predicate_transfer +endif + $(call recurse,all install clean distclean, $(recurse_targets)) all: gpcloud orafce diff --git a/gpcontrib/robust_predicate_transfer/Makefile b/gpcontrib/robust_predicate_transfer/Makefile new file mode 100644 index 00000000000..52e1b582413 --- /dev/null +++ b/gpcontrib/robust_predicate_transfer/Makefile @@ -0,0 +1,97 @@ +#------------------------------------------------------------------------- +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# +# Makefile for the robust_predicate_transfer extension. +# +# IDENTIFICATION +# gpcontrib/robust_predicate_transfer/Makefile +# +#------------------------------------------------------------------------- + +MODULE_big = robust_predicate_transfer +OBJS = \ + $(WIN32RES) \ + src/rptgraph.o \ + src/rptinit.o \ + src/rptplan.o \ + src/rptregion.o + +PGFILEDESC = "robust_predicate_transfer - RPT+ transfer plans over Anser" + +EXTENSION = robust_predicate_transfer +DATA = robust_predicate_transfer--1.0.sql + +REGRESS = rpt_regions rpt_graph + +# Anser is a sibling module, not a linked library: it is preloaded and dfmgr.c +# loads modules RTLD_GLOBAL, so the AnserXxx entry points resolve at load time. +# That is what the PGDLLEXPORT markers on Anser's public headers are for. +PG_CPPFLAGS = -I$(srcdir)/include -I$(srcdir)/../anser/include + +# C++ module; LLVM bitcode is not generated for it (as gpcloud). +with_llvm = no + +ifdef USE_PGXS +PG_CONFIG ?= pg_config +PGXS := $(shell $(PG_CONFIG) --pgxs) +include $(PGXS) +else +subdir = gpcontrib/robust_predicate_transfer +top_builddir = ../.. +include $(top_builddir)/src/Makefile.global +include $(top_srcdir)/contrib/contrib-global.mk +endif + +# The analysis needs anser's library installed and preloaded: our module +# resolves AnserIsRuntimeFilterScan from it at load time (RTLD_NOW), and +# load order must be anser first. Mirrors gpcontrib/anser's preload +# machinery. +installcheck: preload-rpt + +.PHONY: preload-rpt +preload-rpt: + @if [ -z "$$COORDINATOR_DATA_DIRECTORY$$MASTER_DATA_DIRECTORY" ]; then \ + echo "ERROR: COORDINATOR_DATA_DIRECTORY (or MASTER_DATA_DIRECTORY) is not set;" >&2; \ + echo " source cloudberry-env.sh and gpdemo-env.sh before running installcheck." >&2; \ + exit 1; \ + fi + @current=`psql -d postgres -At -c "SHOW shared_preload_libraries;"` || { \ + echo "ERROR: cannot read shared_preload_libraries; is the cluster up?" >&2; \ + exit 1; \ + }; \ + current=`echo "$$current" | tr -d ' '`; \ + case ",$$current," in \ + *,anser,*robust_predicate_transfer,*) \ + echo "==> shared_preload_libraries already carries anser + robust_predicate_transfer" ;; \ + *) \ + without=`echo "$$current" | sed -e 's/anser//g' -e 's/robust_predicate_transfer//g' -e 's/,,*/,/g' -e 's/^,//' -e 's/,$$//'`; \ + if [ -z "$$without" ]; then new="anser,robust_predicate_transfer"; else new="$$without,anser,robust_predicate_transfer"; fi; \ + echo "==> Setting shared_preload_libraries to $$new"; \ + gpconfig -c shared_preload_libraries -v "'$$new'" --skipvalidation || exit 1; \ + gpstop -ra || exit 1 ;; \ + esac + +# CI runs a single make target per test config, so offer one target that +# installs the extension (and Anser, which we load-resolve against), arms the +# cluster, and runs the tests. +.PHONY: installcheck-with-install +installcheck-with-install: + $(MAKE) -C ../anser install + $(MAKE) install + $(MAKE) installcheck diff --git a/gpcontrib/robust_predicate_transfer/README.md b/gpcontrib/robust_predicate_transfer/README.md new file mode 100644 index 00000000000..47201b623ee --- /dev/null +++ b/gpcontrib/robust_predicate_transfer/README.md @@ -0,0 +1,20 @@ +# robust_predicate_transfer + +RPT+ (Robust Predicate Transfer with dynamic execution) on top of Anser: +plans a transfer schedule for Anser's runtime bloom filters over the query's +join graph — a forward pass that chains filters table-to-table and a backward +pass that broadcasts one merged filter per equivalence class (Qiao, Boncz, +Zhang, PVLDB 19(6), 2026). + +Phase I (current) is a pure analysis pass over the finished plan: region +discovery, join graph, equivalence classes, and the asymmetric transfer plan, +ported from the paper's artifact (MIT license — see +`licenses/LICENSE-mit-duckdb-rpt.txt`). `rpt_transfer_plan(text)` dumps the +analysis for a query; `rpt.debug=on` logs it during planning. + +See `docs/design.md` for the architecture and the correctness rules. + +Build: `./configure --with-anser --with-rpt` (needs Anser), then +`make -C gpcontrib/robust_predicate_transfer`. Run tests with +`make -C gpcontrib/robust_predicate_transfer installcheck` against a demo +cluster (it preloads `anser,robust_predicate_transfer` itself). diff --git a/gpcontrib/robust_predicate_transfer/docs/design.md b/gpcontrib/robust_predicate_transfer/docs/design.md new file mode 100644 index 00000000000..98e908bd208 --- /dev/null +++ b/gpcontrib/robust_predicate_transfer/docs/design.md @@ -0,0 +1,135 @@ +# Robust Predicate Transfer (RPT+) over Anser — design + +The `robust_predicate_transfer` extension plans *transfer schedules* for the +bloom filters Anser exchanges: instead of one filter per hash join, the query's +join graph gets a forward pass that chains filters table-to-table and a +backward pass that broadcasts one merged filter per equivalence class — the +scheme of Qiao, Boncz and Zhang, *Robust Predicate Transfer with Dynamic +Execution* (PVLDB 19(6):1278-1290, 2026), ported from the paper's artifact +(`embryo-labs/dynamic-predicate-transfer`, MIT license, Copyright 2018-2025 +Stichting DuckDB Foundation; see `licenses/LICENSE-mit-duckdb-rpt.txt`). + +**Phase I (this code) is a pure analysis pass.** It runs over a finished +`PlannedStmt`, discovers transfer regions, builds the join graph and +equivalence classes, and computes the asymmetric transfer plan. Nothing is +injected or executed; `rpt_transfer_plan(text)` dumps the result, and the +planner hook logs the same dump at `DEBUG1` when `rpt.debug` is on. + +## Architecture at a glance + +``` +dispatch 1 — reducer (Phase III, not implemented yet) + forward chain over predicated + bridge tables, root excluded + +dispatch 2 — the real query (Phase II, not implemented yet) + root scan: consume inbound chain filter from the session cache, + produce the class broadcast filter + others: consume the broadcast filter +``` + +Phase I answers the question everything downstream depends on: given this +plan, what are the regions, the classes, the chain order, the broadcast roots, +and which consumers may safely wait on the broadcast. + +## Transfer regions + +A transfer region is a connected component of the join graph over base scans: +two scans are connected when an equijoin clause between them resolves on both +sides to a column of that scan. The resolution (rptregion.cpp) descends the +operators that pass a column through unchanged — Hash, Motion, the three join +types, Sort, Material, and Anser's own filter nodes — and terminates only at +SeqScan, IndexScan, IndexOnlyScan or BitmapHeapScan, the scans a reducer pass +could replay. Everything else is a region boundary: Agg, WindowAgg, SetOp, +RecursiveUnion, Append/MergeAppend, ProjectSet (that is what `UNNEST` becomes, +so the inject-before-or-after-unnest question is moot — filters never cross +it), SubqueryScan, CteScan, Unique, Limit, Result, DynamicSeqScan. + +Because region membership falls out of the graph, a clause whose operands do +not resolve to base scans simply produces no edge, and the scans it would have +connected land in different regions. This is a correctness gate, not a missed +optimization: a filter built on a scan that does not hold the whole key column +would drop rows that can join. + +## Join graph and equivalence classes + +Edges come from HashJoin hashclauses, MergeJoin mergeclauses, and unparameterized +col=col clauses of NestLoop joinquals. A clause qualifies only when both +sides are plain Vars of the same type, the type is by-value, is not float, and +the operator is the type's own equality (rule 6 below). Clauses of one join +over one vertex pair merge into one edge; the clause count is the edge weight. + +Union-find over the keys (rtindex, attno) gives the equivalence classes; the +ports of the artifact's union-find, `ClassifyTables` (filtered / intermediate / +unfiltered) and `SkipUnfilteredTable` (bypass unfiltered tables with direct +edges, block their outgoing edges) are in rptgraph.cpp. + +## Asymmetric transfer plan + +rptplan.cpp ports the artifact's `LargestRootUpdated` + `FindEdge` + +`CreateTransferPlanUpdated`: a greedy Prim forest (one root per component, +chosen as the largest filtered-or-intermediate table, falling back to the +largest), forward stage assigned smaller→larger along the insertion order, +backward stage redirected through each class's leader — the single vertex per +class the broadcast filter is built on. `rpt.max_chain_depth` caps the +forward chain length once the reducer exists; the default (4) is a placeholder +until hop latency is measured (work item 0 of the phase plan). + +## Correctness rules + +Each of these is a silent-wrong-answer rule, not a performance rule. A bloom +filter may only ever *over*-approximate the build side's key set. + +1. **Per-hop global merge.** Segment *s* holds only part of table *j*'s keys, + so filtering table *j+1* with *s*'s local filter drops rows whose key lives + on another segment. Each hop must be a global publish → merge → deliver, + unless the two tables are provably co-distributed on the join key (same + distkey, same segment count, no redistributing motion between scan and + join). A local hop is an optimization on top, never the default. +2. **Snapshot identity.** The reducer (Phase III) must run under the main + query's snapshot and parameters: a row visible to the query but not to the + reducer would be silently missing from the filter. +3. **Outer joins are directed edges.** LEFT protects the left side, RIGHT and + MARK protect the right; ANTI and FULL produce no edge. The preserved side + of an outer join must never be reduced by the nullable side's keys. +4. **Transitive reduction is sound by induction.** Filtering table *t* with + filter A before building filter B from *t* is correct because A only + removes rows that provably cannot join. This licenses the forward chain. +5. **Cycles cost completeness, not correctness.** The spanning-forest loop + runs on cyclic queries, losing the full-reduction guarantee but never + correctness (the RPT paper's position, which we follow). +6. **Key equality must be bitwise Datum equality.** Anser hashes raw Datum + bytes, so a clause qualifies only when both sides share a by-value, + non-float type and the operator is that type's own equality + (`-0.0 = 0.0` is true but not bitwise equal; cross-type Datums for equal + values are not bitwise equal either). +7. **Plan-order safety.** A backward consumer may only wait on a producer the + plan order guarantees runs first: the consumer's scan must sit on the probe + side of a hash join whose build side transitively contains its class + leader's scan. `consumer_ok` in the dump reports exactly this; ineligible + vertices fail open (no backward filter). +8. **Fail open, always.** Timeout, malformed part, oversized filter, squelched + producer, missing cache entry — all degrade to "no filter", never to an + error and never to a filtered result. + +## Runtime contract with Anser (Phases II/III) + +- The reducer never joins — scan, filter, build — so it fits one slice, one + gang, one dispatch, with the forward chain as an ordered `Append`. +- The backward pass is ordinary Anser injection into the main plan. +- A session-scoped filter cache per QE, keyed by condition_key alone, carries + chain filters across the dispatch boundary and lets the broadcast be + delivered once per segment rather than once per consuming scan. +- Condition keys derive from the semantic identity `(region_id, class_id, + build_rel)`, not a per-query counter. + +## GUCs + +| GUC | Default | Meaning | +| --- | --- | --- | +| `rpt.enable` | off | Master switch for the analysis hook. | +| `rpt.debug` | off | Log the transfer plan of every analyzed query at DEBUG1. | +| `rpt.max_chain_depth` | 4 | Cap on the forward chain length (placeholder until hop latency is measured). | + +Setup: `shared_preload_libraries = 'anser,robust_predicate_transfer'`, in that +order (the extension resolves Anser symbols at load time). The analysis UDF +needs `CREATE EXTENSION robust_predicate_transfer`. diff --git a/gpcontrib/robust_predicate_transfer/expected/rpt_graph.out b/gpcontrib/robust_predicate_transfer/expected/rpt_graph.out new file mode 100644 index 00000000000..9559c210afc --- /dev/null +++ b/gpcontrib/robust_predicate_transfer/expected/rpt_graph.out @@ -0,0 +1,118 @@ +-- Join graph and transfer plan: equivalence classes, root choice, forward +-- chain shape, and consumer eligibility. Assertions are over structural +-- properties of the dump (LIKE/strpos), so the test does not depend on +-- estimate printouts or tie-breaking cosmetics. +CREATE EXTENSION robust_predicate_transfer; +CREATE TABLE rpt_a (a int, b int) DISTRIBUTED BY (a); +CREATE TABLE rpt_b (a int) DISTRIBUTED BY (a); +CREATE TABLE rpt_c (a int) DISTRIBUTED BY (a); +CREATE TABLE rpt_d (x int, y int) DISTRIBUTED BY (x); +INSERT INTO rpt_a SELECT i, i FROM generate_series(1, 100) i; +INSERT INTO rpt_b SELECT i FROM generate_series(1, 10) i; +INSERT INTO rpt_c SELECT i FROM generate_series(1, 50) i; +INSERT INTO rpt_d SELECT i, i FROM generate_series(1, 20) i; +ANALYZE rpt_a; +ANALYZE rpt_b; +ANALYZE rpt_c; +ANALYZE rpt_d; +SET optimizer = off; +-- Three-table chain over one column: one class with all three tables, and the +-- filtered table (rpt_a) leads the transfer order. +SELECT rpt_transfer_plan( + 'SELECT * FROM rpt_a a JOIN rpt_b b ON a.a = b.a JOIN rpt_c c ON b.a = c.a WHERE a.b > 50') + LIKE 'transfer order: 1%' AS root_is_filtered_table; + root_is_filtered_table +------------------------ + t +(1 row) + +SELECT rpt_transfer_plan( + 'SELECT * FROM rpt_a a JOIN rpt_b b ON a.a = b.a JOIN rpt_c c ON b.a = c.a WHERE a.b > 50') + LIKE '%members=1,2,3 leader=1%' AS one_class_all_members; + one_class_all_members +----------------------- + t +(1 row) + +-- Star: rpt_a joins three others on one column -- one class, root rpt_a. +SELECT rpt_transfer_plan( + 'SELECT * FROM rpt_a a JOIN rpt_b b ON a.a = b.a JOIN rpt_c c ON a.a = c.a WHERE a.b > 50') + LIKE '%members=1,2,3 leader=1%' AS star_one_class; + star_one_class +---------------- + t +(1 row) + +-- Cyclic triangle: still produces a plan (cycles cost completeness, not +-- correctness), one class, all three members. +SELECT rpt_transfer_plan( + 'SELECT * FROM rpt_a a JOIN rpt_b b ON a.a = b.a JOIN rpt_c c ON b.a = c.a AND a.a = c.a WHERE a.b > 50') + LIKE '%members=1,2,3%' AS cyclic_one_class; + cyclic_one_class +------------------ + t +(1 row) + +-- Multi-clause join: two clauses on one vertex pair merge into one edge; the +-- keys land in two classes (one per column pair). +SELECT rpt_transfer_plan( + 'SELECT * FROM rpt_a a JOIN rpt_d d ON a.a = d.x AND a.b = d.y WHERE a.b > 50') + LIKE '%members=1,2%' AS compound_members; + compound_members +------------------ + t +(1 row) + +-- LEFT JOIN: the preserved (outer) side must never be reduced by the +-- nullable side's keys, so the backward filter points at rpt_a, never away +-- from it. +SELECT strpos(rpt_transfer_plan( + 'SELECT * FROM rpt_a a LEFT JOIN rpt_b b ON a.a = b.a WHERE a.b > 50'), + 'consumer_ok 1 no') > 0 AS left_join_preserved_side_safe; + left_join_preserved_side_safe +------------------------------- + t +(1 row) + +-- FULL JOIN produces no usable edge at all: no region. +SELECT rpt_transfer_plan( + 'SELECT * FROM rpt_a a FULL JOIN rpt_b b ON a.a = b.a WHERE a.b > 50') + = 'no transfer regions +' AS full_join_no_region; + full_join_no_region +--------------------- + t +(1 row) + +-- SEMI join (IN): unprotected both ways, one class, both members. +SELECT rpt_transfer_plan( + 'SELECT * FROM rpt_a a WHERE a.b > 50 AND a.a IN (SELECT a FROM rpt_b)') + LIKE '%members=1,2%' AS semi_join_members; + semi_join_members +------------------- + t +(1 row) + +-- Cross-type and float keys are refused (bitwise Datum equality rule). +SELECT rpt_transfer_plan( + 'SELECT * FROM rpt_a a JOIN (SELECT a::bigint aa FROM rpt_b) s ON a.a = s.aa') + = 'no transfer regions +' AS cross_type_no_region; + cross_type_no_region +---------------------- + t +(1 row) + +-- The same star through ORCA: the class partition must come out identically. +SET optimizer = on; +SELECT rpt_transfer_plan( + 'SELECT * FROM rpt_a a JOIN rpt_b b ON a.a = b.a JOIN rpt_c c ON a.a = c.a WHERE a.b > 50') + LIKE '%members=1,2,3 leader=1%' AS star_one_class_orca; + star_one_class_orca +--------------------- + t +(1 row) + +RESET optimizer; +DROP TABLE rpt_a, rpt_b, rpt_c, rpt_d; +DROP EXTENSION robust_predicate_transfer; diff --git a/gpcontrib/robust_predicate_transfer/expected/rpt_regions.out b/gpcontrib/robust_predicate_transfer/expected/rpt_regions.out new file mode 100644 index 00000000000..1a50a71f754 --- /dev/null +++ b/gpcontrib/robust_predicate_transfer/expected/rpt_regions.out @@ -0,0 +1,104 @@ +-- Region discovery: transfer regions are connected components of the join +-- graph over base scans, and every operator the key resolver cannot descend +-- (aggregate, set op, window, limit, SRF/UNNEST, materialized CTE, partitioned +-- table) is a boundary -- queries whose only join key comes through one have +-- no region at all. +CREATE EXTENSION robust_predicate_transfer; +CREATE TABLE rpt_a (a int, b int) DISTRIBUTED BY (a); +CREATE TABLE rpt_b (a int) DISTRIBUTED BY (a); +CREATE TABLE rpt_c (a int) DISTRIBUTED BY (a); +INSERT INTO rpt_a SELECT i, i FROM generate_series(1, 100) i; +INSERT INTO rpt_b SELECT i FROM generate_series(1, 10) i; +INSERT INTO rpt_c SELECT i FROM generate_series(1, 50) i; +ANALYZE rpt_a; +ANALYZE rpt_b; +ANALYZE rpt_c; +-- A partitioned table joins through an Append, which resolution cannot cross. +CREATE TABLE rpt_p (a int) DISTRIBUTED BY (a) + PARTITION BY RANGE (a) (START (1) END (101) EVERY (50)); +INSERT INTO rpt_p SELECT i FROM generate_series(1, 100) i; +ANALYZE rpt_p; +-- No relations at all. +SELECT rpt_transfer_plan('SELECT 1'); + rpt_transfer_plan +------------------- + no transfer regions + +(1 row) + +-- A plain join: one region, both vertices unfiltered. +SELECT rpt_transfer_plan('SELECT * FROM rpt_b JOIN rpt_a ON rpt_b.a = rpt_a.a'); + rpt_transfer_plan +----------------------- + transfer order: 2 1 + vertex 1 rpt_b rows=10 unfiltered + vertex 2 rpt_a rows=100 unfiltered + class 1.1: members=1,2 leader=2 + vertex 1 forward: none + vertex 1 backward: none + vertex 2 forward: none + vertex 2 backward: none + consumer_ok 1 no + consumer_ok 2 no + +(1 row) + +-- Join key produced by GROUP BY: SubqueryScan boundary, no region. +SELECT rpt_transfer_plan('SELECT * FROM rpt_a a JOIN (SELECT a, count(*) c FROM rpt_b GROUP BY a) s ON a.a = s.a'); + rpt_transfer_plan +------------------- + no transfer regions + +(1 row) + +-- Join key through UNION: Append boundary, no region. +SELECT rpt_transfer_plan('SELECT * FROM rpt_a a JOIN (SELECT a FROM rpt_b UNION SELECT a FROM rpt_c) u ON a.a = u.a'); + rpt_transfer_plan +------------------- + no transfer regions + +(1 row) + +-- Join key through a window function: boundary, no region. +SELECT rpt_transfer_plan('SELECT * FROM rpt_a a JOIN (SELECT a, row_number() OVER () r FROM rpt_b) w ON a.a = w.a'); + rpt_transfer_plan +------------------- + no transfer regions + +(1 row) + +-- Join key through LIMIT: boundary, no region. +SELECT rpt_transfer_plan('SELECT * FROM rpt_a a JOIN (SELECT a FROM rpt_b ORDER BY a LIMIT 5) l ON a.a = l.a'); + rpt_transfer_plan +------------------- + no transfer regions + +(1 row) + +-- Join key produced by a set-returning function (UNNEST): ProjectSet boundary. +SELECT rpt_transfer_plan('SELECT * FROM rpt_a a JOIN (SELECT u FROM rpt_b, unnest(ARRAY[a]) u) s ON a.a = s.u'); + rpt_transfer_plan +------------------- + no transfer regions + +(1 row) + +-- Join key through a materialized CTE: CteScan boundary. +SELECT rpt_transfer_plan('WITH w AS MATERIALIZED (SELECT a FROM rpt_b) SELECT * FROM rpt_a a JOIN w ON a.a = w.a'); + rpt_transfer_plan +------------------- + no transfer regions + +(1 row) + +-- Join against a partitioned table: Append boundary. +SELECT rpt_transfer_plan('SELECT * FROM rpt_a a JOIN rpt_p p ON a.a = p.a'); + rpt_transfer_plan +------------------- + no transfer regions + +(1 row) + +DROP TABLE rpt_p; +DROP TABLE rpt_a, rpt_b, rpt_c; +DROP EXTENSION robust_predicate_transfer; diff --git a/gpcontrib/robust_predicate_transfer/include/rpt.h b/gpcontrib/robust_predicate_transfer/include/rpt.h new file mode 100644 index 00000000000..545cf477e8f --- /dev/null +++ b/gpcontrib/robust_predicate_transfer/include/rpt.h @@ -0,0 +1,204 @@ +/*------------------------------------------------------------------------- + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * rpt.h + * Shared data model for the robust_predicate_transfer extension. + * + * The structures here are a port of the RPT+ artifact's transfer graph + * (embryo-labs/dynamic-predicate-transfer, MIT license, Copyright 2018-2025 + * Stichting DuckDB Foundation; see licenses/LICENSE-mit-duckdb-rpt.txt): + * DuckDB's ColumnBinding becomes RptKey (rtindex, attno), EdgeInfo becomes + * RptEdgeInfo, JoinKeyTableGroup becomes RptKeyGroup, and dag.hpp's + * GraphNode/GraphEdge become RptGraphNode/RptGraphEdge. What does not exist + * there is ours: the extraction of the join graph from a finished + * PlannedStmt (rptregion.cpp) replaces DuckDB's TableOperatorManager, and + * the consumer-eligibility check answers an MPP question (Anser's deadlock + * rule) the single-node original never had. + * + * Phase I is a pure analysis pass: nothing is injected or executed, and the + * dumps in rptplan.cpp are the only output. Everything lives in transient + * C++ containers in the caller's memory context; nothing may rely on a + * destructor running across an elog(ERROR) longjmp. + * + * IDENTIFICATION + * gpcontrib/robust_predicate_transfer/include/rpt.h + * + *------------------------------------------------------------------------- + */ +#ifndef RPT_H +#define RPT_H + +extern "C" +{ +#include "postgres.h" + +#include "nodes/plannodes.h" +#include "nodes/primnodes.h" +#include "lib/stringinfo.h" +} + +#include +#include +#include +#include + +/* GUCs (defined in rptinit.cpp). */ +extern bool rpt_enable; +extern bool rpt_debug; +extern int rpt_max_chain_depth; + +/* + * One join key: a column of a base relation. Ports DuckDB's ColumnBinding; + * the union-find element for equivalence classes. + */ +struct RptKey +{ + int rtindex; /* rangetable index of the base relation */ + AttrNumber attno; /* column in that relation */ +}; + +inline bool +operator<(const RptKey &a, const RptKey &b) +{ + return a.rtindex < b.rtindex || + (a.rtindex == b.rtindex && a.attno < b.attno); +} + +inline bool +operator==(const RptKey &a, const RptKey &b) +{ + return a.rtindex == b.rtindex && a.attno == b.attno; +} + +/* + * A base scan taking part in at least one resolved equijoin edge; the join + * graph's vertex. Accepted scan types are the ones a reducer pass could + * replay: SeqScan (heap and AO/AOCO), IndexScan, IndexOnlyScan, + * BitmapHeapScan. Everything else never becomes a vertex, because no key + * resolution reaches it. + */ +struct RptVertexInfo +{ + int id; /* rtindex of the scan, the vertex identity */ + Plan *scan; /* the base scan node */ + std::string relname; /* RTE alias, for dumps */ + int64 cardinality; /* scan->plan_rows */ + bool filtered; /* the scan carries restriction quals */ + bool intermediate; /* touches >1 equivalence class */ + bool unfiltered; /* neither filtered nor intermediate */ + std::set join_keys; /* this table's keys over all its edges */ +}; + +/* + * One equijoin edge between two vertices. Ports EdgeInfo. All equality + * clauses of one join over the same vertex pair merge into one edge, so + * clauses.size() is the shared-attribute count the paper uses as the edge + * weight; each clause is one (left key, right key) pair. + * + * protect_left/protect_right are ported verbatim: the preserved side of an + * outer join keeps its rows whether or not they match, so it must never be + * reduced by the other side's keys. LEFT protects left, RIGHT and MARK + * protect right; ANTI and FULL never create an edge at all. + */ +struct RptEdgeInfo +{ + int left_v; + int right_v; /* vertex ids (RptVertexInfo.id) */ + Oid key_type; /* the equality operator's input type */ + std::vector > clauses; + bool protect_left; + bool protect_right; +}; + +/* + * One equivalence class of join keys. Ports JoinKeyTableGroup: members are + * the vertices touching the class, and the leader is the vertex the backward + * broadcast builds its single filter on (registered in Prim insertion + * order, so it is the class's earliest-placed vertex). + */ +struct RptKeyGroup +{ + std::set members; + int leader_id; /* -1 until registered */ + RptKey leader_key; + Oid key_type; +}; + +/* Ports dag.hpp's GraphEdge: one transfer edge of the built plan. */ +struct RptGraphEdge +{ + int destination; + std::vector left_keys; /* keys on the smaller table's side */ + std::vector right_keys; /* keys on the larger table's side */ + std::vector key_types; +}; + +/* Ports dag.hpp's GraphNode: one vertex's full transfer plan. */ +struct RptGraphNode +{ + int id; + int32_t cardinality_order; /* Prim insertion order (0 = root) */ + + struct + { + std::vector in; + std::vector out; + } forward_stage, + backward_stage; +}; + +/* + * The whole analysis. Ports TransferGraphManager; the neighbor matrix is + * symmetric (both [a][b] and [b][a] name the same edge) as in the original. + */ +struct RptTransferMgr +{ + /* rptregion.cpp fills these from the PlannedStmt. */ + std::vector vertices; + std::map by_rti; /* vertex id -> index in vertices */ + /* vertex id -> (vertex id -> edge) */ + std::map > neighbor_matrix; + + /* rptgraph.cpp: classes and classification. */ + std::map groups; /* by class representative key */ + std::map uf_parent; /* union-find over keys */ + + /* rptplan.cpp: the transfer plan. */ + std::map graph; + std::vector transfer_order; /* Prim insertion order of vertex ids */ + + /* Our addition: backward-consumer eligibility (Anser's deadlock rule) -- + * consumer_ok[v] is true only where v's scan sits on the probe side of a + * hash join whose build side transitively contains the vertex's class + * leader's scan. */ + std::map consumer_ok; +}; + +/* rptregion.cpp: extract the join graph from the finished statement. */ +extern void RptCollectGraph(PlannedStmt *stmt, RptTransferMgr *mgr); + +/* rptgraph.cpp: union-find classes, classification, SkipUnfilteredTable. */ +extern void RptBuildClasses(RptTransferMgr *mgr); + +/* rptplan.cpp: the Prim forest (LargestRootUpdated), forward/backward stage + * assignment (CreateTransferPlanUpdated), consumer eligibility, and dumps. */ +extern void RptBuildTransferPlan(PlannedStmt *stmt, RptTransferMgr *mgr); +extern void RptDumpTransferPlan(const RptTransferMgr *mgr, StringInfo out); + +#endif /* RPT_H */ diff --git a/gpcontrib/robust_predicate_transfer/robust_predicate_transfer--1.0.sql b/gpcontrib/robust_predicate_transfer/robust_predicate_transfer--1.0.sql new file mode 100644 index 00000000000..f6e5ac72f8a --- /dev/null +++ b/gpcontrib/robust_predicate_transfer/robust_predicate_transfer--1.0.sql @@ -0,0 +1,9 @@ +/* gpcontrib/robust_predicate_transfer/robust_predicate_transfer--1.0.sql */ + +-- Plan a query and dump its RPT+ transfer plan (regions, join graph, +-- equivalence classes, forward chain, backward broadcast, consumer +-- eligibility). Analysis only: nothing is injected or executed. +CREATE FUNCTION rpt_transfer_plan(query text) +RETURNS text +AS 'MODULE_PATHNAME' +LANGUAGE C STRICT; diff --git a/gpcontrib/robust_predicate_transfer/robust_predicate_transfer.control b/gpcontrib/robust_predicate_transfer/robust_predicate_transfer.control new file mode 100644 index 00000000000..0b8f92d5982 --- /dev/null +++ b/gpcontrib/robust_predicate_transfer/robust_predicate_transfer.control @@ -0,0 +1,5 @@ +# robust_predicate_transfer extension +comment = 'RPT+ transfer-plan analysis over Anser runtime filters' +default_version = '1.0' +module_pathname = '$libdir/robust_predicate_transfer' +relocatable = true diff --git a/gpcontrib/robust_predicate_transfer/sql/rpt_graph.sql b/gpcontrib/robust_predicate_transfer/sql/rpt_graph.sql new file mode 100644 index 00000000000..bc738249233 --- /dev/null +++ b/gpcontrib/robust_predicate_transfer/sql/rpt_graph.sql @@ -0,0 +1,80 @@ +-- Join graph and transfer plan: equivalence classes, root choice, forward +-- chain shape, and consumer eligibility. Assertions are over structural +-- properties of the dump (LIKE/strpos), so the test does not depend on +-- estimate printouts or tie-breaking cosmetics. +CREATE EXTENSION robust_predicate_transfer; + +CREATE TABLE rpt_a (a int, b int) DISTRIBUTED BY (a); +CREATE TABLE rpt_b (a int) DISTRIBUTED BY (a); +CREATE TABLE rpt_c (a int) DISTRIBUTED BY (a); +CREATE TABLE rpt_d (x int, y int) DISTRIBUTED BY (x); +INSERT INTO rpt_a SELECT i, i FROM generate_series(1, 100) i; +INSERT INTO rpt_b SELECT i FROM generate_series(1, 10) i; +INSERT INTO rpt_c SELECT i FROM generate_series(1, 50) i; +INSERT INTO rpt_d SELECT i, i FROM generate_series(1, 20) i; +ANALYZE rpt_a; +ANALYZE rpt_b; +ANALYZE rpt_c; +ANALYZE rpt_d; + +SET optimizer = off; + +-- Three-table chain over one column: one class with all three tables, and the +-- filtered table (rpt_a) leads the transfer order. +SELECT rpt_transfer_plan( + 'SELECT * FROM rpt_a a JOIN rpt_b b ON a.a = b.a JOIN rpt_c c ON b.a = c.a WHERE a.b > 50') + LIKE 'transfer order: 1%' AS root_is_filtered_table; +SELECT rpt_transfer_plan( + 'SELECT * FROM rpt_a a JOIN rpt_b b ON a.a = b.a JOIN rpt_c c ON b.a = c.a WHERE a.b > 50') + LIKE '%members=1,2,3 leader=1%' AS one_class_all_members; + +-- Star: rpt_a joins three others on one column -- one class, root rpt_a. +SELECT rpt_transfer_plan( + 'SELECT * FROM rpt_a a JOIN rpt_b b ON a.a = b.a JOIN rpt_c c ON a.a = c.a WHERE a.b > 50') + LIKE '%members=1,2,3 leader=1%' AS star_one_class; + +-- Cyclic triangle: still produces a plan (cycles cost completeness, not +-- correctness), one class, all three members. +SELECT rpt_transfer_plan( + 'SELECT * FROM rpt_a a JOIN rpt_b b ON a.a = b.a JOIN rpt_c c ON b.a = c.a AND a.a = c.a WHERE a.b > 50') + LIKE '%members=1,2,3%' AS cyclic_one_class; + +-- Multi-clause join: two clauses on one vertex pair merge into one edge; the +-- keys land in two classes (one per column pair). +SELECT rpt_transfer_plan( + 'SELECT * FROM rpt_a a JOIN rpt_d d ON a.a = d.x AND a.b = d.y WHERE a.b > 50') + LIKE '%members=1,2%' AS compound_members; + +-- LEFT JOIN: the preserved (outer) side must never be reduced by the +-- nullable side's keys, so the backward filter points at rpt_a, never away +-- from it. +SELECT strpos(rpt_transfer_plan( + 'SELECT * FROM rpt_a a LEFT JOIN rpt_b b ON a.a = b.a WHERE a.b > 50'), + 'consumer_ok 1 no') > 0 AS left_join_preserved_side_safe; + +-- FULL JOIN produces no usable edge at all: no region. +SELECT rpt_transfer_plan( + 'SELECT * FROM rpt_a a FULL JOIN rpt_b b ON a.a = b.a WHERE a.b > 50') + = 'no transfer regions +' AS full_join_no_region; + +-- SEMI join (IN): unprotected both ways, one class, both members. +SELECT rpt_transfer_plan( + 'SELECT * FROM rpt_a a WHERE a.b > 50 AND a.a IN (SELECT a FROM rpt_b)') + LIKE '%members=1,2%' AS semi_join_members; + +-- Cross-type and float keys are refused (bitwise Datum equality rule). +SELECT rpt_transfer_plan( + 'SELECT * FROM rpt_a a JOIN (SELECT a::bigint aa FROM rpt_b) s ON a.a = s.aa') + = 'no transfer regions +' AS cross_type_no_region; + +-- The same star through ORCA: the class partition must come out identically. +SET optimizer = on; +SELECT rpt_transfer_plan( + 'SELECT * FROM rpt_a a JOIN rpt_b b ON a.a = b.a JOIN rpt_c c ON a.a = c.a WHERE a.b > 50') + LIKE '%members=1,2,3 leader=1%' AS star_one_class_orca; +RESET optimizer; + +DROP TABLE rpt_a, rpt_b, rpt_c, rpt_d; +DROP EXTENSION robust_predicate_transfer; diff --git a/gpcontrib/robust_predicate_transfer/sql/rpt_regions.sql b/gpcontrib/robust_predicate_transfer/sql/rpt_regions.sql new file mode 100644 index 00000000000..72f1847c4fe --- /dev/null +++ b/gpcontrib/robust_predicate_transfer/sql/rpt_regions.sql @@ -0,0 +1,53 @@ +-- Region discovery: transfer regions are connected components of the join +-- graph over base scans, and every operator the key resolver cannot descend +-- (aggregate, set op, window, limit, SRF/UNNEST, materialized CTE, partitioned +-- table) is a boundary -- queries whose only join key comes through one have +-- no region at all. +CREATE EXTENSION robust_predicate_transfer; + +CREATE TABLE rpt_a (a int, b int) DISTRIBUTED BY (a); +CREATE TABLE rpt_b (a int) DISTRIBUTED BY (a); +CREATE TABLE rpt_c (a int) DISTRIBUTED BY (a); +INSERT INTO rpt_a SELECT i, i FROM generate_series(1, 100) i; +INSERT INTO rpt_b SELECT i FROM generate_series(1, 10) i; +INSERT INTO rpt_c SELECT i FROM generate_series(1, 50) i; +ANALYZE rpt_a; +ANALYZE rpt_b; +ANALYZE rpt_c; + +-- A partitioned table joins through an Append, which resolution cannot cross. +CREATE TABLE rpt_p (a int) DISTRIBUTED BY (a) + PARTITION BY RANGE (a) (START (1) END (101) EVERY (50)); +INSERT INTO rpt_p SELECT i FROM generate_series(1, 100) i; +ANALYZE rpt_p; + +-- No relations at all. +SELECT rpt_transfer_plan('SELECT 1'); + +-- A plain join: one region, both vertices unfiltered. +SELECT rpt_transfer_plan('SELECT * FROM rpt_b JOIN rpt_a ON rpt_b.a = rpt_a.a'); + +-- Join key produced by GROUP BY: SubqueryScan boundary, no region. +SELECT rpt_transfer_plan('SELECT * FROM rpt_a a JOIN (SELECT a, count(*) c FROM rpt_b GROUP BY a) s ON a.a = s.a'); + +-- Join key through UNION: Append boundary, no region. +SELECT rpt_transfer_plan('SELECT * FROM rpt_a a JOIN (SELECT a FROM rpt_b UNION SELECT a FROM rpt_c) u ON a.a = u.a'); + +-- Join key through a window function: boundary, no region. +SELECT rpt_transfer_plan('SELECT * FROM rpt_a a JOIN (SELECT a, row_number() OVER () r FROM rpt_b) w ON a.a = w.a'); + +-- Join key through LIMIT: boundary, no region. +SELECT rpt_transfer_plan('SELECT * FROM rpt_a a JOIN (SELECT a FROM rpt_b ORDER BY a LIMIT 5) l ON a.a = l.a'); + +-- Join key produced by a set-returning function (UNNEST): ProjectSet boundary. +SELECT rpt_transfer_plan('SELECT * FROM rpt_a a JOIN (SELECT u FROM rpt_b, unnest(ARRAY[a]) u) s ON a.a = s.u'); + +-- Join key through a materialized CTE: CteScan boundary. +SELECT rpt_transfer_plan('WITH w AS MATERIALIZED (SELECT a FROM rpt_b) SELECT * FROM rpt_a a JOIN w ON a.a = w.a'); + +-- Join against a partitioned table: Append boundary. +SELECT rpt_transfer_plan('SELECT * FROM rpt_a a JOIN rpt_p p ON a.a = p.a'); + +DROP TABLE rpt_p; +DROP TABLE rpt_a, rpt_b, rpt_c; +DROP EXTENSION robust_predicate_transfer; diff --git a/gpcontrib/robust_predicate_transfer/src/rptgraph.cpp b/gpcontrib/robust_predicate_transfer/src/rptgraph.cpp new file mode 100644 index 00000000000..5fd2a01f6f8 --- /dev/null +++ b/gpcontrib/robust_predicate_transfer/src/rptgraph.cpp @@ -0,0 +1,595 @@ +/*------------------------------------------------------------------------- + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * rptgraph.cpp + * Union-find equivalence classes over join keys, vertex + * classification, and the SkipUnfilteredTable rewiring of the + * join graph. + * + * Ported from the RPT+ artifact (embryo-labs/dynamic-predicate-transfer), + * src/optimizer/predicate_transfer/transfer_graph_manager.cpp and dag.cpp -- + * MIT license, Copyright 2018-2025 Stichting DuckDB Foundation (see + * licenses/LICENSE-mit-duckdb-rpt.txt): + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + * IDENTIFICATION + * gpcontrib/robust_predicate_transfer/src/rptgraph.cpp + * + *------------------------------------------------------------------------- + */ +extern "C" +{ +#include "postgres.h" +} + +#include "rpt.h" + +#include + +/* + * One (edge, clause) pair. The artifact's EdgeInfo carries a single join + * condition; ours merges every equality clause of a vertex pair into one + * edge, so the SkipUnfilteredTable helpers track the clause alongside the + * edge wherever the original tracked the edge alone. + */ +struct RptEdgeClause +{ + RptEdgeInfo *edge; + size_t clause; +}; + +/* + * Find the representative key of a key's equivalence class. Ports + * FindBindingRoot (transfer_graph_manager.cpp:12-23), with path + * compression. + */ +static RptKey +rpt_find_key_root(const RptKey &key, std::map &uf_parent) +{ + std::map::iterator it = uf_parent.find(key); + RptKey root; + + if (it == uf_parent.end()) + return key; + + root = it->second; + if (!(root == key)) + { + root = rpt_find_key_root(root, uf_parent); + uf_parent[key] = root; /* path compression */ + } + return root; +} + +/* + * Merge the equivalence classes of two clause keys. Ports UnionBindings + * (transfer_graph_manager.cpp:25-48), union by attaching b to a. The + * artifact keeps a group pointer per stale root and resolves the root again + * at lookup time; mgr->groups is keyed by the representative key, so the + * loser's entry is merged into the winner's and erased here instead. + */ +static void +rpt_union_keys(const RptKey &a, const RptKey &b, Oid key_type, + RptTransferMgr *mgr) +{ + RptKey root_a = rpt_find_key_root(a, mgr->uf_parent); + RptKey root_b = rpt_find_key_root(b, mgr->uf_parent); + std::map::iterator ga; + std::map::iterator gb; + + if (root_a == root_b) + return; + + /* Union by attaching b to a */ + mgr->uf_parent[root_b] = root_a; + + ga = mgr->groups.find(root_a); + if (ga == mgr->groups.end()) + { + RptKeyGroup group; + + group.leader_id = -1; + group.key_type = key_type; + group.members.insert(a.rtindex); + ga = mgr->groups.insert(std::make_pair(root_a, group)).first; + } + + gb = mgr->groups.find(root_b); + if (gb == mgr->groups.end()) + { + RptKeyGroup group; + + group.leader_id = -1; + group.key_type = key_type; + group.members.insert(b.rtindex); + gb = mgr->groups.insert(std::make_pair(root_b, group)).first; + } + + ga->second.members.insert(gb->second.members.begin(), + gb->second.members.end()); + mgr->groups.erase(root_b); +} + +/* + * ClassifyTables (transfer_graph_manager.cpp:265-309): classify all tables + * into three categories: intermediate table, unfiltered table, and filtered + * table. + * + * The artifact tests LOGICAL_FILTER / LogicalGet.table_filters; our region + * pass has already set RptVertexInfo.filtered from the scan's restriction + * quals, which is the same test seen one plan layer down. The artifact + * counts the group of each incident edge's left binding; we count the group + * of each of the vertex's own join keys. The two differ only on protected + * edges, whose keys were never unioned and so count as groups of their own + * here (the artifact conflates all of them into one null group). + */ +static void +rpt_classify_tables(RptTransferMgr *mgr) +{ + size_t i; + + for (i = 0; i < mgr->vertices.size(); i++) + { + RptVertexInfo *v = &mgr->vertices[i]; + std::set belong_groups; + std::set::const_iterator kit; + + v->intermediate = false; + v->unfiltered = false; + + /* 1. Filtered table: the scan carries restriction quals */ + if (v->filtered) + continue; + + /* 2. Find belong groups */ + for (kit = v->join_keys.begin(); kit != v->join_keys.end(); ++kit) + { + if (mgr->uf_parent.find(*kit) != mgr->uf_parent.end() || + mgr->groups.find(*kit) != mgr->groups.end()) + belong_groups.insert(rpt_find_key_root(*kit, mgr->uf_parent)); + else + belong_groups.insert(*kit); /* never unioned: a group of its own */ + } + + /* 3. Intermediate table: touches more than one key group */ + if (belong_groups.size() > 1) + { + v->intermediate = true; + continue; + } + + /* 4. Last, it is an unfiltered table */ + v->unfiltered = true; + } +} + +/* Helper: check whether data can flow from one table to another */ +static bool +rpt_can_flow(const RptEdgeInfo *e, int from_v, int to_v) +{ + if (e->left_v == from_v && e->right_v == to_v) + return !e->protect_right; /* Left → Right allowed if not protected */ + if (e->right_v == from_v && e->left_v == to_v) + return !e->protect_left; /* Right → Left allowed if not protected */ + return false; +} + +/* Helper: get the key on vertex t's side of one clause of edge e */ +static const RptKey & +rpt_key_at(const RptEdgeInfo *e, size_t clause, int t) +{ + return (e->left_v == t) ? e->clauses[clause].first : e->clauses[clause].second; +} + +/* Helper: get the key on the side opposite vertex t in one clause of edge e */ +static const RptKey & +rpt_key_opposite(const RptEdgeInfo *e, size_t clause, int t) +{ + return (e->left_v == t) ? e->clauses[clause].second : e->clauses[clause].first; +} + +/* + * Helper: append the clauses of `other` that `old` does not have yet, in + * old's orientation (the artifact's edges are single-clause, so it has no + * need for this). + */ +static void +rpt_merge_clauses(RptEdgeInfo *old, const RptEdgeInfo &other, bool reversed) +{ + size_t i; + + for (i = 0; i < other.clauses.size(); i++) + { + std::pair clause = other.clauses[i]; + bool have = false; + size_t j; + + if (reversed) + std::swap(clause.first, clause.second); + for (j = 0; j < old->clauses.size(); j++) + { + if (old->clauses[j].first == clause.first && + old->clauses[j].second == clause.second) + { + have = true; + break; + } + } + if (!have) + old->clauses.push_back(clause); + } +} + +/* + * Helper: insert new edge into neighbor_matrix, merging if already exists. + * The matrix holds each edge twice ([a][b] and [b][a]) by value where the + * artifact shares one EdgeInfo between both directions, so every write here + * is mirrored explicitly. + */ +static void +rpt_upsert_edge(RptTransferMgr *mgr, const RptEdgeInfo &ne) +{ + int i = ne.left_v; + int j = ne.right_v; + std::map::iterator ij; + bool exists = false; + + ij = mgr->neighbor_matrix[i].find(j); + if (ij != mgr->neighbor_matrix[i].end()) + { + /* + * The artifact compares the single bindings and clobbers a parallel + * edge on different columns; our edges already hold every clause of + * the vertex pair, so the same vertex pair in either orientation is + * the same edge and the clauses merge instead. + */ + bool same = (ij->second.left_v == ne.left_v && + ij->second.right_v == ne.right_v); + bool rev = (ij->second.left_v == ne.right_v && + ij->second.right_v == ne.left_v); + + if (same) + { + ij->second.protect_left &= ne.protect_left; + ij->second.protect_right &= ne.protect_right; + rpt_merge_clauses(&ij->second, ne, false); + exists = true; + } + else if (rev) + { + ij->second.protect_left &= ne.protect_right; + ij->second.protect_right &= ne.protect_left; + rpt_merge_clauses(&ij->second, ne, true); + exists = true; + } + if (exists) + mgr->neighbor_matrix[j][i] = ij->second; + } + if (!exists) + { + mgr->neighbor_matrix[i][j] = ne; + mgr->neighbor_matrix[j][i] = ne; + } +} + +/* + * SkipUnfilteredTable (transfer_graph_manager.cpp:311-511). + * + * Unfiltered tables only receive Bloom filters, they do not generate them: + * splice a direct bypass edge between the carriers and the destinations on + * the same join key, block the unfiltered table's outgoing edges, and drop + * fully blocked edges. Then, for each intermediate table with no incoming + * edge left, block its outgoing edges as well. + */ +static void +rpt_skip_unfiltered(RptTransferMgr *mgr) +{ + std::vector unfiltered_ids; + std::vector intermediate_ids; + size_t i; + bool changed; + + /* Sorted id lists keep the iteration (and hence the dumps) deterministic. */ + for (i = 0; i < mgr->vertices.size(); i++) + { + if (mgr->vertices[i].unfiltered) + unfiltered_ids.push_back(mgr->vertices[i].id); + else if (mgr->vertices[i].intermediate) + intermediate_ids.push_back(mgr->vertices[i].id); + } + std::sort(unfiltered_ids.begin(), unfiltered_ids.end()); + std::sort(intermediate_ids.begin(), intermediate_ids.end()); + + /* 1) Iteratively skip unfiltered tables until no new edges are added. */ + do + { + changed = false; + + for (i = 0; i < unfiltered_ids.size(); i++) + { + int t = unfiltered_ids[i]; + std::map &adj = mgr->neighbor_matrix[t]; + std::map > incoming_by_col; + std::vector outgoing; + std::map::iterator pit; + std::map >::iterator git; + size_t j; + + /* ---- Step 1: collect all incoming edges from carrier tables → t ---- */ + for (pit = adj.begin(); pit != adj.end(); ++pit) + { + RptEdgeInfo *e = &pit->second; + size_t c; + + if (!rpt_can_flow(e, pit->first, t)) + continue; + for (c = 0; c < e->clauses.size(); c++) + { + RptEdgeClause ref; + + ref.edge = e; + ref.clause = c; + incoming_by_col[rpt_key_at(e, c, t)].push_back(ref); + } + } + + /* ---- Step 2: collect all outgoing edges t → neighbor ---- */ + for (pit = adj.begin(); pit != adj.end(); ++pit) + { + if (rpt_can_flow(&pit->second, t, pit->first)) + outgoing.push_back(&pit->second); + } + + /* + * ---- Step 3: bypass t ---- + * For every (carrier → t) and (t → dst) pair with the same join + * column, create a direct edge (carrier → dst). + */ + for (git = incoming_by_col.begin(); git != incoming_by_col.end(); ++git) + { + for (j = 0; j < outgoing.size(); j++) + { + RptEdgeInfo *out_e = outgoing[j]; + size_t oc; + + for (oc = 0; oc < out_e->clauses.size(); oc++) + { + RptEdgeClause rep = git->second[0]; /* representative incoming edge */ + RptEdgeInfo *in_e = rep.edge; + RptEdgeInfo ne; + int src_v; + int dst_v; + + if (!(rpt_key_at(out_e, oc, t) == git->first)) + continue; /* must be same join key */ + + /* src is the carrier: in_e's side opposite t */ + src_v = (in_e->left_v == t) ? in_e->right_v : in_e->left_v; + + ne.left_v = src_v; + ne.key_type = out_e->key_type; + ne.protect_left = false; + ne.protect_right = false; + + if (out_e->left_v == t) + { + /* t is left side of out_e */ + dst_v = out_e->right_v; + ne.right_v = dst_v; + ne.clauses.push_back(std::make_pair(rpt_key_opposite(in_e, rep.clause, t), + out_e->clauses[oc].second)); + ne.protect_left = true; /* disable reverse flow (R → L) */ + } + else + { + /* t is right side of out_e */ + dst_v = out_e->left_v; + ne.right_v = dst_v; + ne.clauses.push_back(std::make_pair(rpt_key_opposite(in_e, rep.clause, t), + out_e->clauses[oc].first)); + + /* + * disable reverse flow (L → R) -- kept verbatim + * from the artifact; with src on the left this + * blocks the carrier → destination direction, + * where the t-is-left case above blocks the + * reverse. + */ + ne.protect_right = true; + } + + if (src_v == dst_v) + continue; /* a self-edge no later pass can observe */ + + rpt_upsert_edge(mgr, ne); + changed = true; + } + } + } + + /* ---- Step 4: disable all outgoing edges from this unfiltered table ---- */ + for (j = 0; j < outgoing.size(); j++) + { + RptEdgeInfo *e = outgoing[j]; + + if (e->left_v == t) + { + if (!e->protect_right) + { + e->protect_right = true; /* block Left→Right */ + changed = true; + } + } + else + { + if (!e->protect_left) + { + e->protect_left = true; /* block Right→Left */ + changed = true; + } + } + } + + /* ---- Step 5: remove fully blocked edges (no direction left) ---- */ + for (pit = adj.begin(); pit != adj.end();) + { + RptEdgeInfo *e = &pit->second; + int nbr = pit->first; + + if (e->protect_left && e->protect_right) + { + ++pit; + mgr->neighbor_matrix[t].erase(nbr); + mgr->neighbor_matrix[nbr].erase(t); + changed = true; + } + else + { + /* keep the reverse matrix entry in sync */ + mgr->neighbor_matrix[nbr][t] = *e; + ++pit; + } + } + } + } while (changed); + + /* + * 2) For each intermediate table, if it has no filter and any in-edge, it + * should not have out-edge. (The artifact's comment says "any in-edge" + * but its code -- and ours -- does this when no in-edge is left.) + */ + for (i = 0; i < intermediate_ids.size(); i++) + { + int t = intermediate_ids[i]; + std::map &adj = mgr->neighbor_matrix[t]; + std::map > incoming_by_col; + std::map::iterator pit; + size_t c; + + /* ---- collect all incoming edges from carrier tables → t ---- */ + for (pit = adj.begin(); pit != adj.end(); ++pit) + { + RptEdgeInfo *e = &pit->second; + + if (!rpt_can_flow(e, pit->first, t)) + continue; + for (c = 0; c < e->clauses.size(); c++) + { + RptEdgeClause ref; + + ref.edge = e; + ref.clause = c; + incoming_by_col[rpt_key_at(e, c, t)].push_back(ref); + } + } + + if (incoming_by_col.empty()) + { + for (pit = adj.begin(); pit != adj.end(); ++pit) + { + RptEdgeInfo *e = &pit->second; + + if (!rpt_can_flow(e, t, pit->first)) + continue; + if (e->left_v == t) + e->protect_right = true; /* block Left→Right */ + else + e->protect_left = true; /* block Right→Left */ + } + + for (pit = adj.begin(); pit != adj.end();) + { + RptEdgeInfo *e = &pit->second; + int nbr = pit->first; + + if (e->protect_left && e->protect_right) + { + ++pit; + mgr->neighbor_matrix[t].erase(nbr); + mgr->neighbor_matrix[nbr].erase(t); + } + else + { + /* keep the reverse matrix entry in sync */ + mgr->neighbor_matrix[nbr][t] = *e; + ++pit; + } + } + } + } +} + +/* + * Build the join-key equivalence classes and classify the vertices, then + * rewire the graph around tables that carry no information of their own. + * This is the rptgraph half of the artifact's Build() + * (transfer_graph_manager.cpp:50-70): the union pass of ExtractEdgesInfo + * over edges the region pass already extracted, ClassifyTables, and + * SkipUnfilteredTable. + */ +void +RptBuildClasses(RptTransferMgr *mgr) +{ + std::map >::iterator row; + + /* + * Union the clause keys of every edge that is unprotected both ways + * (transfer_graph_manager.cpp:249-251): the preserved side of an outer + * join keeps its rows whether or not they match, so a protected edge + * never merges classes. The matrix holds each edge in both directions; + * unioning twice is idempotent. + */ + for (row = mgr->neighbor_matrix.begin(); row != mgr->neighbor_matrix.end(); ++row) + { + std::map::iterator eit; + + for (eit = row->second.begin(); eit != row->second.end(); ++eit) + { + RptEdgeInfo *edge = &eit->second; + size_t c; + + if (edge->protect_left || edge->protect_right) + continue; + for (c = 0; c < edge->clauses.size(); c++) + rpt_union_keys(edge->clauses[c].first, edge->clauses[c].second, + edge->key_type, mgr); + } + } + + rpt_classify_tables(mgr); + rpt_skip_unfiltered(mgr); +} diff --git a/gpcontrib/robust_predicate_transfer/src/rptinit.cpp b/gpcontrib/robust_predicate_transfer/src/rptinit.cpp new file mode 100644 index 00000000000..25f4894632a --- /dev/null +++ b/gpcontrib/robust_predicate_transfer/src/rptinit.cpp @@ -0,0 +1,230 @@ +/*------------------------------------------------------------------------- + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * rptinit.cpp + * Module entry point: GUCs, the planner hook, and the SQL-visible + * transfer-plan dump function. + * + * The extension rides on Anser (gpcontrib/anser), which must be preloaded + * first: the AnserXxx entry points we call are resolved at load time from + * the global symbol table (dfmgr loads modules RTLD_GLOBAL). + * + * Phase I is analysis-only: the planner hook computes the transfer plan and + * logs it at DEBUG1 when rpt.debug is on, and rpt_transfer_plan() exposes + * the same dump to SQL for the regression tests. Nothing is injected into + * the plan tree. + * + * IDENTIFICATION + * gpcontrib/robust_predicate_transfer/src/rptinit.cpp + * + *------------------------------------------------------------------------- + */ +extern "C" +{ +#include "postgres.h" + +#include "cdb/cdbvars.h" +#include "fmgr.h" +#include "miscadmin.h" +#include "nodes/pg_list.h" +#include "optimizer/planner.h" +#include "parser/analyze.h" +#include "tcop/tcopprot.h" +#include "utils/builtins.h" +#include "utils/guc.h" + +PG_MODULE_MAGIC; + +PG_FUNCTION_INFO_V1(rpt_transfer_plan); + +void _PG_init(void); +} + +#include "rpt.h" + +bool rpt_enable = false; +bool rpt_debug = false; +int rpt_max_chain_depth = 4; + +static planner_hook_type prev_planner_hook = NULL; + +static void rpt_define_gucs(void); +static PlannedStmt *rpt_planner(Query *parse, const char *query_string, + int cursorOptions, ParamListInfo boundParams, + OptimizerOptions *optimizer_options); + +/* + * Run the Phase I analysis over a finished statement and return the dump. + * The manager is plain C++ in the caller's memory context; on any error the + * whole context goes away with the statement. + */ +static void +rpt_analyze(PlannedStmt *stmt, StringInfo out) +{ + RptTransferMgr mgr; + + RptCollectGraph(stmt, &mgr); + if (mgr.vertices.empty()) + { + appendStringInfoString(out, "no transfer regions\n"); + return; + } + + RptBuildClasses(&mgr); + RptBuildTransferPlan(stmt, &mgr); + RptDumpTransferPlan(&mgr, out); +} + +void +_PG_init(void) +{ + rpt_define_gucs(); + + /* + * Only a preloaded library can be relied on to have installed its hooks + * in every backend. Loaded any other way the extension stays inert: the + * GUCs exist (so a stray setting is not an error) and the dump function + * works, but nothing is wired into planning. + */ + if (!process_shared_preload_libraries_in_progress) + return; + + prev_planner_hook = planner_hook; + planner_hook = rpt_planner; + + MarkGUCPrefixReserved("rpt"); +} + +/* + * The extension's GUCs are "rpt.*"-qualified because they belong to a + * loadable module; "anser" is already reserved by the extension we ride on. + */ +static void +rpt_define_gucs(void) +{ + DefineCustomBoolVariable("rpt.enable", + "Enables RPT+ transfer-plan analysis over Anser.", + "When disabled the planner hook is a pass-through.", + &rpt_enable, + false, + PGC_USERSET, + GUC_GPDB_NEED_SYNC, + NULL, NULL, NULL); + + DefineCustomBoolVariable("rpt.debug", + "Logs the RPT+ transfer plan of every analyzed query.", + "Dumps regions, equivalence classes and the forward/backward transfer plan at DEBUG1.", + &rpt_debug, + false, + PGC_USERSET, + GUC_GPDB_NEED_SYNC, + NULL, NULL, NULL); + + DefineCustomIntVariable("rpt.max_chain_depth", + "Sets the maximum forward-chain length RPT+ may plan.", + "Every chain hop is a global publish/merge/deliver barrier in MPP, so the chain depth is capped; the default is a placeholder until hop latency is measured.", + &rpt_max_chain_depth, + 4, 1, INT_MAX, + PGC_USERSET, + GUC_GPDB_NEED_SYNC, + NULL, NULL, NULL); +} + +/* + * Plan as usual, then run the analysis over the finished tree. Chaining + * prev first means that when Anser is preloaded before us its injection pass + * has already run on the tree we analyze, which is what we want: the region + * discovery sees through Anser's own filter nodes. + */ +static PlannedStmt * +rpt_planner(Query *parse, const char *query_string, int cursorOptions, + ParamListInfo boundParams, OptimizerOptions *optimizer_options) +{ + PlannedStmt *result; + + if (prev_planner_hook) + result = prev_planner_hook(parse, query_string, cursorOptions, + boundParams, optimizer_options); + else + result = standard_planner(parse, query_string, cursorOptions, + boundParams, optimizer_options); + + if (rpt_enable && rpt_debug && + Gp_role == GP_ROLE_DISPATCH && + result != NULL && result->commandType == CMD_SELECT && + result->planTree != NULL) + { + StringInfoData buf; + + initStringInfo(&buf); + rpt_analyze(result, &buf); + elog(DEBUG1, "rpt transfer plan:\n%s", buf.data); + } + + return result; +} + +/* + * rpt_transfer_plan(query text) -> text + * + * Plan the query and dump its RPT+ transfer plan. Analysis only: the plan + * is discarded afterwards. This is the regression-test driver. + */ +Datum +rpt_transfer_plan(PG_FUNCTION_ARGS) +{ + char *query_string = text_to_cstring(PG_GETARG_TEXT_PP(0)); + List *raw_parsetree_list; + List *query_list; + Query *query; + PlannedStmt *stmt; + StringInfoData buf; + + raw_parsetree_list = pg_parse_query(query_string); + if (list_length(raw_parsetree_list) != 1) + ereport(ERROR, + (errcode(ERRCODE_SYNTAX_ERROR), + errmsg("rpt_transfer_plan expects a single statement"))); + + query_list = pg_analyze_and_rewrite_fixedparams( + (RawStmt *) linitial(raw_parsetree_list), + query_string, NULL, 0, NULL); + if (list_length(query_list) != 1) + ereport(ERROR, + (errcode(ERRCODE_SYNTAX_ERROR), + errmsg("rpt_transfer_plan expects a single query"))); + + query = (Query *) linitial(query_list); + if (query->commandType != CMD_SELECT) + ereport(ERROR, + (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("rpt_transfer_plan supports only SELECT"))); + + /* + * pg_plan_query runs the hook chain, so with anser preloaded and its + * runtime_filter GUC on the tree we analyze already carries Anser's + * injected nodes -- exactly what production planning will hand us. + */ + stmt = pg_plan_query(query, query_string, 0, NULL); + + initStringInfo(&buf); + rpt_analyze(stmt, &buf); + + PG_RETURN_TEXT_P(cstring_to_text(buf.data)); +} diff --git a/gpcontrib/robust_predicate_transfer/src/rptplan.cpp b/gpcontrib/robust_predicate_transfer/src/rptplan.cpp new file mode 100644 index 00000000000..6a80270da77 --- /dev/null +++ b/gpcontrib/robust_predicate_transfer/src/rptplan.cpp @@ -0,0 +1,730 @@ +/*------------------------------------------------------------------------- + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * rptplan.cpp + * The asymmetric transfer plan: a Prim spanning forest + * (LargestRootUpdated), forward/backward stage assignment + * (CreateTransferPlanUpdated), backward-consumer eligibility, and + * the deterministic text dump that feeds the regression tests. + * + * Ported from the RPT+ artifact (embryo-labs/dynamic-predicate-transfer), + * src/optimizer/predicate_transfer/transfer_graph_manager.cpp and dag.cpp -- + * MIT license, Copyright 2018-2025 Stichting DuckDB Foundation (see + * licenses/LICENSE-mit-duckdb-rpt.txt): + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + * IDENTIFICATION + * gpcontrib/robust_predicate_transfer/src/rptplan.cpp + * + *------------------------------------------------------------------------- + */ +extern "C" +{ +#include "postgres.h" + +#include "optimizer/walkers.h" +} + +#include "rpt.h" + +#include + +/* + * Find the representative key of a key's equivalence class, with path + * compression. A file-local copy of rptgraph.cpp's rpt_find_key_root + * (FindBindingRoot, transfer_graph_manager.cpp:12-23). + */ +static RptKey +rpt_find_key_root(const RptKey &key, std::map &uf_parent) +{ + std::map::iterator it = uf_parent.find(key); + RptKey root; + + if (it == uf_parent.end()) + return key; + + root = it->second; + if (!(root == key)) + { + root = rpt_find_key_root(root, uf_parent); + uf_parent[key] = root; /* path compression */ + } + return root; +} + +/* + * GraphNode::Add (dag.cpp:9-19): find-or-create the edge to `other` in the + * given stage and direction. + */ +static RptGraphEdge * +rpt_node_add(RptGraphNode *node, int other, bool is_forward, bool is_in_edge) +{ + std::vector &edges = is_forward + ? (is_in_edge ? node->forward_stage.in : node->forward_stage.out) + : (is_in_edge ? node->backward_stage.in : node->backward_stage.out); + size_t i; + + for (i = 0; i < edges.size(); i++) + { + if (edges[i].destination == other) + return &edges[i]; + } + edges.push_back(RptGraphEdge()); + edges.back().destination = other; + return &edges.back(); +} + +/* GraphNode::Add with keys (dag.cpp:21-28). */ +static RptGraphEdge * +rpt_node_add_keys(RptGraphNode *node, int other, + const std::vector &left_keys, + const std::vector &right_keys, + const std::vector &key_types, + bool is_forward, bool is_in_edge) +{ + RptGraphEdge *edge = rpt_node_add(node, other, is_forward, is_in_edge); + + edge->left_keys = left_keys; + edge->right_keys = right_keys; + edge->key_types = key_types; + return edge; +} + +/* + * RegisterLeader (transfer_graph_manager.hpp:50-55): the first vertex of a + * class to enter the transfer order becomes the leader the backward + * broadcast builds its single filter on. + */ +static void +rpt_register_leader(RptTransferMgr *mgr, int vertex_id) +{ + const RptVertexInfo &v = mgr->vertices[mgr->by_rti[vertex_id]]; + std::set::const_iterator kit; + + for (kit = v.join_keys.begin(); kit != v.join_keys.end(); ++kit) + { + RptKey rep = rpt_find_key_root(*kit, mgr->uf_parent); + std::map::iterator git = mgr->groups.find(rep); + + if (git != mgr->groups.end() && git->second.leader_id == -1) + { + git->second.leader_id = vertex_id; + git->second.leader_key = *kit; + } + } +} + +/* + * FindEdge (transfer_graph_manager.cpp:736-759): the frontier edge to the + * unconstructed vertex with the largest cardinality, preferring edges that + * are unprotected both ways. + */ +static bool +rpt_find_edge(const RptTransferMgr *mgr, const std::set &constructed_set, + const std::set &unconstructed_set, int *from_id, int *to_id) +{ + int64 max_cardinality = 0; + bool is_indirected = false; + bool found = false; + std::set::const_iterator ii; + + for (ii = unconstructed_set.begin(); ii != unconstructed_set.end(); ++ii) + { + std::set::const_iterator ji; + + for (ji = constructed_set.begin(); ji != constructed_set.end(); ++ji) + { + std::map >::const_iterator row; + std::map::const_iterator eit; + const RptEdgeInfo *edge; + int64 cardinality; + + row = mgr->neighbor_matrix.find(*ji); + if (row == mgr->neighbor_matrix.end()) + continue; + eit = row->second.find(*ii); + if (eit == row->second.end()) + continue; + edge = &eit->second; + + cardinality = mgr->vertices[mgr->by_rti.find(*ii)->second].cardinality; + if (cardinality > max_cardinality || + (!is_indirected && !edge->protect_left && !edge->protect_right)) + { + max_cardinality = cardinality; + *from_id = *ji; + *to_id = *ii; + found = true; + is_indirected = !edge->protect_left && !edge->protect_right; + } + } + } + return found; +} + +/* + * LargestRootUpdated (transfer_graph_manager.cpp:557-626): grow one + * component of the spanning forest by Prim's algorithm, rooted at the + * largest filtered-or-intermediate vertex, falling back to the largest + * vertex. sorted_ids lists every vertex by ascending cardinality; only + * those still in `remaining` are candidates, and placed vertices are removed + * from it (the artifact instead erases from its operator map and can pick a + * stale root from its sorted list on the next component; keeping `remaining` + * explicit avoids that). + */ +static void +rpt_largest_root_updated(RptTransferMgr *mgr, std::set &remaining, + const std::vector &sorted_ids, + std::vector > &selected_edges, + int32 &next_order) +{ + std::set constructed_set; + std::set unconstructed_set; + std::vector::const_reverse_iterator rit; + std::set::iterator sit; + int root = -1; + + /* Try to choose the largest filtered or intermediate table as the root */ + for (rit = sorted_ids.rbegin(); rit != sorted_ids.rend(); ++rit) + { + const RptVertexInfo &v = mgr->vertices[mgr->by_rti[*rit]]; + + if (remaining.count(*rit) && (v.filtered || v.intermediate)) + { + root = *rit; + break; + } + } + + /* If we cannot find it, all tables have no filter */ + if (root == -1) + { + for (rit = sorted_ids.rbegin(); rit != sorted_ids.rend(); ++rit) + { + if (remaining.count(*rit)) + { + root = *rit; + break; + } + } + } + Assert(root != -1); + + /* Initialize nodes */ + for (sit = remaining.begin(); sit != remaining.end(); ++sit) + { + if (*sit == root) + constructed_set.insert(*sit); + else + unconstructed_set.insert(*sit); + + mgr->graph[*sit].id = *sit; + mgr->graph[*sit].cardinality_order = -1; + } + + /* Add root */ + mgr->graph[root].cardinality_order = next_order++; + mgr->transfer_order.push_back(root); + rpt_register_leader(mgr, root); + remaining.erase(root); + + /* Build graph */ + while (!unconstructed_set.empty()) + { + int from_id; + int to_id; + + if (!rpt_find_edge(mgr, constructed_set, unconstructed_set, + &from_id, &to_id)) + break; + + selected_edges.push_back(std::make_pair(from_id, to_id)); + mgr->graph[to_id].cardinality_order = next_order++; + mgr->transfer_order.push_back(to_id); + rpt_register_leader(mgr, to_id); + + unconstructed_set.erase(to_id); + constructed_set.insert(to_id); + remaining.erase(to_id); + } + + /* + * Whatever is still unconstructed is disconnected from this root; it + * stays in `remaining` and the caller wraps it in another component. + */ +} + +/* + * CreateTransferPlanUpdated (transfer_graph_manager.cpp:677-734): assign + * the selected edges to the forward and backward stages. The smaller table + * -- the one that entered the Prim order later -- is on the left; forward + * transfers flow from the smaller to the larger, backward transfers from + * the larger to the smaller, with backward edges redirected at the + * equivalence-class leader. + */ +static void +rpt_create_transfer_plan(RptTransferMgr *mgr, + const std::vector > &selected_edges) +{ + size_t i; + + for (i = 0; i < selected_edges.size(); i++) + { + std::map::iterator eit; + RptEdgeInfo *edge; + RptGraphNode *left_node; + RptGraphNode *right_node; + std::vector left_keys; + std::vector right_keys; + std::vector key_types; + bool protect_left; + bool protect_right; + size_t c; + + eit = mgr->neighbor_matrix[selected_edges[i].first].find(selected_edges[i].second); + if (eit == mgr->neighbor_matrix[selected_edges[i].first].end()) + continue; + edge = &eit->second; + + left_node = &mgr->graph[edge->left_v]; + right_node = &mgr->graph[edge->right_v]; + protect_left = edge->protect_left; + protect_right = edge->protect_right; + + for (c = 0; c < edge->clauses.size(); c++) + { + left_keys.push_back(edge->clauses[c].first); + right_keys.push_back(edge->clauses[c].second); + key_types.push_back(edge->key_type); + } + + /* smaller table is in the left */ + if (left_node->cardinality_order < right_node->cardinality_order) + { + std::swap(left_node, right_node); + std::swap(left_keys, right_keys); + std::swap(protect_left, protect_right); + } + + /* forward: from the smaller to the larger */ + if (!protect_right) + { + rpt_node_add_keys(left_node, right_node->id, left_keys, right_keys, + key_types, true, false); + rpt_node_add_keys(right_node, left_node->id, left_keys, right_keys, + key_types, true, true); + } + + /* backward: from the larger to the smaller */ + if (!protect_left) + { + /* + * Group-leader redirection (transfer_graph_manager.cpp:719-732): + * the backward edge is built on the class leader, which produces + * the single broadcast filter for the whole class. The + * artifact's edges are single-key; ours bucket the clauses by + * destination so a multi-key edge can fan out to one leader per + * clause class. + */ + std::map by_dest; + std::map::iterator dit; + + for (c = 0; c < left_keys.size(); c++) + { + RptKey rep = rpt_find_key_root(right_keys[c], mgr->uf_parent); + std::map::iterator git = mgr->groups.find(rep); + int dest; + RptKey dest_key; + RptGraphEdge *bucket; + + if (git != mgr->groups.end() && git->second.leader_id != -1) + { + dest = git->second.leader_id; + dest_key = git->second.leader_key; + } + else + { + dest = right_node->id; + dest_key = right_keys[c]; + } + + bucket = &by_dest[dest]; + bucket->destination = dest; + bucket->left_keys.push_back(left_keys[c]); + bucket->right_keys.push_back(dest_key); + bucket->key_types.push_back(key_types[c]); + } + + for (dit = by_dest.begin(); dit != by_dest.end(); ++dit) + { + const RptGraphEdge &bucket = dit->second; + + rpt_node_add_keys(left_node, bucket.destination, + bucket.left_keys, bucket.right_keys, + bucket.key_types, false, true); + rpt_node_add_keys(&mgr->graph[bucket.destination], left_node->id, + bucket.left_keys, bucket.right_keys, + bucket.key_types, false, false); + } + } + } +} + +/* + * Backward-consumer eligibility: Anser's deadlock rule, with no counterpart + * in the single-node artifact. A live backward consumer blocks until its + * producer's filter arrives, so it may only wait on a producer the plan + * order guarantees runs first: the producer's scan on the build (inner) + * side of a hash join and the consumer's scan on the probe (outer) side of + * the same join. consumer_ok[v] is true exactly when that holds for one of + * v's backward-stage in-edges; everything else fails open (no backward + * filter for that table). + * + * The contexts are prefixed with plan_tree_base_prefix because + * plan_tree_walker resolves SubPlan nodes through it, even when not + * recursing into subplans. + */ +typedef struct RptScanCollectContext +{ + plan_tree_base_prefix base; /* must be first */ + const std::map *scan_vertex; + std::set ids; +} RptScanCollectContext; + +typedef struct RptConsumerCheckContext +{ + plan_tree_base_prefix base; /* must be first */ + PlannedStmt *stmt; + const std::map *scan_vertex; + const std::map > *producers; + std::map *consumer_ok; +} RptConsumerCheckContext; + +/* Collect the ids of every vertex whose scan appears under this subtree. */ +static bool +rpt_collect_scan_ids_walker(Node *node, void *context) +{ + RptScanCollectContext *ctx = (RptScanCollectContext *) context; + std::map::const_iterator it; + + if (node == NULL) + return false; + + /* vertex scans match by pointer; non-Plan nodes simply never hit */ + it = ctx->scan_vertex->find((const Plan *) node); + if (it != ctx->scan_vertex->end()) + ctx->ids.insert(it->second); + + return plan_tree_walker(node, rpt_collect_scan_ids_walker, context, false); +} + +static bool +rpt_consumer_check_walker(Node *node, void *context) +{ + RptConsumerCheckContext *ctx = (RptConsumerCheckContext *) context; + + if (node == NULL) + return false; + + if (IsA(node, HashJoin)) + { + HashJoin *hj = (HashJoin *) node; + RptScanCollectContext inner_ctx; + RptScanCollectContext outer_ctx; + std::map >::const_iterator pit; + + exec_init_plan_tree_base(&inner_ctx.base, ctx->stmt); + inner_ctx.scan_vertex = ctx->scan_vertex; + exec_init_plan_tree_base(&outer_ctx.base, ctx->stmt); + outer_ctx.scan_vertex = ctx->scan_vertex; + + /* build side is the inner subtree, probe side the outer */ + rpt_collect_scan_ids_walker((Node *) innerPlan(hj), &inner_ctx); + rpt_collect_scan_ids_walker((Node *) outerPlan(hj), &outer_ctx); + + for (pit = ctx->producers->begin(); pit != ctx->producers->end(); ++pit) + { + int consumer = pit->first; + std::set::const_iterator prod; + + if ((*ctx->consumer_ok)[consumer] || + !outer_ctx.ids.count(consumer)) + continue; + for (prod = pit->second.begin(); prod != pit->second.end(); ++prod) + { + if (inner_ctx.ids.count(*prod)) + { + (*ctx->consumer_ok)[consumer] = true; + break; + } + } + } + } + + return plan_tree_walker(node, rpt_consumer_check_walker, context, false); +} + +static void +rpt_check_consumers(PlannedStmt *stmt, RptTransferMgr *mgr) +{ + std::map scan_vertex; + std::map > producers; + RptConsumerCheckContext ctx; + size_t i; + std::map::const_iterator nit; + + for (i = 0; i < mgr->vertices.size(); i++) + { + mgr->consumer_ok[mgr->vertices[i].id] = false; + scan_vertex[mgr->vertices[i].scan] = mgr->vertices[i].id; + } + + /* + * The producer of a backward in-edge is its destination: the class + * leader for a redirected edge, the larger table otherwise. + */ + for (nit = mgr->graph.begin(); nit != mgr->graph.end(); ++nit) + { + const std::vector &ins = nit->second.backward_stage.in; + + for (i = 0; i < ins.size(); i++) + producers[nit->first].insert(ins[i].destination); + } + + if (producers.empty() || stmt == NULL || stmt->planTree == NULL) + return; + + exec_init_plan_tree_base(&ctx.base, stmt); + ctx.stmt = stmt; + ctx.scan_vertex = &scan_vertex; + ctx.producers = &producers; + ctx.consumer_ok = &mgr->consumer_ok; + + rpt_consumer_check_walker((Node *) stmt->planTree, &ctx); +} + +/* + * Build the transfer plan for the join graph: the Prim forest, the + * forward/backward stage assignment, and the consumer-eligibility check. + */ +void +RptBuildTransferPlan(PlannedStmt *stmt, RptTransferMgr *mgr) +{ + std::vector > selected_edges; + std::vector sorted_ids; + std::set remaining; + int32 next_order = 0; + size_t i; + + /* vertices by ascending cardinality, ties by id, for a stable plan */ + for (i = 0; i < mgr->vertices.size(); i++) + { + sorted_ids.push_back(mgr->vertices[i].id); + remaining.insert(mgr->vertices[i].id); + } + std::sort(sorted_ids.begin(), sorted_ids.end(), + [&mgr] (int a, int b) + { + const RptVertexInfo &va = mgr->vertices[mgr->by_rti[a]]; + const RptVertexInfo &vb = mgr->vertices[mgr->by_rti[b]]; + + if (va.cardinality != vb.cardinality) + return va.cardinality < vb.cardinality; + return a < b; + }); + + /* + * The while-loop forest wrapping (transfer_graph_manager.cpp:677-683): + * repeat the Prim pass until every vertex is placed, giving a spanning + * forest -- one tree per connected component of the join graph. + */ + while (!remaining.empty()) + rpt_largest_root_updated(mgr, remaining, sorted_ids, selected_edges, + next_order); + + rpt_create_transfer_plan(mgr, selected_edges); + + rpt_check_consumers(stmt, mgr); +} + +/* + * Dump support. Everything below iterates std::map / sorted copies only, + * so the output is deterministic and can feed expected-output regression + * tests. + */ + +static bool +rpt_edge_less(const RptGraphEdge &a, const RptGraphEdge &b) +{ + if (a.destination != b.destination) + return a.destination < b.destination; + if (a.left_keys != b.left_keys) + return a.left_keys < b.left_keys; + return a.right_keys < b.right_keys; +} + +static void +rpt_dump_key_pairs(StringInfo out, const RptGraphEdge &edge) +{ + size_t i; + + for (i = 0; i < edge.left_keys.size(); i++) + { + appendStringInfo(out, "%s%d.%d->%d.%d", i > 0 ? "," : "", + edge.left_keys[i].rtindex, (int) edge.left_keys[i].attno, + edge.right_keys[i].rtindex, (int) edge.right_keys[i].attno); + } +} + +/* + * One stage of one vertex: " build for on ()" for each + * out-edge, " apply from on ()" for each in-edge, entries + * separated by "; ", edges sorted by (destination, keys). + */ +static void +rpt_dump_stage(StringInfo out, int vertex_id, const char *stage, + const std::vector &in_edges, + const std::vector &out_edges) +{ + std::vector outs(out_edges); + std::vector ins(in_edges); + bool any = false; + size_t i; + + std::sort(outs.begin(), outs.end(), rpt_edge_less); + std::sort(ins.begin(), ins.end(), rpt_edge_less); + + appendStringInfo(out, "vertex %d %s:", vertex_id, stage); + for (i = 0; i < outs.size(); i++) + { + appendStringInfo(out, "%sbuild for %d on (", any ? "; " : " ", + outs[i].destination); + rpt_dump_key_pairs(out, outs[i]); + appendStringInfoChar(out, ')'); + any = true; + } + for (i = 0; i < ins.size(); i++) + { + appendStringInfo(out, "%sapply from %d on (", any ? "; " : " ", + ins[i].destination); + rpt_dump_key_pairs(out, ins[i]); + appendStringInfoChar(out, ')'); + any = true; + } + if (!any) + appendStringInfoString(out, " none"); + appendStringInfoChar(out, '\n'); +} + +/* + * Dump the whole analysis as deterministic text: Prim transfer order, + * vertices by ascending id, equivalence classes by representative key, the + * forward/backward stages per vertex, and the consumer-eligibility verdicts. + */ +void +RptDumpTransferPlan(const RptTransferMgr *mgr, StringInfo out) +{ + std::vector ids; + size_t i; + std::map::const_iterator git; + + for (i = 0; i < mgr->vertices.size(); i++) + ids.push_back(mgr->vertices[i].id); + std::sort(ids.begin(), ids.end()); + + /* Prim insertion order */ + appendStringInfoString(out, "transfer order:"); + for (i = 0; i < mgr->transfer_order.size(); i++) + appendStringInfo(out, " %d", mgr->transfer_order[i]); + appendStringInfoChar(out, '\n'); + + /* vertices */ + for (i = 0; i < ids.size(); i++) + { + const RptVertexInfo &v = mgr->vertices[mgr->by_rti.find(ids[i])->second]; + + appendStringInfo(out, "vertex %d %s rows=" INT64_FORMAT " %s\n", + v.id, v.relname.c_str(), v.cardinality, + v.filtered ? "filtered" : + v.intermediate ? "intermediate" : "unfiltered"); + } + + /* equivalence classes */ + for (git = mgr->groups.begin(); git != mgr->groups.end(); ++git) + { + const RptKeyGroup &g = git->second; + std::set::const_iterator mit; + bool first = true; + + appendStringInfo(out, "class %d.%d: members=", + git->first.rtindex, (int) git->first.attno); + for (mit = g.members.begin(); mit != g.members.end(); ++mit) + { + appendStringInfo(out, "%s%d", first ? "" : ",", *mit); + first = false; + } + appendStringInfo(out, " leader=%d\n", g.leader_id); + } + + /* forward and backward stages per vertex */ + for (i = 0; i < ids.size(); i++) + { + std::map::const_iterator nit = mgr->graph.find(ids[i]); + + if (nit == mgr->graph.end()) + { + appendStringInfo(out, "vertex %d forward: none\n", ids[i]); + appendStringInfo(out, "vertex %d backward: none\n", ids[i]); + continue; + } + rpt_dump_stage(out, ids[i], "forward", + nit->second.forward_stage.in, + nit->second.forward_stage.out); + rpt_dump_stage(out, ids[i], "backward", + nit->second.backward_stage.in, + nit->second.backward_stage.out); + } + + /* backward-consumer eligibility */ + for (i = 0; i < ids.size(); i++) + { + std::map::const_iterator cit = mgr->consumer_ok.find(ids[i]); + + appendStringInfo(out, "consumer_ok %d %s\n", ids[i], + (cit != mgr->consumer_ok.end() && cit->second) ? "yes" : "no"); + } +} diff --git a/gpcontrib/robust_predicate_transfer/src/rptregion.cpp b/gpcontrib/robust_predicate_transfer/src/rptregion.cpp new file mode 100644 index 00000000000..28b77218db6 --- /dev/null +++ b/gpcontrib/robust_predicate_transfer/src/rptregion.cpp @@ -0,0 +1,568 @@ +/*------------------------------------------------------------------------- + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * rptregion.cpp + * Extract the RPT+ join graph -- base-scan vertices and equijoin + * edges -- from a finished PlannedStmt. + * + * This pass replaces the RPT+ artifact's TableOperatorManager + * (embryo-labs/dynamic-predicate-transfer): where DuckDB builds its transfer + * graph from the logical operator tree before join ordering, we read the + * finished plan after set_plan_references, so the join clauses are already + * in OUTER_VAR/INNER_VAR form and the key columns must be traced back to + * their base scans. The traversal mirrors anser_inject_walk and the key + * descent and equality validation mirror AnserResolveKeyScan and + * anser_hashjoin_keys (all in gpcontrib/anser/src/anserplan.c), widened as + * noted at each site. + * + * Phase I is analysis only: nothing here touches the plan tree. + * + * IDENTIFICATION + * gpcontrib/robust_predicate_transfer/src/rptregion.cpp + * + *------------------------------------------------------------------------- + */ +extern "C" +{ +#include "postgres.h" + +#include "nodes/parsenodes.h" +#include "nodes/pg_list.h" +#include "parser/parsetree.h" +#include "utils/fmgroids.h" +#include "utils/lsyscache.h" +#include "utils/typcache.h" + +#include "anserplan.h" +} + +#include "rpt.h" + +static bool rpt_is_vertex_scan(const Plan *plan); +static bool rpt_scan_is_filtered(const Plan *plan); +static RptVertexInfo *rpt_vertex_for_scan(PlannedStmt *stmt, + RptTransferMgr *mgr, Plan *scan); +static bool rpt_resolve_key_scan(Plan *top, AttrNumber key_attno, + Plan **scan_out, AttrNumber *attno_out); +static bool rpt_equality_clause(Node *clause, Var **outer_var, + Var **inner_var, Oid *key_type); +static void rpt_add_edge(PlannedStmt *stmt, RptTransferMgr *mgr, + JoinType jointype, + Plan *outer_plan, AttrNumber outer_attno, + Plan *inner_plan, AttrNumber inner_attno, + Oid key_type); +static void rpt_collect_clauses(PlannedStmt *stmt, RptTransferMgr *mgr, + JoinType jointype, Plan *outer, Plan *inner, + List *clauses); +static void rpt_walk(PlannedStmt *stmt, RptTransferMgr *mgr, Plan *plan); + +/* + * True for the scan nodes a reducer pass could replay, the only accepted + * vertices of the join graph. + */ +static bool +rpt_is_vertex_scan(const Plan *plan) +{ + return IsA(plan, SeqScan) || + IsA(plan, IndexScan) || + IsA(plan, IndexOnlyScan) || + IsA(plan, BitmapHeapScan); +} + +/* + * Does the scan carry restriction quals? Index and bitmap scans restrict + * through their index quals as well as through plan.qual. + */ +static bool +rpt_scan_is_filtered(const Plan *plan) +{ + if (plan->qual != NIL) + return true; + + switch (nodeTag(plan)) + { + case T_IndexScan: + return ((IndexScan *) plan)->indexqual != NIL; + case T_IndexOnlyScan: + return ((IndexOnlyScan *) plan)->indexqual != NIL; + case T_BitmapHeapScan: + return ((BitmapHeapScan *) plan)->bitmapqualorig != NIL; + default: + return false; + } +} + +/* + * Find or create the vertex for a base scan. One vertex per rangetable + * index, deduplicated through mgr->by_rti; vertices are created lazily, only + * when a resolved equijoin edge reaches the scan, so a scan no key resolves + * to never becomes a vertex. The classification fields (intermediate, + * unfiltered) are rptgraph.cpp's; join_keys accumulates here as edges are + * recorded. + */ +static RptVertexInfo * +rpt_vertex_for_scan(PlannedStmt *stmt, RptTransferMgr *mgr, Plan *scan) +{ + int rti = ((Scan *) scan)->scanrelid; + std::map::iterator it; + RptVertexInfo v; + + it = mgr->by_rti.find(rti); + if (it != mgr->by_rti.end()) + return &mgr->vertices[it->second]; + + v.id = rti; + v.scan = scan; + v.cardinality = (int64) scan->plan_rows; + v.filtered = rpt_scan_is_filtered(scan); + v.intermediate = false; + v.unfiltered = false; + + if (rti >= 1 && rti <= list_length(stmt->rtable)) + { + RangeTblEntry *rte = rt_fetch(rti, stmt->rtable); + + if (rte->eref != NULL) + v.relname = rte->eref->aliasname; + } + + mgr->by_rti[rti] = (int) mgr->vertices.size(); + mgr->vertices.push_back(v); + return &mgr->vertices.back(); +} + +/* + * Follow one join-key column down to the base scan that originates it. + * Modeled on AnserResolveKeyScan (gpcontrib/anser/src/anserplan.c) but not + * calling it: Anser's resolver stops at SeqScan and descends only Hash, + * Motion, HashJoin and its own nodes, where we accept the index and bitmap + * scans as termini as well and see through MergeJoin, NestLoop, Sort and + * Material too. Where Anser's loop stops at the parent of the scan (it + * needs the parent to relink the scan under a filter node), we only need the + * scan and its key attno, so the terminus test runs on the node itself: the + * descent steps into the scan, and the scan's own targetlist names its + * table's columns with varno == scanrelid after set_plan_references. + * + * False means no single base scan holds the whole key column -- the column + * comes from an Append, an Agg, a set operation, a SubqueryScan, a CTE, or + * anything else not on the descent list. That is a correctness gate, not a + * missed optimization: a filter built on a partial source of the column + * would reject rows that can join, so the caller drops the edge. + */ +static bool +rpt_resolve_key_scan(Plan *top, AttrNumber key_attno, + Plan **scan_out, AttrNumber *attno_out) +{ + Plan *node = top; + AttrNumber attno = key_attno; + + for (;;) + { + TargetEntry *tle; + Var *var; + Plan *child; + + if (node == NULL || + attno < 1 || attno > list_length(node->targetlist)) + return false; + + tle = (TargetEntry *) list_nth(node->targetlist, attno - 1); + if (tle == NULL || !IsA(tle->expr, Var)) + return false; + var = (Var *) tle->expr; + + /* + * Terminus: a base scan the reducer could replay. The key column is + * the Var's attno in the scanned relation. A targetlist Var naming + * anything but the scan's own rangetable entry (a whole-row Var, a + * placeholder) is not a plain column and ends the descent. + */ + if (rpt_is_vertex_scan(node)) + { + Scan *scan = (Scan *) node; + + if (scan->scanrelid == 0 || var->varno != (int) scan->scanrelid) + return false; + *scan_out = node; + *attno_out = var->varattno; + return true; + } + + /* + * Which child supplies this column. As in AnserResolveKeyScan, a + * join is the only node here with two children; unlike it, all three + * join types are descended, not just HashJoin. An Anser filter node + * is a passthrough with an identity targetlist (its Vars carry + * INDEX_VAR into custom_plans), so a scan Anser already filtered + * stays reachable. + */ + if (var->varno == OUTER_VAR) + child = outerPlan(node); + else if (var->varno == INNER_VAR && + (IsA(node, HashJoin) || IsA(node, MergeJoin) || + IsA(node, NestLoop))) + child = innerPlan(node); + else if (var->varno == INDEX_VAR && AnserIsRuntimeFilterScan(node)) + child = (Plan *) linitial(((CustomScan *) node)->custom_plans); + else + return false; + + if (child == NULL) + return false; + + /* + * Anything not on this list is a region boundary: Append, + * MergeAppend, Agg, ProjectSet, SubqueryScan, CteScan, Unique, Limit, + * SetOp, WindowAgg, Result, the dynamic scans, and the rest. The + * walk descends into them to find joins, but a key column never + * crosses them. + */ + if (!rpt_is_vertex_scan(child) && + !IsA(child, Hash) && !IsA(child, Motion) && + !IsA(child, HashJoin) && !IsA(child, MergeJoin) && + !IsA(child, NestLoop) && + !IsA(child, Sort) && !IsA(child, Material) && + !AnserIsRuntimeFilterScan(child)) + return false; + + node = child; + attno = var->varattno; + } +} + +/* + * Validate one equijoin clause and split it into outer/inner Vars. + * + * The shape and the operator validation are exactly anser_hashjoin_keys's + * (gpcontrib/anser/src/anserplan.c), lifted from HashJoin->hashclauses to + * any clause list: a two-argument OpExpr whose RelabelType-stripped args are + * one OUTER_VAR and one INNER_VAR Var, over the same by-value base type on + * both sides, excluding floats, with opno equal to the type's TYPECACHE_EQ_OPR + * equality operator. Anser hashes raw Datum bytes, and the transfer filters + * will too, so SQL equality must coincide with bitwise Datum equality of the + * key; anything looser could prune rows that actually join. A NestLoop + * clause with a Param on either side fails the Var test, as required -- + * such a clause is not a plain col = col join. + * + * Unlike Anser, which accepts only single-clause equijoins, callers pass one + * clause at a time: edge weight is the shared-attribute count, so + * multi-clause joins must be taken apart rather than refused. + */ +static bool +rpt_equality_clause(Node *clause, Var **outer_var, Var **inner_var, + Oid *key_type) +{ + OpExpr *op; + Node *l; + Node *r; + Var *lvar; + Var *rvar; + Oid keytype; + TypeCacheEntry *tce; + + if (clause == NULL || !IsA(clause, OpExpr)) + return false; + op = (OpExpr *) clause; + if (list_length(op->args) != 2) + return false; + + l = (Node *) linitial(op->args); + r = (Node *) lsecond(op->args); + while (l != NULL && IsA(l, RelabelType)) + l = (Node *) ((RelabelType *) l)->arg; + while (r != NULL && IsA(r, RelabelType)) + r = (Node *) ((RelabelType *) r)->arg; + if (l == NULL || r == NULL || !IsA(l, Var) || !IsA(r, Var)) + return false; + + if (((Var *) l)->varno == OUTER_VAR && ((Var *) r)->varno == INNER_VAR) + { + lvar = (Var *) l; + rvar = (Var *) r; + } + else if (((Var *) l)->varno == INNER_VAR && ((Var *) r)->varno == OUTER_VAR) + { + lvar = (Var *) r; + rvar = (Var *) l; + } + else + return false; + *outer_var = lvar; + *inner_var = rvar; + + if ((*inner_var)->vartype != (*outer_var)->vartype) + return false; + + keytype = getBaseType((*inner_var)->vartype); + if (!get_typbyval(keytype)) + return false; + if (keytype == FLOAT4OID || keytype == FLOAT8OID) + return false; + + /* + * The clause has to be the type's own equality rather than just some + * hashable operator spelled "=". See anser_hashjoin_keys for why this + * alone is not the bitwise-equality proof and the float exclusion above + * is still needed. + */ + tce = lookup_type_cache(keytype, TYPECACHE_EQ_OPR); + if (!OidIsValid(tce->eq_opr) || op->opno != tce->eq_opr) + return false; + + *key_type = keytype; + return true; +} + +/* + * Resolve one validated equijoin clause to its two base scans and record the + * edge between their vertices. + * + * left_v/right_v are canonicalized (lower rtindex on the left), not the + * join's outer/inner: every clause of a vertex pair merges into one + * RptEdgeInfo and the neighbor matrix is symmetric, so a second join over + * the same pair with the opposite orientation must land on the same edge + * rather than colliding with its mirror image. The clause's key pair is + * stored in the same canonical order, and the protect flags name the + * protected *vertex*: JOIN_LEFT protects the outer side's vertex (its rows + * survive whether or not they match, so it must never be reduced by the + * other side's keys), JOIN_RIGHT the inner side's, INNER and SEMI neither, + * and every other join type (ANTI, FULL, LASJ_NOTIN, ...) makes no edge at + * all. protect_left therefore means left must NOT be reduced by right's + * keys. + * + * key_type is the validated equality input type of the first clause recorded + * for the edge; the data model carries one type per edge even though + * different clauses of a pair could equate different types. + */ +static void +rpt_add_edge(PlannedStmt *stmt, RptTransferMgr *mgr, JoinType jointype, + Plan *outer_plan, AttrNumber outer_attno, + Plan *inner_plan, AttrNumber inner_attno, Oid key_type) +{ + Plan *outer_scan; + Plan *inner_scan; + AttrNumber outer_key; + AttrNumber inner_key; + int outer_id; + int inner_id; + RptVertexInfo *outer_v; + RptVertexInfo *inner_v; + int left_id; + int right_id; + RptKey left_key; + RptKey right_key; + int protect_id = 0; + RptEdgeInfo *edge; + + /* ANTI, FULL and the GP extras make no usable edge at all. */ + if (jointype != JOIN_INNER && jointype != JOIN_SEMI && + jointype != JOIN_LEFT && jointype != JOIN_RIGHT) + return; + + if (!rpt_resolve_key_scan(outer_plan, outer_attno, + &outer_scan, &outer_key)) + return; + if (!rpt_resolve_key_scan(inner_plan, inner_attno, + &inner_scan, &inner_key)) + return; + + outer_id = ((Scan *) outer_scan)->scanrelid; + inner_id = ((Scan *) inner_scan)->scanrelid; + + /* + * Create both vertices before taking pointers into mgr.vertices: creating + * the second can reallocate the vector the first pointer refers into. + */ + rpt_vertex_for_scan(stmt, mgr, outer_scan); + rpt_vertex_for_scan(stmt, mgr, inner_scan); + outer_v = &mgr->vertices[mgr->by_rti[outer_id]]; + inner_v = &mgr->vertices[mgr->by_rti[inner_id]]; + + /* The preserved side of an outer join is protected from reduction. */ + if (jointype == JOIN_LEFT) + protect_id = outer_id; + else if (jointype == JOIN_RIGHT) + protect_id = inner_id; + + if (outer_id <= inner_id) + { + left_id = outer_id; + left_key.rtindex = outer_id; + left_key.attno = outer_key; + right_id = inner_id; + right_key.rtindex = inner_id; + right_key.attno = inner_key; + } + else + { + left_id = inner_id; + left_key.rtindex = inner_id; + left_key.attno = inner_key; + right_id = outer_id; + right_key.rtindex = outer_id; + right_key.attno = outer_key; + } + + edge = &mgr->neighbor_matrix[left_id][right_id]; + if (edge->clauses.empty()) + { + edge->left_v = left_id; + edge->right_v = right_id; + edge->key_type = key_type; + edge->protect_left = false; + edge->protect_right = false; + } + edge->clauses.push_back(std::make_pair(left_key, right_key)); + if (protect_id == left_id) + edge->protect_left = true; + else if (protect_id == right_id) + edge->protect_right = true; + + outer_v->join_keys.insert(outer_id == left_id ? left_key : right_key); + inner_v->join_keys.insert(inner_id == left_id ? left_key : right_key); + + /* The matrix is symmetric: both directions name the same edge value. */ + mgr->neighbor_matrix[right_id][left_id] = *edge; +} + +/* + * Run one join's clause list through validation, resolution and edge + * recording. Clauses that fail validation or resolution are skipped + * individually; a join contributes an edge only for the clauses that + * survive. + */ +static void +rpt_collect_clauses(PlannedStmt *stmt, RptTransferMgr *mgr, JoinType jointype, + Plan *outer, Plan *inner, List *clauses) +{ + ListCell *lc; + + foreach(lc, clauses) + { + Var *outer_var; + Var *inner_var; + Oid key_type; + + if (!rpt_equality_clause((Node *) lfirst(lc), + &outer_var, &inner_var, &key_type)) + continue; + + rpt_add_edge(stmt, mgr, jointype, + outer, outer_var->varattno, + inner, inner_var->varattno, key_type); + } +} + +/* + * Manual recursive walk, after anser_inject_walk + * (gpcontrib/anser/src/anserplan.c): the outerPlan/innerPlan spine plus the + * child lists anser's walk omits (Append, MergeAppend, Sequence, BitmapAnd, + * BitmapOr, SubqueryScan, CustomScan custom_plans), widened the same way + * anser_max_plan_node_id widens its own. plan->initPlan is skipped on + * purpose: the SubPlan's Plan tree sits in stmt->subplans, which + * RptCollectGraph walks separately. Descending these lists finds joins + * behind region boundaries; the resolver still refuses to cross them, so a + * join inside a UNION arm can link scans of that arm but never out of it. + */ +static void +rpt_walk(PlannedStmt *stmt, RptTransferMgr *mgr, Plan *plan) +{ + ListCell *lc; + + if (plan == NULL) + return; + + switch (nodeTag(plan)) + { + case T_HashJoin: + rpt_collect_clauses(stmt, mgr, ((HashJoin *) plan)->join.jointype, + outerPlan(plan), innerPlan(plan), + ((HashJoin *) plan)->hashclauses); + break; + case T_MergeJoin: + rpt_collect_clauses(stmt, mgr, ((MergeJoin *) plan)->join.jointype, + outerPlan(plan), innerPlan(plan), + ((MergeJoin *) plan)->mergeclauses); + break; + case T_NestLoop: + rpt_collect_clauses(stmt, mgr, ((NestLoop *) plan)->join.jointype, + outerPlan(plan), innerPlan(plan), + ((NestLoop *) plan)->join.joinqual); + break; + default: + break; + } + + rpt_walk(stmt, mgr, outerPlan(plan)); + rpt_walk(stmt, mgr, innerPlan(plan)); + + switch (nodeTag(plan)) + { + case T_Append: + foreach(lc, ((Append *) plan)->appendplans) + rpt_walk(stmt, mgr, (Plan *) lfirst(lc)); + break; + case T_MergeAppend: + foreach(lc, ((MergeAppend *) plan)->mergeplans) + rpt_walk(stmt, mgr, (Plan *) lfirst(lc)); + break; + case T_Sequence: + foreach(lc, ((Sequence *) plan)->subplans) + rpt_walk(stmt, mgr, (Plan *) lfirst(lc)); + break; + case T_BitmapAnd: + foreach(lc, ((BitmapAnd *) plan)->bitmapplans) + rpt_walk(stmt, mgr, (Plan *) lfirst(lc)); + break; + case T_BitmapOr: + foreach(lc, ((BitmapOr *) plan)->bitmapplans) + rpt_walk(stmt, mgr, (Plan *) lfirst(lc)); + break; + case T_SubqueryScan: + rpt_walk(stmt, mgr, ((SubqueryScan *) plan)->subplan); + break; + case T_CustomScan: + foreach(lc, ((CustomScan *) plan)->custom_plans) + rpt_walk(stmt, mgr, (Plan *) lfirst(lc)); + break; + default: + break; + } +} + +/* + * Extract the join graph of a finished statement: walk the main plan tree + * and every subplan, create a vertex for each base scan a resolved equijoin + * edge reaches, and record the edges in the (symmetric) neighbor matrix. + * Only vertices, by_rti and neighbor_matrix are filled here; the + * classification, equivalence classes and the transfer plan itself are + * rptgraph.cpp's and rptplan.cpp's passes over the same manager. + */ +void +RptCollectGraph(PlannedStmt *stmt, RptTransferMgr *mgr) +{ + ListCell *lc; + + if (stmt == NULL || stmt->planTree == NULL) + return; + + rpt_walk(stmt, mgr, stmt->planTree); + foreach(lc, stmt->subplans) + rpt_walk(stmt, mgr, (Plan *) lfirst(lc)); +} diff --git a/licenses/LICENSE-mit-duckdb-rpt.txt b/licenses/LICENSE-mit-duckdb-rpt.txt new file mode 100644 index 00000000000..ddbb7cc497a --- /dev/null +++ b/licenses/LICENSE-mit-duckdb-rpt.txt @@ -0,0 +1,24 @@ +The RPT+ transfer-graph code in gpcontrib/robust_predicate_transfer is ported +from the dynamic-predicate-transfer artifact +(https://github.com/embryo-labs/dynamic-predicate-transfer), which carries the +following license: + +Copyright 2018-2025 Stichting DuckDB Foundation + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. From 79067662971b1edaf926aac9b7dd07481ab74aeb Mon Sep 17 00:00:00 2001 From: Leonid Borchuk Date: Mon, 9 Nov 2026 15:27:09 +0300 Subject: [PATCH 19/27] Fix RPT transfer planning and error handling Preserve protected join directions when bypassing unfiltered tables, terminate rewiring on actual changes, and choose direction-aware parents with separate forward/backward depth tie-breaks. Enforce the chain cap and identify reducer scans and bridge tables. Classify both stages from distribution policies, including implied edges and replicated sources. Keep local backward filters non-blocking and require cross-slice safety for global consumers. Document producer placement and parallel-worker completeness requirements. Unwind STL owners before reporting PostgreSQL errors, fail open in the diagnostic planner hook, and build dumps before one PostgreSQL append. Wire --with-rpt into normal builds, require Anser, restrict diagnostic access, and resolve IndexOnlyScan keys through the index target list. Add synthetic tests with checks active under NDEBUG and strengthen SQL assertions for both optimizers using relation names where needed. Validation: C++ compilation, unitcheck with -O3 -DNDEBUG, an intentional CHECK failure, ASan/UBSan, configure checks, and dump comparison passed. SQL installcheck could not start: linking pg_regress requires libpq, which is unavailable in the local build. Assisted-by: ChatGPT --- configure | 3 + configure.ac | 3 + gpcontrib/robust_predicate_transfer/Makefile | 16 + gpcontrib/robust_predicate_transfer/README.md | 5 + .../robust_predicate_transfer/docs/design.md | 130 +++- .../expected/rpt_graph.out | 214 ++++--- .../expected/rpt_regions.out | 138 ++--- .../robust_predicate_transfer/include/rpt.h | 69 ++- .../robust_predicate_transfer--1.0.sql | 3 + .../sql/rpt_graph.sql | 176 ++++-- .../sql/rpt_regions.sql | 87 ++- .../src/rptgraph.cpp | 88 ++- .../robust_predicate_transfer/src/rptinit.cpp | 149 ++++- .../robust_predicate_transfer/src/rptplan.cpp | 474 +++++++++----- .../src/rptregion.cpp | 31 +- .../test/graph_test.cpp | 576 ++++++++++++++++++ src/Makefile.global.in | 1 + 17 files changed, 1641 insertions(+), 522 deletions(-) create mode 100644 gpcontrib/robust_predicate_transfer/test/graph_test.cpp diff --git a/configure b/configure index f07cd27cea0..686a235d336 100755 --- a/configure +++ b/configure @@ -3661,6 +3661,9 @@ fi +if test "$with_rpt" = yes && test "$with_anser" != yes; then + as_fn_error $? "--with-rpt requires --with-anser" "$LINENO" 5 +fi # # Enable fault injector diff --git a/configure.ac b/configure.ac index 70ed54930c5..9a1414fd1e6 100644 --- a/configure.ac +++ b/configure.ac @@ -247,6 +247,9 @@ AC_SUBST(with_anser) PGAC_ARG_BOOL(with, rpt, no, [build the RPT+ transfer-plan extension (requires --with-anser)]) AC_SUBST(with_rpt) +if test "$with_rpt" = yes && test "$with_anser" != yes; then + AC_MSG_ERROR([--with-rpt requires --with-anser]) +fi # # Enable fault injector diff --git a/gpcontrib/robust_predicate_transfer/Makefile b/gpcontrib/robust_predicate_transfer/Makefile index 52e1b582413..b870dade8e0 100644 --- a/gpcontrib/robust_predicate_transfer/Makefile +++ b/gpcontrib/robust_predicate_transfer/Makefile @@ -58,6 +58,10 @@ include $(top_builddir)/src/Makefile.global include $(top_srcdir)/contrib/contrib-global.mk endif +# Link with the C++ driver so the module owns its STL/exception runtime +# dependency even when the server was built without ORCA. +COMPILER = $(CXX) $(CXXFLAGS) + # The analysis needs anser's library installed and preloaded: our module # resolves AnserIsRuntimeFilterScan from it at load time (RTLD_NOW), and # load order must be anser first. Mirrors gpcontrib/anser's preload @@ -92,6 +96,18 @@ preload-rpt: # cluster, and runs the tests. .PHONY: installcheck-with-install installcheck-with-install: + $(MAKE) unitcheck $(MAKE) -C ../anser install $(MAKE) install $(MAKE) installcheck + +# Uses PostgreSQL headers but no server: synthetic plans exercise the exact +# production graph code and deterministic failure paths. +.PHONY: unitcheck +unitcheck: test/graph_test + ./test/graph_test + +test/graph_test: $(srcdir)/test/graph_test.cpp $(wildcard $(srcdir)/src/*.cpp) $(srcdir)/include/rpt.h + $(CXX) $(CXXFLAGS) $(CPPFLAGS) $(PG_CPPFLAGS) $< -o $@ + +EXTRA_CLEAN += test/graph_test diff --git a/gpcontrib/robust_predicate_transfer/README.md b/gpcontrib/robust_predicate_transfer/README.md index 47201b623ee..ecbe75d7897 100644 --- a/gpcontrib/robust_predicate_transfer/README.md +++ b/gpcontrib/robust_predicate_transfer/README.md @@ -18,3 +18,8 @@ Build: `./configure --with-anser --with-rpt` (needs Anser), then `make -C gpcontrib/robust_predicate_transfer`. Run tests with `make -C gpcontrib/robust_predicate_transfer installcheck` against a demo cluster (it preloads `anser,robust_predicate_transfer` itself). + +Run `make unitcheck` in this directory after configuring the source tree to +exercise synthetic graph, stage, co-location, consumer-order and exception +fixtures without starting a server. `make installcheck` additionally needs a +running Cloudberry cluster and executes SQL cases under both optimizers. diff --git a/gpcontrib/robust_predicate_transfer/docs/design.md b/gpcontrib/robust_predicate_transfer/docs/design.md index 98e908bd208..01cff2be3b2 100644 --- a/gpcontrib/robust_predicate_transfer/docs/design.md +++ b/gpcontrib/robust_predicate_transfer/docs/design.md @@ -65,31 +65,83 @@ edges, block their outgoing edges) are in rptgraph.cpp. ## Asymmetric transfer plan -rptplan.cpp ports the artifact's `LargestRootUpdated` + `FindEdge` + -`CreateTransferPlanUpdated`: a greedy Prim forest (one root per component, -chosen as the largest filtered-or-intermediate table, falling back to the -largest), forward stage assigned smaller→larger along the insertion order, -backward stage redirected through each class's leader — the single vertex per -class the broadcast filter is built on. `rpt.max_chain_depth` caps the -forward chain length once the reducer exists; the default (4) is a placeholder -until hop latency is measured (work item 0 of the phase plan). +rptplan.cpp uses one Algorithm 1 insertion loop for both trees. Frontier +selection prefers the largest shared-column count, then the largest relation; +each stage chooses among edges open in its transfer direction; among its +highest-weight open edges, parents use greatest forward depth and smallest +backward depth, +with vertex id making remaining ties deterministic. Unprotected equivalence +classes expose their implied equalities, so a SQL star and a SQL chain can +produce the same chain within a class. Root selection retains the artifact's +largest filtered-or-intermediate preference, falling back to the largest table. + +The backward stage redirects each class to its earliest-inserted member. A +non-component-root class leader is a **bridge table**, reported in the dump. +Only filtered or bridge tables can produce forward filters. Component roots +are final destinations in the forward graph, but never reducer scans: they +consume their inbound cached filter in the main query. `reducer order` lists +only scans that actually produce forward filters, in dependency order. + +`rpt.max_chain_depth` limits the number of dependency hops, including the +last delivery to the root. When another hop would exceed the cap, that edge +is omitted from both endpoint lists. This loses selectivity, not correctness. +The dump records the cap, reducer order, bridges, and each forward and +backward hop's merge mode. The default (4) remains provisional pending measured transport latency. + +### C/C++ errors and diagnostic access + +STL containers allocate outside PostgreSQL memory contexts. Calls that can +raise PostgreSQL errors go through `RptPgCall`, which catches the longjmp at +the call site and throws a C++ marker. C++ unwinding destroys the containers +before the analysis boundary rethrows the original PostgreSQL error. The +wrapper restores both PostgreSQL exception stacks on either kind of unwind. +C++ allocation and other exceptions are converted to PostgreSQL errors only +after leaving the C++ catch blocks. Both the analysis and SQL entry points +contain C++ exceptions. + +The dump function plans without executing the query's executor permission +checks. Its default PUBLIC execution privilege is revoked; administrators +should grant it only to roles allowed to inspect relation estimates. + +### Scope and validation still outstanding + +This implementation computes schedules and dumps them; it does not inject +filters, dispatch reducers, or add EXPLAIN annotations. No runtime behavior +or performance improvement is claimed. The Phase I latency benchmark and +JOB/TPC-DS co-location coverage report have not been run. Multi-class runtime +backward chaining remains deferred to later phases. Cloudberry MARK-like +subplan forms are not DuckDB MARK joins: unsupported join kinds contribute +no transfer edge. SQL tests run each case with optimizer off and on; +standalone synthetic tests pin graph shape and error cleanup independently +of SQL optimizer transformations. ## Correctness rules Each of these is a silent-wrong-answer rule, not a performance rule. A bloom filter may only ever *over*-approximate the build side's key set. -1. **Per-hop global merge.** Segment *s* holds only part of table *j*'s keys, - so filtering table *j+1* with *s*'s local filter drops rows whose key lives - on another segment. Each hop must be a global publish → merge → deliver, - unless the two tables are provably co-distributed on the join key (same - distkey, same segment count, no redistributing motion between scan and - join). A local hop is an optimization on top, never the default. +1. **Merge where the source keys are complete.** Decide each forward and + backward edge from the base relations' distribution policies and that + edge's key pairs. Equal ordered hash distribution columns, hash opclasses + and segment counts permit a segment-local transfer when every distribution + column is covered by the transfer equality. Main-plan Motions do not change + this proof: the reducer reads the base scans. Implied equivalence-class + and unfiltered-bypass edges use exactly the same policy test as original + join edges, so a co-distributed star remains local after becoming a chain. + A replicated source is complete on every covered segment: it may transfer + locally if its segment count covers the destination's. A replicated scan + may execute on only one segment: Phase II must place the producer on every + consuming segment or classify that broadcast as global; policy coverage + alone does not guarantee producer execution coverage. A transfer into a + replicated destination stays global, including replicated-to-replicated. + Other cases, including unknown policies and random-to-hash transfers, need + global publish → merge → deliver. A local hop never publishes to the QD. 2. **Snapshot identity.** The reducer (Phase III) must run under the main query's snapshot and parameters: a row visible to the query but not to the reducer would be silently missing from the filter. 3. **Outer joins are directed edges.** LEFT protects the left side, RIGHT and - MARK protect the right; ANTI and FULL produce no edge. The preserved side + MARK protect the right in the artifact; unsupported Cloudberry join kinds, + ANTI and FULL produce no edge. The preserved side of an outer join must never be reduced by the nullable side's keys. 4. **Transitive reduction is sound by induction.** Filtering table *t* with filter A before building filter B from *t* is correct because A only @@ -102,33 +154,65 @@ filter may only ever *over*-approximate the build side's key set. non-float type and the operator is that type's own equality (`-0.0 = 0.0` is true but not bitwise equal; cross-type Datums for equal values are not bitwise equal either). -7. **Plan-order safety.** A backward consumer may only wait on a producer the - plan order guarantees runs first: the consumer's scan must sit on the probe - side of a hash join whose build side transitively contains its class - leader's scan. `consumer_ok` in the dump reports exactly this; ineligible - vertices fail open (no backward filter). +7. **Blocking consumers only across slices.** `consumer_ok` describes + eligibility for a blocking Anser consumer on **global** backward edges. + Every global producer must be on a hash join's build side, the consumer + on its probe side, and the scans in different slices; different ancestors + can prove different producers. Same-slice exchanges are refused, matching + Anser. Local edges never participate in this check, and a vertex with only + local in-edges reports `consumer_ok no`: it needs no blocking consumer. + Core's `gp_enable_runtime_filter_pushdown` covers eligible same-process + HashJoin/SeqScan pairs after building the hash table. Phase II should rely + on that path for local backward edges, or use a non-blocking local filter + whose absence means no filtering. A HashJoin can fetch its first probe + tuple before starting the build; waiting there can deadlock even if the + producer is on its build side. A local edge must never become an Anser + exchange merely because its consumer passes the structural hash-join test. 8. **Fail open, always.** Timeout, malformed part, oversized filter, squelched producer, missing cache entry — all degrade to "no filter", never to an - error and never to a filtered result. + error and never to a filtered result. The optional planner-hook analysis + runs in a disposable memory context: analysis failures warn and return the + original finished plan. User cancellation still propagates. The explicit + diagnostic UDF reports errors to its caller rather than masking them. ## Runtime contract with Anser (Phases II/III) - The reducer never joins — scan, filter, build — so it fits one slice, one gang, one dispatch, with the forward chain as an ordered `Append`. -- The backward pass is ordinary Anser injection into the main plan. +- Global backward edges may use Anser in the main plan only where + `consumer_ok` permits it. Local backward edges use core runtime-filter + pushdown (or an optional non-blocking local filter), with no QD channel, + subscription, network round trip or wait. - A session-scoped filter cache per QE, keyed by condition_key alone, carries chain filters across the dispatch boundary and lets the broadcast be delivered once per segment rather than once per consuming scan. - Condition keys derive from the semantic identity `(region_id, class_id, build_rel)`, not a per-query counter. +### Parallel workers and segment completeness + +`merge=local` is a distribution-policy decision, not permission to use a +single parallel worker's partial filter. With parallel scans, each QE worker +reads only part of its segment's keys. Even a replicated source then needs +all workers' parts combined before its filter is complete on that segment. + +Phase III's initial reducer must be **non-parallel**, with one scanning QE +per segment. Supporting parallel reducers requires a segment-local merge of +all worker filters before delivery or probing. Anser's `docs/local-fold.md` +describes such a rendezvous, but it is an idea, not an implemented facility; +Phase III must not assume it exists. Missing or failed worker parts must +abandon the filter and fail open, never expose an incomplete filter. The +same completeness requirement applies to any custom local backward producer. +Core currently disables its pushdown path in MPP parallel mode, so relying +on core there simply means no local filter, rather than a global exchange. + ## GUCs | GUC | Default | Meaning | | --- | --- | --- | | `rpt.enable` | off | Master switch for the analysis hook. | | `rpt.debug` | off | Log the transfer plan of every analyzed query at DEBUG1. | -| `rpt.max_chain_depth` | 4 | Cap on the forward chain length (placeholder until hop latency is measured). | +| `rpt.max_chain_depth` | 4 | Enforced maximum dependency hops; default provisional until hop latency is measured. | Setup: `shared_preload_libraries = 'anser,robust_predicate_transfer'`, in that order (the extension resolves Anser symbols at load time). The analysis UDF diff --git a/gpcontrib/robust_predicate_transfer/expected/rpt_graph.out b/gpcontrib/robust_predicate_transfer/expected/rpt_graph.out index 9559c210afc..36d3316d525 100644 --- a/gpcontrib/robust_predicate_transfer/expected/rpt_graph.out +++ b/gpcontrib/robust_predicate_transfer/expected/rpt_graph.out @@ -7,6 +7,7 @@ CREATE TABLE rpt_a (a int, b int) DISTRIBUTED BY (a); CREATE TABLE rpt_b (a int) DISTRIBUTED BY (a); CREATE TABLE rpt_c (a int) DISTRIBUTED BY (a); CREATE TABLE rpt_d (x int, y int) DISTRIBUTED BY (x); +CREATE TABLE rpt_r (a int) DISTRIBUTED REPLICATED; INSERT INTO rpt_a SELECT i, i FROM generate_series(1, 100) i; INSERT INTO rpt_b SELECT i FROM generate_series(1, 10) i; INSERT INTO rpt_c SELECT i FROM generate_series(1, 50) i; @@ -15,104 +16,117 @@ ANALYZE rpt_a; ANALYZE rpt_b; ANALYZE rpt_c; ANALYZE rpt_d; -SET optimizer = off; --- Three-table chain over one column: one class with all three tables, and the --- filtered table (rpt_a) leads the transfer order. -SELECT rpt_transfer_plan( - 'SELECT * FROM rpt_a a JOIN rpt_b b ON a.a = b.a JOIN rpt_c c ON b.a = c.a WHERE a.b > 50') - LIKE 'transfer order: 1%' AS root_is_filtered_table; - root_is_filtered_table ------------------------- - t -(1 row) - -SELECT rpt_transfer_plan( - 'SELECT * FROM rpt_a a JOIN rpt_b b ON a.a = b.a JOIN rpt_c c ON b.a = c.a WHERE a.b > 50') - LIKE '%members=1,2,3 leader=1%' AS one_class_all_members; - one_class_all_members ------------------------ - t -(1 row) - --- Star: rpt_a joins three others on one column -- one class, root rpt_a. -SELECT rpt_transfer_plan( - 'SELECT * FROM rpt_a a JOIN rpt_b b ON a.a = b.a JOIN rpt_c c ON a.a = c.a WHERE a.b > 50') - LIKE '%members=1,2,3 leader=1%' AS star_one_class; - star_one_class ----------------- - t -(1 row) - --- Cyclic triangle: still produces a plan (cycles cost completeness, not --- correctness), one class, all three members. -SELECT rpt_transfer_plan( - 'SELECT * FROM rpt_a a JOIN rpt_b b ON a.a = b.a JOIN rpt_c c ON b.a = c.a AND a.a = c.a WHERE a.b > 50') - LIKE '%members=1,2,3%' AS cyclic_one_class; - cyclic_one_class ------------------- - t -(1 row) - --- Multi-clause join: two clauses on one vertex pair merge into one edge; the --- keys land in two classes (one per column pair). -SELECT rpt_transfer_plan( - 'SELECT * FROM rpt_a a JOIN rpt_d d ON a.a = d.x AND a.b = d.y WHERE a.b > 50') - LIKE '%members=1,2%' AS compound_members; - compound_members ------------------- - t -(1 row) - --- LEFT JOIN: the preserved (outer) side must never be reduced by the --- nullable side's keys, so the backward filter points at rpt_a, never away --- from it. -SELECT strpos(rpt_transfer_plan( - 'SELECT * FROM rpt_a a LEFT JOIN rpt_b b ON a.a = b.a WHERE a.b > 50'), - 'consumer_ok 1 no') > 0 AS left_join_preserved_side_safe; - left_join_preserved_side_safe -------------------------------- - t -(1 row) - --- FULL JOIN produces no usable edge at all: no region. -SELECT rpt_transfer_plan( - 'SELECT * FROM rpt_a a FULL JOIN rpt_b b ON a.a = b.a WHERE a.b > 50') - = 'no transfer regions -' AS full_join_no_region; - full_join_no_region ---------------------- - t -(1 row) - --- SEMI join (IN): unprotected both ways, one class, both members. -SELECT rpt_transfer_plan( - 'SELECT * FROM rpt_a a WHERE a.b > 50 AND a.a IN (SELECT a FROM rpt_b)') - LIKE '%members=1,2%' AS semi_join_members; - semi_join_members -------------------- - t -(1 row) - --- Cross-type and float keys are refused (bitwise Datum equality rule). -SELECT rpt_transfer_plan( - 'SELECT * FROM rpt_a a JOIN (SELECT a::bigint aa FROM rpt_b) s ON a.a = s.aa') - = 'no transfer regions -' AS cross_type_no_region; - cross_type_no_region ----------------------- - t -(1 row) - --- The same star through ORCA: the class partition must come out identically. -SET optimizer = on; -SELECT rpt_transfer_plan( - 'SELECT * FROM rpt_a a JOIN rpt_b b ON a.a = b.a JOIN rpt_c c ON a.a = c.a WHERE a.b > 50') - LIKE '%members=1,2,3 leader=1%' AS star_one_class_orca; - star_one_class_orca ---------------------- - t -(1 row) - -RESET optimizer; -DROP TABLE rpt_a, rpt_b, rpt_c, rpt_d; +INSERT INTO rpt_r SELECT i FROM generate_series(1, 10) i; +ANALYZE rpt_r; +DO $$ +DECLARE + opt text; + dump text; + q text; + distributed_id text; + replicated_id text; + preserved_name text; + preserved_id text; + expected_members text; + member_count int; +BEGIN + IF EXISTS (SELECT FROM pg_proc p, LATERAL aclexplode(p.proacl) acl + WHERE p.oid = 'rpt_transfer_plan(text)'::regprocedure + AND acl.grantee = 0 AND acl.privilege_type = 'EXECUTE') THEN + RAISE EXCEPTION 'diagnostic function is executable by PUBLIC'; + END IF; + FOREACH opt IN ARRAY ARRAY['off', 'on'] LOOP + PERFORM set_config('optimizer', opt, true); + FOREACH q IN ARRAY ARRAY[ + 'SELECT * FROM rpt_a JOIN rpt_b ON rpt_a.a = rpt_b.a JOIN rpt_c ON rpt_b.a = rpt_c.a WHERE rpt_a.b > 50', + 'SELECT * FROM rpt_a JOIN rpt_b ON rpt_a.a = rpt_b.a JOIN rpt_c ON rpt_a.a = rpt_c.a WHERE rpt_a.b > 50', + 'SELECT * FROM rpt_a JOIN rpt_b ON rpt_a.a = rpt_b.a JOIN rpt_c ON rpt_b.a = rpt_c.a AND rpt_a.a = rpt_c.a WHERE rpt_a.b > 50' + ] LOOP + dump := rpt_transfer_plan(q); + distributed_id := substring(dump FROM 'vertex ([0-9]+) rpt_a rows='); + SELECT count(*), string_agg(parts[1], ',' ORDER BY parts[1]::int) + INTO member_count, expected_members + FROM regexp_matches(dump, 'vertex ([0-9]+) (rpt_a|rpt_b|rpt_c) rows=', 'g') AS vertices(parts); + IF distributed_id IS NULL OR member_count <> 3 OR + dump NOT LIKE ('%members=' || expected_members || ' leader=' || distributed_id || chr(10) || '%') THEN + RAISE EXCEPTION 'class/root mismatch (optimizer=%): %', opt, dump; + END IF; + IF dump NOT LIKE ('%reducer order:' || chr(10) || '%') THEN + RAISE EXCEPTION 'unfiltered tables entered reducer: %', dump; + END IF; + END LOOP; + -- A co-distributed star must stay local after Algorithm 1 creates + -- implied dimension-to-dimension hops, in both transfer stages. + dump := rpt_transfer_plan('SELECT * FROM rpt_a a JOIN rpt_b b ON a.a = b.a JOIN rpt_c c ON a.a = c.a JOIN rpt_d d ON a.a = d.x WHERE a.b > 0 AND b.a > 0 AND c.a > 0 AND d.y > 0'); + IF dump NOT LIKE '%forward hop%' OR dump NOT LIKE '%backward hop%' OR + dump LIKE '%merge=global%' OR + EXISTS (SELECT FROM regexp_split_to_table(dump, chr(10)) line + WHERE line LIKE 'consumer_ok % yes') THEN + RAISE EXCEPTION 'local star uses global/blocking filters (optimizer=%): %', opt, dump; + END IF; + -- Replicated -> hash is local; the reverse broadcast stays global. + dump := rpt_transfer_plan('SELECT * FROM rpt_a JOIN rpt_r ON rpt_a.a = rpt_r.a WHERE rpt_a.b > 0 AND rpt_r.a > 0'); + -- Resolve table names from vertex records: ORCA can reorder rtindexes. + distributed_id := substring(dump FROM 'vertex ([0-9]+) rpt_a rows='); + replicated_id := substring(dump FROM 'vertex ([0-9]+) rpt_r rows='); + IF distributed_id IS NULL OR replicated_id IS NULL OR + dump NOT LIKE ('%forward hop ' || replicated_id || '->' || distributed_id || ' merge=local%') OR + dump NOT LIKE ('%backward hop ' || distributed_id || '->' || replicated_id || ' merge=global%') THEN + RAISE EXCEPTION 'replicated direction mismatch (optimizer=%): %', opt, dump; + END IF; + dump := rpt_transfer_plan('SELECT * FROM rpt_a JOIN rpt_d ON rpt_a.a = rpt_d.x AND rpt_a.b = rpt_d.y WHERE rpt_a.b > 50'); + SELECT count(*), string_agg(parts[1], ',' ORDER BY parts[1]::int) + INTO member_count, expected_members + FROM regexp_matches(dump, 'vertex ([0-9]+) (rpt_a|rpt_d) rows=', 'g') AS vertices(parts); + IF member_count <> 2 OR + (SELECT count(*) FROM regexp_split_to_table(dump, chr(10)) line + WHERE line LIKE ('class %: members=' || expected_members || ' leader=%')) <> 2 THEN + RAISE EXCEPTION 'expected two compound-key classes: %', dump; + END IF; + FOR q, preserved_name IN + SELECT * FROM (VALUES + ('SELECT * FROM rpt_a LEFT JOIN rpt_b ON rpt_a.a = rpt_b.a WHERE rpt_a.b > 50', 'rpt_a'), + ('SELECT * FROM rpt_b RIGHT JOIN rpt_a ON rpt_a.a = rpt_b.a WHERE rpt_a.b > 50', 'rpt_a'), + ('SELECT * FROM rpt_c LEFT JOIN (rpt_b JOIN rpt_a ON rpt_a.a = rpt_b.a AND rpt_b.a > 0) ON rpt_c.a = rpt_a.a WHERE rpt_c.a > 1', 'rpt_c') + ) AS cases(query, preserved) + LOOP + dump := rpt_transfer_plan(q); + preserved_id := substring(dump FROM ('vertex ([0-9]+) ' || preserved_name || ' rows=')); + IF preserved_id IS NULL THEN + RAISE EXCEPTION 'missing preserved table % (optimizer=%): %', preserved_name, opt, dump; + END IF; + IF EXISTS (SELECT FROM regexp_split_to_table(dump, chr(10)) line + WHERE line LIKE ('vertex ' || preserved_id || ' %') + AND line LIKE '%apply from%') THEN + RAISE EXCEPTION 'preserved side reduced: %', dump; + END IF; + -- The first LEFT JOIN must filter the nullable side, so an + -- entirely empty transfer plan cannot satisfy this safety test. + IF q LIKE 'SELECT * FROM rpt_a LEFT JOIN%' AND + NOT EXISTS (SELECT FROM regexp_split_to_table(dump, chr(10)) line + WHERE line LIKE 'vertex %' AND line LIKE '%apply from%') THEN + RAISE EXCEPTION 'LEFT JOIN produced no filter applications (optimizer=%): %', opt, dump; + END IF; + END LOOP; + FOREACH q IN ARRAY ARRAY[ + 'SELECT * FROM rpt_a a FULL JOIN rpt_b b ON a.a = b.a', + 'SELECT a.a IN (SELECT b.a FROM rpt_b b) FROM rpt_a a', + 'SELECT * FROM rpt_a a WHERE NOT EXISTS (SELECT FROM rpt_b b WHERE a.a = b.a)', + 'SELECT * FROM rpt_a a JOIN (SELECT a::bigint aa FROM rpt_b) s ON a.a = s.aa' + ] LOOP + IF rpt_transfer_plan(q) <> ('no transfer regions' || chr(10)) THEN + RAISE EXCEPTION 'unsupported join/key accepted (optimizer=%): %', opt, q; + END IF; + END LOOP; + dump := rpt_transfer_plan('SELECT * FROM rpt_a WHERE rpt_a.b > 50 AND rpt_a.a IN (SELECT a FROM rpt_b)'); + SELECT count(*), string_agg(parts[1], ',' ORDER BY parts[1]::int) + INTO member_count, expected_members + FROM regexp_matches(dump, 'vertex ([0-9]+) (rpt_a|rpt_b) rows=', 'g') AS vertices(parts); + IF member_count <> 2 OR dump NOT LIKE ('%members=' || expected_members || ' leader=%') THEN + RAISE EXCEPTION 'missing SEMI class: %', dump; + END IF; + END LOOP; +END +$$; +DROP TABLE rpt_a, rpt_b, rpt_c, rpt_d, rpt_r; DROP EXTENSION robust_predicate_transfer; diff --git a/gpcontrib/robust_predicate_transfer/expected/rpt_regions.out b/gpcontrib/robust_predicate_transfer/expected/rpt_regions.out index 1a50a71f754..57912fd02df 100644 --- a/gpcontrib/robust_predicate_transfer/expected/rpt_regions.out +++ b/gpcontrib/robust_predicate_transfer/expected/rpt_regions.out @@ -1,6 +1,6 @@ -- Region discovery: transfer regions are connected components of the join -- graph over base scans, and every operator the key resolver cannot descend --- (aggregate, set op, window, limit, SRF/UNNEST, materialized CTE, partitioned +-- (aggregate, set op, window, limit, SRF/UNNEST, CteScan, partitioned -- table) is a boundary -- queries whose only join key comes through one have -- no region at all. CREATE EXTENSION robust_predicate_transfer; @@ -18,87 +18,61 @@ CREATE TABLE rpt_p (a int) DISTRIBUTED BY (a) PARTITION BY RANGE (a) (START (1) END (101) EVERY (50)); INSERT INTO rpt_p SELECT i FROM generate_series(1, 100) i; ANALYZE rpt_p; --- No relations at all. -SELECT rpt_transfer_plan('SELECT 1'); - rpt_transfer_plan -------------------- - no transfer regions - -(1 row) - --- A plain join: one region, both vertices unfiltered. -SELECT rpt_transfer_plan('SELECT * FROM rpt_b JOIN rpt_a ON rpt_b.a = rpt_a.a'); - rpt_transfer_plan ------------------------ - transfer order: 2 1 - vertex 1 rpt_b rows=10 unfiltered - vertex 2 rpt_a rows=100 unfiltered - class 1.1: members=1,2 leader=2 - vertex 1 forward: none - vertex 1 backward: none - vertex 2 forward: none - vertex 2 backward: none - consumer_ok 1 no - consumer_ok 2 no - -(1 row) - --- Join key produced by GROUP BY: SubqueryScan boundary, no region. -SELECT rpt_transfer_plan('SELECT * FROM rpt_a a JOIN (SELECT a, count(*) c FROM rpt_b GROUP BY a) s ON a.a = s.a'); - rpt_transfer_plan -------------------- - no transfer regions - -(1 row) - --- Join key through UNION: Append boundary, no region. -SELECT rpt_transfer_plan('SELECT * FROM rpt_a a JOIN (SELECT a FROM rpt_b UNION SELECT a FROM rpt_c) u ON a.a = u.a'); - rpt_transfer_plan -------------------- - no transfer regions - -(1 row) - --- Join key through a window function: boundary, no region. -SELECT rpt_transfer_plan('SELECT * FROM rpt_a a JOIN (SELECT a, row_number() OVER () r FROM rpt_b) w ON a.a = w.a'); - rpt_transfer_plan -------------------- - no transfer regions - -(1 row) - --- Join key through LIMIT: boundary, no region. -SELECT rpt_transfer_plan('SELECT * FROM rpt_a a JOIN (SELECT a FROM rpt_b ORDER BY a LIMIT 5) l ON a.a = l.a'); - rpt_transfer_plan -------------------- - no transfer regions - -(1 row) - --- Join key produced by a set-returning function (UNNEST): ProjectSet boundary. -SELECT rpt_transfer_plan('SELECT * FROM rpt_a a JOIN (SELECT u FROM rpt_b, unnest(ARRAY[a]) u) s ON a.a = s.u'); - rpt_transfer_plan -------------------- - no transfer regions - -(1 row) - --- Join key through a materialized CTE: CteScan boundary. -SELECT rpt_transfer_plan('WITH w AS MATERIALIZED (SELECT a FROM rpt_b) SELECT * FROM rpt_a a JOIN w ON a.a = w.a'); - rpt_transfer_plan -------------------- - no transfer regions - -(1 row) - --- Join against a partitioned table: Append boundary. -SELECT rpt_transfer_plan('SELECT * FROM rpt_a a JOIN rpt_p p ON a.a = p.a'); - rpt_transfer_plan -------------------- - no transfer regions - -(1 row) - +DO $$ +DECLARE + opt text; + q text; + dump text; + expected_members text; + member_count int; +BEGIN + FOREACH opt IN ARRAY ARRAY['off', 'on'] LOOP + PERFORM set_config('optimizer', opt, true); + FOREACH q IN ARRAY ARRAY[ + 'SELECT 1', + 'SELECT * FROM rpt_a a JOIN (SELECT a, count(*) c FROM rpt_b GROUP BY a) s ON a.a = s.a WHERE s.c > 1', + 'SELECT * FROM rpt_a a JOIN (SELECT a FROM rpt_b UNION SELECT a FROM rpt_c) u ON a.a = u.a', + 'SELECT * FROM rpt_a a JOIN (SELECT a, row_number() OVER () r FROM rpt_b) w ON a.a = w.a WHERE w.r > 1', + 'SELECT * FROM rpt_a a JOIN (SELECT a FROM rpt_b ORDER BY a LIMIT 5) l ON a.a = l.a', + 'SELECT * FROM rpt_a a JOIN (SELECT u FROM rpt_b, unnest(ARRAY[a]) u) s ON a.a = s.u', + 'SELECT * FROM rpt_a a JOIN rpt_p p ON a.a = p.a' + ] LOOP + dump := rpt_transfer_plan(q); + IF dump <> ('no transfer regions' || chr(10)) THEN + RAISE EXCEPTION 'region boundary crossed (optimizer=%): %: %', opt, q, dump; + END IF; + END LOOP; + -- PostgreSQL retains a CteScan boundary here. ORCA exposes the + -- underlying base scans, so its unfiltered class is expected. + q := 'WITH w AS MATERIALIZED (SELECT a FROM rpt_b) SELECT * FROM rpt_a a JOIN w ON a.a = w.a'; + dump := rpt_transfer_plan(q); + IF opt = 'off' THEN + IF dump <> ('no transfer regions' || chr(10)) THEN + RAISE EXCEPTION 'CteScan boundary crossed: %', dump; + END IF; + ELSE + SELECT count(*), string_agg(parts[1], ',' ORDER BY parts[1]::int) + INTO member_count, expected_members + FROM regexp_matches(dump, 'vertex ([0-9]+) (a|rpt_b) rows=', 'g') AS vertices(parts); + IF member_count <> 2 OR + dump NOT LIKE ('%members=' || expected_members || ' leader=%') OR + dump NOT LIKE ('%reducer order:' || chr(10) || '%') OR + dump LIKE '%build for%' THEN + RAISE EXCEPTION 'unexpected ORCA materialized CTE plan: %', dump; + END IF; + END IF; + dump := rpt_transfer_plan('SELECT * FROM rpt_b JOIN rpt_a ON rpt_b.a = rpt_a.a'); + SELECT count(*), string_agg(parts[1], ',' ORDER BY parts[1]::int) + INTO member_count, expected_members + FROM regexp_matches(dump, 'vertex ([0-9]+) (rpt_a|rpt_b) rows=', 'g') AS vertices(parts); + IF member_count <> 2 OR dump NOT LIKE ('%members=' || expected_members || ' leader=%') OR + dump NOT LIKE ('%reducer order:' || chr(10) || '%') OR + dump LIKE '%build for%' THEN + RAISE EXCEPTION 'unfiltered pair produced transfers: %', dump; + END IF; + END LOOP; +END +$$; DROP TABLE rpt_p; DROP TABLE rpt_a, rpt_b, rpt_c; DROP EXTENSION robust_predicate_transfer; diff --git a/gpcontrib/robust_predicate_transfer/include/rpt.h b/gpcontrib/robust_predicate_transfer/include/rpt.h index 545cf477e8f..fe79965a3ce 100644 --- a/gpcontrib/robust_predicate_transfer/include/rpt.h +++ b/gpcontrib/robust_predicate_transfer/include/rpt.h @@ -32,9 +32,9 @@ * rule) the single-node original never had. * * Phase I is a pure analysis pass: nothing is injected or executed, and the - * dumps in rptplan.cpp are the only output. Everything lives in transient - * C++ containers in the caller's memory context; nothing may rely on a - * destructor running across an elog(ERROR) longjmp. + * dumps in rptplan.cpp are the only output. STL containers own malloc-backed + * storage. PostgreSQL errors must be translated to C++ exceptions before + * unwinding those containers, then rethrown at the C boundary. * * IDENTIFICATION * gpcontrib/robust_predicate_transfer/include/rpt.h @@ -58,6 +58,44 @@ extern "C" #include #include +/* + * Like gpopt's GP_WRAP: never longjmp over a live C++ container. Only call + * PostgreSQL through this boundary; callbacks must do the same for their + * own PostgreSQL calls. The guard also restores PG's stacks when a callback + * throws a C++ exception through a C walker. + */ +struct RptPgError {}; + +struct RptPgStackGuard +{ + sigjmp_buf *exception_stack; + ErrorContextCallback *context_stack; + + RptPgStackGuard() : exception_stack(PG_exception_stack), + context_stack(error_context_stack) {} + ~RptPgStackGuard() + { + PG_exception_stack = exception_stack; + error_context_stack = context_stack; + } +}; + +template +auto RptPgCall(const F &call) -> decltype(call()) +{ + RptPgStackGuard guard; + sigjmp_buf local; + int saved_dispatch_nest_level = dispatch_nest_level; + + if (sigsetjmp(local, 0) != 0) + { + dispatch_nest_level = saved_dispatch_nest_level; + throw RptPgError(); + } + PG_exception_stack = &local; + return call(); +} + /* GUCs (defined in rptinit.cpp). */ extern bool rpt_enable; extern bool rpt_debug; @@ -102,6 +140,10 @@ struct RptVertexInfo bool filtered; /* the scan carries restriction quals */ bool intermediate; /* touches >1 equivalence class */ bool unfiltered; /* neither filtered nor intermediate */ + bool replicated = false; + int numsegments = 0; + std::vector distkeys; + std::vector distopclasses; std::set join_keys; /* this table's keys over all its edges */ }; @@ -111,10 +153,10 @@ struct RptVertexInfo * clauses.size() is the shared-attribute count the paper uses as the edge * weight; each clause is one (left key, right key) pair. * - * protect_left/protect_right are ported verbatim: the preserved side of an + * protect_left/protect_right name the protected destination: the preserved side of an * outer join keeps its rows whether or not they match, so it must never be - * reduced by the other side's keys. LEFT protects left, RIGHT and MARK - * protect right; ANTI and FULL never create an edge at all. + * reduced by the other side's keys. LEFT protects left and RIGHT + * protects right; unsupported join kinds, ANTI and FULL create no edge. */ struct RptEdgeInfo { @@ -147,6 +189,7 @@ struct RptGraphEdge std::vector left_keys; /* keys on the smaller table's side */ std::vector right_keys; /* keys on the larger table's side */ std::vector key_types; + bool local = false; /* policy permits local merge; never use QD transport */ }; /* Ports dag.hpp's GraphNode: one vertex's full transfer plan. */ @@ -181,18 +224,24 @@ struct RptTransferMgr /* rptplan.cpp: the transfer plan. */ std::map graph; + std::set roots; + std::set bridges; + std::vector reducer_order; std::vector transfer_order; /* Prim insertion order of vertex ids */ - /* Our addition: backward-consumer eligibility (Anser's deadlock rule) -- - * consumer_ok[v] is true only where v's scan sits on the probe side of a - * hash join whose build side transitively contains the vertex's class - * leader's scan. */ + /* Eligibility for blocking global backward consumers only. Every global + * producer must be on a hash join's build side in a different slice from + * the probe-side consumer. Local edges never wait and do not participate; + * a vertex with no global in-edges has consumer_ok=false. */ std::map consumer_ok; }; /* rptregion.cpp: extract the join graph from the finished statement. */ extern void RptCollectGraph(PlannedStmt *stmt, RptTransferMgr *mgr); +extern RptKey RptFindKeyRoot(const RptKey &key, + std::map &uf_parent); + /* rptgraph.cpp: union-find classes, classification, SkipUnfilteredTable. */ extern void RptBuildClasses(RptTransferMgr *mgr); diff --git a/gpcontrib/robust_predicate_transfer/robust_predicate_transfer--1.0.sql b/gpcontrib/robust_predicate_transfer/robust_predicate_transfer--1.0.sql index f6e5ac72f8a..caf6c091678 100644 --- a/gpcontrib/robust_predicate_transfer/robust_predicate_transfer--1.0.sql +++ b/gpcontrib/robust_predicate_transfer/robust_predicate_transfer--1.0.sql @@ -7,3 +7,6 @@ CREATE FUNCTION rpt_transfer_plan(query text) RETURNS text AS 'MODULE_PATHNAME' LANGUAGE C STRICT; + +-- Planning exposes estimates without executing the query permission checks. +REVOKE ALL ON FUNCTION rpt_transfer_plan(text) FROM PUBLIC; diff --git a/gpcontrib/robust_predicate_transfer/sql/rpt_graph.sql b/gpcontrib/robust_predicate_transfer/sql/rpt_graph.sql index bc738249233..36d3316d525 100644 --- a/gpcontrib/robust_predicate_transfer/sql/rpt_graph.sql +++ b/gpcontrib/robust_predicate_transfer/sql/rpt_graph.sql @@ -3,11 +3,11 @@ -- properties of the dump (LIKE/strpos), so the test does not depend on -- estimate printouts or tie-breaking cosmetics. CREATE EXTENSION robust_predicate_transfer; - CREATE TABLE rpt_a (a int, b int) DISTRIBUTED BY (a); CREATE TABLE rpt_b (a int) DISTRIBUTED BY (a); CREATE TABLE rpt_c (a int) DISTRIBUTED BY (a); CREATE TABLE rpt_d (x int, y int) DISTRIBUTED BY (x); +CREATE TABLE rpt_r (a int) DISTRIBUTED REPLICATED; INSERT INTO rpt_a SELECT i, i FROM generate_series(1, 100) i; INSERT INTO rpt_b SELECT i FROM generate_series(1, 10) i; INSERT INTO rpt_c SELECT i FROM generate_series(1, 50) i; @@ -16,65 +16,117 @@ ANALYZE rpt_a; ANALYZE rpt_b; ANALYZE rpt_c; ANALYZE rpt_d; - -SET optimizer = off; - --- Three-table chain over one column: one class with all three tables, and the --- filtered table (rpt_a) leads the transfer order. -SELECT rpt_transfer_plan( - 'SELECT * FROM rpt_a a JOIN rpt_b b ON a.a = b.a JOIN rpt_c c ON b.a = c.a WHERE a.b > 50') - LIKE 'transfer order: 1%' AS root_is_filtered_table; -SELECT rpt_transfer_plan( - 'SELECT * FROM rpt_a a JOIN rpt_b b ON a.a = b.a JOIN rpt_c c ON b.a = c.a WHERE a.b > 50') - LIKE '%members=1,2,3 leader=1%' AS one_class_all_members; - --- Star: rpt_a joins three others on one column -- one class, root rpt_a. -SELECT rpt_transfer_plan( - 'SELECT * FROM rpt_a a JOIN rpt_b b ON a.a = b.a JOIN rpt_c c ON a.a = c.a WHERE a.b > 50') - LIKE '%members=1,2,3 leader=1%' AS star_one_class; - --- Cyclic triangle: still produces a plan (cycles cost completeness, not --- correctness), one class, all three members. -SELECT rpt_transfer_plan( - 'SELECT * FROM rpt_a a JOIN rpt_b b ON a.a = b.a JOIN rpt_c c ON b.a = c.a AND a.a = c.a WHERE a.b > 50') - LIKE '%members=1,2,3%' AS cyclic_one_class; - --- Multi-clause join: two clauses on one vertex pair merge into one edge; the --- keys land in two classes (one per column pair). -SELECT rpt_transfer_plan( - 'SELECT * FROM rpt_a a JOIN rpt_d d ON a.a = d.x AND a.b = d.y WHERE a.b > 50') - LIKE '%members=1,2%' AS compound_members; - --- LEFT JOIN: the preserved (outer) side must never be reduced by the --- nullable side's keys, so the backward filter points at rpt_a, never away --- from it. -SELECT strpos(rpt_transfer_plan( - 'SELECT * FROM rpt_a a LEFT JOIN rpt_b b ON a.a = b.a WHERE a.b > 50'), - 'consumer_ok 1 no') > 0 AS left_join_preserved_side_safe; - --- FULL JOIN produces no usable edge at all: no region. -SELECT rpt_transfer_plan( - 'SELECT * FROM rpt_a a FULL JOIN rpt_b b ON a.a = b.a WHERE a.b > 50') - = 'no transfer regions -' AS full_join_no_region; - --- SEMI join (IN): unprotected both ways, one class, both members. -SELECT rpt_transfer_plan( - 'SELECT * FROM rpt_a a WHERE a.b > 50 AND a.a IN (SELECT a FROM rpt_b)') - LIKE '%members=1,2%' AS semi_join_members; - --- Cross-type and float keys are refused (bitwise Datum equality rule). -SELECT rpt_transfer_plan( - 'SELECT * FROM rpt_a a JOIN (SELECT a::bigint aa FROM rpt_b) s ON a.a = s.aa') - = 'no transfer regions -' AS cross_type_no_region; - --- The same star through ORCA: the class partition must come out identically. -SET optimizer = on; -SELECT rpt_transfer_plan( - 'SELECT * FROM rpt_a a JOIN rpt_b b ON a.a = b.a JOIN rpt_c c ON a.a = c.a WHERE a.b > 50') - LIKE '%members=1,2,3 leader=1%' AS star_one_class_orca; -RESET optimizer; - -DROP TABLE rpt_a, rpt_b, rpt_c, rpt_d; +INSERT INTO rpt_r SELECT i FROM generate_series(1, 10) i; +ANALYZE rpt_r; +DO $$ +DECLARE + opt text; + dump text; + q text; + distributed_id text; + replicated_id text; + preserved_name text; + preserved_id text; + expected_members text; + member_count int; +BEGIN + IF EXISTS (SELECT FROM pg_proc p, LATERAL aclexplode(p.proacl) acl + WHERE p.oid = 'rpt_transfer_plan(text)'::regprocedure + AND acl.grantee = 0 AND acl.privilege_type = 'EXECUTE') THEN + RAISE EXCEPTION 'diagnostic function is executable by PUBLIC'; + END IF; + FOREACH opt IN ARRAY ARRAY['off', 'on'] LOOP + PERFORM set_config('optimizer', opt, true); + FOREACH q IN ARRAY ARRAY[ + 'SELECT * FROM rpt_a JOIN rpt_b ON rpt_a.a = rpt_b.a JOIN rpt_c ON rpt_b.a = rpt_c.a WHERE rpt_a.b > 50', + 'SELECT * FROM rpt_a JOIN rpt_b ON rpt_a.a = rpt_b.a JOIN rpt_c ON rpt_a.a = rpt_c.a WHERE rpt_a.b > 50', + 'SELECT * FROM rpt_a JOIN rpt_b ON rpt_a.a = rpt_b.a JOIN rpt_c ON rpt_b.a = rpt_c.a AND rpt_a.a = rpt_c.a WHERE rpt_a.b > 50' + ] LOOP + dump := rpt_transfer_plan(q); + distributed_id := substring(dump FROM 'vertex ([0-9]+) rpt_a rows='); + SELECT count(*), string_agg(parts[1], ',' ORDER BY parts[1]::int) + INTO member_count, expected_members + FROM regexp_matches(dump, 'vertex ([0-9]+) (rpt_a|rpt_b|rpt_c) rows=', 'g') AS vertices(parts); + IF distributed_id IS NULL OR member_count <> 3 OR + dump NOT LIKE ('%members=' || expected_members || ' leader=' || distributed_id || chr(10) || '%') THEN + RAISE EXCEPTION 'class/root mismatch (optimizer=%): %', opt, dump; + END IF; + IF dump NOT LIKE ('%reducer order:' || chr(10) || '%') THEN + RAISE EXCEPTION 'unfiltered tables entered reducer: %', dump; + END IF; + END LOOP; + -- A co-distributed star must stay local after Algorithm 1 creates + -- implied dimension-to-dimension hops, in both transfer stages. + dump := rpt_transfer_plan('SELECT * FROM rpt_a a JOIN rpt_b b ON a.a = b.a JOIN rpt_c c ON a.a = c.a JOIN rpt_d d ON a.a = d.x WHERE a.b > 0 AND b.a > 0 AND c.a > 0 AND d.y > 0'); + IF dump NOT LIKE '%forward hop%' OR dump NOT LIKE '%backward hop%' OR + dump LIKE '%merge=global%' OR + EXISTS (SELECT FROM regexp_split_to_table(dump, chr(10)) line + WHERE line LIKE 'consumer_ok % yes') THEN + RAISE EXCEPTION 'local star uses global/blocking filters (optimizer=%): %', opt, dump; + END IF; + -- Replicated -> hash is local; the reverse broadcast stays global. + dump := rpt_transfer_plan('SELECT * FROM rpt_a JOIN rpt_r ON rpt_a.a = rpt_r.a WHERE rpt_a.b > 0 AND rpt_r.a > 0'); + -- Resolve table names from vertex records: ORCA can reorder rtindexes. + distributed_id := substring(dump FROM 'vertex ([0-9]+) rpt_a rows='); + replicated_id := substring(dump FROM 'vertex ([0-9]+) rpt_r rows='); + IF distributed_id IS NULL OR replicated_id IS NULL OR + dump NOT LIKE ('%forward hop ' || replicated_id || '->' || distributed_id || ' merge=local%') OR + dump NOT LIKE ('%backward hop ' || distributed_id || '->' || replicated_id || ' merge=global%') THEN + RAISE EXCEPTION 'replicated direction mismatch (optimizer=%): %', opt, dump; + END IF; + dump := rpt_transfer_plan('SELECT * FROM rpt_a JOIN rpt_d ON rpt_a.a = rpt_d.x AND rpt_a.b = rpt_d.y WHERE rpt_a.b > 50'); + SELECT count(*), string_agg(parts[1], ',' ORDER BY parts[1]::int) + INTO member_count, expected_members + FROM regexp_matches(dump, 'vertex ([0-9]+) (rpt_a|rpt_d) rows=', 'g') AS vertices(parts); + IF member_count <> 2 OR + (SELECT count(*) FROM regexp_split_to_table(dump, chr(10)) line + WHERE line LIKE ('class %: members=' || expected_members || ' leader=%')) <> 2 THEN + RAISE EXCEPTION 'expected two compound-key classes: %', dump; + END IF; + FOR q, preserved_name IN + SELECT * FROM (VALUES + ('SELECT * FROM rpt_a LEFT JOIN rpt_b ON rpt_a.a = rpt_b.a WHERE rpt_a.b > 50', 'rpt_a'), + ('SELECT * FROM rpt_b RIGHT JOIN rpt_a ON rpt_a.a = rpt_b.a WHERE rpt_a.b > 50', 'rpt_a'), + ('SELECT * FROM rpt_c LEFT JOIN (rpt_b JOIN rpt_a ON rpt_a.a = rpt_b.a AND rpt_b.a > 0) ON rpt_c.a = rpt_a.a WHERE rpt_c.a > 1', 'rpt_c') + ) AS cases(query, preserved) + LOOP + dump := rpt_transfer_plan(q); + preserved_id := substring(dump FROM ('vertex ([0-9]+) ' || preserved_name || ' rows=')); + IF preserved_id IS NULL THEN + RAISE EXCEPTION 'missing preserved table % (optimizer=%): %', preserved_name, opt, dump; + END IF; + IF EXISTS (SELECT FROM regexp_split_to_table(dump, chr(10)) line + WHERE line LIKE ('vertex ' || preserved_id || ' %') + AND line LIKE '%apply from%') THEN + RAISE EXCEPTION 'preserved side reduced: %', dump; + END IF; + -- The first LEFT JOIN must filter the nullable side, so an + -- entirely empty transfer plan cannot satisfy this safety test. + IF q LIKE 'SELECT * FROM rpt_a LEFT JOIN%' AND + NOT EXISTS (SELECT FROM regexp_split_to_table(dump, chr(10)) line + WHERE line LIKE 'vertex %' AND line LIKE '%apply from%') THEN + RAISE EXCEPTION 'LEFT JOIN produced no filter applications (optimizer=%): %', opt, dump; + END IF; + END LOOP; + FOREACH q IN ARRAY ARRAY[ + 'SELECT * FROM rpt_a a FULL JOIN rpt_b b ON a.a = b.a', + 'SELECT a.a IN (SELECT b.a FROM rpt_b b) FROM rpt_a a', + 'SELECT * FROM rpt_a a WHERE NOT EXISTS (SELECT FROM rpt_b b WHERE a.a = b.a)', + 'SELECT * FROM rpt_a a JOIN (SELECT a::bigint aa FROM rpt_b) s ON a.a = s.aa' + ] LOOP + IF rpt_transfer_plan(q) <> ('no transfer regions' || chr(10)) THEN + RAISE EXCEPTION 'unsupported join/key accepted (optimizer=%): %', opt, q; + END IF; + END LOOP; + dump := rpt_transfer_plan('SELECT * FROM rpt_a WHERE rpt_a.b > 50 AND rpt_a.a IN (SELECT a FROM rpt_b)'); + SELECT count(*), string_agg(parts[1], ',' ORDER BY parts[1]::int) + INTO member_count, expected_members + FROM regexp_matches(dump, 'vertex ([0-9]+) (rpt_a|rpt_b) rows=', 'g') AS vertices(parts); + IF member_count <> 2 OR dump NOT LIKE ('%members=' || expected_members || ' leader=%') THEN + RAISE EXCEPTION 'missing SEMI class: %', dump; + END IF; + END LOOP; +END +$$; +DROP TABLE rpt_a, rpt_b, rpt_c, rpt_d, rpt_r; DROP EXTENSION robust_predicate_transfer; diff --git a/gpcontrib/robust_predicate_transfer/sql/rpt_regions.sql b/gpcontrib/robust_predicate_transfer/sql/rpt_regions.sql index 72f1847c4fe..57912fd02df 100644 --- a/gpcontrib/robust_predicate_transfer/sql/rpt_regions.sql +++ b/gpcontrib/robust_predicate_transfer/sql/rpt_regions.sql @@ -1,10 +1,9 @@ -- Region discovery: transfer regions are connected components of the join -- graph over base scans, and every operator the key resolver cannot descend --- (aggregate, set op, window, limit, SRF/UNNEST, materialized CTE, partitioned +-- (aggregate, set op, window, limit, SRF/UNNEST, CteScan, partitioned -- table) is a boundary -- queries whose only join key comes through one have -- no region at all. CREATE EXTENSION robust_predicate_transfer; - CREATE TABLE rpt_a (a int, b int) DISTRIBUTED BY (a); CREATE TABLE rpt_b (a int) DISTRIBUTED BY (a); CREATE TABLE rpt_c (a int) DISTRIBUTED BY (a); @@ -14,40 +13,66 @@ INSERT INTO rpt_c SELECT i FROM generate_series(1, 50) i; ANALYZE rpt_a; ANALYZE rpt_b; ANALYZE rpt_c; - -- A partitioned table joins through an Append, which resolution cannot cross. CREATE TABLE rpt_p (a int) DISTRIBUTED BY (a) PARTITION BY RANGE (a) (START (1) END (101) EVERY (50)); INSERT INTO rpt_p SELECT i FROM generate_series(1, 100) i; ANALYZE rpt_p; - --- No relations at all. -SELECT rpt_transfer_plan('SELECT 1'); - --- A plain join: one region, both vertices unfiltered. -SELECT rpt_transfer_plan('SELECT * FROM rpt_b JOIN rpt_a ON rpt_b.a = rpt_a.a'); - --- Join key produced by GROUP BY: SubqueryScan boundary, no region. -SELECT rpt_transfer_plan('SELECT * FROM rpt_a a JOIN (SELECT a, count(*) c FROM rpt_b GROUP BY a) s ON a.a = s.a'); - --- Join key through UNION: Append boundary, no region. -SELECT rpt_transfer_plan('SELECT * FROM rpt_a a JOIN (SELECT a FROM rpt_b UNION SELECT a FROM rpt_c) u ON a.a = u.a'); - --- Join key through a window function: boundary, no region. -SELECT rpt_transfer_plan('SELECT * FROM rpt_a a JOIN (SELECT a, row_number() OVER () r FROM rpt_b) w ON a.a = w.a'); - --- Join key through LIMIT: boundary, no region. -SELECT rpt_transfer_plan('SELECT * FROM rpt_a a JOIN (SELECT a FROM rpt_b ORDER BY a LIMIT 5) l ON a.a = l.a'); - --- Join key produced by a set-returning function (UNNEST): ProjectSet boundary. -SELECT rpt_transfer_plan('SELECT * FROM rpt_a a JOIN (SELECT u FROM rpt_b, unnest(ARRAY[a]) u) s ON a.a = s.u'); - --- Join key through a materialized CTE: CteScan boundary. -SELECT rpt_transfer_plan('WITH w AS MATERIALIZED (SELECT a FROM rpt_b) SELECT * FROM rpt_a a JOIN w ON a.a = w.a'); - --- Join against a partitioned table: Append boundary. -SELECT rpt_transfer_plan('SELECT * FROM rpt_a a JOIN rpt_p p ON a.a = p.a'); - +DO $$ +DECLARE + opt text; + q text; + dump text; + expected_members text; + member_count int; +BEGIN + FOREACH opt IN ARRAY ARRAY['off', 'on'] LOOP + PERFORM set_config('optimizer', opt, true); + FOREACH q IN ARRAY ARRAY[ + 'SELECT 1', + 'SELECT * FROM rpt_a a JOIN (SELECT a, count(*) c FROM rpt_b GROUP BY a) s ON a.a = s.a WHERE s.c > 1', + 'SELECT * FROM rpt_a a JOIN (SELECT a FROM rpt_b UNION SELECT a FROM rpt_c) u ON a.a = u.a', + 'SELECT * FROM rpt_a a JOIN (SELECT a, row_number() OVER () r FROM rpt_b) w ON a.a = w.a WHERE w.r > 1', + 'SELECT * FROM rpt_a a JOIN (SELECT a FROM rpt_b ORDER BY a LIMIT 5) l ON a.a = l.a', + 'SELECT * FROM rpt_a a JOIN (SELECT u FROM rpt_b, unnest(ARRAY[a]) u) s ON a.a = s.u', + 'SELECT * FROM rpt_a a JOIN rpt_p p ON a.a = p.a' + ] LOOP + dump := rpt_transfer_plan(q); + IF dump <> ('no transfer regions' || chr(10)) THEN + RAISE EXCEPTION 'region boundary crossed (optimizer=%): %: %', opt, q, dump; + END IF; + END LOOP; + -- PostgreSQL retains a CteScan boundary here. ORCA exposes the + -- underlying base scans, so its unfiltered class is expected. + q := 'WITH w AS MATERIALIZED (SELECT a FROM rpt_b) SELECT * FROM rpt_a a JOIN w ON a.a = w.a'; + dump := rpt_transfer_plan(q); + IF opt = 'off' THEN + IF dump <> ('no transfer regions' || chr(10)) THEN + RAISE EXCEPTION 'CteScan boundary crossed: %', dump; + END IF; + ELSE + SELECT count(*), string_agg(parts[1], ',' ORDER BY parts[1]::int) + INTO member_count, expected_members + FROM regexp_matches(dump, 'vertex ([0-9]+) (a|rpt_b) rows=', 'g') AS vertices(parts); + IF member_count <> 2 OR + dump NOT LIKE ('%members=' || expected_members || ' leader=%') OR + dump NOT LIKE ('%reducer order:' || chr(10) || '%') OR + dump LIKE '%build for%' THEN + RAISE EXCEPTION 'unexpected ORCA materialized CTE plan: %', dump; + END IF; + END IF; + dump := rpt_transfer_plan('SELECT * FROM rpt_b JOIN rpt_a ON rpt_b.a = rpt_a.a'); + SELECT count(*), string_agg(parts[1], ',' ORDER BY parts[1]::int) + INTO member_count, expected_members + FROM regexp_matches(dump, 'vertex ([0-9]+) (rpt_a|rpt_b) rows=', 'g') AS vertices(parts); + IF member_count <> 2 OR dump NOT LIKE ('%members=' || expected_members || ' leader=%') OR + dump NOT LIKE ('%reducer order:' || chr(10) || '%') OR + dump LIKE '%build for%' THEN + RAISE EXCEPTION 'unfiltered pair produced transfers: %', dump; + END IF; + END LOOP; +END +$$; DROP TABLE rpt_p; DROP TABLE rpt_a, rpt_b, rpt_c; DROP EXTENSION robust_predicate_transfer; diff --git a/gpcontrib/robust_predicate_transfer/src/rptgraph.cpp b/gpcontrib/robust_predicate_transfer/src/rptgraph.cpp index 5fd2a01f6f8..602f6d7606a 100644 --- a/gpcontrib/robust_predicate_transfer/src/rptgraph.cpp +++ b/gpcontrib/robust_predicate_transfer/src/rptgraph.cpp @@ -77,8 +77,8 @@ struct RptEdgeClause * FindBindingRoot (transfer_graph_manager.cpp:12-23), with path * compression. */ -static RptKey -rpt_find_key_root(const RptKey &key, std::map &uf_parent) +RptKey +RptFindKeyRoot(const RptKey &key, std::map &uf_parent) { std::map::iterator it = uf_parent.find(key); RptKey root; @@ -89,7 +89,7 @@ rpt_find_key_root(const RptKey &key, std::map &uf_parent) root = it->second; if (!(root == key)) { - root = rpt_find_key_root(root, uf_parent); + root = RptFindKeyRoot(root, uf_parent); uf_parent[key] = root; /* path compression */ } return root; @@ -106,8 +106,8 @@ static void rpt_union_keys(const RptKey &a, const RptKey &b, Oid key_type, RptTransferMgr *mgr) { - RptKey root_a = rpt_find_key_root(a, mgr->uf_parent); - RptKey root_b = rpt_find_key_root(b, mgr->uf_parent); + RptKey root_a = RptFindKeyRoot(a, mgr->uf_parent); + RptKey root_b = RptFindKeyRoot(b, mgr->uf_parent); std::map::iterator ga; std::map::iterator gb; @@ -180,7 +180,7 @@ rpt_classify_tables(RptTransferMgr *mgr) { if (mgr->uf_parent.find(*kit) != mgr->uf_parent.end() || mgr->groups.find(*kit) != mgr->groups.end()) - belong_groups.insert(rpt_find_key_root(*kit, mgr->uf_parent)); + belong_groups.insert(RptFindKeyRoot(*kit, mgr->uf_parent)); else belong_groups.insert(*kit); /* never unioned: a group of its own */ } @@ -260,13 +260,14 @@ rpt_merge_clauses(RptEdgeInfo *old, const RptEdgeInfo &other, bool reversed) * artifact shares one EdgeInfo between both directions, so every write here * is mirrored explicitly. */ -static void +static bool rpt_upsert_edge(RptTransferMgr *mgr, const RptEdgeInfo &ne) { int i = ne.left_v; int j = ne.right_v; std::map::iterator ij; bool exists = false; + bool changed = false; ij = mgr->neighbor_matrix[i].find(j); if (ij != mgr->neighbor_matrix[i].end()) @@ -277,6 +278,8 @@ rpt_upsert_edge(RptTransferMgr *mgr, const RptEdgeInfo &ne) * the vertex pair, so the same vertex pair in either orientation is * the same edge and the clauses merge instead. */ + RptEdgeInfo before = ij->second; + bool same = (ij->second.left_v == ne.left_v && ij->second.right_v == ne.right_v); bool rev = (ij->second.left_v == ne.right_v && @@ -284,26 +287,33 @@ rpt_upsert_edge(RptTransferMgr *mgr, const RptEdgeInfo &ne) if (same) { - ij->second.protect_left &= ne.protect_left; - ij->second.protect_right &= ne.protect_right; + ij->second.protect_left |= ne.protect_left; + ij->second.protect_right |= ne.protect_right; rpt_merge_clauses(&ij->second, ne, false); exists = true; } else if (rev) { - ij->second.protect_left &= ne.protect_right; - ij->second.protect_right &= ne.protect_left; + ij->second.protect_left |= ne.protect_right; + ij->second.protect_right |= ne.protect_left; rpt_merge_clauses(&ij->second, ne, true); exists = true; } if (exists) + { + changed = before.protect_left != ij->second.protect_left || + before.protect_right != ij->second.protect_right || + before.clauses.size() != ij->second.clauses.size(); mgr->neighbor_matrix[j][i] = ij->second; + } } if (!exists) { mgr->neighbor_matrix[i][j] = ne; mgr->neighbor_matrix[j][i] = ne; + changed = true; } + return changed; } /* @@ -355,7 +365,8 @@ rpt_skip_unfiltered(RptTransferMgr *mgr) RptEdgeInfo *e = &pit->second; size_t c; - if (!rpt_can_flow(e, pit->first, t)) + if (mgr->vertices[mgr->by_rti[pit->first]].unfiltered || + !rpt_can_flow(e, pit->first, t)) continue; for (c = 0; c < e->clauses.size(); c++) { @@ -422,21 +433,13 @@ rpt_skip_unfiltered(RptTransferMgr *mgr) ne.clauses.push_back(std::make_pair(rpt_key_opposite(in_e, rep.clause, t), out_e->clauses[oc].first)); - /* - * disable reverse flow (L → R) -- kept verbatim - * from the artifact; with src on the left this - * blocks the carrier → destination direction, - * where the t-is-left case above blocks the - * reverse. - */ - ne.protect_right = true; + ne.protect_left = true; /* disable dst -> src */ } if (src_v == dst_v) continue; /* a self-edge no later pass can observe */ - rpt_upsert_edge(mgr, ne); - changed = true; + changed = rpt_upsert_edge(mgr, ne) || changed; } } } @@ -590,6 +593,47 @@ RptBuildClasses(RptTransferMgr *mgr) } } + /* Equality is transitive within an unprotected class. Make its implicit + * edges explicit so Algorithm 1 can choose deepest/shallowest parents + * even when the SQL spelled a star rather than a clique. Never merge + * through a protected outer-join edge. */ + for (const auto &group : mgr->groups) + { + std::vector keys; + for (const auto &vertex : mgr->vertices) + for (const auto &key : vertex.join_keys) + if (RptFindKeyRoot(key, mgr->uf_parent) == group.first) + keys.push_back(key); + for (size_t i = 0; i < keys.size(); ++i) + for (size_t j = i + 1; j < keys.size(); ++j) + { + if (keys[i].rtindex == keys[j].rtindex) + continue; + RptEdgeInfo edge; + edge.left_v = keys[i].rtindex; + edge.right_v = keys[j].rtindex; + edge.key_type = group.second.key_type; + edge.protect_left = edge.protect_right = false; + edge.clauses.push_back(std::make_pair(keys[i], keys[j])); + const auto &existing_row = mgr->neighbor_matrix.find(edge.left_v); + if (existing_row != mgr->neighbor_matrix.end()) + { + const auto &existing = existing_row->second.find(edge.right_v); + if (existing != existing_row->second.end()) + { + bool reversed = existing->second.left_v != edge.left_v; + auto clause = reversed ? std::make_pair(keys[j], keys[i]) : + std::make_pair(keys[i], keys[j]); + if (std::find(existing->second.clauses.begin(), + existing->second.clauses.end(), clause) != + existing->second.clauses.end()) + continue; + } + } + rpt_upsert_edge(mgr, edge); + } + } + rpt_classify_tables(mgr); rpt_skip_unfiltered(mgr); } diff --git a/gpcontrib/robust_predicate_transfer/src/rptinit.cpp b/gpcontrib/robust_predicate_transfer/src/rptinit.cpp index 25f4894632a..2a6775daaf3 100644 --- a/gpcontrib/robust_predicate_transfer/src/rptinit.cpp +++ b/gpcontrib/robust_predicate_transfer/src/rptinit.cpp @@ -48,6 +48,7 @@ extern "C" #include "tcop/tcopprot.h" #include "utils/builtins.h" #include "utils/guc.h" +#include "utils/memutils.h" PG_MODULE_MAGIC; @@ -58,6 +59,9 @@ void _PG_init(void); #include "rpt.h" +#include +#include + bool rpt_enable = false; bool rpt_debug = false; int rpt_max_chain_depth = 4; @@ -69,26 +73,52 @@ static PlannedStmt *rpt_planner(Query *parse, const char *query_string, int cursorOptions, ParamListInfo boundParams, OptimizerOptions *optimizer_options); -/* - * Run the Phase I analysis over a finished statement and return the dump. - * The manager is plain C++ in the caller's memory context; on any error the - * whole context goes away with the statement. - */ +/* Run analysis with C++ unwinding before reporting an error to C callers. */ static void rpt_analyze(PlannedStmt *stmt, StringInfo out) { - RptTransferMgr mgr; + bool pg_error = false; + bool out_of_memory = false; + char message[256] = "unknown C++ exception"; - RptCollectGraph(stmt, &mgr); - if (mgr.vertices.empty()) + try { - appendStringInfoString(out, "no transfer regions\n"); + RptTransferMgr mgr; + + RptCollectGraph(stmt, &mgr); + if (mgr.vertices.empty()) + RptPgCall([&]() { appendStringInfoString(out, "no transfer regions\n"); }); + else + { + RptBuildClasses(&mgr); + RptBuildTransferPlan(stmt, &mgr); + RptDumpTransferPlan(&mgr, out); + } return; } + catch (const RptPgError &) + { + pg_error = true; + } + catch (const std::bad_alloc &) + { + out_of_memory = true; + } + catch (const std::exception &e) + { + strlcpy(message, e.what(), sizeof(message)); + } + catch (...) + { + } - RptBuildClasses(&mgr); - RptBuildTransferPlan(stmt, &mgr); - RptDumpTransferPlan(&mgr, out); + /* No live C++ objects (including exception objects) at these longjmps. */ + if (pg_error) + PG_RE_THROW(); + if (out_of_memory) + ereport(ERROR, (errcode(ERRCODE_OUT_OF_MEMORY), + errmsg("out of memory during RPT analysis"))); + ereport(ERROR, (errmsg("RPT analysis failed: %s", message))); } void @@ -138,7 +168,7 @@ rpt_define_gucs(void) DefineCustomIntVariable("rpt.max_chain_depth", "Sets the maximum forward-chain length RPT+ may plan.", - "Every chain hop is a global publish/merge/deliver barrier in MPP, so the chain depth is capped; the default is a placeholder until hop latency is measured.", + "Global hops require publish/merge/deliver barriers, so chain depth is capped; the default is a placeholder until hop latency is measured.", &rpt_max_chain_depth, 4, 1, INT_MAX, PGC_USERSET, @@ -146,6 +176,51 @@ rpt_define_gucs(void) NULL, NULL, NULL); } +/* The diagnostic hook must not turn an optional analysis failure into a + * failed user query. No C++ owners live across this PostgreSQL catch: the + * analysis boundary has already unwound them before rethrowing the error. */ +static void +rpt_log_analysis(PlannedStmt *stmt) +{ + MemoryContext saved_context = CurrentMemoryContext; + MemoryContext volatile analysis_context = NULL; + + PG_TRY(); + { + analysis_context = AllocSetContextCreate(saved_context, + "RPT analysis", + ALLOCSET_DEFAULT_SIZES); + MemoryContextSwitchTo(analysis_context); + StringInfoData buf; + + initStringInfo(&buf); + rpt_analyze(stmt, &buf); + elog(DEBUG1, "rpt transfer plan:\n%s", buf.data); + MemoryContextSwitchTo(saved_context); + } + PG_CATCH(); + { + MemoryContextSwitchTo(saved_context); + ErrorData *error = CopyErrorData(); + + FlushErrorState(); + if (analysis_context != NULL) + { + MemoryContextDelete(analysis_context); + analysis_context = NULL; + } + /* An optional pass must still honor the user's cancellation. */ + if (error->sqlerrcode == ERRCODE_QUERY_CANCELED) + ReThrowError(error); + ereport(WARNING, (errmsg("RPT analysis skipped: %s", error->message))); + FreeErrorData(error); + } + PG_END_TRY(); + + if (analysis_context != NULL) + MemoryContextDelete(analysis_context); +} + /* * Plan as usual, then run the analysis over the finished tree. Chaining * prev first means that when Anser is preloaded before us its injection pass @@ -169,13 +244,7 @@ rpt_planner(Query *parse, const char *query_string, int cursorOptions, Gp_role == GP_ROLE_DISPATCH && result != NULL && result->commandType == CMD_SELECT && result->planTree != NULL) - { - StringInfoData buf; - - initStringInfo(&buf); - rpt_analyze(result, &buf); - elog(DEBUG1, "rpt transfer plan:\n%s", buf.data); - } + rpt_log_analysis(result); return result; } @@ -186,8 +255,8 @@ rpt_planner(Query *parse, const char *query_string, int cursorOptions, * Plan the query and dump its RPT+ transfer plan. Analysis only: the plan * is discarded afterwards. This is the regression-test driver. */ -Datum -rpt_transfer_plan(PG_FUNCTION_ARGS) +static Datum +rpt_transfer_plan_impl(FunctionCallInfo fcinfo) { char *query_string = text_to_cstring(PG_GETARG_TEXT_PP(0)); List *raw_parsetree_list; @@ -228,3 +297,39 @@ rpt_transfer_plan(PG_FUNCTION_ARGS) PG_RETURN_TEXT_P(cstring_to_text(buf.data)); } + +/* The SQL entry point also contains exceptions raised by future UDF code. */ +Datum +rpt_transfer_plan(PG_FUNCTION_ARGS) +{ + bool pg_error = false; + bool out_of_memory = false; + char message[256] = "unknown C++ exception"; + + try + { + return rpt_transfer_plan_impl(fcinfo); + } + catch (const RptPgError &) + { + pg_error = true; + } + catch (const std::bad_alloc &) + { + out_of_memory = true; + } + catch (const std::exception &e) + { + strlcpy(message, e.what(), sizeof(message)); + } + catch (...) + { + } + if (pg_error) + PG_RE_THROW(); + if (out_of_memory) + ereport(ERROR, (errcode(ERRCODE_OUT_OF_MEMORY), + errmsg("out of memory while planning RPT query"))); + ereport(ERROR, (errmsg("RPT query planning failed: %s", message))); + PG_RETURN_NULL(); +} diff --git a/gpcontrib/robust_predicate_transfer/src/rptplan.cpp b/gpcontrib/robust_predicate_transfer/src/rptplan.cpp index 6a80270da77..99a488fb5f9 100644 --- a/gpcontrib/robust_predicate_transfer/src/rptplan.cpp +++ b/gpcontrib/robust_predicate_transfer/src/rptplan.cpp @@ -62,29 +62,7 @@ extern "C" #include "rpt.h" #include - -/* - * Find the representative key of a key's equivalence class, with path - * compression. A file-local copy of rptgraph.cpp's rpt_find_key_root - * (FindBindingRoot, transfer_graph_manager.cpp:12-23). - */ -static RptKey -rpt_find_key_root(const RptKey &key, std::map &uf_parent) -{ - std::map::iterator it = uf_parent.find(key); - RptKey root; - - if (it == uf_parent.end()) - return key; - - root = it->second; - if (!(root == key)) - { - root = rpt_find_key_root(root, uf_parent); - uf_parent[key] = root; /* path compression */ - } - return root; -} +#include /* * GraphNode::Add (dag.cpp:9-19): find-or-create the edge to `other` in the @@ -137,7 +115,7 @@ rpt_register_leader(RptTransferMgr *mgr, int vertex_id) for (kit = v.join_keys.begin(); kit != v.join_keys.end(); ++kit) { - RptKey rep = rpt_find_key_root(*kit, mgr->uf_parent); + RptKey rep = RptFindKeyRoot(*kit, mgr->uf_parent); std::map::iterator git = mgr->groups.find(rep); if (git != mgr->groups.end() && git->second.leader_id == -1) @@ -149,51 +127,84 @@ rpt_register_leader(RptTransferMgr *mgr, int vertex_id) } /* - * FindEdge (transfer_graph_manager.cpp:736-759): the frontier edge to the - * unconstructed vertex with the largest cardinality, preferring edges that - * are unprotected both ways. + * Algorithm 1: choose the maximum-weight frontier vertex, break ties by + * cardinality, then select deepest forward / shallowest backward parents. + * Fully protected edges never participate. */ static bool -rpt_find_edge(const RptTransferMgr *mgr, const std::set &constructed_set, - const std::set &unconstructed_set, int *from_id, int *to_id) +rpt_find_edge(const RptTransferMgr *mgr, const std::set &constructed, + const std::set &unconstructed, + const std::map &forward_depth, + const std::map &backward_depth, + int *forward_from, int *backward_from, int *to) { - int64 max_cardinality = 0; - bool is_indirected = false; - bool found = false; - std::set::const_iterator ii; + size_t best_weight = 0; + int64 best_rows = -1; + bool found = false; - for (ii = unconstructed_set.begin(); ii != unconstructed_set.end(); ++ii) + /* Algorithm 1: select the frontier vertex, then each tree's parent. */ + for (int candidate : unconstructed) { - std::set::const_iterator ji; - - for (ji = constructed_set.begin(); ji != constructed_set.end(); ++ji) + for (int parent : constructed) { - std::map >::const_iterator row; - std::map::const_iterator eit; - const RptEdgeInfo *edge; - int64 cardinality; - - row = mgr->neighbor_matrix.find(*ji); + const auto &row = mgr->neighbor_matrix.find(parent); if (row == mgr->neighbor_matrix.end()) continue; - eit = row->second.find(*ii); - if (eit == row->second.end()) + const auto &eit = row->second.find(candidate); + if (eit == row->second.end() || + (eit->second.protect_left && eit->second.protect_right)) continue; - edge = &eit->second; + size_t weight = eit->second.clauses.size(); + int64 rows = mgr->vertices[mgr->by_rti.find(candidate)->second].cardinality; - cardinality = mgr->vertices[mgr->by_rti.find(*ii)->second].cardinality; - if (cardinality > max_cardinality || - (!is_indirected && !edge->protect_left && !edge->protect_right)) + if (!found || weight > best_weight || + (weight == best_weight && rows > best_rows)) { - max_cardinality = cardinality; - *from_id = *ji; - *to_id = *ii; found = true; - is_indirected = !edge->protect_left && !edge->protect_right; + best_weight = weight; + best_rows = rows; + *to = candidate; } } } - return found; + if (!found) + return false; + + *forward_from = *backward_from = -1; + size_t forward_weight = 0; + size_t backward_weight = 0; + for (int parent : constructed) + { + const auto &row = mgr->neighbor_matrix.find(parent); + if (row == mgr->neighbor_matrix.end()) + continue; + const auto &eit = row->second.find(*to); + if (eit == row->second.end() || + (eit->second.protect_left && eit->second.protect_right)) + continue; + const RptEdgeInfo &edge = eit->second; + size_t weight = edge.clauses.size(); + bool forward_open = edge.left_v == *to ? !edge.protect_right : !edge.protect_left; + bool backward_open = edge.left_v == parent ? !edge.protect_right : !edge.protect_left; + + /* A protected edge is not a parent candidate for that stage. Prefer + * its best open alternative, even if it has lower weight. */ + if (forward_open && (*forward_from == -1 || weight > forward_weight || + (weight == forward_weight && + forward_depth.at(parent) > forward_depth.at(*forward_from)))) + { + *forward_from = parent; + forward_weight = weight; + } + if (backward_open && (*backward_from == -1 || weight > backward_weight || + (weight == backward_weight && + backward_depth.at(parent) < backward_depth.at(*backward_from)))) + { + *backward_from = parent; + backward_weight = weight; + } + } + return true; } /* @@ -209,9 +220,12 @@ rpt_find_edge(const RptTransferMgr *mgr, const std::set &constructed_set, static void rpt_largest_root_updated(RptTransferMgr *mgr, std::set &remaining, const std::vector &sorted_ids, - std::vector > &selected_edges, + std::vector > &forward_edges, + std::vector > &backward_edges, int32 &next_order) { + std::map forward_depth; + std::map backward_depth; std::set constructed_set; std::set unconstructed_set; std::vector::const_reverse_iterator rit; @@ -257,6 +271,8 @@ rpt_largest_root_updated(RptTransferMgr *mgr, std::set &remaining, } /* Add root */ + mgr->roots.insert(root); + forward_depth[root] = backward_depth[root] = 0; mgr->graph[root].cardinality_order = next_order++; mgr->transfer_order.push_back(root); rpt_register_leader(mgr, root); @@ -265,14 +281,21 @@ rpt_largest_root_updated(RptTransferMgr *mgr, std::set &remaining, /* Build graph */ while (!unconstructed_set.empty()) { - int from_id; + int forward_from; + int backward_from; int to_id; if (!rpt_find_edge(mgr, constructed_set, unconstructed_set, - &from_id, &to_id)) + forward_depth, backward_depth, + &forward_from, &backward_from, &to_id)) break; - selected_edges.push_back(std::make_pair(from_id, to_id)); + if (forward_from != -1) + forward_edges.push_back(std::make_pair(forward_from, to_id)); + if (backward_from != -1) + backward_edges.push_back(std::make_pair(backward_from, to_id)); + forward_depth[to_id] = forward_from == -1 ? 0 : forward_depth[forward_from] + 1; + backward_depth[to_id] = backward_from == -1 ? 0 : backward_depth[backward_from] + 1; mgr->graph[to_id].cardinality_order = next_order++; mgr->transfer_order.push_back(to_id); rpt_register_leader(mgr, to_id); @@ -298,7 +321,8 @@ rpt_largest_root_updated(RptTransferMgr *mgr, std::set &remaining, */ static void rpt_create_transfer_plan(RptTransferMgr *mgr, - const std::vector > &selected_edges) + const std::vector > &selected_edges, + bool forward) { size_t i; @@ -341,7 +365,9 @@ rpt_create_transfer_plan(RptTransferMgr *mgr, } /* forward: from the smaller to the larger */ - if (!protect_right) + if (forward && !protect_right && !mgr->roots.count(left_node->id) && + (mgr->vertices[mgr->by_rti[left_node->id]].filtered || + mgr->bridges.count(left_node->id))) { rpt_node_add_keys(left_node, right_node->id, left_keys, right_keys, key_types, true, false); @@ -350,7 +376,7 @@ rpt_create_transfer_plan(RptTransferMgr *mgr, } /* backward: from the larger to the smaller */ - if (!protect_left) + if (!forward && !protect_left) { /* * Group-leader redirection (transfer_graph_manager.cpp:719-732): @@ -365,7 +391,7 @@ rpt_create_transfer_plan(RptTransferMgr *mgr, for (c = 0; c < left_keys.size(); c++) { - RptKey rep = rpt_find_key_root(right_keys[c], mgr->uf_parent); + RptKey rep = RptFindKeyRoot(right_keys[c], mgr->uf_parent); std::map::iterator git = mgr->groups.find(rep); int dest; RptKey dest_key; @@ -404,15 +430,78 @@ rpt_create_transfer_plan(RptTransferMgr *mgr, } } +/* Locality is a property of the base scans' distribution policies, including + * for implied and bypass edges. A Motion elsewhere in the main plan does not + * change where the reducer reads a key. Parallel workers still need a + * segment-local fold (or a non-parallel reducer); see docs/design.md. */ +static bool +rpt_local_hop(const RptTransferMgr *mgr, int source, const RptGraphEdge &hop) +{ + const auto &src = mgr->vertices[mgr->by_rti.at(source)]; + const auto &dst = mgr->vertices[mgr->by_rti.at(hop.destination)]; + + /* A replicated destination needs the complete key set on every segment. */ + if (dst.replicated || src.numsegments <= 0 || dst.numsegments <= 0) + return false; + if (src.replicated) + return src.numsegments >= dst.numsegments; + if (src.numsegments != dst.numsegments || src.distkeys.empty() || + src.distkeys.size() != dst.distkeys.size() || + src.distopclasses.size() != src.distkeys.size() || + src.distopclasses != dst.distopclasses) + return false; + for (size_t col = 0; col < src.distkeys.size(); ++col) + { + bool matched = false; + for (size_t key = 0; key < hop.left_keys.size(); ++key) + { + const RptKey &a = hop.left_keys[key]; + const RptKey &b = hop.right_keys[key]; + if ((a == RptKey{source, src.distkeys[col]} && + b == RptKey{hop.destination, dst.distkeys[col]}) || + (b == RptKey{source, src.distkeys[col]} && + a == RptKey{hop.destination, dst.distkeys[col]})) + matched = true; + } + if (!matched) + return false; + } + return true; +} + +/* Persist the decision on both copies of every stage edge. In/out edges + * carry the same key pairs, but destination means producer for an in-edge. */ +static void +rpt_classify_hops(RptTransferMgr *mgr) +{ + for (auto &node : mgr->graph) + { + for (bool forward : {true, false}) + { + auto &outs = forward ? node.second.forward_stage.out : node.second.backward_stage.out; + for (auto &edge : outs) + { + edge.local = rpt_local_hop(mgr, node.first, edge); + auto &dest = mgr->graph.at(edge.destination); + auto &ins = forward ? dest.forward_stage.in : dest.backward_stage.in; + for (auto &in : ins) + if (in.destination == node.first) + in.local = edge.local; + } + } + } +} + /* * Backward-consumer eligibility: Anser's deadlock rule, with no counterpart * in the single-node artifact. A live backward consumer blocks until its * producer's filter arrives, so it may only wait on a producer the plan * order guarantees runs first: the producer's scan on the build (inner) * side of a hash join and the consumer's scan on the probe (outer) side of - * the same join. consumer_ok[v] is true exactly when that holds for one of - * v's backward-stage in-edges; everything else fails open (no backward - * filter for that table). + * the same join, in different slices (Anser refuses same-slice exchanges). + * consumer_ok[v] is true only when every GLOBAL backward in-edge satisfies + * that condition. Local edges bypass Anser and never wait; vertices with no + * global in-edges keep consumer_ok=false. * * The contexts are prefixed with plan_tree_base_prefix because * plan_tree_walker resolves SubPlan nodes through it, even when not @@ -422,19 +511,21 @@ typedef struct RptScanCollectContext { plan_tree_base_prefix base; /* must be first */ const std::map *scan_vertex; - std::set ids; + std::map slices; + int slice_index; } RptScanCollectContext; typedef struct RptConsumerCheckContext { plan_tree_base_prefix base; /* must be first */ PlannedStmt *stmt; + int slice_index; const std::map *scan_vertex; const std::map > *producers; - std::map *consumer_ok; + std::map > *safe_producers; } RptConsumerCheckContext; -/* Collect the ids of every vertex whose scan appears under this subtree. */ +/* Collect each vertex scan and the slice where it executes. */ static bool rpt_collect_scan_ids_walker(Node *node, void *context) { @@ -446,10 +537,15 @@ rpt_collect_scan_ids_walker(Node *node, void *context) /* vertex scans match by pointer; non-Plan nodes simply never hit */ it = ctx->scan_vertex->find((const Plan *) node); + int saved_slice = ctx->slice_index; + if (IsA(node, Motion)) + ctx->slice_index = ((Motion *) node)->motionID; if (it != ctx->scan_vertex->end()) - ctx->ids.insert(it->second); + ctx->slices[it->second] = ctx->slice_index; - return plan_tree_walker(node, rpt_collect_scan_ids_walker, context, false); + bool result = RptPgCall([&]() { return plan_tree_walker(node, rpt_collect_scan_ids_walker, context, false); }); + ctx->slice_index = saved_slice; + return result; } static bool @@ -460,6 +556,10 @@ rpt_consumer_check_walker(Node *node, void *context) if (node == NULL) return false; + int saved_slice = ctx->slice_index; + if (IsA(node, Motion)) + ctx->slice_index = ((Motion *) node)->motionID; + if (IsA(node, HashJoin)) { HashJoin *hj = (HashJoin *) node; @@ -467,10 +567,12 @@ rpt_consumer_check_walker(Node *node, void *context) RptScanCollectContext outer_ctx; std::map >::const_iterator pit; - exec_init_plan_tree_base(&inner_ctx.base, ctx->stmt); + RptPgCall([&]() { return exec_init_plan_tree_base(&inner_ctx.base, ctx->stmt); }); inner_ctx.scan_vertex = ctx->scan_vertex; - exec_init_plan_tree_base(&outer_ctx.base, ctx->stmt); + inner_ctx.slice_index = ctx->slice_index; + RptPgCall([&]() { return exec_init_plan_tree_base(&outer_ctx.base, ctx->stmt); }); outer_ctx.scan_vertex = ctx->scan_vertex; + outer_ctx.slice_index = ctx->slice_index; /* build side is the inner subtree, probe side the outer */ rpt_collect_scan_ids_walker((Node *) innerPlan(hj), &inner_ctx); @@ -481,21 +583,24 @@ rpt_consumer_check_walker(Node *node, void *context) int consumer = pit->first; std::set::const_iterator prod; - if ((*ctx->consumer_ok)[consumer] || - !outer_ctx.ids.count(consumer)) + if (!outer_ctx.slices.count(consumer)) continue; for (prod = pit->second.begin(); prod != pit->second.end(); ++prod) { - if (inner_ctx.ids.count(*prod)) + if (inner_ctx.slices.count(*prod) && + inner_ctx.slices.at(*prod) >= 0 && + outer_ctx.slices.at(consumer) >= 0 && + inner_ctx.slices.at(*prod) != outer_ctx.slices.at(consumer)) { - (*ctx->consumer_ok)[consumer] = true; - break; + (*ctx->safe_producers)[consumer].insert(*prod); } } } } - return plan_tree_walker(node, rpt_consumer_check_walker, context, false); + bool result = RptPgCall([&]() { return plan_tree_walker(node, rpt_consumer_check_walker, context, false); }); + ctx->slice_index = saved_slice; + return result; } static void @@ -503,6 +608,7 @@ rpt_check_consumers(PlannedStmt *stmt, RptTransferMgr *mgr) { std::map scan_vertex; std::map > producers; + std::map > safe_producers; RptConsumerCheckContext ctx; size_t i; std::map::const_iterator nit; @@ -515,26 +621,32 @@ rpt_check_consumers(PlannedStmt *stmt, RptTransferMgr *mgr) /* * The producer of a backward in-edge is its destination: the class - * leader for a redirected edge, the larger table otherwise. + * leader for a redirected edge, the larger table otherwise. Local filters + * are non-blocking and never need Anser consumer eligibility. */ for (nit = mgr->graph.begin(); nit != mgr->graph.end(); ++nit) { const std::vector &ins = nit->second.backward_stage.in; for (i = 0; i < ins.size(); i++) - producers[nit->first].insert(ins[i].destination); + if (!ins[i].local) + producers[nit->first].insert(ins[i].destination); } if (producers.empty() || stmt == NULL || stmt->planTree == NULL) return; - exec_init_plan_tree_base(&ctx.base, stmt); + RptPgCall([&]() { return exec_init_plan_tree_base(&ctx.base, stmt); }); ctx.stmt = stmt; + ctx.slice_index = 0; ctx.scan_vertex = &scan_vertex; ctx.producers = &producers; - ctx.consumer_ok = &mgr->consumer_ok; + ctx.safe_producers = &safe_producers; rpt_consumer_check_walker((Node *) stmt->planTree, &ctx); + for (const auto &entry : producers) + mgr->consumer_ok[entry.first] = + safe_producers[entry.first] == entry.second; } /* @@ -544,7 +656,8 @@ rpt_check_consumers(PlannedStmt *stmt, RptTransferMgr *mgr) void RptBuildTransferPlan(PlannedStmt *stmt, RptTransferMgr *mgr) { - std::vector > selected_edges; + std::vector > forward_edges; + std::vector > backward_edges; std::vector sorted_ids; std::set remaining; int32 next_order = 0; @@ -573,11 +686,43 @@ RptBuildTransferPlan(PlannedStmt *stmt, RptTransferMgr *mgr) * forest -- one tree per connected component of the join graph. */ while (!remaining.empty()) - rpt_largest_root_updated(mgr, remaining, sorted_ids, selected_edges, + rpt_largest_root_updated(mgr, remaining, sorted_ids, forward_edges, backward_edges, next_order); - rpt_create_transfer_plan(mgr, selected_edges); + for (const auto &group : mgr->groups) + if (group.second.leader_id != -1 && !mgr->roots.count(group.second.leader_id)) + mgr->bridges.insert(group.second.leader_id); + + rpt_create_transfer_plan(mgr, forward_edges, true); + rpt_create_transfer_plan(mgr, backward_edges, false); + + /* Transfers run in reverse insertion order. Cap actual dependency hops, + * keeping every inbound edge mirrored in its producer's out list. */ + std::map depth; + for (auto it = mgr->transfer_order.rbegin(); it != mgr->transfer_order.rend(); ++it) + { + auto &outs = mgr->graph[*it].forward_stage.out; + for (auto edge = outs.begin(); edge != outs.end();) + { + int dest = edge->destination; + if (depth[*it] >= rpt_max_chain_depth) + { + auto &ins = mgr->graph[dest].forward_stage.in; + ins.erase(std::remove_if(ins.begin(), ins.end(), + [&](const RptGraphEdge &in) { return in.destination == *it; }), ins.end()); + edge = outs.erase(edge); + } + else + { + depth[dest] = std::max(depth[dest], depth[*it] + 1); + ++edge; + } + } + if (!outs.empty()) + mgr->reducer_order.push_back(*it); + } + rpt_classify_hops(mgr); rpt_check_consumers(stmt, mgr); } @@ -598,133 +743,128 @@ rpt_edge_less(const RptGraphEdge &a, const RptGraphEdge &b) } static void -rpt_dump_key_pairs(StringInfo out, const RptGraphEdge &edge) +rpt_dump_key_pairs(std::ostream &out, const RptGraphEdge &edge) { - size_t i; - - for (i = 0; i < edge.left_keys.size(); i++) + for (size_t i = 0; i < edge.left_keys.size(); i++) { - appendStringInfo(out, "%s%d.%d->%d.%d", i > 0 ? "," : "", - edge.left_keys[i].rtindex, (int) edge.left_keys[i].attno, - edge.right_keys[i].rtindex, (int) edge.right_keys[i].attno); + if (i > 0) + out << ','; + out << edge.left_keys[i].rtindex << '.' << edge.left_keys[i].attno + << "->" << edge.right_keys[i].rtindex << '.' << edge.right_keys[i].attno; } } -/* - * One stage of one vertex: " build for on ()" for each - * out-edge, " apply from on ()" for each in-edge, entries - * separated by "; ", edges sorted by (destination, keys). - */ +/* A stage's sorted build/apply entries, preserving the diagnostic format. */ static void -rpt_dump_stage(StringInfo out, int vertex_id, const char *stage, +rpt_dump_stage(std::ostream &out, int vertex_id, const char *stage, const std::vector &in_edges, const std::vector &out_edges) { std::vector outs(out_edges); std::vector ins(in_edges); - bool any = false; - size_t i; + bool any = false; std::sort(outs.begin(), outs.end(), rpt_edge_less); std::sort(ins.begin(), ins.end(), rpt_edge_less); - - appendStringInfo(out, "vertex %d %s:", vertex_id, stage); - for (i = 0; i < outs.size(); i++) + out << "vertex " << vertex_id << ' ' << stage << ':'; + for (const auto &edge : outs) { - appendStringInfo(out, "%sbuild for %d on (", any ? "; " : " ", - outs[i].destination); - rpt_dump_key_pairs(out, outs[i]); - appendStringInfoChar(out, ')'); + out << (any ? "; " : " ") << "build for " << edge.destination << " on ("; + rpt_dump_key_pairs(out, edge); + out << ')'; any = true; } - for (i = 0; i < ins.size(); i++) + for (const auto &edge : ins) { - appendStringInfo(out, "%sapply from %d on (", any ? "; " : " ", - ins[i].destination); - rpt_dump_key_pairs(out, ins[i]); - appendStringInfoChar(out, ')'); + out << (any ? "; " : " ") << "apply from " << edge.destination << " on ("; + rpt_dump_key_pairs(out, edge); + out << ')'; any = true; } if (!any) - appendStringInfoString(out, " none"); - appendStringInfoChar(out, '\n'); + out << " none"; + out << '\n'; } -/* - * Dump the whole analysis as deterministic text: Prim transfer order, - * vertices by ascending id, equivalence classes by representative key, the - * forward/backward stages per vertex, and the consumer-eligibility verdicts. - */ +/* Build in C++ storage, then cross the PostgreSQL allocation boundary once. + * RptPgCall ensures a failed append unwinds the stream and string owners. */ void -RptDumpTransferPlan(const RptTransferMgr *mgr, StringInfo out) +RptDumpTransferPlan(const RptTransferMgr *mgr, StringInfo output) { + std::ostringstream out; std::vector ids; - size_t i; - std::map::const_iterator git; - for (i = 0; i < mgr->vertices.size(); i++) - ids.push_back(mgr->vertices[i].id); + out.exceptions(std::ios::badbit | std::ios::failbit); + out.imbue(std::locale::classic()); + for (const auto &vertex : mgr->vertices) + ids.push_back(vertex.id); std::sort(ids.begin(), ids.end()); - /* Prim insertion order */ - appendStringInfoString(out, "transfer order:"); - for (i = 0; i < mgr->transfer_order.size(); i++) - appendStringInfo(out, " %d", mgr->transfer_order[i]); - appendStringInfoChar(out, '\n'); - - /* vertices */ - for (i = 0; i < ids.size(); i++) + out << "transfer order:"; + for (int id : mgr->transfer_order) + out << ' ' << id; + out << "\nforward depth limit: " << rpt_max_chain_depth << "\nreducer order:"; + for (int id : mgr->reducer_order) + out << ' ' << id; + out << "\nbridge tables:"; + for (int id : mgr->bridges) + out << ' ' << id; + out << '\n'; + + for (int id : ids) { - const RptVertexInfo &v = mgr->vertices[mgr->by_rti.find(ids[i])->second]; + const auto &v = mgr->vertices[mgr->by_rti.at(id)]; - appendStringInfo(out, "vertex %d %s rows=" INT64_FORMAT " %s\n", - v.id, v.relname.c_str(), v.cardinality, - v.filtered ? "filtered" : - v.intermediate ? "intermediate" : "unfiltered"); + out << "vertex " << v.id << ' ' << v.relname << " rows=" << v.cardinality + << ' ' << (v.filtered ? "filtered" : v.intermediate ? "intermediate" : "unfiltered") + << '\n'; } - - /* equivalence classes */ - for (git = mgr->groups.begin(); git != mgr->groups.end(); ++git) + for (const auto &group : mgr->groups) { - const RptKeyGroup &g = git->second; - std::set::const_iterator mit; - bool first = true; + bool first = true; - appendStringInfo(out, "class %d.%d: members=", - git->first.rtindex, (int) git->first.attno); - for (mit = g.members.begin(); mit != g.members.end(); ++mit) + out << "class " << group.first.rtindex << '.' << group.first.attno << ": members="; + for (int member : group.second.members) { - appendStringInfo(out, "%s%d", first ? "" : ",", *mit); + if (!first) + out << ','; + out << member; first = false; } - appendStringInfo(out, " leader=%d\n", g.leader_id); + out << " leader=" << group.second.leader_id << '\n'; } - - /* forward and backward stages per vertex */ - for (i = 0; i < ids.size(); i++) + for (int id : ids) { - std::map::const_iterator nit = mgr->graph.find(ids[i]); + const auto &node = mgr->graph.find(id); - if (nit == mgr->graph.end()) + if (node == mgr->graph.end()) { - appendStringInfo(out, "vertex %d forward: none\n", ids[i]); - appendStringInfo(out, "vertex %d backward: none\n", ids[i]); + out << "vertex " << id << " forward: none\n" + << "vertex " << id << " backward: none\n"; continue; } - rpt_dump_stage(out, ids[i], "forward", - nit->second.forward_stage.in, - nit->second.forward_stage.out); - rpt_dump_stage(out, ids[i], "backward", - nit->second.backward_stage.in, - nit->second.backward_stage.out); + rpt_dump_stage(out, id, "forward", node->second.forward_stage.in, + node->second.forward_stage.out); + rpt_dump_stage(out, id, "backward", node->second.backward_stage.in, + node->second.backward_stage.out); } - - /* backward-consumer eligibility */ - for (i = 0; i < ids.size(); i++) + for (const auto &node : mgr->graph) + { + for (const auto &edge : node.second.forward_stage.out) + out << "forward hop " << node.first << "->" << edge.destination + << " merge=" << (edge.local ? "local" : "global") << '\n'; + for (const auto &edge : node.second.backward_stage.out) + out << "backward hop " << node.first << "->" << edge.destination + << " merge=" << (edge.local ? "local" : "global") << '\n'; + } + for (int id : ids) { - std::map::const_iterator cit = mgr->consumer_ok.find(ids[i]); + const auto &consumer = mgr->consumer_ok.find(id); - appendStringInfo(out, "consumer_ok %d %s\n", ids[i], - (cit != mgr->consumer_ok.end() && cit->second) ? "yes" : "no"); + out << "consumer_ok " << id << ' ' + << (consumer != mgr->consumer_ok.end() && consumer->second ? "yes" : "no") << '\n'; } + + std::string text = out.str(); + RptPgCall([&]() { appendStringInfoString(output, text.c_str()); }); } diff --git a/gpcontrib/robust_predicate_transfer/src/rptregion.cpp b/gpcontrib/robust_predicate_transfer/src/rptregion.cpp index 28b77218db6..d080c1e268e 100644 --- a/gpcontrib/robust_predicate_transfer/src/rptregion.cpp +++ b/gpcontrib/robust_predicate_transfer/src/rptregion.cpp @@ -42,6 +42,7 @@ extern "C" { #include "postgres.h" +#include "catalog/gp_distribution_policy.h" #include "nodes/parsenodes.h" #include "nodes/pg_list.h" #include "parser/parsetree.h" @@ -140,6 +141,18 @@ rpt_vertex_for_scan(PlannedStmt *stmt, RptTransferMgr *mgr, Plan *scan) if (rte->eref != NULL) v.relname = rte->eref->aliasname; + if (rte->rtekind == RTE_RELATION) + { + GpPolicy *policy = RptPgCall([&]() { return GpPolicyFetch(rte->relid); }); + v.replicated = policy->ptype == POLICYTYPE_REPLICATED; + v.numsegments = policy->numsegments; + if (policy->ptype == POLICYTYPE_PARTITIONED && policy->nattrs > 0) + { + v.distkeys.assign(policy->attrs, policy->attrs + policy->nattrs); + v.distopclasses.assign(policy->opclasses, policy->opclasses + policy->nattrs); + } + RptPgCall([&]() { pfree(policy); }); + } } mgr->by_rti[rti] = (int) mgr->vertices.size(); @@ -187,6 +200,18 @@ rpt_resolve_key_scan(Plan *top, AttrNumber key_attno, return false; var = (Var *) tle->expr; + if (IsA(node, IndexOnlyScan) && var->varno == INDEX_VAR) + { + IndexOnlyScan *ios = (IndexOnlyScan *) node; + + if (var->varattno < 1 || var->varattno > list_length(ios->indextlist)) + return false; + tle = (TargetEntry *) list_nth(ios->indextlist, var->varattno - 1); + if (!IsA(tle->expr, Var)) + return false; + var = (Var *) tle->expr; + } + /* * Terminus: a base scan the reducer could replay. The key column is * the Var's attno in the scanned relation. A targetlist Var naming @@ -309,8 +334,8 @@ rpt_equality_clause(Node *clause, Var **outer_var, Var **inner_var, if ((*inner_var)->vartype != (*outer_var)->vartype) return false; - keytype = getBaseType((*inner_var)->vartype); - if (!get_typbyval(keytype)) + keytype = RptPgCall([&]() { return getBaseType((*inner_var)->vartype); }); + if (!RptPgCall([&]() { return get_typbyval(keytype); })) return false; if (keytype == FLOAT4OID || keytype == FLOAT8OID) return false; @@ -321,7 +346,7 @@ rpt_equality_clause(Node *clause, Var **outer_var, Var **inner_var, * alone is not the bitwise-equality proof and the float exclusion above * is still needed. */ - tce = lookup_type_cache(keytype, TYPECACHE_EQ_OPR); + tce = RptPgCall([&]() { return lookup_type_cache(keytype, TYPECACHE_EQ_OPR); }); if (!OidIsValid(tce->eq_opr) || op->opno != tce->eq_opr) return false; diff --git a/gpcontrib/robust_predicate_transfer/test/graph_test.cpp b/gpcontrib/robust_predicate_transfer/test/graph_test.cpp new file mode 100644 index 00000000000..f95fb7b9d00 --- /dev/null +++ b/gpcontrib/robust_predicate_transfer/test/graph_test.cpp @@ -0,0 +1,576 @@ +/*------------------------------------------------------------------------- + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * graph_test.cpp + * Deterministic tests of the production graph, planner, resolver and + * PostgreSQL/C++ error boundary, using synthetic plans and C-call stubs. + * + * IDENTIFICATION + * gpcontrib/robust_predicate_transfer/test/graph_test.cpp + * + *------------------------------------------------------------------------- + */ +/* Standalone tests of the production graph/plan code. PostgreSQL calls are + * stubbed below; no server or optimizer choices affect these graph fixtures. */ +#include "../src/rptgraph.cpp" +#include "../src/rptplan.cpp" +#include "../src/rptregion.cpp" + +#include +#include +#include +#undef vsnprintf + +/* Unlike assert(), checks and their side effects remain active with NDEBUG. */ +#define CHECK(condition) \ + do { \ + if (!(condition)) { \ + fputs("check failed: " #condition "\n", stderr); \ + abort(); \ + } \ + } while (0) + +extern "C" { +sigjmp_buf *PG_exception_stack = NULL; +ErrorContextCallback *error_context_stack = NULL; +int dispatch_nest_level = 0; +Oid getBaseType(Oid type) { return type; } +bool get_typbyval(Oid) { return true; } +TypeCacheEntry *lookup_type_cache(Oid, int) { abort(); } +GpPolicy *GpPolicyFetch(Oid) { abort(); } +void pfree(void *) { abort(); } +bool AnserIsRuntimeFilterScan(const Plan *) { return false; } + +void ExceptionalCondition(const char *, const char *, int) { abort(); } +void exec_init_plan_tree_base(plan_tree_base_prefix *, PlannedStmt *) {} +bool plan_tree_walker(Node *node, bool (*walker)(Node *, void *), void *ctx, bool) +{ + Plan *plan = (Plan *) node; + return walker((Node *) outerPlan(plan), ctx) || + walker((Node *) innerPlan(plan), ctx); +} +void appendStringInfo(StringInfo str, const char *format, ...) +{ + va_list args; + va_start(args, format); + int count = vsnprintf(str->data + str->len, str->maxlen - str->len, format, args); + va_end(args); + CHECK(count >= 0 && count < str->maxlen - str->len); + str->len += count; +} +void appendStringInfoString(StringInfo str, const char *text) +{ + appendStringInfo(str, "%s", text); +} +void appendStringInfoChar(StringInfo str, char ch) +{ + appendStringInfo(str, "%c", ch); +} +} +bool rpt_enable = false; +bool rpt_debug = false; +int rpt_max_chain_depth = 4; + +static void vertex(RptTransferMgr &mgr, int id, int rows, bool filtered) +{ + RptVertexInfo v = {}; + v.id = id; + v.cardinality = rows; + v.filtered = filtered; + mgr.by_rti[id] = mgr.vertices.size(); + mgr.vertices.push_back(v); +} + +static RptEdgeInfo edge(int a, int b, bool protect_a = false, + bool protect_b = false, int col_a = 1, int col_b = 1) +{ + RptEdgeInfo e = {}; + e.left_v = a; + e.right_v = b; + e.key_type = 23; + e.protect_left = protect_a; + e.protect_right = protect_b; + e.clauses.push_back(std::make_pair(RptKey{a, (AttrNumber) col_a}, + RptKey{b, (AttrNumber) col_b})); + return e; +} + +static void connect(RptTransferMgr &mgr, const RptEdgeInfo &e) +{ + rpt_upsert_edge(&mgr, e); + mgr.vertices[mgr.by_rti[e.left_v]].join_keys.insert(e.clauses[0].first); + mgr.vertices[mgr.by_rti[e.right_v]].join_keys.insert(e.clauses[0].second); +} + +static void bypass_direction(bool reverse) +{ + RptTransferMgr mgr; + vertex(mgr, 1, 100, true); + vertex(mgr, 2, 10, true); + vertex(mgr, 3, 50, false); + /* 1 LEFT JOIN (2 JOIN 3): only 1 -> 3 -> 2 is legal. */ + connect(mgr, reverse ? edge(3, 1, false, true) : edge(1, 3, true)); + connect(mgr, reverse ? edge(3, 2) : edge(2, 3)); + mgr.vertices[2].unfiltered = true; + rpt_skip_unfiltered(&mgr); + const auto &e = mgr.neighbor_matrix.at(1).at(2); + CHECK(rpt_can_flow(&e, 1, 2)); + CHECK(!rpt_can_flow(&e, 2, 1)); + CHECK(!rpt_can_flow(&mgr.neighbor_matrix.at(2).at(1), 2, 1)); +} + +static void protection_and_idempotence() +{ + RptTransferMgr mgr; + CHECK(rpt_upsert_edge(&mgr, edge(1, 2, true))); + CHECK(!rpt_upsert_edge(&mgr, edge(1, 2, true))); + CHECK(!rpt_upsert_edge(&mgr, edge(2, 1))); + CHECK(mgr.neighbor_matrix[1][2].protect_left); + CHECK(rpt_upsert_edge(&mgr, edge(2, 1, true))); + CHECK(mgr.neighbor_matrix[1][2].protect_right); +} + +static RptTransferMgr star(int cap) +{ + RptTransferMgr mgr; + rpt_max_chain_depth = cap; + for (int id = 1; id <= 4; ++id) + vertex(mgr, id, 100 / id, true); + for (int id = 2; id <= 4; ++id) + connect(mgr, edge(1, id)); + RptBuildClasses(&mgr); + RptBuildTransferPlan(NULL, &mgr); + return mgr; +} + +static void asymmetric_shape() +{ + auto mgr = star(4); + CHECK(mgr.groups.size() == 1); + CHECK(mgr.transfer_order == std::vector({1, 2, 3, 4})); + CHECK(mgr.reducer_order == std::vector({4, 3, 2})); + for (int id = 2; id <= 4; ++id) + { + CHECK(mgr.graph[id].forward_stage.out.size() == 1); + CHECK(mgr.graph[id].forward_stage.out[0].destination == id - 1); + CHECK(mgr.graph[id].backward_stage.in.size() == 1); + CHECK(mgr.graph[id].backward_stage.in[0].destination == 1); + } + CHECK(mgr.graph[1].forward_stage.out.empty()); + CHECK(mgr.graph[1].backward_stage.out.size() == 3); + mgr = star(1); + CHECK(mgr.graph[4].forward_stage.out.size() == 1); + CHECK(mgr.graph[3].forward_stage.out.empty()); + CHECK(mgr.graph[2].forward_stage.in.empty()); +} + +static void unfiltered_cycle() +{ + RptTransferMgr mgr; + for (int id = 1; id <= 4; ++id) + vertex(mgr, id, 10, id == 1); + connect(mgr, edge(1, 2)); + connect(mgr, edge(2, 3)); + connect(mgr, edge(3, 4)); + connect(mgr, edge(4, 2)); + RptBuildClasses(&mgr); /* Must terminate, with no unfiltered producers. */ + for (const auto &row : mgr.neighbor_matrix) + for (const auto &entry : row.second) + if (row.first != 1) + CHECK(!rpt_can_flow(&entry.second, row.first, entry.first)); +} + +static void all_producers_required() +{ + RptTransferMgr mgr; + Plan scans[3] = {}; + for (int id = 1; id <= 3; ++id) + { + vertex(mgr, id, 10, true); + mgr.vertices.back().scan = &scans[id - 1]; + } + HashJoin join = {}; + NodeSetTag(&join, T_HashJoin); + outerPlan(&join) = &scans[0]; + innerPlan(&join) = &scans[1]; + PlannedStmt stmt = {}; + stmt.planTree = (Plan *) &join; + RptGraphEdge in = {}; + in.destination = 2; + mgr.graph[1].backward_stage.in.push_back(in); + rpt_check_consumers(&stmt, &mgr); + CHECK(!mgr.consumer_ok[1]); + Motion build_motion = {}; + NodeSetTag(&build_motion, T_Motion); + build_motion.motionID = 1; + outerPlan(&build_motion) = &scans[1]; + innerPlan(&join) = (Plan *) &build_motion; + rpt_check_consumers(&stmt, &mgr); + CHECK(mgr.consumer_ok[1]); + build_motion.motionID = -1; + rpt_check_consumers(&stmt, &mgr); + CHECK(!mgr.consumer_ok[1]); + build_motion.motionID = 1; + in.destination = 3; /* This producer is not ordered before the probe. */ + mgr.graph[1].backward_stage.in.push_back(in); + rpt_check_consumers(&stmt, &mgr); + CHECK(!mgr.consumer_ok[1]); + mgr.graph[1].backward_stage.in[1].local = true; + rpt_check_consumers(&stmt, &mgr); + CHECK(mgr.consumer_ok[1]); /* The unsafe local edge does not wait. */ + mgr.graph[1].backward_stage.in[0].local = true; + rpt_check_consumers(&stmt, &mgr); + CHECK(!mgr.consumer_ok[1]); /* All-local means no blocking consumer. */ + for (auto &edge : mgr.graph[1].backward_stage.in) + edge.local = false; + /* Different hash joins can independently prove the two producers. */ + HashJoin upper = {}; + NodeSetTag(&upper, T_HashJoin); + outerPlan(&upper) = (Plan *) &join; + Motion upper_motion = {}; + NodeSetTag(&upper_motion, T_Motion); + upper_motion.motionID = 2; + outerPlan(&upper_motion) = &scans[2]; + innerPlan(&upper) = (Plan *) &upper_motion; + stmt.planTree = (Plan *) &upper; + rpt_check_consumers(&stmt, &mgr); + CHECK(mgr.consumer_ok[1]); +} + +static void bridges_and_locality() +{ + RptTransferMgr mgr; + vertex(mgr, 1, 100, true); + vertex(mgr, 2, 50, false); + vertex(mgr, 3, 20, true); + connect(mgr, edge(1, 2)); + connect(mgr, edge(2, 3, false, false, 2)); + RptBuildClasses(&mgr); + rpt_max_chain_depth = 4; + RptBuildTransferPlan(NULL, &mgr); + CHECK(mgr.groups.size() == 2); + CHECK(mgr.bridges == std::set({2})); + CHECK(mgr.reducer_order == std::vector({3, 2})); + for (auto &v : mgr.vertices) + { + v.numsegments = 3; + v.distkeys = {1}; + v.distopclasses = {1978}; + } + RptGraphEdge hop = {}; + hop.destination = 2; + hop.left_keys = {{1, 1}}; + hop.right_keys = {{2, 1}}; + CHECK(rpt_local_hop(&mgr, 1, hop)); + mgr.vertices[1].numsegments = 2; + CHECK(!rpt_local_hop(&mgr, 1, hop)); + mgr.vertices[1].numsegments = 3; + mgr.vertices[1].distopclasses = {1}; + CHECK(!rpt_local_hop(&mgr, 1, hop)); + mgr.vertices[1].distopclasses = {1978}; + mgr.vertices[1].distkeys = {2}; + CHECK(!rpt_local_hop(&mgr, 1, hop)); +} + +static void hash_policy(RptVertexInfo &v, int segments = 3) +{ + v.numsegments = segments; + v.distkeys = {1}; + v.distopclasses = {1978}; +} + +static void local_star_and_backward() +{ + auto mgr = star(4); + for (auto &v : mgr.vertices) + hash_policy(v); + rpt_classify_hops(&mgr); + int forward = 0; + int backward = 0; + for (const auto &node : mgr.graph) + { + for (const auto &hop : node.second.forward_stage.out) + { + ++forward; + CHECK(hop.local); /* Includes synthetic 4->3 and 3->2. */ + CHECK(mgr.graph.at(hop.destination).forward_stage.in[0].local); + } + for (const auto &hop : node.second.backward_stage.out) + { + ++backward; + CHECK(hop.local); + CHECK(mgr.graph.at(hop.destination).backward_stage.in[0].local); + } + } + CHECK(forward == 3 && backward == 3); + char buffer[16384] = {}; + StringInfoData dump = {}; + dump.data = buffer; + dump.maxlen = sizeof(buffer); + RptDumpTransferPlan(&mgr, &dump); + std::string text(buffer); + CHECK(text.find("merge=global") == std::string::npos); + CHECK(text.find("forward hop 3->2 merge=local\n") != std::string::npos); + CHECK(text.find("forward hop 4->3 merge=local\n") != std::string::npos); + CHECK(text.find("backward hop 1->4 merge=local\n") != std::string::npos); + + /* A class can mix local and global broadcast recipients. */ + mgr.vertices[mgr.by_rti[4]].numsegments = 2; + rpt_classify_hops(&mgr); + for (const auto &hop : mgr.graph[1].backward_stage.out) + CHECK(hop.local == (hop.destination != 4)); + + /* Bypassing an unfiltered table preserves policy-local transfer too. */ + RptTransferMgr bypass; + vertex(bypass, 1, 100, true); + vertex(bypass, 2, 10, true); + vertex(bypass, 3, 50, false); + for (auto &v : bypass.vertices) + hash_policy(v); + connect(bypass, edge(1, 3, true)); + connect(bypass, edge(2, 3)); + bypass.vertices[2].unfiltered = true; + rpt_skip_unfiltered(&bypass); + const auto &e = bypass.neighbor_matrix.at(1).at(2); + RptGraphEdge hop = {}; + hop.destination = 2; + hop.left_keys = {e.clauses[0].first}; + hop.right_keys = {e.clauses[0].second}; + CHECK(rpt_local_hop(&bypass, 1, hop)); +} + +static void replicated_and_compound_policies() +{ + RptTransferMgr mgr; + vertex(mgr, 1, 100, true); + vertex(mgr, 2, 10, true); + for (auto &v : mgr.vertices) + hash_policy(v); + RptGraphEdge hop = {}; + hop.destination = 2; + hop.left_keys = {{1, 1}}; + hop.right_keys = {{2, 1}}; + /* No neighbor-matrix edge is needed to prove policy locality. */ + CHECK(rpt_local_hop(&mgr, 1, hop)); + auto &src = mgr.vertices[0]; + auto &dst = mgr.vertices[1]; + src.distkeys = {1, 2}; + src.distopclasses = {1978, 1978}; + dst.distkeys = {1, 2}; + dst.distopclasses = {1978, 1978}; + CHECK(!rpt_local_hop(&mgr, 1, hop)); /* Missing a distribution column. */ + hop.left_keys.push_back({1, 2}); + hop.right_keys.push_back({2, 2}); + CHECK(rpt_local_hop(&mgr, 1, hop)); + std::swap(dst.distkeys[0], dst.distkeys[1]); + CHECK(!rpt_local_hop(&mgr, 1, hop)); /* Hash order matters. */ + + src.replicated = true; + src.distkeys.clear(); + src.distopclasses.clear(); + CHECK(rpt_local_hop(&mgr, 1, hop)); + src.numsegments = 4; + CHECK(rpt_local_hop(&mgr, 1, hop)); + src.numsegments = 2; + CHECK(!rpt_local_hop(&mgr, 1, hop)); + src.numsegments = 3; + dst.replicated = true; + CHECK(!rpt_local_hop(&mgr, 1, hop)); /* Into replicated always global. */ + src.replicated = false; + CHECK(!rpt_local_hop(&mgr, 1, hop)); + dst.replicated = false; + CHECK(!rpt_local_hop(&mgr, 1, hop)); /* Random/unknown source. */ +} + +static void index_only_endpoint() +{ + IndexOnlyScan scan = {}; + NodeSetTag(&scan, T_IndexOnlyScan); + scan.scan.scanrelid = 7; + TargetEntry output = {}; + Var index_var = {}; + NodeSetTag(&index_var, T_Var); + index_var.varno = INDEX_VAR; + index_var.varattno = 1; + output.expr = (Expr *) &index_var; + Var base_var = {}; + NodeSetTag(&base_var, T_Var); + base_var.varno = 7; + base_var.varattno = 3; + TargetEntry index_column = {}; + index_column.expr = (Expr *) &base_var; + /* A one-cell PostgreSQL list, without requiring palloc in the driver. */ + ListCell out_cell = {}; + out_cell.ptr_value = &output; + List out_list = {}; + out_list.type = T_List; + out_list.length = out_list.max_length = 1; + out_list.elements = &out_cell; + ListCell index_cell = {}; + index_cell.ptr_value = &index_column; + List index_list = out_list; + index_list.elements = &index_cell; + scan.scan.plan.targetlist = &out_list; + scan.indextlist = &index_list; + Plan *resolved = NULL; + AttrNumber attno = 0; + CHECK(rpt_resolve_key_scan((Plan *) &scan, 1, &resolved, &attno)); + CHECK(resolved == (Plan *) &scan && attno == 3); +} + +static void join_protection() +{ + for (JoinType type : {JOIN_INNER, JOIN_SEMI, JOIN_LEFT, JOIN_RIGHT, + JOIN_FULL, JOIN_ANTI, JOIN_LASJ_NOTIN}) + { + RptTransferMgr mgr; + SeqScan scans[2] = {}; + Var vars[2] = {}; + TargetEntry entries[2] = {}; + ListCell cells[2] = {}; + List lists[2] = {}; + for (int i = 0; i < 2; ++i) + { + NodeSetTag(&scans[i], T_SeqScan); + scans[i].scan.scanrelid = i + 1; + scans[i].scan.plan.plan_rows = 100 - i; + NodeSetTag(&vars[i], T_Var); + vars[i].varno = i + 1; + vars[i].varattno = 1; + entries[i].expr = (Expr *) &vars[i]; + cells[i].ptr_value = &entries[i]; + lists[i].type = T_List; + lists[i].length = lists[i].max_length = 1; + lists[i].elements = &cells[i]; + scans[i].scan.plan.targetlist = &lists[i]; + scans[i].scan.plan.qual = &lists[i]; /* mark both scans filtered */ + } + PlannedStmt stmt = {}; + rpt_add_edge(&stmt, &mgr, type, (Plan *) &scans[0], 1, + (Plan *) &scans[1], 1, 23); + if (type == JOIN_FULL || type == JOIN_ANTI || type == JOIN_LASJ_NOTIN) + { + CHECK(mgr.vertices.empty() && mgr.neighbor_matrix.empty()); + continue; + } + const auto &e = mgr.neighbor_matrix.at(1).at(2); + CHECK(e.protect_left == (type == JOIN_LEFT)); + CHECK(e.protect_right == (type == JOIN_RIGHT)); + RptBuildClasses(&mgr); + RptBuildTransferPlan(NULL, &mgr); + if (type == JOIN_LEFT || type == JOIN_RIGHT) + { + int preserved = type == JOIN_LEFT ? 1 : 2; + CHECK(mgr.graph[preserved].forward_stage.in.empty()); + CHECK(mgr.graph[preserved].backward_stage.in.empty()); + } + } +} + +static void directed_parent_choice() +{ + RptTransferMgr mgr; + for (int id = 1; id <= 3; ++id) + vertex(mgr, id, 100 / id, true); + std::set constructed = {1, 2}; + std::set remaining = {3}; + std::map forward_depth = {{1, 0}, {2, 3}}; + std::map backward_depth = forward_depth; + int forward_from, backward_from, to; + + connect(mgr, edge(1, 3)); + connect(mgr, edge(2, 3, true)); /* deepest parent blocks 3 -> 2 */ + CHECK(rpt_find_edge(&mgr, constructed, remaining, forward_depth, backward_depth, + &forward_from, &backward_from, &to)); + CHECK(to == 3 && forward_from == 1 && backward_from == 1); + + mgr.neighbor_matrix.clear(); + connect(mgr, edge(1, 3, false, true)); /* shallowest blocks 1 -> 3 */ + connect(mgr, edge(2, 3)); + CHECK(rpt_find_edge(&mgr, constructed, remaining, forward_depth, backward_depth, + &forward_from, &backward_from, &to)); + CHECK(forward_from == 2 && backward_from == 2); + + mgr.neighbor_matrix.clear(); + RptEdgeInfo heavy = edge(1, 3, true); + heavy.clauses.push_back(std::make_pair(RptKey{1, 2}, RptKey{3, 2})); + connect(mgr, heavy); + connect(mgr, edge(2, 3)); + CHECK(rpt_find_edge(&mgr, constructed, remaining, forward_depth, backward_depth, + &forward_from, &backward_from, &to)); + CHECK(forward_from == 2 && backward_from == 1); /* lower-weight open edge */ + + mgr.neighbor_matrix.clear(); + connect(mgr, edge(1, 3, true)); + connect(mgr, edge(2, 3, true)); + CHECK(rpt_find_edge(&mgr, constructed, remaining, forward_depth, backward_depth, + &forward_from, &backward_from, &to)); + CHECK(forward_from == -1 && backward_from == 1); + + mgr.neighbor_matrix.clear(); + connect(mgr, edge(1, 3, false, true)); + connect(mgr, edge(2, 3, false, true)); + CHECK(rpt_find_edge(&mgr, constructed, remaining, forward_depth, backward_depth, + &forward_from, &backward_from, &to)); + CHECK(forward_from == 2 && backward_from == -1); +} + +static void error_unwinds() +{ + int destroyed = 0; + struct Owner { + int &count; + ~Owner() { ++count; } + }; + try + { + Owner owner{destroyed}; + RptPgCall([]() { siglongjmp(*PG_exception_stack, 1); }); + abort(); + } + catch (const RptPgError &) {} + CHECK(destroyed == 1); + CHECK(PG_exception_stack == NULL); + /* A C++ callback exception must restore PG's exception stack too. */ + try + { + RptPgCall([]() { throw std::bad_alloc(); }); + abort(); + } + catch (const std::bad_alloc &) {} + CHECK(PG_exception_stack == NULL); +} + +int main() +{ + bypass_direction(false); + bypass_direction(true); + protection_and_idempotence(); + asymmetric_shape(); + unfiltered_cycle(); + all_producers_required(); + bridges_and_locality(); + local_star_and_backward(); + replicated_and_compound_policies(); + index_only_endpoint(); + join_protection(); + directed_parent_choice(); + error_unwinds(); + return 0; +} diff --git a/src/Makefile.global.in b/src/Makefile.global.in index 23d9e9080f8..d235317d36a 100644 --- a/src/Makefile.global.in +++ b/src/Makefile.global.in @@ -267,6 +267,7 @@ have_yaml = @have_yaml@ YAML_LIBS = @YAML_LIBS@ with_zstd = @with_zstd@ with_anser = @with_anser@ +with_rpt = @with_rpt@ ZSTD_CFLAGS = @ZSTD_CFLAGS@ ZSTD_LIBS = @ZSTD_LIBS@ EVENT_LIBS = @EVENT_LIBS@ From 840adb9dd675db3149bcf1b164773fab6379b275 Mon Sep 17 00:00:00 2001 From: Leonid Borchuk Date: Tue, 10 Nov 2026 12:33:56 +0300 Subject: [PATCH 20/27] Feature: Add RPT backward eligibility survey Prepare the Phase II go/no-go survey without injecting executor nodes. Extract backward eligibility into a shared analysis pass and record per-class decisions, refusal reasons, relink parents, tuple-output key positions, slice IDs, and bounded-prefetch requirements. Require a component-root producer, a global transfer, and a hash-build ordering proof. Reject nullable-side crossings, multi-tuple prefetch paths, all SubPlan placements even below Motions, and any non-empty build-side extParam. Apply a per-class consumer cap and report rough NDV benefit estimates, preserving unknown statistics explicitly. Add a planning-only, read-only survey runner comparing RPT eligibility with Anser's injected producer count under both optimizer settings. Count SELECT statements rather than files, split multi-SELECT templates, and require TPC-H Q15's CTE form. Expect 103 TPC-DS statements from 99 templates. Record per-statement results, failures, and raw plan artifacts. Add reserved QD-only backward settings, document diagnostic EXECUTE privileges, and update the runtime design to use one statement without a session cache. Runtime injection remains gated on benchmark results. Add native safety tests and production-walker/resolver SQL regressions, including a correlated SubPlan with an internal Motion. SQL assertions identify tables by name and account for retained optimizer plan shapes. Validation: C++ compilation, native tests with -O3 -DNDEBUG, ASan/UBSan, all 10 Python survey tests, and git diff --check passed. The new SQL regressions and full benchmark survey have not run on a cluster. Assisted-by: ChatGPT --- gpcontrib/robust_predicate_transfer/Makefile | 10 +- gpcontrib/robust_predicate_transfer/README.md | 12 + .../robust_predicate_transfer/docs/design.md | 133 +++++- .../expected/rpt_backward_eligibility.out | 208 +++++++++ .../robust_predicate_transfer/include/rpt.h | 28 ++ .../scripts/survey.py | 342 ++++++++++++++ .../sql/rpt_backward_eligibility.sql | 208 +++++++++ .../src/rpteligible.cpp | 425 ++++++++++++++++++ .../robust_predicate_transfer/src/rptinit.cpp | 14 + .../robust_predicate_transfer/src/rptplan.cpp | 218 +++------ .../src/rptregion.cpp | 32 ++ .../test/graph_test.cpp | 300 +++++++++++-- .../test/survey_test.py | 150 +++++++ 13 files changed, 1851 insertions(+), 229 deletions(-) create mode 100644 gpcontrib/robust_predicate_transfer/expected/rpt_backward_eligibility.out create mode 100644 gpcontrib/robust_predicate_transfer/scripts/survey.py create mode 100644 gpcontrib/robust_predicate_transfer/sql/rpt_backward_eligibility.sql create mode 100644 gpcontrib/robust_predicate_transfer/src/rpteligible.cpp create mode 100644 gpcontrib/robust_predicate_transfer/test/survey_test.py diff --git a/gpcontrib/robust_predicate_transfer/Makefile b/gpcontrib/robust_predicate_transfer/Makefile index b870dade8e0..a0c5b8247fe 100644 --- a/gpcontrib/robust_predicate_transfer/Makefile +++ b/gpcontrib/robust_predicate_transfer/Makefile @@ -28,6 +28,7 @@ MODULE_big = robust_predicate_transfer OBJS = \ $(WIN32RES) \ src/rptgraph.o \ + src/rpteligible.o \ src/rptinit.o \ src/rptplan.o \ src/rptregion.o @@ -37,7 +38,7 @@ PGFILEDESC = "robust_predicate_transfer - RPT+ transfer plans over Anser" EXTENSION = robust_predicate_transfer DATA = robust_predicate_transfer--1.0.sql -REGRESS = rpt_regions rpt_graph +REGRESS = rpt_regions rpt_graph rpt_backward_eligibility # Anser is a sibling module, not a linked library: it is preloaded and dfmgr.c # loads modules RTLD_GLOBAL, so the AnserXxx entry points resolve at load time. @@ -96,7 +97,7 @@ preload-rpt: # cluster, and runs the tests. .PHONY: installcheck-with-install installcheck-with-install: - $(MAKE) unitcheck + $(MAKE) unitcheck surveycheck $(MAKE) -C ../anser install $(MAKE) install $(MAKE) installcheck @@ -111,3 +112,8 @@ test/graph_test: $(srcdir)/test/graph_test.cpp $(wildcard $(srcdir)/src/*.cpp) $ $(CXX) $(CXXFLAGS) $(CPPFLAGS) $(PG_CPPFLAGS) $< -o $@ EXTRA_CLEAN += test/graph_test + +SURVEY_PYTHON ?= python3 +.PHONY: surveycheck +surveycheck: + PYTHONDONTWRITEBYTECODE=1 $(SURVEY_PYTHON) $(srcdir)/test/survey_test.py diff --git a/gpcontrib/robust_predicate_transfer/README.md b/gpcontrib/robust_predicate_transfer/README.md index ecbe75d7897..95910f690df 100644 --- a/gpcontrib/robust_predicate_transfer/README.md +++ b/gpcontrib/robust_predicate_transfer/README.md @@ -23,3 +23,15 @@ Run `make unitcheck` in this directory after configuring the source tree to exercise synthetic graph, stage, co-location, consumer-order and exception fixtures without starting a server. `make installcheck` additionally needs a running Cloudberry cluster and executes SQL cases under both optimizers. + +Phase II runtime injection is gated by the benchmark eligibility survey in +`plans/rpt-plus-phase2-plan.md`. The stricter analysis and planning-only +`scripts/survey.py` are available; no backward executor nodes are injected yet. +See `docs/design.md` for the survey command and metrics. `make surveycheck` +checks the runner without needing a server. + +The survey requires a superuser or explicit EXECUTE on `rpt_transfer_plan(text)`. +It counts SELECT statements: JOB 113, TPC-H 22 (Q15 in CTE form), TPC-DS 103 +(99 templates, including the four two-SELECT variants). See the normalization +instructions in the design document. `rpt_backward_eligibility` supplies +production-walker/resolver regression coverage under both optimizers. diff --git a/gpcontrib/robust_predicate_transfer/docs/design.md b/gpcontrib/robust_predicate_transfer/docs/design.md index 01cff2be3b2..9c3136ea83e 100644 --- a/gpcontrib/robust_predicate_transfer/docs/design.md +++ b/gpcontrib/robust_predicate_transfer/docs/design.md @@ -18,13 +18,13 @@ planner hook logs the same dump at `DEBUG1` when `rpt.debug` is on. ## Architecture at a glance ``` -dispatch 1 — reducer (Phase III, not implemented yet) - forward chain over predicated + bridge tables, root excluded +Phase II candidate — one statement, one dispatch (survey gate pending) + class-root scan: produce the global class broadcast filter + eligible global members: consume it, with bounded prefetch + local members: core runtime-filter pushdown only -dispatch 2 — the real query (Phase II, not implemented yet) - root scan: consume inbound chain filter from the session cache, - produce the class broadcast filter - others: consume the broadcast filter +Phase III candidate — inside that same statement (not implemented) + Sequence(reducer Append, main plan); no cross-command filter cache ``` Phase I answers the question everything downstream depends on: given this @@ -79,7 +79,7 @@ The backward stage redirects each class to its earliest-inserted member. A non-component-root class leader is a **bridge table**, reported in the dump. Only filtered or bridge tables can produce forward filters. Component roots are final destinations in the forward graph, but never reducer scans: they -consume their inbound cached filter in the main query. `reducer order` lists +would consume their inbound chain filter in the main query in Phase III. `reducer order` lists only scans that actually produce forward filters, in dependency order. `rpt.max_chain_depth` limits the number of dependency hops, including the @@ -177,17 +177,17 @@ filter may only ever *over*-approximate the build side's key set. ## Runtime contract with Anser (Phases II/III) -- The reducer never joins — scan, filter, build — so it fits one slice, one - gang, one dispatch, with the forward chain as an ordered `Append`. -- Global backward edges may use Anser in the main plan only where - `consumer_ok` permits it. Local backward edges use core runtime-filter - pushdown (or an optional non-blocking local filter), with no QD channel, - subscription, network round trip or wait. -- A session-scoped filter cache per QE, keyed by condition_key alone, carries - chain filters across the dispatch boundary and lets the broadcast be - delivered once per segment rather than once per consuming scan. -- Condition keys derive from the semantic identity `(region_id, class_id, - build_rel)`, not a per-query counter. +- Phase II has no reducer: its root builds from its own predicates only, + inside the main statement. Runtime injection is gated by the survey below. +- Anser transport is reused, while its per-join injection is disabled. Plans + carrying Anser per-join nodes are left untouched rather than mixed. +- Global backward edges may use Anser only where strict eligibility permits + it. Local members use core runtime-filter pushdown; RPT injects no local + consumers, QD channels or blocking waits for them. +- Channels are statement-scoped. The broadcast is delivered once per + consuming QE; fan-out is capped per class. There is no session filter cache. +- A future forward reducer must run inside the same statement and dispatch, + preserving the snapshot, parameters and Anser command/channel identity. ### Parallel workers and segment completeness @@ -206,12 +206,109 @@ same completeness requirement applies to any custom local backward producer. Core currently disables its pushdown path in MPP parallel mode, so relying on core there simply means no local filter, rather than a global exchange. +## Phase II eligibility survey gate (in progress) + +The agreed next phase is backward broadcast inside the main statement. It +uses no session-scoped cache and injects no local filters: local edges stay +with core pushdown. Anser transport must be enabled and Anser's per-join +injection disabled; the two injection strategies are alternatives. A class +led by a bridge is deferred. Runtime injection remains **unimplemented** +until Work item 0 of `plans/rpt-plus-phase2-plan.md` has been completed and +its result justifies proceeding. + +The analysis now records a decision for each class/member pair. In addition +to different slices and a producer on a hash-build side, it rejects +multi-tuple prefetch paths and nullable-side crossings. If the qualifying +join builds first (`prefetch_inner` or RIGHT), the prefetch blocker is lifted; +the shared Anser key resolver must still accept the path. This conservatively +rejects unsupported resolver shapes even when scheduling alone is safe. +Every placement under `stmt->subplans` is refused, even below a Motion and +even for an uncorrelated InitPlan, matching Anser's conservative scope. A +non-empty build-side `extParam` is also refused regardless of the parameter +source; publication has no rescan generation protocol yet. Successful decisions record +parents, output-tuple key positions, slice IDs and `defer_first`; physical +column numbers are never assumed to be tuple positions. + +`consumer_ok` is an aggregate over all non-local decisions for a vertex. +Per-edge lines report refusals separately, including local edges, bridge-led +classes, unsupported key paths and the fan-out cap. The `survey` line counts +classes, component-root-led classes, global class/member pairs, structural +rule-9 passes, strict passes before the cap, and eligible consumers after it. +NDV estimates come from `pg_statistic`, with negative fractions scaled by +`pg_class.reltuples`. Their intersection estimate assumes uniform key overlap, +caps root NDV by estimated filtered root rows, and reports missing statistics +as unknown. Estimated removed rows use consumer scan-plan-row units, not a +cluster-total or a correctness claim. A table in multiple classes can be +counted more than once. Unknown-benefit consumers are ranked by their scan +row count as an upper-bound fallback when applying the per-class cap. + +Run the planning-only survey after installing this analysis build and loading +benchmark schemas, data and statistics. `rpt_transfer_plan(text)` has EXECUTE +revoked from PUBLIC, so connect as a superuser or a role explicitly granted +EXECUTE on that function (and SELECT on the benchmark tables). For example, +an administrator can grant `EXECUTE ON FUNCTION public.rpt_transfer_plan(text) +TO rpt_survey`; adjust the function schema if installed elsewhere: + +```sh +python3 scripts/survey.py \ + --job /bench/job/queries --job-dbname imdb \ + --tpch /bench/tpch/queries --tpch-dbname tpch \ + --tpcds /bench/tpcds/queries --tpcds-dbname tpcds \ + --output /tmp/rpt-phase2-survey +``` + +Normalize the benchmark input before running the gate: + +- Use instantiated SQL with the benchmark's original parameters, not templates. +- TPC-DS templates 14, 23, 24 and 39 each contain two SELECT statements. The + runner splits them automatically and records `filename#1` / `filename#2`; + alternatively supply separate `14a.sql` / `14b.sql`, etc. Both layouts + represent **103 SELECT statements from 99 templates**. +- Rewrite TPC-H Q15's `CREATE VIEW revenue... AS ...; SELECT ...; DROP VIEW ...` + as `WITH revenue... AS (...) SELECT ...;`, preserving its view column names, + SELECT body and date parameters. The runner rejects view DDL instead of + executing it or silently excluding Q15. The normalized suite has 22 SELECTs. +- Statement splitting respects quoted strings, identifiers, dollar quotes + and nested comments. The server still parses and validates each statement. + +The tool uses read-only transactions, `rpt.enable=off`, `rpt.backward=off`, +and EXPLAIN without ANALYZE to compare RPT eligibility with Anser's actual +producer-node count. It records per-statement JSON/CSV, source file and +statement ordinal, raw dumps/JSON plans, optimizer identity and suite +summaries. Failures stay in the denominator. Completion is counted by +statements, never by files: 113 JOB, 22 TPC-H and 103 TPC-DS, each under both +optimizer settings. `--allow-partial` is a pilot and cannot satisfy the gate. + +| Suite | SELECT statements | Optimizers | Eligibility vs. Anser | +| --- | --- | --- | --- | +| JOB | 113 | off/on | Not run: benchmark cluster and query set not supplied | +| TPC-H | 22 (Q15 CTE form) | off/on | Not run: benchmark cluster and query set not supplied | +| TPC-DS | 103 (99 templates) | off/on | Not run: benchmark cluster and query set not supplied | + +`rpt_backward_eligibility` is a server regression under both optimizer +settings, using the production plan walker and Anser resolver. It asserts +qualification, local/same-slice and root-on-probe refusals, bridge-led classes, +cap enforcement, parameterized builds and a correlated SubPlan containing a +Motion. Sort and nullable-side expectations follow the actual retained plan +shape: removing those operators is a legitimate optimizer transformation. +Aggregate/window/set-operation boundaries also have server coverage in +`rpt_regions`. Native fixtures additionally cover the individual prefetch +blocker tags and defensive states that valid SQL cannot reliably force. +The SQL suite has been added but has not been executed on a local cluster. + +No eligibility percentages or go/no-go conclusion are available yet. This +section must be updated with measured results before adding runtime nodes. +Phase III's reducer must likewise live inside the same statement (for example +Sequence plus Append); no cross-dispatch state is planned. + ## GUCs | GUC | Default | Meaning | | --- | --- | --- | | `rpt.enable` | off | Master switch for the analysis hook. | | `rpt.debug` | off | Log the transfer plan of every analyzed query at DEBUG1. | +| `rpt.backward` | on | QD-only runtime switch reserved for Phase II; disable during the survey. | +| `rpt.max_backward_consumers` | 4 | QD-only cap on consumers per class, applied to analysis decisions. | | `rpt.max_chain_depth` | 4 | Enforced maximum dependency hops; default provisional until hop latency is measured. | Setup: `shared_preload_libraries = 'anser,robust_predicate_transfer'`, in that diff --git a/gpcontrib/robust_predicate_transfer/expected/rpt_backward_eligibility.out b/gpcontrib/robust_predicate_transfer/expected/rpt_backward_eligibility.out new file mode 100644 index 00000000000..bde24939160 --- /dev/null +++ b/gpcontrib/robust_predicate_transfer/expected/rpt_backward_eligibility.out @@ -0,0 +1,208 @@ +--------------------------------------------------------------------------- +-- +-- Licensed to the Apache Software Foundation (ASF) under one +-- or more contributor license agreements. See the NOTICE file +-- distributed with this work for additional information +-- regarding copyright ownership. The ASF licenses this file +-- to you under the Apache License, Version 2.0 (the +-- "License"); you may not use this file except in compliance +-- with the License. You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, +-- software distributed under the License is distributed on an +-- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +-- KIND, either express or implied. See the License for the +-- specific language governing permissions and limitations +-- under the License. +-- +-- Production walker/resolver coverage. Both optimizer settings run these +-- queries; every assertion resolves vertex IDs from relation names. +CREATE EXTENSION robust_predicate_transfer; +SET rpt.enable = off; +SET anser.runtime_filter = off; +SET enable_nestloop = off; +SET enable_mergejoin = off; +SET join_collapse_limit = 1; +SET optimizer_join_order = query; +SET statement_timeout = '30s'; +CREATE TABLE rpt_elig_root (pad int, k int, v int) DISTRIBUTED BY (k); +CREATE TABLE rpt_elig_member (k int) DISTRIBUTED RANDOMLY; +CREATE TABLE rpt_elig_local (k int) DISTRIBUTED BY (k); +CREATE TABLE rpt_elig_replica (k int) DISTRIBUTED REPLICATED; +CREATE TABLE rpt_elig_outer (k int) DISTRIBUTED BY (k); +CREATE TABLE rpt_elig_bridge (k1 int, k2 int) DISTRIBUTED RANDOMLY; +CREATE TABLE rpt_elig_member2 (k int) DISTRIBUTED RANDOMLY; +CREATE TABLE rpt_elig_member3 (k int) DISTRIBUTED RANDOMLY; +INSERT INTO rpt_elig_root SELECT 0, i, i FROM generate_series(1, 10000) i; +INSERT INTO rpt_elig_member SELECT i FROM generate_series(1, 10000) i; +INSERT INTO rpt_elig_local SELECT i FROM generate_series(1, 10000) i; +INSERT INTO rpt_elig_replica SELECT i FROM generate_series(1, 10000) i; +INSERT INTO rpt_elig_outer SELECT i FROM generate_series(1, 10) i; +INSERT INTO rpt_elig_bridge SELECT i, i FROM generate_series(1, 1000) i; +INSERT INTO rpt_elig_member2 SELECT i FROM generate_series(1, 10000) i; +INSERT INTO rpt_elig_member3 SELECT i FROM generate_series(1, 10000) i; +ANALYZE rpt_elig_root; +ANALYZE rpt_elig_member; +ANALYZE rpt_elig_local; +ANALYZE rpt_elig_replica; +ANALYZE rpt_elig_outer; +ANALYZE rpt_elig_bridge; +ANALYZE rpt_elig_member2; +ANALYZE rpt_elig_member3; +CREATE FUNCTION pg_temp.rpt_expect(q text, producer text, consumer text, + reasons text[], accepted boolean DEFAULT NULL) +RETURNS text LANGUAGE plpgsql AS $$ +DECLARE + dump text := rpt_transfer_plan(q); + producer_id text; + consumer_id text; + decision text; +BEGIN + producer_id := substring(dump FROM ('vertex ([0-9]+) ' || producer || ' rows=')); + consumer_id := substring(dump FROM ('vertex ([0-9]+) ' || consumer || ' rows=')); + IF producer_id IS NULL OR consumer_id IS NULL THEN + RAISE EXCEPTION 'missing fixture scans % / %: %', producer, consumer, dump; + END IF; + SELECT line INTO decision FROM regexp_split_to_table(dump, chr(10)) line + WHERE line LIKE ('backward consumer ' || consumer_id || ' producer ' || producer_id || ' %') + AND substring(line FROM ' reason=(.*)$') = ANY(reasons) + AND (accepted IS NULL OR line LIKE ('% eligible=' || CASE WHEN accepted THEN 'yes' ELSE 'no' END || ' %')) + LIMIT 1; + IF decision IS NULL THEN + RAISE EXCEPTION 'expected % for % -> %, optimizer=%: %', reasons, producer, consumer, current_setting('optimizer'), dump; + END IF; + RETURN dump; +END +$$; +CREATE FUNCTION pg_temp.rpt_explain(q text) RETURNS jsonb LANGUAGE plpgsql AS $$ +DECLARE + plan jsonb; +BEGIN + EXECUTE 'EXPLAIN (FORMAT JSON) ' || q INTO plan; + RETURN plan; +END +$$; +CREATE FUNCTION pg_temp.rpt_scan_path(q text, relation_name text) RETURNS text[] LANGUAGE SQL AS $$ + WITH RECURSIVE walk(node, path) AS ( + SELECT pg_temp.rpt_explain(q)->0->'Plan', ARRAY[]::text[] + UNION ALL + SELECT child, path || ((node->>'Node Type') || ':' || + COALESCE(node->>'Join Type', '') || ':' || COALESCE(child->>'Parent Relationship', '')) + FROM walk CROSS JOIN LATERAL jsonb_array_elements(COALESCE(node->'Plans', '[]'::jsonb)) AS children(child) + ) + SELECT path FROM walk WHERE node->>'Relation Name' = relation_name LIMIT 1 +$$; +DO $$ +DECLARE + opt text; + q text; + dump text; + path text[]; + producer_id text; + correlated text; +BEGIN + FOREACH opt IN ARRAY ARRAY['off', 'on'] LOOP + PERFORM set_config('optimizer', opt, true); + -- Expressions force the real walker through targetlists and quals. + -- k is physical column 2; the resolver must use its output position. + PERFORM pg_temp.rpt_expect( + 'SELECT rpt_elig_root.k + rpt_elig_member.k, rpt_elig_root.v FROM rpt_elig_root JOIN rpt_elig_member USING (k) WHERE rpt_elig_root.v < 100 AND rpt_elig_root.v % 2 = 0', + 'rpt_elig_root', 'rpt_elig_member', ARRAY['eligible'], true); + PERFORM pg_temp.rpt_expect( + 'SELECT * FROM rpt_elig_root JOIN rpt_elig_local USING (k) WHERE rpt_elig_root.v < 100', + 'rpt_elig_root', 'rpt_elig_local', ARRAY['local edge uses core runtime filtering'], false); + PERFORM pg_temp.rpt_expect( + 'SELECT * FROM rpt_elig_root JOIN rpt_elig_member USING (k) WHERE rpt_elig_root.v < 100 AND rpt_elig_member.k > 0', + 'rpt_elig_member', 'rpt_elig_root', ARRAY['producer is not on a qualifying hash build side'], false); + PERFORM pg_temp.rpt_expect( + 'SELECT * FROM rpt_elig_root JOIN rpt_elig_replica USING (k) WHERE rpt_elig_root.v < 100', + 'rpt_elig_root', 'rpt_elig_replica', ARRAY['same or unknown slice'], false); + PERFORM pg_temp.rpt_expect( + 'SELECT * FROM rpt_elig_root LEFT JOIN rpt_elig_member USING (k) WHERE rpt_elig_root.v < 100', + 'rpt_elig_root', 'rpt_elig_member', ARRAY['edge has no unprotected equivalence class'], false); + PERFORM pg_temp.rpt_expect( + 'SELECT * FROM rpt_elig_root JOIN rpt_elig_bridge ON rpt_elig_root.k = rpt_elig_bridge.k1 JOIN rpt_elig_member ON rpt_elig_bridge.k2 = rpt_elig_member.k WHERE rpt_elig_root.v < 9000', + 'rpt_elig_bridge', 'rpt_elig_member', ARRAY['bridge-led class is deferred'], false); + -- ORDER BY can be discarded by ORCA. Assert the refusal only when + -- the Sort remains below the qualifying hash join; otherwise require + -- the unblocked query to qualify rather than silently skipping it. + q := 'SELECT * FROM (SELECT * FROM rpt_elig_member ORDER BY k) rpt_elig_member JOIN rpt_elig_root USING (k) WHERE rpt_elig_root.v < 100'; + path := pg_temp.rpt_scan_path(q, 'rpt_elig_member'); + IF path IS NULL THEN + RAISE EXCEPTION 'sort fixture lost its member scan'; + END IF; + IF EXISTS (SELECT FROM unnest(path) WITH ORDINALITY h(tag, n), unnest(path) WITH ORDINALITY s(tag, n) + WHERE h.tag LIKE 'Hash Join:%:Outer' AND s.tag LIKE 'Sort:%' AND h.n < s.n) THEN + PERFORM pg_temp.rpt_expect(q, 'rpt_elig_root', 'rpt_elig_member', + ARRAY['sort between consumer and join', 'unsupported consumer key path'], false); + ELSE + PERFORM pg_temp.rpt_expect(q, 'rpt_elig_root', 'rpt_elig_member', ARRAY['eligible'], true); + END IF; + -- A strict upper join may simplify the LEFT join to INNER. If it + -- survives, the nullable path must be refused by the production proof. + q := 'SELECT * FROM (rpt_elig_outer LEFT JOIN rpt_elig_member USING (k)) JOIN rpt_elig_root ON rpt_elig_root.k = rpt_elig_member.k WHERE rpt_elig_root.v < 100'; + path := pg_temp.rpt_scan_path(q, 'rpt_elig_member'); + IF path IS NULL THEN + RAISE EXCEPTION 'nullable-side fixture lost its member scan'; + END IF; + IF EXISTS (SELECT FROM unnest(path) tag WHERE tag LIKE '%:Left:Inner' OR tag LIKE '%:Right:Outer') THEN + PERFORM pg_temp.rpt_expect(q, 'rpt_elig_root', 'rpt_elig_member', ARRAY['nullable side of outer join'], false); + ELSE + PERFORM pg_temp.rpt_expect(q, 'rpt_elig_root', 'rpt_elig_member', ARRAY['eligible'], true); + END IF; + -- The aggregate scalar subquery commonly becomes an InitPlan in the + -- PostgreSQL planner. ORCA may instead use a join, without extParam. + q := 'SELECT * FROM rpt_elig_root JOIN rpt_elig_member USING (k) WHERE rpt_elig_root.v < (SELECT max(k) FROM rpt_elig_outer)'; + IF pg_temp.rpt_explain(q)::text LIKE '%InitPlan%' THEN + PERFORM pg_temp.rpt_expect(q, 'rpt_elig_root', 'rpt_elig_member', ARRAY['parameterized producer can be rescanned'], false); + ELSE + PERFORM pg_temp.rpt_expect(q, 'rpt_elig_root', 'rpt_elig_member', + ARRAY['eligible', 'unsupported producer key path', 'producer is not on a qualifying hash build side']); + END IF; + -- Volatility and OFFSET retain a correlated SubPlan instead of + -- allowing a decorrelating join rewrite. Its internal Motion must + -- not undo the explicit SubPlan refusal. + correlated := 'SELECT rpt_elig_outer.k, (SELECT count(*) FROM rpt_elig_root JOIN rpt_elig_member USING (k) WHERE rpt_elig_root.v < rpt_elig_outer.k AND random() < 0.9 OFFSET 0) FROM rpt_elig_outer'; + IF pg_temp.rpt_explain(correlated)::text NOT LIKE '%SubPlan%' THEN + RAISE EXCEPTION 'correlated fixture no longer contains a SubPlan (optimizer=%)', opt; + END IF; + path := pg_temp.rpt_scan_path(correlated, 'rpt_elig_root'); + IF path IS NULL OR NOT EXISTS ( + SELECT FROM unnest(path) WITH ORDINALITY sp(tag, n), unnest(path) WITH ORDINALITY motion(tag, n) + WHERE sp.tag LIKE '%:SubPlan' AND motion.tag LIKE '%Motion:%' AND sp.n < motion.n) THEN + RAISE EXCEPTION 'correlated fixture lost the Motion inside its SubPlan (optimizer=%)', opt; + END IF; + dump := pg_temp.rpt_expect(correlated, 'rpt_elig_root', 'rpt_elig_member', ARRAY['scan inside a SubPlan'], false); + IF EXISTS (SELECT FROM regexp_split_to_table(dump, chr(10)) line + WHERE line LIKE 'backward consumer %' AND line LIKE '% eligible=yes %') THEN + RAISE EXCEPTION 'SubPlan produced an eligible consumer: %', dump; + END IF; + PERFORM set_config('rpt.max_backward_consumers', '1', true); + dump := rpt_transfer_plan('SELECT * FROM rpt_elig_root JOIN rpt_elig_member ON rpt_elig_root.k = rpt_elig_member.k JOIN rpt_elig_member2 ON rpt_elig_root.k = rpt_elig_member2.k JOIN rpt_elig_member3 ON rpt_elig_root.k = rpt_elig_member3.k WHERE rpt_elig_root.v < 100'); + producer_id := substring(dump FROM 'vertex ([0-9]+) rpt_elig_root rows='); + IF producer_id IS NULL OR + NOT EXISTS (SELECT FROM regexp_split_to_table(dump, chr(10)) line + WHERE line LIKE ('backward consumer % producer ' || producer_id || ' %') + AND line LIKE '% reason=backward consumer cap exceeded') OR + (SELECT count(*) FROM regexp_split_to_table(dump, chr(10)) line + WHERE line LIKE ('backward consumer % producer ' || producer_id || ' %') + AND line LIKE '% eligible=yes %') <> 1 THEN + RAISE EXCEPTION 'fan-out cap fixture did not retain exactly one consumer (optimizer=%): %', opt, dump; + END IF; + PERFORM set_config('rpt.max_backward_consumers', '4', true); + END LOOP; +END +$$; +DROP TABLE rpt_elig_root, rpt_elig_member, rpt_elig_local, rpt_elig_replica, + rpt_elig_outer, rpt_elig_bridge, rpt_elig_member2, rpt_elig_member3; +DROP EXTENSION robust_predicate_transfer; +RESET optimizer; +RESET rpt.enable; +RESET anser.runtime_filter; +RESET enable_nestloop; +RESET enable_mergejoin; +RESET join_collapse_limit; +RESET optimizer_join_order; +RESET statement_timeout; diff --git a/gpcontrib/robust_predicate_transfer/include/rpt.h b/gpcontrib/robust_predicate_transfer/include/rpt.h index fe79965a3ce..625157237dc 100644 --- a/gpcontrib/robust_predicate_transfer/include/rpt.h +++ b/gpcontrib/robust_predicate_transfer/include/rpt.h @@ -99,6 +99,8 @@ auto RptPgCall(const F &call) -> decltype(call()) /* GUCs (defined in rptinit.cpp). */ extern bool rpt_enable; extern bool rpt_debug; +extern bool rpt_backward; +extern int rpt_max_backward_consumers; extern int rpt_max_chain_depth; /* @@ -135,6 +137,7 @@ struct RptVertexInfo { int id; /* rtindex of the scan, the vertex identity */ Plan *scan; /* the base scan node */ + Oid relid = InvalidOid; std::string relname; /* RTE alias, for dumps */ int64 cardinality; /* scan->plan_rows */ bool filtered; /* the scan carries restriction quals */ @@ -206,6 +209,28 @@ struct RptGraphNode backward_stage; }; +/* One class/member decision. Relink points and tuple-output positions are + * recorded only after the complete consumer and producer paths are proven. */ +struct RptBackwardConsumer +{ + RptKey class_key = {0, 0}; + int producer = 0; + int consumer = 0; + Plan *producer_parent = NULL; + Plan *consumer_parent = NULL; + AttrNumber producer_attno = 0; + AttrNumber consumer_attno = 0; + int producer_slice = -1; + int consumer_slice = -1; + bool local = false; + bool rule9 = false; + bool strict_eligible = false; + bool eligible = false; + bool defer_first = true; + double estimated_rows_removed = -1; /* unknown without column statistics */ + std::string reason; +}; + /* * The whole analysis. Ports TransferGraphManager; the neighbor matrix is * symmetric (both [a][b] and [b][a] name the same edge) as in the original. @@ -234,6 +259,7 @@ struct RptTransferMgr * the probe-side consumer. Local edges never wait and do not participate; * a vertex with no global in-edges has consumer_ok=false. */ std::map consumer_ok; + std::vector backward_consumers; }; /* rptregion.cpp: extract the join graph from the finished statement. */ @@ -248,6 +274,8 @@ extern void RptBuildClasses(RptTransferMgr *mgr); /* rptplan.cpp: the Prim forest (LargestRootUpdated), forward/backward stage * assignment (CreateTransferPlanUpdated), consumer eligibility, and dumps. */ extern void RptBuildTransferPlan(PlannedStmt *stmt, RptTransferMgr *mgr); +extern void RptCheckBackwardConsumers(PlannedStmt *stmt, RptTransferMgr *mgr); +extern double RptEstimateDistinct(const RptVertexInfo &vertex, AttrNumber attno); extern void RptDumpTransferPlan(const RptTransferMgr *mgr, StringInfo out); #endif /* RPT_H */ diff --git a/gpcontrib/robust_predicate_transfer/scripts/survey.py b/gpcontrib/robust_predicate_transfer/scripts/survey.py new file mode 100644 index 00000000000..49c1c9ce136 --- /dev/null +++ b/gpcontrib/robust_predicate_transfer/scripts/survey.py @@ -0,0 +1,342 @@ +#!/usr/bin/env python3 +#------------------------------------------------------------------------- +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# +"""Plan benchmark queries only; compare RPT eligibility to Anser injection. + +Files may contain multiple SELECT/WITH statements, counted separately. +TPC-H Q15 must use a read-only CTE instead of CREATE/DROP VIEW. +The caller must load schemas/statistics and install/preload both extensions. +No ANALYZE, query execution, schema creation or persistent GUC writes occur. +""" +import argparse +import csv +import json +from pathlib import Path +import re +import subprocess +import uuid + +EXPECTED = {"JOB": 113, "TPC-H": 22, "TPC-DS": 103} +BEGIN_DUMP = "__RPT_SURVEY_DUMP__" +BEGIN_ANSER = "__RPT_SURVEY_ANSER__" +END = "__RPT_SURVEY_END__" + + +def split_select_statements(sql): + """Split instantiated benchmark SQL without splitting quoted semicolons. + + Keep comments in the statement text. Comment-only fragments are ignored. + Reject DDL up front, so standard Q15 gets an actionable normalization error. + This is a lexer, not a SQL parser; the server validates each SELECT later. + """ + statements = [] + start = i = 0 + state = "normal" + depth = 0 + dollar = "" + escape_string = False + first_keyword = None + while i < len(sql): + ch = sql[i] + pair = sql[i:i + 2] + if state == "line": + if ch == "\n": + state = "normal" + elif state == "block": + if pair == "/*": + depth += 1 + i += 1 + elif pair == "*/": + depth -= 1 + i += 1 + if depth == 0: + state = "normal" + elif state == "dollar": + if sql.startswith(dollar, i): + i += len(dollar) - 1 + state = "normal" + elif state in ("single", "double"): + quote = "'" if state == "single" else '"' + if ch == quote: + if i + 1 < len(sql) and sql[i + 1] == quote: + i += 1 + else: + state = "normal" + elif state == "single" and escape_string and ch == "\\": + i += 1 + elif pair == "--": + state = "line" + i += 1 + elif pair == "/*": + state = "block" + depth = 1 + i += 1 + elif ch == ";": + if first_keyword is not None: + statements.append((sql[start:i].strip(), first_keyword)) + start = i + 1 + first_keyword = None + elif not ch.isspace(): + if first_keyword is None: + word = re.match(r"[A-Za-z_]+", sql[i:]) + first_keyword = word.group().upper() if word else "" + if ch == "'": + state = "single" + escape_string = (i > 0 and sql[i - 1] in "eE" and + (i < 2 or not (sql[i - 2].isalnum() or sql[i - 2] == "_"))) + elif ch == '"': + state = "double" + elif ch == "$": + tag = re.match(r"\$(?:[A-Za-z_][A-Za-z0-9_]*)?\$", sql[i:]) + if tag: + dollar = tag.group() + state = "dollar" + i += len(dollar) - 1 + i += 1 + if state not in ("normal", "line"): + raise ValueError("unterminated SQL quote or comment") + if first_keyword is not None: + statements.append((sql[start:].strip(), first_keyword)) + for statement, keyword in statements: + if keyword not in ("SELECT", "WITH"): + raise ValueError("benchmark statements must be SELECT/WITH; use the read-only CTE form of TPC-H Q15 instead of view DDL") + return [statement for statement, keyword in statements] + + +def load_queries(directory): + result = [] + for path in sorted(directory.rglob("*.sql")): + try: + statements = split_select_statements(path.read_text(encoding="utf-8")) + except ValueError as error: + raise ValueError(str(path) + ": " + str(error)) from error + for index, statement in enumerate(statements, 1): + result.append(dict(source_file=str(path.relative_to(directory)), + statement=index, sql=statement)) + return result + + +def sql_literal(value): + return "'" + value.replace("'", "''") + "'" + + +def survey_sql(query, optimizer, timeout_ms, search_path): + tag = "$rpt_" + uuid.uuid4().hex + "$" + query = query.strip().rstrip(";") + return "\n".join([ + "BEGIN READ ONLY;", + "SET LOCAL standard_conforming_strings = on;", + "SET LOCAL statement_timeout = " + str(timeout_ms) + ";", + "SET LOCAL optimizer = " + sql_literal(optimizer) + ";", + "SELECT set_config('search_path', " + sql_literal(search_path) + ", true);", + "SET LOCAL rpt.enable = off;", + "SET LOCAL rpt.backward = off;", + "SET LOCAL anser.enable = on;", + "SET LOCAL anser.runtime_filter = off;", + "SELECT " + sql_literal(BEGIN_DUMP) + ";", + "SELECT rpt_transfer_plan(" + tag + query + tag + ");", + "SELECT " + sql_literal(BEGIN_ANSER) + ";", + "SET LOCAL anser.runtime_filter = on;", + "EXPLAIN (FORMAT JSON) " + query + "\n;", + "SELECT " + sql_literal(END) + ";", + "ROLLBACK;", + ]) + + +def parse_dump(dump): + if dump.strip() == "no transfer regions": + return dict(classes=0, root_classes=0, global_edges=0, rule9=0, + strict=0, eligible=0, known_rows_removed=0.0, + unknown_estimates=0) + lines = [line for line in dump.splitlines() if line.startswith("survey ")] + if len(lines) != 1: + raise ValueError("missing survey metrics; install the analysis/survey build") + metrics = dict(re.findall(r"([a-z0-9_]+)=([^ ]+)", lines[0])) + fields = ("classes", "root_classes", "global_edges", "rule9", "strict", + "eligible", "unknown_estimates") + result = {key: int(metrics[key]) for key in fields} + result["known_rows_removed"] = float(metrics["known_rows_removed"]) + if (any(value < 0 for value in result.values()) or + result["root_classes"] > result["classes"] or + not result["eligible"] <= result["strict"] <= result["rule9"] <= result["global_edges"] or + result["unknown_estimates"] > result["strict"]): + raise ValueError("inconsistent survey metrics") + return result + + +def count_anser_producers(value): + if isinstance(value, dict): + own = int(value.get("Custom Plan Provider") == "Anser Bloom Producer") + return own + sum(count_anser_producers(child) for child in value.values()) + if isinstance(value, list): + return sum(count_anser_producers(child) for child in value) + return 0 + + +def optimizer_used(value): + if isinstance(value, dict): + if "Optimizer" in value: + return str(value["Optimizer"]) + for child in value.values(): + found = optimizer_used(child) + if found: + return found + elif isinstance(value, list): + for child in value: + found = optimizer_used(child) + if found: + return found + return "" + + +def parse_output(output): + before, sep, rest = output.partition(BEGIN_DUMP + "\n") + if not sep: + raise ValueError("missing RPT output marker") + dump, sep, rest = rest.partition(BEGIN_ANSER + "\n") + if not sep: + raise ValueError("missing Anser output marker") + explain, sep, after = rest.partition(END) + if not sep: + raise ValueError("missing completion marker") + plan = json.loads(explain) + metrics = parse_dump(dump) + metrics["anser_joins"] = count_anser_producers(plan) + metrics["anser_optimizer_used"] = optimizer_used(plan) + return metrics, dump, plan + + +def summarize(records, complete): + lines = ["# RPT Phase II eligibility survey", "", + "Survey complete: " + ("yes" if complete else "no"), "", + "Planning only. Each SELECT statement is counted separately, not each file. " + "No benchmark query was executed. Counts labelled " + "strict precede the per-class fan-out cap; eligible follows it.", "", + "| Suite | Optimizer | Planned/total | RPT strict SELECTs | RPT capped SELECTs | Anser SELECTs |", + "| --- | --- | --- | --- | --- | --- |"] + for suite in EXPECTED: + for optimizer in ("off", "on"): + group = [row for row in records if row["suite"] == suite and + row["optimizer"] == optimizer] + good = [row for row in group if row["status"] == "ok"] + strict = sum(row["strict"] > 0 for row in good) + eligible = sum(row["eligible"] > 0 for row in good) + anser = sum(row["anser_joins"] > 0 for row in good) + lines.append("| %s | %s | %d/%d | %d/%d | %d/%d | %d/%d |" % ( + suite, optimizer, len(good), len(group), strict, len(group), + eligible, len(group), anser, len(group))) + lines += ["", "Failures remain in the denominator. See results.json/CSV " + "and raw artifacts for refusals, errors and optimizer fallback.", "", + "NDV estimates are rough: known_rows_removed sums scan-plan-row " + "estimates across strict edges, so classes can double-count a " + "table. unknown_estimates counts missing statistics; it is not zero benefit.", "", + "The survey is evidence for the plan's go/no-go decision; this " + "script never authorizes runtime injection."] + return "\n".join(lines) + "\n" + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + for flag in ("job", "tpch", "tpcds"): + parser.add_argument("--" + flag, required=True, type=Path, + help="directory containing instantiated SELECT/WITH .sql files") + for flag in ("job", "tpch", "tpcds"): + parser.add_argument("--" + flag + "-dbname", help="override database for this suite") + parser.add_argument("--" + flag + "-search-path", help="override search_path for this suite") + parser.add_argument("--dbname", default="postgres", help="database or libpq service; credentials may use PG* environment") + parser.add_argument("--psql", default="psql") + parser.add_argument("--search-path", default="public") + parser.add_argument("--statement-timeout-ms", type=int, default=30000) + parser.add_argument("--output", required=True, type=Path) + parser.add_argument("--allow-partial", action="store_true", help="pilot only; never marks the blocking survey complete") + args = parser.parse_args() + if args.statement_timeout_ms <= 0: + parser.error("statement timeout must be positive") + suites = {"JOB": args.job, "TPC-H": args.tpch, "TPC-DS": args.tpcds} + connections = {"JOB": args.job_dbname or args.dbname, + "TPC-H": args.tpch_dbname or args.dbname, + "TPC-DS": args.tpcds_dbname or args.dbname} + schemas = {"JOB": args.job_search_path or args.search_path, + "TPC-H": args.tpch_search_path or args.search_path, + "TPC-DS": args.tpcds_search_path or args.search_path} + queries = {} + for name, directory in suites.items(): + if not directory.is_dir(): + parser.error("not a query directory: " + str(directory)) + try: + queries[name] = load_queries(directory) + except (OSError, ValueError) as error: + parser.error(str(error)) + if len(queries[name]) != EXPECTED[name] and not args.allow_partial: + parser.error("%s needs %d SELECT statements, found %d; split TPC-DS variants and use Q15 CTE form; --allow-partial is for pilots only" % + (name, EXPECTED[name], len(queries[name]))) + args.output.mkdir(parents=True, exist_ok=True) + raw = args.output / "raw" + raw.mkdir(exist_ok=True) + records = [] + for suite, statements in queries.items(): + for optimizer in ("off", "on"): + for index, statement in enumerate(statements): + identity = "%s-%s-%03d" % (suite, optimizer, index + 1) + row = dict(suite=suite, optimizer=optimizer, + query=statement["source_file"] + "#" + str(statement["statement"]), + source_file=statement["source_file"], + statement=statement["statement"], status="error") + try: + query = statement["sql"] + result = subprocess.run( + [args.psql, "-X", "-q", "-A", "-t", "--no-password", "-v", "ON_ERROR_STOP=1", "-d", connections[suite]], + input=survey_sql(query, optimizer, args.statement_timeout_ms, schemas[suite]), + text=True, capture_output=True, + timeout=2 * args.statement_timeout_ms / 1000 + 15) + if result.returncode: + # Do not record connection strings or arbitrary stderr, + # which can contain credentials. Raw query plans only. + raise ValueError("psql failed (exit %d); inspect the connection/query on the cluster" % result.returncode) + metrics, dump, explain = parse_output(result.stdout) + row.update(metrics) + row["status"] = "ok" + (raw / (identity + ".rpt.txt")).write_text(dump, encoding="utf-8") + (raw / (identity + ".anser.json")).write_text(json.dumps(explain, indent=2), encoding="utf-8") + except (OSError, ValueError, KeyError, subprocess.TimeoutExpired) as error: + if isinstance(error, subprocess.TimeoutExpired): + row["error"] = "psql timed out" + elif isinstance(error, OSError): + row["error"] = type(error).__name__ + else: + row["error"] = str(error) + records.append(row) + print("%s: %s" % (identity, row["status"]), flush=True) + complete = (not args.allow_partial and + all(len(queries[name]) == count for name, count in EXPECTED.items()) and + all(row["status"] == "ok" for row in records)) + report = dict(survey_complete=complete, expected_statements=EXPECTED, results=records) + (args.output / "results.json").write_text(json.dumps(report, indent=2) + "\n", encoding="utf-8") + fields = sorted({field for row in records for field in row}) + with (args.output / "results.csv").open("w", newline="", encoding="utf-8") as stream: + writer = csv.DictWriter(stream, fieldnames=fields) + writer.writeheader() + writer.writerows(records) + (args.output / "summary.md").write_text(summarize(records, complete), encoding="utf-8") + return int(any(row["status"] != "ok" for row in records)) + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/gpcontrib/robust_predicate_transfer/sql/rpt_backward_eligibility.sql b/gpcontrib/robust_predicate_transfer/sql/rpt_backward_eligibility.sql new file mode 100644 index 00000000000..bde24939160 --- /dev/null +++ b/gpcontrib/robust_predicate_transfer/sql/rpt_backward_eligibility.sql @@ -0,0 +1,208 @@ +--------------------------------------------------------------------------- +-- +-- Licensed to the Apache Software Foundation (ASF) under one +-- or more contributor license agreements. See the NOTICE file +-- distributed with this work for additional information +-- regarding copyright ownership. The ASF licenses this file +-- to you under the Apache License, Version 2.0 (the +-- "License"); you may not use this file except in compliance +-- with the License. You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, +-- software distributed under the License is distributed on an +-- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +-- KIND, either express or implied. See the License for the +-- specific language governing permissions and limitations +-- under the License. +-- +-- Production walker/resolver coverage. Both optimizer settings run these +-- queries; every assertion resolves vertex IDs from relation names. +CREATE EXTENSION robust_predicate_transfer; +SET rpt.enable = off; +SET anser.runtime_filter = off; +SET enable_nestloop = off; +SET enable_mergejoin = off; +SET join_collapse_limit = 1; +SET optimizer_join_order = query; +SET statement_timeout = '30s'; +CREATE TABLE rpt_elig_root (pad int, k int, v int) DISTRIBUTED BY (k); +CREATE TABLE rpt_elig_member (k int) DISTRIBUTED RANDOMLY; +CREATE TABLE rpt_elig_local (k int) DISTRIBUTED BY (k); +CREATE TABLE rpt_elig_replica (k int) DISTRIBUTED REPLICATED; +CREATE TABLE rpt_elig_outer (k int) DISTRIBUTED BY (k); +CREATE TABLE rpt_elig_bridge (k1 int, k2 int) DISTRIBUTED RANDOMLY; +CREATE TABLE rpt_elig_member2 (k int) DISTRIBUTED RANDOMLY; +CREATE TABLE rpt_elig_member3 (k int) DISTRIBUTED RANDOMLY; +INSERT INTO rpt_elig_root SELECT 0, i, i FROM generate_series(1, 10000) i; +INSERT INTO rpt_elig_member SELECT i FROM generate_series(1, 10000) i; +INSERT INTO rpt_elig_local SELECT i FROM generate_series(1, 10000) i; +INSERT INTO rpt_elig_replica SELECT i FROM generate_series(1, 10000) i; +INSERT INTO rpt_elig_outer SELECT i FROM generate_series(1, 10) i; +INSERT INTO rpt_elig_bridge SELECT i, i FROM generate_series(1, 1000) i; +INSERT INTO rpt_elig_member2 SELECT i FROM generate_series(1, 10000) i; +INSERT INTO rpt_elig_member3 SELECT i FROM generate_series(1, 10000) i; +ANALYZE rpt_elig_root; +ANALYZE rpt_elig_member; +ANALYZE rpt_elig_local; +ANALYZE rpt_elig_replica; +ANALYZE rpt_elig_outer; +ANALYZE rpt_elig_bridge; +ANALYZE rpt_elig_member2; +ANALYZE rpt_elig_member3; +CREATE FUNCTION pg_temp.rpt_expect(q text, producer text, consumer text, + reasons text[], accepted boolean DEFAULT NULL) +RETURNS text LANGUAGE plpgsql AS $$ +DECLARE + dump text := rpt_transfer_plan(q); + producer_id text; + consumer_id text; + decision text; +BEGIN + producer_id := substring(dump FROM ('vertex ([0-9]+) ' || producer || ' rows=')); + consumer_id := substring(dump FROM ('vertex ([0-9]+) ' || consumer || ' rows=')); + IF producer_id IS NULL OR consumer_id IS NULL THEN + RAISE EXCEPTION 'missing fixture scans % / %: %', producer, consumer, dump; + END IF; + SELECT line INTO decision FROM regexp_split_to_table(dump, chr(10)) line + WHERE line LIKE ('backward consumer ' || consumer_id || ' producer ' || producer_id || ' %') + AND substring(line FROM ' reason=(.*)$') = ANY(reasons) + AND (accepted IS NULL OR line LIKE ('% eligible=' || CASE WHEN accepted THEN 'yes' ELSE 'no' END || ' %')) + LIMIT 1; + IF decision IS NULL THEN + RAISE EXCEPTION 'expected % for % -> %, optimizer=%: %', reasons, producer, consumer, current_setting('optimizer'), dump; + END IF; + RETURN dump; +END +$$; +CREATE FUNCTION pg_temp.rpt_explain(q text) RETURNS jsonb LANGUAGE plpgsql AS $$ +DECLARE + plan jsonb; +BEGIN + EXECUTE 'EXPLAIN (FORMAT JSON) ' || q INTO plan; + RETURN plan; +END +$$; +CREATE FUNCTION pg_temp.rpt_scan_path(q text, relation_name text) RETURNS text[] LANGUAGE SQL AS $$ + WITH RECURSIVE walk(node, path) AS ( + SELECT pg_temp.rpt_explain(q)->0->'Plan', ARRAY[]::text[] + UNION ALL + SELECT child, path || ((node->>'Node Type') || ':' || + COALESCE(node->>'Join Type', '') || ':' || COALESCE(child->>'Parent Relationship', '')) + FROM walk CROSS JOIN LATERAL jsonb_array_elements(COALESCE(node->'Plans', '[]'::jsonb)) AS children(child) + ) + SELECT path FROM walk WHERE node->>'Relation Name' = relation_name LIMIT 1 +$$; +DO $$ +DECLARE + opt text; + q text; + dump text; + path text[]; + producer_id text; + correlated text; +BEGIN + FOREACH opt IN ARRAY ARRAY['off', 'on'] LOOP + PERFORM set_config('optimizer', opt, true); + -- Expressions force the real walker through targetlists and quals. + -- k is physical column 2; the resolver must use its output position. + PERFORM pg_temp.rpt_expect( + 'SELECT rpt_elig_root.k + rpt_elig_member.k, rpt_elig_root.v FROM rpt_elig_root JOIN rpt_elig_member USING (k) WHERE rpt_elig_root.v < 100 AND rpt_elig_root.v % 2 = 0', + 'rpt_elig_root', 'rpt_elig_member', ARRAY['eligible'], true); + PERFORM pg_temp.rpt_expect( + 'SELECT * FROM rpt_elig_root JOIN rpt_elig_local USING (k) WHERE rpt_elig_root.v < 100', + 'rpt_elig_root', 'rpt_elig_local', ARRAY['local edge uses core runtime filtering'], false); + PERFORM pg_temp.rpt_expect( + 'SELECT * FROM rpt_elig_root JOIN rpt_elig_member USING (k) WHERE rpt_elig_root.v < 100 AND rpt_elig_member.k > 0', + 'rpt_elig_member', 'rpt_elig_root', ARRAY['producer is not on a qualifying hash build side'], false); + PERFORM pg_temp.rpt_expect( + 'SELECT * FROM rpt_elig_root JOIN rpt_elig_replica USING (k) WHERE rpt_elig_root.v < 100', + 'rpt_elig_root', 'rpt_elig_replica', ARRAY['same or unknown slice'], false); + PERFORM pg_temp.rpt_expect( + 'SELECT * FROM rpt_elig_root LEFT JOIN rpt_elig_member USING (k) WHERE rpt_elig_root.v < 100', + 'rpt_elig_root', 'rpt_elig_member', ARRAY['edge has no unprotected equivalence class'], false); + PERFORM pg_temp.rpt_expect( + 'SELECT * FROM rpt_elig_root JOIN rpt_elig_bridge ON rpt_elig_root.k = rpt_elig_bridge.k1 JOIN rpt_elig_member ON rpt_elig_bridge.k2 = rpt_elig_member.k WHERE rpt_elig_root.v < 9000', + 'rpt_elig_bridge', 'rpt_elig_member', ARRAY['bridge-led class is deferred'], false); + -- ORDER BY can be discarded by ORCA. Assert the refusal only when + -- the Sort remains below the qualifying hash join; otherwise require + -- the unblocked query to qualify rather than silently skipping it. + q := 'SELECT * FROM (SELECT * FROM rpt_elig_member ORDER BY k) rpt_elig_member JOIN rpt_elig_root USING (k) WHERE rpt_elig_root.v < 100'; + path := pg_temp.rpt_scan_path(q, 'rpt_elig_member'); + IF path IS NULL THEN + RAISE EXCEPTION 'sort fixture lost its member scan'; + END IF; + IF EXISTS (SELECT FROM unnest(path) WITH ORDINALITY h(tag, n), unnest(path) WITH ORDINALITY s(tag, n) + WHERE h.tag LIKE 'Hash Join:%:Outer' AND s.tag LIKE 'Sort:%' AND h.n < s.n) THEN + PERFORM pg_temp.rpt_expect(q, 'rpt_elig_root', 'rpt_elig_member', + ARRAY['sort between consumer and join', 'unsupported consumer key path'], false); + ELSE + PERFORM pg_temp.rpt_expect(q, 'rpt_elig_root', 'rpt_elig_member', ARRAY['eligible'], true); + END IF; + -- A strict upper join may simplify the LEFT join to INNER. If it + -- survives, the nullable path must be refused by the production proof. + q := 'SELECT * FROM (rpt_elig_outer LEFT JOIN rpt_elig_member USING (k)) JOIN rpt_elig_root ON rpt_elig_root.k = rpt_elig_member.k WHERE rpt_elig_root.v < 100'; + path := pg_temp.rpt_scan_path(q, 'rpt_elig_member'); + IF path IS NULL THEN + RAISE EXCEPTION 'nullable-side fixture lost its member scan'; + END IF; + IF EXISTS (SELECT FROM unnest(path) tag WHERE tag LIKE '%:Left:Inner' OR tag LIKE '%:Right:Outer') THEN + PERFORM pg_temp.rpt_expect(q, 'rpt_elig_root', 'rpt_elig_member', ARRAY['nullable side of outer join'], false); + ELSE + PERFORM pg_temp.rpt_expect(q, 'rpt_elig_root', 'rpt_elig_member', ARRAY['eligible'], true); + END IF; + -- The aggregate scalar subquery commonly becomes an InitPlan in the + -- PostgreSQL planner. ORCA may instead use a join, without extParam. + q := 'SELECT * FROM rpt_elig_root JOIN rpt_elig_member USING (k) WHERE rpt_elig_root.v < (SELECT max(k) FROM rpt_elig_outer)'; + IF pg_temp.rpt_explain(q)::text LIKE '%InitPlan%' THEN + PERFORM pg_temp.rpt_expect(q, 'rpt_elig_root', 'rpt_elig_member', ARRAY['parameterized producer can be rescanned'], false); + ELSE + PERFORM pg_temp.rpt_expect(q, 'rpt_elig_root', 'rpt_elig_member', + ARRAY['eligible', 'unsupported producer key path', 'producer is not on a qualifying hash build side']); + END IF; + -- Volatility and OFFSET retain a correlated SubPlan instead of + -- allowing a decorrelating join rewrite. Its internal Motion must + -- not undo the explicit SubPlan refusal. + correlated := 'SELECT rpt_elig_outer.k, (SELECT count(*) FROM rpt_elig_root JOIN rpt_elig_member USING (k) WHERE rpt_elig_root.v < rpt_elig_outer.k AND random() < 0.9 OFFSET 0) FROM rpt_elig_outer'; + IF pg_temp.rpt_explain(correlated)::text NOT LIKE '%SubPlan%' THEN + RAISE EXCEPTION 'correlated fixture no longer contains a SubPlan (optimizer=%)', opt; + END IF; + path := pg_temp.rpt_scan_path(correlated, 'rpt_elig_root'); + IF path IS NULL OR NOT EXISTS ( + SELECT FROM unnest(path) WITH ORDINALITY sp(tag, n), unnest(path) WITH ORDINALITY motion(tag, n) + WHERE sp.tag LIKE '%:SubPlan' AND motion.tag LIKE '%Motion:%' AND sp.n < motion.n) THEN + RAISE EXCEPTION 'correlated fixture lost the Motion inside its SubPlan (optimizer=%)', opt; + END IF; + dump := pg_temp.rpt_expect(correlated, 'rpt_elig_root', 'rpt_elig_member', ARRAY['scan inside a SubPlan'], false); + IF EXISTS (SELECT FROM regexp_split_to_table(dump, chr(10)) line + WHERE line LIKE 'backward consumer %' AND line LIKE '% eligible=yes %') THEN + RAISE EXCEPTION 'SubPlan produced an eligible consumer: %', dump; + END IF; + PERFORM set_config('rpt.max_backward_consumers', '1', true); + dump := rpt_transfer_plan('SELECT * FROM rpt_elig_root JOIN rpt_elig_member ON rpt_elig_root.k = rpt_elig_member.k JOIN rpt_elig_member2 ON rpt_elig_root.k = rpt_elig_member2.k JOIN rpt_elig_member3 ON rpt_elig_root.k = rpt_elig_member3.k WHERE rpt_elig_root.v < 100'); + producer_id := substring(dump FROM 'vertex ([0-9]+) rpt_elig_root rows='); + IF producer_id IS NULL OR + NOT EXISTS (SELECT FROM regexp_split_to_table(dump, chr(10)) line + WHERE line LIKE ('backward consumer % producer ' || producer_id || ' %') + AND line LIKE '% reason=backward consumer cap exceeded') OR + (SELECT count(*) FROM regexp_split_to_table(dump, chr(10)) line + WHERE line LIKE ('backward consumer % producer ' || producer_id || ' %') + AND line LIKE '% eligible=yes %') <> 1 THEN + RAISE EXCEPTION 'fan-out cap fixture did not retain exactly one consumer (optimizer=%): %', opt, dump; + END IF; + PERFORM set_config('rpt.max_backward_consumers', '4', true); + END LOOP; +END +$$; +DROP TABLE rpt_elig_root, rpt_elig_member, rpt_elig_local, rpt_elig_replica, + rpt_elig_outer, rpt_elig_bridge, rpt_elig_member2, rpt_elig_member3; +DROP EXTENSION robust_predicate_transfer; +RESET optimizer; +RESET rpt.enable; +RESET anser.runtime_filter; +RESET enable_nestloop; +RESET enable_mergejoin; +RESET join_collapse_limit; +RESET optimizer_join_order; +RESET statement_timeout; diff --git a/gpcontrib/robust_predicate_transfer/src/rpteligible.cpp b/gpcontrib/robust_predicate_transfer/src/rpteligible.cpp new file mode 100644 index 00000000000..cd9bc3b392d --- /dev/null +++ b/gpcontrib/robust_predicate_transfer/src/rpteligible.cpp @@ -0,0 +1,425 @@ +/*------------------------------------------------------------------------- + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * rpteligible.cpp + * Analysis-only backward eligibility and refusal diagnostics. These + * proofs are shared by the survey and the future Phase II injector. + * + * IDENTIFICATION + * gpcontrib/robust_predicate_transfer/src/rpteligible.cpp + * + *------------------------------------------------------------------------- + */ +extern "C" +{ +#include "postgres.h" +#include "optimizer/walkers.h" +#include "anserplan.h" +} + +#include "rpt.h" +#include + +struct RptPlacement +{ + std::vector path; + int slice = -1; + bool ambiguous = false; + bool in_subplan = false; +}; + +struct RptPlacementContext +{ + plan_tree_base_prefix base; + std::set scans; + std::map placements; + std::vector path; + int slice = 0; + bool in_subplan = false; +}; + +/* Expressions are visited too, but a scan's ancestor path contains only + * plans and plan lists. Omit lists so the previous entry is its relink parent. */ +static bool +rpt_placement_walker(Node *node, void *context) +{ + auto *ctx = static_cast(context); + if (node == NULL) + return false; + bool list = IsA(node, List) || IsA(node, IntList) || IsA(node, OidList); + int saved_slice = ctx->slice; + if (IsA(node, Motion)) + ctx->slice = ((Motion *) node)->motionID; + if (!list) + ctx->path.push_back((Plan *) node); + if (ctx->scans.count((Plan *) node)) + { + auto &placement = ctx->placements[(Plan *) node]; + placement.in_subplan = placement.in_subplan || ctx->in_subplan; + if (!placement.path.empty()) + placement.ambiguous = true; + else + { + placement.path = ctx->path; + placement.slice = ctx->slice; + } + } + bool result = RptPgCall([&]() { + return plan_tree_walker(node, rpt_placement_walker, context, false); + }); + if (!list) + ctx->path.pop_back(); + ctx->slice = saved_slice; + return result; +} + +/* Map a base column upward through identity/projection Vars. Do not guess + * that a relation's physical attno is also its output-tuple position. */ +static AttrNumber +rpt_path_key_slot(const RptPlacement &placement, size_t top, const RptKey &key) +{ + Plan *scan = placement.path.back(); + AttrNumber slot = 0; + ListCell *lc; + + if (!IsA(scan, SeqScan)) + return 0; /* Anser's builders/resolver support SeqScan endpoints. */ + foreach(lc, scan->targetlist) + { + auto *tle = (TargetEntry *) lfirst(lc); + if (IsA(tle->expr, Var)) + { + auto *var = (Var *) tle->expr; + if (var->varno == key.rtindex && var->varattno == key.attno) + { + slot = tle->resno; + break; + } + } + } + for (size_t i = placement.path.size() - 1; slot > 0 && i > top; --i) + { + Plan *parent = placement.path[i - 1]; + Plan *child = placement.path[i]; + int varno = 0; + if (outerPlan(parent) == child) + varno = OUTER_VAR; + else if (innerPlan(parent) == child) + varno = INNER_VAR; + else if (AnserIsRuntimeFilterScan(parent)) + varno = INDEX_VAR; + AttrNumber mapped = 0; + foreach(lc, parent->targetlist) + { + auto *tle = (TargetEntry *) lfirst(lc); + if (IsA(tle->expr, Var)) + { + auto *var = (Var *) tle->expr; + if (varno != 0 && var->varno == varno && var->varattno == slot) + { + mapped = tle->resno; + break; + } + } + } + slot = mapped; + } + return slot; +} + +static int +rpt_path_slice(const RptPlacement &placement, size_t top) +{ + int slice = 0; + for (size_t i = 0; i <= top; ++i) + if (IsA(placement.path[i], Motion)) + slice = ((Motion *) placement.path[i])->motionID; + return slice; +} + +static bool +rpt_resolve_placement(const RptPlacement &placement, size_t top, + const RptKey &key, bool consumer, + Plan **parent, AttrNumber *attno, int *slice) +{ + AttrNumber slot = rpt_path_key_slot(placement, top, key); + if (slot <= 0 || placement.path.size() < 2) + return false; + Plan *scan = placement.path.back(); + Plan *resolved = NULL; + if (top == placement.path.size() - 1) + { + *parent = placement.path[top - 1]; + *attno = slot; + *slice = placement.slice; + return outerPlan(*parent) == scan; + } + bool ok = RptPgCall([&]() { + return AnserResolveKeyScan(placement.path[top], slot, + rpt_path_slice(placement, top), consumer, parent, &resolved, attno, slice); + }); + return ok && resolved == scan && *slice == placement.slice; +} + +static const char * +rpt_consumer_path_reason(const RptPlacement &placement, size_t join, + bool builds_first) +{ + for (size_t i = join + 1; i + 1 < placement.path.size(); ++i) + { + Plan *node = placement.path[i]; + Plan *child = placement.path[i + 1]; + if (IsA(node, HashJoin) || IsA(node, MergeJoin) || IsA(node, NestLoop)) + { + JoinType type = ((Join *) node)->jointype; + bool inner = innerPlan(node) == child; + if ((type == JOIN_LEFT && inner) || (type == JOIN_RIGHT && !inner) || type == JOIN_FULL) + return "nullable side of outer join"; + if (type != JOIN_INNER && type != JOIN_LEFT && type != JOIN_RIGHT) + return "unsupported join on consumer path"; + } + } + if (builds_first) + return NULL; + for (size_t i = join + 1; i + 1 < placement.path.size(); ++i) + { + switch (nodeTag(placement.path[i])) + { + case T_Sort: return "sort between consumer and join"; + case T_Agg: return "aggregate between consumer and join"; + case T_Material: return "material between consumer and join"; + case T_Unique: return "unique between consumer and join"; + case T_WindowAgg: return "window between consumer and join"; + case T_Hash: return "hash between consumer and join"; + case T_SetOp: return "set operation between consumer and join"; + case T_HashJoin: + case T_MergeJoin: + case T_NestLoop: return "join between consumer and join"; + default: break; + } + } + return NULL; +} + +/* Anser retains its publication across rescans. Any external parameter, + * including SubPlan correlation and InitPlan parameters, is conservatively + * refused until the runtime has a generation/rebuild protocol. */ +static bool +rpt_parameterized_producer(Plan *build) +{ + return !RptPgCall([&]() { return bms_is_empty(build->extParam); }); +} + +static void +rpt_prove_consumer(RptBackwardConsumer *decision, const RptKey &producer_key, + const RptKey &consumer_key, const RptPlacement &producer, + const RptPlacement &consumer) +{ + decision->reason = "producer is not on a qualifying hash build side"; + if (producer.in_subplan || consumer.in_subplan) + { + decision->reason = "scan inside a SubPlan"; + return; + } + if (producer.ambiguous || consumer.ambiguous) + { + decision->reason = "scan has multiple plan placements"; + return; + } + if (producer.slice < 0 || consumer.slice < 0 || producer.slice == consumer.slice) + { + decision->reason = "same or unknown slice"; + return; + } + for (size_t i = 0; i + 1 < consumer.path.size(); ++i) + { + Plan *node = consumer.path[i]; + if (!IsA(node, HashJoin) || consumer.path[i + 1] != outerPlan(node)) + continue; + auto *join = (HashJoin *) node; + auto build = std::find(producer.path.begin(), producer.path.end(), innerPlan(node)); + if (build == producer.path.end()) + continue; + decision->rule9 = true; + if (join->join.jointype != JOIN_INNER && join->join.jointype != JOIN_RIGHT) + { + decision->reason = "unsupported qualifying hash join"; + continue; + } + bool builds_first = join->join.prefetch_inner || join->join.jointype == JOIN_RIGHT; + const char *reason = rpt_consumer_path_reason(consumer, i, builds_first); + if (reason != NULL) + { + decision->reason = reason; + continue; + } + if (rpt_parameterized_producer(innerPlan(node))) + { + decision->reason = "parameterized producer can be rescanned"; + continue; + } + if (!rpt_resolve_placement(producer, build - producer.path.begin(), producer_key, + false, &decision->producer_parent, + &decision->producer_attno, &decision->producer_slice)) + { + decision->reason = "unsupported producer key path"; + continue; + } + if (!rpt_resolve_placement(consumer, i + 1, consumer_key, true, + &decision->consumer_parent, &decision->consumer_attno, + &decision->consumer_slice)) + { + decision->reason = "unsupported consumer key path"; + continue; + } + decision->defer_first = !builds_first; + decision->strict_eligible = decision->eligible = true; + decision->reason = "eligible"; + return; + } +} + +static void +rpt_cap_backward_consumers(RptTransferMgr *mgr) +{ + std::map > classes; + for (size_t i = 0; i < mgr->backward_consumers.size(); ++i) + if (mgr->backward_consumers[i].strict_eligible) + classes[mgr->backward_consumers[i].class_key].push_back(i); + for (auto &group : classes) + { + auto &indices = group.second; + std::stable_sort(indices.begin(), indices.end(), [&](size_t a, size_t b) { + const auto &left = mgr->backward_consumers[a]; + const auto &right = mgr->backward_consumers[b]; + double l = left.estimated_rows_removed >= 0 ? left.estimated_rows_removed : + mgr->vertices[mgr->by_rti.at(left.consumer)].cardinality; + double r = right.estimated_rows_removed >= 0 ? right.estimated_rows_removed : + mgr->vertices[mgr->by_rti.at(right.consumer)].cardinality; + return l > r || (l == r && left.consumer < right.consumer); + }); + for (size_t i = rpt_max_backward_consumers; i < indices.size(); ++i) + { + auto &decision = mgr->backward_consumers[indices[i]]; + decision.eligible = false; + decision.reason = "backward consumer cap exceeded"; + } + } +} + +static void +rpt_consumer_verdicts(RptTransferMgr *mgr) +{ + for (const auto &vertex : mgr->vertices) + mgr->consumer_ok[vertex.id] = false; + std::map all_global; + for (const auto &decision : mgr->backward_consumers) + { + if (decision.local) + continue; + auto found = all_global.find(decision.consumer); + if (found == all_global.end()) + all_global[decision.consumer] = decision.eligible; + else + found->second = found->second && decision.eligible; + } + for (const auto &entry : all_global) + mgr->consumer_ok[entry.first] = entry.second; +} + +void +RptCheckBackwardConsumers(PlannedStmt *stmt, RptTransferMgr *mgr) +{ + RptPlacementContext context; + for (const auto &vertex : mgr->vertices) + { + mgr->consumer_ok[vertex.id] = false; + context.scans.insert(vertex.scan); + } + mgr->backward_consumers.clear(); + if (stmt == NULL || stmt->planTree == NULL) + return; + RptPgCall([&]() { exec_init_plan_tree_base(&context.base, stmt); }); + rpt_placement_walker((Node *) stmt->planTree, &context); + ListCell *lc; + foreach(lc, stmt->subplans) + { + context.slice = -1; + context.in_subplan = true; /* Motions must not clear the SubPlan refusal. */ + rpt_placement_walker((Node *) lfirst(lc), &context); + } + std::set > seen; + for (const auto &node : mgr->graph) + { + for (const auto &edge : node.second.backward_stage.out) + { + for (size_t key = 0; key < edge.left_keys.size(); ++key) + { + RptKey source = edge.right_keys[key]; + RptKey target = edge.left_keys[key]; + RptKey rep = RptFindKeyRoot(source, mgr->uf_parent); + if (!seen.insert(std::make_pair(edge.destination, rep)).second) + continue; + RptBackwardConsumer decision; + decision.class_key = rep; + decision.producer = node.first; + decision.consumer = edge.destination; + decision.local = edge.local; + auto group = mgr->groups.find(rep); + auto source_placement = context.placements.find(mgr->vertices[mgr->by_rti.at(node.first)].scan); + auto target_placement = context.placements.find(mgr->vertices[mgr->by_rti.at(edge.destination)].scan); + if ((source_placement != context.placements.end() && source_placement->second.in_subplan) || + (target_placement != context.placements.end() && target_placement->second.in_subplan)) + decision.reason = "scan inside a SubPlan"; + else if (edge.local) + decision.reason = "local edge uses core runtime filtering"; + else if (group == mgr->groups.end() || group->second.leader_id != node.first) + decision.reason = "edge has no unprotected equivalence class"; + else if (!mgr->roots.count(node.first)) + decision.reason = "bridge-led class is deferred"; + else if (!context.placements.count(mgr->vertices[mgr->by_rti.at(node.first)].scan) || + !context.placements.count(mgr->vertices[mgr->by_rti.at(edge.destination)].scan)) + decision.reason = "scan placement not found"; + else + { + const auto &producer = mgr->vertices[mgr->by_rti.at(node.first)]; + const auto &consumer = mgr->vertices[mgr->by_rti.at(edge.destination)]; + rpt_prove_consumer(&decision, source, target, + context.placements.at(producer.scan), context.placements.at(consumer.scan)); + if (decision.strict_eligible) + { + double root_ndv = RptEstimateDistinct(producer, source.attno); + double member_ndv = RptEstimateDistinct(consumer, target.attno); + if (root_ndv > 0 && member_ndv > 0) + { + double root_rows = producer.cardinality * + (producer.replicated ? 1.0 : std::max(1, producer.numsegments)); + root_ndv = std::min(root_ndv, root_rows); + decision.estimated_rows_removed = consumer.cardinality * + (1.0 - std::min(1.0, root_ndv / member_ndv)); + } + } + } + mgr->backward_consumers.push_back(decision); + } + } + } + rpt_cap_backward_consumers(mgr); + rpt_consumer_verdicts(mgr); +} diff --git a/gpcontrib/robust_predicate_transfer/src/rptinit.cpp b/gpcontrib/robust_predicate_transfer/src/rptinit.cpp index 2a6775daaf3..86495050d0e 100644 --- a/gpcontrib/robust_predicate_transfer/src/rptinit.cpp +++ b/gpcontrib/robust_predicate_transfer/src/rptinit.cpp @@ -64,6 +64,8 @@ void _PG_init(void); bool rpt_enable = false; bool rpt_debug = false; +bool rpt_backward = true; +int rpt_max_backward_consumers = 4; int rpt_max_chain_depth = 4; static planner_hook_type prev_planner_hook = NULL; @@ -166,6 +168,18 @@ rpt_define_gucs(void) GUC_GPDB_NEED_SYNC, NULL, NULL, NULL); + DefineCustomBoolVariable("rpt.backward", + "Reserves the backward runtime switch for a future Phase II build.", + "QD-only; this analysis-only build never injects executor nodes.", + &rpt_backward, true, PGC_USERSET, 0, + NULL, NULL, NULL); + + DefineCustomIntVariable("rpt.max_backward_consumers", + "Caps backward consumers per equivalence class.", + "QD-only; prefer consumers with larger estimated benefit.", + &rpt_max_backward_consumers, 4, 1, INT_MAX, + PGC_USERSET, 0, NULL, NULL, NULL); + DefineCustomIntVariable("rpt.max_chain_depth", "Sets the maximum forward-chain length RPT+ may plan.", "Global hops require publish/merge/deliver barriers, so chain depth is capped; the default is a placeholder until hop latency is measured.", diff --git a/gpcontrib/robust_predicate_transfer/src/rptplan.cpp b/gpcontrib/robust_predicate_transfer/src/rptplan.cpp index 99a488fb5f9..872714752d9 100644 --- a/gpcontrib/robust_predicate_transfer/src/rptplan.cpp +++ b/gpcontrib/robust_predicate_transfer/src/rptplan.cpp @@ -492,163 +492,6 @@ rpt_classify_hops(RptTransferMgr *mgr) } } -/* - * Backward-consumer eligibility: Anser's deadlock rule, with no counterpart - * in the single-node artifact. A live backward consumer blocks until its - * producer's filter arrives, so it may only wait on a producer the plan - * order guarantees runs first: the producer's scan on the build (inner) - * side of a hash join and the consumer's scan on the probe (outer) side of - * the same join, in different slices (Anser refuses same-slice exchanges). - * consumer_ok[v] is true only when every GLOBAL backward in-edge satisfies - * that condition. Local edges bypass Anser and never wait; vertices with no - * global in-edges keep consumer_ok=false. - * - * The contexts are prefixed with plan_tree_base_prefix because - * plan_tree_walker resolves SubPlan nodes through it, even when not - * recursing into subplans. - */ -typedef struct RptScanCollectContext -{ - plan_tree_base_prefix base; /* must be first */ - const std::map *scan_vertex; - std::map slices; - int slice_index; -} RptScanCollectContext; - -typedef struct RptConsumerCheckContext -{ - plan_tree_base_prefix base; /* must be first */ - PlannedStmt *stmt; - int slice_index; - const std::map *scan_vertex; - const std::map > *producers; - std::map > *safe_producers; -} RptConsumerCheckContext; - -/* Collect each vertex scan and the slice where it executes. */ -static bool -rpt_collect_scan_ids_walker(Node *node, void *context) -{ - RptScanCollectContext *ctx = (RptScanCollectContext *) context; - std::map::const_iterator it; - - if (node == NULL) - return false; - - /* vertex scans match by pointer; non-Plan nodes simply never hit */ - it = ctx->scan_vertex->find((const Plan *) node); - int saved_slice = ctx->slice_index; - if (IsA(node, Motion)) - ctx->slice_index = ((Motion *) node)->motionID; - if (it != ctx->scan_vertex->end()) - ctx->slices[it->second] = ctx->slice_index; - - bool result = RptPgCall([&]() { return plan_tree_walker(node, rpt_collect_scan_ids_walker, context, false); }); - ctx->slice_index = saved_slice; - return result; -} - -static bool -rpt_consumer_check_walker(Node *node, void *context) -{ - RptConsumerCheckContext *ctx = (RptConsumerCheckContext *) context; - - if (node == NULL) - return false; - - int saved_slice = ctx->slice_index; - if (IsA(node, Motion)) - ctx->slice_index = ((Motion *) node)->motionID; - - if (IsA(node, HashJoin)) - { - HashJoin *hj = (HashJoin *) node; - RptScanCollectContext inner_ctx; - RptScanCollectContext outer_ctx; - std::map >::const_iterator pit; - - RptPgCall([&]() { return exec_init_plan_tree_base(&inner_ctx.base, ctx->stmt); }); - inner_ctx.scan_vertex = ctx->scan_vertex; - inner_ctx.slice_index = ctx->slice_index; - RptPgCall([&]() { return exec_init_plan_tree_base(&outer_ctx.base, ctx->stmt); }); - outer_ctx.scan_vertex = ctx->scan_vertex; - outer_ctx.slice_index = ctx->slice_index; - - /* build side is the inner subtree, probe side the outer */ - rpt_collect_scan_ids_walker((Node *) innerPlan(hj), &inner_ctx); - rpt_collect_scan_ids_walker((Node *) outerPlan(hj), &outer_ctx); - - for (pit = ctx->producers->begin(); pit != ctx->producers->end(); ++pit) - { - int consumer = pit->first; - std::set::const_iterator prod; - - if (!outer_ctx.slices.count(consumer)) - continue; - for (prod = pit->second.begin(); prod != pit->second.end(); ++prod) - { - if (inner_ctx.slices.count(*prod) && - inner_ctx.slices.at(*prod) >= 0 && - outer_ctx.slices.at(consumer) >= 0 && - inner_ctx.slices.at(*prod) != outer_ctx.slices.at(consumer)) - { - (*ctx->safe_producers)[consumer].insert(*prod); - } - } - } - } - - bool result = RptPgCall([&]() { return plan_tree_walker(node, rpt_consumer_check_walker, context, false); }); - ctx->slice_index = saved_slice; - return result; -} - -static void -rpt_check_consumers(PlannedStmt *stmt, RptTransferMgr *mgr) -{ - std::map scan_vertex; - std::map > producers; - std::map > safe_producers; - RptConsumerCheckContext ctx; - size_t i; - std::map::const_iterator nit; - - for (i = 0; i < mgr->vertices.size(); i++) - { - mgr->consumer_ok[mgr->vertices[i].id] = false; - scan_vertex[mgr->vertices[i].scan] = mgr->vertices[i].id; - } - - /* - * The producer of a backward in-edge is its destination: the class - * leader for a redirected edge, the larger table otherwise. Local filters - * are non-blocking and never need Anser consumer eligibility. - */ - for (nit = mgr->graph.begin(); nit != mgr->graph.end(); ++nit) - { - const std::vector &ins = nit->second.backward_stage.in; - - for (i = 0; i < ins.size(); i++) - if (!ins[i].local) - producers[nit->first].insert(ins[i].destination); - } - - if (producers.empty() || stmt == NULL || stmt->planTree == NULL) - return; - - RptPgCall([&]() { return exec_init_plan_tree_base(&ctx.base, stmt); }); - ctx.stmt = stmt; - ctx.slice_index = 0; - ctx.scan_vertex = &scan_vertex; - ctx.producers = &producers; - ctx.safe_producers = &safe_producers; - - rpt_consumer_check_walker((Node *) stmt->planTree, &ctx); - for (const auto &entry : producers) - mgr->consumer_ok[entry.first] = - safe_producers[entry.first] == entry.second; -} - /* * Build the transfer plan for the join graph: the Prim forest, the * forward/backward stage assignment, and the consumer-eligibility check. @@ -723,7 +566,7 @@ RptBuildTransferPlan(PlannedStmt *stmt, RptTransferMgr *mgr) } rpt_classify_hops(mgr); - rpt_check_consumers(stmt, mgr); + RptCheckBackwardConsumers(stmt, mgr); } /* @@ -857,12 +700,65 @@ RptDumpTransferPlan(const RptTransferMgr *mgr, StringInfo output) out << "backward hop " << node.first << "->" << edge.destination << " merge=" << (edge.local ? "local" : "global") << '\n'; } + int global_edges = 0, rule9 = 0, strict = 0, eligible = 0, root_classes = 0; + int unknown_estimates = 0; + double rows_removed = 0; + for (const auto &group : mgr->groups) + if (mgr->roots.count(group.second.leader_id)) + ++root_classes; + for (const auto &decision : mgr->backward_consumers) + { + global_edges += !decision.local; + rule9 += decision.rule9; + strict += decision.strict_eligible; + eligible += decision.eligible; + if (decision.strict_eligible) + { + if (decision.estimated_rows_removed < 0) + ++unknown_estimates; + else + rows_removed += decision.estimated_rows_removed; + } + out << "backward consumer " << decision.consumer << " producer " << decision.producer + << " class " << decision.class_key.rtindex << '.' << decision.class_key.attno + << " merge=" << (decision.local ? "local" : "global") + << " rule9=" << (decision.rule9 ? "yes" : "no") + << " strict=" << (decision.strict_eligible ? "yes" : "no") + << " eligible=" << (decision.eligible ? "yes" : "no") + << " defer_first=" << (decision.defer_first ? "yes" : "no") + << " producer_slice=" << decision.producer_slice + << " consumer_slice=" << decision.consumer_slice + << " producer_attno=" << decision.producer_attno + << " consumer_attno=" << decision.consumer_attno + << " rows_removed="; + if (decision.estimated_rows_removed < 0) + out << "unknown"; + else + out << decision.estimated_rows_removed; + out << " reason=" << decision.reason << '\n'; + } + out << "survey classes=" << mgr->groups.size() << " root_classes=" << root_classes + << " global_edges=" << global_edges << " rule9=" << rule9 << " strict=" << strict + << " eligible=" << eligible << " known_rows_removed=" << rows_removed + << " unknown_estimates=" << unknown_estimates << '\n'; for (int id : ids) { const auto &consumer = mgr->consumer_ok.find(id); - - out << "consumer_ok " << id << ' ' - << (consumer != mgr->consumer_ok.end() && consumer->second ? "yes" : "no") << '\n'; + bool ok = consumer != mgr->consumer_ok.end() && consumer->second; + out << "consumer_ok " << id << ' ' << (ok ? "yes" : "no"); + if (!ok) + { + std::string reason = "no backward edge"; + for (const auto &decision : mgr->backward_consumers) + if (decision.consumer == id && !decision.eligible) + { + reason = decision.reason; + if (!decision.local) + break; + } + out << ": " << reason; + } + out << '\n'; } std::string text = out.str(); diff --git a/gpcontrib/robust_predicate_transfer/src/rptregion.cpp b/gpcontrib/robust_predicate_transfer/src/rptregion.cpp index d080c1e268e..9811011762d 100644 --- a/gpcontrib/robust_predicate_transfer/src/rptregion.cpp +++ b/gpcontrib/robust_predicate_transfer/src/rptregion.cpp @@ -43,6 +43,9 @@ extern "C" #include "postgres.h" #include "catalog/gp_distribution_policy.h" +#include "catalog/pg_class.h" +#include "catalog/pg_statistic.h" +#include "utils/syscache.h" #include "nodes/parsenodes.h" #include "nodes/pg_list.h" #include "parser/parsetree.h" @@ -143,6 +146,7 @@ rpt_vertex_for_scan(PlannedStmt *stmt, RptTransferMgr *mgr, Plan *scan) v.relname = rte->eref->aliasname; if (rte->rtekind == RTE_RELATION) { + v.relid = rte->relid; GpPolicy *policy = RptPgCall([&]() { return GpPolicyFetch(rte->relid); }); v.replicated = policy->ptype == POLICYTYPE_REPLICATED; v.numsegments = policy->numsegments; @@ -591,3 +595,31 @@ RptCollectGraph(PlannedStmt *stmt, RptTransferMgr *mgr) foreach(lc, stmt->subplans) rpt_walk(stmt, mgr, (Plan *) lfirst(lc)); } + +/* Catalog NDV is global. A negative stadistinct is a fraction of reltuples. + * This is a survey estimate, never a predicate-transfer correctness input. */ +double +RptEstimateDistinct(const RptVertexInfo &vertex, AttrNumber attno) +{ + if (!OidIsValid(vertex.relid) || attno <= 0) + return -1; + HeapTuple stats = RptPgCall([&]() { return SearchSysCache3(STATRELATTINH, + ObjectIdGetDatum(vertex.relid), Int16GetDatum(attno), BoolGetDatum(false)); }); + if (!HeapTupleIsValid(stats)) + return -1; + double distinct = ((Form_pg_statistic) GETSTRUCT(stats))->stadistinct; + RptPgCall([&]() { ReleaseSysCache(stats); }); + if (distinct < 0) + { + HeapTuple relation = RptPgCall([&]() { return SearchSysCache1(RELOID, + ObjectIdGetDatum(vertex.relid)); }); + if (!HeapTupleIsValid(relation)) + return -1; + double rows = ((Form_pg_class) GETSTRUCT(relation))->reltuples; + RptPgCall([&]() { ReleaseSysCache(relation); }); + if (rows < 0) + return -1; + distinct *= -rows; + } + return distinct > 0 ? distinct : -1; +} diff --git a/gpcontrib/robust_predicate_transfer/test/graph_test.cpp b/gpcontrib/robust_predicate_transfer/test/graph_test.cpp index f95fb7b9d00..e1efb08d2ee 100644 --- a/gpcontrib/robust_predicate_transfer/test/graph_test.cpp +++ b/gpcontrib/robust_predicate_transfer/test/graph_test.cpp @@ -31,6 +31,7 @@ #include "../src/rptgraph.cpp" #include "../src/rptplan.cpp" #include "../src/rptregion.cpp" +#include "../src/rpteligible.cpp" #include #include @@ -46,10 +47,37 @@ } \ } while (0) +struct TestResolution +{ + Plan *parent; + Plan *scan; + AttrNumber attno; + int slice; + bool consumer; +}; +static std::map test_resolutions; + extern "C" { sigjmp_buf *PG_exception_stack = NULL; ErrorContextCallback *error_context_stack = NULL; int dispatch_nest_level = 0; +HeapTuple SearchSysCache1(int, Datum) { abort(); } +HeapTuple SearchSysCache3(int, Datum, Datum, Datum) { abort(); } +void ReleaseSysCache(HeapTuple) { abort(); } +bool AnserResolveKeyScan(Plan *top, AttrNumber slot, int, bool consumer, + Plan **parent, Plan **scan, AttrNumber *attno, int *slice) +{ + CHECK(slot > 0); + auto found = test_resolutions.find(top); + if (found == test_resolutions.end()) + return false; + CHECK(found->second.consumer == consumer); + *parent = found->second.parent; + *scan = found->second.scan; + *attno = found->second.attno; + *slice = found->second.slice; + return true; +} Oid getBaseType(Oid type) { return type; } bool get_typbyval(Oid) { return true; } TypeCacheEntry *lookup_type_cache(Oid, int) { abort(); } @@ -85,6 +113,8 @@ void appendStringInfoChar(StringInfo str, char ch) } bool rpt_enable = false; bool rpt_debug = false; +bool rpt_backward = true; +int rpt_max_backward_consumers = 4; int rpt_max_chain_depth = 4; static void vertex(RptTransferMgr &mgr, int id, int rows, bool filtered) @@ -196,61 +226,232 @@ static void unfiltered_cycle() CHECK(!rpt_can_flow(&entry.second, row.first, entry.first)); } -static void all_producers_required() +/* A single-column identity targetlist, allocated without PostgreSQL memory. */ +struct TestTargetList { - RptTransferMgr mgr; - Plan scans[3] = {}; - for (int id = 1; id <= 3; ++id) + Var var = {}; + TargetEntry entry = {}; + ListCell cell = {}; + List list = {}; + + void bind(Plan *plan, int varno, int attno) { - vertex(mgr, id, 10, true); - mgr.vertices.back().scan = &scans[id - 1]; + NodeSetTag(&var, T_Var); + var.varno = varno; + var.varattno = attno; + entry.expr = (Expr *) &var; + entry.resno = 1; + cell.ptr_value = &entry; + list.type = T_List; + list.length = list.max_length = 1; + list.elements = &cell; + plan->targetlist = &list; } +}; + +struct TestBackwardPlan +{ + SeqScan consumer = {}; + SeqScan producer = {}; + Motion motion = {}; + Hash hash = {}; HashJoin join = {}; - NodeSetTag(&join, T_HashJoin); - outerPlan(&join) = &scans[0]; - innerPlan(&join) = &scans[1]; PlannedStmt stmt = {}; - stmt.planTree = (Plan *) &join; - RptGraphEdge in = {}; - in.destination = 2; - mgr.graph[1].backward_stage.in.push_back(in); - rpt_check_consumers(&stmt, &mgr); + TestTargetList lists[4]; + RptTransferMgr mgr; + + TestBackwardPlan() + { + test_resolutions.clear(); + NodeSetTag(&consumer, T_SeqScan); + NodeSetTag(&producer, T_SeqScan); + consumer.scan.scanrelid = 1; + producer.scan.scanrelid = 2; + lists[0].bind((Plan *) &consumer, 1, 3); + lists[1].bind((Plan *) &producer, 2, 3); + NodeSetTag(&motion, T_Motion); + motion.motionID = 1; + outerPlan(&motion) = (Plan *) &producer; + lists[2].bind((Plan *) &motion, OUTER_VAR, 1); + NodeSetTag(&hash, T_Hash); + outerPlan(&hash) = (Plan *) &motion; + lists[3].bind((Plan *) &hash, OUTER_VAR, 1); + NodeSetTag(&join, T_HashJoin); + join.join.jointype = JOIN_INNER; + outerPlan(&join) = (Plan *) &consumer; + innerPlan(&join) = (Plan *) &hash; + stmt.planTree = (Plan *) &join; + vertex(mgr, 1, 1000, false); + vertex(mgr, 2, 100, true); + mgr.vertices[0].scan = (Plan *) &consumer; + mgr.vertices[1].scan = (Plan *) &producer; + connect(mgr, edge(1, 2, false, false, 3, 3)); + RptBuildClasses(&mgr); + test_resolutions[(Plan *) &hash] = {(Plan *) &motion, (Plan *) &producer, 1, 1, false}; + RptBuildTransferPlan(&stmt, &mgr); + } +}; + +static void strict_backward_eligibility() +{ + TestBackwardPlan plan; + CHECK(plan.mgr.backward_consumers.size() == 1); + auto decision = plan.mgr.backward_consumers[0]; + CHECK(decision.eligible && decision.rule9 && decision.defer_first); + CHECK(decision.producer == 2 && decision.consumer == 1); + CHECK(decision.consumer_attno == 1 && decision.producer_attno == 1); + CHECK(decision.consumer_parent == (Plan *) &plan.join); + CHECK(decision.producer_parent == (Plan *) &plan.motion); + CHECK(decision.producer_slice == 1 && decision.consumer_slice == 0); + CHECK(plan.mgr.consumer_ok[1]); + + for (NodeTag tag : {T_Sort, T_Agg, T_Material, T_Unique, T_WindowAgg, T_Hash, T_SetOp}) + { + Plan blocker = {}; + blocker.type = tag; + outerPlan(&blocker) = (Plan *) &plan.consumer; + outerPlan(&plan.join) = &blocker; + RptCheckBackwardConsumers(&plan.stmt, &plan.mgr); + CHECK(!plan.mgr.backward_consumers[0].eligible); + CHECK(plan.mgr.backward_consumers[0].rule9); + CHECK(plan.mgr.backward_consumers[0].reason.find("between consumer and join") != std::string::npos); + } + outerPlan(&plan.join) = (Plan *) &plan.consumer; + plan.join.join.prefetch_inner = true; + RptCheckBackwardConsumers(&plan.stmt, &plan.mgr); + CHECK(plan.mgr.backward_consumers[0].eligible); + CHECK(!plan.mgr.backward_consumers[0].defer_first); + plan.join.join.prefetch_inner = false; + plan.join.join.jointype = JOIN_RIGHT; + RptCheckBackwardConsumers(&plan.stmt, &plan.mgr); + CHECK(plan.mgr.backward_consumers[0].eligible); + CHECK(!plan.mgr.backward_consumers[0].defer_first); + plan.join.join.jointype = JOIN_INNER; + + HashJoin nested = {}; + Hash nested_hash = {}; + Plan other = {}; + NodeSetTag(&nested, T_HashJoin); + NodeSetTag(&nested_hash, T_Hash); + nested.join.jointype = JOIN_LEFT; + outerPlan(&nested) = &other; + innerPlan(&nested) = (Plan *) &nested_hash; + outerPlan(&nested_hash) = (Plan *) &plan.consumer; + outerPlan(&plan.join) = (Plan *) &nested; + RptCheckBackwardConsumers(&plan.stmt, &plan.mgr); + CHECK(plan.mgr.backward_consumers[0].reason == "nullable side of outer join"); + + TestTargetList nested_list; + nested.join.jointype = JOIN_INNER; + outerPlan(&nested) = (Plan *) &plan.consumer; + innerPlan(&nested) = &other; + nested_list.bind((Plan *) &nested, OUTER_VAR, 1); + test_resolutions[(Plan *) &nested] = {(Plan *) &nested, (Plan *) &plan.consumer, 1, 0, true}; + RptCheckBackwardConsumers(&plan.stmt, &plan.mgr); + CHECK(plan.mgr.backward_consumers[0].reason == "join between consumer and join"); + plan.join.join.prefetch_inner = true; + RptCheckBackwardConsumers(&plan.stmt, &plan.mgr); + CHECK(plan.mgr.backward_consumers[0].eligible); + CHECK(!plan.mgr.backward_consumers[0].defer_first); + plan.join.join.prefetch_inner = false; + + outerPlan(&plan.join) = (Plan *) &plan.hash; + innerPlan(&plan.join) = (Plan *) &plan.consumer; + RptCheckBackwardConsumers(&plan.stmt, &plan.mgr); + CHECK(!plan.mgr.backward_consumers[0].rule9); + CHECK(plan.mgr.backward_consumers[0].reason == "producer is not on a qualifying hash build side"); + outerPlan(&plan.join) = (Plan *) &plan.consumer; + innerPlan(&plan.join) = (Plan *) &plan.hash; + plan.motion.motionID = 0; + RptCheckBackwardConsumers(&plan.stmt, &plan.mgr); + CHECK(plan.mgr.backward_consumers[0].reason == "same or unknown slice"); + plan.motion.motionID = 1; + plan.mgr.roots.clear(); + RptCheckBackwardConsumers(&plan.stmt, &plan.mgr); + CHECK(plan.mgr.backward_consumers[0].reason == "bridge-led class is deferred"); + plan.mgr.graph[2].backward_stage.out[0].local = true; + RptCheckBackwardConsumers(&plan.stmt, &plan.mgr); + CHECK(plan.mgr.backward_consumers[0].reason == "local edge uses core runtime filtering"); +} + +static void subplans_and_external_parameters_are_refused() +{ + TestBackwardPlan plan; + Bitmapset *parameters = (Bitmapset *) calloc(1, offsetof(Bitmapset, words) + sizeof(bitmapword)); + parameters->nwords = 1; + parameters->words[0] = 1; + plan.hash.plan.extParam = parameters; + RptCheckBackwardConsumers(&plan.stmt, &plan.mgr); + CHECK(!plan.mgr.backward_consumers[0].eligible); + CHECK(plan.mgr.backward_consumers[0].reason == "parameterized producer can be rescanned"); + plan.hash.plan.extParam = NULL; + RptCheckBackwardConsumers(&plan.stmt, &plan.mgr); + CHECK(plan.mgr.backward_consumers[0].eligible); + plan.hash.plan.extParam = NULL; + free(parameters); + + /* The Motion still gives valid slices inside the SubPlan. A separate + * placement flag must refuse it regardless of slice-number changes. */ + ListCell cell = {}; + cell.ptr_value = &plan.join; + List subplans = {}; + subplans.type = T_List; + subplans.length = subplans.max_length = 1; + subplans.elements = &cell; + Plan main = {}; + NodeSetTag(&main, T_Result); + plan.stmt.planTree = &main; + plan.stmt.subplans = &subplans; + RptCheckBackwardConsumers(&plan.stmt, &plan.mgr); + CHECK(!plan.mgr.backward_consumers[0].eligible); + CHECK(plan.mgr.backward_consumers[0].reason == "scan inside a SubPlan"); + plan.mgr.graph[2].backward_stage.out[0].local = true; + RptCheckBackwardConsumers(&plan.stmt, &plan.mgr); + CHECK(plan.mgr.backward_consumers[0].reason == "scan inside a SubPlan"); +} + +static void every_global_producer_is_required() +{ + RptTransferMgr mgr; + vertex(mgr, 1, 100, false); + RptBackwardConsumer first; + first.consumer = 1; + first.producer = 2; + first.eligible = true; + RptBackwardConsumer second = first; + second.producer = 3; + second.eligible = false; + mgr.backward_consumers = {first, second}; + rpt_consumer_verdicts(&mgr); CHECK(!mgr.consumer_ok[1]); - Motion build_motion = {}; - NodeSetTag(&build_motion, T_Motion); - build_motion.motionID = 1; - outerPlan(&build_motion) = &scans[1]; - innerPlan(&join) = (Plan *) &build_motion; - rpt_check_consumers(&stmt, &mgr); + mgr.backward_consumers[1].local = true; + rpt_consumer_verdicts(&mgr); CHECK(mgr.consumer_ok[1]); - build_motion.motionID = -1; - rpt_check_consumers(&stmt, &mgr); - CHECK(!mgr.consumer_ok[1]); - build_motion.motionID = 1; - in.destination = 3; /* This producer is not ordered before the probe. */ - mgr.graph[1].backward_stage.in.push_back(in); - rpt_check_consumers(&stmt, &mgr); + mgr.backward_consumers[0].local = true; + rpt_consumer_verdicts(&mgr); CHECK(!mgr.consumer_ok[1]); - mgr.graph[1].backward_stage.in[1].local = true; - rpt_check_consumers(&stmt, &mgr); - CHECK(mgr.consumer_ok[1]); /* The unsafe local edge does not wait. */ - mgr.graph[1].backward_stage.in[0].local = true; - rpt_check_consumers(&stmt, &mgr); - CHECK(!mgr.consumer_ok[1]); /* All-local means no blocking consumer. */ - for (auto &edge : mgr.graph[1].backward_stage.in) - edge.local = false; - /* Different hash joins can independently prove the two producers. */ - HashJoin upper = {}; - NodeSetTag(&upper, T_HashJoin); - outerPlan(&upper) = (Plan *) &join; - Motion upper_motion = {}; - NodeSetTag(&upper_motion, T_Motion); - upper_motion.motionID = 2; - outerPlan(&upper_motion) = &scans[2]; - innerPlan(&upper) = (Plan *) &upper_motion; - stmt.planTree = (Plan *) &upper; - rpt_check_consumers(&stmt, &mgr); - CHECK(mgr.consumer_ok[1]); +} + +static void backward_consumer_cap() +{ + RptTransferMgr mgr; + for (int i = 1; i <= 3; ++i) + { + vertex(mgr, i, 100 * i, false); + RptBackwardConsumer decision; + decision.class_key = {9, 1}; + decision.consumer = i; + decision.strict_eligible = decision.eligible = true; + decision.estimated_rows_removed = 100 * i; + mgr.backward_consumers.push_back(decision); + } + rpt_max_backward_consumers = 1; + rpt_cap_backward_consumers(&mgr); + CHECK(!mgr.backward_consumers[0].eligible); + CHECK(!mgr.backward_consumers[1].eligible); + CHECK(mgr.backward_consumers[2].eligible); + CHECK(mgr.backward_consumers[0].reason == "backward consumer cap exceeded"); + rpt_max_backward_consumers = 4; } static void bridges_and_locality() @@ -564,7 +765,10 @@ int main() protection_and_idempotence(); asymmetric_shape(); unfiltered_cycle(); - all_producers_required(); + strict_backward_eligibility(); + subplans_and_external_parameters_are_refused(); + every_global_producer_is_required(); + backward_consumer_cap(); bridges_and_locality(); local_star_and_backward(); replicated_and_compound_policies(); diff --git a/gpcontrib/robust_predicate_transfer/test/survey_test.py b/gpcontrib/robust_predicate_transfer/test/survey_test.py new file mode 100644 index 00000000000..dbf71b3d51b --- /dev/null +++ b/gpcontrib/robust_predicate_transfer/test/survey_test.py @@ -0,0 +1,150 @@ +#------------------------------------------------------------------------- +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# +"""Tests for survey accounting and non-executing SQL generation.""" +import importlib.util +import json +from pathlib import Path +import unittest +import tempfile +from unittest.mock import patch +from contextlib import redirect_stdout +import io +import subprocess + +spec = importlib.util.spec_from_file_location( + "rpt_survey", Path(__file__).resolve().parents[1] / "scripts" / "survey.py") +survey = importlib.util.module_from_spec(spec) +spec.loader.exec_module(survey) + + +class SurveyTests(unittest.TestCase): + def test_no_region_is_zero_not_missing_data(self): + self.assertEqual(survey.parse_dump("no transfer regions\n")["strict"], 0) + with self.assertRaises(ValueError): + survey.parse_dump("transfer order: 1 2\n") + + def test_comparison_counts_channels_not_both_endpoints(self): + explain = [{"Plan": {"Node Type": "Custom Scan", + "Custom Plan Provider": "Anser Bloom Producer", + "Plans": [{"Custom Plan Provider": "Anser Bloom Consumer"}]}, + "Optimizer": "GPORCA"}] + output = (survey.BEGIN_DUMP + "\n" + + "survey classes=2 root_classes=1 global_edges=3 rule9=2 " + "strict=1 eligible=1 known_rows_removed=250 unknown_estimates=0\n" + + survey.BEGIN_ANSER + "\n" + json.dumps(explain) + "\n" + survey.END + "\n") + metrics, dump, plan = survey.parse_output(output) + self.assertEqual(metrics["anser_joins"], 1) + self.assertEqual(metrics["strict"], 1) + self.assertEqual(metrics["anser_optimizer_used"], "GPORCA") + self.assertEqual(metrics["known_rows_removed"], 250) + + def test_incomplete_output_cannot_be_reported_as_zero(self): + with self.assertRaises(ValueError): + survey.parse_output(survey.BEGIN_DUMP + "\nno transfer regions\n") + with self.assertRaises(ValueError): + survey.parse_dump("survey classes=1 root_classes=1 global_edges=1 " + "rule9=1 strict=2 eligible=1 known_rows_removed=0 unknown_estimates=0") + + def test_queries_are_planned_in_read_only_transaction(self): + sql = survey.survey_sql("SELECT 'O''Brien' -- trailing comment", "on", 1000, "imdb,public") + self.assertIn("BEGIN READ ONLY;", sql) + self.assertIn("SET LOCAL rpt.enable = off;", sql) + self.assertIn("SET LOCAL rpt.backward = off;", sql) + self.assertIn("SET LOCAL anser.runtime_filter = on;", sql) + self.assertIn("EXPLAIN (FORMAT JSON) SELECT 'O''Brien' -- trailing comment\n;", sql) + self.assertNotIn("EXPLAIN ANALYZE", sql) + self.assertTrue(sql.endswith("ROLLBACK;")) + self.assertEqual(survey.sql_literal("a'b"), "'a''b'") + + def test_templates_count_selects_not_files(self): + with tempfile.TemporaryDirectory() as directory: + path = Path(directory) + (path / "14.sql").write_text("-- variant a\nSELECT 14; /* variant b */ SELECT 15; -- end\n") + (path / "15.sql").write_text("WITH revenue AS (SELECT 1 n) SELECT * FROM revenue;") + queries = survey.load_queries(path) + self.assertEqual(len(queries), 3) + self.assertEqual([q["statement"] for q in queries], [1, 2, 1]) + self.assertEqual(survey.EXPECTED["TPC-DS"], 103) + + def test_standard_tpcds_layout_and_split_variants_have_103_statements(self): + with tempfile.TemporaryDirectory() as directory: + path = Path(directory) + for number in range(1, 100): + sql = "SELECT %d;" % number + if number in (14, 23, 24, 39): + sql += " SELECT %d;" % number + (path / (str(number) + ".sql")).write_text(sql) + self.assertEqual(len(list(path.glob("*.sql"))), 99) + self.assertEqual(len(survey.load_queries(path)), 103) + for number in (14, 23, 24, 39): + (path / (str(number) + ".sql")).unlink() + for suffix in ("a", "b"): + (path / (str(number) + suffix + ".sql")).write_text("SELECT %d;" % number) + self.assertEqual(len(list(path.glob("*.sql"))), 103) + self.assertEqual(len(survey.load_queries(path)), 103) + + def test_driver_completes_by_statement_count(self): + with tempfile.TemporaryDirectory() as directory: + root = Path(directory) + arguments = ["survey.py"] + for suite, flag in [("JOB", "job"), ("TPC-H", "tpch"), ("TPC-DS", "tpcds")]: + path = root / suite + path.mkdir() + (path / "one.sql").write_text("SELECT 1; SELECT 2;" if flag == "tpcds" else "SELECT 1;") + arguments += ["--" + flag, str(path)] + output = root / "output" + arguments += ["--output", str(output)] + text = (survey.BEGIN_DUMP + "\nno transfer regions\n" + survey.BEGIN_ANSER + + "\n" + json.dumps([{"Plan": {"Node Type": "Result"}}]) + "\n" + survey.END + "\n") + result = subprocess.CompletedProcess([], 0, stdout=text, stderr="") + with patch("sys.argv", arguments), patch.object(survey, "EXPECTED", {"JOB": 1, "TPC-H": 1, "TPC-DS": 2}), \ + patch.object(survey.subprocess, "run", return_value=result), redirect_stdout(io.StringIO()): + self.assertEqual(survey.main(), 0) + report = json.loads((output / "results.json").read_text()) + self.assertTrue(report["survey_complete"]) + self.assertEqual(report["expected_statements"]["TPC-DS"], 2) + self.assertEqual(len(report["results"]), 8) + tpcds = [row for row in report["results"] if row["suite"] == "TPC-DS"] + self.assertEqual([row["statement"] for row in tpcds], [1, 2, 1, 2]) + + def test_split_respects_literals_and_nested_comments(self): + sql = "SELECT ';', $$x;y$$, $tag$a;b$tag$, \"a;b\"; /* outer /* inner ; */ end */ SELECT 2;" + self.assertEqual(len(survey.split_select_statements(sql)), 2) + self.assertEqual(len(survey.split_select_statements("SELECT E'a\\\';b'; SELECT 2;")), 2) + self.assertEqual(survey.split_select_statements("-- only comment\n /* another */"), []) + for sql in ["SELECT 'unterminated", "SELECT $$unterminated", "/* unterminated"]: + with self.assertRaises(ValueError): + survey.split_select_statements(sql) + + def test_q15_view_form_is_rejected_with_cte_instruction(self): + with self.assertRaisesRegex(ValueError, "CTE form of TPC-H Q15"): + survey.split_select_statements("CREATE VIEW revenue AS SELECT 1; SELECT * FROM revenue; DROP VIEW revenue;") + + def test_failed_queries_stay_in_denominator(self): + records = [dict(suite="JOB", optimizer="on", status="ok", strict=1, + eligible=1, anser_joins=1), + dict(suite="JOB", optimizer="on", status="error")] + text = survey.summarize(records, False) + self.assertIn("Survey complete: no", text) + self.assertIn("| JOB | on | 1/2 | 1/2 | 1/2 | 1/2 |", text) + + +if __name__ == "__main__": + unittest.main() From 05c12eef6dd9d0559e3ac469c3396bdab92d4ec2 Mon Sep 17 00:00:00 2001 From: Leonid Borchuk Date: Tue, 10 Nov 2026 14:34:20 +0300 Subject: [PATCH 21/27] Feature: Prepare RPT forward InitPlan construction Build a separate forward schedule rooted at the largest relation, including unfiltered fact tables, without changing backward survey leaders or eligibility decisions. Add a finished-plan builder for the distributed reducer InitPlan: append coordinator and non-parallel reader slices, Gather and Sequence nodes, an empty Result with an implicit-AND false qual, and a boolean PARAM_EXEC. Continue node IDs above both existing plans and reducer children, preserving existing InitPlans and slice mappings. Add standalone layout tests for fresh and existing InitPlans, checking slice indices and parents, parameter numbering, node IDs and qual shape. Run these tests through unitcheck and ignore generated test binaries. This is preparatory Phase III work. The builder is not called by the planner hook yet; reducer scans, filter wrappers, cost gating and runtime injection remain unimplemented. SQL execution and cross-dispatch filter delivery have not been validated on a cluster. Validation: native unitcheck and 10 survey tests pass with -O3 -DNDEBUG; InitPlan layout tests pass AddressSanitizer and UndefinedBehaviorSanitizer. Assisted-by: OpenAI Codex --- .../robust_predicate_transfer/.gitignore | 2 + gpcontrib/robust_predicate_transfer/Makefile | 9 +- .../robust_predicate_transfer/include/rpt.h | 3 + .../src/rptinject.cpp | 118 +++++++++++ .../robust_predicate_transfer/src/rptplan.cpp | 49 ++++- .../test/graph_test.cpp | 29 +++ .../test/inject_test.cpp | 187 ++++++++++++++++++ 7 files changed, 389 insertions(+), 8 deletions(-) create mode 100644 gpcontrib/robust_predicate_transfer/.gitignore create mode 100644 gpcontrib/robust_predicate_transfer/src/rptinject.cpp create mode 100644 gpcontrib/robust_predicate_transfer/test/inject_test.cpp diff --git a/gpcontrib/robust_predicate_transfer/.gitignore b/gpcontrib/robust_predicate_transfer/.gitignore new file mode 100644 index 00000000000..39c5900422a --- /dev/null +++ b/gpcontrib/robust_predicate_transfer/.gitignore @@ -0,0 +1,2 @@ +/test/graph_test +/test/inject_test diff --git a/gpcontrib/robust_predicate_transfer/Makefile b/gpcontrib/robust_predicate_transfer/Makefile index a0c5b8247fe..42014b73dff 100644 --- a/gpcontrib/robust_predicate_transfer/Makefile +++ b/gpcontrib/robust_predicate_transfer/Makefile @@ -30,6 +30,7 @@ OBJS = \ src/rptgraph.o \ src/rpteligible.o \ src/rptinit.o \ + src/rptinject.o \ src/rptplan.o \ src/rptregion.o @@ -105,13 +106,17 @@ installcheck-with-install: # Uses PostgreSQL headers but no server: synthetic plans exercise the exact # production graph code and deterministic failure paths. .PHONY: unitcheck -unitcheck: test/graph_test +unitcheck: test/graph_test test/inject_test ./test/graph_test + ./test/inject_test test/graph_test: $(srcdir)/test/graph_test.cpp $(wildcard $(srcdir)/src/*.cpp) $(srcdir)/include/rpt.h $(CXX) $(CXXFLAGS) $(CPPFLAGS) $(PG_CPPFLAGS) $< -o $@ -EXTRA_CLEAN += test/graph_test +test/inject_test: $(srcdir)/test/inject_test.cpp $(srcdir)/src/rptinject.cpp $(srcdir)/include/rpt.h + $(CXX) $(CXXFLAGS) $(CPPFLAGS) $(PG_CPPFLAGS) $< -o $@ + +EXTRA_CLEAN += test/graph_test test/inject_test SURVEY_PYTHON ?= python3 .PHONY: surveycheck diff --git a/gpcontrib/robust_predicate_transfer/include/rpt.h b/gpcontrib/robust_predicate_transfer/include/rpt.h index 625157237dc..45135eabda9 100644 --- a/gpcontrib/robust_predicate_transfer/include/rpt.h +++ b/gpcontrib/robust_predicate_transfer/include/rpt.h @@ -274,6 +274,9 @@ extern void RptBuildClasses(RptTransferMgr *mgr); /* rptplan.cpp: the Prim forest (LargestRootUpdated), forward/backward stage * assignment (CreateTransferPlanUpdated), consumer eligibility, and dumps. */ extern void RptBuildTransferPlan(PlannedStmt *stmt, RptTransferMgr *mgr); +extern void RptBuildForwardPlan(const RptTransferMgr *analysis, + RptTransferMgr *forward); +extern void RptAttachReducerInitPlan(PlannedStmt *stmt, List *children, int width); extern void RptCheckBackwardConsumers(PlannedStmt *stmt, RptTransferMgr *mgr); extern double RptEstimateDistinct(const RptVertexInfo &vertex, AttrNumber attno); extern void RptDumpTransferPlan(const RptTransferMgr *mgr, StringInfo out); diff --git a/gpcontrib/robust_predicate_transfer/src/rptinject.cpp b/gpcontrib/robust_predicate_transfer/src/rptinject.cpp new file mode 100644 index 00000000000..554d595534e --- /dev/null +++ b/gpcontrib/robust_predicate_transfer/src/rptinject.cpp @@ -0,0 +1,118 @@ +/*------------------------------------------------------------------------- + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * rptinject.cpp + * Finished-plan construction of the distributed forward InitPlan. + * + *------------------------------------------------------------------------- + */ +extern "C" +{ +#include "postgres.h" +#include "catalog/pg_type.h" +#include "nodes/makefuncs.h" +#include "anserplan.h" +} + +#include "rpt.h" + +/* The caller owns a private copy of the statement. This function may throw; + * publish that copy only after the entire injection pass has succeeded. + * Children must be non-parallel reducer producers that return NULL at EOF. + * The final empty Result makes EXISTS drain the Sequence completely. */ +void +RptAttachReducerInitPlan(PlannedStmt *stmt, List *children, int width) +{ + RptPgCall([&]() { + if (!stmt->planTree || width <= 0 || children == NIL || + stmt->numSlices <= 0 || !stmt->slices || + (stmt->subplans != NIL && !stmt->subplan_sliceIds)) + elog(ERROR, "invalid RPT reducer InitPlan input"); + + int max_id = AnserMaxPlanNodeId(stmt); + ListCell *lc; + foreach(lc, children) + { + PlannedStmt child_stmt = {}; + child_stmt.planTree = (Plan *) lfirst(lc); + max_id = Max(max_id, AnserMaxPlanNodeId(&child_stmt)); + } + int next_id = max_id + 1; + int root_slice = stmt->numSlices; + int sender_slice = root_slice + 1; + int old_subplans = list_length(stmt->subplans); + int param_id = list_length(stmt->paramExecTypes); + Flow *distributed = makeNode(Flow); + distributed->flotype = FLOW_PARTITIONED; + distributed->locustype = CdbLocusType_Strewn; + distributed->numsegments = width; + Result *empty = makeNode(Result); + empty->plan.plan_node_id = next_id++; + empty->plan.flow = distributed; + /* ExecInitResult passes this field to ExecInitQual as implicit AND. */ + empty->resconstantqual = (Node *) list_make1(makeBoolConst(false, false)); + Sequence *sequence = makeNode(Sequence); + sequence->plan.plan_node_id = next_id++; + sequence->plan.flow = distributed; + sequence->subplans = lappend(list_copy(children), empty); + Motion *gather = makeNode(Motion); + gather->plan.plan_node_id = next_id++; + gather->plan.lefttree = (Plan *) sequence; + gather->motionType = MOTIONTYPE_GATHER; + gather->motionID = sender_slice; + Flow *singleton = makeNode(Flow); + singleton->flotype = FLOW_SINGLETON; + singleton->locustype = CdbLocusType_Entry; + singleton->segindex = -1; + singleton->numsegments = 1; + gather->plan.flow = singleton; + + PlanSlice *slices = (PlanSlice *) palloc0( + sizeof(PlanSlice) * (stmt->numSlices + 2)); + memcpy(slices, stmt->slices, sizeof(PlanSlice) * stmt->numSlices); + slices[root_slice].sliceIndex = root_slice; + slices[root_slice].parentIndex = -1; + slices[root_slice].gangType = GANGTYPE_UNALLOCATED; + slices[root_slice].segindex = -1; + slices[sender_slice].sliceIndex = sender_slice; + slices[sender_slice].parentIndex = root_slice; + slices[sender_slice].gangType = GANGTYPE_PRIMARY_READER; + slices[sender_slice].segindex = 0; + slices[sender_slice].numsegments = width; + int *slice_ids = (int *) palloc(sizeof(int) * (old_subplans + 1)); + if (old_subplans) + memcpy(slice_ids, stmt->subplan_sliceIds, sizeof(int) * old_subplans); + slice_ids[old_subplans] = root_slice; + + SubPlan *subplan = makeNode(SubPlan); + subplan->subLinkType = EXISTS_SUBLINK; + subplan->plan_id = old_subplans + 1; + subplan->plan_name = psprintf("InitPlan %d", subplan->plan_id); + subplan->firstColType = VOIDOID; + subplan->firstColTypmod = -1; + subplan->is_initplan = true; + subplan->setParam = list_make1_int(param_id); + stmt->subplans = lappend(stmt->subplans, gather); + stmt->paramExecTypes = lappend_oid(stmt->paramExecTypes, BOOLOID); + stmt->planTree->initPlan = lappend(stmt->planTree->initPlan, subplan); + stmt->subplan_sliceIds = slice_ids; + stmt->slices = slices; + stmt->numSlices += 2; + }); +} diff --git a/gpcontrib/robust_predicate_transfer/src/rptplan.cpp b/gpcontrib/robust_predicate_transfer/src/rptplan.cpp index 872714752d9..58d87b67098 100644 --- a/gpcontrib/robust_predicate_transfer/src/rptplan.cpp +++ b/gpcontrib/robust_predicate_transfer/src/rptplan.cpp @@ -222,7 +222,7 @@ rpt_largest_root_updated(RptTransferMgr *mgr, std::set &remaining, const std::vector &sorted_ids, std::vector > &forward_edges, std::vector > &backward_edges, - int32 &next_order) + int32 &next_order, bool largest_relation) { std::map forward_depth; std::map backward_depth; @@ -237,7 +237,8 @@ rpt_largest_root_updated(RptTransferMgr *mgr, std::set &remaining, { const RptVertexInfo &v = mgr->vertices[mgr->by_rti[*rit]]; - if (remaining.count(*rit) && (v.filtered || v.intermediate)) + if (remaining.count(*rit) && + (largest_relation || v.filtered || v.intermediate)) { root = *rit; break; @@ -496,8 +497,9 @@ rpt_classify_hops(RptTransferMgr *mgr) * Build the transfer plan for the join graph: the Prim forest, the * forward/backward stage assignment, and the consumer-eligibility check. */ -void -RptBuildTransferPlan(PlannedStmt *stmt, RptTransferMgr *mgr) +static void +rpt_build_transfer_plan(PlannedStmt *stmt, RptTransferMgr *mgr, + bool largest_relation) { std::vector > forward_edges; std::vector > backward_edges; @@ -530,7 +532,7 @@ RptBuildTransferPlan(PlannedStmt *stmt, RptTransferMgr *mgr) */ while (!remaining.empty()) rpt_largest_root_updated(mgr, remaining, sorted_ids, forward_edges, backward_edges, - next_order); + next_order, largest_relation); for (const auto &group : mgr->groups) if (group.second.leader_id != -1 && !mgr->roots.count(group.second.leader_id)) @@ -566,7 +568,42 @@ RptBuildTransferPlan(PlannedStmt *stmt, RptTransferMgr *mgr) } rpt_classify_hops(mgr); - RptCheckBackwardConsumers(stmt, mgr); + if (!largest_relation) + RptCheckBackwardConsumers(stmt, mgr); +} + +/* Backward analysis retains the artifact's filtered-root heuristic. */ +void +RptBuildTransferPlan(PlannedStmt *stmt, RptTransferMgr *mgr) +{ + rpt_build_transfer_plan(stmt, mgr, false); +} + +/* The reducer targets the largest relation, including an unfiltered fact. + * Build a separate schedule: changing the backward leaders would change the + * eligibility survey and its independently gated injection decisions. */ +void +RptBuildForwardPlan(const RptTransferMgr *analysis, RptTransferMgr *forward) +{ + *forward = *analysis; + forward->graph.clear(); + forward->roots.clear(); + forward->bridges.clear(); + forward->reducer_order.clear(); + forward->transfer_order.clear(); + forward->consumer_ok.clear(); + forward->backward_consumers.clear(); + for (auto &group : forward->groups) + group.second.leader_id = -1; + rpt_build_transfer_plan(NULL, forward, true); + /* This schedule is not a backward eligibility decision. */ + forward->consumer_ok.clear(); + forward->backward_consumers.clear(); + for (auto &node : forward->graph) + { + node.second.backward_stage.in.clear(); + node.second.backward_stage.out.clear(); + } } /* diff --git a/gpcontrib/robust_predicate_transfer/test/graph_test.cpp b/gpcontrib/robust_predicate_transfer/test/graph_test.cpp index e1efb08d2ee..273a4124d52 100644 --- a/gpcontrib/robust_predicate_transfer/test/graph_test.cpp +++ b/gpcontrib/robust_predicate_transfer/test/graph_test.cpp @@ -732,6 +732,34 @@ static void directed_parent_choice() CHECK(forward_from == 2 && backward_from == -1); } +static void forward_root_includes_unfiltered_fact() +{ + RptTransferMgr analysis; + vertex(analysis, 1, 1000000, false); + vertex(analysis, 2, 100, true); + vertex(analysis, 3, 50, true); + connect(analysis, edge(1, 2)); + connect(analysis, edge(1, 3)); + RptBuildClasses(&analysis); + RptBuildTransferPlan(NULL, &analysis); + CHECK(analysis.roots.count(2)); + RptTransferMgr forward; + RptBuildForwardPlan(&analysis, &forward); + CHECK(forward.roots.size() == 1 && forward.roots.count(1)); + CHECK(!forward.graph.at(1).forward_stage.in.empty()); + CHECK(forward.graph.at(1).forward_stage.out.empty()); + CHECK(forward.reducer_order.size() == 2); + CHECK(analysis.roots.count(2)); + CHECK(analysis.groups.begin()->second.leader_id == 2); + CHECK(forward.backward_consumers.empty()); + for (const auto &node : forward.graph) + CHECK(node.second.backward_stage.in.empty() && + node.second.backward_stage.out.empty()); + /* Rebuilding must discard the previous schedule. */ + RptBuildForwardPlan(&analysis, &forward); + CHECK(forward.reducer_order.size() == 2); +} + static void error_unwinds() { int destroyed = 0; @@ -775,6 +803,7 @@ int main() index_only_endpoint(); join_protection(); directed_parent_choice(); + forward_root_includes_unfiltered_fact(); error_unwinds(); return 0; } diff --git a/gpcontrib/robust_predicate_transfer/test/inject_test.cpp b/gpcontrib/robust_predicate_transfer/test/inject_test.cpp new file mode 100644 index 00000000000..9e58b2e7b3e --- /dev/null +++ b/gpcontrib/robust_predicate_transfer/test/inject_test.cpp @@ -0,0 +1,187 @@ +/*------------------------------------------------------------------------- + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * inject_test.cpp + * Deterministic tests of the production graph, planner, resolver and + * PostgreSQL/C++ error boundary, using synthetic plans and C-call stubs. + * + * IDENTIFICATION + * gpcontrib/robust_predicate_transfer/test/inject_test.cpp + * + *------------------------------------------------------------------------- + */ +#include "../src/rptinject.cpp" +#include +#include +#include +#include +#undef vsnprintf +#undef fprintf +#define CHECK(c) do { if (!(c)) { fprintf(stderr, "check failed: %s:%d: %s\n", __FILE__, __LINE__, #c); abort(); } } while (0) + +static std::vector allocations; +extern "C" { +sigjmp_buf *PG_exception_stack = NULL; +ErrorContextCallback *error_context_stack = NULL; +MemoryContext CurrentMemoryContext = NULL; +int dispatch_nest_level = 0; +void *palloc(Size n) { void *p = malloc(n); CHECK(p); allocations.push_back(p); return p; } +void *palloc0(Size n) { void *p = palloc(n); memset(p, 0, n); return p; } +void *MemoryContextAllocZero(MemoryContext, Size n) { return palloc0(n); } +void *MemoryContextAllocZeroAligned(MemoryContext, Size n) { return palloc0(n); } +bool errstart_cold(int, const char *) { abort(); } +void ExceptionalCondition(const char *, const char *, int) { abort(); } +bool errstart(int, const char *) { abort(); } +void errfinish(const char *, int, const char *) { abort(); } +void errmsg_internal(const char *, ...) { abort(); } +char *psprintf(const char *fmt, ...) +{ + char *p = (char *) palloc(128); + va_list args; va_start(args, fmt); vsnprintf(p, 128, fmt, args); va_end(args); + return p; +} +static List *append_cell(List *list, NodeTag tag, ListCell cell) +{ + int n = list_length(list); + List *out = (List *) palloc0(sizeof(List)); + out->type = tag; out->length = out->max_length = n + 1; + out->elements = (ListCell *) palloc(sizeof(ListCell) * (n + 1)); + if (n) memcpy(out->elements, list->elements, sizeof(ListCell) * n); + out->elements[n] = cell; + return out; +} +List *list_make1_impl(NodeTag tag, ListCell c) { return append_cell(NIL, tag, c); } +List *lappend(List *l, void *p) { return append_cell(l, T_List, list_make_ptr_cell(p)); } +List *lappend_oid(List *l, Oid o) { return append_cell(l, T_OidList, list_make_oid_cell(o)); } +List *list_copy(const List *l) +{ + List *out = NIL; + for (int i = 0; i < list_length(l); ++i) out = append_cell(out, l->type, l->elements[i]); + return out; +} +Node *makeBoolConst(bool value, bool isnull) +{ + Const *c = makeNode(Const); c->consttype = BOOLOID; c->constvalue = BoolGetDatum(value); + c->constisnull = isnull; return (Node *) c; +} +static int max_id(Plan *p) +{ + if (!p) return 0; + int m = Max(p->plan_node_id, Max(max_id(p->lefttree), max_id(p->righttree))); + if (IsA(p, Sequence)) + { + ListCell *lc; + foreach(lc, ((Sequence *) p)->subplans) m = Max(m, max_id((Plan *) lfirst(lc))); + } + return m; +} +int AnserMaxPlanNodeId(PlannedStmt *s) +{ + int m = max_id(s->planTree); ListCell *lc; + foreach(lc, s->subplans) m = Max(m, max_id((Plan *) lfirst(lc))); + return m; +} +} + +static void collect_ids(Plan *p, std::set &ids) +{ + if (!p) return; + CHECK(ids.insert(p->plan_node_id).second); + collect_ids(p->lefttree, ids); collect_ids(p->righttree, ids); + if (IsA(p, Sequence)) + { + ListCell *lc; + foreach(lc, ((Sequence *) p)->subplans) collect_ids((Plan *) lfirst(lc), ids); + } +} + +static void layout(bool existing) +{ + PlannedStmt stmt = {}; + Result main = {}; main.plan.type = T_Result; main.plan.plan_node_id = 5; + stmt.planTree = &main.plan; + PlanSlice initial[2] = {}; + initial[0].sliceIndex = 0; initial[0].parentIndex = -1; + initial[1].sliceIndex = 1; initial[1].parentIndex = -1; + stmt.slices = initial; stmt.numSlices = existing ? 2 : 1; + Result prior = {}; prior.plan.type = T_Result; prior.plan.plan_node_id = 90; + int prior_slice = 1; + SubPlan old_init = {}; + if (existing) + { + stmt.subplans = list_make1(&prior); + stmt.subplan_sliceIds = &prior_slice; + stmt.paramExecTypes = list_make1_oid(INT4OID); + stmt.planTree->initPlan = list_make1(&old_init); + } + Result child = {}; child.plan.type = T_Result; child.plan.plan_node_id = 110; + List *children = list_make1(&child); + int old_slices = stmt.numSlices; + RptAttachReducerInitPlan(&stmt, children, 8); + CHECK(stmt.numSlices == old_slices + 2); + CHECK(memcmp(stmt.slices, initial, sizeof(PlanSlice) * old_slices) == 0); + for (int i = 0; i < stmt.numSlices; ++i) CHECK(stmt.slices[i].sliceIndex == i); + PlanSlice &r = stmt.slices[old_slices], &s = stmt.slices[old_slices + 1]; + CHECK(r.parentIndex == -1 && r.gangType == GANGTYPE_UNALLOCATED && r.segindex == -1); + CHECK(s.parentIndex == old_slices && s.gangType == GANGTYPE_PRIMARY_READER); + CHECK(s.numsegments == 8 && s.segindex == 0 && s.parallel_workers == 0); + CHECK(!s.directDispatch.isDirectDispatch && s.directDispatch.contentIds == NIL); + CHECK(list_length(stmt.subplans) == (existing ? 2 : 1)); + CHECK(list_length(stmt.paramExecTypes) == (existing ? 2 : 1)); + CHECK(llast_oid(stmt.paramExecTypes) == BOOLOID); + CHECK(list_length(stmt.planTree->initPlan) == (existing ? 2 : 1)); + if (existing) + { + CHECK(stmt.subplan_sliceIds[0] == 1 && linitial(stmt.subplans) == &prior); + CHECK(linitial_oid(stmt.paramExecTypes) == INT4OID); + CHECK(linitial(stmt.planTree->initPlan) == &old_init); + } + SubPlan *sp = (SubPlan *) llast(stmt.planTree->initPlan); + CHECK(sp->is_initplan && sp->subLinkType == EXISTS_SUBLINK); + CHECK(sp->plan_id == list_length(stmt.subplans)); + CHECK(list_length(sp->setParam) == 1 && linitial_int(sp->setParam) == (existing ? 1 : 0)); + CHECK(sp->parParam == NIL && sp->args == NIL); + CHECK(stmt.subplan_sliceIds[sp->plan_id - 1] == old_slices); + Motion *motion = (Motion *) llast(stmt.subplans); + CHECK(IsA(motion, Motion) && motion->motionID == old_slices + 1); + CHECK(motion->motionType == MOTIONTYPE_GATHER); + CHECK(motion->plan.flow->flotype == FLOW_SINGLETON); + Sequence *seq = (Sequence *) motion->plan.lefttree; + CHECK(IsA(seq, Sequence) && list_length(seq->subplans) == 2); + CHECK(linitial(seq->subplans) == &child && list_length(children) == 1); + CHECK(seq->plan.flow->flotype == FLOW_PARTITIONED && seq->plan.flow->numsegments == 8); + Result *empty = (Result *) llast(seq->subplans); + CHECK(IsA(empty, Result) && IsA(empty->resconstantqual, List)); + List *quals = (List *) empty->resconstantqual; + CHECK(list_length(quals) == 1 && IsA(linitial(quals), Const)); + Const *qual = (Const *) linitial(quals); + CHECK(qual->consttype == BOOLOID && !qual->constisnull); + CHECK(!DatumGetBool(qual->constvalue)); + std::set ids; + collect_ids(stmt.planTree, ids); + ListCell *lc; foreach(lc, stmt.subplans) collect_ids((Plan *) lfirst(lc), ids); + CHECK(ids.size() == (existing ? 6 : 5)); + CHECK(empty->plan.plan_node_id > 110 && seq->plan.plan_node_id > 110 && motion->plan.plan_node_id > 110); +} +int main() +{ + layout(false); layout(true); + for (void *p : allocations) free(p); + return 0; +} From 439272db87f03df2a5f6684a383b750096f2ebe0 Mon Sep 17 00:00:00 2001 From: Leonid Borchuk Date: Tue, 10 Nov 2026 19:11:00 +0300 Subject: [PATCH 22/27] Feature: Execute RPT forward reducer InitPlans Build the forward reducer on a private copy of the finished statement and attach it as an uncorrelated distributed InitPlan. Apply completed filters to main-plan roots, or also to chain scans with rpt.forward_apply=all. Keep Anser per-join injection exclusive with RPT and fail open on planner construction errors while preserving query cancellation. Copy key-only SeqScans using the statement's existing range table and snapshot. Retain quals only when every operation belongs to an explicit built-in comparison/hash allowlist; otherwise drop the whole qual list. Reflect dropped quals in filter sizing and selectivity estimates. Gate components by root rows, estimated selectivity and payload exchange cost. Hand intermediate co-distributed filters between reducer scans in-process, without coordinator traffic or blocking waits. Use statement-scoped Anser channels for global hops and filters consumed by the main dispatch. Phase II backward runtime injection remains deferred. Add multi-hop, local/global, apply-all, early-termination, prepared and SPI SQL coverage plus forced transport-failure cases. Correct backward survey expectations for simplified outer joins whose root is on the probe side. Provide an empty-InitPlan measurement switch, workload generator, recorded exchange measurements and a staged cluster experiment plan. Validation: native unitcheck and 10 survey tests pass with -O3 -DNDEBUG; graph, InitPlan and Anser connection-event tests pass ASan/UBSan. Changed C/C++ sources pass syntax checks. The new SQL suites and cross-dispatch runtime behavior still require validation on a running cluster. Assisted-by: OpenAI Codex --- gpcontrib/robust_predicate_transfer/Makefile | 7 +- .../robust_predicate_transfer/docs/design.md | 107 +++- .../docs/phase3-experiments.md | 124 ++++ .../docs/phase3-measurements.md | 87 +++ .../expected/rpt_backward_eligibility.out | 22 +- .../expected/rpt_forward.out | 343 ++++++++++ .../expected/rpt_forward_faults.out | 158 +++++ .../robust_predicate_transfer/include/rpt.h | 94 +++ .../include/rptlocal.h | 40 ++ .../include/rptqual.h | 130 ++++ .../scripts/measure_initplan.py | 58 ++ .../sql/rpt_backward_eligibility.sql | 22 +- .../sql/rpt_forward.sql | 343 ++++++++++ .../sql/rpt_forward_faults.sql | 158 +++++ .../src/rptforward.cpp | 587 ++++++++++++++++++ .../src/rptforwardplan.cpp | 221 +++++++ .../robust_predicate_transfer/src/rptinit.cpp | 270 +++++++- .../src/rptinject.cpp | 5 +- .../robust_predicate_transfer/src/rptlocal.c | 429 +++++++++++++ .../test/graph_test.cpp | 258 ++++++++ .../test/inject_test.cpp | 18 +- 21 files changed, 3461 insertions(+), 20 deletions(-) create mode 100644 gpcontrib/robust_predicate_transfer/docs/phase3-experiments.md create mode 100644 gpcontrib/robust_predicate_transfer/docs/phase3-measurements.md create mode 100644 gpcontrib/robust_predicate_transfer/expected/rpt_forward.out create mode 100644 gpcontrib/robust_predicate_transfer/expected/rpt_forward_faults.out create mode 100644 gpcontrib/robust_predicate_transfer/include/rptlocal.h create mode 100644 gpcontrib/robust_predicate_transfer/include/rptqual.h create mode 100644 gpcontrib/robust_predicate_transfer/scripts/measure_initplan.py create mode 100644 gpcontrib/robust_predicate_transfer/sql/rpt_forward.sql create mode 100644 gpcontrib/robust_predicate_transfer/sql/rpt_forward_faults.sql create mode 100644 gpcontrib/robust_predicate_transfer/src/rptforward.cpp create mode 100644 gpcontrib/robust_predicate_transfer/src/rptforwardplan.cpp create mode 100644 gpcontrib/robust_predicate_transfer/src/rptlocal.c diff --git a/gpcontrib/robust_predicate_transfer/Makefile b/gpcontrib/robust_predicate_transfer/Makefile index 42014b73dff..a872465ba37 100644 --- a/gpcontrib/robust_predicate_transfer/Makefile +++ b/gpcontrib/robust_predicate_transfer/Makefile @@ -29,8 +29,11 @@ OBJS = \ $(WIN32RES) \ src/rptgraph.o \ src/rpteligible.o \ + src/rptforward.o \ + src/rptforwardplan.o \ src/rptinit.o \ src/rptinject.o \ + src/rptlocal.o \ src/rptplan.o \ src/rptregion.o @@ -39,7 +42,7 @@ PGFILEDESC = "robust_predicate_transfer - RPT+ transfer plans over Anser" EXTENSION = robust_predicate_transfer DATA = robust_predicate_transfer--1.0.sql -REGRESS = rpt_regions rpt_graph rpt_backward_eligibility +REGRESS = rpt_regions rpt_graph rpt_backward_eligibility rpt_forward rpt_forward_faults # Anser is a sibling module, not a linked library: it is preloaded and dfmgr.c # loads modules RTLD_GLOBAL, so the AnserXxx entry points resolve at load time. @@ -110,7 +113,7 @@ unitcheck: test/graph_test test/inject_test ./test/graph_test ./test/inject_test -test/graph_test: $(srcdir)/test/graph_test.cpp $(wildcard $(srcdir)/src/*.cpp) $(srcdir)/include/rpt.h +test/graph_test: $(srcdir)/test/graph_test.cpp $(wildcard $(srcdir)/src/*.cpp) $(srcdir)/include/rpt.h $(srcdir)/include/rptqual.h $(CXX) $(CXXFLAGS) $(CPPFLAGS) $(PG_CPPFLAGS) $< -o $@ test/inject_test: $(srcdir)/test/inject_test.cpp $(srcdir)/src/rptinject.cpp $(srcdir)/include/rpt.h diff --git a/gpcontrib/robust_predicate_transfer/docs/design.md b/gpcontrib/robust_predicate_transfer/docs/design.md index 9c3136ea83e..33267d65c75 100644 --- a/gpcontrib/robust_predicate_transfer/docs/design.md +++ b/gpcontrib/robust_predicate_transfer/docs/design.md @@ -186,8 +186,10 @@ filter may only ever *over*-approximate the build side's key set. consumers, QD channels or blocking waits for them. - Channels are statement-scoped. The broadcast is delivered once per consuming QE; fan-out is capped per class. There is no session filter cache. -- A future forward reducer must run inside the same statement and dispatch, - preserving the snapshot, parameters and Anser command/channel identity. +- The forward reducer (Phase III, below) runs inside the same statement, + as an uncorrelated InitPlan. Cloudberry dispatches it before the main plan, + with the same snapshot and `gp_command_count`, so both dispatches share one + Anser channel namespace. Channels live until `ExecutorEnd`. ### Parallel workers and segment completeness @@ -298,8 +300,99 @@ The SQL suite has been added but has not been executed on a local cluster. No eligibility percentages or go/no-go conclusion are available yet. This section must be updated with measured results before adding runtime nodes. -Phase III's reducer must likewise live inside the same statement (for example -Sequence plus Append); no cross-dispatch state is planned. +Phase III's reducer likewise lives inside the same statement; its only state +crossing the InitPlan/main dispatch boundary is the statement's Anser channels. + +## Phase III forward reducer (implemented, not yet run on a cluster) + +Injection happens in the planner hook, on the coordinator, for top-level +`SELECT`s without `INTO`/`COPY`/`REFRESH` targets. It requires `rpt.enable`, +`rpt.forward`, `anser.enable=on` and `anser.runtime_filter=off`, and leaves +plans that already carry Anser nodes alone. Any construction error warns and +keeps the original plan; cancellation propagates. + +Selection (`rptforwardplan.cpp`, unit-tested) works per forward root, which is +the largest relation of its component and may differ from the backward +(Phase II) root: + +1. The chain is `reducer_order` restricted to vertices whose filters reach the + root, root excluded; producers come before consumers. +2. Each chain vertex's forward out-edges become channels, keyed by the first + key pair. Filters are sized by `AnserRuntimeFilterSize` from the source's + estimated rows. + A filter is sized for the rows its reducer scan inserts: the table's + rows per segment (`reltuples`) times the fraction its own reducer quals + keep -- all rows when those quals were dropped. +3. Exchange cost: `c × n_producers × filter_MiB` per channel, with + `c = rpt.forward_ms_per_mib_part` and `n_producers` the number of + primaries; a chain's cost is the sum of its channels (hops run serially). + An in-process hop (below) costs nothing. +4. Benefit: the fraction of root rows the chain is expected to keep. A + vertex keeps what its own reducer quals keep (`plan_rows / reltuples` per + segment; 1 when dropped or unknown) times what its inbound filters keep; a + filter from `u` keeps `keep(u) × min(1, ndv(u.key) / ndv(v.key))` of `v`, + assuming the smaller key domain is contained in the larger. NDVs come + from `pg_statistic`; unknown NDV means `keep(u)`. +5. Gates, in order: no forward edges; root rows below + `rpt.forward_min_root_rows`; expected root selectivity not below + `rpt.forward_max_selectivity` (the RPT+ paper's λ = 0.35); estimated cost + above `rpt.forward_max_exchange_ms`. + +Construction (`rptforward.cpp`): every chain table must be a plain Seq Scan in +the main plan, and the root must be one whose output carries its key. For each +chain vertex, the reducer gets a key-only copy of its scan with its own quals, +wrapped in consumers for its incoming channels and then producers for its +outgoing ones. + +Rule 13 for the reducer: the quals are copied whole only when every one is +provably unable to fail -- built from Vars, Consts, AND/OR/NOT, NULL and +boolean tests, binary-compatible relabels, and an explicit allowlist of +built-in integer, OID and boolean comparisons. Hashed array operations must +also use allowlisted hash and negator functions. LEAKPROOF alone is not a +proof of error-free evaluation: a leakproof user function can fail for reasons +unrelated to its arguments. No arbitrary functions, arithmetic, coercions, +Params or SubPlans are accepted. Otherwise all are +dropped. The reducer runs eagerly, before the main plan and over rows the +main plan may never read (`LIMIT 0`, an empty outer side): copying +`100 / a > 1` would turn a successful query into a division-by-zero error +that no planner fail-open can catch. Dropping quals keeps the reducer's rows a +superset, and the selectivity estimate and filter sizing see the drop. + +In-process hops (rule 16, `rptlocal.c`): a local hop (`rpt_local_hop`: the +destination's rows that can match live on the producer's segment) whose +filter is consumed only inside the reducer is handed over without Anser. +`RPT Local Bloom Producer` builds a `lib/bloomfilter.h` filter while passing +rows through and, when its child is exhausted, leaves it in a per-execution +registry (keyed by EState and condition, unregistered by a reset callback on +the query context); the `RPT Local Bloom Consumer` of a later Sequence child +probes it. A consumer that finds no completed filter passes everything. A +filter the main plan also consumes -- every hop into the root, and every hop +under `rpt.forward_apply=all` -- is published through Anser instead. The main plan's root scan (and with +`rpt.forward_apply=all`, every chain scan) is wrapped in consumers. The +children are attached with `RptAttachReducerInitPlan`: one EXISTS InitPlan, +Gather Motion over a Sequence, so its dispatch completes before the main one. + +Connection lifetime: the reducer's dispatcher releases its gangs in +`ExecSetParamPlan`, long before `ExecutorEnd`, and every dispatcher does so +early on the error path. Anser keeps `PGconn` pointers for deliveries and for +partially flushed chunks, and `cleanupQE` destroys connections with unflushed +output. Before retaining a connection pointer, Anser registers a libpq event +handler. `PGEVT_CONNDESTROY` forgets all of that connection's deliveries and +queued-chunk references before `PQfinish` frees it, including on InitPlan +and error cleanup paths. `PGEVT_CONNRESET` drops state for the previous stream. +Cloudberry hides public PQ event APIs in the backend, so Anser installs its +entry using the existing libpq-int.h event layout and libc allocator; it does +not link a second libpq or require new exported core symbols. +The callbacks do not allocate, log, wait, or scan dynahash. Failed registration +refuses the subscription, so the consumer times out unfiltered. A recycled +connection that stays allocated remains safe to reference; completed channel +payloads stay available to the main dispatch. No core hook is required. + +Known limits of this version: only Seq Scan targets; a single key per channel; +the selectivity estimate assumes independence and key containment; the cost +model was calibrated on one host with three primaries. `rpt.debug` dumps and +`rpt_transfer_plan()` report the selection with `decision=` reasons, the +estimated selectivity (`est_sel=`), in-process hops and dropped quals. ## GUCs @@ -310,6 +403,12 @@ Sequence plus Append); no cross-dispatch state is planned. | `rpt.backward` | on | QD-only runtime switch reserved for Phase II; disable during the survey. | | `rpt.max_backward_consumers` | 4 | QD-only cap on consumers per class, applied to analysis decisions. | | `rpt.max_chain_depth` | 4 | Enforced maximum dependency hops; default provisional until hop latency is measured. | +| `rpt.forward` | on | Build the Phase III forward reducer when `rpt.enable` is on. | +| `rpt.forward_apply` | root | Where the main plan consumes forward filters: `root` only, or `all` chain scans too. | +| `rpt.forward_ms_per_mib_part` | 3.5 | Cost model `c`, ms per MiB of filter per producer part. | +| `rpt.forward_max_exchange_ms` | 100 | Refuse a chain whose estimated exchange exceeds this; −1 disables the gate. | +| `rpt.forward_min_root_rows` | 10000 | Refuse a chain whose root is estimated below this many rows per segment. | +| `rpt.forward_max_selectivity` | 0.35 | λ: refuse a chain expected to keep at least this fraction of the root's rows; 1 disables. | Setup: `shared_preload_libraries = 'anser,robust_predicate_transfer'`, in that order (the extension resolves Anser symbols at load time). The analysis UDF diff --git a/gpcontrib/robust_predicate_transfer/docs/phase3-experiments.md b/gpcontrib/robust_predicate_transfer/docs/phase3-experiments.md new file mode 100644 index 00000000000..2d003ca199b --- /dev/null +++ b/gpcontrib/robust_predicate_transfer/docs/phase3-experiments.md @@ -0,0 +1,124 @@ +# Phase III experiment plan + +Goal: find out whether the forward reducer is correct on a real cluster, what +it costs, where it pays off, and whether `c × n_producers × filter_MiB` is a +good enough gate. Experiments are ordered; each states what blocks the next. + +Common setup for every run: + +- `shared_preload_libraries = 'anser,robust_predicate_transfer'`; restart the + cluster after installing new binaries on every host. +- Record: commit hash, number of hosts and primaries, interconnect type, + `gp_vmem`/`work_mem`, both `optimizer` settings. +- Per query, keep the `rpt_transfer_plan()` dump, `EXPLAIN (ANALYZE, FORMAT + JSON)` and wall time. Reject any run with an "RPT forward reducer skipped" + WARNING in the log, unless the experiment is about that warning. +- Timings: 1 warm-up, then 10 runs; report median and p95. + +## E0. Correctness gate (blocks everything) + +1. `make installcheck` in `gpcontrib/anser` and in + `gpcontrib/robust_predicate_transfer`, which now includes `rpt_forward` + and `rpt_forward_faults` (the latter needs a build with fault injection). +2. `make installcheck` (core) with `rpt.enable=on`, `rpt.forward=on`, + `rpt.forward_min_root_rows=0` and `rpt.forward_max_exchange_ms=-1` set in + `PGOPTIONS`. Failures count only when they disappear with + `rpt.enable=off`. This is the broad crash and wrong-result sweep. +3. The same with `rpt.forward_apply=all`. +4. Cancel and error paths: `pg_cancel_backend` during the reducer; a segment + `gp_inject_fault` error inside an Anser producer; `statement_timeout` + shorter than the reducer. Expect a clean error, no hung QEs and no leaked + channels on the next statement. + +Pass: zero result differences. Any wrong result stops the plan. + +## E1. Cross-dispatch proof + +On the `rpt_forward` star fixture, under both optimizers: in the main plan, +`Rows Removed by Bloom Filter` plus `Rows Removed by Pushdown Runtime Filter` +is greater than 0. Also check with `gp_log_interconnect`/Anser tracing that the +producer channels are created in the InitPlan's dispatch and consumed in the +main dispatch, with the same `gp_command_count`. + +## E2. Fixed overhead of the InitPlan + +`rpt.debug_noop_initplan` with `scripts/measure_initplan.py` +(see `phase3-measurements.md`). Measure on 3, 8 and 24 primaries, and on 1 vs +several hosts. Output: the extra dispatch latency per statement. This is the +floor any accepted chain must beat; it should become a constant term in the +gate if it exceeds a few milliseconds. + +## E3. Calibrating c + +Single-channel star (one dimension, filter sized to 1, 8 and 64 MiB) with +forward-only RPT. For each topology (3/8/24 primaries, 1 and 2+ hosts): + +- measured exchange = reducer InitPlan time − reducer scan time; +- fit `measured = a + c × n_producers × MiB`. + +Pass: the fit is within 25% of measured times, and `c` stays within 2× of 3.5 +across topologies. Otherwise, make `c` per topology or change the model, for +example `c₁ × n_producers × MiB + c₂ × n_consumers × MiB` for delivery. + +Small filters (64–512 KiB) need Anser's 1 MiB floor lowered. That requires a +debug-only GUC in Anser first; it is optional and should run only if E4 shows +many small filters. + +## E4. Star benchmarks: where it pays off + +Fixtures: 1 fact table (10M, 100M rows), 1–4 dimensions, selectivity of each +dimension 0.1%, 1%, 10%, 50%, 100%. Four configurations: + +| Config | Settings | +| --- | --- | +| baseline | `anser.enable=off` | +| Anser RF | `anser.enable=on`, `anser.runtime_filter=on` | +| RPT root | `rpt.enable=on`, `anser.runtime_filter=off`, `rpt.forward_apply=root` | +| RPT all | as above, `rpt.forward_apply=all` | + +Run first with the gates open (`min_root_rows=0`, `max_exchange_ms=-1`, +`max_selectivity=1`) to measure real gains and losses, then with default +gates to see what the gates keep. Record `est_sel` from the dump next to the +measured fraction of root rows removed, to check the selectivity estimate. Output per query: speedup against baseline and against Anser RF, plus the +gate decision. Expected: wins at low selectivity and large facts; losses at +50–100% selectivity, which the gates should refuse. + +## E5. Chains and snowflakes + +Snowflake schemas (fact → dim → subdim, depth 2–4) and two-fact queries, +where transitive transfer is the point of RPT+. Compare RPT root/all with +Anser RF. This checks that serial hops add up as the model assumes (sum of +channels), and gives data for `rpt.max_chain_depth`. + +## E6. Workloads + +JOB (IMDB), TPC-H SF100 and TPC-DS SF100, under both optimizers: + +1. Survey: for every query, the dump's forward decision and refusal reason + (e.g. "no forward edges", gates, non-Seq-Scan targets in preflight). +2. Benchmark: baseline / Anser RF / RPT root, using default gates. +3. Report regressions per query (> 5% slower than the better of baseline and + Anser RF), each with its dump. These decide the default gate values. + +## E7. Gate tuning + +From E3–E6, choose defaults for `rpt.forward_ms_per_mib_part`, +`rpt.forward_max_exchange_ms`, `rpt.forward_min_root_rows` and +`rpt.forward_max_selectivity` (λ = 0.35 is the paper's value, not ours). A stronger gate +would compare the cost to estimated savings (removed root rows × per-row cost +above the root). Add that only if E6 shows cost-only gating either refuses +winners or admits losers. + +## Open items found while implementing + +- In-process hops are implemented for local hops consumed only inside the + reducer. E5 should compare a colocated snowflake with a random one to + measure what the in-process path saves. +- Reducer quals that are not provably error-free (non-leakproof functions, + I/O casts) are dropped whole. E6's survey should count how often that + happens; if it is frequent, keeping the provably safe subset is a + follow-up. +- Only Seq Scan targets, one key per channel. E6's survey shows how many + queries this excludes. +- Publish-start timestamp in Anser: needed for exact exchange timing in E3. + The current proxy starts at serialization. diff --git a/gpcontrib/robust_predicate_transfer/docs/phase3-measurements.md b/gpcontrib/robust_predicate_transfer/docs/phase3-measurements.md new file mode 100644 index 00000000000..182a45dd4aa --- /dev/null +++ b/gpcontrib/robust_predicate_transfer/docs/phase3-measurements.md @@ -0,0 +1,87 @@ +# Phase III measurement status + +## Exchange measurements, 2026-11-10 + +The supplied `anser-20261110` results cover three primaries on one host. +Eight- and 24-primary topologies were unavailable. The fixture was one +cross-Motion Anser channel under GPORCA, with 10,000 build keys and a +1,000,000-row probe. All 90 measured exchanges completed with three producer +parts, one complete merge and three complete consumers; traced query results +were 10,000. Anser regressions passed 4/4. + +| Bitset | Exchange proxy median / p95, ms | Delivery tail median / p95, ms | Untraced execution median off / on, ms | +| --- | --- | --- | --- | +| 1 MiB | 11.188 / 17.082 | 1.197 / 1.711 | 89.382 / 33.613 | +| 8 MiB | 82.946 / 87.325 | 6.223 / 7.770 | 87.834 / 121.425 | +| 64 MiB | 765.530 / 789.889 | 48.387 / 58.273 | 89.446 / 880.313 | + +The proxy begins at the trace immediately before serialization, not an exact +publish-start timestamp. Trace timings include logging overhead. Off/on +execution differences combine construction, transport and filtering; they +are not network timings. The sample CSV medians and nearest-rank p95 values +were checked against the supplied summary. These observations do not establish +multi-host scaling or Phase III performance. + +Payload size must participate in the forward cost estimate. A depth cap alone +cannot distinguish an approximately 11 ms exchange from a 766 ms exchange. +Keep the current chain-depth default explicitly provisional. Dispatch cost, +reducer scan cost and representative root-scan savings remain unmeasured. + +RPT regressions passed 2/3. The nullable-side fixture in +`rpt_backward_eligibility`, with optimizer off, expected an eligible producer +but received `producer is not on a qualifying hash build side`. The supplied physical +EXPLAIN confirms the LEFT join simplifies to INNER, but the root is the +upper hash join's probe and the outer/member subtree is its build. Rejection +is correct. The regression now checks the named scans' physical build/probe +placement before expecting eligibility; this test correction still needs a +cluster rerun. + +## Exact no-op InitPlan harness + +`rpt.debug_noop_initplan` is a superuser-only, QD-only diagnostic switch, +default off and independent of `rpt.enable`. It clones the finished statement +and appends the production builder's empty reducer: + +```text +InitPlan (EXISTS, unused boolean PARAM_EXEC) + Gather Motion + Sequence + Result (One-Time Filter: false) +``` + +Its reader gang covers every primary segment, with no parallel workers or +direct dispatch. It builds no filters and tests no cross-dispatch channels. +Construction errors warn and retain the original plan; cancellation propagates. +**Reject measurements with a skipped-InitPlan warning or missing plan shape.** + +After rebuilding and installing the extension on all hosts, restart the test +cluster so all backends use the same binary. Use a fresh superuser connection +and an idle cluster, with the existing Anser/RPT preload order. From the +repository root: + +```sh +python3 gpcontrib/robust_predicate_transfer/scripts/measure_initplan.py \ + > /tmp/rpt-initplan.sql +psql -X -v ON_ERROR_STOP=1 -d rpt_exchange_bench \ + -f /tmp/rpt-initplan.sql > /tmp/rpt-initplan.out 2>&1 +``` + +The script uses a temporary, randomly distributed 3,000-row table. It emits a +shape EXPLAIN and a query with an existing scalar subquery for both optimizers, +then 10 warmups and 100 measured pairs per optimizer. The existing-subquery +query must return 3,000. Each pair runs the same count with the diagnostic +switch off and on; first-arm order alternates. Runtime filters and tracing +are disabled. `SAMPLE initplan ` labels +precede each measured EXPLAIN ANALYZE. + +Check that both shape plans contain the new InitPlan, Gather, Sequence and +false Result. Record planning time, execution time and psql elapsed time for +each labeled EXPLAIN ANALYZE; ignore times printed for SET commands. Client +elapsed time matters because distributed InitPlans run during ExecutorStart, +not necessarily inside the reported main-plan execution interval. + +Report median and p95 of each arm, plus the median and p95 of paired on-minus-off +client elapsed times, separately by optimizer. Preserve raw output and relevant +QD/QE logs. The harness has passed native layout tests and C++ syntax checks; +cluster execution remains unvalidated. This measurement is the next executor +smoke test, not proof of filter delivery between dispatches. diff --git a/gpcontrib/robust_predicate_transfer/expected/rpt_backward_eligibility.out b/gpcontrib/robust_predicate_transfer/expected/rpt_backward_eligibility.out index bde24939160..db9aa1e785d 100644 --- a/gpcontrib/robust_predicate_transfer/expected/rpt_backward_eligibility.out +++ b/gpcontrib/robust_predicate_transfer/expected/rpt_backward_eligibility.out @@ -100,6 +100,8 @@ DECLARE q text; dump text; path text[]; + producer_path text[]; + split_at integer; producer_id text; correlated text; BEGIN @@ -150,7 +152,25 @@ BEGIN IF EXISTS (SELECT FROM unnest(path) tag WHERE tag LIKE '%:Left:Inner' OR tag LIKE '%:Right:Outer') THEN PERFORM pg_temp.rpt_expect(q, 'rpt_elig_root', 'rpt_elig_member', ARRAY['nullable side of outer join'], false); ELSE - PERFORM pg_temp.rpt_expect(q, 'rpt_elig_root', 'rpt_elig_member', ARRAY['eligible'], true); + -- Simplification does not fix hash build/probe order. Compare + -- the first differing path step: the shared parent is the join + -- separating these named scans, regardless of range-table IDs. + producer_path := pg_temp.rpt_scan_path(q, 'rpt_elig_root'); + IF producer_path IS NULL THEN + RAISE EXCEPTION 'nullable-side fixture lost its root scan'; + END IF; + SELECT min(n) INTO split_at FROM generate_subscripts(path, 1) n + WHERE path[n] IS DISTINCT FROM producer_path[n]; + IF split_at IS NULL THEN + RAISE EXCEPTION 'nullable-side fixture has no separating join'; + END IF; + IF producer_path[split_at] LIKE 'Hash Join:%:Outer' + AND path[split_at] LIKE 'Hash Join:%:Inner' THEN + PERFORM pg_temp.rpt_expect(q, 'rpt_elig_root', 'rpt_elig_member', + ARRAY['producer is not on a qualifying hash build side'], false); + ELSE + PERFORM pg_temp.rpt_expect(q, 'rpt_elig_root', 'rpt_elig_member', ARRAY['eligible'], true); + END IF; END IF; -- The aggregate scalar subquery commonly becomes an InitPlan in the -- PostgreSQL planner. ORCA may instead use a join, without extParam. diff --git a/gpcontrib/robust_predicate_transfer/expected/rpt_forward.out b/gpcontrib/robust_predicate_transfer/expected/rpt_forward.out new file mode 100644 index 00000000000..18516893a34 --- /dev/null +++ b/gpcontrib/robust_predicate_transfer/expected/rpt_forward.out @@ -0,0 +1,343 @@ +--------------------------------------------------------------------------- +-- +-- Licensed to the Apache Software Foundation (ASF) under one +-- or more contributor license agreements. See the NOTICE file +-- distributed with this work for additional information +-- regarding copyright ownership. The ASF licenses this file +-- to you under the Apache License, Version 2.0 (the +-- "License"); you may not use this file except in compliance +-- with the License. You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, +-- software distributed under the License is distributed on an +-- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +-- KIND, either express or implied. See the License for the +-- specific language governing permissions and limitations +-- under the License. +-- +-- Phase III forward reducer, end to end: the InitPlan is built, its filters +-- reach the main plan across the dispatch boundary, intermediate hops work +-- both through Anser and in-process, and results never change. Both +-- optimizers; every check raises with the plan or dump that failed it. +-- Faults live in rpt_forward_faults. +CREATE EXTENSION robust_predicate_transfer; +SET anser.runtime_filter = off; +SET statement_timeout = '120s'; +-- Star: two dimensions on two different fact keys (two hops into the root). +CREATE TABLE rpt_fwd_fact (id int, d1 int, d2 int, v int) DISTRIBUTED RANDOMLY; +CREATE TABLE rpt_fwd_d1 (k int, a int) DISTRIBUTED RANDOMLY; +CREATE TABLE rpt_fwd_d2 (k int, b int) DISTRIBUTED RANDOMLY; +CREATE TABLE rpt_fwd_empty (k int, a int) DISTRIBUTED RANDOMLY; +-- Snowflake: fact -> dimension -> subdimension, a chain of two hops whose +-- first hop is consumed inside the reducer. rpt_fwd_dl and rpt_fwd_sub are +-- distributed on their join columns, so that hop is local (in-process); +-- rpt_fwd_dr is the same dimension distributed randomly, so the hop is global. +CREATE TABLE rpt_fwd_dl (k int, s int) DISTRIBUTED BY (s); +CREATE TABLE rpt_fwd_dr (k int, s int) DISTRIBUTED RANDOMLY; +CREATE TABLE rpt_fwd_sub (k int, c int) DISTRIBUTED BY (k); +INSERT INTO rpt_fwd_fact + SELECT i, CASE WHEN i % 97 = 0 THEN NULL ELSE i % 1000 END, i % 500, i + FROM generate_series(1, 200000) i; +INSERT INTO rpt_fwd_d1 SELECT i, i FROM generate_series(0, 999) i; +INSERT INTO rpt_fwd_d2 SELECT i, i FROM generate_series(0, 499) i; +INSERT INTO rpt_fwd_dl SELECT i, i % 100 FROM generate_series(0, 999) i; +INSERT INTO rpt_fwd_dr SELECT i, i % 100 FROM generate_series(0, 999) i; +INSERT INTO rpt_fwd_sub SELECT i, i FROM generate_series(0, 99) i; +ANALYZE rpt_fwd_fact; +ANALYZE rpt_fwd_d1; +ANALYZE rpt_fwd_d2; +ANALYZE rpt_fwd_empty; +ANALYZE rpt_fwd_dl; +ANALYZE rpt_fwd_dr; +ANALYZE rpt_fwd_sub; +-- Legal LEAKPROOF function: the error reveals nothing about its argument. +-- It is still unsafe for an eager reducer when the main scan never executes. +CREATE FUNCTION pg_temp.rpt_fwd_constant_error(integer) RETURNS boolean +LANGUAGE plpgsql IMMUTABLE LEAKPROOF AS $$ +BEGIN + RAISE EXCEPTION 'constant failure'; +END +$$; +CREATE FUNCTION pg_temp.rpt_fwd_plan(q text, analyze boolean) RETURNS jsonb +LANGUAGE plpgsql AS $$ +DECLARE + plan jsonb; +BEGIN + EXECUTE format('EXPLAIN (%s FORMAT JSON) %s', + CASE WHEN analyze THEN 'ANALYZE,' ELSE '' END, q) INTO plan; + RETURN plan; +END +$$; +-- Result of q as one comparable text value, with RPT on or off. +CREATE FUNCTION pg_temp.rpt_fwd_result(q text, rpt boolean) RETURNS text +LANGUAGE plpgsql AS $$ +DECLARE + r text; +BEGIN + PERFORM set_config('rpt.enable', CASE WHEN rpt THEN 'on' ELSE 'off' END, true); + EXECUTE format('SELECT string_agg(t::text, %L ORDER BY t::text) FROM (%s) t', ';', q) INTO r; + PERFORM set_config('rpt.enable', 'on', true); + RETURN coalesce(r, ''); +END +$$; +-- Nodes of a provider inside InitPlans (reducer) or outside them (main plan); +-- a NULL provider matches every node. +CREATE FUNCTION pg_temp.rpt_fwd_nodes(plan jsonb, provider text, in_reducer boolean) +RETURNS SETOF jsonb LANGUAGE sql AS $$ + WITH RECURSIVE walk(node, in_init) AS ( + SELECT plan->0->'Plan', false + UNION ALL + SELECT child, in_init OR coalesce(child->>'Parent Relationship', '') = 'InitPlan' + FROM walk CROSS JOIN LATERAL jsonb_array_elements(coalesce(node->'Plans', '[]')) child + ) + SELECT node FROM walk + WHERE in_init = in_reducer AND (provider IS NULL OR node->>'Custom Plan Provider' = provider) +$$; +CREATE FUNCTION pg_temp.rpt_fwd_count(plan jsonb, provider text, in_reducer boolean) +RETURNS bigint LANGUAGE sql AS $$ + SELECT count(*) FROM pg_temp.rpt_fwd_nodes(plan, provider, in_reducer) +$$; +-- Rows removed by forward filters in the main plan: by a consumer itself, or +-- by the Seq Scan it pushed the filter into. +CREATE FUNCTION pg_temp.rpt_fwd_removed(plan jsonb) RETURNS float8 +LANGUAGE sql AS $$ + SELECT coalesce(sum(coalesce((node->>'Rows Removed by Bloom Filter')::float8, 0) + + coalesce((node->>'Rows Removed by Pushdown Runtime Filter')::float8, 0)), 0) + FROM pg_temp.rpt_fwd_nodes(plan, NULL, false) node +$$; +DO $$ +DECLARE + opt text; + q text; + mode text; + dump text; + plan jsonb; + prep_n bigint; + prep_s bigint; + star constant text := + 'SELECT count(*) AS n, sum(f.v) AS s FROM rpt_fwd_fact f + JOIN rpt_fwd_d1 d1 ON f.d1 = d1.k JOIN rpt_fwd_d2 d2 ON f.d2 = d2.k + WHERE d1.a < 50 AND d2.b < 100'; + flake_local constant text := + 'SELECT count(*) AS n, sum(f.v) AS s FROM rpt_fwd_fact f + JOIN rpt_fwd_dl d ON f.d1 = d.k JOIN rpt_fwd_sub u ON d.s = u.k + WHERE u.c < 5'; + flake_global constant text := + 'SELECT count(*) AS n, sum(f.v) AS s FROM rpt_fwd_fact f + JOIN rpt_fwd_dr d ON f.d1 = d.k JOIN rpt_fwd_sub u ON d.s = u.k + WHERE u.c < 5'; +BEGIN + PERFORM set_config('rpt.enable', 'on', true); + PERFORM set_config('rpt.forward_min_root_rows', '0', true); + PERFORM set_config('rpt.forward_max_exchange_ms', '-1', true); + FOREACH opt IN ARRAY ARRAY['off', 'on'] LOOP + PERFORM set_config('optimizer', opt, true); + PERFORM set_config('rpt.forward_apply', 'root', true); + + -- Shape: a reducer InitPlan with producers, a consumer in the main plan. + dump := rpt_transfer_plan(star); + IF dump NOT LIKE '%decision=inject%' THEN + RAISE EXCEPTION 'star not selected (optimizer=%): %', opt, dump; + END IF; + plan := pg_temp.rpt_fwd_plan(star, false); + IF pg_temp.rpt_fwd_count(plan, 'Anser Bloom Producer', true) = 0 OR + pg_temp.rpt_fwd_count(plan, 'Anser Bloom Consumer', false) = 0 THEN + RAISE EXCEPTION 'reducer shape missing (optimizer=%): %', opt, plan; + END IF; + + -- The property the design rests on: filters produced in the InitPlan's + -- dispatch are applied by the main plan's dispatch. + plan := pg_temp.rpt_fwd_plan(star, true); + IF pg_temp.rpt_fwd_removed(plan) <= 0 THEN + RAISE EXCEPTION 'no rows removed by a forward filter in the main plan (optimizer=%): %', opt, plan; + END IF; + + -- Multi-hop, global: the intermediate hop is an Anser consumer + -- inside the reducer, fed by a producer of the same dispatch. + dump := rpt_transfer_plan(flake_global); + IF dump NOT LIKE '%decision=inject%' OR dump NOT LIKE '%channels=2 %' OR + dump LIKE '%in-process%' THEN + RAISE EXCEPTION 'global chain not selected as two Anser hops (optimizer=%): %', opt, dump; + END IF; + plan := pg_temp.rpt_fwd_plan(flake_global, true); + IF pg_temp.rpt_fwd_count(plan, 'Anser Bloom Consumer', true) = 0 OR + pg_temp.rpt_fwd_count(plan, 'Anser Bloom Producer', true) < 2 OR + pg_temp.rpt_fwd_removed(plan) <= 0 THEN + RAISE EXCEPTION 'global chain shape or effect missing (optimizer=%): %', opt, plan; + END IF; + + -- Multi-hop, local: the intermediate hop never leaves the QE. + dump := rpt_transfer_plan(flake_local); + IF dump NOT LIKE '%decision=inject%' OR dump NOT LIKE '%local in-process%' THEN + RAISE EXCEPTION 'local hop not in-process (optimizer=%): %', opt, dump; + END IF; + plan := pg_temp.rpt_fwd_plan(flake_local, true); + IF pg_temp.rpt_fwd_count(plan, 'RPT Local Bloom Producer', true) <> 1 OR + pg_temp.rpt_fwd_count(plan, 'RPT Local Bloom Consumer', true) <> 1 OR + pg_temp.rpt_fwd_count(plan, 'Anser Bloom Consumer', true) <> 0 OR + (SELECT sum((n->>'Rows Removed by Local Bloom Filter')::float8) + FROM pg_temp.rpt_fwd_nodes(plan, 'RPT Local Bloom Consumer', true) n) <= 0 OR + pg_temp.rpt_fwd_removed(plan) <= 0 THEN + RAISE EXCEPTION 'local chain shape or effect missing (optimizer=%): %', opt, plan; + END IF; + + -- forward_apply=all: the chain's main-plan scans consume too, so the + -- local hop is published through Anser instead. + PERFORM set_config('rpt.forward_apply', 'all', true); + dump := rpt_transfer_plan(flake_local); + plan := pg_temp.rpt_fwd_plan(flake_local, false); + IF dump LIKE '%in-process%' OR + pg_temp.rpt_fwd_count(plan, 'Anser Bloom Consumer', false) < 2 OR + pg_temp.rpt_fwd_count(plan, 'RPT Local Bloom Consumer', true) <> 0 THEN + RAISE EXCEPTION 'forward_apply=all shape wrong (optimizer=%): % %', opt, dump, plan; + END IF; + + -- Results never change, under both apply modes: star, chains, empty + -- filters, NULL keys, a volatile qual (dropped whole in the reducer), + -- an empty dimension, a preserved side. + FOREACH q IN ARRAY ARRAY[ + star, + flake_local, + flake_global, + 'SELECT f.id, f.v FROM rpt_fwd_fact f JOIN rpt_fwd_d1 d1 ON f.d1 = d1.k WHERE d1.a < 3', + 'SELECT count(*) FROM rpt_fwd_fact f JOIN rpt_fwd_d1 d1 ON f.d1 = d1.k WHERE d1.a < 0', + 'SELECT count(*) FROM rpt_fwd_fact f JOIN rpt_fwd_d1 d1 ON f.d1 = d1.k WHERE d1.a IN (0,1,2,3,4,5,6,7,8,9)', + 'SELECT count(*) FROM rpt_fwd_fact f JOIN rpt_fwd_d1 d1 ON f.d1 = d1.k WHERE d1.a NOT IN (0,1,2,3,4,5,6,7,8,9)', + 'SELECT count(*) FROM rpt_fwd_fact f JOIN rpt_fwd_d1 d1 ON f.d1 = d1.k WHERE d1.a < 50 AND random() >= 0', + 'SELECT count(*) FROM rpt_fwd_fact f JOIN rpt_fwd_empty e ON f.d1 = e.k WHERE e.a < 50', + 'SELECT count(*) FROM rpt_fwd_fact f LEFT JOIN rpt_fwd_d1 d1 ON f.d1 = d1.k AND d1.a < 50', + 'SELECT d.k, count(*) FROM rpt_fwd_fact f JOIN rpt_fwd_dl d ON f.d1 = d.k + JOIN rpt_fwd_sub u ON d.s = u.k WHERE u.c IN (1, 7) GROUP BY d.k' + ] LOOP + FOREACH mode IN ARRAY ARRAY['root', 'all'] LOOP + PERFORM set_config('rpt.forward_apply', mode, true); + IF pg_temp.rpt_fwd_result(q, true) IS DISTINCT FROM pg_temp.rpt_fwd_result(q, false) THEN + RAISE EXCEPTION 'results differ with RPT (optimizer=%, apply=%): %', opt, mode, q; + END IF; + END LOOP; + END LOOP; + PERFORM set_config('rpt.forward_apply', 'root', true); + + -- A prepared statement keeps its reducer and its answer. + EXECUTE 'PREPARE rpt_fwd_prep AS ' || star; + FOR i IN 1..2 LOOP + EXECUTE 'EXECUTE rpt_fwd_prep' INTO prep_n, prep_s; + IF row(prep_n, prep_s)::text IS DISTINCT FROM pg_temp.rpt_fwd_result(star, false) THEN + RAISE EXCEPTION 'prepared statement result differs (optimizer=%, run %)', opt, i; + END IF; + END LOOP; + EXECUTE 'DEALLOCATE rpt_fwd_prep'; + + -- Gates: below the root-row threshold, nothing is injected. + PERFORM set_config('rpt.forward_min_root_rows', '1e12', true); + IF rpt_transfer_plan(star) NOT LIKE '%decision=root rows below rpt.forward_min_root_rows%' OR + pg_temp.rpt_fwd_count(pg_temp.rpt_fwd_plan(star, false), NULL, true) <> 0 THEN + RAISE EXCEPTION 'root-row gate did not refuse (optimizer=%)', opt; + END IF; + PERFORM set_config('rpt.forward_min_root_rows', '0', true); + + -- Gates: an exchange budget below the estimate refuses the chain. + PERFORM set_config('rpt.forward_max_exchange_ms', '0.001', true); + IF rpt_transfer_plan(star) NOT LIKE '%decision=estimated exchange cost above%' THEN + RAISE EXCEPTION 'cost gate did not refuse (optimizer=%)', opt; + END IF; + PERFORM set_config('rpt.forward_max_exchange_ms', '-1', true); + + -- Gates: a dimension keeping 90% of its keys is not worth a reducer. + q := 'SELECT count(*) FROM rpt_fwd_fact f JOIN rpt_fwd_d1 d1 ON f.d1 = d1.k WHERE d1.a < 900'; + IF rpt_transfer_plan(q) NOT LIKE '%decision=estimated root selectivity not below%' OR + pg_temp.rpt_fwd_count(pg_temp.rpt_fwd_plan(q, false), NULL, true) <> 0 THEN + RAISE EXCEPTION 'selectivity gate did not refuse (optimizer=%)', opt; + END IF; + + -- Rule 13: a qual that can fail is never run by the reducer. The + -- reducer runs before the main plan and over rows the main plan may + -- never read; with LIMIT 0 the main plan reads none, so a reducer + -- evaluating 100 / a would raise division by zero (a = 0 exists), and + -- so would an I/O cast it cannot prove safe. With lambda the + -- qual-less chain is refused; with lambda off it is built with no + -- quals. Either way: no error, the same answer. + q := 'SELECT f.id FROM rpt_fwd_fact f JOIN rpt_fwd_d1 d1 ON f.d1 = d1.k WHERE 100 / d1.a > 1'; + dump := rpt_transfer_plan(q); + IF dump NOT LIKE '%dropped in the reducer (not provably error-free)%' OR + dump NOT LIKE '%decision=estimated root selectivity not below%' THEN + RAISE EXCEPTION 'unsafe qual not dropped, or chain not refused (optimizer=%): %', opt, dump; + END IF; + q := 'SELECT f.id FROM rpt_fwd_fact f JOIN rpt_fwd_d1 d1 ON f.d1 = d1.k WHERE pg_temp.rpt_fwd_constant_error(d1.a)'; + IF rpt_transfer_plan(q) NOT LIKE '%dropped in the reducer (not provably error-free)%' THEN + RAISE EXCEPTION 'leakproof user function accepted by reducer (optimizer=%)', opt; + END IF; + FOREACH mode IN ARRAY ARRAY['0.35', '1'] LOOP + PERFORM set_config('rpt.forward_max_selectivity', mode, true); + -- The planner keeps the LIMIT 0 join, so with lambda off the + -- reducer really runs (GPORCA may fold the query away instead). + q := 'SELECT f.id FROM rpt_fwd_fact f JOIN rpt_fwd_d1 d1 ON f.d1 = d1.k WHERE 100 / d1.a > 1 LIMIT 0'; + IF mode = '1' AND opt = 'off' AND rpt_transfer_plan(q) NOT LIKE '%decision=inject%' THEN + RAISE EXCEPTION 'LIMIT 0 reducer not built with lambda off: %', rpt_transfer_plan(q); + END IF; + FOREACH q IN ARRAY ARRAY[ + 'SELECT f.id FROM rpt_fwd_fact f JOIN rpt_fwd_d1 d1 ON f.d1 = d1.k WHERE 100 / d1.a > 1 LIMIT 0', + 'SELECT f.id FROM rpt_fwd_fact f JOIN rpt_fwd_d1 d1 ON f.d1 = d1.k WHERE d1.a < 50 AND 100 / d1.a > 1 LIMIT 0', + 'SELECT f.id FROM rpt_fwd_fact f JOIN rpt_fwd_d1 d1 ON f.d1 = d1.k WHERE (d1.a::text)::int < 50 LIMIT 0', + 'SELECT f.id FROM rpt_fwd_fact f JOIN rpt_fwd_d1 d1 ON f.d1 = d1.k WHERE pg_temp.rpt_fwd_constant_error(d1.a) LIMIT 0' + ] LOOP + IF pg_temp.rpt_fwd_result(q, true) <> '' THEN + RAISE EXCEPTION 'LIMIT 0 returned rows (optimizer=%): %', opt, q; + END IF; + END LOOP; + END LOOP; + PERFORM set_config('rpt.forward_max_selectivity', '0.35', true); + -- ... while provably safe quals are kept. + IF rpt_transfer_plan(star) LIKE '%dropped in the reducer%' THEN + RAISE EXCEPTION 'safe quals dropped (optimizer=%)', opt; + END IF; + + -- Exclusivity: with Anser's own runtime filter on, RPT+ stands down. + PERFORM set_config('anser.runtime_filter', 'on', true); + IF rpt_transfer_plan(star) NOT LIKE '%forward: needs anser.enable=on and anser.runtime_filter=off%' OR + pg_temp.rpt_fwd_count(pg_temp.rpt_fwd_plan(star, false), NULL, true) <> 0 THEN + RAISE EXCEPTION 'RPT+ did not stand down for anser.runtime_filter (optimizer=%)', opt; + END IF; + PERFORM set_config('anser.runtime_filter', 'off', true); + END LOOP; +END +$$; +-- PL/pgSQL (SPI) runs the reducer like any other caller. +CREATE FUNCTION pg_temp.rpt_fwd_spi() RETURNS bigint LANGUAGE plpgsql AS $$ +DECLARE + total bigint := 0; + r record; +BEGIN + FOR r IN SELECT f.v FROM rpt_fwd_fact f JOIN rpt_fwd_d1 d1 ON f.d1 = d1.k WHERE d1.a < 50 LOOP + total := total + r.v; + END LOOP; + FOR r IN SELECT f.v FROM rpt_fwd_fact f JOIN rpt_fwd_dl d ON f.d1 = d.k + JOIN rpt_fwd_sub u ON d.s = u.k WHERE u.c < 5 LOOP + total := total + r.v; + END LOOP; + RETURN total; +END +$$; +DO $$ +DECLARE + with_rpt bigint; + without bigint; +BEGIN + PERFORM set_config('rpt.forward_min_root_rows', '0', true); + PERFORM set_config('rpt.forward_max_exchange_ms', '-1', true); + PERFORM set_config('rpt.enable', 'on', true); + with_rpt := pg_temp.rpt_fwd_spi(); + PERFORM set_config('rpt.enable', 'off', true); + without := pg_temp.rpt_fwd_spi(); + IF with_rpt IS DISTINCT FROM without THEN + RAISE EXCEPTION 'SPI result differs: % vs %', with_rpt, without; + END IF; +END +$$; +DROP TABLE rpt_fwd_fact, rpt_fwd_d1, rpt_fwd_d2, rpt_fwd_empty, + rpt_fwd_dl, rpt_fwd_dr, rpt_fwd_sub; +DROP EXTENSION robust_predicate_transfer; +RESET anser.runtime_filter; +RESET statement_timeout; diff --git a/gpcontrib/robust_predicate_transfer/expected/rpt_forward_faults.out b/gpcontrib/robust_predicate_transfer/expected/rpt_forward_faults.out new file mode 100644 index 00000000000..204de98bc23 --- /dev/null +++ b/gpcontrib/robust_predicate_transfer/expected/rpt_forward_faults.out @@ -0,0 +1,158 @@ +--------------------------------------------------------------------------- +-- +-- Licensed to the Apache Software Foundation (ASF) under one +-- or more contributor license agreements. See the NOTICE file +-- distributed with this work for additional information +-- regarding copyright ownership. The ASF licenses this file +-- to you under the Apache License, Version 2.0 (the +-- "License"); you may not use this file except in compliance +-- with the License. You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, +-- software distributed under the License is distributed on an +-- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +-- KIND, either express or implied. See the License for the +-- specific language governing permissions and limitations +-- under the License. +-- +-- Phase III forward reducer under forced transport failures. Kept apart from +-- rpt_forward because gp_inject_fault exists only with the debug extensions, +-- and the regression driver skips any file that calls it. +-- +-- The chain is fact <- dimension <- subdimension with a random dimension, so +-- both hops are Anser channels and the first one has its consumer inside the +-- reducer InitPlan. Every case must return the RPT-off answer, and the next +-- command on the same gangs must answer: a reducer's dispatcher releases its +-- gangs before ExecutorEnd. If one of those connections is destroyed, libpq's +-- PGEVT_CONNDESTROY callback must remove Anser's references before it is freed. +-- start_ignore +CREATE EXTENSION IF NOT EXISTS gp_inject_fault; +-- end_ignore +CREATE EXTENSION robust_predicate_transfer; +SET optimizer = off; +SET anser.runtime_filter = off; +SET anser.debug = on; +SET rpt.enable = on; +SET rpt.forward_min_root_rows = 0; +SET rpt.forward_max_exchange_ms = -1; +SET statement_timeout = '120s'; +CREATE TABLE rpt_fwf_fact (id int, d int, v int) DISTRIBUTED RANDOMLY; +CREATE TABLE rpt_fwf_dim (k int, s int) DISTRIBUTED RANDOMLY; +CREATE TABLE rpt_fwf_sub (k int, c int) DISTRIBUTED RANDOMLY; +INSERT INTO rpt_fwf_fact SELECT i, i % 1000, i FROM generate_series(1, 200000) i; +INSERT INTO rpt_fwf_dim SELECT i, i % 100 FROM generate_series(0, 999) i; +INSERT INTO rpt_fwf_sub SELECT i, i FROM generate_series(0, 99) i; +ANALYZE rpt_fwf_fact; +ANALYZE rpt_fwf_dim; +ANALYZE rpt_fwf_sub; +CREATE FUNCTION pg_temp.rpt_fwf_run(rpt boolean) RETURNS text LANGUAGE plpgsql AS $$ +DECLARE + r text; +BEGIN + PERFORM set_config('rpt.enable', CASE WHEN rpt THEN 'on' ELSE 'off' END, true); + SELECT count(*)::text || '/' || sum(f.v)::text INTO r + FROM rpt_fwf_fact f JOIN rpt_fwf_dim d ON f.d = d.k + JOIN rpt_fwf_sub u ON d.s = u.k WHERE u.c < 5; + PERFORM set_config('rpt.enable', 'on', true); + RETURN r; +END +$$; +-- First occurrence only; on the coordinator a 'sleep' fault sleeps 1 s. +CREATE FUNCTION pg_temp.rpt_fwf_fault(fault text, action text, coordinator boolean) +RETURNS void LANGUAGE plpgsql AS $$ +BEGIN + IF action = 'reset' THEN + PERFORM gp_inject_fault(fault, 'reset', dbid) + FROM gp_segment_configuration + WHERE role = 'p' AND (content = -1) = coordinator; + ELSE + PERFORM gp_inject_fault(fault, action, '', '', '', 1, 1, + CASE WHEN coordinator THEN 1 ELSE 0 END, dbid) + FROM gp_segment_configuration + WHERE role = 'p' AND (content = -1) = coordinator; + END IF; +END +$$; +DO $$ +DECLARE + expected text := pg_temp.rpt_fwf_run(false); + got text; +BEGIN + IF rpt_transfer_plan('SELECT count(*) FROM rpt_fwf_fact f JOIN rpt_fwf_dim d ON f.d = d.k + JOIN rpt_fwf_sub u ON d.s = u.k WHERE u.c < 5') + NOT LIKE '%channels=2 %decision=inject%' THEN + RAISE EXCEPTION 'fixture chain not selected'; + END IF; + + -- 1. The coordinator holds the first delivery for a second; every + -- consumer waiting on it, the reducer's included, gives up at 100 ms and + -- runs unfiltered. + PERFORM set_config('anser.timeout_ms', '100', true); + PERFORM pg_temp.rpt_fwf_fault('anser_before_deliver', 'sleep', true); + got := pg_temp.rpt_fwf_run(true); + PERFORM pg_temp.rpt_fwf_fault('anser_before_deliver', 'reset', true); + IF got IS DISTINCT FROM expected THEN + RAISE EXCEPTION 'held delivery: % <> %', got, expected; + END IF; + IF pg_temp.rpt_fwf_run(true) IS DISTINCT FROM expected THEN + RAISE EXCEPTION 'next command after held delivery'; + END IF; + + -- 2. The first consumer on each segment -- the reducer's -- walks away + -- after the first chunk of a 64 MB filter, leaving its delivery + -- unfinished on a connection of the InitPlan's gang, which is released + -- before the main plan runs. + PERFORM set_config('anser.timeout_ms', '500', true); + PERFORM set_config('anser.debug_delivery_chunk_bytes', '8192', true); + PERFORM set_config('anser.debug_min_filter_bytes', '67108864', true); + PERFORM pg_temp.rpt_fwf_fault('anser_after_chunk', 'skip', false); + got := pg_temp.rpt_fwf_run(true); + PERFORM pg_temp.rpt_fwf_fault('anser_after_chunk', 'reset', false); + IF got IS DISTINCT FROM expected THEN + RAISE EXCEPTION 'walk-away: % <> %', got, expected; + END IF; + FOR i IN 1..2 LOOP + IF pg_temp.rpt_fwf_run(true) IS DISTINCT FROM expected THEN + RAISE EXCEPTION 'next command % after walk-away', i; + END IF; + END LOOP; + PERFORM set_config('anser.debug_delivery_chunk_bytes', '0', true); + PERFORM set_config('anser.debug_min_filter_bytes', '0', true); +END +$$; +-- 3. The statement is cancelled while the reducer waits for a delivery, so +-- the reducer's dispatcher is torn down on the abort path. Whether the +-- cancel lands before the query finishes is timing, so its output is not +-- compared; the session and its gangs must survive with the right answers. +DO $$ +BEGIN + PERFORM pg_temp.rpt_fwf_fault('anser_before_deliver', 'sleep', true); +END +$$; +SET anser.timeout_ms = 5000; +SET statement_timeout = '300ms'; +-- start_ignore +SELECT pg_temp.rpt_fwf_run(true) IS NOT NULL AS finished; +-- end_ignore +SET statement_timeout = '120s'; +RESET anser.timeout_ms; +DO $$ +BEGIN + PERFORM pg_temp.rpt_fwf_fault('anser_before_deliver', 'reset', true); + IF pg_temp.rpt_fwf_run(true) IS DISTINCT FROM pg_temp.rpt_fwf_run(false) THEN + RAISE EXCEPTION 'next command after cancel'; + END IF; +END +$$; +SELECT count(*) AS dim_rows FROM rpt_fwf_dim; +DROP TABLE rpt_fwf_fact, rpt_fwf_dim, rpt_fwf_sub; +DROP EXTENSION robust_predicate_transfer; +RESET rpt.enable; +RESET rpt.forward_min_root_rows; +RESET rpt.forward_max_exchange_ms; +RESET anser.debug; +RESET anser.runtime_filter; +RESET optimizer; +RESET statement_timeout; diff --git a/gpcontrib/robust_predicate_transfer/include/rpt.h b/gpcontrib/robust_predicate_transfer/include/rpt.h index 45135eabda9..baf832953d8 100644 --- a/gpcontrib/robust_predicate_transfer/include/rpt.h +++ b/gpcontrib/robust_predicate_transfer/include/rpt.h @@ -262,6 +262,100 @@ struct RptTransferMgr std::vector backward_consumers; }; +/* + * Phase III forward pass. A channel is one forward transfer edge: the + * reducer scan of `from` builds a bloom filter on from_key, and every scan of + * `to` (in the reducer, and the root's scan in the main plan) applies it on + * to_key. Only the first clause of a multi-key edge is transferred; one key + * of a conjunctive equality still over-approximates, so this loses + * selectivity, never rows. + */ +struct RptForwardChannel +{ + int from = 0; + int to = 0; + RptKey from_key = {0, 0}; + RptKey to_key = {0, 0}; + bool local = false; /* policy-local (rptplan.cpp, rpt_local_hop) */ + bool in_process = false; /* local and consumed only inside the reducer: + * handed over in-process, no Anser channel */ + uint32 condition_id = 0; + std::string condition_key; + int64 total_elems = 0; + int64 max_payload = 0; + int64 planned_bytes = 0; + double est_cost_ms = 0; +}; + +/* One root's forward chain and the decision about it. */ +struct RptForwardComponent +{ + int root = 0; + std::vector chain; /* reducer scans, producers before consumers */ + std::vector channels; + double est_cost_ms = 0; /* sum of hop estimates */ + double est_selectivity = 1; /* estimated fraction of root rows kept */ + bool accepted = false; + std::string reason; /* "inject", or why not */ +}; + +struct RptForwardParams +{ + int width = 0; /* reducer gang size: producers per channel */ + double ms_per_mib_part = 0; /* c in c * n_producers * filter_MiB */ + double max_exchange_ms = 0; /* component budget; < 0 disables the limit */ + double min_root_rows = 0; /* root scan plan_rows below this: no reducer */ + double max_selectivity = 1; /* lambda; >= 1 disables the check */ + bool apply_all = false; /* chain scans of the main plan consume too */ +}; + +/* + * What the reducer scan of one vertex is expected to keep, from the plan and + * the catalog (rptforward.cpp). `rows` is per segment, before quals; it sizes + * the vertex's filters together with `selectivity`, the fraction its own + * reducer quals keep -- 1 when they were dropped (rule 13). Vertices with no + * entry use their plan_rows and selectivity 1. + */ +struct RptForwardVertexEst +{ + double rows = -1; + double selectivity = 1; + bool quals_dropped = false; +}; + +/* Distinct values of a column over the whole table, or < 0 when unknown. */ +typedef double (*RptNdvFn) (const RptVertexInfo &vertex, AttrNumber attno); + +/* Anser's bloom sizing (AnserRuntimeFilterSize), injectable for unit tests. */ +typedef bool (*RptFilterSizeFn) (double est_rows, int64 *total_elems, + int64 *max_payload, int64 *planned_bytes); + +/* rptforwardplan.cpp: pure selection over a forward schedule. */ +extern void RptSelectForward(const RptTransferMgr &forward, + const RptForwardParams ¶ms, + const std::map &est, + RptFilterSizeFn size_fn, RptNdvFn ndv_fn, + std::vector *out); +extern void RptDumpForward(const std::vector &components, + std::string *out); + +/* rptforward.cpp: the planner-hook pass. Returns a new statement, or stmt + * itself when nothing is injected; *trace receives the decision dump. May + * throw; the caller fails open. */ +extern PlannedStmt *RptInjectForward(PlannedStmt *stmt, bool materialize, + std::string *trace); + +/* Phase III GUCs (rptinit.cpp). */ +extern bool rpt_forward; +extern int rpt_forward_apply; /* RPT_FORWARD_APPLY_* */ +extern double rpt_forward_ms_per_mib_part; +extern double rpt_forward_max_exchange_ms; +extern double rpt_forward_min_root_rows; +extern double rpt_forward_max_selectivity; + +#define RPT_FORWARD_APPLY_ROOT 0 +#define RPT_FORWARD_APPLY_ALL 1 + /* rptregion.cpp: extract the join graph from the finished statement. */ extern void RptCollectGraph(PlannedStmt *stmt, RptTransferMgr *mgr); diff --git a/gpcontrib/robust_predicate_transfer/include/rptlocal.h b/gpcontrib/robust_predicate_transfer/include/rptlocal.h new file mode 100644 index 00000000000..9035f89b608 --- /dev/null +++ b/gpcontrib/robust_predicate_transfer/include/rptlocal.h @@ -0,0 +1,40 @@ +/*------------------------------------------------------------------------- + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * rptlocal.h + * In-process bloom handoff for local reducer hops (design rule 16). + * + * IDENTIFICATION + * gpcontrib/robust_predicate_transfer/include/rptlocal.h + * + *------------------------------------------------------------------------- + */ +#ifndef RPTLOCAL_H +#define RPTLOCAL_H + +#include "nodes/plannodes.h" + +extern void RptRegisterLocalMethods(void); +extern CustomScan *RptBuildLocalProducerScan(Plan *child, AttrNumber key_attno, + uint32 condition_id, int64 total_elems); +extern CustomScan *RptBuildLocalConsumerScan(Plan *child, AttrNumber key_attno, + uint32 condition_id, int64 total_elems); +extern bool RptIsLocalScan(const Plan *plan); + +#endif /* RPTLOCAL_H */ diff --git a/gpcontrib/robust_predicate_transfer/include/rptqual.h b/gpcontrib/robust_predicate_transfer/include/rptqual.h new file mode 100644 index 00000000000..9a3e3733a7f --- /dev/null +++ b/gpcontrib/robust_predicate_transfer/include/rptqual.h @@ -0,0 +1,130 @@ +/*------------------------------------------------------------------------- + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * rptqual.h + * Trusted built-in functions for eager reducer quals. + * + * IDENTIFICATION + * gpcontrib/robust_predicate_transfer/include/rptqual.h + * + *------------------------------------------------------------------------- + */ +#ifndef RPTQUAL_H +#define RPTQUAL_H + +#include "utils/fmgroids.h" + +/* OIDs of built-in comparisons with no argument-dependent or unconditional + * evaluation errors. LEAKPROOF alone does not promise this for user functions. + * Keep arithmetic, coercions and arbitrary extension functions out. */ +static inline bool +RptQualFunctionSafe(Oid function) +{ + switch (function) + { + case F_BOOLEQ: + case F_BOOLNE: + case F_BOOLLT: + case F_BOOLLE: + case F_BOOLGT: + case F_BOOLGE: + case F_INT2EQ: + case F_INT2NE: + case F_INT2LT: + case F_INT2LE: + case F_INT2GT: + case F_INT2GE: + case F_INT4EQ: + case F_INT4NE: + case F_INT4LT: + case F_INT4LE: + case F_INT4GT: + case F_INT4GE: + case F_INT8EQ: + case F_INT8NE: + case F_INT8LT: + case F_INT8LE: + case F_INT8GT: + case F_INT8GE: + case F_INT24EQ: + case F_INT24NE: + case F_INT24LT: + case F_INT24LE: + case F_INT24GT: + case F_INT24GE: + case F_INT42EQ: + case F_INT42NE: + case F_INT42LT: + case F_INT42LE: + case F_INT42GT: + case F_INT42GE: + case F_INT28EQ: + case F_INT28NE: + case F_INT28LT: + case F_INT28LE: + case F_INT28GT: + case F_INT28GE: + case F_INT82EQ: + case F_INT82NE: + case F_INT82LT: + case F_INT82LE: + case F_INT82GT: + case F_INT82GE: + case F_INT48EQ: + case F_INT48NE: + case F_INT48LT: + case F_INT48LE: + case F_INT48GT: + case F_INT48GE: + case F_INT84EQ: + case F_INT84NE: + case F_INT84LT: + case F_INT84LE: + case F_INT84GT: + case F_INT84GE: + case F_OIDEQ: + case F_OIDNE: + case F_OIDLT: + case F_OIDLE: + case F_OIDGT: + case F_OIDGE: + return true; + default: + return false; + } +} + +/* ScalarArrayOpExpr can call these in addition to its comparison function. */ +static inline bool +RptQualHashFunctionSafe(Oid function) +{ + switch (function) + { + case F_HASHINT2: + case F_HASHINT4: + case F_HASHINT8: + case F_HASHOID: + case F_HASHCHAR: + return true; + default: + return false; + } +} + +#endif /* RPTQUAL_H */ diff --git a/gpcontrib/robust_predicate_transfer/scripts/measure_initplan.py b/gpcontrib/robust_predicate_transfer/scripts/measure_initplan.py new file mode 100644 index 00000000000..c9c44cf0dfc --- /dev/null +++ b/gpcontrib/robust_predicate_transfer/scripts/measure_initplan.py @@ -0,0 +1,58 @@ +#!/usr/bin/env python3 +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +"""Emit a psql workload comparing the exact empty reducer InitPlan off/on.""" +import argparse + +parser = argparse.ArgumentParser(description=__doc__) +parser.add_argument("--samples", type=int, default=100) +parser.add_argument("--warmups", type=int, default=10) +args = parser.parse_args() +if args.samples < 1 or args.warmups < 0: + parser.error("samples must be positive and warmups nonnegative") + +print(r"\set ON_ERROR_STOP on") +print("SET anser.runtime_filter = off;") +print("SET anser.debug = off;") +print("SET rpt.enable = off;") +print("SET rpt.debug = off;") +print("SET rpt.debug_noop_initplan = off;") +print("SET statement_timeout = '120s';") +print("CREATE TEMP TABLE rpt_initplan_bench (k int) DISTRIBUTED RANDOMLY;") +print("INSERT INTO rpt_initplan_bench SELECT i FROM generate_series(1, 3000) g(i);") +print("ANALYZE rpt_initplan_bench;") +query = "SELECT count(*) FROM rpt_initplan_bench" +for optimizer in ("off", "on"): + print(f"SET optimizer = {optimizer};") + print("SET rpt.debug_noop_initplan = on;") + print(f"EXPLAIN (COSTS OFF) {query};") + # Also exercise a pre-existing InitPlan and its PARAM_EXEC numbering. + print("SELECT count(*) FROM rpt_initplan_bench " + "WHERE k <= (SELECT max(k) FROM rpt_initplan_bench);") + print(r"\timing on") + for phase, count in (("warmup", args.warmups), ("measured", args.samples)): + for index in range(1, count + 1): + # Alternate which arm goes first to reduce systematic drift. + arms = ("off", "on") if index % 2 else ("on", "off") + for arm in arms: + print(f"SET rpt.debug_noop_initplan = {arm};") + print(f"\\echo SAMPLE initplan {optimizer} {phase} {index} {arm}") + print(f"EXPLAIN (ANALYZE, TIMING OFF, SUMMARY ON) {query};") + print(r"\timing off") +print("SET rpt.debug_noop_initplan = off;") +print("DROP TABLE rpt_initplan_bench;") diff --git a/gpcontrib/robust_predicate_transfer/sql/rpt_backward_eligibility.sql b/gpcontrib/robust_predicate_transfer/sql/rpt_backward_eligibility.sql index bde24939160..db9aa1e785d 100644 --- a/gpcontrib/robust_predicate_transfer/sql/rpt_backward_eligibility.sql +++ b/gpcontrib/robust_predicate_transfer/sql/rpt_backward_eligibility.sql @@ -100,6 +100,8 @@ DECLARE q text; dump text; path text[]; + producer_path text[]; + split_at integer; producer_id text; correlated text; BEGIN @@ -150,7 +152,25 @@ BEGIN IF EXISTS (SELECT FROM unnest(path) tag WHERE tag LIKE '%:Left:Inner' OR tag LIKE '%:Right:Outer') THEN PERFORM pg_temp.rpt_expect(q, 'rpt_elig_root', 'rpt_elig_member', ARRAY['nullable side of outer join'], false); ELSE - PERFORM pg_temp.rpt_expect(q, 'rpt_elig_root', 'rpt_elig_member', ARRAY['eligible'], true); + -- Simplification does not fix hash build/probe order. Compare + -- the first differing path step: the shared parent is the join + -- separating these named scans, regardless of range-table IDs. + producer_path := pg_temp.rpt_scan_path(q, 'rpt_elig_root'); + IF producer_path IS NULL THEN + RAISE EXCEPTION 'nullable-side fixture lost its root scan'; + END IF; + SELECT min(n) INTO split_at FROM generate_subscripts(path, 1) n + WHERE path[n] IS DISTINCT FROM producer_path[n]; + IF split_at IS NULL THEN + RAISE EXCEPTION 'nullable-side fixture has no separating join'; + END IF; + IF producer_path[split_at] LIKE 'Hash Join:%:Outer' + AND path[split_at] LIKE 'Hash Join:%:Inner' THEN + PERFORM pg_temp.rpt_expect(q, 'rpt_elig_root', 'rpt_elig_member', + ARRAY['producer is not on a qualifying hash build side'], false); + ELSE + PERFORM pg_temp.rpt_expect(q, 'rpt_elig_root', 'rpt_elig_member', ARRAY['eligible'], true); + END IF; END IF; -- The aggregate scalar subquery commonly becomes an InitPlan in the -- PostgreSQL planner. ORCA may instead use a join, without extParam. diff --git a/gpcontrib/robust_predicate_transfer/sql/rpt_forward.sql b/gpcontrib/robust_predicate_transfer/sql/rpt_forward.sql new file mode 100644 index 00000000000..18516893a34 --- /dev/null +++ b/gpcontrib/robust_predicate_transfer/sql/rpt_forward.sql @@ -0,0 +1,343 @@ +--------------------------------------------------------------------------- +-- +-- Licensed to the Apache Software Foundation (ASF) under one +-- or more contributor license agreements. See the NOTICE file +-- distributed with this work for additional information +-- regarding copyright ownership. The ASF licenses this file +-- to you under the Apache License, Version 2.0 (the +-- "License"); you may not use this file except in compliance +-- with the License. You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, +-- software distributed under the License is distributed on an +-- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +-- KIND, either express or implied. See the License for the +-- specific language governing permissions and limitations +-- under the License. +-- +-- Phase III forward reducer, end to end: the InitPlan is built, its filters +-- reach the main plan across the dispatch boundary, intermediate hops work +-- both through Anser and in-process, and results never change. Both +-- optimizers; every check raises with the plan or dump that failed it. +-- Faults live in rpt_forward_faults. +CREATE EXTENSION robust_predicate_transfer; +SET anser.runtime_filter = off; +SET statement_timeout = '120s'; +-- Star: two dimensions on two different fact keys (two hops into the root). +CREATE TABLE rpt_fwd_fact (id int, d1 int, d2 int, v int) DISTRIBUTED RANDOMLY; +CREATE TABLE rpt_fwd_d1 (k int, a int) DISTRIBUTED RANDOMLY; +CREATE TABLE rpt_fwd_d2 (k int, b int) DISTRIBUTED RANDOMLY; +CREATE TABLE rpt_fwd_empty (k int, a int) DISTRIBUTED RANDOMLY; +-- Snowflake: fact -> dimension -> subdimension, a chain of two hops whose +-- first hop is consumed inside the reducer. rpt_fwd_dl and rpt_fwd_sub are +-- distributed on their join columns, so that hop is local (in-process); +-- rpt_fwd_dr is the same dimension distributed randomly, so the hop is global. +CREATE TABLE rpt_fwd_dl (k int, s int) DISTRIBUTED BY (s); +CREATE TABLE rpt_fwd_dr (k int, s int) DISTRIBUTED RANDOMLY; +CREATE TABLE rpt_fwd_sub (k int, c int) DISTRIBUTED BY (k); +INSERT INTO rpt_fwd_fact + SELECT i, CASE WHEN i % 97 = 0 THEN NULL ELSE i % 1000 END, i % 500, i + FROM generate_series(1, 200000) i; +INSERT INTO rpt_fwd_d1 SELECT i, i FROM generate_series(0, 999) i; +INSERT INTO rpt_fwd_d2 SELECT i, i FROM generate_series(0, 499) i; +INSERT INTO rpt_fwd_dl SELECT i, i % 100 FROM generate_series(0, 999) i; +INSERT INTO rpt_fwd_dr SELECT i, i % 100 FROM generate_series(0, 999) i; +INSERT INTO rpt_fwd_sub SELECT i, i FROM generate_series(0, 99) i; +ANALYZE rpt_fwd_fact; +ANALYZE rpt_fwd_d1; +ANALYZE rpt_fwd_d2; +ANALYZE rpt_fwd_empty; +ANALYZE rpt_fwd_dl; +ANALYZE rpt_fwd_dr; +ANALYZE rpt_fwd_sub; +-- Legal LEAKPROOF function: the error reveals nothing about its argument. +-- It is still unsafe for an eager reducer when the main scan never executes. +CREATE FUNCTION pg_temp.rpt_fwd_constant_error(integer) RETURNS boolean +LANGUAGE plpgsql IMMUTABLE LEAKPROOF AS $$ +BEGIN + RAISE EXCEPTION 'constant failure'; +END +$$; +CREATE FUNCTION pg_temp.rpt_fwd_plan(q text, analyze boolean) RETURNS jsonb +LANGUAGE plpgsql AS $$ +DECLARE + plan jsonb; +BEGIN + EXECUTE format('EXPLAIN (%s FORMAT JSON) %s', + CASE WHEN analyze THEN 'ANALYZE,' ELSE '' END, q) INTO plan; + RETURN plan; +END +$$; +-- Result of q as one comparable text value, with RPT on or off. +CREATE FUNCTION pg_temp.rpt_fwd_result(q text, rpt boolean) RETURNS text +LANGUAGE plpgsql AS $$ +DECLARE + r text; +BEGIN + PERFORM set_config('rpt.enable', CASE WHEN rpt THEN 'on' ELSE 'off' END, true); + EXECUTE format('SELECT string_agg(t::text, %L ORDER BY t::text) FROM (%s) t', ';', q) INTO r; + PERFORM set_config('rpt.enable', 'on', true); + RETURN coalesce(r, ''); +END +$$; +-- Nodes of a provider inside InitPlans (reducer) or outside them (main plan); +-- a NULL provider matches every node. +CREATE FUNCTION pg_temp.rpt_fwd_nodes(plan jsonb, provider text, in_reducer boolean) +RETURNS SETOF jsonb LANGUAGE sql AS $$ + WITH RECURSIVE walk(node, in_init) AS ( + SELECT plan->0->'Plan', false + UNION ALL + SELECT child, in_init OR coalesce(child->>'Parent Relationship', '') = 'InitPlan' + FROM walk CROSS JOIN LATERAL jsonb_array_elements(coalesce(node->'Plans', '[]')) child + ) + SELECT node FROM walk + WHERE in_init = in_reducer AND (provider IS NULL OR node->>'Custom Plan Provider' = provider) +$$; +CREATE FUNCTION pg_temp.rpt_fwd_count(plan jsonb, provider text, in_reducer boolean) +RETURNS bigint LANGUAGE sql AS $$ + SELECT count(*) FROM pg_temp.rpt_fwd_nodes(plan, provider, in_reducer) +$$; +-- Rows removed by forward filters in the main plan: by a consumer itself, or +-- by the Seq Scan it pushed the filter into. +CREATE FUNCTION pg_temp.rpt_fwd_removed(plan jsonb) RETURNS float8 +LANGUAGE sql AS $$ + SELECT coalesce(sum(coalesce((node->>'Rows Removed by Bloom Filter')::float8, 0) + + coalesce((node->>'Rows Removed by Pushdown Runtime Filter')::float8, 0)), 0) + FROM pg_temp.rpt_fwd_nodes(plan, NULL, false) node +$$; +DO $$ +DECLARE + opt text; + q text; + mode text; + dump text; + plan jsonb; + prep_n bigint; + prep_s bigint; + star constant text := + 'SELECT count(*) AS n, sum(f.v) AS s FROM rpt_fwd_fact f + JOIN rpt_fwd_d1 d1 ON f.d1 = d1.k JOIN rpt_fwd_d2 d2 ON f.d2 = d2.k + WHERE d1.a < 50 AND d2.b < 100'; + flake_local constant text := + 'SELECT count(*) AS n, sum(f.v) AS s FROM rpt_fwd_fact f + JOIN rpt_fwd_dl d ON f.d1 = d.k JOIN rpt_fwd_sub u ON d.s = u.k + WHERE u.c < 5'; + flake_global constant text := + 'SELECT count(*) AS n, sum(f.v) AS s FROM rpt_fwd_fact f + JOIN rpt_fwd_dr d ON f.d1 = d.k JOIN rpt_fwd_sub u ON d.s = u.k + WHERE u.c < 5'; +BEGIN + PERFORM set_config('rpt.enable', 'on', true); + PERFORM set_config('rpt.forward_min_root_rows', '0', true); + PERFORM set_config('rpt.forward_max_exchange_ms', '-1', true); + FOREACH opt IN ARRAY ARRAY['off', 'on'] LOOP + PERFORM set_config('optimizer', opt, true); + PERFORM set_config('rpt.forward_apply', 'root', true); + + -- Shape: a reducer InitPlan with producers, a consumer in the main plan. + dump := rpt_transfer_plan(star); + IF dump NOT LIKE '%decision=inject%' THEN + RAISE EXCEPTION 'star not selected (optimizer=%): %', opt, dump; + END IF; + plan := pg_temp.rpt_fwd_plan(star, false); + IF pg_temp.rpt_fwd_count(plan, 'Anser Bloom Producer', true) = 0 OR + pg_temp.rpt_fwd_count(plan, 'Anser Bloom Consumer', false) = 0 THEN + RAISE EXCEPTION 'reducer shape missing (optimizer=%): %', opt, plan; + END IF; + + -- The property the design rests on: filters produced in the InitPlan's + -- dispatch are applied by the main plan's dispatch. + plan := pg_temp.rpt_fwd_plan(star, true); + IF pg_temp.rpt_fwd_removed(plan) <= 0 THEN + RAISE EXCEPTION 'no rows removed by a forward filter in the main plan (optimizer=%): %', opt, plan; + END IF; + + -- Multi-hop, global: the intermediate hop is an Anser consumer + -- inside the reducer, fed by a producer of the same dispatch. + dump := rpt_transfer_plan(flake_global); + IF dump NOT LIKE '%decision=inject%' OR dump NOT LIKE '%channels=2 %' OR + dump LIKE '%in-process%' THEN + RAISE EXCEPTION 'global chain not selected as two Anser hops (optimizer=%): %', opt, dump; + END IF; + plan := pg_temp.rpt_fwd_plan(flake_global, true); + IF pg_temp.rpt_fwd_count(plan, 'Anser Bloom Consumer', true) = 0 OR + pg_temp.rpt_fwd_count(plan, 'Anser Bloom Producer', true) < 2 OR + pg_temp.rpt_fwd_removed(plan) <= 0 THEN + RAISE EXCEPTION 'global chain shape or effect missing (optimizer=%): %', opt, plan; + END IF; + + -- Multi-hop, local: the intermediate hop never leaves the QE. + dump := rpt_transfer_plan(flake_local); + IF dump NOT LIKE '%decision=inject%' OR dump NOT LIKE '%local in-process%' THEN + RAISE EXCEPTION 'local hop not in-process (optimizer=%): %', opt, dump; + END IF; + plan := pg_temp.rpt_fwd_plan(flake_local, true); + IF pg_temp.rpt_fwd_count(plan, 'RPT Local Bloom Producer', true) <> 1 OR + pg_temp.rpt_fwd_count(plan, 'RPT Local Bloom Consumer', true) <> 1 OR + pg_temp.rpt_fwd_count(plan, 'Anser Bloom Consumer', true) <> 0 OR + (SELECT sum((n->>'Rows Removed by Local Bloom Filter')::float8) + FROM pg_temp.rpt_fwd_nodes(plan, 'RPT Local Bloom Consumer', true) n) <= 0 OR + pg_temp.rpt_fwd_removed(plan) <= 0 THEN + RAISE EXCEPTION 'local chain shape or effect missing (optimizer=%): %', opt, plan; + END IF; + + -- forward_apply=all: the chain's main-plan scans consume too, so the + -- local hop is published through Anser instead. + PERFORM set_config('rpt.forward_apply', 'all', true); + dump := rpt_transfer_plan(flake_local); + plan := pg_temp.rpt_fwd_plan(flake_local, false); + IF dump LIKE '%in-process%' OR + pg_temp.rpt_fwd_count(plan, 'Anser Bloom Consumer', false) < 2 OR + pg_temp.rpt_fwd_count(plan, 'RPT Local Bloom Consumer', true) <> 0 THEN + RAISE EXCEPTION 'forward_apply=all shape wrong (optimizer=%): % %', opt, dump, plan; + END IF; + + -- Results never change, under both apply modes: star, chains, empty + -- filters, NULL keys, a volatile qual (dropped whole in the reducer), + -- an empty dimension, a preserved side. + FOREACH q IN ARRAY ARRAY[ + star, + flake_local, + flake_global, + 'SELECT f.id, f.v FROM rpt_fwd_fact f JOIN rpt_fwd_d1 d1 ON f.d1 = d1.k WHERE d1.a < 3', + 'SELECT count(*) FROM rpt_fwd_fact f JOIN rpt_fwd_d1 d1 ON f.d1 = d1.k WHERE d1.a < 0', + 'SELECT count(*) FROM rpt_fwd_fact f JOIN rpt_fwd_d1 d1 ON f.d1 = d1.k WHERE d1.a IN (0,1,2,3,4,5,6,7,8,9)', + 'SELECT count(*) FROM rpt_fwd_fact f JOIN rpt_fwd_d1 d1 ON f.d1 = d1.k WHERE d1.a NOT IN (0,1,2,3,4,5,6,7,8,9)', + 'SELECT count(*) FROM rpt_fwd_fact f JOIN rpt_fwd_d1 d1 ON f.d1 = d1.k WHERE d1.a < 50 AND random() >= 0', + 'SELECT count(*) FROM rpt_fwd_fact f JOIN rpt_fwd_empty e ON f.d1 = e.k WHERE e.a < 50', + 'SELECT count(*) FROM rpt_fwd_fact f LEFT JOIN rpt_fwd_d1 d1 ON f.d1 = d1.k AND d1.a < 50', + 'SELECT d.k, count(*) FROM rpt_fwd_fact f JOIN rpt_fwd_dl d ON f.d1 = d.k + JOIN rpt_fwd_sub u ON d.s = u.k WHERE u.c IN (1, 7) GROUP BY d.k' + ] LOOP + FOREACH mode IN ARRAY ARRAY['root', 'all'] LOOP + PERFORM set_config('rpt.forward_apply', mode, true); + IF pg_temp.rpt_fwd_result(q, true) IS DISTINCT FROM pg_temp.rpt_fwd_result(q, false) THEN + RAISE EXCEPTION 'results differ with RPT (optimizer=%, apply=%): %', opt, mode, q; + END IF; + END LOOP; + END LOOP; + PERFORM set_config('rpt.forward_apply', 'root', true); + + -- A prepared statement keeps its reducer and its answer. + EXECUTE 'PREPARE rpt_fwd_prep AS ' || star; + FOR i IN 1..2 LOOP + EXECUTE 'EXECUTE rpt_fwd_prep' INTO prep_n, prep_s; + IF row(prep_n, prep_s)::text IS DISTINCT FROM pg_temp.rpt_fwd_result(star, false) THEN + RAISE EXCEPTION 'prepared statement result differs (optimizer=%, run %)', opt, i; + END IF; + END LOOP; + EXECUTE 'DEALLOCATE rpt_fwd_prep'; + + -- Gates: below the root-row threshold, nothing is injected. + PERFORM set_config('rpt.forward_min_root_rows', '1e12', true); + IF rpt_transfer_plan(star) NOT LIKE '%decision=root rows below rpt.forward_min_root_rows%' OR + pg_temp.rpt_fwd_count(pg_temp.rpt_fwd_plan(star, false), NULL, true) <> 0 THEN + RAISE EXCEPTION 'root-row gate did not refuse (optimizer=%)', opt; + END IF; + PERFORM set_config('rpt.forward_min_root_rows', '0', true); + + -- Gates: an exchange budget below the estimate refuses the chain. + PERFORM set_config('rpt.forward_max_exchange_ms', '0.001', true); + IF rpt_transfer_plan(star) NOT LIKE '%decision=estimated exchange cost above%' THEN + RAISE EXCEPTION 'cost gate did not refuse (optimizer=%)', opt; + END IF; + PERFORM set_config('rpt.forward_max_exchange_ms', '-1', true); + + -- Gates: a dimension keeping 90% of its keys is not worth a reducer. + q := 'SELECT count(*) FROM rpt_fwd_fact f JOIN rpt_fwd_d1 d1 ON f.d1 = d1.k WHERE d1.a < 900'; + IF rpt_transfer_plan(q) NOT LIKE '%decision=estimated root selectivity not below%' OR + pg_temp.rpt_fwd_count(pg_temp.rpt_fwd_plan(q, false), NULL, true) <> 0 THEN + RAISE EXCEPTION 'selectivity gate did not refuse (optimizer=%)', opt; + END IF; + + -- Rule 13: a qual that can fail is never run by the reducer. The + -- reducer runs before the main plan and over rows the main plan may + -- never read; with LIMIT 0 the main plan reads none, so a reducer + -- evaluating 100 / a would raise division by zero (a = 0 exists), and + -- so would an I/O cast it cannot prove safe. With lambda the + -- qual-less chain is refused; with lambda off it is built with no + -- quals. Either way: no error, the same answer. + q := 'SELECT f.id FROM rpt_fwd_fact f JOIN rpt_fwd_d1 d1 ON f.d1 = d1.k WHERE 100 / d1.a > 1'; + dump := rpt_transfer_plan(q); + IF dump NOT LIKE '%dropped in the reducer (not provably error-free)%' OR + dump NOT LIKE '%decision=estimated root selectivity not below%' THEN + RAISE EXCEPTION 'unsafe qual not dropped, or chain not refused (optimizer=%): %', opt, dump; + END IF; + q := 'SELECT f.id FROM rpt_fwd_fact f JOIN rpt_fwd_d1 d1 ON f.d1 = d1.k WHERE pg_temp.rpt_fwd_constant_error(d1.a)'; + IF rpt_transfer_plan(q) NOT LIKE '%dropped in the reducer (not provably error-free)%' THEN + RAISE EXCEPTION 'leakproof user function accepted by reducer (optimizer=%)', opt; + END IF; + FOREACH mode IN ARRAY ARRAY['0.35', '1'] LOOP + PERFORM set_config('rpt.forward_max_selectivity', mode, true); + -- The planner keeps the LIMIT 0 join, so with lambda off the + -- reducer really runs (GPORCA may fold the query away instead). + q := 'SELECT f.id FROM rpt_fwd_fact f JOIN rpt_fwd_d1 d1 ON f.d1 = d1.k WHERE 100 / d1.a > 1 LIMIT 0'; + IF mode = '1' AND opt = 'off' AND rpt_transfer_plan(q) NOT LIKE '%decision=inject%' THEN + RAISE EXCEPTION 'LIMIT 0 reducer not built with lambda off: %', rpt_transfer_plan(q); + END IF; + FOREACH q IN ARRAY ARRAY[ + 'SELECT f.id FROM rpt_fwd_fact f JOIN rpt_fwd_d1 d1 ON f.d1 = d1.k WHERE 100 / d1.a > 1 LIMIT 0', + 'SELECT f.id FROM rpt_fwd_fact f JOIN rpt_fwd_d1 d1 ON f.d1 = d1.k WHERE d1.a < 50 AND 100 / d1.a > 1 LIMIT 0', + 'SELECT f.id FROM rpt_fwd_fact f JOIN rpt_fwd_d1 d1 ON f.d1 = d1.k WHERE (d1.a::text)::int < 50 LIMIT 0', + 'SELECT f.id FROM rpt_fwd_fact f JOIN rpt_fwd_d1 d1 ON f.d1 = d1.k WHERE pg_temp.rpt_fwd_constant_error(d1.a) LIMIT 0' + ] LOOP + IF pg_temp.rpt_fwd_result(q, true) <> '' THEN + RAISE EXCEPTION 'LIMIT 0 returned rows (optimizer=%): %', opt, q; + END IF; + END LOOP; + END LOOP; + PERFORM set_config('rpt.forward_max_selectivity', '0.35', true); + -- ... while provably safe quals are kept. + IF rpt_transfer_plan(star) LIKE '%dropped in the reducer%' THEN + RAISE EXCEPTION 'safe quals dropped (optimizer=%)', opt; + END IF; + + -- Exclusivity: with Anser's own runtime filter on, RPT+ stands down. + PERFORM set_config('anser.runtime_filter', 'on', true); + IF rpt_transfer_plan(star) NOT LIKE '%forward: needs anser.enable=on and anser.runtime_filter=off%' OR + pg_temp.rpt_fwd_count(pg_temp.rpt_fwd_plan(star, false), NULL, true) <> 0 THEN + RAISE EXCEPTION 'RPT+ did not stand down for anser.runtime_filter (optimizer=%)', opt; + END IF; + PERFORM set_config('anser.runtime_filter', 'off', true); + END LOOP; +END +$$; +-- PL/pgSQL (SPI) runs the reducer like any other caller. +CREATE FUNCTION pg_temp.rpt_fwd_spi() RETURNS bigint LANGUAGE plpgsql AS $$ +DECLARE + total bigint := 0; + r record; +BEGIN + FOR r IN SELECT f.v FROM rpt_fwd_fact f JOIN rpt_fwd_d1 d1 ON f.d1 = d1.k WHERE d1.a < 50 LOOP + total := total + r.v; + END LOOP; + FOR r IN SELECT f.v FROM rpt_fwd_fact f JOIN rpt_fwd_dl d ON f.d1 = d.k + JOIN rpt_fwd_sub u ON d.s = u.k WHERE u.c < 5 LOOP + total := total + r.v; + END LOOP; + RETURN total; +END +$$; +DO $$ +DECLARE + with_rpt bigint; + without bigint; +BEGIN + PERFORM set_config('rpt.forward_min_root_rows', '0', true); + PERFORM set_config('rpt.forward_max_exchange_ms', '-1', true); + PERFORM set_config('rpt.enable', 'on', true); + with_rpt := pg_temp.rpt_fwd_spi(); + PERFORM set_config('rpt.enable', 'off', true); + without := pg_temp.rpt_fwd_spi(); + IF with_rpt IS DISTINCT FROM without THEN + RAISE EXCEPTION 'SPI result differs: % vs %', with_rpt, without; + END IF; +END +$$; +DROP TABLE rpt_fwd_fact, rpt_fwd_d1, rpt_fwd_d2, rpt_fwd_empty, + rpt_fwd_dl, rpt_fwd_dr, rpt_fwd_sub; +DROP EXTENSION robust_predicate_transfer; +RESET anser.runtime_filter; +RESET statement_timeout; diff --git a/gpcontrib/robust_predicate_transfer/sql/rpt_forward_faults.sql b/gpcontrib/robust_predicate_transfer/sql/rpt_forward_faults.sql new file mode 100644 index 00000000000..204de98bc23 --- /dev/null +++ b/gpcontrib/robust_predicate_transfer/sql/rpt_forward_faults.sql @@ -0,0 +1,158 @@ +--------------------------------------------------------------------------- +-- +-- Licensed to the Apache Software Foundation (ASF) under one +-- or more contributor license agreements. See the NOTICE file +-- distributed with this work for additional information +-- regarding copyright ownership. The ASF licenses this file +-- to you under the Apache License, Version 2.0 (the +-- "License"); you may not use this file except in compliance +-- with the License. You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, +-- software distributed under the License is distributed on an +-- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +-- KIND, either express or implied. See the License for the +-- specific language governing permissions and limitations +-- under the License. +-- +-- Phase III forward reducer under forced transport failures. Kept apart from +-- rpt_forward because gp_inject_fault exists only with the debug extensions, +-- and the regression driver skips any file that calls it. +-- +-- The chain is fact <- dimension <- subdimension with a random dimension, so +-- both hops are Anser channels and the first one has its consumer inside the +-- reducer InitPlan. Every case must return the RPT-off answer, and the next +-- command on the same gangs must answer: a reducer's dispatcher releases its +-- gangs before ExecutorEnd. If one of those connections is destroyed, libpq's +-- PGEVT_CONNDESTROY callback must remove Anser's references before it is freed. +-- start_ignore +CREATE EXTENSION IF NOT EXISTS gp_inject_fault; +-- end_ignore +CREATE EXTENSION robust_predicate_transfer; +SET optimizer = off; +SET anser.runtime_filter = off; +SET anser.debug = on; +SET rpt.enable = on; +SET rpt.forward_min_root_rows = 0; +SET rpt.forward_max_exchange_ms = -1; +SET statement_timeout = '120s'; +CREATE TABLE rpt_fwf_fact (id int, d int, v int) DISTRIBUTED RANDOMLY; +CREATE TABLE rpt_fwf_dim (k int, s int) DISTRIBUTED RANDOMLY; +CREATE TABLE rpt_fwf_sub (k int, c int) DISTRIBUTED RANDOMLY; +INSERT INTO rpt_fwf_fact SELECT i, i % 1000, i FROM generate_series(1, 200000) i; +INSERT INTO rpt_fwf_dim SELECT i, i % 100 FROM generate_series(0, 999) i; +INSERT INTO rpt_fwf_sub SELECT i, i FROM generate_series(0, 99) i; +ANALYZE rpt_fwf_fact; +ANALYZE rpt_fwf_dim; +ANALYZE rpt_fwf_sub; +CREATE FUNCTION pg_temp.rpt_fwf_run(rpt boolean) RETURNS text LANGUAGE plpgsql AS $$ +DECLARE + r text; +BEGIN + PERFORM set_config('rpt.enable', CASE WHEN rpt THEN 'on' ELSE 'off' END, true); + SELECT count(*)::text || '/' || sum(f.v)::text INTO r + FROM rpt_fwf_fact f JOIN rpt_fwf_dim d ON f.d = d.k + JOIN rpt_fwf_sub u ON d.s = u.k WHERE u.c < 5; + PERFORM set_config('rpt.enable', 'on', true); + RETURN r; +END +$$; +-- First occurrence only; on the coordinator a 'sleep' fault sleeps 1 s. +CREATE FUNCTION pg_temp.rpt_fwf_fault(fault text, action text, coordinator boolean) +RETURNS void LANGUAGE plpgsql AS $$ +BEGIN + IF action = 'reset' THEN + PERFORM gp_inject_fault(fault, 'reset', dbid) + FROM gp_segment_configuration + WHERE role = 'p' AND (content = -1) = coordinator; + ELSE + PERFORM gp_inject_fault(fault, action, '', '', '', 1, 1, + CASE WHEN coordinator THEN 1 ELSE 0 END, dbid) + FROM gp_segment_configuration + WHERE role = 'p' AND (content = -1) = coordinator; + END IF; +END +$$; +DO $$ +DECLARE + expected text := pg_temp.rpt_fwf_run(false); + got text; +BEGIN + IF rpt_transfer_plan('SELECT count(*) FROM rpt_fwf_fact f JOIN rpt_fwf_dim d ON f.d = d.k + JOIN rpt_fwf_sub u ON d.s = u.k WHERE u.c < 5') + NOT LIKE '%channels=2 %decision=inject%' THEN + RAISE EXCEPTION 'fixture chain not selected'; + END IF; + + -- 1. The coordinator holds the first delivery for a second; every + -- consumer waiting on it, the reducer's included, gives up at 100 ms and + -- runs unfiltered. + PERFORM set_config('anser.timeout_ms', '100', true); + PERFORM pg_temp.rpt_fwf_fault('anser_before_deliver', 'sleep', true); + got := pg_temp.rpt_fwf_run(true); + PERFORM pg_temp.rpt_fwf_fault('anser_before_deliver', 'reset', true); + IF got IS DISTINCT FROM expected THEN + RAISE EXCEPTION 'held delivery: % <> %', got, expected; + END IF; + IF pg_temp.rpt_fwf_run(true) IS DISTINCT FROM expected THEN + RAISE EXCEPTION 'next command after held delivery'; + END IF; + + -- 2. The first consumer on each segment -- the reducer's -- walks away + -- after the first chunk of a 64 MB filter, leaving its delivery + -- unfinished on a connection of the InitPlan's gang, which is released + -- before the main plan runs. + PERFORM set_config('anser.timeout_ms', '500', true); + PERFORM set_config('anser.debug_delivery_chunk_bytes', '8192', true); + PERFORM set_config('anser.debug_min_filter_bytes', '67108864', true); + PERFORM pg_temp.rpt_fwf_fault('anser_after_chunk', 'skip', false); + got := pg_temp.rpt_fwf_run(true); + PERFORM pg_temp.rpt_fwf_fault('anser_after_chunk', 'reset', false); + IF got IS DISTINCT FROM expected THEN + RAISE EXCEPTION 'walk-away: % <> %', got, expected; + END IF; + FOR i IN 1..2 LOOP + IF pg_temp.rpt_fwf_run(true) IS DISTINCT FROM expected THEN + RAISE EXCEPTION 'next command % after walk-away', i; + END IF; + END LOOP; + PERFORM set_config('anser.debug_delivery_chunk_bytes', '0', true); + PERFORM set_config('anser.debug_min_filter_bytes', '0', true); +END +$$; +-- 3. The statement is cancelled while the reducer waits for a delivery, so +-- the reducer's dispatcher is torn down on the abort path. Whether the +-- cancel lands before the query finishes is timing, so its output is not +-- compared; the session and its gangs must survive with the right answers. +DO $$ +BEGIN + PERFORM pg_temp.rpt_fwf_fault('anser_before_deliver', 'sleep', true); +END +$$; +SET anser.timeout_ms = 5000; +SET statement_timeout = '300ms'; +-- start_ignore +SELECT pg_temp.rpt_fwf_run(true) IS NOT NULL AS finished; +-- end_ignore +SET statement_timeout = '120s'; +RESET anser.timeout_ms; +DO $$ +BEGIN + PERFORM pg_temp.rpt_fwf_fault('anser_before_deliver', 'reset', true); + IF pg_temp.rpt_fwf_run(true) IS DISTINCT FROM pg_temp.rpt_fwf_run(false) THEN + RAISE EXCEPTION 'next command after cancel'; + END IF; +END +$$; +SELECT count(*) AS dim_rows FROM rpt_fwf_dim; +DROP TABLE rpt_fwf_fact, rpt_fwf_dim, rpt_fwf_sub; +DROP EXTENSION robust_predicate_transfer; +RESET rpt.enable; +RESET rpt.forward_min_root_rows; +RESET rpt.forward_max_exchange_ms; +RESET anser.debug; +RESET anser.runtime_filter; +RESET optimizer; +RESET statement_timeout; diff --git a/gpcontrib/robust_predicate_transfer/src/rptforward.cpp b/gpcontrib/robust_predicate_transfer/src/rptforward.cpp new file mode 100644 index 00000000000..316d6660e92 --- /dev/null +++ b/gpcontrib/robust_predicate_transfer/src/rptforward.cpp @@ -0,0 +1,587 @@ +/*------------------------------------------------------------------------- + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * rptforward.cpp + * Phase III: build the forward reducer as an InitPlan of the statement + * and apply its filters to the root scans of the main plan. + * + * A transfer is an Anser channel, except a local hop consumed only inside + * the reducer, which is handed over in-process (rptlocal.c). The reducer + * InitPlan is dispatched by + * preprocess_initplans() before the main plan, under the same + * gp_command_count, and the coordinator keeps a command's channels until + * ExecutorEnd -- so every channel is complete before a main-plan consumer + * subscribes, and main-plan consumers never wait on anything still running. + * + * Reducer scans are copies of the main plan's scans (same scanrelid, so the + * same RTE, locks and permission checks) with a key-only targetlist. Their + * row set must be the main scan's or a superset (design rule 13): the quals + * are copied whole, in order, or dropped whole unless every one of them is + * proven unable to fail. The reducer runs eagerly, over rows the main plan + * may never visit (LIMIT 0, an empty outer side), so a qual that can raise an + * error there -- 100 / a with a = 0 -- would turn a successful query into a + * failing one, and the planner's fail-open cannot catch an executor error. + * + * Everything runs on a private copy of the statement; the caller publishes + * it only when the whole pass succeeds. PostgreSQL calls are made through + * RptPgCall, because C++ containers are alive throughout. + * + * IDENTIFICATION + * gpcontrib/robust_predicate_transfer/src/rptforward.cpp + * + *------------------------------------------------------------------------- + */ +extern "C" +{ +#include "postgres.h" + +#include "cdb/cdbllize.h" +#include "cdb/cdbutil.h" +#include "nodes/makefuncs.h" +#include "nodes/nodeFuncs.h" +#include "optimizer/clauses.h" +#include "optimizer/optimizer.h" +#include "optimizer/walkers.h" +#include "utils/lsyscache.h" + +#include "catalog/pg_class.h" +#include "utils/syscache.h" + +#include "anser.h" +#include "anserplan.h" +#include "rptlocal.h" +} + +#include "rpt.h" +#include "rptqual.h" + +#include + +/* ---- finding where a main-plan scan hangs ---- */ + +typedef struct RptParentContext +{ + plan_tree_base_prefix base; /* must be first */ + const std::set *targets; + std::map *parents; + std::vector *stack; /* enclosing plan nodes */ +} RptParentContext; + +static bool +rpt_parent_walker(Node *node, void *context) +{ + RptParentContext *ctx = (RptParentContext *) context; + + if (node == NULL) + return false; + if (!is_plan_node(node)) + return plan_tree_walker(node, rpt_parent_walker, context, false); + + if (ctx->targets->count((Plan *) node) && !ctx->stack->empty()) + (*ctx->parents)[(Plan *) node] = ctx->stack->back(); + + ctx->stack->push_back((Plan *) node); + bool result = plan_tree_walker(node, rpt_parent_walker, context, false); + + ctx->stack->pop_back(); + return result; +} + +/* Parents of the targets in the main plan tree only; SubPlans are not + * entered, so a scan inside one is simply not found. */ +static std::map +rpt_find_parents(PlannedStmt *stmt, const std::set &targets) +{ + std::map parents; + std::vector stack; + RptParentContext ctx; + + ctx.targets = &targets; + ctx.parents = &parents; + ctx.stack = &stack; + RptPgCall([&]() { + exec_init_plan_tree_base(&ctx.base, stmt); + (void) rpt_parent_walker((Node *) stmt->planTree, &ctx); + }); + return parents; +} + +/* Replace `child` under `parent`. Only the links key resolution can pass + * through hold base scans here: the outer/inner trees and Anser's own nodes. */ +static bool +rpt_relink(Plan *parent, Plan *child, Plan *replacement) +{ + if (parent->lefttree == child) + { + parent->lefttree = replacement; + return true; + } + if (parent->righttree == child) + { + parent->righttree = replacement; + return true; + } + if (IsA(parent, CustomScan)) + { + ListCell *lc; + + foreach(lc, ((CustomScan *) parent)->custom_plans) + { + if (lfirst(lc) == child) + { + lfirst(lc) = replacement; + return true; + } + } + } + return false; +} + +/* Output position of a base column in a scan's targetlist, or 0. */ +static AttrNumber +rpt_scan_key_position(Plan *scan, const RptKey &key) +{ + ListCell *lc; + + foreach(lc, scan->targetlist) + { + TargetEntry *tle = (TargetEntry *) lfirst(lc); + + if (IsA(tle->expr, Var) && + ((Var *) tle->expr)->varno == (int) ((Scan *) scan)->scanrelid && + ((Var *) tle->expr)->varattno == key.attno) + return tle->resno; + } + return 0; +} + +/* ---- rule 13: quals a reducer scan may evaluate ---- */ + +/* Only explicitly trusted built-in comparisons and their hash support may + * run eagerly. LEAKPROOF prevents argument disclosure, not unconditional + * errors. Every other call, coercion, Param or SubPlan drops the whole qual. */ +static bool +rpt_unsafe_qual_walker(Node *node, void *context) +{ + if (node == NULL) + return false; + switch (nodeTag(node)) + { + case T_Var: + case T_Const: + case T_List: + case T_BoolExpr: + case T_NullTest: + case T_BooleanTest: + case T_RelabelType: + break; + case T_FuncExpr: + if (!RptQualFunctionSafe(((FuncExpr *) node)->funcid)) + return true; + break; + case T_OpExpr: + case T_DistinctExpr: + case T_NullIfExpr: + set_opfuncid((OpExpr *) node); + if (!RptQualFunctionSafe(((OpExpr *) node)->opfuncid)) + return true; + break; + case T_ScalarArrayOpExpr: + { + ScalarArrayOpExpr *op = (ScalarArrayOpExpr *) node; + + set_sa_opfuncid(op); + if (!RptQualFunctionSafe(op->opfuncid) || + (OidIsValid(op->negfuncid) && !RptQualFunctionSafe(op->negfuncid)) || + (OidIsValid(op->hashfuncid) && !RptQualHashFunctionSafe(op->hashfuncid))) + return true; + break; + } + default: + return true; /* Param, SubPlan, coercions, everything else */ + } + return expression_tree_walker(node, rpt_unsafe_qual_walker, context); +} + +/* Whether a reducer may evaluate these quals (all of them, or none). */ +static bool +rpt_quals_safe(List *quals) +{ + return quals == NIL || RptPgCall([&]() { + return !contain_volatile_functions((Node *) quals) && + !rpt_unsafe_qual_walker((Node *) quals, NULL); + }); +} + +/* All of the main scan's quals, or none of them. */ +static List * +rpt_reducer_quals(List *quals) +{ + if (quals == NIL || !rpt_quals_safe(quals)) + return NIL; + return (List *) copyObjectImpl(quals); +} + +/* Rows per segment before quals, from pg_class.reltuples; < 0 when unknown. + * Partitioned tables spread them over numsegments, a replicated table keeps + * them all on each segment (as costsize.c's adjust_reloptinfo). */ +static double +rpt_base_rows(const RptVertexInfo &v) +{ + if (!OidIsValid(v.relid)) + return -1; + return RptPgCall([&]() { + HeapTuple tuple = SearchSysCache1(RELOID, ObjectIdGetDatum(v.relid)); + double rows; + + if (!HeapTupleIsValid(tuple)) + return -1.0; + rows = ((Form_pg_class) GETSTRUCT(tuple))->reltuples; + ReleaseSysCache(tuple); + if (rows < 0) + return -1.0; + if (!v.replicated && v.numsegments > 0) + rows /= v.numsegments; + return rows; + }); +} + +/* What each vertex's reducer scan is expected to keep. */ +static std::map +rpt_vertex_estimates(const RptTransferMgr &forward) +{ + std::map est; + + for (const RptVertexInfo &v : forward.vertices) + { + RptForwardVertexEst e; + double base = rpt_base_rows(v); + + e.quals_dropped = !rpt_quals_safe(v.scan->qual); + e.rows = base > 0 ? base : (double) v.cardinality; + if (v.scan->qual != NIL && !e.quals_dropped && base > 0) + e.selectivity = std::min(1.0, std::max(0.0, v.scan->plan_rows / base)); + est[v.id] = e; + } + return est; +} + +/* ---- node construction ---- */ + +static void +rpt_reset_copied_plan(Plan *plan, int *next_id) +{ + plan->plan_node_id = (*next_id)++; + plan->lefttree = NULL; + plan->righttree = NULL; + plan->initPlan = NIL; + plan->extParam = NULL; + plan->allParam = NULL; + plan->parallel_aware = false; + plan->async_capable = false; + plan->parallel = 0; + plan->flow = NULL; +} + +static Plan * +rpt_reducer_scan(Plan *main_scan, Oid relid, const std::vector &attnos, + bool *quals_dropped, int *next_id) +{ + Plan *scan = (Plan *) copyObjectImpl(main_scan); + List *tlist = NIL; + Index varno = ((Scan *) main_scan)->scanrelid; + AttrNumber resno = 0; + + for (AttrNumber attno : attnos) + { + Oid typid; + int32 typmod; + Oid collid; + + get_atttypetypmodcoll(relid, attno, &typid, &typmod, &collid); + tlist = lappend(tlist, + makeTargetEntry((Expr *) makeVar(varno, attno, typid, typmod, collid, 0), + ++resno, NULL, false)); + } + scan->targetlist = tlist; + scan->qual = rpt_reducer_quals(main_scan->qual); + *quals_dropped = (main_scan->qual != NIL && scan->qual == NIL); + rpt_reset_copied_plan(scan, next_id); + return scan; +} + +static Plan * +rpt_wrap_consumer(Plan *child, AttrNumber position, const RptForwardChannel &ch, + int width, int *next_id) +{ + CustomScan *cs = AnserBuildBloomConsumerScan(child, position, ch.condition_id, + ch.condition_key.c_str(), + ch.total_elems, (Size) ch.max_payload, + ch.planned_bytes, width, + false /* channels are complete */ ); + + cs->scan.plan.plan_node_id = (*next_id)++; + return (Plan *) cs; +} + +static Plan * +rpt_wrap_producer(Plan *child, AttrNumber position, const RptForwardChannel &ch, + int width, int *next_id) +{ + CustomScan *cs = AnserBuildBloomProducerScan(child, position, ch.condition_id, + ch.condition_key.c_str(), + ch.total_elems, (Size) ch.max_payload, + ch.planned_bytes, width); + + cs->scan.plan.plan_node_id = (*next_id)++; + return (Plan *) cs; +} + +static bool +rpt_anser_size(double est_rows, int64 *total_elems, int64 *max_payload, + int64 *planned_bytes) +{ + return RptPgCall([&]() { + return AnserRuntimeFilterSize(est_rows, total_elems, max_payload, + planned_bytes); + }); +} + +static bool +rpt_plan_has_anser_nodes_walker(Node *node, void *context) +{ + if (node == NULL) + return false; + if (is_plan_node(node) && + (AnserIsRuntimeFilterScan((Plan *) node) || RptIsLocalScan((Plan *) node))) + return true; + return plan_tree_walker(node, rpt_plan_has_anser_nodes_walker, context, true); +} + +/* Main-plan scans of `vertex`, wrapped in consumers of every channel into it. + * False when the scan cannot be found or keyed; nothing is changed then. */ +static bool +rpt_apply_in_main(const RptVertexInfo &vertex, + const std::vector &channels, + const std::map &parents, int width, + int *next_id, bool dry_run) +{ + auto parent = parents.find(vertex.scan); + + if (!IsA(vertex.scan, SeqScan) || parent == parents.end()) + return false; + for (const RptForwardChannel &ch : channels) + if (ch.to == vertex.id && !ch.in_process && + rpt_scan_key_position(vertex.scan, ch.to_key) == 0) + return false; + if (dry_run) + return true; + + Plan *node = vertex.scan; + + for (const RptForwardChannel &ch : channels) + if (ch.to == vertex.id && !ch.in_process) + node = RptPgCall([&]() { + return rpt_wrap_consumer(node, rpt_scan_key_position(vertex.scan, ch.to_key), + ch, width, next_id); + }); + return node != vertex.scan && rpt_relink(parent->second, vertex.scan, node); +} + +PlannedStmt * +RptInjectForward(PlannedStmt *stmt, bool materialize, std::string *trace) +{ + if (!gp_anser_enable || gp_anser_runtime_filter) + { + *trace += "forward: needs anser.enable=on and anser.runtime_filter=off\n"; + return stmt; + } + if (RptPgCall([&]() { + return rpt_plan_has_anser_nodes_walker((Node *) stmt->planTree, NULL); + })) + { + *trace += "forward: plan already carries Anser nodes\n"; + return stmt; + } + + PlannedStmt *work = materialize + ? RptPgCall([&]() { return (PlannedStmt *) copyObjectImpl(stmt); }) + : stmt; + + RptTransferMgr analysis; + + RptCollectGraph(work, &analysis); + if (analysis.vertices.empty()) + { + *trace += "forward: no transfer regions\n"; + return stmt; + } + RptBuildClasses(&analysis); + RptBuildTransferPlan(work, &analysis); + + RptTransferMgr forward; + + RptBuildForwardPlan(&analysis, &forward); + + RptForwardParams params; + + params.width = RptPgCall([]() { return getgpsegmentCount(); }); + params.ms_per_mib_part = rpt_forward_ms_per_mib_part; + params.max_exchange_ms = rpt_forward_max_exchange_ms; + params.min_root_rows = rpt_forward_min_root_rows; + params.max_selectivity = rpt_forward_max_selectivity; + params.apply_all = (rpt_forward_apply == RPT_FORWARD_APPLY_ALL); + + std::map est = rpt_vertex_estimates(forward); + std::vector comps; + + RptSelectForward(forward, params, est, rpt_anser_size, RptEstimateDistinct, &comps); + + /* Preflight against the plan: everything that could refuse a component + * is checked before any node is built, so a refusal changes nothing. */ + std::set targets; + + for (const RptVertexInfo &v : forward.vertices) + targets.insert(v.scan); + + std::map parents = rpt_find_parents(work, targets); + int next_id = 0; /* unused by dry runs */ + + for (RptForwardComponent &comp : comps) + { + if (!comp.accepted) + continue; + for (int id : comp.chain) + { + const RptVertexInfo &v = forward.vertices[forward.by_rti.at(id)]; + + if (!IsA(v.scan, SeqScan) || !OidIsValid(v.relid)) + { + comp.accepted = false; + comp.reason = "chain table " + std::to_string(id) + " is not a plain SeqScan"; + break; + } + } + if (comp.accepted && + !rpt_apply_in_main(forward.vertices[forward.by_rti.at(comp.root)], + comp.channels, parents, params.width, &next_id, true)) + { + comp.accepted = false; + comp.reason = "root scan not found, not a SeqScan, or key not in its output"; + } + } + + RptDumpForward(comps, trace); + for (const RptForwardComponent &comp : comps) + for (int id : comp.chain) + if (est[id].quals_dropped) + *trace += "forward: quals of " + std::to_string(id) + + " dropped in the reducer (not provably error-free)\n"; + + bool any = false; + + for (const RptForwardComponent &comp : comps) + any = any || comp.accepted; + if (!materialize || !any) + return stmt; + + next_id = RptPgCall([&]() { return AnserMaxPlanNodeId(work); }) + 1; + + List *children = NIL; + + for (const RptForwardComponent &comp : comps) + { + if (!comp.accepted) + continue; + + for (int id : comp.chain) + { + const RptVertexInfo &v = forward.vertices[forward.by_rti.at(id)]; + std::vector attnos; + + for (const RptForwardChannel &ch : comp.channels) + { + AttrNumber a = 0; + + if (ch.to == id) + a = ch.to_key.attno; + else if (ch.from == id) + a = ch.from_key.attno; + if (a != 0 && std::find(attnos.begin(), attnos.end(), a) == attnos.end()) + attnos.push_back(a); + } + + bool dropped = false; /* reported with the dump */ + Plan *node = RptPgCall([&]() { + return rpt_reducer_scan(v.scan, v.relid, attnos, &dropped, &next_id); + }); + + /* Consumers below producers: a filter is built from rows the + * inbound filters already reduced (design rule 4). */ + for (const RptForwardChannel &ch : comp.channels) + { + if (ch.to != id) + continue; + AttrNumber pos = (AttrNumber) (std::find(attnos.begin(), attnos.end(), + ch.to_key.attno) - attnos.begin()) + 1; + + node = RptPgCall([&]() { + if (!ch.in_process) + return rpt_wrap_consumer(node, pos, ch, params.width, &next_id); + CustomScan *cs = RptBuildLocalConsumerScan(node, pos, ch.condition_id, + ch.total_elems); + + cs->scan.plan.plan_node_id = next_id++; + return (Plan *) cs; + }); + } + for (const RptForwardChannel &ch : comp.channels) + { + if (ch.from != id) + continue; + AttrNumber pos = (AttrNumber) (std::find(attnos.begin(), attnos.end(), + ch.from_key.attno) - attnos.begin()) + 1; + + node = RptPgCall([&]() { + if (!ch.in_process) + return rpt_wrap_producer(node, pos, ch, params.width, &next_id); + CustomScan *cs = RptBuildLocalProducerScan(node, pos, ch.condition_id, + ch.total_elems); + + cs->scan.plan.plan_node_id = next_id++; + return (Plan *) cs; + }); + } + children = RptPgCall([&]() { return lappend(children, node); }); + } + + if (!rpt_apply_in_main(forward.vertices[forward.by_rti.at(comp.root)], + comp.channels, parents, params.width, &next_id, false)) + RptPgCall([]() { elog(ERROR, "RPT forward: root relink failed after preflight"); }); + + if (rpt_forward_apply == RPT_FORWARD_APPLY_ALL) + for (int id : comp.chain) + (void) rpt_apply_in_main(forward.vertices[forward.by_rti.at(id)], + comp.channels, parents, params.width, + &next_id, false); + } + + RptAttachReducerInitPlan(work, children, params.width); + return work; +} diff --git a/gpcontrib/robust_predicate_transfer/src/rptforwardplan.cpp b/gpcontrib/robust_predicate_transfer/src/rptforwardplan.cpp new file mode 100644 index 00000000000..d4c72c61ba3 --- /dev/null +++ b/gpcontrib/robust_predicate_transfer/src/rptforwardplan.cpp @@ -0,0 +1,221 @@ +/*------------------------------------------------------------------------- + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * rptforwardplan.cpp + * Phase III selection: which forward chains to build, their channels, + * and the cost gate. No PostgreSQL calls, so it is unit-tested directly. + * + * The cost model is the one measured on 2026-11-10 (docs/design.md): every + * global hop pays about c milliseconds per MiB of bitset per producer, because + * the coordinator receives and folds the parts one at a time. Hops of one + * chain run one after another inside the reducer, so a component's cost is + * the sum over its channels. A local hop consumed only inside the reducer is + * handed over in-process (rptlocal.c) and costs no exchange. + * + * The benefit side is the paper's selectivity test: inject only when the + * chain is expected to keep less than lambda of the root's rows. A vertex + * keeps the fraction its own reducer quals keep, times what its inbound + * filters keep; a filter from u keeps s(u) * min(1, ndv(u.key) / ndv(v.key)) + * of v, assuming the smaller key domain is contained in the larger one. + * + * IDENTIFICATION + * gpcontrib/robust_predicate_transfer/src/rptforwardplan.cpp + * + *------------------------------------------------------------------------- + */ +extern "C" +{ +#include "postgres.h" +} + +#include "rpt.h" + +#include +#include + +/* Every vertex whose filters can reach `root` through forward edges. */ +static std::set +rpt_forward_members(const RptTransferMgr &forward, int root) +{ + std::set members; + std::vector queue(1, root); + + members.insert(root); + while (!queue.empty()) + { + int node = queue.back(); + auto it = forward.graph.find(node); + + queue.pop_back(); + if (it == forward.graph.end()) + continue; + /* An in-edge's destination field names the producing side. */ + for (const RptGraphEdge &in : it->second.forward_stage.in) + if (members.insert(in.destination).second) + queue.push_back(in.destination); + } + return members; +} + +/* The fraction of `to` rows a filter from `from` keeps. */ +static double +rpt_transfer_keep(const RptTransferMgr &forward, const RptForwardChannel &ch, + double from_keep, RptNdvFn ndv_fn) +{ + double d = ndv_fn ? ndv_fn(forward.vertices[forward.by_rti.at(ch.from)], + ch.from_key.attno) : -1; + double n = ndv_fn ? ndv_fn(forward.vertices[forward.by_rti.at(ch.to)], + ch.to_key.attno) : -1; + + if (d > 0 && n > 0 && d < n) + return from_keep * d / n; + return from_keep; +} + +void +RptSelectForward(const RptTransferMgr &forward, const RptForwardParams ¶ms, + const std::map &est, + RptFilterSizeFn size_fn, RptNdvFn ndv_fn, + std::vector *out) +{ + uint32 next_condition = 0; + + out->clear(); + for (int root : forward.roots) + { + RptForwardComponent comp; + std::set members = rpt_forward_members(forward, root); + std::map keep; /* chain vertex -> fraction it keeps */ + bool sized = true; + + comp.root = root; + + /* reducer_order is already topological: producers before consumers. */ + for (int id : forward.reducer_order) + if (id != root && members.count(id)) + comp.chain.push_back(id); + + for (int id : comp.chain) + { + const RptGraphNode &node = forward.graph.at(id); + const RptVertexInfo &source = forward.vertices[forward.by_rti.at(id)]; + auto e = est.find(id); + double rows = (e != est.end() && e->second.rows >= 0) + ? e->second.rows : (double) source.cardinality; + double own = e != est.end() ? e->second.selectivity : 1.0; + double k = own; + + /* In-channels of id were made by earlier chain vertices. */ + for (const RptForwardChannel &in : comp.channels) + if (in.to == id) + k *= rpt_transfer_keep(forward, in, keep[in.from], ndv_fn); + keep[id] = k; + + for (const RptGraphEdge &edge : node.forward_stage.out) + { + RptForwardChannel ch; + + if (edge.left_keys.empty() || edge.right_keys.empty()) + continue; + ch.from = id; + ch.to = edge.destination; + ch.from_key = edge.left_keys[0]; /* out-edge: left = this side */ + ch.to_key = edge.right_keys[0]; + ch.local = edge.local; + ch.in_process = ch.local && ch.to != root && !params.apply_all; + + /* Sized for what the reducer scan inserts: its rows after its + * own quals -- all of them when the quals were dropped. */ + if (!size_fn(rows * own, &ch.total_elems, + &ch.max_payload, &ch.planned_bytes)) + { + comp.reason = "filter sizing declined for " + + std::to_string(ch.from) + "->" + std::to_string(ch.to); + sized = false; + break; + } + if (!ch.in_process) + ch.est_cost_ms = params.ms_per_mib_part * params.width * + ((double) ch.planned_bytes / (1024.0 * 1024.0)); + comp.est_cost_ms += ch.est_cost_ms; + comp.channels.push_back(ch); + } + if (!sized) + break; + } + + for (const RptForwardChannel &ch : comp.channels) + if (ch.to == root) + comp.est_selectivity *= rpt_transfer_keep(forward, ch, keep[ch.from], ndv_fn); + + const RptVertexInfo &rootv = forward.vertices[forward.by_rti.at(root)]; + + if (!sized) + ; /* reason already set */ + else if (comp.channels.empty()) + comp.reason = "no forward edges"; + else if ((double) rootv.cardinality < params.min_root_rows) + comp.reason = "root rows below rpt.forward_min_root_rows"; + else if (params.max_selectivity < 1 && + comp.est_selectivity >= params.max_selectivity) + comp.reason = "estimated root selectivity not below rpt.forward_max_selectivity"; + else if (params.max_exchange_ms >= 0 && + comp.est_cost_ms > params.max_exchange_ms) + comp.reason = "estimated exchange cost above rpt.forward_max_exchange_ms"; + else + { + comp.accepted = true; + comp.reason = "inject"; + for (RptForwardChannel &ch : comp.channels) + { + ch.condition_id = next_condition++; + ch.condition_key = "rpt_f_" + std::to_string(ch.from) + "_" + + std::to_string(ch.to); + } + } + out->push_back(comp); + } +} + +void +RptDumpForward(const std::vector &components, std::string *out) +{ + std::ostringstream text; + + text << std::fixed << std::setprecision(3); + for (const RptForwardComponent &comp : components) + { + text << "forward component root=" << comp.root << " chain=["; + for (size_t i = 0; i < comp.chain.size(); ++i) + text << (i ? " " : "") << comp.chain[i]; + text << "] channels=" << comp.channels.size() + << " est_cost_ms=" << comp.est_cost_ms + << " est_sel=" << comp.est_selectivity + << " decision=" << comp.reason << '\n'; + for (const RptForwardChannel &ch : comp.channels) + text << "forward channel " << ch.from_key.rtindex << '.' << ch.from_key.attno + << "->" << ch.to_key.rtindex << '.' << ch.to_key.attno + << " key=" << (ch.condition_key.empty() ? "-" : ch.condition_key) + << " bytes=" << ch.planned_bytes + << " est_ms=" << ch.est_cost_ms + << (ch.local ? " local" : "") + << (ch.in_process ? " in-process" : "") << '\n'; + } + *out += text.str(); +} diff --git a/gpcontrib/robust_predicate_transfer/src/rptinit.cpp b/gpcontrib/robust_predicate_transfer/src/rptinit.cpp index 86495050d0e..bcae2c318a9 100644 --- a/gpcontrib/robust_predicate_transfer/src/rptinit.cpp +++ b/gpcontrib/robust_predicate_transfer/src/rptinit.cpp @@ -27,8 +27,9 @@ * * Phase I is analysis-only: the planner hook computes the transfer plan and * logs it at DEBUG1 when rpt.debug is on, and rpt_transfer_plan() exposes - * the same dump to SQL for the regression tests. Nothing is injected into - * the plan tree. + * the same dump to SQL for the regression tests. Runtime filters are not + * injected yet. The explicit debug_noop_initplan measurement switch adds + * only an empty distributed InitPlan. * * IDENTIFICATION * gpcontrib/robust_predicate_transfer/src/rptinit.cpp @@ -40,6 +41,7 @@ extern "C" #include "postgres.h" #include "cdb/cdbvars.h" +#include "cdb/cdbutil.h" #include "fmgr.h" #include "miscadmin.h" #include "nodes/pg_list.h" @@ -50,6 +52,8 @@ extern "C" #include "utils/guc.h" #include "utils/memutils.h" +#include "rptlocal.h" + PG_MODULE_MAGIC; PG_FUNCTION_INFO_V1(rpt_transfer_plan); @@ -68,7 +72,27 @@ bool rpt_backward = true; int rpt_max_backward_consumers = 4; int rpt_max_chain_depth = 4; +bool rpt_forward = true; +int rpt_forward_apply = RPT_FORWARD_APPLY_ROOT; +/* Measured 2026-11-10 on one host, 3 primaries (docs/design.md). */ +double rpt_forward_ms_per_mib_part = 3.5; +double rpt_forward_max_exchange_ms = 100.0; +double rpt_forward_min_root_rows = 10000.0; +/* The RPT+ paper's lambda (its Algorithm 2 threshold). */ +double rpt_forward_max_selectivity = 0.35; + +static const struct config_enum_entry rpt_forward_apply_options[] = { + {"root", RPT_FORWARD_APPLY_ROOT, false}, + {"all", RPT_FORWARD_APPLY_ALL, false}, + {NULL, 0, false} +}; + static planner_hook_type prev_planner_hook = NULL; +static bool rpt_debug_noop_initplan = false; + +/* Set while rpt_transfer_plan() plans its query: the dump must describe the + * uninjected plan, so the hook leaves it alone. */ +static bool rpt_suppress_injection = false; static void rpt_define_gucs(void); static PlannedStmt *rpt_planner(Query *parse, const char *query_string, @@ -95,6 +119,12 @@ rpt_analyze(PlannedStmt *stmt, StringInfo out) RptBuildClasses(&mgr); RptBuildTransferPlan(stmt, &mgr); RptDumpTransferPlan(&mgr, out); + + /* What the forward pass would do with this plan, built nothing. */ + std::string forward_trace; + + (void) RptInjectForward(stmt, false, &forward_trace); + RptPgCall([&]() { appendStringInfoString(out, forward_trace.c_str()); }); } return; } @@ -128,6 +158,12 @@ _PG_init(void) { rpt_define_gucs(); + /* + * The local reducer nodes travel to the segments inside dispatched plans, + * so every backend that loads us must be able to resolve them by name. + */ + RptRegisterLocalMethods(); + /* * Only a preloaded library can be relied on to have installed its hooks * in every backend. Loaded any other way the extension stays inert: the @@ -168,6 +204,12 @@ rpt_define_gucs(void) GUC_GPDB_NEED_SYNC, NULL, NULL, NULL); + DefineCustomBoolVariable("rpt.debug_noop_initplan", + "Adds an empty distributed InitPlan for overhead measurement.", + "QD-only, independent of rpt.enable. No filters are built or applied.", + &rpt_debug_noop_initplan, false, PGC_SUSET, 0, + NULL, NULL, NULL); + DefineCustomBoolVariable("rpt.backward", "Reserves the backward runtime switch for a future Phase II build.", "QD-only; this analysis-only build never injects executor nodes.", @@ -180,6 +222,43 @@ rpt_define_gucs(void) &rpt_max_backward_consumers, 4, 1, INT_MAX, PGC_USERSET, 0, NULL, NULL, NULL); + DefineCustomBoolVariable("rpt.forward", + "Builds the RPT+ forward reducer when rpt.enable is on.", + "QD-only. Needs anser.enable=on and anser.runtime_filter=off.", + &rpt_forward, true, PGC_USERSET, 0, + NULL, NULL, NULL); + + DefineCustomEnumVariable("rpt.forward_apply", + "Which main-plan scans apply forward filters.", + "root: only each chain's root; all: chain tables too.", + &rpt_forward_apply, RPT_FORWARD_APPLY_ROOT, + rpt_forward_apply_options, PGC_USERSET, 0, + NULL, NULL, NULL); + + DefineCustomRealVariable("rpt.forward_ms_per_mib_part", + "Estimated cost of one global hop per MiB of bitset per producer.", + "The c in c * n_producers * filter_MiB; QD-only.", + &rpt_forward_ms_per_mib_part, 3.5, 0.0, 1.0e6, + PGC_USERSET, 0, NULL, NULL, NULL); + + DefineCustomRealVariable("rpt.forward_max_exchange_ms", + "Skips a forward chain whose estimated exchange cost is higher.", + "-1 disables the limit; QD-only.", + &rpt_forward_max_exchange_ms, 100.0, -1.0, 1.0e9, + PGC_USERSET, 0, NULL, NULL, NULL); + + DefineCustomRealVariable("rpt.forward_min_root_rows", + "Skips a forward chain whose root scan is estimated smaller.", + "Compared with the root scan's plan_rows (per segment); QD-only.", + &rpt_forward_min_root_rows, 10000.0, 0.0, 1.0e15, + PGC_USERSET, 0, NULL, NULL, NULL); + + DefineCustomRealVariable("rpt.forward_max_selectivity", + "Skips a forward chain expected to keep at least this fraction of the root's rows.", + "Estimated from the reducer quals and column NDVs; 1 disables the check; QD-only.", + &rpt_forward_max_selectivity, 0.35, 0.0, 1.0, + PGC_USERSET, 0, NULL, NULL, NULL); + DefineCustomIntVariable("rpt.max_chain_depth", "Sets the maximum forward-chain length RPT+ may plan.", "Global hops require publish/merge/deliver barriers, so chain depth is capped; the default is a placeholder until hop latency is measured.", @@ -235,6 +314,169 @@ rpt_log_analysis(PlannedStmt *stmt) MemoryContextDelete(analysis_context); } +/* Contain C++ exceptions before returning to the PostgreSQL error handler. */ +static PlannedStmt * +rpt_build_noop_initplan(PlannedStmt *stmt) +{ + bool pg_error = false; + bool out_of_memory = false; + char message[256] = "unknown C++ exception"; + + try + { + PlannedStmt *copy = RptPgCall([&]() { + return (PlannedStmt *) copyObjectImpl(stmt); + }); + int width = RptPgCall([]() { return getgpsegmentCount(); }); + + RptAttachReducerInitPlan(copy, NIL, width); + return copy; + } + catch (const RptPgError &) + { + pg_error = true; + } + catch (const std::bad_alloc &) + { + out_of_memory = true; + } + catch (const std::exception &e) + { + strlcpy(message, e.what(), sizeof(message)); + } + catch (...) + { + } + if (pg_error) + PG_RE_THROW(); + if (out_of_memory) + ereport(ERROR, (errcode(ERRCODE_OUT_OF_MEMORY), + errmsg("out of memory building RPT measurement InitPlan"))); + ereport(ERROR, (errmsg("RPT measurement InitPlan failed: %s", message))); + return NULL; +} + +/* Atomic finished-plan surgery: retain the original on construction failure. + * This switch is only a measurement harness, independent of runtime filters. + * Successful allocations stay under the planner's memory context. */ +static PlannedStmt * +rpt_try_noop_initplan(PlannedStmt *stmt) +{ + MemoryContext saved_context = CurrentMemoryContext; + MemoryContext volatile build_context = NULL; + PlannedStmt *volatile result = stmt; + + PG_TRY(); + { + build_context = AllocSetContextCreate(saved_context, + "RPT measurement InitPlan", + ALLOCSET_DEFAULT_SIZES); + MemoryContextSwitchTo(build_context); + result = rpt_build_noop_initplan(stmt); + MemoryContextSwitchTo(saved_context); + } + PG_CATCH(); + { + MemoryContextSwitchTo(saved_context); + ErrorData *error = CopyErrorData(); + + FlushErrorState(); + if (build_context != NULL) + MemoryContextDelete(build_context); + if (error->sqlerrcode == ERRCODE_QUERY_CANCELED) + ReThrowError(error); + ereport(WARNING, (errmsg("RPT measurement InitPlan skipped: %s", error->message))); + FreeErrorData(error); + } + PG_END_TRY(); + + return result; +} + +/* Contain C++ exceptions before returning to the PostgreSQL error handler. */ +static PlannedStmt * +rpt_build_forward(PlannedStmt *stmt) +{ + bool pg_error = false; + bool out_of_memory = false; + char message[256] = "unknown C++ exception"; + + try + { + std::string trace; + PlannedStmt *result = RptInjectForward(stmt, true, &trace); + + if (rpt_debug) + RptPgCall([&]() { elog(DEBUG1, "rpt forward:\n%s", trace.c_str()); }); + return result; + } + catch (const RptPgError &) + { + pg_error = true; + } + catch (const std::bad_alloc &) + { + out_of_memory = true; + } + catch (const std::exception &e) + { + strlcpy(message, e.what(), sizeof(message)); + } + catch (...) + { + } + if (pg_error) + PG_RE_THROW(); + if (out_of_memory) + ereport(ERROR, (errcode(ERRCODE_OUT_OF_MEMORY), + errmsg("out of memory building the RPT forward reducer"))); + ereport(ERROR, (errmsg("RPT forward reducer failed: %s", message))); + return NULL; +} + +/* Same contract as rpt_try_noop_initplan: the original plan survives any + * failure, which is reported as a WARNING; only cancellation propagates. */ +static PlannedStmt * +rpt_try_forward(PlannedStmt *stmt) +{ + MemoryContext saved_context = CurrentMemoryContext; + MemoryContext volatile build_context = NULL; + PlannedStmt *volatile result = stmt; + + PG_TRY(); + { + build_context = AllocSetContextCreate(saved_context, + "RPT forward reducer", + ALLOCSET_DEFAULT_SIZES); + MemoryContextSwitchTo(build_context); + result = rpt_build_forward(stmt); + MemoryContextSwitchTo(saved_context); + /* Nothing injected: the scratch context holds only garbage. */ + if (result == stmt) + { + MemoryContextDelete(build_context); + build_context = NULL; + } + } + PG_CATCH(); + { + MemoryContextSwitchTo(saved_context); + ErrorData *error = CopyErrorData(); + + FlushErrorState(); + if (build_context != NULL) + MemoryContextDelete(build_context); + if (error->sqlerrcode == ERRCODE_QUERY_CANCELED) + ReThrowError(error); + ereport(WARNING, (errmsg("RPT forward reducer skipped: %s", error->message))); + FreeErrorData(error); + result = stmt; + } + PG_END_TRY(); + + return result; +} + /* * Plan as usual, then run the analysis over the finished tree. Chaining * prev first means that when Anser is preloaded before us its injection pass @@ -260,6 +502,17 @@ rpt_planner(Query *parse, const char *query_string, int cursorOptions, result->planTree != NULL) rpt_log_analysis(result); + if (rpt_enable && rpt_forward && !rpt_suppress_injection && + Gp_role == GP_ROLE_DISPATCH && + result != NULL && result->commandType == CMD_SELECT && + result->planTree != NULL && result->intoClause == NULL && + result->copyIntoClause == NULL && result->refreshClause == NULL) + result = rpt_try_forward(result); + + if (rpt_debug_noop_initplan && Gp_role == GP_ROLE_DISPATCH && + result != NULL && result->commandType == CMD_SELECT && result->planTree != NULL) + result = rpt_try_noop_initplan(result); + return result; } @@ -304,7 +557,18 @@ rpt_transfer_plan_impl(FunctionCallInfo fcinfo) * runtime_filter GUC on the tree we analyze already carries Anser's * injected nodes -- exactly what production planning will hand us. */ - stmt = pg_plan_query(query, query_string, 0, NULL); + /* The dump describes the uninjected plan; the forward decisions are + * appended by a dry run inside rpt_analyze(). */ + rpt_suppress_injection = true; + PG_TRY(); + { + stmt = pg_plan_query(query, query_string, 0, NULL); + } + PG_FINALLY(); + { + rpt_suppress_injection = false; + } + PG_END_TRY(); initStringInfo(&buf); rpt_analyze(stmt, &buf); diff --git a/gpcontrib/robust_predicate_transfer/src/rptinject.cpp b/gpcontrib/robust_predicate_transfer/src/rptinject.cpp index 554d595534e..eafe3c731f0 100644 --- a/gpcontrib/robust_predicate_transfer/src/rptinject.cpp +++ b/gpcontrib/robust_predicate_transfer/src/rptinject.cpp @@ -35,12 +35,13 @@ extern "C" /* The caller owns a private copy of the statement. This function may throw; * publish that copy only after the entire injection pass has succeeded. * Children must be non-parallel reducer producers that return NULL at EOF. - * The final empty Result makes EXISTS drain the Sequence completely. */ + * The final empty Result makes EXISTS drain the Sequence completely. + * NIL children builds the no-op shape used to measure dispatch overhead. */ void RptAttachReducerInitPlan(PlannedStmt *stmt, List *children, int width) { RptPgCall([&]() { - if (!stmt->planTree || width <= 0 || children == NIL || + if (!stmt->planTree || width <= 0 || stmt->numSlices <= 0 || !stmt->slices || (stmt->subplans != NIL && !stmt->subplan_sliceIds)) elog(ERROR, "invalid RPT reducer InitPlan input"); diff --git a/gpcontrib/robust_predicate_transfer/src/rptlocal.c b/gpcontrib/robust_predicate_transfer/src/rptlocal.c new file mode 100644 index 00000000000..4807c7ae0fd --- /dev/null +++ b/gpcontrib/robust_predicate_transfer/src/rptlocal.c @@ -0,0 +1,429 @@ +/*------------------------------------------------------------------------- + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * rptlocal.c + * In-process bloom handoff for local reducer hops (design rule 16). + * + * A local hop joins co-located tables: every row of the destination that can + * match a row of the source lives on the same segment as that row. So the + * source's filter on segment i is complete for the destination on segment i, + * and the hop needs no coordinator. Within the reducer both scans run in one + * QE process, as children of one Sequence: the producer's child is exhausted + * before the consumer's scan starts. The producer leaves its filter in a + * per-execution registry; the consumer picks it up there. + * + * Only hops consumed inside the reducer use this. A filter the main plan + * applies goes through Anser, because the main plan is a different dispatch. + * + * Failing open: a consumer that finds no completed filter passes every row. + * The registry entry lives in the executor's query context and unregisters + * itself when that context goes, so an aborted execution leaves nothing behind. + * + * Keys are hashed as raw Datums, exactly as Anser does: both sides have the + * same by-value type, whose equality is bitwise (rptregion.cpp). + * + * IDENTIFICATION + * gpcontrib/robust_predicate_transfer/src/rptlocal.c + * + *------------------------------------------------------------------------- + */ +#include "postgres.h" + +#include "commands/explain.h" +#include "executor/executor.h" +#include "lib/bloomfilter.h" +#include "lib/ilist.h" +#include "miscadmin.h" +#include "nodes/extensible.h" +#include "nodes/makefuncs.h" +#include "nodes/nodeFuncs.h" +#include "utils/memutils.h" + +#include "rptlocal.h" + +/* custom_private layout */ +#define RPT_LOCAL_PRIV_CONDITION 0 +#define RPT_LOCAL_PRIV_KEY_ATTNO 1 +#define RPT_LOCAL_PRIV_TOTAL_ELEMS 2 + +/* One filter of one execution. */ +typedef struct RptLocalEntry +{ + dlist_node node; + EState *estate; + uint32 condition_id; + bloom_filter *filter; + bool complete; /* the producer's child was exhausted */ + MemoryContextCallback unregister; +} RptLocalEntry; + +static dlist_head rpt_local_entries = DLIST_STATIC_INIT(rpt_local_entries); + +typedef struct RptLocalScanState +{ + CustomScanState csstate; + uint32 condition_id; + AttrNumber key_attno; + int64 total_elems; + bool started; + RptLocalEntry *entry; /* producer: where it adds; consumer: what it probes */ +} RptLocalScanState; + +static Node *rpt_local_create_producer(CustomScan *cscan); +static Node *rpt_local_create_consumer(CustomScan *cscan); +static void rpt_local_begin(CustomScanState *node, EState *estate, int eflags); +static TupleTableSlot *rpt_local_produce_exec(CustomScanState *node); +static TupleTableSlot *rpt_local_consume_exec(CustomScanState *node); +static void rpt_local_end(CustomScanState *node); +static void rpt_local_rescan(CustomScanState *node); +static void rpt_local_explain(CustomScanState *node, List *ancestors, + ExplainState *es); + +static const CustomScanMethods rpt_local_producer_methods = { + .CustomName = "RPT Local Bloom Producer", + .CreateCustomScanState = rpt_local_create_producer, +}; + +static const CustomScanMethods rpt_local_consumer_methods = { + .CustomName = "RPT Local Bloom Consumer", + .CreateCustomScanState = rpt_local_create_consumer, +}; + +static const CustomExecMethods rpt_local_producer_exec = { + .CustomName = "RPT Local Bloom Producer", + .BeginCustomScan = rpt_local_begin, + .ExecCustomScan = rpt_local_produce_exec, + .EndCustomScan = rpt_local_end, + .ReScanCustomScan = rpt_local_rescan, + .ExplainCustomScan = rpt_local_explain, +}; + +static const CustomExecMethods rpt_local_consumer_exec = { + .CustomName = "RPT Local Bloom Consumer", + .BeginCustomScan = rpt_local_begin, + .ExecCustomScan = rpt_local_consume_exec, + .EndCustomScan = rpt_local_end, + .ReScanCustomScan = rpt_local_rescan, + .ExplainCustomScan = rpt_local_explain, +}; + +void +RptRegisterLocalMethods(void) +{ + RegisterCustomScanMethods(&rpt_local_producer_methods); + RegisterCustomScanMethods(&rpt_local_consumer_methods); +} + +bool +RptIsLocalScan(const Plan *plan) +{ + if (plan == NULL || !IsA(plan, CustomScan)) + return false; + return ((const CustomScan *) plan)->methods == &rpt_local_producer_methods || + ((const CustomScan *) plan)->methods == &rpt_local_consumer_methods; +} + +/* ---- plan construction ---- */ + +/* Pass-through targetlist over the child's output, as Anser's nodes use. */ +static List * +rpt_local_identity_tlist(List *child_tlist) +{ + List *tlist = NIL; + ListCell *lc; + AttrNumber attno = 0; + + foreach(lc, child_tlist) + { + TargetEntry *tle = (TargetEntry *) lfirst(lc); + + attno++; + tlist = lappend(tlist, + makeTargetEntry((Expr *) makeVar(INDEX_VAR, attno, + exprType((Node *) tle->expr), + exprTypmod((Node *) tle->expr), + exprCollation((Node *) tle->expr), + 0), + attno, + tle->resname ? pstrdup(tle->resname) : NULL, + tle->resjunk)); + } + return tlist; +} + +static CustomScan * +rpt_local_build(const CustomScanMethods *methods, Plan *child, + AttrNumber key_attno, uint32 condition_id, int64 total_elems) +{ + CustomScan *cs = makeNode(CustomScan); + + cs->custom_private = list_make3(makeInteger((int) condition_id), + makeInteger((int) key_attno), + makeInteger((int) Min(total_elems, PG_INT32_MAX))); + cs->scan.plan.targetlist = rpt_local_identity_tlist(child->targetlist); + cs->scan.plan.startup_cost = child->startup_cost; + cs->scan.plan.total_cost = child->total_cost; + cs->scan.plan.plan_rows = child->plan_rows; + cs->scan.plan.plan_width = child->plan_width; + cs->scan.plan.parallel_aware = false; + cs->scan.plan.parallel_safe = false; + cs->scan.plan.flow = (Flow *) copyObject(child->flow); + cs->scan.scanrelid = 0; + cs->custom_plans = list_make1(child); + cs->custom_scan_tlist = copyObject(child->targetlist); + cs->methods = methods; + return cs; +} + +CustomScan * +RptBuildLocalProducerScan(Plan *child, AttrNumber key_attno, + uint32 condition_id, int64 total_elems) +{ + return rpt_local_build(&rpt_local_producer_methods, child, key_attno, + condition_id, total_elems); +} + +CustomScan * +RptBuildLocalConsumerScan(Plan *child, AttrNumber key_attno, + uint32 condition_id, int64 total_elems) +{ + return rpt_local_build(&rpt_local_consumer_methods, child, key_attno, + condition_id, total_elems); +} + +/* ---- registry ---- */ + +static void +rpt_local_unregister(void *arg) +{ + dlist_delete(&((RptLocalEntry *) arg)->node); +} + +static RptLocalEntry * +rpt_local_find(EState *estate, uint32 condition_id) +{ + dlist_iter iter; + + dlist_foreach(iter, &rpt_local_entries) + { + RptLocalEntry *e = dlist_container(RptLocalEntry, node, iter.cur); + + if (e->estate == estate && e->condition_id == condition_id) + return e; + } + return NULL; +} + +/* ---- execution ---- */ + +static Node * +rpt_local_create_state(CustomScan *cscan, const CustomExecMethods *exec) +{ + RptLocalScanState *st = (RptLocalScanState *) + newNode(sizeof(RptLocalScanState), T_CustomScanState); + + st->csstate.methods = exec; + return (Node *) st; +} + +static Node * +rpt_local_create_producer(CustomScan *cscan) +{ + return rpt_local_create_state(cscan, &rpt_local_producer_exec); +} + +static Node * +rpt_local_create_consumer(CustomScan *cscan) +{ + return rpt_local_create_state(cscan, &rpt_local_consumer_exec); +} + +static void +rpt_local_begin(CustomScanState *node, EState *estate, int eflags) +{ + RptLocalScanState *st = (RptLocalScanState *) node; + CustomScan *cscan = (CustomScan *) node->ss.ps.plan; + + st->condition_id = (uint32) intVal(list_nth(cscan->custom_private, RPT_LOCAL_PRIV_CONDITION)); + st->key_attno = (AttrNumber) intVal(list_nth(cscan->custom_private, RPT_LOCAL_PRIV_KEY_ATTNO)); + st->total_elems = intVal(list_nth(cscan->custom_private, RPT_LOCAL_PRIV_TOTAL_ELEMS)); + st->started = false; + st->entry = NULL; + + /* Nothing is allocated until the node runs: the coordinator initializes + * every slice, and EXPLAIN initializes without running. */ + node->custom_ps = list_make1(ExecInitNode((Plan *) linitial(cscan->custom_plans), + estate, eflags)); +} + +static TupleTableSlot * +rpt_local_pass(CustomScanState *node, TupleTableSlot *childslot) +{ + ExecCopySlot(node->ss.ss_ScanTupleSlot, childslot); + return node->ss.ss_ScanTupleSlot; +} + +static bool +rpt_local_recheck(CustomScanState *node, TupleTableSlot *slot) +{ + return true; +} + +static TupleTableSlot * +rpt_local_produce_next(CustomScanState *node) +{ + RptLocalScanState *st = (RptLocalScanState *) node; + PlanState *child = (PlanState *) linitial(node->custom_ps); + TupleTableSlot *slot; + + if (!st->started) + { + EState *estate = node->ss.ps.state; + + st->started = true; + st->entry = rpt_local_find(estate, st->condition_id); + + /* + * A rescan keeps the entry and adds to the same filter: the result + * is a superset of the keys of every pass, which is all a consumer + * needs. + */ + if (st->entry == NULL) + { + MemoryContext oldcxt = MemoryContextSwitchTo(estate->es_query_cxt); + RptLocalEntry *e = (RptLocalEntry *) palloc0(sizeof(RptLocalEntry)); + + e->estate = estate; + e->condition_id = st->condition_id; + e->filter = bloom_create(Max(st->total_elems, 1), work_mem, 0); + e->unregister.func = rpt_local_unregister; + e->unregister.arg = e; + MemoryContextRegisterResetCallback(estate->es_query_cxt, &e->unregister); + dlist_push_head(&rpt_local_entries, &e->node); + MemoryContextSwitchTo(oldcxt); + st->entry = e; + } + } + + slot = ExecProcNode(child); + if (TupIsNull(slot)) + { + st->entry->complete = true; + return NULL; + } + + { + bool isnull; + Datum value = slot_getattr(slot, st->key_attno, &isnull); + + if (!isnull) + bloom_add_element(st->entry->filter, (unsigned char *) &value, + sizeof(Datum)); + } + return rpt_local_pass(node, slot); +} + +static TupleTableSlot * +rpt_local_produce_exec(CustomScanState *node) +{ + return ExecScan(&node->ss, (ExecScanAccessMtd) rpt_local_produce_next, + (ExecScanRecheckMtd) rpt_local_recheck); +} + +static TupleTableSlot * +rpt_local_consume_next(CustomScanState *node) +{ + RptLocalScanState *st = (RptLocalScanState *) node; + PlanState *child = (PlanState *) linitial(node->custom_ps); + + if (!st->started) + { + RptLocalEntry *e = rpt_local_find(node->ss.ps.state, st->condition_id); + + st->started = true; + st->entry = (e != NULL && e->complete) ? e : NULL; + } + + for (;;) + { + TupleTableSlot *slot = ExecProcNode(child); + bool isnull; + Datum value; + + if (TupIsNull(slot)) + return NULL; + if (st->entry == NULL) + return rpt_local_pass(node, slot); /* fail open */ + + /* NULLs never match an equijoin key; let the join handle them. */ + value = slot_getattr(slot, st->key_attno, &isnull); + if (!isnull) + { + InstrCountFiltered1(node, 1); + if (bloom_lacks_element(st->entry->filter, (unsigned char *) &value, + sizeof(Datum))) + { + InstrCountFiltered2(node, 1); + continue; + } + } + return rpt_local_pass(node, slot); + } +} + +static TupleTableSlot * +rpt_local_consume_exec(CustomScanState *node) +{ + return ExecScan(&node->ss, (ExecScanAccessMtd) rpt_local_consume_next, + (ExecScanRecheckMtd) rpt_local_recheck); +} + +static void +rpt_local_end(CustomScanState *node) +{ + /* The entry belongs to the query context, not to this node. */ + ((RptLocalScanState *) node)->entry = NULL; + if (node->custom_ps != NIL) + ExecEndNode((PlanState *) linitial(node->custom_ps)); +} + +static void +rpt_local_rescan(CustomScanState *node) +{ + RptLocalScanState *st = (RptLocalScanState *) node; + + st->started = false; + st->entry = NULL; + if (node->custom_ps != NIL) + ExecReScan((PlanState *) linitial(node->custom_ps)); +} + +static void +rpt_local_explain(CustomScanState *node, List *ancestors, ExplainState *es) +{ + RptLocalScanState *st = (RptLocalScanState *) node; + + ExplainPropertyInteger("Local Bloom Condition", NULL, st->condition_id, es); + if (es->analyze && node->ss.ps.instrument != NULL && + node->methods == &rpt_local_consumer_exec && + node->ss.ps.instrument->nloops > 0) + ExplainPropertyFloat("Rows Removed by Local Bloom Filter", NULL, + node->ss.ps.instrument->nfiltered2 / + node->ss.ps.instrument->nloops, 0, es); +} diff --git a/gpcontrib/robust_predicate_transfer/test/graph_test.cpp b/gpcontrib/robust_predicate_transfer/test/graph_test.cpp index 273a4124d52..bba8df6a65e 100644 --- a/gpcontrib/robust_predicate_transfer/test/graph_test.cpp +++ b/gpcontrib/robust_predicate_transfer/test/graph_test.cpp @@ -32,6 +32,8 @@ #include "../src/rptplan.cpp" #include "../src/rptregion.cpp" #include "../src/rpteligible.cpp" +#include "../include/rptqual.h" +#include "../src/rptforwardplan.cpp" #include #include @@ -786,8 +788,264 @@ static void error_unwinds() CHECK(PG_exception_stack == NULL); } +static bool size_one_mib(double, int64 *elems, int64 *payload, int64 *planned) +{ + *elems = 1000; *payload = 1 << 20; *planned = 1 << 20; + return true; +} + +static bool size_declined(double, int64 *, int64 *, int64 *) +{ + return false; +} + +static RptTransferMgr forward_star() +{ + RptTransferMgr analysis; + vertex(analysis, 1, 1000000, false); + vertex(analysis, 2, 100, true); + vertex(analysis, 3, 50, true); + connect(analysis, edge(1, 2)); + connect(analysis, edge(1, 3)); + RptBuildClasses(&analysis); + rpt_max_chain_depth = 4; + RptBuildTransferPlan(NULL, &analysis); + RptTransferMgr forward; + RptBuildForwardPlan(&analysis, &forward); + return forward; +} + +static RptForwardParams forward_params(double budget, double min_rows) +{ + RptForwardParams p; + p.width = 3; p.ms_per_mib_part = 3.5; + p.max_exchange_ms = budget; p.min_root_rows = min_rows; + return p; +} + +static void forward_selection_star() +{ + RptTransferMgr forward = forward_star(); + std::vector comps; + RptSelectForward(forward, forward_params(-1, 0), {}, size_one_mib, nullptr, &comps); + CHECK(comps.size() == 1); + const RptForwardComponent &c = comps[0]; + CHECK(c.root == 1 && c.accepted && c.reason == "inject"); + CHECK(!c.chain.empty() && !c.channels.empty()); + CHECK(std::find(c.chain.begin(), c.chain.end(), 1) == c.chain.end()); + std::set ids; + double cost = 0; + bool reaches_root = false; + for (const RptForwardChannel &ch : c.channels) + { + /* producers before consumers: a channel's source precedes its target */ + auto from = std::find(c.chain.begin(), c.chain.end(), ch.from); + CHECK(from != c.chain.end()); + if (ch.to == c.root) + reaches_root = true; + else + { + auto to = std::find(c.chain.begin(), c.chain.end(), ch.to); + CHECK(to != c.chain.end() && from < to); + } + CHECK(ids.insert(ch.condition_id).second); + CHECK(ch.condition_key == "rpt_f_" + std::to_string(ch.from) + "_" + std::to_string(ch.to)); + CHECK(ch.from_key.rtindex == ch.from && ch.to_key.rtindex == ch.to); + CHECK(ch.est_cost_ms > 10.49 && ch.est_cost_ms < 10.51); /* 3.5 * 3 * 1 MiB */ + cost += ch.est_cost_ms; + } + CHECK(reaches_root); + CHECK(c.est_cost_ms > cost - 1e-9 && c.est_cost_ms < cost + 1e-9); + CHECK(*ids.begin() == 0 && *ids.rbegin() == ids.size() - 1); + std::string dump; + RptDumpForward(comps, &dump); + CHECK(dump.find("forward component root=1") == 0); + CHECK(dump.find("decision=inject") != std::string::npos); +} + +static void forward_selection_gates() +{ + RptTransferMgr forward = forward_star(); + std::vector comps; + + RptSelectForward(forward, forward_params(-1, 2000000), {}, size_one_mib, nullptr, &comps); + CHECK(!comps[0].accepted && comps[0].reason == "root rows below rpt.forward_min_root_rows"); + for (const RptForwardChannel &ch : comps[0].channels) + CHECK(ch.condition_key.empty()); + + RptSelectForward(forward, forward_params(1.0, 0), {}, size_one_mib, nullptr, &comps); + CHECK(!comps[0].accepted && + comps[0].reason == "estimated exchange cost above rpt.forward_max_exchange_ms"); + + RptSelectForward(forward, forward_params(-1, 0), {}, size_declined, nullptr, &comps); + CHECK(!comps[0].accepted && comps[0].reason.find("filter sizing declined") == 0); + + /* nothing filtered: no table can produce, so there is no chain */ + RptTransferMgr analysis; + vertex(analysis, 1, 1000000, false); + vertex(analysis, 2, 100, false); + connect(analysis, edge(1, 2)); + RptBuildClasses(&analysis); + RptBuildTransferPlan(NULL, &analysis); + RptTransferMgr bare; + RptBuildForwardPlan(&analysis, &bare); + RptSelectForward(bare, forward_params(-1, 0), {}, size_one_mib, nullptr, &comps); + for (const RptForwardComponent &c : comps) + CHECK(!c.accepted && c.reason == "no forward edges"); + + /* ids restart per call and only accepted components consume them */ + RptSelectForward(forward, forward_params(-1, 0), {}, size_one_mib, nullptr, &comps); + CHECK(comps[0].channels[0].condition_id == 0); +} + +/* Snowflake: fact 1 joins dimension 2 on 1.1 = 2.1, dimension 2 joins + * subdimension 3 on 2.2 = 3.1 -- a genuine chain 3 -> 2 -> 1. Colocated + * means 2 and 3 are both hash-distributed on their join columns, so 3 -> 2 + * is a local hop; 2 -> 1 never is. */ +static RptTransferMgr forward_chain(bool colocated) +{ + RptTransferMgr analysis; + vertex(analysis, 1, 1000000, false); + vertex(analysis, 2, 100, true); + vertex(analysis, 3, 50, true); + for (auto &v : analysis.vertices) + hash_policy(v); + analysis.vertices[analysis.by_rti[2]].distkeys = {(AttrNumber) (colocated ? 2 : 3)}; + connect(analysis, edge(1, 2)); + connect(analysis, edge(2, 3, false, false, 2, 1)); + RptBuildClasses(&analysis); + rpt_max_chain_depth = 4; + RptBuildTransferPlan(NULL, &analysis); + RptTransferMgr forward; + RptBuildForwardPlan(&analysis, &forward); + return forward; +} + +static std::vector sized_rows; + +static bool size_recording(double rows, int64 *elems, int64 *payload, int64 *planned) +{ + sized_rows.push_back(rows); + return size_one_mib(rows, elems, payload, planned); +} + +static double ndv_small_dims(const RptVertexInfo &v, AttrNumber) +{ + return v.id == 1 ? 1000 : 100; +} + +static double ndv_large_dims(const RptVertexInfo &v, AttrNumber) +{ + return v.id == 1 ? 100 : 1000; +} + +static void forward_selection_chain_and_selectivity() +{ + RptTransferMgr forward = forward_chain(false); + std::vector comps; + RptForwardParams params = forward_params(-1, 0); + std::map est; + + params.max_selectivity = 0.35; + est[2].rows = 100; est[2].selectivity = 0.1; + est[3].rows = 50; est[3].selectivity = 0.5; + sized_rows.clear(); + RptSelectForward(forward, params, est, size_recording, nullptr, &comps); + CHECK(comps.size() == 1 && comps[0].accepted); + CHECK(comps[0].chain == std::vector({3, 2})); + CHECK(comps[0].channels.size() == 2); + CHECK(comps[0].channels[0].from == 3 && comps[0].channels[0].to == 2); + CHECK(comps[0].channels[1].from == 2 && comps[0].channels[1].to == 1); + /* 2 keeps 0.1 of itself times 0.5 from 3; the root keeps what 2 keeps */ + CHECK(comps[0].est_selectivity > 0.0499 && comps[0].est_selectivity < 0.0501); + /* filters are sized for rows after the vertex's own reducer quals */ + CHECK(sized_rows == std::vector({25, 10})); + + /* NDV: a smaller source domain keeps proportionally less of the target */ + RptSelectForward(forward, params, est, size_one_mib, ndv_small_dims, &comps); + CHECK(comps[0].est_selectivity > 0.00499 && comps[0].est_selectivity < 0.00501); + /* a larger source domain cannot keep more than its own fraction */ + RptSelectForward(forward, params, est, size_one_mib, ndv_large_dims, &comps); + CHECK(comps[0].est_selectivity > 0.0499 && comps[0].est_selectivity < 0.0501); + + /* Quals dropped in the reducer (rule 13): nothing selective is left, the + * filters grow to the whole table, and lambda refuses the chain. */ + est[2].selectivity = 1; est[2].quals_dropped = true; + est[3].selectivity = 1; est[3].quals_dropped = true; + sized_rows.clear(); + RptSelectForward(forward, params, est, size_recording, nullptr, &comps); + CHECK(!comps[0].accepted && + comps[0].reason == "estimated root selectivity not below rpt.forward_max_selectivity"); + CHECK(sized_rows == std::vector({50, 100})); + for (const RptForwardChannel &ch : comps[0].channels) + CHECK(ch.condition_key.empty()); + + /* Just below lambda passes, exactly lambda does not; 1 disables it. */ + est[2].selectivity = 0.34; + RptSelectForward(forward, params, est, size_one_mib, nullptr, &comps); + CHECK(comps[0].accepted); + est[2].selectivity = 0.35; + RptSelectForward(forward, params, est, size_one_mib, nullptr, &comps); + CHECK(!comps[0].accepted); + params.max_selectivity = 1; + RptSelectForward(forward, params, est, size_one_mib, nullptr, &comps); + CHECK(comps[0].accepted); +} + +/* A local hop consumed only inside the reducer is in-process and free; the + * hop into the root, or any hop the main plan consumes, stays on Anser. */ +static void forward_selection_in_process() +{ + RptTransferMgr forward = forward_chain(true); + std::vector comps; + RptForwardParams params = forward_params(-1, 0); + + RptSelectForward(forward, params, {}, size_one_mib, nullptr, &comps); + CHECK(comps[0].accepted && comps[0].channels.size() == 2); + const RptForwardChannel &inner = comps[0].channels[0]; + const RptForwardChannel &into_root = comps[0].channels[1]; + CHECK(inner.to == 2 && inner.local && inner.in_process && inner.est_cost_ms == 0); + CHECK(into_root.to == 1 && !into_root.local && !into_root.in_process); + CHECK(comps[0].est_cost_ms > 10.49 && comps[0].est_cost_ms < 10.51); + std::string dump; + RptDumpForward(comps, &dump); + CHECK(dump.find(" local in-process\n") != std::string::npos); + + params.apply_all = true; + RptSelectForward(forward, params, {}, size_one_mib, nullptr, &comps); + CHECK(comps[0].channels[0].local); + for (const RptForwardChannel &ch : comps[0].channels) + CHECK(!ch.in_process && ch.est_cost_ms > 0); + + forward = forward_chain(false); + params.apply_all = false; + RptSelectForward(forward, params, {}, size_one_mib, nullptr, &comps); + for (const RptForwardChannel &ch : comps[0].channels) + CHECK(!ch.local && !ch.in_process); +} + +static void qual_function_allowlist() +{ + CHECK(RptQualFunctionSafe(F_INT4LT)); + CHECK(RptQualFunctionSafe(F_INT8EQ)); + CHECK(RptQualFunctionSafe(F_INT24NE)); + CHECK(RptQualFunctionSafe(F_OIDGE)); + CHECK(!RptQualFunctionSafe(F_INT4DIV)); + CHECK(!RptQualFunctionSafe(F_INT4PL)); + CHECK(!RptQualFunctionSafe(1000000)); /* user function, even if leakproof */ + CHECK(!RptQualFunctionSafe(InvalidOid)); + CHECK(RptQualHashFunctionSafe(F_HASHINT4)); + CHECK(!RptQualHashFunctionSafe(F_INT4EQ)); + CHECK(!RptQualHashFunctionSafe(1000000)); +} + int main() { + qual_function_allowlist(); + forward_selection_chain_and_selectivity(); + forward_selection_in_process(); + forward_selection_star(); + forward_selection_gates(); bypass_direction(false); bypass_direction(true); protection_and_idempotence(); diff --git a/gpcontrib/robust_predicate_transfer/test/inject_test.cpp b/gpcontrib/robust_predicate_transfer/test/inject_test.cpp index 9e58b2e7b3e..faef37d52b0 100644 --- a/gpcontrib/robust_predicate_transfer/test/inject_test.cpp +++ b/gpcontrib/robust_predicate_transfer/test/inject_test.cpp @@ -111,7 +111,7 @@ static void collect_ids(Plan *p, std::set &ids) } } -static void layout(bool existing) +static void layout(bool existing, bool noop) { PlannedStmt stmt = {}; Result main = {}; main.plan.type = T_Result; main.plan.plan_node_id = 5; @@ -131,7 +131,7 @@ static void layout(bool existing) stmt.planTree->initPlan = list_make1(&old_init); } Result child = {}; child.plan.type = T_Result; child.plan.plan_node_id = 110; - List *children = list_make1(&child); + List *children = noop ? NIL : list_make1(&child); int old_slices = stmt.numSlices; RptAttachReducerInitPlan(&stmt, children, 8); CHECK(stmt.numSlices == old_slices + 2); @@ -163,8 +163,9 @@ static void layout(bool existing) CHECK(motion->motionType == MOTIONTYPE_GATHER); CHECK(motion->plan.flow->flotype == FLOW_SINGLETON); Sequence *seq = (Sequence *) motion->plan.lefttree; - CHECK(IsA(seq, Sequence) && list_length(seq->subplans) == 2); - CHECK(linitial(seq->subplans) == &child && list_length(children) == 1); + CHECK(IsA(seq, Sequence) && list_length(seq->subplans) == (noop ? 1 : 2)); + CHECK(list_length(children) == (noop ? 0 : 1)); + if (!noop) CHECK(linitial(seq->subplans) == &child); CHECK(seq->plan.flow->flotype == FLOW_PARTITIONED && seq->plan.flow->numsegments == 8); Result *empty = (Result *) llast(seq->subplans); CHECK(IsA(empty, Result) && IsA(empty->resconstantqual, List)); @@ -176,12 +177,15 @@ static void layout(bool existing) std::set ids; collect_ids(stmt.planTree, ids); ListCell *lc; foreach(lc, stmt.subplans) collect_ids((Plan *) lfirst(lc), ids); - CHECK(ids.size() == (existing ? 6 : 5)); - CHECK(empty->plan.plan_node_id > 110 && seq->plan.plan_node_id > 110 && motion->plan.plan_node_id > 110); + CHECK(ids.size() == (existing ? 6U : 5U) - (noop ? 1U : 0U)); + int previous_max = noop ? (existing ? 90 : 5) : 110; + CHECK(empty->plan.plan_node_id > previous_max && + seq->plan.plan_node_id > previous_max && motion->plan.plan_node_id > previous_max); } int main() { - layout(false); layout(true); + layout(false, false); layout(true, false); + layout(false, true); layout(true, true); for (void *p : allocations) free(p); return 0; } From 50ee54ce51952d07ac62dc9d2a0e0a3e665b6025 Mon Sep 17 00:00:00 2001 From: Leonid Borchuk Date: Thu, 12 Nov 2026 16:35:10 +0300 Subject: [PATCH 23/27] Fix RPT key safety, walkers and regression tests Restrict raw-Datum Bloom keys to canonical built-in by-value types, so custom equality operators cannot introduce false negatives. Add the modulo-equality reproducer as a regression under both optimizers. Supply a PlannedStmt-backed context when checking existing InitPlans and expression SubPlans. Test this path with the real backend plan walker. Parse C++ locale headers before PostgreSQL gettext macros to avoid clean non-NLS build failures. Rename the reserved ANALYZE helper parameter and correct fault-test expected output. Replan fault queries when toggling RPT and assert that injected faults actually fire. Add step-marked forward diagnostics with an independent watchdog that captures activity before cancellation and leaves server backends intact. The reported forward-test hang remains unlocalized; no hang fix is claimed. Remove implementation-phase references and detailed design documents from version control while retaining local copies. Keep a general README with paper and artifact links, and record artifact attribution in NOTICE and LICENSE alongside the existing MIT license text. Validation: native graph, injection and backend-walker tests pass with -O3 -DNDEBUG; 10 survey and four diagnostic tests pass. Graph/walker tests pass ASan/UBSan. GCC 15 compiles all extension sources without the forced libintl include; local linking stops at the absent postgres executable. Cluster SQL regressions and the original GCC 13 environment remain unrun. Assisted-by: OpenAI Codex --- LICENSE | 6 + NOTICE | 9 + .../robust_predicate_transfer/.gitignore | 4 + gpcontrib/robust_predicate_transfer/Makefile | 28 +- gpcontrib/robust_predicate_transfer/README.md | 111 +++-- .../robust_predicate_transfer/docs/design.md | 415 ------------------ .../docs/phase3-experiments.md | 124 ------ .../docs/phase3-measurements.md | 87 ---- .../expected/rpt_forward.out | 22 +- .../expected/rpt_forward_faults.out | 29 +- .../expected/rpt_key_types.out | 92 ++++ .../robust_predicate_transfer/include/rpt.h | 33 +- .../include/rptlocal.h | 2 +- .../scripts/diagnose_forward.py | 147 +++++++ .../scripts/survey.py | 2 +- .../sql/rpt_forward.sql | 22 +- .../sql/rpt_forward_faults.sql | 24 +- .../sql/rpt_key_types.sql | 92 ++++ .../src/rpteligible.cpp | 6 +- .../src/rptforward.cpp | 29 +- .../src/rptforwardplan.cpp | 8 +- .../src/rptgraph.cpp | 4 +- .../robust_predicate_transfer/src/rptinit.cpp | 19 +- .../src/rptinject.cpp | 3 +- .../robust_predicate_transfer/src/rptlocal.c | 2 +- .../robust_predicate_transfer/src/rptplan.cpp | 6 +- .../src/rptregion.cpp | 43 +- .../test/diagnose_forward_test.py | 85 ++++ .../test/forward_walk_test.cpp | 98 +++++ .../test/graph_test.cpp | 34 +- 30 files changed, 845 insertions(+), 741 deletions(-) delete mode 100644 gpcontrib/robust_predicate_transfer/docs/design.md delete mode 100644 gpcontrib/robust_predicate_transfer/docs/phase3-experiments.md delete mode 100644 gpcontrib/robust_predicate_transfer/docs/phase3-measurements.md create mode 100644 gpcontrib/robust_predicate_transfer/expected/rpt_key_types.out create mode 100644 gpcontrib/robust_predicate_transfer/scripts/diagnose_forward.py create mode 100644 gpcontrib/robust_predicate_transfer/sql/rpt_key_types.sql create mode 100644 gpcontrib/robust_predicate_transfer/test/diagnose_forward_test.py create mode 100644 gpcontrib/robust_predicate_transfer/test/forward_walk_test.cpp diff --git a/LICENSE b/LICENSE index a42d93295ee..2bc7349dd78 100644 --- a/LICENSE +++ b/LICENSE @@ -394,6 +394,12 @@ The PostgreSQL software includes: contrib/pgcrypto/imath.c see licenses/LICENSE-imath.txt + RPT+ transfer-graph code adapted from dynamic-predicate-transfer: + gpcontrib/robust_predicate_transfer/include/rpt.h + gpcontrib/robust_predicate_transfer/src/rptgraph.cpp + gpcontrib/robust_predicate_transfer/src/rptplan.cpp + see licenses/LICENSE-mit-duckdb-rpt.txt + ---------------------------- Apache License v2.0 with LLVM Exceptions diff --git a/NOTICE b/NOTICE index 0226ded13da..e078e7b7f4f 100644 --- a/NOTICE +++ b/NOTICE @@ -10,3 +10,12 @@ This product includes software originally developed by VMware. Greenplum Database Copyright (c) 2004-2020 VMware, Inc. or its affiliates. All Rights Reserved. + +--------------------------------------------------------------------------- + +This product includes RPT+ transfer-graph code in +gpcontrib/robust_predicate_transfer adapted from the +dynamic-predicate-transfer artifact: +https://github.com/embryo-labs/dynamic-predicate-transfer + +Copyright 2018-2025 Stichting DuckDB Foundation diff --git a/gpcontrib/robust_predicate_transfer/.gitignore b/gpcontrib/robust_predicate_transfer/.gitignore index 39c5900422a..2dea2174cb0 100644 --- a/gpcontrib/robust_predicate_transfer/.gitignore +++ b/gpcontrib/robust_predicate_transfer/.gitignore @@ -1,2 +1,6 @@ /test/graph_test /test/inject_test +/test/forward_walk_test + +# Local design notes and implementation plans. +/docs/ diff --git a/gpcontrib/robust_predicate_transfer/Makefile b/gpcontrib/robust_predicate_transfer/Makefile index a872465ba37..95be2afbbd4 100644 --- a/gpcontrib/robust_predicate_transfer/Makefile +++ b/gpcontrib/robust_predicate_transfer/Makefile @@ -42,7 +42,7 @@ PGFILEDESC = "robust_predicate_transfer - RPT+ transfer plans over Anser" EXTENSION = robust_predicate_transfer DATA = robust_predicate_transfer--1.0.sql -REGRESS = rpt_regions rpt_graph rpt_backward_eligibility rpt_forward rpt_forward_faults +REGRESS = rpt_regions rpt_graph rpt_backward_eligibility rpt_key_types rpt_forward rpt_forward_faults # Anser is a sibling module, not a linked library: it is preloaded and dfmgr.c # loads modules RTLD_GLOBAL, so the AnserXxx entry points resolve at load time. @@ -101,7 +101,7 @@ preload-rpt: # cluster, and runs the tests. .PHONY: installcheck-with-install installcheck-with-install: - $(MAKE) unitcheck surveycheck + $(MAKE) unitcheck surveycheck diagnosticcheck $(MAKE) -C ../anser install $(MAKE) install $(MAKE) installcheck @@ -109,9 +109,10 @@ installcheck-with-install: # Uses PostgreSQL headers but no server: synthetic plans exercise the exact # production graph code and deterministic failure paths. .PHONY: unitcheck -unitcheck: test/graph_test test/inject_test +unitcheck: test/graph_test test/inject_test test/forward_walk_test ./test/graph_test ./test/inject_test + ./test/forward_walk_test test/graph_test: $(srcdir)/test/graph_test.cpp $(wildcard $(srcdir)/src/*.cpp) $(srcdir)/include/rpt.h $(srcdir)/include/rptqual.h $(CXX) $(CXXFLAGS) $(CPPFLAGS) $(PG_CPPFLAGS) $< -o $@ @@ -125,3 +126,24 @@ SURVEY_PYTHON ?= python3 .PHONY: surveycheck surveycheck: PYTHONDONTWRITEBYTECODE=1 $(SURVEY_PYTHON) $(srcdir)/test/survey_test.py + +# Link the production backend walker; unused planner code is discarded so the +# fixture needs no server. Expression-list traversal is stubbed in the test. +ifeq ($(PORTNAME),darwin) +WALK_TEST_LDFLAGS = -Wl,-dead_strip +else +WALK_TEST_LDFLAGS = -Wl,--gc-sections +endif + +test/forward_walk_backend.o: $(top_srcdir)/src/backend/optimizer/util/walkers.c + $(CC) $(CFLAGS) $(CPPFLAGS) $(PG_CPPFLAGS) -ffunction-sections -fdata-sections -c $< -o $@ + +test/forward_walk_test: $(srcdir)/test/forward_walk_test.cpp $(srcdir)/src/rptforward.cpp $(srcdir)/include/rpt.h test/forward_walk_backend.o + $(CXX) $(CXXFLAGS) $(CPPFLAGS) $(PG_CPPFLAGS) -ffunction-sections -fdata-sections \ + $(WALK_TEST_LDFLAGS) $< test/forward_walk_backend.o -o $@ + +EXTRA_CLEAN += test/forward_walk_test test/forward_walk_backend.o + +.PHONY: diagnosticcheck +diagnosticcheck: + PYTHONDONTWRITEBYTECODE=1 $(SURVEY_PYTHON) $(srcdir)/test/diagnose_forward_test.py diff --git a/gpcontrib/robust_predicate_transfer/README.md b/gpcontrib/robust_predicate_transfer/README.md index 95910f690df..e9f6b3999ee 100644 --- a/gpcontrib/robust_predicate_transfer/README.md +++ b/gpcontrib/robust_predicate_transfer/README.md @@ -1,37 +1,74 @@ -# robust_predicate_transfer - -RPT+ (Robust Predicate Transfer with dynamic execution) on top of Anser: -plans a transfer schedule for Anser's runtime bloom filters over the query's -join graph — a forward pass that chains filters table-to-table and a backward -pass that broadcasts one merged filter per equivalence class (Qiao, Boncz, -Zhang, PVLDB 19(6), 2026). - -Phase I (current) is a pure analysis pass over the finished plan: region -discovery, join graph, equivalence classes, and the asymmetric transfer plan, -ported from the paper's artifact (MIT license — see -`licenses/LICENSE-mit-duckdb-rpt.txt`). `rpt_transfer_plan(text)` dumps the -analysis for a query; `rpt.debug=on` logs it during planning. - -See `docs/design.md` for the architecture and the correctness rules. - -Build: `./configure --with-anser --with-rpt` (needs Anser), then -`make -C gpcontrib/robust_predicate_transfer`. Run tests with -`make -C gpcontrib/robust_predicate_transfer installcheck` against a demo -cluster (it preloads `anser,robust_predicate_transfer` itself). - -Run `make unitcheck` in this directory after configuring the source tree to -exercise synthetic graph, stage, co-location, consumer-order and exception -fixtures without starting a server. `make installcheck` additionally needs a -running Cloudberry cluster and executes SQL cases under both optimizers. - -Phase II runtime injection is gated by the benchmark eligibility survey in -`plans/rpt-plus-phase2-plan.md`. The stricter analysis and planning-only -`scripts/survey.py` are available; no backward executor nodes are injected yet. -See `docs/design.md` for the survey command and metrics. `make surveycheck` -checks the runner without needing a server. - -The survey requires a superuser or explicit EXECUTE on `rpt_transfer_plan(text)`. -It counts SELECT statements: JOB 113, TPC-H 22 (Q15 in CTE form), TPC-DS 103 -(99 templates, including the four two-SELECT variants). See the normalization -instructions in the design document. `rpt_backward_eligibility` supplies -production-walker/resolver regression coverage under both optimizers. +# RPT+ for Apache Cloudberry + +RPT+ (Robust Predicate Transfer with Dynamic Execution) reduces the work of +multi-table joins by transferring runtime filters between related tables. +A selective predicate on one table can reduce another table before their +join executes, and that reduction can propagate through the join graph. +The goal is to make query performance less sensitive to join order and +inaccurate cardinality estimates. + +A Bloom filter summarizes the join keys that survive filtering. A receiving +scan can discard keys that are definitely absent, while the join still +checks all remaining rows. Bloom-filter false positives can add work but do +not remove matching rows. RPT+ chooses transfer schedules and controls their +overhead; building and exchanging filters is useful only when the saved +query work exceeds that cost. + +## Why it uses Anser + +Cloudberry executes queries across multiple segments. For a global filter, +keys from every producer must be combined before a receiving scan can safely +use it. The [Anser extension](../anser/README.md) supplies this runtime +information-sharing mechanism: producers publish filter parts, the +coordinator merges them, and consumers receive the completed filter over +existing dispatch connections. Its channel identity, delivery protocol and +timeout/cancellation handling let RPT+ reuse the transport rather than build +another distributed filter service. + +RPT+ decides **which tables exchange filters and in what order**; Anser +provides **global filter exchange and scan consumers**. RPT+ uses Anser's +transport independently of Anser's per-join injection policy. Enable RPT+ +with `anser.runtime_filter=off` so only one policy injects filters. + +The forward reducer runs as an InitPlan of the same statement, under its +snapshot, before the main query starts. Intermediate co-distributed transfers +stay inside the reducer process. Filters used by the main query cross the +dispatch boundary through Anser's statement-scoped channels. Backward +transfer schedules are available for analysis; backward runtime injection +and dynamic abandonment are not implemented. + +## References and attribution + +- **RPT+ paper:** Yiming Qiao, Peter Boncz and Huanchen Zhang, + [*Robust Predicate Transfer with Dynamic Execution*](https://doi.org/10.14778/3797919.3797934), + PVLDB 19(6):1278–1290, 2026. +- **RPT+ artifact:** + [embryo-labs/dynamic-predicate-transfer](https://github.com/embryo-labs/dynamic-predicate-transfer). + The transfer-graph code in this extension is adapted from this artifact. +- **Earlier RPT implementation:** + [robust-sql/robust](https://github.com/robust-sql/robust), the baseline + implementation extended by the RPT+ work. +- **Anser paper:** Liang Lin et al., + [*Anser: Adaptive Information Sharing Framework of AnalyticDB*](https://doi.org/10.14778/3611540.3611553), + PVLDB 16(12):3636–3648, 2023. + +The adapted artifact code is MIT-licensed, Copyright 2018–2025 Stichting +DuckDB Foundation. See [NOTICE](../../NOTICE), [LICENSE](../../LICENSE) and +[the artifact license](../../licenses/LICENSE-mit-duckdb-rpt.txt). + +## Building and enabling + +Configure Cloudberry with both extensions: + +```sh +./configure --with-anser --with-rpt +``` + +Install the extensions on every host and preload +`anser,robust_predicate_transfer` in that order. Set `anser.enable=on` in the +server configuration and restart the cluster. In a database, create the +`robust_predicate_transfer` extension and set `rpt.enable=on` and +`anser.runtime_filter=off` for queries using RPT+. + +`rpt_transfer_plan(text)` reports the transfer analysis without injecting +filters into the query it describes. `rpt.debug=on` logs planning decisions. diff --git a/gpcontrib/robust_predicate_transfer/docs/design.md b/gpcontrib/robust_predicate_transfer/docs/design.md deleted file mode 100644 index 33267d65c75..00000000000 --- a/gpcontrib/robust_predicate_transfer/docs/design.md +++ /dev/null @@ -1,415 +0,0 @@ -# Robust Predicate Transfer (RPT+) over Anser — design - -The `robust_predicate_transfer` extension plans *transfer schedules* for the -bloom filters Anser exchanges: instead of one filter per hash join, the query's -join graph gets a forward pass that chains filters table-to-table and a -backward pass that broadcasts one merged filter per equivalence class — the -scheme of Qiao, Boncz and Zhang, *Robust Predicate Transfer with Dynamic -Execution* (PVLDB 19(6):1278-1290, 2026), ported from the paper's artifact -(`embryo-labs/dynamic-predicate-transfer`, MIT license, Copyright 2018-2025 -Stichting DuckDB Foundation; see `licenses/LICENSE-mit-duckdb-rpt.txt`). - -**Phase I (this code) is a pure analysis pass.** It runs over a finished -`PlannedStmt`, discovers transfer regions, builds the join graph and -equivalence classes, and computes the asymmetric transfer plan. Nothing is -injected or executed; `rpt_transfer_plan(text)` dumps the result, and the -planner hook logs the same dump at `DEBUG1` when `rpt.debug` is on. - -## Architecture at a glance - -``` -Phase II candidate — one statement, one dispatch (survey gate pending) - class-root scan: produce the global class broadcast filter - eligible global members: consume it, with bounded prefetch - local members: core runtime-filter pushdown only - -Phase III candidate — inside that same statement (not implemented) - Sequence(reducer Append, main plan); no cross-command filter cache -``` - -Phase I answers the question everything downstream depends on: given this -plan, what are the regions, the classes, the chain order, the broadcast roots, -and which consumers may safely wait on the broadcast. - -## Transfer regions - -A transfer region is a connected component of the join graph over base scans: -two scans are connected when an equijoin clause between them resolves on both -sides to a column of that scan. The resolution (rptregion.cpp) descends the -operators that pass a column through unchanged — Hash, Motion, the three join -types, Sort, Material, and Anser's own filter nodes — and terminates only at -SeqScan, IndexScan, IndexOnlyScan or BitmapHeapScan, the scans a reducer pass -could replay. Everything else is a region boundary: Agg, WindowAgg, SetOp, -RecursiveUnion, Append/MergeAppend, ProjectSet (that is what `UNNEST` becomes, -so the inject-before-or-after-unnest question is moot — filters never cross -it), SubqueryScan, CteScan, Unique, Limit, Result, DynamicSeqScan. - -Because region membership falls out of the graph, a clause whose operands do -not resolve to base scans simply produces no edge, and the scans it would have -connected land in different regions. This is a correctness gate, not a missed -optimization: a filter built on a scan that does not hold the whole key column -would drop rows that can join. - -## Join graph and equivalence classes - -Edges come from HashJoin hashclauses, MergeJoin mergeclauses, and unparameterized -col=col clauses of NestLoop joinquals. A clause qualifies only when both -sides are plain Vars of the same type, the type is by-value, is not float, and -the operator is the type's own equality (rule 6 below). Clauses of one join -over one vertex pair merge into one edge; the clause count is the edge weight. - -Union-find over the keys (rtindex, attno) gives the equivalence classes; the -ports of the artifact's union-find, `ClassifyTables` (filtered / intermediate / -unfiltered) and `SkipUnfilteredTable` (bypass unfiltered tables with direct -edges, block their outgoing edges) are in rptgraph.cpp. - -## Asymmetric transfer plan - -rptplan.cpp uses one Algorithm 1 insertion loop for both trees. Frontier -selection prefers the largest shared-column count, then the largest relation; -each stage chooses among edges open in its transfer direction; among its -highest-weight open edges, parents use greatest forward depth and smallest -backward depth, -with vertex id making remaining ties deterministic. Unprotected equivalence -classes expose their implied equalities, so a SQL star and a SQL chain can -produce the same chain within a class. Root selection retains the artifact's -largest filtered-or-intermediate preference, falling back to the largest table. - -The backward stage redirects each class to its earliest-inserted member. A -non-component-root class leader is a **bridge table**, reported in the dump. -Only filtered or bridge tables can produce forward filters. Component roots -are final destinations in the forward graph, but never reducer scans: they -would consume their inbound chain filter in the main query in Phase III. `reducer order` lists -only scans that actually produce forward filters, in dependency order. - -`rpt.max_chain_depth` limits the number of dependency hops, including the -last delivery to the root. When another hop would exceed the cap, that edge -is omitted from both endpoint lists. This loses selectivity, not correctness. -The dump records the cap, reducer order, bridges, and each forward and -backward hop's merge mode. The default (4) remains provisional pending measured transport latency. - -### C/C++ errors and diagnostic access - -STL containers allocate outside PostgreSQL memory contexts. Calls that can -raise PostgreSQL errors go through `RptPgCall`, which catches the longjmp at -the call site and throws a C++ marker. C++ unwinding destroys the containers -before the analysis boundary rethrows the original PostgreSQL error. The -wrapper restores both PostgreSQL exception stacks on either kind of unwind. -C++ allocation and other exceptions are converted to PostgreSQL errors only -after leaving the C++ catch blocks. Both the analysis and SQL entry points -contain C++ exceptions. - -The dump function plans without executing the query's executor permission -checks. Its default PUBLIC execution privilege is revoked; administrators -should grant it only to roles allowed to inspect relation estimates. - -### Scope and validation still outstanding - -This implementation computes schedules and dumps them; it does not inject -filters, dispatch reducers, or add EXPLAIN annotations. No runtime behavior -or performance improvement is claimed. The Phase I latency benchmark and -JOB/TPC-DS co-location coverage report have not been run. Multi-class runtime -backward chaining remains deferred to later phases. Cloudberry MARK-like -subplan forms are not DuckDB MARK joins: unsupported join kinds contribute -no transfer edge. SQL tests run each case with optimizer off and on; -standalone synthetic tests pin graph shape and error cleanup independently -of SQL optimizer transformations. - -## Correctness rules - -Each of these is a silent-wrong-answer rule, not a performance rule. A bloom -filter may only ever *over*-approximate the build side's key set. - -1. **Merge where the source keys are complete.** Decide each forward and - backward edge from the base relations' distribution policies and that - edge's key pairs. Equal ordered hash distribution columns, hash opclasses - and segment counts permit a segment-local transfer when every distribution - column is covered by the transfer equality. Main-plan Motions do not change - this proof: the reducer reads the base scans. Implied equivalence-class - and unfiltered-bypass edges use exactly the same policy test as original - join edges, so a co-distributed star remains local after becoming a chain. - A replicated source is complete on every covered segment: it may transfer - locally if its segment count covers the destination's. A replicated scan - may execute on only one segment: Phase II must place the producer on every - consuming segment or classify that broadcast as global; policy coverage - alone does not guarantee producer execution coverage. A transfer into a - replicated destination stays global, including replicated-to-replicated. - Other cases, including unknown policies and random-to-hash transfers, need - global publish → merge → deliver. A local hop never publishes to the QD. -2. **Snapshot identity.** The reducer (Phase III) must run under the main - query's snapshot and parameters: a row visible to the query but not to the - reducer would be silently missing from the filter. -3. **Outer joins are directed edges.** LEFT protects the left side, RIGHT and - MARK protect the right in the artifact; unsupported Cloudberry join kinds, - ANTI and FULL produce no edge. The preserved side - of an outer join must never be reduced by the nullable side's keys. -4. **Transitive reduction is sound by induction.** Filtering table *t* with - filter A before building filter B from *t* is correct because A only - removes rows that provably cannot join. This licenses the forward chain. -5. **Cycles cost completeness, not correctness.** The spanning-forest loop - runs on cyclic queries, losing the full-reduction guarantee but never - correctness (the RPT paper's position, which we follow). -6. **Key equality must be bitwise Datum equality.** Anser hashes raw Datum - bytes, so a clause qualifies only when both sides share a by-value, - non-float type and the operator is that type's own equality - (`-0.0 = 0.0` is true but not bitwise equal; cross-type Datums for equal - values are not bitwise equal either). -7. **Blocking consumers only across slices.** `consumer_ok` describes - eligibility for a blocking Anser consumer on **global** backward edges. - Every global producer must be on a hash join's build side, the consumer - on its probe side, and the scans in different slices; different ancestors - can prove different producers. Same-slice exchanges are refused, matching - Anser. Local edges never participate in this check, and a vertex with only - local in-edges reports `consumer_ok no`: it needs no blocking consumer. - Core's `gp_enable_runtime_filter_pushdown` covers eligible same-process - HashJoin/SeqScan pairs after building the hash table. Phase II should rely - on that path for local backward edges, or use a non-blocking local filter - whose absence means no filtering. A HashJoin can fetch its first probe - tuple before starting the build; waiting there can deadlock even if the - producer is on its build side. A local edge must never become an Anser - exchange merely because its consumer passes the structural hash-join test. -8. **Fail open, always.** Timeout, malformed part, oversized filter, squelched - producer, missing cache entry — all degrade to "no filter", never to an - error and never to a filtered result. The optional planner-hook analysis - runs in a disposable memory context: analysis failures warn and return the - original finished plan. User cancellation still propagates. The explicit - diagnostic UDF reports errors to its caller rather than masking them. - -## Runtime contract with Anser (Phases II/III) - -- Phase II has no reducer: its root builds from its own predicates only, - inside the main statement. Runtime injection is gated by the survey below. -- Anser transport is reused, while its per-join injection is disabled. Plans - carrying Anser per-join nodes are left untouched rather than mixed. -- Global backward edges may use Anser only where strict eligibility permits - it. Local members use core runtime-filter pushdown; RPT injects no local - consumers, QD channels or blocking waits for them. -- Channels are statement-scoped. The broadcast is delivered once per - consuming QE; fan-out is capped per class. There is no session filter cache. -- The forward reducer (Phase III, below) runs inside the same statement, - as an uncorrelated InitPlan. Cloudberry dispatches it before the main plan, - with the same snapshot and `gp_command_count`, so both dispatches share one - Anser channel namespace. Channels live until `ExecutorEnd`. - -### Parallel workers and segment completeness - -`merge=local` is a distribution-policy decision, not permission to use a -single parallel worker's partial filter. With parallel scans, each QE worker -reads only part of its segment's keys. Even a replicated source then needs -all workers' parts combined before its filter is complete on that segment. - -Phase III's initial reducer must be **non-parallel**, with one scanning QE -per segment. Supporting parallel reducers requires a segment-local merge of -all worker filters before delivery or probing. Anser's `docs/local-fold.md` -describes such a rendezvous, but it is an idea, not an implemented facility; -Phase III must not assume it exists. Missing or failed worker parts must -abandon the filter and fail open, never expose an incomplete filter. The -same completeness requirement applies to any custom local backward producer. -Core currently disables its pushdown path in MPP parallel mode, so relying -on core there simply means no local filter, rather than a global exchange. - -## Phase II eligibility survey gate (in progress) - -The agreed next phase is backward broadcast inside the main statement. It -uses no session-scoped cache and injects no local filters: local edges stay -with core pushdown. Anser transport must be enabled and Anser's per-join -injection disabled; the two injection strategies are alternatives. A class -led by a bridge is deferred. Runtime injection remains **unimplemented** -until Work item 0 of `plans/rpt-plus-phase2-plan.md` has been completed and -its result justifies proceeding. - -The analysis now records a decision for each class/member pair. In addition -to different slices and a producer on a hash-build side, it rejects -multi-tuple prefetch paths and nullable-side crossings. If the qualifying -join builds first (`prefetch_inner` or RIGHT), the prefetch blocker is lifted; -the shared Anser key resolver must still accept the path. This conservatively -rejects unsupported resolver shapes even when scheduling alone is safe. -Every placement under `stmt->subplans` is refused, even below a Motion and -even for an uncorrelated InitPlan, matching Anser's conservative scope. A -non-empty build-side `extParam` is also refused regardless of the parameter -source; publication has no rescan generation protocol yet. Successful decisions record -parents, output-tuple key positions, slice IDs and `defer_first`; physical -column numbers are never assumed to be tuple positions. - -`consumer_ok` is an aggregate over all non-local decisions for a vertex. -Per-edge lines report refusals separately, including local edges, bridge-led -classes, unsupported key paths and the fan-out cap. The `survey` line counts -classes, component-root-led classes, global class/member pairs, structural -rule-9 passes, strict passes before the cap, and eligible consumers after it. -NDV estimates come from `pg_statistic`, with negative fractions scaled by -`pg_class.reltuples`. Their intersection estimate assumes uniform key overlap, -caps root NDV by estimated filtered root rows, and reports missing statistics -as unknown. Estimated removed rows use consumer scan-plan-row units, not a -cluster-total or a correctness claim. A table in multiple classes can be -counted more than once. Unknown-benefit consumers are ranked by their scan -row count as an upper-bound fallback when applying the per-class cap. - -Run the planning-only survey after installing this analysis build and loading -benchmark schemas, data and statistics. `rpt_transfer_plan(text)` has EXECUTE -revoked from PUBLIC, so connect as a superuser or a role explicitly granted -EXECUTE on that function (and SELECT on the benchmark tables). For example, -an administrator can grant `EXECUTE ON FUNCTION public.rpt_transfer_plan(text) -TO rpt_survey`; adjust the function schema if installed elsewhere: - -```sh -python3 scripts/survey.py \ - --job /bench/job/queries --job-dbname imdb \ - --tpch /bench/tpch/queries --tpch-dbname tpch \ - --tpcds /bench/tpcds/queries --tpcds-dbname tpcds \ - --output /tmp/rpt-phase2-survey -``` - -Normalize the benchmark input before running the gate: - -- Use instantiated SQL with the benchmark's original parameters, not templates. -- TPC-DS templates 14, 23, 24 and 39 each contain two SELECT statements. The - runner splits them automatically and records `filename#1` / `filename#2`; - alternatively supply separate `14a.sql` / `14b.sql`, etc. Both layouts - represent **103 SELECT statements from 99 templates**. -- Rewrite TPC-H Q15's `CREATE VIEW revenue... AS ...; SELECT ...; DROP VIEW ...` - as `WITH revenue... AS (...) SELECT ...;`, preserving its view column names, - SELECT body and date parameters. The runner rejects view DDL instead of - executing it or silently excluding Q15. The normalized suite has 22 SELECTs. -- Statement splitting respects quoted strings, identifiers, dollar quotes - and nested comments. The server still parses and validates each statement. - -The tool uses read-only transactions, `rpt.enable=off`, `rpt.backward=off`, -and EXPLAIN without ANALYZE to compare RPT eligibility with Anser's actual -producer-node count. It records per-statement JSON/CSV, source file and -statement ordinal, raw dumps/JSON plans, optimizer identity and suite -summaries. Failures stay in the denominator. Completion is counted by -statements, never by files: 113 JOB, 22 TPC-H and 103 TPC-DS, each under both -optimizer settings. `--allow-partial` is a pilot and cannot satisfy the gate. - -| Suite | SELECT statements | Optimizers | Eligibility vs. Anser | -| --- | --- | --- | --- | -| JOB | 113 | off/on | Not run: benchmark cluster and query set not supplied | -| TPC-H | 22 (Q15 CTE form) | off/on | Not run: benchmark cluster and query set not supplied | -| TPC-DS | 103 (99 templates) | off/on | Not run: benchmark cluster and query set not supplied | - -`rpt_backward_eligibility` is a server regression under both optimizer -settings, using the production plan walker and Anser resolver. It asserts -qualification, local/same-slice and root-on-probe refusals, bridge-led classes, -cap enforcement, parameterized builds and a correlated SubPlan containing a -Motion. Sort and nullable-side expectations follow the actual retained plan -shape: removing those operators is a legitimate optimizer transformation. -Aggregate/window/set-operation boundaries also have server coverage in -`rpt_regions`. Native fixtures additionally cover the individual prefetch -blocker tags and defensive states that valid SQL cannot reliably force. -The SQL suite has been added but has not been executed on a local cluster. - -No eligibility percentages or go/no-go conclusion are available yet. This -section must be updated with measured results before adding runtime nodes. -Phase III's reducer likewise lives inside the same statement; its only state -crossing the InitPlan/main dispatch boundary is the statement's Anser channels. - -## Phase III forward reducer (implemented, not yet run on a cluster) - -Injection happens in the planner hook, on the coordinator, for top-level -`SELECT`s without `INTO`/`COPY`/`REFRESH` targets. It requires `rpt.enable`, -`rpt.forward`, `anser.enable=on` and `anser.runtime_filter=off`, and leaves -plans that already carry Anser nodes alone. Any construction error warns and -keeps the original plan; cancellation propagates. - -Selection (`rptforwardplan.cpp`, unit-tested) works per forward root, which is -the largest relation of its component and may differ from the backward -(Phase II) root: - -1. The chain is `reducer_order` restricted to vertices whose filters reach the - root, root excluded; producers come before consumers. -2. Each chain vertex's forward out-edges become channels, keyed by the first - key pair. Filters are sized by `AnserRuntimeFilterSize` from the source's - estimated rows. - A filter is sized for the rows its reducer scan inserts: the table's - rows per segment (`reltuples`) times the fraction its own reducer quals - keep -- all rows when those quals were dropped. -3. Exchange cost: `c × n_producers × filter_MiB` per channel, with - `c = rpt.forward_ms_per_mib_part` and `n_producers` the number of - primaries; a chain's cost is the sum of its channels (hops run serially). - An in-process hop (below) costs nothing. -4. Benefit: the fraction of root rows the chain is expected to keep. A - vertex keeps what its own reducer quals keep (`plan_rows / reltuples` per - segment; 1 when dropped or unknown) times what its inbound filters keep; a - filter from `u` keeps `keep(u) × min(1, ndv(u.key) / ndv(v.key))` of `v`, - assuming the smaller key domain is contained in the larger. NDVs come - from `pg_statistic`; unknown NDV means `keep(u)`. -5. Gates, in order: no forward edges; root rows below - `rpt.forward_min_root_rows`; expected root selectivity not below - `rpt.forward_max_selectivity` (the RPT+ paper's λ = 0.35); estimated cost - above `rpt.forward_max_exchange_ms`. - -Construction (`rptforward.cpp`): every chain table must be a plain Seq Scan in -the main plan, and the root must be one whose output carries its key. For each -chain vertex, the reducer gets a key-only copy of its scan with its own quals, -wrapped in consumers for its incoming channels and then producers for its -outgoing ones. - -Rule 13 for the reducer: the quals are copied whole only when every one is -provably unable to fail -- built from Vars, Consts, AND/OR/NOT, NULL and -boolean tests, binary-compatible relabels, and an explicit allowlist of -built-in integer, OID and boolean comparisons. Hashed array operations must -also use allowlisted hash and negator functions. LEAKPROOF alone is not a -proof of error-free evaluation: a leakproof user function can fail for reasons -unrelated to its arguments. No arbitrary functions, arithmetic, coercions, -Params or SubPlans are accepted. Otherwise all are -dropped. The reducer runs eagerly, before the main plan and over rows the -main plan may never read (`LIMIT 0`, an empty outer side): copying -`100 / a > 1` would turn a successful query into a division-by-zero error -that no planner fail-open can catch. Dropping quals keeps the reducer's rows a -superset, and the selectivity estimate and filter sizing see the drop. - -In-process hops (rule 16, `rptlocal.c`): a local hop (`rpt_local_hop`: the -destination's rows that can match live on the producer's segment) whose -filter is consumed only inside the reducer is handed over without Anser. -`RPT Local Bloom Producer` builds a `lib/bloomfilter.h` filter while passing -rows through and, when its child is exhausted, leaves it in a per-execution -registry (keyed by EState and condition, unregistered by a reset callback on -the query context); the `RPT Local Bloom Consumer` of a later Sequence child -probes it. A consumer that finds no completed filter passes everything. A -filter the main plan also consumes -- every hop into the root, and every hop -under `rpt.forward_apply=all` -- is published through Anser instead. The main plan's root scan (and with -`rpt.forward_apply=all`, every chain scan) is wrapped in consumers. The -children are attached with `RptAttachReducerInitPlan`: one EXISTS InitPlan, -Gather Motion over a Sequence, so its dispatch completes before the main one. - -Connection lifetime: the reducer's dispatcher releases its gangs in -`ExecSetParamPlan`, long before `ExecutorEnd`, and every dispatcher does so -early on the error path. Anser keeps `PGconn` pointers for deliveries and for -partially flushed chunks, and `cleanupQE` destroys connections with unflushed -output. Before retaining a connection pointer, Anser registers a libpq event -handler. `PGEVT_CONNDESTROY` forgets all of that connection's deliveries and -queued-chunk references before `PQfinish` frees it, including on InitPlan -and error cleanup paths. `PGEVT_CONNRESET` drops state for the previous stream. -Cloudberry hides public PQ event APIs in the backend, so Anser installs its -entry using the existing libpq-int.h event layout and libc allocator; it does -not link a second libpq or require new exported core symbols. -The callbacks do not allocate, log, wait, or scan dynahash. Failed registration -refuses the subscription, so the consumer times out unfiltered. A recycled -connection that stays allocated remains safe to reference; completed channel -payloads stay available to the main dispatch. No core hook is required. - -Known limits of this version: only Seq Scan targets; a single key per channel; -the selectivity estimate assumes independence and key containment; the cost -model was calibrated on one host with three primaries. `rpt.debug` dumps and -`rpt_transfer_plan()` report the selection with `decision=` reasons, the -estimated selectivity (`est_sel=`), in-process hops and dropped quals. - -## GUCs - -| GUC | Default | Meaning | -| --- | --- | --- | -| `rpt.enable` | off | Master switch for the analysis hook. | -| `rpt.debug` | off | Log the transfer plan of every analyzed query at DEBUG1. | -| `rpt.backward` | on | QD-only runtime switch reserved for Phase II; disable during the survey. | -| `rpt.max_backward_consumers` | 4 | QD-only cap on consumers per class, applied to analysis decisions. | -| `rpt.max_chain_depth` | 4 | Enforced maximum dependency hops; default provisional until hop latency is measured. | -| `rpt.forward` | on | Build the Phase III forward reducer when `rpt.enable` is on. | -| `rpt.forward_apply` | root | Where the main plan consumes forward filters: `root` only, or `all` chain scans too. | -| `rpt.forward_ms_per_mib_part` | 3.5 | Cost model `c`, ms per MiB of filter per producer part. | -| `rpt.forward_max_exchange_ms` | 100 | Refuse a chain whose estimated exchange exceeds this; −1 disables the gate. | -| `rpt.forward_min_root_rows` | 10000 | Refuse a chain whose root is estimated below this many rows per segment. | -| `rpt.forward_max_selectivity` | 0.35 | λ: refuse a chain expected to keep at least this fraction of the root's rows; 1 disables. | - -Setup: `shared_preload_libraries = 'anser,robust_predicate_transfer'`, in that -order (the extension resolves Anser symbols at load time). The analysis UDF -needs `CREATE EXTENSION robust_predicate_transfer`. diff --git a/gpcontrib/robust_predicate_transfer/docs/phase3-experiments.md b/gpcontrib/robust_predicate_transfer/docs/phase3-experiments.md deleted file mode 100644 index 2d003ca199b..00000000000 --- a/gpcontrib/robust_predicate_transfer/docs/phase3-experiments.md +++ /dev/null @@ -1,124 +0,0 @@ -# Phase III experiment plan - -Goal: find out whether the forward reducer is correct on a real cluster, what -it costs, where it pays off, and whether `c × n_producers × filter_MiB` is a -good enough gate. Experiments are ordered; each states what blocks the next. - -Common setup for every run: - -- `shared_preload_libraries = 'anser,robust_predicate_transfer'`; restart the - cluster after installing new binaries on every host. -- Record: commit hash, number of hosts and primaries, interconnect type, - `gp_vmem`/`work_mem`, both `optimizer` settings. -- Per query, keep the `rpt_transfer_plan()` dump, `EXPLAIN (ANALYZE, FORMAT - JSON)` and wall time. Reject any run with an "RPT forward reducer skipped" - WARNING in the log, unless the experiment is about that warning. -- Timings: 1 warm-up, then 10 runs; report median and p95. - -## E0. Correctness gate (blocks everything) - -1. `make installcheck` in `gpcontrib/anser` and in - `gpcontrib/robust_predicate_transfer`, which now includes `rpt_forward` - and `rpt_forward_faults` (the latter needs a build with fault injection). -2. `make installcheck` (core) with `rpt.enable=on`, `rpt.forward=on`, - `rpt.forward_min_root_rows=0` and `rpt.forward_max_exchange_ms=-1` set in - `PGOPTIONS`. Failures count only when they disappear with - `rpt.enable=off`. This is the broad crash and wrong-result sweep. -3. The same with `rpt.forward_apply=all`. -4. Cancel and error paths: `pg_cancel_backend` during the reducer; a segment - `gp_inject_fault` error inside an Anser producer; `statement_timeout` - shorter than the reducer. Expect a clean error, no hung QEs and no leaked - channels on the next statement. - -Pass: zero result differences. Any wrong result stops the plan. - -## E1. Cross-dispatch proof - -On the `rpt_forward` star fixture, under both optimizers: in the main plan, -`Rows Removed by Bloom Filter` plus `Rows Removed by Pushdown Runtime Filter` -is greater than 0. Also check with `gp_log_interconnect`/Anser tracing that the -producer channels are created in the InitPlan's dispatch and consumed in the -main dispatch, with the same `gp_command_count`. - -## E2. Fixed overhead of the InitPlan - -`rpt.debug_noop_initplan` with `scripts/measure_initplan.py` -(see `phase3-measurements.md`). Measure on 3, 8 and 24 primaries, and on 1 vs -several hosts. Output: the extra dispatch latency per statement. This is the -floor any accepted chain must beat; it should become a constant term in the -gate if it exceeds a few milliseconds. - -## E3. Calibrating c - -Single-channel star (one dimension, filter sized to 1, 8 and 64 MiB) with -forward-only RPT. For each topology (3/8/24 primaries, 1 and 2+ hosts): - -- measured exchange = reducer InitPlan time − reducer scan time; -- fit `measured = a + c × n_producers × MiB`. - -Pass: the fit is within 25% of measured times, and `c` stays within 2× of 3.5 -across topologies. Otherwise, make `c` per topology or change the model, for -example `c₁ × n_producers × MiB + c₂ × n_consumers × MiB` for delivery. - -Small filters (64–512 KiB) need Anser's 1 MiB floor lowered. That requires a -debug-only GUC in Anser first; it is optional and should run only if E4 shows -many small filters. - -## E4. Star benchmarks: where it pays off - -Fixtures: 1 fact table (10M, 100M rows), 1–4 dimensions, selectivity of each -dimension 0.1%, 1%, 10%, 50%, 100%. Four configurations: - -| Config | Settings | -| --- | --- | -| baseline | `anser.enable=off` | -| Anser RF | `anser.enable=on`, `anser.runtime_filter=on` | -| RPT root | `rpt.enable=on`, `anser.runtime_filter=off`, `rpt.forward_apply=root` | -| RPT all | as above, `rpt.forward_apply=all` | - -Run first with the gates open (`min_root_rows=0`, `max_exchange_ms=-1`, -`max_selectivity=1`) to measure real gains and losses, then with default -gates to see what the gates keep. Record `est_sel` from the dump next to the -measured fraction of root rows removed, to check the selectivity estimate. Output per query: speedup against baseline and against Anser RF, plus the -gate decision. Expected: wins at low selectivity and large facts; losses at -50–100% selectivity, which the gates should refuse. - -## E5. Chains and snowflakes - -Snowflake schemas (fact → dim → subdim, depth 2–4) and two-fact queries, -where transitive transfer is the point of RPT+. Compare RPT root/all with -Anser RF. This checks that serial hops add up as the model assumes (sum of -channels), and gives data for `rpt.max_chain_depth`. - -## E6. Workloads - -JOB (IMDB), TPC-H SF100 and TPC-DS SF100, under both optimizers: - -1. Survey: for every query, the dump's forward decision and refusal reason - (e.g. "no forward edges", gates, non-Seq-Scan targets in preflight). -2. Benchmark: baseline / Anser RF / RPT root, using default gates. -3. Report regressions per query (> 5% slower than the better of baseline and - Anser RF), each with its dump. These decide the default gate values. - -## E7. Gate tuning - -From E3–E6, choose defaults for `rpt.forward_ms_per_mib_part`, -`rpt.forward_max_exchange_ms`, `rpt.forward_min_root_rows` and -`rpt.forward_max_selectivity` (λ = 0.35 is the paper's value, not ours). A stronger gate -would compare the cost to estimated savings (removed root rows × per-row cost -above the root). Add that only if E6 shows cost-only gating either refuses -winners or admits losers. - -## Open items found while implementing - -- In-process hops are implemented for local hops consumed only inside the - reducer. E5 should compare a colocated snowflake with a random one to - measure what the in-process path saves. -- Reducer quals that are not provably error-free (non-leakproof functions, - I/O casts) are dropped whole. E6's survey should count how often that - happens; if it is frequent, keeping the provably safe subset is a - follow-up. -- Only Seq Scan targets, one key per channel. E6's survey shows how many - queries this excludes. -- Publish-start timestamp in Anser: needed for exact exchange timing in E3. - The current proxy starts at serialization. diff --git a/gpcontrib/robust_predicate_transfer/docs/phase3-measurements.md b/gpcontrib/robust_predicate_transfer/docs/phase3-measurements.md deleted file mode 100644 index 182a45dd4aa..00000000000 --- a/gpcontrib/robust_predicate_transfer/docs/phase3-measurements.md +++ /dev/null @@ -1,87 +0,0 @@ -# Phase III measurement status - -## Exchange measurements, 2026-11-10 - -The supplied `anser-20261110` results cover three primaries on one host. -Eight- and 24-primary topologies were unavailable. The fixture was one -cross-Motion Anser channel under GPORCA, with 10,000 build keys and a -1,000,000-row probe. All 90 measured exchanges completed with three producer -parts, one complete merge and three complete consumers; traced query results -were 10,000. Anser regressions passed 4/4. - -| Bitset | Exchange proxy median / p95, ms | Delivery tail median / p95, ms | Untraced execution median off / on, ms | -| --- | --- | --- | --- | -| 1 MiB | 11.188 / 17.082 | 1.197 / 1.711 | 89.382 / 33.613 | -| 8 MiB | 82.946 / 87.325 | 6.223 / 7.770 | 87.834 / 121.425 | -| 64 MiB | 765.530 / 789.889 | 48.387 / 58.273 | 89.446 / 880.313 | - -The proxy begins at the trace immediately before serialization, not an exact -publish-start timestamp. Trace timings include logging overhead. Off/on -execution differences combine construction, transport and filtering; they -are not network timings. The sample CSV medians and nearest-rank p95 values -were checked against the supplied summary. These observations do not establish -multi-host scaling or Phase III performance. - -Payload size must participate in the forward cost estimate. A depth cap alone -cannot distinguish an approximately 11 ms exchange from a 766 ms exchange. -Keep the current chain-depth default explicitly provisional. Dispatch cost, -reducer scan cost and representative root-scan savings remain unmeasured. - -RPT regressions passed 2/3. The nullable-side fixture in -`rpt_backward_eligibility`, with optimizer off, expected an eligible producer -but received `producer is not on a qualifying hash build side`. The supplied physical -EXPLAIN confirms the LEFT join simplifies to INNER, but the root is the -upper hash join's probe and the outer/member subtree is its build. Rejection -is correct. The regression now checks the named scans' physical build/probe -placement before expecting eligibility; this test correction still needs a -cluster rerun. - -## Exact no-op InitPlan harness - -`rpt.debug_noop_initplan` is a superuser-only, QD-only diagnostic switch, -default off and independent of `rpt.enable`. It clones the finished statement -and appends the production builder's empty reducer: - -```text -InitPlan (EXISTS, unused boolean PARAM_EXEC) - Gather Motion - Sequence - Result (One-Time Filter: false) -``` - -Its reader gang covers every primary segment, with no parallel workers or -direct dispatch. It builds no filters and tests no cross-dispatch channels. -Construction errors warn and retain the original plan; cancellation propagates. -**Reject measurements with a skipped-InitPlan warning or missing plan shape.** - -After rebuilding and installing the extension on all hosts, restart the test -cluster so all backends use the same binary. Use a fresh superuser connection -and an idle cluster, with the existing Anser/RPT preload order. From the -repository root: - -```sh -python3 gpcontrib/robust_predicate_transfer/scripts/measure_initplan.py \ - > /tmp/rpt-initplan.sql -psql -X -v ON_ERROR_STOP=1 -d rpt_exchange_bench \ - -f /tmp/rpt-initplan.sql > /tmp/rpt-initplan.out 2>&1 -``` - -The script uses a temporary, randomly distributed 3,000-row table. It emits a -shape EXPLAIN and a query with an existing scalar subquery for both optimizers, -then 10 warmups and 100 measured pairs per optimizer. The existing-subquery -query must return 3,000. Each pair runs the same count with the diagnostic -switch off and on; first-arm order alternates. Runtime filters and tracing -are disabled. `SAMPLE initplan ` labels -precede each measured EXPLAIN ANALYZE. - -Check that both shape plans contain the new InitPlan, Gather, Sequence and -false Result. Record planning time, execution time and psql elapsed time for -each labeled EXPLAIN ANALYZE; ignore times printed for SET commands. Client -elapsed time matters because distributed InitPlans run during ExecutorStart, -not necessarily inside the reported main-plan execution interval. - -Report median and p95 of each arm, plus the median and p95 of paired on-minus-off -client elapsed times, separately by optimizer. Preserve raw output and relevant -QD/QE logs. The harness has passed native layout tests and C++ syntax checks; -cluster execution remains unvalidated. This measurement is the next executor -smoke test, not proof of filter delivery between dispatches. diff --git a/gpcontrib/robust_predicate_transfer/expected/rpt_forward.out b/gpcontrib/robust_predicate_transfer/expected/rpt_forward.out index 18516893a34..83d31b206ac 100644 --- a/gpcontrib/robust_predicate_transfer/expected/rpt_forward.out +++ b/gpcontrib/robust_predicate_transfer/expected/rpt_forward.out @@ -17,7 +17,7 @@ -- specific language governing permissions and limitations -- under the License. -- --- Phase III forward reducer, end to end: the InitPlan is built, its filters +-- Forward reducer, end to end: the InitPlan is built, its filters -- reach the main plan across the dispatch boundary, intermediate hops work -- both through Anser and in-process, and results never change. Both -- optimizers; every check raises with the plan or dump that failed it. @@ -60,13 +60,13 @@ BEGIN RAISE EXCEPTION 'constant failure'; END $$; -CREATE FUNCTION pg_temp.rpt_fwd_plan(q text, analyze boolean) RETURNS jsonb +CREATE FUNCTION pg_temp.rpt_fwd_plan(q text, do_analyze boolean) RETURNS jsonb LANGUAGE plpgsql AS $$ DECLARE plan jsonb; BEGIN EXECUTE format('EXPLAIN (%s FORMAT JSON) %s', - CASE WHEN analyze THEN 'ANALYZE,' ELSE '' END, q) INTO plan; + CASE WHEN do_analyze THEN 'ANALYZE,' ELSE '' END, q) INTO plan; RETURN plan; END $$; @@ -202,6 +202,11 @@ BEGIN flake_local, flake_global, 'SELECT f.id, f.v FROM rpt_fwd_fact f JOIN rpt_fwd_d1 d1 ON f.d1 = d1.k WHERE d1.a < 3', + -- Existing scalar InitPlans and correlated expression SubPlans + -- must be walked with the statement's subplan lookup context. + 'SELECT count(*) FROM rpt_fwd_fact f JOIN rpt_fwd_d1 d1 ON f.d1 = d1.k WHERE d1.a < 50 AND f.v <= (SELECT max(v) FROM rpt_fwd_fact)', + 'SELECT f.id, (SELECT max(b) FROM rpt_fwd_d2 d2 WHERE d2.k = f.d2) FROM rpt_fwd_fact f JOIN rpt_fwd_d1 d1 ON f.d1 = d1.k WHERE d1.a < 50 AND f.id < 10', + 'SELECT count(*) FROM rpt_fwd_fact f JOIN rpt_fwd_d1 d1 ON f.d1 = d1.k WHERE d1.a < 0', 'SELECT count(*) FROM rpt_fwd_fact f JOIN rpt_fwd_d1 d1 ON f.d1 = d1.k WHERE d1.a IN (0,1,2,3,4,5,6,7,8,9)', 'SELECT count(*) FROM rpt_fwd_fact f JOIN rpt_fwd_d1 d1 ON f.d1 = d1.k WHERE d1.a NOT IN (0,1,2,3,4,5,6,7,8,9)', @@ -252,7 +257,7 @@ BEGIN RAISE EXCEPTION 'selectivity gate did not refuse (optimizer=%)', opt; END IF; - -- Rule 13: a qual that can fail is never run by the reducer. The + -- A qual that can fail is never run by the reducer. The -- reducer runs before the main plan and over rows the main plan may -- never read; with LIMIT 0 the main plan reads none, so a reducer -- evaluating 100 / a would raise division by zero (a = 0 exists), and @@ -304,7 +309,9 @@ BEGIN END LOOP; END $$; --- PL/pgSQL (SPI) runs the reducer like any other caller. +-- PL/pgSQL (SPI) runs the reducer like any other caller. Its static +-- statements keep the plan of their first call, so the function runs once, +-- with RPT on, and the baseline comes from uncached dynamic SQL. CREATE FUNCTION pg_temp.rpt_fwd_spi() RETURNS bigint LANGUAGE plpgsql AS $$ DECLARE total bigint := 0; @@ -330,7 +337,10 @@ BEGIN PERFORM set_config('rpt.enable', 'on', true); with_rpt := pg_temp.rpt_fwd_spi(); PERFORM set_config('rpt.enable', 'off', true); - without := pg_temp.rpt_fwd_spi(); + EXECUTE 'SELECT (SELECT sum(f.v) FROM rpt_fwd_fact f JOIN rpt_fwd_d1 d1 ON f.d1 = d1.k + WHERE d1.a < 50) + + (SELECT sum(f.v) FROM rpt_fwd_fact f JOIN rpt_fwd_dl d ON f.d1 = d.k + JOIN rpt_fwd_sub u ON d.s = u.k WHERE u.c < 5)' INTO without; IF with_rpt IS DISTINCT FROM without THEN RAISE EXCEPTION 'SPI result differs: % vs %', with_rpt, without; END IF; diff --git a/gpcontrib/robust_predicate_transfer/expected/rpt_forward_faults.out b/gpcontrib/robust_predicate_transfer/expected/rpt_forward_faults.out index 204de98bc23..2a826f2f3b1 100644 --- a/gpcontrib/robust_predicate_transfer/expected/rpt_forward_faults.out +++ b/gpcontrib/robust_predicate_transfer/expected/rpt_forward_faults.out @@ -17,7 +17,7 @@ -- specific language governing permissions and limitations -- under the License. -- --- Phase III forward reducer under forced transport failures. Kept apart from +-- Forward reducer under forced transport failures. Kept apart from -- rpt_forward because gp_inject_fault exists only with the debug extensions, -- and the regression driver skips any file that calls it. -- @@ -47,18 +47,28 @@ INSERT INTO rpt_fwf_sub SELECT i, i FROM generate_series(0, 99) i; ANALYZE rpt_fwf_fact; ANALYZE rpt_fwf_dim; ANALYZE rpt_fwf_sub; +-- Dynamic SQL: a static statement would keep the plan of its first call, +-- with or without the reducer, whatever rpt.enable says later. CREATE FUNCTION pg_temp.rpt_fwf_run(rpt boolean) RETURNS text LANGUAGE plpgsql AS $$ DECLARE r text; BEGIN PERFORM set_config('rpt.enable', CASE WHEN rpt THEN 'on' ELSE 'off' END, true); - SELECT count(*)::text || '/' || sum(f.v)::text INTO r - FROM rpt_fwf_fact f JOIN rpt_fwf_dim d ON f.d = d.k - JOIN rpt_fwf_sub u ON d.s = u.k WHERE u.c < 5; + EXECUTE 'SELECT count(*)::text || ''/'' || sum(f.v)::text + FROM rpt_fwf_fact f JOIN rpt_fwf_dim d ON f.d = d.k + JOIN rpt_fwf_sub u ON d.s = u.k WHERE u.c < 5' INTO r; PERFORM set_config('rpt.enable', 'on', true); RETURN r; END $$; +-- How often a fault fired, so a case cannot pass without exercising it. +CREATE FUNCTION pg_temp.rpt_fwf_hits(fault text, coordinator boolean) +RETURNS int LANGUAGE sql AS $$ + SELECT coalesce(sum(substring(gp_inject_fault(fault, 'status', dbid) + FROM 'num times hit:''([0-9]+)''')::int), 0)::int + FROM gp_segment_configuration + WHERE role = 'p' AND (content = -1) = coordinator +$$; -- First occurrence only; on the coordinator a 'sleep' fault sleeps 1 s. CREATE FUNCTION pg_temp.rpt_fwf_fault(fault text, action text, coordinator boolean) RETURNS void LANGUAGE plpgsql AS $$ @@ -92,6 +102,9 @@ BEGIN PERFORM set_config('anser.timeout_ms', '100', true); PERFORM pg_temp.rpt_fwf_fault('anser_before_deliver', 'sleep', true); got := pg_temp.rpt_fwf_run(true); + IF pg_temp.rpt_fwf_hits('anser_before_deliver', true) = 0 THEN + RAISE EXCEPTION 'held delivery: fault never fired'; + END IF; PERFORM pg_temp.rpt_fwf_fault('anser_before_deliver', 'reset', true); IF got IS DISTINCT FROM expected THEN RAISE EXCEPTION 'held delivery: % <> %', got, expected; @@ -109,6 +122,9 @@ BEGIN PERFORM set_config('anser.debug_min_filter_bytes', '67108864', true); PERFORM pg_temp.rpt_fwf_fault('anser_after_chunk', 'skip', false); got := pg_temp.rpt_fwf_run(true); + IF pg_temp.rpt_fwf_hits('anser_after_chunk', false) = 0 THEN + RAISE EXCEPTION 'walk-away: fault never fired'; + END IF; PERFORM pg_temp.rpt_fwf_fault('anser_after_chunk', 'reset', false); IF got IS DISTINCT FROM expected THEN RAISE EXCEPTION 'walk-away: % <> %', got, expected; @@ -147,6 +163,11 @@ BEGIN END $$; SELECT count(*) AS dim_rows FROM rpt_fwf_dim; + dim_rows +---------- + 1000 +(1 row) + DROP TABLE rpt_fwf_fact, rpt_fwf_dim, rpt_fwf_sub; DROP EXTENSION robust_predicate_transfer; RESET rpt.enable; diff --git a/gpcontrib/robust_predicate_transfer/expected/rpt_key_types.out b/gpcontrib/robust_predicate_transfer/expected/rpt_key_types.out new file mode 100644 index 00000000000..b3859499d0e --- /dev/null +++ b/gpcontrib/robust_predicate_transfer/expected/rpt_key_types.out @@ -0,0 +1,92 @@ +--------------------------------------------------------------------------- +-- +-- Licensed to the Apache Software Foundation (ASF) under one +-- or more contributor license agreements. See the NOTICE file +-- distributed with this work for additional information +-- regarding copyright ownership. The ASF licenses this file +-- to you under the Apache License, Version 2.0 (the +-- "License"); you may not use this file except in compliance +-- with the License. You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, +-- software distributed under the License is distributed on an +-- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +-- KIND, either express or implied. See the License for the +-- specific language governing permissions and limitations +-- under the License. +-- +-- Equality modulo 10 equates different raw Datum representations. RPT must +-- reject it even though the type is by-value and has default operator classes. +BEGIN; +SET LOCAL client_min_messages = warning; +CREATE EXTENSION robust_predicate_transfer; +CREATE SCHEMA rpt_eq_repro; +SET LOCAL search_path=rpt_eq_repro,pg_catalog; +SET LOCAL optimizer=off; +SET LOCAL statement_timeout='30s'; +SET LOCAL anser.runtime_filter=off; +SET LOCAL rpt.enable=off; +-- Legal by-value type whose equality identifies congruent integers mod 10. +CREATE TYPE modint; +CREATE FUNCTION modint_in(cstring) RETURNS modint AS 'int4in' LANGUAGE internal IMMUTABLE STRICT; +CREATE FUNCTION modint_out(modint) RETURNS cstring AS 'int4out' LANGUAGE internal IMMUTABLE STRICT; +CREATE TYPE modint(INPUT=modint_in,OUTPUT=modint_out,INTERNALLENGTH=4,PASSEDBYVALUE,ALIGNMENT=int4); +CREATE CAST (modint AS integer) WITHOUT FUNCTION AS IMPLICIT; +CREATE CAST (integer AS modint) WITHOUT FUNCTION AS IMPLICIT; +CREATE FUNCTION modint_eq(modint,modint) RETURNS boolean LANGUAGE plpgsql IMMUTABLE STRICT + AS $$ BEGIN RETURN ($1::integer % 10)=($2::integer % 10); END $$; +CREATE FUNCTION modint_hash(modint) RETURNS integer LANGUAGE sql IMMUTABLE STRICT + AS 'SELECT hashint4($1::integer % 10)'; +CREATE OPERATOR = (LEFTARG=modint,RIGHTARG=modint,FUNCTION=modint_eq,COMMUTATOR='=',HASHES); +CREATE OPERATOR CLASS modint_hash_ops DEFAULT FOR TYPE modint USING hash AS + OPERATOR 1 = (modint,modint), FUNCTION 1 modint_hash(modint); +-- Supply the type's own btree family as well: otherwise PostgreSQL can +-- choose integer's default family via the explicitly binary-compatible cast. +CREATE FUNCTION modint_cmp(modint,modint) RETURNS integer LANGUAGE sql IMMUTABLE STRICT + AS 'SELECT CASE WHEN $1::integer % 10 < $2::integer % 10 THEN -1 WHEN $1::integer % 10 > $2::integer % 10 THEN 1 ELSE 0 END'; +CREATE FUNCTION modint_lt(modint,modint) RETURNS boolean LANGUAGE sql IMMUTABLE STRICT AS 'SELECT modint_cmp($1,$2)<0'; +CREATE FUNCTION modint_le(modint,modint) RETURNS boolean LANGUAGE sql IMMUTABLE STRICT AS 'SELECT modint_cmp($1,$2)<=0'; +CREATE FUNCTION modint_gt(modint,modint) RETURNS boolean LANGUAGE sql IMMUTABLE STRICT AS 'SELECT modint_cmp($1,$2)>0'; +CREATE FUNCTION modint_ge(modint,modint) RETURNS boolean LANGUAGE sql IMMUTABLE STRICT AS 'SELECT modint_cmp($1,$2)>=0'; +CREATE OPERATOR < (LEFTARG=modint,RIGHTARG=modint,FUNCTION=modint_lt); +CREATE OPERATOR <= (LEFTARG=modint,RIGHTARG=modint,FUNCTION=modint_le); +CREATE OPERATOR > (LEFTARG=modint,RIGHTARG=modint,FUNCTION=modint_gt); +CREATE OPERATOR >= (LEFTARG=modint,RIGHTARG=modint,FUNCTION=modint_ge); +CREATE OPERATOR CLASS modint_btree_ops DEFAULT FOR TYPE modint USING btree AS + OPERATOR 1 <, OPERATOR 2 <=, OPERATOR 3 =, OPERATOR 4 >=, OPERATOR 5 >, FUNCTION 1 modint_cmp(modint,modint); +CREATE TABLE fact(k modint) DISTRIBUTED RANDOMLY; +CREATE TABLE dim(k modint,a int) DISTRIBUTED RANDOMLY; +INSERT INTO fact SELECT i::modint FROM generate_series(11,1010)i; +INSERT INTO dim SELECT i::modint,i FROM generate_series(1,10)i; +ANALYZE fact; +ANALYZE dim; +SET LOCAL rpt.forward_min_root_rows=0; +SET LOCAL rpt.forward_max_exchange_ms=-1; +SET LOCAL rpt.forward_max_selectivity=1; +DO $$ +DECLARE + opt text; + baseline bigint; + filtered bigint; + dump text; + q constant text := 'SELECT count(*) FROM fact f JOIN dim d ON f.k = d.k WHERE d.a < 11'; +BEGIN + FOREACH opt IN ARRAY ARRAY['off', 'on'] LOOP + PERFORM set_config('optimizer', opt, true); + PERFORM set_config('rpt.enable', 'off', true); + EXECUTE q INTO baseline; + PERFORM set_config('rpt.enable', 'on', true); + dump := public.rpt_transfer_plan(q); + EXECUTE q INTO filtered; + IF baseline <> 1000 OR filtered IS DISTINCT FROM baseline THEN + RAISE EXCEPTION 'custom equality lost rows (optimizer=%): % vs %', opt, filtered, baseline; + END IF; + IF dump NOT LIKE 'no transfer regions%' THEN + RAISE EXCEPTION 'custom equality was accepted (optimizer=%): %', opt, dump; + END IF; + END LOOP; +END +$$; +ROLLBACK; diff --git a/gpcontrib/robust_predicate_transfer/include/rpt.h b/gpcontrib/robust_predicate_transfer/include/rpt.h index baf832953d8..a6e3056ed33 100644 --- a/gpcontrib/robust_predicate_transfer/include/rpt.h +++ b/gpcontrib/robust_predicate_transfer/include/rpt.h @@ -31,10 +31,9 @@ * the consumer-eligibility check answers an MPP question (Anser's deadlock * rule) the single-node original never had. * - * Phase I is a pure analysis pass: nothing is injected or executed, and the - * dumps in rptplan.cpp are the only output. STL containers own malloc-backed - * storage. PostgreSQL errors must be translated to C++ exceptions before - * unwinding those containers, then rethrown at the C boundary. + * STL containers own malloc-backed storage. PostgreSQL errors must be + * translated to C++ exceptions before unwinding those containers, then + * rethrown at the C boundary. * * IDENTIFICATION * gpcontrib/robust_predicate_transfer/include/rpt.h @@ -44,6 +43,21 @@ #ifndef RPT_H #define RPT_H +/* libstdc++ locale headers may include libintl.h. Parse them before + * postgres.h defines gettext macros in builds without NLS. */ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + extern "C" { #include "postgres.h" @@ -53,11 +67,6 @@ extern "C" #include "lib/stringinfo.h" } -#include -#include -#include -#include - /* * Like gpopt's GP_WRAP: never longjmp over a live C++ container. Only call * PostgreSQL through this boundary; callbacks must do the same for their @@ -263,7 +272,7 @@ struct RptTransferMgr }; /* - * Phase III forward pass. A channel is one forward transfer edge: the + * Forward reducer. A channel is one forward transfer edge: the * reducer scan of `from` builds a bloom filter on from_key, and every scan of * `to` (in the reducer, and the root's scan in the main plan) applies it on * to_key. Only the first clause of a multi-key edge is transferred; one key @@ -313,7 +322,7 @@ struct RptForwardParams * What the reducer scan of one vertex is expected to keep, from the plan and * the catalog (rptforward.cpp). `rows` is per segment, before quals; it sizes * the vertex's filters together with `selectivity`, the fraction its own - * reducer quals keep -- 1 when they were dropped (rule 13). Vertices with no + * reducer quals keep -- 1 when they were dropped. Vertices with no * entry use their plan_rows and selectivity 1. */ struct RptForwardVertexEst @@ -345,7 +354,7 @@ extern void RptDumpForward(const std::vector &components, extern PlannedStmt *RptInjectForward(PlannedStmt *stmt, bool materialize, std::string *trace); -/* Phase III GUCs (rptinit.cpp). */ +/* Forward reducer GUCs (rptinit.cpp). */ extern bool rpt_forward; extern int rpt_forward_apply; /* RPT_FORWARD_APPLY_* */ extern double rpt_forward_ms_per_mib_part; diff --git a/gpcontrib/robust_predicate_transfer/include/rptlocal.h b/gpcontrib/robust_predicate_transfer/include/rptlocal.h index 9035f89b608..0e3d552254e 100644 --- a/gpcontrib/robust_predicate_transfer/include/rptlocal.h +++ b/gpcontrib/robust_predicate_transfer/include/rptlocal.h @@ -18,7 +18,7 @@ * under the License. * * rptlocal.h - * In-process bloom handoff for local reducer hops (design rule 16). + * In-process bloom handoff for local reducer hops. * * IDENTIFICATION * gpcontrib/robust_predicate_transfer/include/rptlocal.h diff --git a/gpcontrib/robust_predicate_transfer/scripts/diagnose_forward.py b/gpcontrib/robust_predicate_transfer/scripts/diagnose_forward.py new file mode 100644 index 00000000000..e5052528141 --- /dev/null +++ b/gpcontrib/robust_predicate_transfer/scripts/diagnose_forward.py @@ -0,0 +1,147 @@ +#!/usr/bin/env python3 +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +"""Run the forward regression with step markers and an independent watchdog. + +Use an isolated test database with both libraries installed and preloaded. +The runner never terminates a server backend or restarts the cluster. +""" +import argparse +import json +import os +from pathlib import Path +import subprocess +import time +import uuid + + +def instrument_sql(source): + """Keep regression assertions, but identify every dynamic query first.""" + source = source.replace("rpt_transfer_plan(", "pg_temp.rpt_diag_transfer_plan(") + anchor = "DO $$\nDECLARE\n opt text;" + if source.count(anchor) != 1: + raise ValueError("forward regression assertion block changed") + helper = """CREATE FUNCTION pg_temp.rpt_diag_transfer_plan(q text) RETURNS text +LANGUAGE plpgsql AS $diag$ +BEGIN + RAISE NOTICE 'RPT STEP transfer_plan SQL=%', q; + RETURN public.rpt_transfer_plan(q); +END +$diag$; +""" + source = source.replace(anchor, helper + anchor) + edits = { + " EXECUTE format('EXPLAIN (%s FORMAT JSON) %s',": + " RAISE NOTICE 'RPT STEP explain analyze=% SQL=%', do_analyze, q;\n", + " EXECUTE format('SELECT string_agg(t::text, %L ORDER BY t::text) FROM (%s) t',": + " RAISE NOTICE 'RPT STEP results rpt=% SQL=%', rpt, q;\n", + " EXECUTE 'PREPARE rpt_fwd_prep AS ' || star;": + " RAISE NOTICE 'RPT STEP prepare optimizer=%', opt;\n", + " EXECUTE 'EXECUTE rpt_fwd_prep' INTO prep_n, prep_s;": + " RAISE NOTICE 'RPT STEP execute prepared optimizer=% run=%', opt, i;\n", + " FOR r IN SELECT f.v FROM rpt_fwd_fact f JOIN rpt_fwd_d1": + " RAISE NOTICE 'RPT STEP SPI dimension scan';\n", + " FOR r IN SELECT f.v FROM rpt_fwd_fact f JOIN rpt_fwd_dl": + " RAISE NOTICE 'RPT STEP SPI local chain';\n", + } + for anchor, marker in edits.items(): + if source.count(anchor) != 1: + raise ValueError(f"forward regression instrumentation anchor changed: {anchor}") + source = source.replace(anchor, marker + anchor) + return source + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--database", default="postgres") + parser.add_argument("--output", required=True, type=Path) + parser.add_argument("--psql", default="psql") + parser.add_argument("--wall-timeout", type=float, default=180) + parser.add_argument("--cancel-grace", type=float, default=15) + parser.add_argument("--emit-only", action="store_true") + args = parser.parse_args() + if args.wall_timeout <= 0 or args.cancel_grace < 0: + parser.error("wall-timeout must be positive and cancel-grace nonnegative") + args.output.mkdir(parents=True, exist_ok=True) + source = Path(__file__).resolve().parents[1] / "sql" / "rpt_forward.sql" + application = "rpt_diagnostic_" + uuid.uuid4().hex + workload = args.output / "workload.sql" + prefix = ("\\set ON_ERROR_STOP on\n\\timing on\n" + f"SET application_name = '{application}';\n" + "SET client_min_messages = notice;\n" + "SELECT pg_backend_pid() AS diagnostic_backend_pid;\n") + workload.write_text(prefix + instrument_sql(source.read_text())) + if args.emit_only: + print(workload) + return 0 + + command = [args.psql, "-X", "-v", "ON_ERROR_STOP=1", "-d", args.database] + env = dict(os.environ) + env["PGCONNECT_TIMEOUT"] = "5" + + def monitor(filename, sql): + try: + result = subprocess.run(command + ["-A", "-t", "-c", sql], + env=env, capture_output=True, text=True, timeout=8) + (args.output / filename).write_text(result.stdout + result.stderr) + return result.returncode + except subprocess.TimeoutExpired as error: + (args.output / filename).write_text(f"monitor timed out: {error.timeout}s\n") + return -1 + + with (args.output / "client.log").open("w") as output: + proc = subprocess.Popen(command + ["-e", "-f", str(workload)], + env=env, stdout=output, stderr=subprocess.STDOUT) + start = time.monotonic() + timed_out = False + try: + proc.wait(timeout=args.wall_timeout) + except subprocess.TimeoutExpired: + timed_out = True + monitor("activity-before-cancel.json", "SELECT coalesce(json_agg(t), '[]'::json) FROM " + "(SELECT pid, application_name, state, wait_event_type, wait_event, " + "query_start, query FROM pg_stat_activity WHERE application_name = " + f"'{application}') t;") + monitor("cancel.txt", "SELECT pid, pg_cancel_backend(pid) FROM pg_stat_activity " + f"WHERE application_name = '{application}';") + try: + proc.wait(timeout=args.cancel_grace) + except subprocess.TimeoutExpired: + monitor("activity-after-cancel.json", "SELECT coalesce(json_agg(t), '[]'::json) FROM " + "(SELECT pid, state, wait_event_type, wait_event, query FROM pg_stat_activity " + f"WHERE application_name = '{application}') t;") + # Stop only this client. Leave a stuck server available for inspection. + proc.terminate() + try: + proc.wait(timeout=5) + except subprocess.TimeoutExpired: + proc.kill() + proc.wait() + result = {"application_name": application, "timed_out": timed_out, + "client_returncode": proc.returncode, + "elapsed_seconds": time.monotonic() - start} + (args.output / "result.json").write_text(json.dumps(result, indent=2) + "\n") + print(f"Saved diagnostic output in {args.output}") + if timed_out: + print("Watchdog expired. Server backends were not terminated; inspect activity and server logs.") + return 2 + return proc.returncode + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/gpcontrib/robust_predicate_transfer/scripts/survey.py b/gpcontrib/robust_predicate_transfer/scripts/survey.py index 49c1c9ce136..a700034a4b6 100644 --- a/gpcontrib/robust_predicate_transfer/scripts/survey.py +++ b/gpcontrib/robust_predicate_transfer/scripts/survey.py @@ -224,7 +224,7 @@ def parse_output(output): def summarize(records, complete): - lines = ["# RPT Phase II eligibility survey", "", + lines = ["# RPT backward eligibility survey", "", "Survey complete: " + ("yes" if complete else "no"), "", "Planning only. Each SELECT statement is counted separately, not each file. " "No benchmark query was executed. Counts labelled " diff --git a/gpcontrib/robust_predicate_transfer/sql/rpt_forward.sql b/gpcontrib/robust_predicate_transfer/sql/rpt_forward.sql index 18516893a34..83d31b206ac 100644 --- a/gpcontrib/robust_predicate_transfer/sql/rpt_forward.sql +++ b/gpcontrib/robust_predicate_transfer/sql/rpt_forward.sql @@ -17,7 +17,7 @@ -- specific language governing permissions and limitations -- under the License. -- --- Phase III forward reducer, end to end: the InitPlan is built, its filters +-- Forward reducer, end to end: the InitPlan is built, its filters -- reach the main plan across the dispatch boundary, intermediate hops work -- both through Anser and in-process, and results never change. Both -- optimizers; every check raises with the plan or dump that failed it. @@ -60,13 +60,13 @@ BEGIN RAISE EXCEPTION 'constant failure'; END $$; -CREATE FUNCTION pg_temp.rpt_fwd_plan(q text, analyze boolean) RETURNS jsonb +CREATE FUNCTION pg_temp.rpt_fwd_plan(q text, do_analyze boolean) RETURNS jsonb LANGUAGE plpgsql AS $$ DECLARE plan jsonb; BEGIN EXECUTE format('EXPLAIN (%s FORMAT JSON) %s', - CASE WHEN analyze THEN 'ANALYZE,' ELSE '' END, q) INTO plan; + CASE WHEN do_analyze THEN 'ANALYZE,' ELSE '' END, q) INTO plan; RETURN plan; END $$; @@ -202,6 +202,11 @@ BEGIN flake_local, flake_global, 'SELECT f.id, f.v FROM rpt_fwd_fact f JOIN rpt_fwd_d1 d1 ON f.d1 = d1.k WHERE d1.a < 3', + -- Existing scalar InitPlans and correlated expression SubPlans + -- must be walked with the statement's subplan lookup context. + 'SELECT count(*) FROM rpt_fwd_fact f JOIN rpt_fwd_d1 d1 ON f.d1 = d1.k WHERE d1.a < 50 AND f.v <= (SELECT max(v) FROM rpt_fwd_fact)', + 'SELECT f.id, (SELECT max(b) FROM rpt_fwd_d2 d2 WHERE d2.k = f.d2) FROM rpt_fwd_fact f JOIN rpt_fwd_d1 d1 ON f.d1 = d1.k WHERE d1.a < 50 AND f.id < 10', + 'SELECT count(*) FROM rpt_fwd_fact f JOIN rpt_fwd_d1 d1 ON f.d1 = d1.k WHERE d1.a < 0', 'SELECT count(*) FROM rpt_fwd_fact f JOIN rpt_fwd_d1 d1 ON f.d1 = d1.k WHERE d1.a IN (0,1,2,3,4,5,6,7,8,9)', 'SELECT count(*) FROM rpt_fwd_fact f JOIN rpt_fwd_d1 d1 ON f.d1 = d1.k WHERE d1.a NOT IN (0,1,2,3,4,5,6,7,8,9)', @@ -252,7 +257,7 @@ BEGIN RAISE EXCEPTION 'selectivity gate did not refuse (optimizer=%)', opt; END IF; - -- Rule 13: a qual that can fail is never run by the reducer. The + -- A qual that can fail is never run by the reducer. The -- reducer runs before the main plan and over rows the main plan may -- never read; with LIMIT 0 the main plan reads none, so a reducer -- evaluating 100 / a would raise division by zero (a = 0 exists), and @@ -304,7 +309,9 @@ BEGIN END LOOP; END $$; --- PL/pgSQL (SPI) runs the reducer like any other caller. +-- PL/pgSQL (SPI) runs the reducer like any other caller. Its static +-- statements keep the plan of their first call, so the function runs once, +-- with RPT on, and the baseline comes from uncached dynamic SQL. CREATE FUNCTION pg_temp.rpt_fwd_spi() RETURNS bigint LANGUAGE plpgsql AS $$ DECLARE total bigint := 0; @@ -330,7 +337,10 @@ BEGIN PERFORM set_config('rpt.enable', 'on', true); with_rpt := pg_temp.rpt_fwd_spi(); PERFORM set_config('rpt.enable', 'off', true); - without := pg_temp.rpt_fwd_spi(); + EXECUTE 'SELECT (SELECT sum(f.v) FROM rpt_fwd_fact f JOIN rpt_fwd_d1 d1 ON f.d1 = d1.k + WHERE d1.a < 50) + + (SELECT sum(f.v) FROM rpt_fwd_fact f JOIN rpt_fwd_dl d ON f.d1 = d.k + JOIN rpt_fwd_sub u ON d.s = u.k WHERE u.c < 5)' INTO without; IF with_rpt IS DISTINCT FROM without THEN RAISE EXCEPTION 'SPI result differs: % vs %', with_rpt, without; END IF; diff --git a/gpcontrib/robust_predicate_transfer/sql/rpt_forward_faults.sql b/gpcontrib/robust_predicate_transfer/sql/rpt_forward_faults.sql index 204de98bc23..08adbfbdd63 100644 --- a/gpcontrib/robust_predicate_transfer/sql/rpt_forward_faults.sql +++ b/gpcontrib/robust_predicate_transfer/sql/rpt_forward_faults.sql @@ -17,7 +17,7 @@ -- specific language governing permissions and limitations -- under the License. -- --- Phase III forward reducer under forced transport failures. Kept apart from +-- Forward reducer under forced transport failures. Kept apart from -- rpt_forward because gp_inject_fault exists only with the debug extensions, -- and the regression driver skips any file that calls it. -- @@ -47,18 +47,28 @@ INSERT INTO rpt_fwf_sub SELECT i, i FROM generate_series(0, 99) i; ANALYZE rpt_fwf_fact; ANALYZE rpt_fwf_dim; ANALYZE rpt_fwf_sub; +-- Dynamic SQL: a static statement would keep the plan of its first call, +-- with or without the reducer, whatever rpt.enable says later. CREATE FUNCTION pg_temp.rpt_fwf_run(rpt boolean) RETURNS text LANGUAGE plpgsql AS $$ DECLARE r text; BEGIN PERFORM set_config('rpt.enable', CASE WHEN rpt THEN 'on' ELSE 'off' END, true); - SELECT count(*)::text || '/' || sum(f.v)::text INTO r - FROM rpt_fwf_fact f JOIN rpt_fwf_dim d ON f.d = d.k - JOIN rpt_fwf_sub u ON d.s = u.k WHERE u.c < 5; + EXECUTE 'SELECT count(*)::text || ''/'' || sum(f.v)::text + FROM rpt_fwf_fact f JOIN rpt_fwf_dim d ON f.d = d.k + JOIN rpt_fwf_sub u ON d.s = u.k WHERE u.c < 5' INTO r; PERFORM set_config('rpt.enable', 'on', true); RETURN r; END $$; +-- How often a fault fired, so a case cannot pass without exercising it. +CREATE FUNCTION pg_temp.rpt_fwf_hits(fault text, coordinator boolean) +RETURNS int LANGUAGE sql AS $$ + SELECT coalesce(sum(substring(gp_inject_fault(fault, 'status', dbid) + FROM 'num times hit:''([0-9]+)''')::int), 0)::int + FROM gp_segment_configuration + WHERE role = 'p' AND (content = -1) = coordinator +$$; -- First occurrence only; on the coordinator a 'sleep' fault sleeps 1 s. CREATE FUNCTION pg_temp.rpt_fwf_fault(fault text, action text, coordinator boolean) RETURNS void LANGUAGE plpgsql AS $$ @@ -92,6 +102,9 @@ BEGIN PERFORM set_config('anser.timeout_ms', '100', true); PERFORM pg_temp.rpt_fwf_fault('anser_before_deliver', 'sleep', true); got := pg_temp.rpt_fwf_run(true); + IF pg_temp.rpt_fwf_hits('anser_before_deliver', true) = 0 THEN + RAISE EXCEPTION 'held delivery: fault never fired'; + END IF; PERFORM pg_temp.rpt_fwf_fault('anser_before_deliver', 'reset', true); IF got IS DISTINCT FROM expected THEN RAISE EXCEPTION 'held delivery: % <> %', got, expected; @@ -109,6 +122,9 @@ BEGIN PERFORM set_config('anser.debug_min_filter_bytes', '67108864', true); PERFORM pg_temp.rpt_fwf_fault('anser_after_chunk', 'skip', false); got := pg_temp.rpt_fwf_run(true); + IF pg_temp.rpt_fwf_hits('anser_after_chunk', false) = 0 THEN + RAISE EXCEPTION 'walk-away: fault never fired'; + END IF; PERFORM pg_temp.rpt_fwf_fault('anser_after_chunk', 'reset', false); IF got IS DISTINCT FROM expected THEN RAISE EXCEPTION 'walk-away: % <> %', got, expected; diff --git a/gpcontrib/robust_predicate_transfer/sql/rpt_key_types.sql b/gpcontrib/robust_predicate_transfer/sql/rpt_key_types.sql new file mode 100644 index 00000000000..b3859499d0e --- /dev/null +++ b/gpcontrib/robust_predicate_transfer/sql/rpt_key_types.sql @@ -0,0 +1,92 @@ +--------------------------------------------------------------------------- +-- +-- Licensed to the Apache Software Foundation (ASF) under one +-- or more contributor license agreements. See the NOTICE file +-- distributed with this work for additional information +-- regarding copyright ownership. The ASF licenses this file +-- to you under the Apache License, Version 2.0 (the +-- "License"); you may not use this file except in compliance +-- with the License. You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, +-- software distributed under the License is distributed on an +-- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +-- KIND, either express or implied. See the License for the +-- specific language governing permissions and limitations +-- under the License. +-- +-- Equality modulo 10 equates different raw Datum representations. RPT must +-- reject it even though the type is by-value and has default operator classes. +BEGIN; +SET LOCAL client_min_messages = warning; +CREATE EXTENSION robust_predicate_transfer; +CREATE SCHEMA rpt_eq_repro; +SET LOCAL search_path=rpt_eq_repro,pg_catalog; +SET LOCAL optimizer=off; +SET LOCAL statement_timeout='30s'; +SET LOCAL anser.runtime_filter=off; +SET LOCAL rpt.enable=off; +-- Legal by-value type whose equality identifies congruent integers mod 10. +CREATE TYPE modint; +CREATE FUNCTION modint_in(cstring) RETURNS modint AS 'int4in' LANGUAGE internal IMMUTABLE STRICT; +CREATE FUNCTION modint_out(modint) RETURNS cstring AS 'int4out' LANGUAGE internal IMMUTABLE STRICT; +CREATE TYPE modint(INPUT=modint_in,OUTPUT=modint_out,INTERNALLENGTH=4,PASSEDBYVALUE,ALIGNMENT=int4); +CREATE CAST (modint AS integer) WITHOUT FUNCTION AS IMPLICIT; +CREATE CAST (integer AS modint) WITHOUT FUNCTION AS IMPLICIT; +CREATE FUNCTION modint_eq(modint,modint) RETURNS boolean LANGUAGE plpgsql IMMUTABLE STRICT + AS $$ BEGIN RETURN ($1::integer % 10)=($2::integer % 10); END $$; +CREATE FUNCTION modint_hash(modint) RETURNS integer LANGUAGE sql IMMUTABLE STRICT + AS 'SELECT hashint4($1::integer % 10)'; +CREATE OPERATOR = (LEFTARG=modint,RIGHTARG=modint,FUNCTION=modint_eq,COMMUTATOR='=',HASHES); +CREATE OPERATOR CLASS modint_hash_ops DEFAULT FOR TYPE modint USING hash AS + OPERATOR 1 = (modint,modint), FUNCTION 1 modint_hash(modint); +-- Supply the type's own btree family as well: otherwise PostgreSQL can +-- choose integer's default family via the explicitly binary-compatible cast. +CREATE FUNCTION modint_cmp(modint,modint) RETURNS integer LANGUAGE sql IMMUTABLE STRICT + AS 'SELECT CASE WHEN $1::integer % 10 < $2::integer % 10 THEN -1 WHEN $1::integer % 10 > $2::integer % 10 THEN 1 ELSE 0 END'; +CREATE FUNCTION modint_lt(modint,modint) RETURNS boolean LANGUAGE sql IMMUTABLE STRICT AS 'SELECT modint_cmp($1,$2)<0'; +CREATE FUNCTION modint_le(modint,modint) RETURNS boolean LANGUAGE sql IMMUTABLE STRICT AS 'SELECT modint_cmp($1,$2)<=0'; +CREATE FUNCTION modint_gt(modint,modint) RETURNS boolean LANGUAGE sql IMMUTABLE STRICT AS 'SELECT modint_cmp($1,$2)>0'; +CREATE FUNCTION modint_ge(modint,modint) RETURNS boolean LANGUAGE sql IMMUTABLE STRICT AS 'SELECT modint_cmp($1,$2)>=0'; +CREATE OPERATOR < (LEFTARG=modint,RIGHTARG=modint,FUNCTION=modint_lt); +CREATE OPERATOR <= (LEFTARG=modint,RIGHTARG=modint,FUNCTION=modint_le); +CREATE OPERATOR > (LEFTARG=modint,RIGHTARG=modint,FUNCTION=modint_gt); +CREATE OPERATOR >= (LEFTARG=modint,RIGHTARG=modint,FUNCTION=modint_ge); +CREATE OPERATOR CLASS modint_btree_ops DEFAULT FOR TYPE modint USING btree AS + OPERATOR 1 <, OPERATOR 2 <=, OPERATOR 3 =, OPERATOR 4 >=, OPERATOR 5 >, FUNCTION 1 modint_cmp(modint,modint); +CREATE TABLE fact(k modint) DISTRIBUTED RANDOMLY; +CREATE TABLE dim(k modint,a int) DISTRIBUTED RANDOMLY; +INSERT INTO fact SELECT i::modint FROM generate_series(11,1010)i; +INSERT INTO dim SELECT i::modint,i FROM generate_series(1,10)i; +ANALYZE fact; +ANALYZE dim; +SET LOCAL rpt.forward_min_root_rows=0; +SET LOCAL rpt.forward_max_exchange_ms=-1; +SET LOCAL rpt.forward_max_selectivity=1; +DO $$ +DECLARE + opt text; + baseline bigint; + filtered bigint; + dump text; + q constant text := 'SELECT count(*) FROM fact f JOIN dim d ON f.k = d.k WHERE d.a < 11'; +BEGIN + FOREACH opt IN ARRAY ARRAY['off', 'on'] LOOP + PERFORM set_config('optimizer', opt, true); + PERFORM set_config('rpt.enable', 'off', true); + EXECUTE q INTO baseline; + PERFORM set_config('rpt.enable', 'on', true); + dump := public.rpt_transfer_plan(q); + EXECUTE q INTO filtered; + IF baseline <> 1000 OR filtered IS DISTINCT FROM baseline THEN + RAISE EXCEPTION 'custom equality lost rows (optimizer=%): % vs %', opt, filtered, baseline; + END IF; + IF dump NOT LIKE 'no transfer regions%' THEN + RAISE EXCEPTION 'custom equality was accepted (optimizer=%): %', opt, dump; + END IF; + END LOOP; +END +$$; +ROLLBACK; diff --git a/gpcontrib/robust_predicate_transfer/src/rpteligible.cpp b/gpcontrib/robust_predicate_transfer/src/rpteligible.cpp index cd9bc3b392d..dc481d85740 100644 --- a/gpcontrib/robust_predicate_transfer/src/rpteligible.cpp +++ b/gpcontrib/robust_predicate_transfer/src/rpteligible.cpp @@ -19,13 +19,15 @@ * * rpteligible.cpp * Analysis-only backward eligibility and refusal diagnostics. These - * proofs are shared by the survey and the future Phase II injector. + * proofs are used by the backward eligibility survey. * * IDENTIFICATION * gpcontrib/robust_predicate_transfer/src/rpteligible.cpp * *------------------------------------------------------------------------- */ +#include "rpt.h" + extern "C" { #include "postgres.h" @@ -33,8 +35,6 @@ extern "C" #include "anserplan.h" } -#include "rpt.h" -#include struct RptPlacement { diff --git a/gpcontrib/robust_predicate_transfer/src/rptforward.cpp b/gpcontrib/robust_predicate_transfer/src/rptforward.cpp index 316d6660e92..b768e76a1b1 100644 --- a/gpcontrib/robust_predicate_transfer/src/rptforward.cpp +++ b/gpcontrib/robust_predicate_transfer/src/rptforward.cpp @@ -18,7 +18,7 @@ * under the License. * * rptforward.cpp - * Phase III: build the forward reducer as an InitPlan of the statement + * Build the forward reducer as an InitPlan of the statement * and apply its filters to the root scans of the main plan. * * A transfer is an Anser channel, except a local hop consumed only inside @@ -31,7 +31,7 @@ * * Reducer scans are copies of the main plan's scans (same scanrelid, so the * same RTE, locks and permission checks) with a key-only targetlist. Their - * row set must be the main scan's or a superset (design rule 13): the quals + * row set must be the main scan's or a superset: the quals * are copied whole, in order, or dropped whole unless every one of them is * proven unable to fail. The reducer runs eagerly, over rows the main plan * may never visit (LIMIT 0, an empty outer side), so a qual that can raise an @@ -47,6 +47,8 @@ * *------------------------------------------------------------------------- */ +#include "rpt.h" + extern "C" { #include "postgres.h" @@ -68,10 +70,8 @@ extern "C" #include "rptlocal.h" } -#include "rpt.h" #include "rptqual.h" -#include /* ---- finding where a main-plan scan hangs ---- */ @@ -171,7 +171,7 @@ rpt_scan_key_position(Plan *scan, const RptKey &key) return 0; } -/* ---- rule 13: quals a reducer scan may evaluate ---- */ +/* ---- quals a reducer scan may evaluate ---- */ /* Only explicitly trusted built-in comparisons and their hash support may * run eagerly. LEAKPROOF prevents argument disclosure, not unconditional @@ -374,6 +374,19 @@ rpt_plan_has_anser_nodes_walker(Node *node, void *context) return plan_tree_walker(node, rpt_plan_has_anser_nodes_walker, context, true); } +/* The backend walker resolves SubPlan IDs even while walking expressions. + * Always provide the statement-backed prefix, including for diagnostic runs. */ +static bool +rpt_has_runtime_filters(PlannedStmt *stmt) +{ + plan_tree_base_prefix context; + + return RptPgCall([&]() { + exec_init_plan_tree_base(&context, stmt); + return rpt_plan_has_anser_nodes_walker((Node *) stmt->planTree, &context); + }); +} + /* Main-plan scans of `vertex`, wrapped in consumers of every channel into it. * False when the scan cannot be found or keyed; nothing is changed then. */ static bool @@ -412,9 +425,7 @@ RptInjectForward(PlannedStmt *stmt, bool materialize, std::string *trace) *trace += "forward: needs anser.enable=on and anser.runtime_filter=off\n"; return stmt; } - if (RptPgCall([&]() { - return rpt_plan_has_anser_nodes_walker((Node *) stmt->planTree, NULL); - })) + if (rpt_has_runtime_filters(stmt)) { *trace += "forward: plan already carries Anser nodes\n"; return stmt; @@ -533,7 +544,7 @@ RptInjectForward(PlannedStmt *stmt, bool materialize, std::string *trace) }); /* Consumers below producers: a filter is built from rows the - * inbound filters already reduced (design rule 4). */ + * inbound filters already reduced. */ for (const RptForwardChannel &ch : comp.channels) { if (ch.to != id) diff --git a/gpcontrib/robust_predicate_transfer/src/rptforwardplan.cpp b/gpcontrib/robust_predicate_transfer/src/rptforwardplan.cpp index d4c72c61ba3..3059057e5db 100644 --- a/gpcontrib/robust_predicate_transfer/src/rptforwardplan.cpp +++ b/gpcontrib/robust_predicate_transfer/src/rptforwardplan.cpp @@ -18,10 +18,10 @@ * under the License. * * rptforwardplan.cpp - * Phase III selection: which forward chains to build, their channels, + * Forward reducer selection: which forward chains to build, their channels, * and the cost gate. No PostgreSQL calls, so it is unit-tested directly. * - * The cost model is the one measured on 2026-11-10 (docs/design.md): every + * The cost model was measured on one host with three primaries: every * global hop pays about c milliseconds per MiB of bitset per producer, because * the coordinator receives and folds the parts one at a time. Hops of one * chain run one after another inside the reducer, so a component's cost is @@ -39,14 +39,14 @@ * *------------------------------------------------------------------------- */ +#include "rpt.h" + extern "C" { #include "postgres.h" } -#include "rpt.h" -#include #include /* Every vertex whose filters can reach `root` through forward edges. */ diff --git a/gpcontrib/robust_predicate_transfer/src/rptgraph.cpp b/gpcontrib/robust_predicate_transfer/src/rptgraph.cpp index 602f6d7606a..3c3e9324f03 100644 --- a/gpcontrib/robust_predicate_transfer/src/rptgraph.cpp +++ b/gpcontrib/robust_predicate_transfer/src/rptgraph.cpp @@ -51,14 +51,14 @@ * *------------------------------------------------------------------------- */ +#include "rpt.h" + extern "C" { #include "postgres.h" } -#include "rpt.h" -#include /* * One (edge, clause) pair. The artifact's EdgeInfo carries a single join diff --git a/gpcontrib/robust_predicate_transfer/src/rptinit.cpp b/gpcontrib/robust_predicate_transfer/src/rptinit.cpp index bcae2c318a9..f30ff8584ec 100644 --- a/gpcontrib/robust_predicate_transfer/src/rptinit.cpp +++ b/gpcontrib/robust_predicate_transfer/src/rptinit.cpp @@ -25,17 +25,18 @@ * first: the AnserXxx entry points we call are resolved at load time from * the global symbol table (dfmgr loads modules RTLD_GLOBAL). * - * Phase I is analysis-only: the planner hook computes the transfer plan and - * logs it at DEBUG1 when rpt.debug is on, and rpt_transfer_plan() exposes - * the same dump to SQL for the regression tests. Runtime filters are not - * injected yet. The explicit debug_noop_initplan measurement switch adds - * only an empty distributed InitPlan. + * The planner hook builds forward reducers and logs transfer decisions at + * DEBUG1 when rpt.debug is on. rpt_transfer_plan() exposes analysis without + * injecting filters. debug_noop_initplan adds an empty distributed InitPlan + * for overhead measurement. * * IDENTIFICATION * gpcontrib/robust_predicate_transfer/src/rptinit.cpp * *------------------------------------------------------------------------- */ +#include "rpt.h" + extern "C" { #include "postgres.h" @@ -61,9 +62,7 @@ PG_FUNCTION_INFO_V1(rpt_transfer_plan); void _PG_init(void); } -#include "rpt.h" -#include #include bool rpt_enable = false; @@ -74,7 +73,7 @@ int rpt_max_chain_depth = 4; bool rpt_forward = true; int rpt_forward_apply = RPT_FORWARD_APPLY_ROOT; -/* Measured 2026-11-10 on one host, 3 primaries (docs/design.md). */ +/* Measured 2026-11-10 on one host with three primaries. */ double rpt_forward_ms_per_mib_part = 3.5; double rpt_forward_max_exchange_ms = 100.0; double rpt_forward_min_root_rows = 10000.0; @@ -211,8 +210,8 @@ rpt_define_gucs(void) NULL, NULL, NULL); DefineCustomBoolVariable("rpt.backward", - "Reserves the backward runtime switch for a future Phase II build.", - "QD-only; this analysis-only build never injects executor nodes.", + "Reserves the backward runtime switch.", + "QD-only; backward runtime injection is not implemented.", &rpt_backward, true, PGC_USERSET, 0, NULL, NULL, NULL); diff --git a/gpcontrib/robust_predicate_transfer/src/rptinject.cpp b/gpcontrib/robust_predicate_transfer/src/rptinject.cpp index eafe3c731f0..04d6dfec6b0 100644 --- a/gpcontrib/robust_predicate_transfer/src/rptinject.cpp +++ b/gpcontrib/robust_predicate_transfer/src/rptinject.cpp @@ -22,6 +22,8 @@ * *------------------------------------------------------------------------- */ +#include "rpt.h" + extern "C" { #include "postgres.h" @@ -30,7 +32,6 @@ extern "C" #include "anserplan.h" } -#include "rpt.h" /* The caller owns a private copy of the statement. This function may throw; * publish that copy only after the entire injection pass has succeeded. diff --git a/gpcontrib/robust_predicate_transfer/src/rptlocal.c b/gpcontrib/robust_predicate_transfer/src/rptlocal.c index 4807c7ae0fd..22f7a74fc39 100644 --- a/gpcontrib/robust_predicate_transfer/src/rptlocal.c +++ b/gpcontrib/robust_predicate_transfer/src/rptlocal.c @@ -18,7 +18,7 @@ * under the License. * * rptlocal.c - * In-process bloom handoff for local reducer hops (design rule 16). + * In-process bloom handoff for local reducer hops. * * A local hop joins co-located tables: every row of the destination that can * match a row of the source lives on the same segment as that row. So the diff --git a/gpcontrib/robust_predicate_transfer/src/rptplan.cpp b/gpcontrib/robust_predicate_transfer/src/rptplan.cpp index 58d87b67098..ec005a4107c 100644 --- a/gpcontrib/robust_predicate_transfer/src/rptplan.cpp +++ b/gpcontrib/robust_predicate_transfer/src/rptplan.cpp @@ -52,6 +52,8 @@ * *------------------------------------------------------------------------- */ +#include "rpt.h" + extern "C" { #include "postgres.h" @@ -59,9 +61,7 @@ extern "C" #include "optimizer/walkers.h" } -#include "rpt.h" -#include #include /* @@ -434,7 +434,7 @@ rpt_create_transfer_plan(RptTransferMgr *mgr, /* Locality is a property of the base scans' distribution policies, including * for implied and bypass edges. A Motion elsewhere in the main plan does not * change where the reducer reads a key. Parallel workers still need a - * segment-local fold (or a non-parallel reducer); see docs/design.md. */ + * segment-local fold (or a non-parallel reducer). */ static bool rpt_local_hop(const RptTransferMgr *mgr, int source, const RptGraphEdge &hop) { diff --git a/gpcontrib/robust_predicate_transfer/src/rptregion.cpp b/gpcontrib/robust_predicate_transfer/src/rptregion.cpp index 9811011762d..9e9994e31d2 100644 --- a/gpcontrib/robust_predicate_transfer/src/rptregion.cpp +++ b/gpcontrib/robust_predicate_transfer/src/rptregion.cpp @@ -31,13 +31,15 @@ * anser_hashjoin_keys (all in gpcontrib/anser/src/anserplan.c), widened as * noted at each site. * - * Phase I is analysis only: nothing here touches the plan tree. + * Graph extraction does not modify the plan tree. * * IDENTIFICATION * gpcontrib/robust_predicate_transfer/src/rptregion.cpp * *------------------------------------------------------------------------- */ +#include "rpt.h" + extern "C" { #include "postgres.h" @@ -56,7 +58,6 @@ extern "C" #include "anserplan.h" } -#include "rpt.h" static bool rpt_is_vertex_scan(const Plan *plan); static bool rpt_scan_is_filtered(const Plan *plan); @@ -275,6 +276,35 @@ rpt_resolve_key_scan(Plan *top, AttrNumber key_attno, } } +/* Raw Datum bytes are a valid Bloom key only for these canonical built-in + * types. A custom by-value type can equate distinct representations even + * through its own default equality operator. Domains are unwrapped first. */ +static bool +rpt_key_type_supported(Oid type) +{ + switch (type) + { + case BOOLOID: + case CHAROID: + case INT2OID: + case INT4OID: + case INT8OID: + case OIDOID: + case XIDOID: + case CIDOID: + case DATEOID: + case TIMEOID: + case TIMESTAMPOID: + case TIMESTAMPTZOID: + case MONEYOID: + case PG_LSNOID: + case XID8OID: + return true; + default: + return false; + } +} + /* * Validate one equijoin clause and split it into outer/inner Vars. * @@ -282,7 +312,7 @@ rpt_resolve_key_scan(Plan *top, AttrNumber key_attno, * (gpcontrib/anser/src/anserplan.c), lifted from HashJoin->hashclauses to * any clause list: a two-argument OpExpr whose RelabelType-stripped args are * one OUTER_VAR and one INNER_VAR Var, over the same by-value base type on - * both sides, excluding floats, with opno equal to the type's TYPECACHE_EQ_OPR + * both sides, restricted to trusted built-ins, with opno equal to TYPECACHE_EQ_OPR * equality operator. Anser hashes raw Datum bytes, and the transfer filters * will too, so SQL equality must coincide with bitwise Datum equality of the * key; anything looser could prune rows that actually join. A NestLoop @@ -341,14 +371,13 @@ rpt_equality_clause(Node *clause, Var **outer_var, Var **inner_var, keytype = RptPgCall([&]() { return getBaseType((*inner_var)->vartype); }); if (!RptPgCall([&]() { return get_typbyval(keytype); })) return false; - if (keytype == FLOAT4OID || keytype == FLOAT8OID) + if (!rpt_key_type_supported(keytype)) return false; /* * The clause has to be the type's own equality rather than just some - * hashable operator spelled "=". See anser_hashjoin_keys for why this - * alone is not the bitwise-equality proof and the float exclusion above - * is still needed. + * hashable operator spelled "=". The type allowlist above supplies the + * separate proof that equal keys have the same Datum representation. */ tce = RptPgCall([&]() { return lookup_type_cache(keytype, TYPECACHE_EQ_OPR); }); if (!OidIsValid(tce->eq_opr) || op->opno != tce->eq_opr) diff --git a/gpcontrib/robust_predicate_transfer/test/diagnose_forward_test.py b/gpcontrib/robust_predicate_transfer/test/diagnose_forward_test.py new file mode 100644 index 00000000000..22622bb8e41 --- /dev/null +++ b/gpcontrib/robust_predicate_transfer/test/diagnose_forward_test.py @@ -0,0 +1,85 @@ +#!/usr/bin/env python3 +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +"""Offline checks for the forward diagnostic runner and watchdog.""" +import importlib.util +import json +from pathlib import Path +import subprocess +import sys +import tempfile +import unittest + +ROOT = Path(__file__).resolve().parents[1] +SCRIPT = ROOT / "scripts" / "diagnose_forward.py" +spec = importlib.util.spec_from_file_location("diagnose_forward", SCRIPT) +diag = importlib.util.module_from_spec(spec) +spec.loader.exec_module(diag) + + +class DiagnosticTest(unittest.TestCase): + def test_instrumentation_retains_assertions_and_reports_dynamic_queries(self): + source = (ROOT / "sql" / "rpt_forward.sql").read_text() + output = diag.instrument_sql(source) + self.assertIn("IF pg_temp.rpt_fwd_removed(plan) <= 0 THEN", output) + self.assertIn("RPT STEP explain analyze=% SQL=%", output) + self.assertIn("RPT STEP results rpt=% SQL=%", output) + self.assertIn("RPT STEP transfer_plan SQL=%", output) + self.assertIn("RETURN public.rpt_transfer_plan(q);", output) + self.assertIn("RPT STEP SPI local chain", output) + + def test_changed_fixture_fails_instead_of_silently_losing_markers(self): + with self.assertRaises(ValueError): + diag.instrument_sql("SELECT 1;") + + def run_fixture(self, hangs): + with tempfile.TemporaryDirectory() as directory: + root = Path(directory) + fake_psql = root / "psql" + fake_psql.write_text("#!" + sys.executable + "\n" + "import sys, time\n" + "if '-c' in sys.argv:\n" + " print(sys.argv[sys.argv.index('-c') + 1])\n" + " sys.exit(0)\n" + "print('RPT STEP fixture', flush=True)\n" + + ("time.sleep(30)\n" if hangs else "")) + fake_psql.chmod(0o755) + output = root / "output" + result = subprocess.run([sys.executable, str(SCRIPT), "--psql", str(fake_psql), + "--output", str(output), "--wall-timeout", + "0.2" if hangs else "5", "--cancel-grace", "0.1"], + capture_output=True, text=True, timeout=10) + state = json.loads((output / "result.json").read_text()) + self.assertEqual(state["timed_out"], hangs) + self.assertEqual(result.returncode, 2 if hangs else 0, result.stderr) + if hangs: + self.assertIn("pg_stat_activity", (output / "activity-before-cancel.json").read_text()) + self.assertIn("pg_cancel_backend", (output / "cancel.txt").read_text()) + self.assertTrue((output / "activity-after-cancel.json").is_file()) + self.assertIn("not terminated", result.stdout) + self.assertIn("RPT STEP fixture", (output / "client.log").read_text()) + + def test_successful_workload(self): + self.run_fixture(False) + + def test_external_watchdog_survives_an_unresponsive_client(self): + self.run_fixture(True) + + +if __name__ == "__main__": + unittest.main() diff --git a/gpcontrib/robust_predicate_transfer/test/forward_walk_test.cpp b/gpcontrib/robust_predicate_transfer/test/forward_walk_test.cpp new file mode 100644 index 00000000000..ef0f3ee0d70 --- /dev/null +++ b/gpcontrib/robust_predicate_transfer/test/forward_walk_test.cpp @@ -0,0 +1,98 @@ +/*------------------------------------------------------------------------- + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * forward_walk_test.cpp + * SubPlan context tests using the real backend plan walker. + * + * IDENTIFICATION + * gpcontrib/robust_predicate_transfer/test/forward_walk_test.cpp + * + *------------------------------------------------------------------------- + */ +#include "../src/rptforward.cpp" +#undef fprintf +#define CHECK(c) do { if (!(c)) { fprintf(stderr, "check failed: %s:%d: %s\n", __FILE__, __LINE__, #c); abort(); } } while (0) + +static Plan *anser_node; +static Plan *local_node; +extern "C" { +sigjmp_buf *PG_exception_stack = NULL; +ErrorContextCallback *error_context_stack = NULL; +int dispatch_nest_level = 0; +void ExceptionalCondition(const char *, const char *, int) { abort(); } +void check_stack_depth(void) {} +bool AnserIsRuntimeFilterScan(const Plan *p) { return p == anser_node; } +bool RptIsLocalScan(const Plan *p) { return p == local_node; } +bool is_plan_node(Node *p) +{ + return IsA(p, Result) || IsA(p, SeqScan) || IsA(p, CustomScan); +} +bool expression_tree_walker_impl(Node *node, tree_walker_callback walker, void *ctx) +{ + if (!node) return false; + CHECK(IsA(node, List)); + ListCell *lc; + foreach(lc, (List *) node) + if (walker((Node *) lfirst(lc), ctx)) return true; + return false; +} +bool query_tree_walker_impl(Query *, tree_walker_callback, void *, int) { abort(); } +} + +int main() +{ + PlannedStmt stmt = {}; + stmt.type = T_PlannedStmt; + Result root = {}, subroot = {}; + root.plan.type = subroot.plan.type = T_Result; + stmt.planTree = &root.plan; + ListCell plan_cell = {}, expression_cell = {}; + List plans = {}, expressions = {}; + plans.type = expressions.type = T_List; + plans.length = plans.max_length = expressions.length = expressions.max_length = 1; + plans.elements = &plan_cell; expressions.elements = &expression_cell; + plan_cell.ptr_value = &subroot; + stmt.subplans = &plans; + SubPlan subplan = {}; + subplan.xpr.type = T_SubPlan; + subplan.plan_id = 1; + expression_cell.ptr_value = &subplan; + + /* Resolving an existing InitPlan must not assert or miss its children. */ + root.plan.initPlan = &expressions; + CHECK(!rpt_has_runtime_filters(&stmt)); + CustomScan marker = {}; + marker.scan.plan.type = T_CustomScan; + subroot.plan.lefttree = &marker.scan.plan; + anser_node = &marker.scan.plan; + CHECK(rpt_has_runtime_filters(&stmt)); + anser_node = NULL; + local_node = &marker.scan.plan; + CHECK(rpt_has_runtime_filters(&stmt)); + local_node = NULL; + + /* Correlated/expression SubPlans use the same lookup context. */ + root.plan.initPlan = NIL; + root.plan.qual = &expressions; + CHECK(!rpt_has_runtime_filters(&stmt)); + anser_node = &marker.scan.plan; + CHECK(rpt_has_runtime_filters(&stmt)); + CHECK(PG_exception_stack == NULL && error_context_stack == NULL); + return 0; +} diff --git a/gpcontrib/robust_predicate_transfer/test/graph_test.cpp b/gpcontrib/robust_predicate_transfer/test/graph_test.cpp index bba8df6a65e..51a5a5f28c7 100644 --- a/gpcontrib/robust_predicate_transfer/test/graph_test.cpp +++ b/gpcontrib/robust_predicate_transfer/test/graph_test.cpp @@ -968,7 +968,7 @@ static void forward_selection_chain_and_selectivity() RptSelectForward(forward, params, est, size_one_mib, ndv_large_dims, &comps); CHECK(comps[0].est_selectivity > 0.0499 && comps[0].est_selectivity < 0.0501); - /* Quals dropped in the reducer (rule 13): nothing selective is left, the + /* Quals dropped in the reducer: nothing selective is left, the * filters grow to the whole table, and lambda refuses the chain. */ est[2].selectivity = 1; est[2].quals_dropped = true; est[3].selectivity = 1; est[3].quals_dropped = true; @@ -1039,8 +1039,40 @@ static void qual_function_allowlist() CHECK(!RptQualHashFunctionSafe(1000000)); } +static void raw_datum_key_types() +{ + for (Oid type : {BOOLOID, CHAROID, INT2OID, INT4OID, INT8OID, OIDOID, + XIDOID, CIDOID, DATEOID, TIMEOID, TIMESTAMPOID, + TIMESTAMPTZOID, MONEYOID, PG_LSNOID, XID8OID}) + CHECK(rpt_key_type_supported(type)); + for (Oid type : std::vector{InvalidOid, FLOAT4OID, FLOAT8OID, TEXTOID, NUMERICOID, + 1000000}) + CHECK(!rpt_key_type_supported(type)); + + /* Even if a custom type is by-value, refuse it before consulting its + * own default equality (the type-cache stub aborts if reached). */ + Var outer = {}, inner = {}; + outer.xpr.type = inner.xpr.type = T_Var; + outer.varno = OUTER_VAR; inner.varno = INNER_VAR; + outer.vartype = inner.vartype = 1000000; + ListCell args[2] = {}; + args[0].ptr_value = &outer; args[1].ptr_value = &inner; + List arguments = {}; + arguments.type = T_List; + arguments.length = arguments.max_length = 2; + arguments.elements = args; + OpExpr equality = {}; + equality.xpr.type = T_OpExpr; + equality.args = &arguments; + equality.opno = 1000001; + Var *out = NULL, *in = NULL; + Oid keytype = InvalidOid; + CHECK(!rpt_equality_clause((Node *) &equality, &out, &in, &keytype)); +} + int main() { + raw_datum_key_types(); qual_function_allowlist(); forward_selection_chain_and_selectivity(); forward_selection_in_process(); From c0e3637bfd84ecfdc82b29ae3f3c4f92a0fcdcd2 Mon Sep 17 00:00:00 2001 From: Leonid Borchuk Date: Fri, 13 Nov 2026 12:59:30 +0300 Subject: [PATCH 24/27] Feature: Inject RPT backward runtime filters Use the largest-root forest for forward and backward transfers. Build one backward producer per root-led equivalence class above the root's forward consumers, and place member consumers only where the existing eligibility proof permits waiting. Share channel and plan-node IDs and track scan stacks so both passes relink the original parent safely. Add backward sizing, selectivity and exchange-cost gates, including a per-consumer delivery term shared with forward selection. Support backward-only injection and retain fail-open planning and cancellation. No core or Anser changes are required. Make range-table identities explicit throughout RPT and resolve relation aliases on demand. Document the call chain and runtime structures, and extend the survey and diagnostic runner with largest-root decisions. Add native placement and selection coverage plus SQL and transport-fault regressions under both optimizers. Allow 120 seconds for assertion blocks, verify oversized producers do not complete filter deliveries, and ignore native-test dSYM bundles. Validation: native tests with -DNDEBUG and ASan/UBSan; compiler checks; 11 survey tests and 6 diagnostic tests; git diff --check. Cluster regressions and benchmark experiments remain pending on the dev cluster. Assisted-by: ChatGPT --- .../robust_predicate_transfer/.gitignore | 1 + gpcontrib/robust_predicate_transfer/Makefile | 13 +- gpcontrib/robust_predicate_transfer/README.md | 297 ++++++++++++++- .../expected/rpt_backward.out | 276 ++++++++++++++ .../expected/rpt_backward_eligibility.out | 22 +- .../expected/rpt_backward_faults.out | 209 +++++++++++ .../expected/rpt_forward.out | 2 + .../expected/rpt_forward_faults.out | 2 + .../expected/rpt_graph.out | 28 +- .../robust_predicate_transfer/include/rpt.h | 107 ++++-- .../include/rptinject.h | 50 +++ .../scripts/diagnose_forward.py | 128 +------ .../scripts/diagnose_rpt.py | 212 +++++++++++ .../scripts/survey.py | 36 +- .../sql/rpt_backward.sql | 276 ++++++++++++++ .../sql/rpt_backward_eligibility.sql | 22 +- .../sql/rpt_backward_faults.sql | 209 +++++++++++ .../sql/rpt_forward.sql | 2 + .../sql/rpt_forward_faults.sql | 2 + .../sql/rpt_graph.sql | 28 +- .../src/rptbackward.cpp | 142 +++++++ .../src/rptbackwardplan.cpp | 159 ++++++++ .../src/rpteligible.cpp | 79 ++-- .../src/rptforward.cpp | 346 ++++++++++++------ .../src/rptforwardplan.cpp | 108 +++--- .../src/rptgraph.cpp | 136 +++---- .../robust_predicate_transfer/src/rptinit.cpp | 57 +-- .../src/rptinject.cpp | 48 +-- .../robust_predicate_transfer/src/rptplan.cpp | 326 +++++++++-------- .../src/rptregion.cpp | 74 ++-- ...e_forward_test.py => diagnose_rpt_test.py} | 27 +- .../test/forward_walk_test.cpp | 12 + .../test/graph_test.cpp | 315 +++++++++++----- .../test/inject_test.cpp | 185 +++++++++- .../test/survey_test.py | 15 +- 35 files changed, 3098 insertions(+), 853 deletions(-) create mode 100644 gpcontrib/robust_predicate_transfer/expected/rpt_backward.out create mode 100644 gpcontrib/robust_predicate_transfer/expected/rpt_backward_faults.out create mode 100644 gpcontrib/robust_predicate_transfer/include/rptinject.h create mode 100644 gpcontrib/robust_predicate_transfer/scripts/diagnose_rpt.py create mode 100644 gpcontrib/robust_predicate_transfer/sql/rpt_backward.sql create mode 100644 gpcontrib/robust_predicate_transfer/sql/rpt_backward_faults.sql create mode 100644 gpcontrib/robust_predicate_transfer/src/rptbackward.cpp create mode 100644 gpcontrib/robust_predicate_transfer/src/rptbackwardplan.cpp rename gpcontrib/robust_predicate_transfer/test/{diagnose_forward_test.py => diagnose_rpt_test.py} (71%) diff --git a/gpcontrib/robust_predicate_transfer/.gitignore b/gpcontrib/robust_predicate_transfer/.gitignore index 2dea2174cb0..9545f33b39b 100644 --- a/gpcontrib/robust_predicate_transfer/.gitignore +++ b/gpcontrib/robust_predicate_transfer/.gitignore @@ -1,6 +1,7 @@ /test/graph_test /test/inject_test /test/forward_walk_test +/test/*.dSYM # Local design notes and implementation plans. /docs/ diff --git a/gpcontrib/robust_predicate_transfer/Makefile b/gpcontrib/robust_predicate_transfer/Makefile index 95be2afbbd4..1fa6a2c20ed 100644 --- a/gpcontrib/robust_predicate_transfer/Makefile +++ b/gpcontrib/robust_predicate_transfer/Makefile @@ -27,6 +27,8 @@ MODULE_big = robust_predicate_transfer OBJS = \ $(WIN32RES) \ + src/rptbackward.o \ + src/rptbackwardplan.o \ src/rptgraph.o \ src/rpteligible.o \ src/rptforward.o \ @@ -42,7 +44,7 @@ PGFILEDESC = "robust_predicate_transfer - RPT+ transfer plans over Anser" EXTENSION = robust_predicate_transfer DATA = robust_predicate_transfer--1.0.sql -REGRESS = rpt_regions rpt_graph rpt_backward_eligibility rpt_key_types rpt_forward rpt_forward_faults +REGRESS = rpt_regions rpt_graph rpt_backward_eligibility rpt_key_types rpt_forward rpt_forward_faults rpt_backward rpt_backward_faults # Anser is a sibling module, not a linked library: it is preloaded and dfmgr.c # loads modules RTLD_GLOBAL, so the AnserXxx entry points resolve at load time. @@ -117,8 +119,9 @@ unitcheck: test/graph_test test/inject_test test/forward_walk_test test/graph_test: $(srcdir)/test/graph_test.cpp $(wildcard $(srcdir)/src/*.cpp) $(srcdir)/include/rpt.h $(srcdir)/include/rptqual.h $(CXX) $(CXXFLAGS) $(CPPFLAGS) $(PG_CPPFLAGS) $< -o $@ -test/inject_test: $(srcdir)/test/inject_test.cpp $(srcdir)/src/rptinject.cpp $(srcdir)/include/rpt.h - $(CXX) $(CXXFLAGS) $(CPPFLAGS) $(PG_CPPFLAGS) $< -o $@ +test/inject_test: $(srcdir)/test/inject_test.cpp $(wildcard $(srcdir)/src/*.cpp) $(srcdir)/include/rpt.h $(srcdir)/include/rptinject.h + $(CXX) $(CXXFLAGS) $(CPPFLAGS) $(PG_CPPFLAGS) -ffunction-sections -fdata-sections \ + $(WALK_TEST_LDFLAGS) $< -o $@ EXTRA_CLEAN += test/graph_test test/inject_test @@ -138,7 +141,7 @@ endif test/forward_walk_backend.o: $(top_srcdir)/src/backend/optimizer/util/walkers.c $(CC) $(CFLAGS) $(CPPFLAGS) $(PG_CPPFLAGS) -ffunction-sections -fdata-sections -c $< -o $@ -test/forward_walk_test: $(srcdir)/test/forward_walk_test.cpp $(srcdir)/src/rptforward.cpp $(srcdir)/include/rpt.h test/forward_walk_backend.o +test/forward_walk_test: $(srcdir)/test/forward_walk_test.cpp $(srcdir)/src/rptforward.cpp $(srcdir)/include/rpt.h $(srcdir)/include/rptinject.h test/forward_walk_backend.o $(CXX) $(CXXFLAGS) $(CPPFLAGS) $(PG_CPPFLAGS) -ffunction-sections -fdata-sections \ $(WALK_TEST_LDFLAGS) $< test/forward_walk_backend.o -o $@ @@ -146,4 +149,4 @@ EXTRA_CLEAN += test/forward_walk_test test/forward_walk_backend.o .PHONY: diagnosticcheck diagnosticcheck: - PYTHONDONTWRITEBYTECODE=1 $(SURVEY_PYTHON) $(srcdir)/test/diagnose_forward_test.py + PYTHONDONTWRITEBYTECODE=1 $(SURVEY_PYTHON) $(srcdir)/test/diagnose_rpt_test.py diff --git a/gpcontrib/robust_predicate_transfer/README.md b/gpcontrib/robust_predicate_transfer/README.md index e9f6b3999ee..003da4300d2 100644 --- a/gpcontrib/robust_predicate_transfer/README.md +++ b/gpcontrib/robust_predicate_transfer/README.md @@ -33,9 +33,14 @@ with `anser.runtime_filter=off` so only one policy injects filters. The forward reducer runs as an InitPlan of the same statement, under its snapshot, before the main query starts. Intermediate co-distributed transfers stay inside the reducer process. Filters used by the main query cross the -dispatch boundary through Anser's statement-scoped channels. Backward -transfer schedules are available for analysis; backward runtime injection -and dynamic abandonment are not implemented. +dispatch boundary through Anser's statement-scoped channels. + +The backward pass builds one filter per equivalence class on the largest +root's main-plan scan, above its forward consumers. Eligible class members +consume that filter in their main-plan scans. No extra root scan is added. +A consumer is injected only when the existing hash build/probe and slice +ordering proves it can wait safely. Local edges rely on core runtime-filter +pushdown; bridge-led backward classes and dynamic abandonment are deferred. ## References and attribution @@ -72,3 +77,289 @@ server configuration and restart the cluster. In a database, create the `rpt_transfer_plan(text)` reports the transfer analysis without injecting filters into the query it describes. `rpt.debug=on` logs planning decisions. + +## Call chain and data structures + +### PostgreSQL planning entry point + +At preload, `_PG_init()` registers RPT's local CustomScan methods and +installs `rpt_planner()` as a planner hook. The query follows this path: + +```text +SQL → parse → analyze and rewrite → pg_plan_query() → planner() + → rpt_planner() + → previous planner hook (Anser) + → standard_planner() → PostgreSQL planner or ORCA + ← PlannedStmt + → RPT processing + ← final PlannedStmt +→ executor +``` + +RPT analyzes a finished distributed plan, after the previous hook returns. +Runtime injection runs on the coordinator for eligible SELECT statements +when `rpt.enable` and either `rpt.forward` or `rpt.backward` are enabled. It requires +`anser.enable=on` and `anser.runtime_filter=off`. + +The hook calls `rpt_try_injection()` → `rpt_build_injection()` → +`RptInject(stmt, true, ...)`. Injection works on a private copy of +`PlannedStmt`. An ordinary construction error produces a warning and retains +the original plan; query cancellation still propagates. See +[rptinit.cpp](src/rptinit.cpp) and [rptforward.cpp](src/rptforward.cpp). + +### Analysis and schedule selection + +Inside `RptInject()`, the analysis proceeds as follows: + +| Call | Product | Use | +| --- | --- | --- | +| `RptCollectGraph()` | Scan vertices and join edges in `RptTransferMgr` | Records relations, keys, predicates, distribution policies and allowed transfer directions | +| `RptBuildClasses()` | Key equivalence classes and table classifications | Finds relations connected through equal keys and possible bypass edges | +| `RptBuildForwardPlan()` | Largest-root schedule for both runtime passes | Retains forward and backward edges and their local/global classifications | +| `RptCheckBackwardConsumers()` | Per-edge eligibility proof | Records original parents, key positions, slice indexes, refusal reasons and the fan-out cap | +| `RptSelectForward()` | `vector` | Selects chains using cost, root-size and selectivity gates | +| `RptSelectBackward()` | `vector` | Groups proven consumers by class and applies sizing, selectivity and exchange-cost gates | + +The implementations are in [rptregion.cpp](src/rptregion.cpp), +[rptgraph.cpp](src/rptgraph.cpp), [rptplan.cpp](src/rptplan.cpp), +[rpteligible.cpp](src/rpteligible.cpp) and +[rptforwardplan.cpp](src/rptforwardplan.cpp) and +[rptbackwardplan.cpp](src/rptbackwardplan.cpp). + +The structures are declared in [rpt.h](include/rpt.h): + +| Structure | Meaning | +| --- | --- | +| `RptVertexInfo` | One scan, its relation OID, row estimate, predicates and distribution properties; `scan` points into the working plan | +| `RptKey` | A column identified by `{rtindex, attno}` | +| `RptEdgeInfo` | Equality connection between two vertices, key pairs and directions prohibited by join semantics | +| `RptKeyGroup` | An equivalence class of join keys and its leader | +| `RptGraphNode` | Incoming and outgoing forward/backward transfers for one vertex | +| `RptGraphEdge` | One transfer; `other_rtindex` names the producer for an incoming edge and the consumer for an outgoing edge | +| `RptTransferMgr` | Temporary owner of the vertices, edges, classes, schedules and backward eligibility decisions | +| `RptForwardComponent` | One root, its ordered reducer chain, channels, estimates, and acceptance or refusal reason | +| `RptForwardChannel` | Source and target keys, filter sizing, channel identity and local/global execution decisions | +| `RptBackwardConsumer` | One survey proof or refusal for a class member | +| `RptBackwardClass` | One root producer, its sizing and selected consumers, estimated cost and decision | +| `RptInjectionState` | Shared channel/node allocators and original-parent/current-top maps, declared in [rptinject.h](include/rptinject.h) | + +`RptTransferMgr.vertex_index_by_rtindex` maps a range-table index to a +position in `vertices`. `neighbor_matrix` maps relation pairs to edges; +`uf_parent` represents the key union-find; `groups` contains the resulting +classes. `graph` maps each range-table index to its transfer node. +`root_rtindexes`, `bridge_rtindexes`, `reducer_order_rtindexes` and +`transfer_order_rtindexes` describe the schedule. These are planning +structures; the executor receives the resulting plan nodes instead. + +Keep the identities distinct when reading or extending the code: + +| Name | Identity | +| --- | --- | +| `rtindex` | Relation occurrence in `PlannedStmt.rtable`; distinguishes self-join aliases | +| `relid` | Relation OID; identifies the underlying relation | +| `attno` | Column number within the relation or the relevant node's output | +| `plan_node_id` | Executable plan node | +| `slice_index` | Distributed execution slice | +| `condition_id` | Runtime filter channel connecting a producer and its consumers | + +After selection, preflight checks that the scans and required key outputs +can be used. A rejected component adds no executable nodes. + +### Constructing the executable plan + +For an accepted chain `A → B → C`, where C is the largest relation, RPT +builds this conceptual plan: + +```text +Reducer InitPlan: + Sequence + Producer(filter A) + copied scan A + safe restriction quals + Producer(filter B) + Consumer(filter A) + copied scan B + safe restriction quals + empty Result + +Main plan: + original joins + Producer(backward filter C) + Consumer(filter B) + original scan C + Consumer(backward filter C) [only on proven class members] + original member scan +``` + +Reducer scans output the required key columns. Restriction quals that +cannot be proven safe for this extra execution are omitted, reducing filter +selectivity. Consumers sit below producers, so B's outgoing filter contains +keys from rows that have already passed A's filter. + +In-process reducer channels use `RptBuildLocalProducerScan()` and +`RptBuildLocalConsumerScan()` from [rptlocal.c](src/rptlocal.c). Channels +requiring Anser use `AnserBuildBloomProducerScan()` and +`AnserBuildBloomConsumerScan()`. Policy locality and in-process execution +are separate decisions: a filter consumed by the main plan must cross the +InitPlan dispatch boundary even when its keys are co-distributed. + +By default, only the main-plan root receives forward filters. +`rpt.forward_apply=all` also attempts to wrap applicable chain scans. +Backward producers and consumers are added by +[`RptInjectBackward()`](src/rptbackward.cpp) after forward wrappers. A class +is preflighted before any of its nodes are built: every endpoint must occur +once, remain under its proven original parent, and expose the required key. +Each wrapper uses the current stack top and relinks only the original parent. +One allocator supplies condition IDs across both passes, and one supplies +plan-node IDs. If forward is disabled or refused, backward may still run on +the plain root scan. + +`RptAttachReducerInitPlan()` in [rptinject.cpp](src/rptinject.cpp) attaches +the reducer: + +```text +Coordinator InitPlan slice + Gather Motion + Segment sender slice + Sequence + reducer subtree 1 + reducer subtree 2 + ... + Result with constant false +``` + +It appends the reducer tree to `PlannedStmt.subplans`, attaches a `SubPlan` +descriptor to the main tree's `initPlan`, adds a `PARAM_EXEC` slot in +`paramExecTypes`, and extends `slices`, `subplan_sliceIds` and `numSlices`. +New nodes receive unique `plan_node_id` values. The modified `PlannedStmt` +is the lasting output of RPT planning. + +### Execution + +Cloudberry initializes the plan and processes distributed InitPlans through +`preprocess_initplans()` before main execution. The reducer's `Sequence` +drains its children in producer-before-consumer order. The final empty +`Result` returns no query rows; the reducer's useful output is its filters. + +For the example above, execution scans A and builds its filter, scans B +through A's filter and builds B's filter, then executes the main plan with +B's filter applied to C. C's backward producer summarizes the rows that +pass these consumers and publishes to the proven class-member consumers. +Anser's `defer_first` preserves the probe tuple a hash join may fetch before +building its hash table. The original joins still check the exact SQL +conditions. Abandoned producers cancel their channels, and exchange failure +or timeout makes consumers run unfiltered. + +An in-process producer stores its Bloom filter in executor-owned state, +identified by `EState` and `condition_id`. A local consumer uses only a +complete filter; otherwise it passes rows through. Anser-backed nodes +handle filter publication, transport and consumption across dispatches. + +### Diagnostic entry point + +`rpt_transfer_plan(text)` follows a separate path: + +```text +rpt_transfer_plan() + → pg_parse_query() + → pg_analyze_and_rewrite_fixedparams() + → pg_plan_query() [RPT injection suppressed] + → rpt_analyze() + → RptCollectGraph() + → RptBuildClasses() + → RptBuildTransferPlan() + → RptDumpTransferPlan() + → RptInject(materialize=false) + → text containing analysis and forward decisions +``` + +This function plans the supplied query but does not execute it, run reducer +scans or build filters. + +The diagnostic dump retains the filtered-root survey schedule for comparison. +The `forward component`, `backward class` and `backward consumer ... root=` +lines describe runtime decisions on the shared largest-root schedule. + +## Backward controls and cost model + +`rpt.backward` defaults to `on`; `rpt.enable` remains `off` by default. +`rpt.forward=off` enables a backward-only comparison. The backward producer +is sized from the root's estimated per-segment rows multiplied by the +accepted forward component's estimated selectivity, or by 1 when no forward +component was accepted. These estimates affect selection and sizing, not +SQL correctness. + +Both passes use the exchange model: + +```text +collection_ms_per_MiB × producer_processes × filter_MiB + + delivery_ms_per_MiB × consumer_processes × filter_MiB +``` + +`rpt.forward_ms_per_mib_part` (3.5) controls collection, and +`rpt.delivery_ms_per_mib` (0.8) controls delivery. The defaults come from the +2026-11-10 measurement on one host with three primaries. The existing +`rpt.forward_max_exchange_ms` and `rpt.forward_max_selectivity` names are +retained and apply to both passes. The exchange limit is per forward chain +or backward class; `-1` disables it. A selectivity threshold of 1 disables +the benefit gate. Unknown backward NDV is treated as a kept fraction of 1. +`rpt.max_backward_consumers` caps each class after the proof, preferring +larger estimated rows removed. + +Backward process counts come from the actual producer and consumer slices, +including direct dispatch, singleton gangs and parallel workers. Reducer +scans remain nonparallel; a worker's partial filter must never be treated as +a complete segment-local filter. + +## Validation and experiments + +Run native checks without a cluster: + +```sh +make -C gpcontrib/robust_predicate_transfer unitcheck surveycheck diagnosticcheck +``` + +With both extensions installed and preloaded on a running cluster: + +```sh +make -C gpcontrib/anser installcheck +make -C gpcontrib/robust_predicate_transfer installcheck +``` + +`rpt_backward` checks runtime shape, row reduction, gates and result equality +under both optimizers. `rpt_backward_faults` requires `gp_inject_fault` and +checks held delivery, consumer walk-away, oversized-producer cancellation, +query cancellation and a succeeding next command. Assertions use dynamic +SQL so changes to RPT settings cannot silently reuse an earlier plan. + +For a step-by-step run in an isolated test database: + +```sh +python3 gpcontrib/robust_predicate_transfer/scripts/diagnose_rpt.py \ + --suite backward --database rpt_test --output /tmp/rpt-backward +``` + +The runner saves SQL, client output, per-query decisions in `decisions.json`, +and watchdog/cancellation evidence. `--suite forward` and +`--suite backward_faults` run the other workloads. The legacy +`diagnose_forward.py` entry point remains supported. + +Before choosing production defaults, measure: + +| Experiment | Measurement | +| --- | --- | +| B0 | Largest-root eligibility and runtime refusal reasons on JOB/TPC-H/TPC-DS, under both optimizers | +| B1 | Delivery scaling with 1/2/4/8 consumers, 1/8/64 MiB filters and available cluster widths | +| B2 | Probe first-tuple time and consumer wait cost with backward off/on | +| B3 | No filters / Anser filters / RPT forward / RPT forward plus backward on snowflake and multi-fact queries | +| B4 | Per-query correctness and latency changes across JOB and TPC-DS SF100 | + +Use `scripts/survey.py` for B0; it plans queries without executing them and +reports both the original survey and largest-root runtime decisions. Run as +a superuser or a role granted EXECUTE on `rpt_transfer_plan(text)`. TPC-DS +14/23/24/39 contain two SELECTs each: split them into a/b variants or retain +two SELECT statements per file. Use the CTE form of TPC-H Q15. Expected query +counts are statements (103 for TPC-DS, 22 for TPC-H), not SQL file counts. + +A reducer-side backward fallback, bridge-chained backward filters, dynamic +abandonment and sharing delivered filters between consumers in one process +remain deferred. The reducer fallback would scan the largest table again; +it is not enabled or selected automatically. diff --git a/gpcontrib/robust_predicate_transfer/expected/rpt_backward.out b/gpcontrib/robust_predicate_transfer/expected/rpt_backward.out new file mode 100644 index 00000000000..3dee383a2e7 --- /dev/null +++ b/gpcontrib/robust_predicate_transfer/expected/rpt_backward.out @@ -0,0 +1,276 @@ +--------------------------------------------------------------------------- +-- +-- Licensed to the Apache Software Foundation (ASF) under one +-- or more contributor license agreements. See the NOTICE file +-- distributed with this work for additional information +-- regarding copyright ownership. The ASF licenses this file +-- to you under the Apache License, Version 2.0 (the +-- "License"); you may not use this file except in compliance +-- with the License. You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, +-- software distributed under the License is distributed on an +-- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +-- KIND, either express or implied. See the License for the +-- specific language governing permissions and limitations +-- under the License. +-- +-- Backward producers run in the main plan above forward consumers. Every +-- result query is dynamic SQL, so GUC changes cannot reuse an earlier plan. +CREATE EXTENSION robust_predicate_transfer; +SET anser.runtime_filter = off; +SET anser.timeout_ms = 600000; +-- This timeout covers the entire assertion DO block, under both optimizers. +SET statement_timeout = '120s'; +SET work_mem = '1MB'; +SET enable_nestloop = off; +SET enable_mergejoin = off; +SET join_collapse_limit = 1; +SET optimizer_join_order = query; +CREATE TABLE rpt_bw_root (k int, s int) DISTRIBUTED BY (k); +CREATE TABLE rpt_bw_member (k int, pad text) DISTRIBUTED RANDOMLY; +CREATE TABLE rpt_bw_dim (s int, v int) DISTRIBUTED RANDOMLY; +CREATE TABLE rpt_bw_bridge (k int, s int) DISTRIBUTED RANDOMLY; +CREATE TABLE rpt_bw_empty (k int) DISTRIBUTED RANDOMLY; +CREATE TABLE rpt_bw_local (k int) DISTRIBUTED BY (k); +CREATE TABLE rpt_bw_replica (k int) DISTRIBUTED REPLICATED; +CREATE TABLE rpt_bw_part (k int, pad text) DISTRIBUTED RANDOMLY + PARTITION BY RANGE (k) (START (0) END (2000) EVERY (1000)); +-- The root has more rows but a smaller hash footprint than the wide member. +-- Duplicate root keys also make its backward filter selective without a +-- root restriction that would change the largest-root choice. +INSERT INTO rpt_bw_root SELECT CASE WHEN i % 101 = 0 THEN NULL ELSE i % 10 END, + i % 100 FROM generate_series(1, 10000) i; +INSERT INTO rpt_bw_member SELECT i, repeat(md5(i::text), 100) + FROM generate_series(0, 999) i; +INSERT INTO rpt_bw_dim SELECT i, i FROM generate_series(0, 99) i; +INSERT INTO rpt_bw_bridge SELECT i, i % 100 FROM generate_series(0, 999) i; +INSERT INTO rpt_bw_local SELECT i FROM generate_series(0, 999) i; +INSERT INTO rpt_bw_replica SELECT i FROM generate_series(0, 999) i; +INSERT INTO rpt_bw_part SELECT * FROM rpt_bw_member; +ANALYZE rpt_bw_root; +ANALYZE rpt_bw_member; +ANALYZE rpt_bw_dim; +ANALYZE rpt_bw_bridge; +ANALYZE rpt_bw_empty; +ANALYZE rpt_bw_local; +ANALYZE rpt_bw_replica; +ANALYZE rpt_bw_part; +CREATE FUNCTION pg_temp.rpt_bw_result(q text, enabled boolean, backward boolean) +RETURNS text LANGUAGE plpgsql AS $$ +DECLARE + r text; +BEGIN + PERFORM set_config('rpt.enable', CASE WHEN enabled THEN 'on' ELSE 'off' END, true); + PERFORM set_config('rpt.backward', CASE WHEN backward THEN 'on' ELSE 'off' END, true); + q := format('SELECT coalesce(string_agg(t::text, %L ORDER BY t::text), %L) FROM (%s) t', + ';', '', q); + EXECUTE q INTO r; + PERFORM set_config('rpt.enable', 'on', true); + PERFORM set_config('rpt.backward', 'on', true); + RETURN r; +END +$$; +CREATE FUNCTION pg_temp.rpt_bw_plan(q text) RETURNS jsonb LANGUAGE plpgsql AS $$ +DECLARE + plan jsonb; +BEGIN + EXECUTE 'EXPLAIN (ANALYZE, FORMAT JSON) ' || q INTO plan; + RETURN plan; +END +$$; +CREATE FUNCTION pg_temp.rpt_bw_nodes(plan jsonb) +RETURNS SETOF jsonb LANGUAGE sql AS $$ + WITH RECURSIVE walk(node, in_init) AS ( + SELECT plan->0->'Plan', false + UNION ALL + SELECT child, in_init OR coalesce(child->>'Parent Relationship', '') = 'InitPlan' + FROM walk CROSS JOIN LATERAL jsonb_array_elements(coalesce(node->'Plans', '[]')) child + ) SELECT node FROM walk WHERE NOT in_init +$$; +CREATE FUNCTION pg_temp.rpt_bw_expect(q text, relation_name text, reason text) +RETURNS void LANGUAGE plpgsql AS $$ +DECLARE + dump text := rpt_transfer_plan(q); + member_rtindex text; +BEGIN + member_rtindex := substring(dump FROM ('vertex ([0-9]+) ' || relation_name || ' rows=')); + IF member_rtindex IS NULL OR NOT EXISTS ( + SELECT FROM regexp_split_to_table(dump, chr(10)) line + WHERE line LIKE ('backward consumer ' || member_rtindex || ' root=%') + AND line LIKE ('% decision=' || reason)) THEN + RAISE EXCEPTION 'missing backward decision % for %, optimizer=%: %', + reason, relation_name, current_setting('optimizer'), dump; + END IF; +END +$$; +DO $$ +DECLARE + opt text; + mode text; + q text; + dump text; + plan jsonb; + baseline text; + answer text; + cursor_name refcursor; + first_row record; + plain constant text := 'SELECT r.k, m.pad FROM rpt_bw_member m JOIN rpt_bw_root r ON m.k = r.k'; + chained constant text := 'SELECT r.k, m.pad FROM rpt_bw_member m JOIN + (rpt_bw_root r JOIN rpt_bw_dim d ON r.s = d.s) ON m.k = r.k WHERE d.v < 5'; +BEGIN + PERFORM set_config('rpt.enable', 'on', true); + PERFORM set_config('rpt.forward_min_root_rows', '0', true); + PERFORM set_config('rpt.forward_max_exchange_ms', '-1', true); + FOREACH opt IN ARRAY ARRAY['off', 'on'] LOOP + PERFORM set_config('optimizer', opt, true); + PERFORM set_config('rpt.forward_apply', 'root', true); + PERFORM set_config('rpt.forward', 'off', true); + PERFORM pg_temp.rpt_bw_expect(plain, 'm', 'inject'); + plan := pg_temp.rpt_bw_plan(plain); + IF NOT EXISTS (SELECT FROM pg_temp.rpt_bw_nodes(plan) n + WHERE n->>'Custom Plan Provider' = 'Anser Bloom Producer') OR + NOT EXISTS (SELECT FROM pg_temp.rpt_bw_nodes(plan) n + WHERE n->>'Custom Plan Provider' = 'Anser Bloom Consumer') OR + plan::text LIKE '%InitPlan%' THEN + RAISE EXCEPTION 'backward-only shape wrong (optimizer=%): %', opt, plan; + END IF; + IF (SELECT coalesce(sum(coalesce((n->>'Rows Removed by Bloom Filter')::float8, 0) + + coalesce((n->>'Rows Removed by Pushdown Runtime Filter')::float8, 0)), 0) + FROM pg_temp.rpt_bw_nodes(plan) n) <= 0 THEN + RAISE EXCEPTION 'backward consumer removed no rows (optimizer=%): %', opt, plan; + END IF; + -- The root producer must directly wrap the forward consumer stack. + PERFORM set_config('rpt.forward', 'on', true); + PERFORM pg_temp.rpt_bw_expect(chained, 'm', 'inject'); + plan := pg_temp.rpt_bw_plan(chained); + IF plan::text NOT LIKE '%InitPlan%' OR NOT EXISTS ( + SELECT FROM pg_temp.rpt_bw_nodes(plan) n + WHERE n->>'Custom Plan Provider' = 'Anser Bloom Producer' + AND n->'Plans'->0->>'Custom Plan Provider' = 'Anser Bloom Consumer') THEN + RAISE EXCEPTION 'backward producer is not above forward consumer (optimizer=%): %', opt, plan; + END IF; + -- Refusing forward must not suppress a useful backward filter. + PERFORM set_config('rpt.forward_min_root_rows', '1e12', true); + PERFORM pg_temp.rpt_bw_expect(chained, 'm', 'inject'); + IF rpt_transfer_plan(chained) NOT LIKE '%decision=root rows below rpt.forward_min_root_rows%' THEN + RAISE EXCEPTION 'forward gate fixture did not actually refuse'; + END IF; + PERFORM set_config('rpt.forward_min_root_rows', '0', true); + -- Runtime gates, with the same fixture known to inject above. + PERFORM set_config('rpt.forward_max_selectivity', '0', true); + PERFORM pg_temp.rpt_bw_expect(plain, 'm', + 'estimated member selectivity not below rpt.forward_max_selectivity'); + PERFORM set_config('rpt.forward_max_selectivity', '0.35', true); + PERFORM set_config('rpt.forward_max_exchange_ms', '0', true); + PERFORM pg_temp.rpt_bw_expect(plain, 'm', + 'estimated exchange cost above rpt.forward_max_exchange_ms'); + PERFORM set_config('rpt.forward_max_exchange_ms', '-1', true); + PERFORM pg_temp.rpt_bw_expect( + 'SELECT * FROM rpt_bw_root r JOIN rpt_bw_local l ON r.k = l.k', + 'l', 'local edge uses core runtime filtering'); + -- Narrow projection makes the smaller member the hash build side. + PERFORM pg_temp.rpt_bw_expect( + 'SELECT count(*) FROM rpt_bw_root r JOIN rpt_bw_member m ON r.k = m.k', + 'm', 'producer is not on a qualifying hash build side'); + -- LEFT's preserved root cannot be filtered by its nullable member. + -- Its member has no unprotected equivalence class for broadcasting. + PERFORM pg_temp.rpt_bw_expect( + 'SELECT r.k, m.pad FROM rpt_bw_root r LEFT JOIN rpt_bw_member m ON r.k = m.k', + 'm', 'edge has no unprotected equivalence class'); + dump := rpt_transfer_plan('SELECT * FROM rpt_bw_root r JOIN rpt_bw_bridge b + ON r.k = b.k JOIN rpt_bw_dim d ON b.s = d.s WHERE d.v < 5'); + IF dump NOT LIKE '%backward class %decision=bridge-led class is deferred%' THEN + RAISE EXCEPTION 'bridge-led backward class was not deferred (optimizer=%): %', opt, dump; + END IF; + -- Both joins consume the narrow root subtree on their build side. + -- Cross-relation join filters keep wide member columns in each join's + -- input, while only r.k survives into the next hash build. + PERFORM set_config('rpt.max_backward_consumers', '1', true); + dump := rpt_transfer_plan('SELECT count(*) FROM + (rpt_bw_member m1 JOIN rpt_bw_root r ON m1.k = r.k AND length(m1.pad) > r.k) + JOIN rpt_bw_member m2 ON m2.k = r.k AND length(m2.pad) > r.k'); + IF (SELECT count(*) FROM regexp_split_to_table(dump, chr(10)) line + WHERE line LIKE 'backward consumer % root=% decision=inject') <> 1 OR + dump NOT LIKE '%root=%decision=backward consumer cap exceeded%' THEN + RAISE EXCEPTION 'runtime backward fan-out cap not exercised (optimizer=%): %', opt, dump; + END IF; + PERFORM set_config('rpt.max_backward_consumers', '4', true); + -- Every runtime proof refusal is retained verbatim in the new dump; + -- real-plan proof cases also run in rpt_backward_eligibility. + dump := rpt_transfer_plan('SELECT r.k, (SELECT count(*) FROM rpt_bw_member m + JOIN rpt_bw_dim d ON m.k = d.s WHERE m.k < r.k AND random() < 1 OFFSET 0) + FROM rpt_bw_root r'); + IF dump NOT LIKE '%decision=scan inside a SubPlan%' THEN + RAISE EXCEPTION 'correlated producer was not refused (optimizer=%): %', opt, dump; + END IF; + FOREACH q IN ARRAY ARRAY[ + plain, chained, + 'SELECT count(*), sum(r.k) FROM rpt_bw_root r LEFT JOIN rpt_bw_member m ON r.k = m.k', + 'SELECT count(*), sum(r.k) FROM rpt_bw_member m RIGHT JOIN rpt_bw_root r ON r.k = m.k', + 'SELECT count(*), sum(r.k) FROM rpt_bw_root r FULL JOIN rpt_bw_member m ON r.k = m.k', + 'SELECT count(*), sum(r.k) FROM rpt_bw_root r WHERE EXISTS (SELECT FROM rpt_bw_member m WHERE m.k = r.k)', + 'SELECT count(*), sum(r.k) FROM rpt_bw_root r WHERE NOT EXISTS (SELECT FROM rpt_bw_member m WHERE m.k = r.k)', + 'SELECT count(*) FROM rpt_bw_root r JOIN rpt_bw_empty e ON r.k = e.k', + 'SELECT count(*) FROM rpt_bw_empty e JOIN rpt_bw_member m ON e.k = m.k', + 'SELECT count(*) FROM rpt_bw_root r JOIN rpt_bw_replica p ON r.k = p.k', + 'SELECT count(*) FROM rpt_bw_replica p JOIN rpt_bw_member m ON p.k = m.k', + 'SELECT count(*) FROM rpt_bw_root r JOIN rpt_bw_part p ON r.k = p.k', + 'SELECT r.k, m.pad FROM rpt_bw_member m JOIN rpt_bw_root r ON m.k = r.k LIMIT 1', + 'SELECT r.k, m.pad FROM rpt_bw_member m JOIN rpt_bw_root r ON m.k = r.k LIMIT 0' + ] LOOP + -- LIMIT 1 has no SQL ordering guarantee; compare a count instead. + IF q LIKE '%LIMIT 1' THEN q := 'SELECT count(*) FROM (' || q || ') limited'; END IF; + baseline := pg_temp.rpt_bw_result(q, false, false); + FOREACH mode IN ARRAY ARRAY['root', 'all'] LOOP + PERFORM set_config('rpt.forward_apply', mode, true); + IF pg_temp.rpt_bw_result(q, true, false) IS DISTINCT FROM baseline OR + pg_temp.rpt_bw_result(q, true, true) IS DISTINCT FROM baseline THEN + RAISE EXCEPTION 'backward result differs (optimizer=%, apply=%): %', opt, mode, q; + END IF; + END LOOP; + END LOOP; + PERFORM set_config('rpt.forward_apply', 'root', true); + q := 'SELECT count(*)::text || ''/'' || sum(length(m.pad || r.k::text))::text + FROM rpt_bw_member m JOIN rpt_bw_root r ON m.k = r.k'; + PERFORM pg_temp.rpt_bw_expect(q, 'm', 'inject'); + baseline := pg_temp.rpt_bw_result(q, false, false); + EXECUTE 'PREPARE rpt_bw_prepared AS ' || q; + FOR i IN 1..2 LOOP + EXECUTE 'EXECUTE rpt_bw_prepared' INTO answer; + IF '(' || answer || ')' IS DISTINCT FROM baseline THEN + RAISE EXCEPTION 'backward prepared answer differs'; + END IF; + END LOOP; + EXECUTE 'DEALLOCATE rpt_bw_prepared'; + cursor_name := 'rpt_bw_cursor'; + OPEN cursor_name FOR EXECUTE plain; + FETCH cursor_name INTO first_row; + IF NOT FOUND THEN RAISE EXCEPTION 'backward cursor returned no row'; END IF; + CLOSE cursor_name; + -- A new command must answer after the cursor squelches its producer. + IF pg_temp.rpt_bw_result(q, true, true) IS DISTINCT FROM baseline THEN + RAISE EXCEPTION 'next command after backward cursor'; + END IF; + PERFORM set_config('anser.runtime_filter', 'on', true); + dump := rpt_transfer_plan(chained); + IF dump LIKE '%key=rpt_b_%' OR dump NOT LIKE '%needs anser.enable=on and anser.runtime_filter=off%' THEN + RAISE EXCEPTION 'backward did not stand down for Anser: %', dump; + END IF; + PERFORM set_config('anser.runtime_filter', 'off', true); + END LOOP; +END +$$; +DROP TABLE rpt_bw_root, rpt_bw_member, rpt_bw_dim, rpt_bw_bridge, rpt_bw_empty, + rpt_bw_local, rpt_bw_replica, rpt_bw_part; +DROP EXTENSION robust_predicate_transfer; +RESET anser.runtime_filter; +RESET anser.timeout_ms; +RESET statement_timeout; +RESET enable_nestloop; +RESET enable_mergejoin; +RESET work_mem; +RESET join_collapse_limit; +RESET optimizer_join_order; diff --git a/gpcontrib/robust_predicate_transfer/expected/rpt_backward_eligibility.out b/gpcontrib/robust_predicate_transfer/expected/rpt_backward_eligibility.out index db9aa1e785d..292641d3eef 100644 --- a/gpcontrib/robust_predicate_transfer/expected/rpt_backward_eligibility.out +++ b/gpcontrib/robust_predicate_transfer/expected/rpt_backward_eligibility.out @@ -56,17 +56,17 @@ CREATE FUNCTION pg_temp.rpt_expect(q text, producer text, consumer text, RETURNS text LANGUAGE plpgsql AS $$ DECLARE dump text := rpt_transfer_plan(q); - producer_id text; - consumer_id text; + producer_rtindex text; + consumer_rtindex text; decision text; BEGIN - producer_id := substring(dump FROM ('vertex ([0-9]+) ' || producer || ' rows=')); - consumer_id := substring(dump FROM ('vertex ([0-9]+) ' || consumer || ' rows=')); - IF producer_id IS NULL OR consumer_id IS NULL THEN + producer_rtindex := substring(dump FROM ('vertex ([0-9]+) ' || producer || ' rows=')); + consumer_rtindex := substring(dump FROM ('vertex ([0-9]+) ' || consumer || ' rows=')); + IF producer_rtindex IS NULL OR consumer_rtindex IS NULL THEN RAISE EXCEPTION 'missing fixture scans % / %: %', producer, consumer, dump; END IF; SELECT line INTO decision FROM regexp_split_to_table(dump, chr(10)) line - WHERE line LIKE ('backward consumer ' || consumer_id || ' producer ' || producer_id || ' %') + WHERE line LIKE ('backward consumer ' || consumer_rtindex || ' producer ' || producer_rtindex || ' %') AND substring(line FROM ' reason=(.*)$') = ANY(reasons) AND (accepted IS NULL OR line LIKE ('% eligible=' || CASE WHEN accepted THEN 'yes' ELSE 'no' END || ' %')) LIMIT 1; @@ -102,7 +102,7 @@ DECLARE path text[]; producer_path text[]; split_at integer; - producer_id text; + producer_rtindex text; correlated text; BEGIN FOREACH opt IN ARRAY ARRAY['off', 'on'] LOOP @@ -201,13 +201,13 @@ BEGIN END IF; PERFORM set_config('rpt.max_backward_consumers', '1', true); dump := rpt_transfer_plan('SELECT * FROM rpt_elig_root JOIN rpt_elig_member ON rpt_elig_root.k = rpt_elig_member.k JOIN rpt_elig_member2 ON rpt_elig_root.k = rpt_elig_member2.k JOIN rpt_elig_member3 ON rpt_elig_root.k = rpt_elig_member3.k WHERE rpt_elig_root.v < 100'); - producer_id := substring(dump FROM 'vertex ([0-9]+) rpt_elig_root rows='); - IF producer_id IS NULL OR + producer_rtindex := substring(dump FROM 'vertex ([0-9]+) rpt_elig_root rows='); + IF producer_rtindex IS NULL OR NOT EXISTS (SELECT FROM regexp_split_to_table(dump, chr(10)) line - WHERE line LIKE ('backward consumer % producer ' || producer_id || ' %') + WHERE line LIKE ('backward consumer % producer ' || producer_rtindex || ' %') AND line LIKE '% reason=backward consumer cap exceeded') OR (SELECT count(*) FROM regexp_split_to_table(dump, chr(10)) line - WHERE line LIKE ('backward consumer % producer ' || producer_id || ' %') + WHERE line LIKE ('backward consumer % producer ' || producer_rtindex || ' %') AND line LIKE '% eligible=yes %') <> 1 THEN RAISE EXCEPTION 'fan-out cap fixture did not retain exactly one consumer (optimizer=%): %', opt, dump; END IF; diff --git a/gpcontrib/robust_predicate_transfer/expected/rpt_backward_faults.out b/gpcontrib/robust_predicate_transfer/expected/rpt_backward_faults.out new file mode 100644 index 00000000000..7bd6fd54b59 --- /dev/null +++ b/gpcontrib/robust_predicate_transfer/expected/rpt_backward_faults.out @@ -0,0 +1,209 @@ +--------------------------------------------------------------------------- +-- +-- Licensed to the Apache Software Foundation (ASF) under one +-- or more contributor license agreements. See the NOTICE file +-- distributed with this work for additional information +-- regarding copyright ownership. The ASF licenses this file +-- to you under the Apache License, Version 2.0 (the +-- "License"); you may not use this file except in compliance +-- with the License. You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, +-- software distributed under the License is distributed on an +-- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +-- KIND, either express or implied. See the License for the +-- specific language governing permissions and limitations +-- under the License. +-- +-- Backward-only transport failures. Every armed fault must actually fire. +-- start_ignore +CREATE EXTENSION IF NOT EXISTS gp_inject_fault; +CREATE EXTENSION IF NOT EXISTS anser_test; +-- end_ignore +CREATE EXTENSION robust_predicate_transfer; +SET anser.runtime_filter = off; +SET rpt.enable = on; +SET rpt.forward = off; +SET rpt.backward = on; +SET rpt.forward_max_exchange_ms = -1; +SET statement_timeout = '120s'; +SET work_mem = '1MB'; +SET enable_nestloop = off; +SET enable_mergejoin = off; +CREATE TABLE rpt_bwf_root (k int) DISTRIBUTED BY (k); +CREATE TABLE rpt_bwf_member (k int, pad text) DISTRIBUTED RANDOMLY; +INSERT INTO rpt_bwf_root SELECT i % 10 FROM generate_series(1, 10000) i; +INSERT INTO rpt_bwf_member SELECT i, repeat(md5(i::text), 100) FROM generate_series(0, 999) i; +ANALYZE rpt_bwf_root; +ANALYZE rpt_bwf_member; +CREATE FUNCTION pg_temp.rpt_bwf_query() RETURNS text LANGUAGE sql AS $$ + SELECT 'SELECT count(*)::text || ''/'' || sum(length(m.pad || r.k::text))::text + FROM rpt_bwf_member m JOIN rpt_bwf_root r ON m.k = r.k' +$$; +CREATE FUNCTION pg_temp.rpt_bwf_run(enabled boolean) RETURNS text LANGUAGE plpgsql AS $$ +DECLARE + q text := pg_temp.rpt_bwf_query(); + r text; +BEGIN + PERFORM set_config('rpt.enable', CASE WHEN enabled THEN 'on' ELSE 'off' END, true); + EXECUTE q INTO r; + PERFORM set_config('rpt.enable', 'on', true); + RETURN r; +END +$$; +-- How often a fault fired, so a case cannot pass without exercising it. +CREATE FUNCTION pg_temp.rpt_bwf_hits(fault text, coordinator boolean) +RETURNS int LANGUAGE sql AS $$ + SELECT coalesce(sum(substring(gp_inject_fault(fault, 'status', dbid) + FROM 'num times hit:''([0-9]+)''')::int), 0)::int + FROM gp_segment_configuration + WHERE role = 'p' AND (content = -1) = coordinator +$$; +-- First occurrence only; on the coordinator a 'sleep' fault sleeps 1 s. +CREATE FUNCTION pg_temp.rpt_bwf_fault(fault text, action text, coordinator boolean) +RETURNS void LANGUAGE plpgsql AS $$ +BEGIN + IF action = 'reset' THEN + PERFORM gp_inject_fault(fault, 'reset', dbid) + FROM gp_segment_configuration + WHERE role = 'p' AND (content = -1) = coordinator; + ELSE + PERFORM gp_inject_fault(fault, action, '', '', '', 1, 1, + CASE WHEN coordinator THEN 1 ELSE 0 END, dbid) + FROM gp_segment_configuration + WHERE role = 'p' AND (content = -1) = coordinator; + END IF; +END +$$; +DO $$ +DECLARE + opt text; + baseline text; + answer text; + plan jsonb; + deliveries_before bigint; +BEGIN + FOREACH opt IN ARRAY ARRAY['off', 'on'] LOOP + PERFORM set_config('optimizer', opt, true); + baseline := pg_temp.rpt_bwf_run(false); + IF rpt_transfer_plan(pg_temp.rpt_bwf_query()) NOT LIKE '%backward class %decision=inject%' THEN + RAISE EXCEPTION 'backward fault fixture not selected (optimizer=%)', opt; + END IF; + PERFORM set_config('anser.timeout_ms', '100', true); + PERFORM pg_temp.rpt_bwf_fault('anser_before_deliver', 'sleep', true); + answer := pg_temp.rpt_bwf_run(true); + IF pg_temp.rpt_bwf_hits('anser_before_deliver', true) = 0 THEN + RAISE EXCEPTION 'backward delivery sleep never fired'; + END IF; + PERFORM pg_temp.rpt_bwf_fault('anser_before_deliver', 'reset', true); + IF answer IS DISTINCT FROM baseline OR pg_temp.rpt_bwf_run(true) IS DISTINCT FROM baseline THEN + RAISE EXCEPTION 'backward timeout or next command changed results'; + END IF; + PERFORM set_config('anser.timeout_ms', '1000', true); + PERFORM set_config('anser.debug_delivery_chunk_bytes', '8192', true); + PERFORM set_config('anser.debug_min_filter_bytes', '8388608', true); + PERFORM pg_temp.rpt_bwf_fault('anser_after_chunk', 'skip', false); + answer := pg_temp.rpt_bwf_run(true); + IF pg_temp.rpt_bwf_hits('anser_after_chunk', false) = 0 THEN + RAISE EXCEPTION 'backward consumer walk-away never fired'; + END IF; + PERFORM pg_temp.rpt_bwf_fault('anser_after_chunk', 'reset', false); + IF answer IS DISTINCT FROM baseline OR pg_temp.rpt_bwf_run(true) IS DISTINCT FROM baseline THEN + RAISE EXCEPTION 'backward walk-away or next command changed results'; + END IF; + -- Plan with an 8 MiB filter, then lower the runtime payload cap. + -- EXPLAIN EXECUTE forces the generic plan before the cap changes; + -- the second EXPLAIN proves the cached producer was not resized away. + EXECUTE 'PREPARE rpt_bwf_oversized AS ' || pg_temp.rpt_bwf_query(); + EXECUTE 'EXPLAIN (FORMAT JSON) EXECUTE rpt_bwf_oversized' INTO plan; + IF plan::text NOT LIKE '%8388608%' THEN + RAISE EXCEPTION 'oversized producer fixture was not planned: %', plan; + END IF; + PERFORM set_config('anser.max_info_size', '2097152', true); + EXECUTE 'EXPLAIN (FORMAT JSON) EXECUTE rpt_bwf_oversized' INTO plan; + IF plan::text NOT LIKE '%8388608%' THEN + RAISE EXCEPTION 'oversized producer fixture was replanned: %', plan; + END IF; + PERFORM pg_temp.rpt_bwf_fault('anser_before_deliver', 'skip', true); + deliveries_before := anser_test_deliveries_completed(); + EXECUTE 'EXECUTE rpt_bwf_oversized' INTO answer; + -- The fault proves delivery was attempted; only the counter can + -- distinguish a cancellation from a successfully delivered filter. + IF anser_test_deliveries_completed() <> deliveries_before THEN + RAISE EXCEPTION 'oversized producer delivered a filter instead of cancelling'; + END IF; + IF pg_temp.rpt_bwf_hits('anser_before_deliver', true) = 0 THEN + RAISE EXCEPTION 'oversized producer never delivered its cancellation'; + END IF; + PERFORM pg_temp.rpt_bwf_fault('anser_before_deliver', 'reset', true); + EXECUTE 'DEALLOCATE rpt_bwf_oversized'; + PERFORM set_config('anser.max_info_size', '68157440', true); + PERFORM set_config('anser.debug_min_filter_bytes', '0', true); + PERFORM set_config('anser.debug_delivery_chunk_bytes', '0', true); + IF answer IS DISTINCT FROM baseline OR pg_temp.rpt_bwf_run(true) IS DISTINCT FROM baseline THEN + RAISE EXCEPTION 'backward producer cancellation changed results'; + END IF; + END LOOP; +END +$$; +CREATE FUNCTION pg_temp.rpt_bwf_cancel() RETURNS void LANGUAGE plpgsql AS $$ +DECLARE + cancelled boolean := false; +BEGIN + BEGIN + PERFORM pg_temp.rpt_bwf_run(true); + EXCEPTION WHEN query_canceled THEN + cancelled := true; + END; + IF NOT cancelled THEN RAISE EXCEPTION 'backward wait was not cancelled'; END IF; +END +$$; +-- Separate top-level commands arm statement_timeout for each optimizer. +SET optimizer = off; +DO $$ BEGIN PERFORM pg_temp.rpt_bwf_fault('anser_before_deliver', 'sleep', true); END $$; +SET anser.timeout_ms = 600000; +SET statement_timeout = '300ms'; +DO $$ BEGIN PERFORM pg_temp.rpt_bwf_cancel(); END $$; +SET statement_timeout = '120s'; +DO $$ +BEGIN + IF pg_temp.rpt_bwf_hits('anser_before_deliver', true) = 0 THEN + RAISE EXCEPTION 'cancel test never reached backward delivery'; + END IF; + PERFORM pg_temp.rpt_bwf_fault('anser_before_deliver', 'reset', true); + IF pg_temp.rpt_bwf_run(true) IS DISTINCT FROM pg_temp.rpt_bwf_run(false) THEN + RAISE EXCEPTION 'session failed after backward cancellation'; + END IF; +END +$$; +SET optimizer = on; +DO $$ BEGIN PERFORM pg_temp.rpt_bwf_fault('anser_before_deliver', 'sleep', true); END $$; +SET statement_timeout = '300ms'; +DO $$ BEGIN PERFORM pg_temp.rpt_bwf_cancel(); END $$; +SET statement_timeout = '120s'; +DO $$ +BEGIN + IF pg_temp.rpt_bwf_hits('anser_before_deliver', true) = 0 THEN + RAISE EXCEPTION 'ORCA cancel test never reached backward delivery'; + END IF; + PERFORM pg_temp.rpt_bwf_fault('anser_before_deliver', 'reset', true); + IF pg_temp.rpt_bwf_run(true) IS DISTINCT FROM pg_temp.rpt_bwf_run(false) THEN + RAISE EXCEPTION 'ORCA session failed after backward cancellation'; + END IF; +END +$$; +DROP TABLE rpt_bwf_root, rpt_bwf_member; +DROP EXTENSION robust_predicate_transfer; +RESET rpt.enable; +RESET rpt.forward; +RESET rpt.backward; +RESET rpt.forward_max_exchange_ms; +RESET anser.runtime_filter; +RESET anser.timeout_ms; +RESET optimizer; +RESET statement_timeout; +RESET enable_nestloop; +RESET enable_mergejoin; +RESET work_mem; diff --git a/gpcontrib/robust_predicate_transfer/expected/rpt_forward.out b/gpcontrib/robust_predicate_transfer/expected/rpt_forward.out index 83d31b206ac..1743ebaf0e2 100644 --- a/gpcontrib/robust_predicate_transfer/expected/rpt_forward.out +++ b/gpcontrib/robust_predicate_transfer/expected/rpt_forward.out @@ -24,6 +24,7 @@ -- Faults live in rpt_forward_faults. CREATE EXTENSION robust_predicate_transfer; SET anser.runtime_filter = off; +SET rpt.backward = off; SET statement_timeout = '120s'; -- Star: two dimensions on two different fact keys (two hops into the root). CREATE TABLE rpt_fwd_fact (id int, d1 int, d2 int, v int) DISTRIBUTED RANDOMLY; @@ -350,4 +351,5 @@ DROP TABLE rpt_fwd_fact, rpt_fwd_d1, rpt_fwd_d2, rpt_fwd_empty, rpt_fwd_dl, rpt_fwd_dr, rpt_fwd_sub; DROP EXTENSION robust_predicate_transfer; RESET anser.runtime_filter; +RESET rpt.backward; RESET statement_timeout; diff --git a/gpcontrib/robust_predicate_transfer/expected/rpt_forward_faults.out b/gpcontrib/robust_predicate_transfer/expected/rpt_forward_faults.out index 2a826f2f3b1..8da7e0d5e2f 100644 --- a/gpcontrib/robust_predicate_transfer/expected/rpt_forward_faults.out +++ b/gpcontrib/robust_predicate_transfer/expected/rpt_forward_faults.out @@ -33,6 +33,7 @@ CREATE EXTENSION IF NOT EXISTS gp_inject_fault; CREATE EXTENSION robust_predicate_transfer; SET optimizer = off; SET anser.runtime_filter = off; +SET rpt.backward = off; SET anser.debug = on; SET rpt.enable = on; SET rpt.forward_min_root_rows = 0; @@ -175,5 +176,6 @@ RESET rpt.forward_min_root_rows; RESET rpt.forward_max_exchange_ms; RESET anser.debug; RESET anser.runtime_filter; +RESET rpt.backward; RESET optimizer; RESET statement_timeout; diff --git a/gpcontrib/robust_predicate_transfer/expected/rpt_graph.out b/gpcontrib/robust_predicate_transfer/expected/rpt_graph.out index 36d3316d525..3e2fe5c29a9 100644 --- a/gpcontrib/robust_predicate_transfer/expected/rpt_graph.out +++ b/gpcontrib/robust_predicate_transfer/expected/rpt_graph.out @@ -23,10 +23,10 @@ DECLARE opt text; dump text; q text; - distributed_id text; - replicated_id text; + distributed_rtindex text; + replicated_rtindex text; preserved_name text; - preserved_id text; + preserved_rtindex text; expected_members text; member_count int; BEGIN @@ -43,12 +43,12 @@ BEGIN 'SELECT * FROM rpt_a JOIN rpt_b ON rpt_a.a = rpt_b.a JOIN rpt_c ON rpt_b.a = rpt_c.a AND rpt_a.a = rpt_c.a WHERE rpt_a.b > 50' ] LOOP dump := rpt_transfer_plan(q); - distributed_id := substring(dump FROM 'vertex ([0-9]+) rpt_a rows='); + distributed_rtindex := substring(dump FROM 'vertex ([0-9]+) rpt_a rows='); SELECT count(*), string_agg(parts[1], ',' ORDER BY parts[1]::int) INTO member_count, expected_members FROM regexp_matches(dump, 'vertex ([0-9]+) (rpt_a|rpt_b|rpt_c) rows=', 'g') AS vertices(parts); - IF distributed_id IS NULL OR member_count <> 3 OR - dump NOT LIKE ('%members=' || expected_members || ' leader=' || distributed_id || chr(10) || '%') THEN + IF distributed_rtindex IS NULL OR member_count <> 3 OR + dump NOT LIKE ('%members=' || expected_members || ' leader=' || distributed_rtindex || chr(10) || '%') THEN RAISE EXCEPTION 'class/root mismatch (optimizer=%): %', opt, dump; END IF; IF dump NOT LIKE ('%reducer order:' || chr(10) || '%') THEN @@ -67,11 +67,11 @@ BEGIN -- Replicated -> hash is local; the reverse broadcast stays global. dump := rpt_transfer_plan('SELECT * FROM rpt_a JOIN rpt_r ON rpt_a.a = rpt_r.a WHERE rpt_a.b > 0 AND rpt_r.a > 0'); -- Resolve table names from vertex records: ORCA can reorder rtindexes. - distributed_id := substring(dump FROM 'vertex ([0-9]+) rpt_a rows='); - replicated_id := substring(dump FROM 'vertex ([0-9]+) rpt_r rows='); - IF distributed_id IS NULL OR replicated_id IS NULL OR - dump NOT LIKE ('%forward hop ' || replicated_id || '->' || distributed_id || ' merge=local%') OR - dump NOT LIKE ('%backward hop ' || distributed_id || '->' || replicated_id || ' merge=global%') THEN + distributed_rtindex := substring(dump FROM 'vertex ([0-9]+) rpt_a rows='); + replicated_rtindex := substring(dump FROM 'vertex ([0-9]+) rpt_r rows='); + IF distributed_rtindex IS NULL OR replicated_rtindex IS NULL OR + dump NOT LIKE ('%forward hop ' || replicated_rtindex || '->' || distributed_rtindex || ' merge=local%') OR + dump NOT LIKE ('%backward hop ' || distributed_rtindex || '->' || replicated_rtindex || ' merge=global%') THEN RAISE EXCEPTION 'replicated direction mismatch (optimizer=%): %', opt, dump; END IF; dump := rpt_transfer_plan('SELECT * FROM rpt_a JOIN rpt_d ON rpt_a.a = rpt_d.x AND rpt_a.b = rpt_d.y WHERE rpt_a.b > 50'); @@ -91,12 +91,12 @@ BEGIN ) AS cases(query, preserved) LOOP dump := rpt_transfer_plan(q); - preserved_id := substring(dump FROM ('vertex ([0-9]+) ' || preserved_name || ' rows=')); - IF preserved_id IS NULL THEN + preserved_rtindex := substring(dump FROM ('vertex ([0-9]+) ' || preserved_name || ' rows=')); + IF preserved_rtindex IS NULL THEN RAISE EXCEPTION 'missing preserved table % (optimizer=%): %', preserved_name, opt, dump; END IF; IF EXISTS (SELECT FROM regexp_split_to_table(dump, chr(10)) line - WHERE line LIKE ('vertex ' || preserved_id || ' %') + WHERE line LIKE ('vertex ' || preserved_rtindex || ' %') AND line LIKE '%apply from%') THEN RAISE EXCEPTION 'preserved side reduced: %', dump; END IF; diff --git a/gpcontrib/robust_predicate_transfer/include/rpt.h b/gpcontrib/robust_predicate_transfer/include/rpt.h index a6e3056ed33..ef51e22af17 100644 --- a/gpcontrib/robust_predicate_transfer/include/rpt.h +++ b/gpcontrib/robust_predicate_transfer/include/rpt.h @@ -144,10 +144,9 @@ operator==(const RptKey &a, const RptKey &b) */ struct RptVertexInfo { - int id; /* rtindex of the scan, the vertex identity */ + int rtindex; /* range-table index of the scan */ Plan *scan; /* the base scan node */ Oid relid = InvalidOid; - std::string relname; /* RTE alias, for dumps */ int64 cardinality; /* scan->plan_rows */ bool filtered; /* the scan carries restriction quals */ bool intermediate; /* touches >1 equivalence class */ @@ -172,8 +171,8 @@ struct RptVertexInfo */ struct RptEdgeInfo { - int left_v; - int right_v; /* vertex ids (RptVertexInfo.id) */ + int left_rtindex; + int right_rtindex; /* range-table indexes (RptVertexInfo.rtindex) */ Oid key_type; /* the equality operator's input type */ std::vector > clauses; bool protect_left; @@ -188,8 +187,8 @@ struct RptEdgeInfo */ struct RptKeyGroup { - std::set members; - int leader_id; /* -1 until registered */ + std::set member_rtindexes; + int leader_rtindex; /* -1 until registered */ RptKey leader_key; Oid key_type; }; @@ -197,7 +196,7 @@ struct RptKeyGroup /* Ports dag.hpp's GraphEdge: one transfer edge of the built plan. */ struct RptGraphEdge { - int destination; + int other_rtindex; /* producer for in-edges, consumer for out-edges */ std::vector left_keys; /* keys on the smaller table's side */ std::vector right_keys; /* keys on the larger table's side */ std::vector key_types; @@ -207,7 +206,7 @@ struct RptGraphEdge /* Ports dag.hpp's GraphNode: one vertex's full transfer plan. */ struct RptGraphNode { - int id; + int rtindex; int32_t cardinality_order; /* Prim insertion order (0 = root) */ struct @@ -223,14 +222,16 @@ struct RptGraphNode struct RptBackwardConsumer { RptKey class_key = {0, 0}; - int producer = 0; - int consumer = 0; + RptKey producer_key = {0, 0}; + RptKey consumer_key = {0, 0}; + int producer_rtindex = 0; + int consumer_rtindex = 0; Plan *producer_parent = NULL; Plan *consumer_parent = NULL; AttrNumber producer_attno = 0; AttrNumber consumer_attno = 0; - int producer_slice = -1; - int consumer_slice = -1; + int producer_slice_index = -1; + int consumer_slice_index = -1; bool local = false; bool rule9 = false; bool strict_eligible = false; @@ -248,8 +249,9 @@ struct RptTransferMgr { /* rptregion.cpp fills these from the PlannedStmt. */ std::vector vertices; - std::map by_rti; /* vertex id -> index in vertices */ - /* vertex id -> (vertex id -> edge) */ + /* rtindex -> vector index in vertices */ + std::map vertex_index_by_rtindex; + /* rtindex -> (rtindex -> edge) */ std::map > neighbor_matrix; /* rptgraph.cpp: classes and classification. */ @@ -258,33 +260,33 @@ struct RptTransferMgr /* rptplan.cpp: the transfer plan. */ std::map graph; - std::set roots; - std::set bridges; - std::vector reducer_order; - std::vector transfer_order; /* Prim insertion order of vertex ids */ + std::set root_rtindexes; + std::set bridge_rtindexes; + std::vector reducer_order_rtindexes; + std::vector transfer_order_rtindexes; /* Prim insertion order of range-table indexes */ /* Eligibility for blocking global backward consumers only. Every global * producer must be on a hash join's build side in a different slice from * the probe-side consumer. Local edges never wait and do not participate; - * a vertex with no global in-edges has consumer_ok=false. */ - std::map consumer_ok; + * a vertex with no global in-edges has consumer_ok_by_rtindex=false. */ + std::map consumer_ok_by_rtindex; std::vector backward_consumers; }; /* * Forward reducer. A channel is one forward transfer edge: the - * reducer scan of `from` builds a bloom filter on from_key, and every scan of - * `to` (in the reducer, and the root's scan in the main plan) applies it on - * to_key. Only the first clause of a multi-key edge is transferred; one key - * of a conjunctive equality still over-approximates, so this loses + * reducer scan of source_rtindex builds a bloom filter on source_key, and + * every scan of target_rtindex (in the reducer or main plan) applies it on + * target_key. Only the first clause of a multi-key edge is transferred; + * one key of a conjunctive equality still over-approximates, so this loses * selectivity, never rows. */ struct RptForwardChannel { - int from = 0; - int to = 0; - RptKey from_key = {0, 0}; - RptKey to_key = {0, 0}; + int source_rtindex = 0; + int target_rtindex = 0; + RptKey source_key = {0, 0}; + RptKey target_key = {0, 0}; bool local = false; /* policy-local (rptplan.cpp, rpt_local_hop) */ bool in_process = false; /* local and consumed only inside the reducer: * handed over in-process, no Anser channel */ @@ -299,8 +301,8 @@ struct RptForwardChannel /* One root's forward chain and the decision about it. */ struct RptForwardComponent { - int root = 0; - std::vector chain; /* reducer scans, producers before consumers */ + int root_rtindex = 0; + std::vector chain_rtindexes; /* reducer scans, producers before consumers */ std::vector channels; double est_cost_ms = 0; /* sum of hop estimates */ double est_selectivity = 1; /* estimated fraction of root rows kept */ @@ -311,10 +313,12 @@ struct RptForwardComponent struct RptForwardParams { int width = 0; /* reducer gang size: producers per channel */ + double delivery_ms_per_mib = 0; double ms_per_mib_part = 0; /* c in c * n_producers * filter_MiB */ double max_exchange_ms = 0; /* component budget; < 0 disables the limit */ double min_root_rows = 0; /* root scan plan_rows below this: no reducer */ double max_selectivity = 1; /* lambda; >= 1 disables the check */ + std::map main_consumer_processes; /* rtindex -> main scan width */ bool apply_all = false; /* chain scans of the main plan consume too */ }; @@ -339,6 +343,43 @@ typedef double (*RptNdvFn) (const RptVertexInfo &vertex, AttrNumber attno); typedef bool (*RptFilterSizeFn) (double est_rows, int64 *total_elems, int64 *max_payload, int64 *planned_bytes); +/* Backward selection preserves the survey's per-edge proof and refusal. */ +struct RptBackwardTarget +{ + RptBackwardConsumer proof; + int processes = 0; + double estimated_rows_removed = -1; + bool accepted = false; + std::string reason; +}; + +struct RptBackwardClass +{ + RptKey class_key = {0, 0}; + int producer_rtindex = 0; + RptKey producer_key = {0, 0}; + int producer_processes = 0; + int64 total_elems = 0; + int64 max_payload = 0; + int64 planned_bytes = 0; + uint32 condition_id = 0; + std::string condition_key; + double estimated_producer_rows = 0; + double est_cost_ms = 0; + bool accepted = false; + std::string reason; + std::vector consumers; +}; + +extern void RptSelectBackward(const RptTransferMgr &schedule, + const std::vector &forward, + const RptForwardParams ¶ms, + const std::map &slice_processes, + RptFilterSizeFn size_fn, + std::vector *out); +extern void RptDumpBackward(const std::vector &classes, + std::string *out); + /* rptforwardplan.cpp: pure selection over a forward schedule. */ extern void RptSelectForward(const RptTransferMgr &forward, const RptForwardParams ¶ms, @@ -351,13 +392,14 @@ extern void RptDumpForward(const std::vector &components, /* rptforward.cpp: the planner-hook pass. Returns a new statement, or stmt * itself when nothing is injected; *trace receives the decision dump. May * throw; the caller fails open. */ -extern PlannedStmt *RptInjectForward(PlannedStmt *stmt, bool materialize, +extern PlannedStmt *RptInject(PlannedStmt *stmt, bool materialize, std::string *trace); /* Forward reducer GUCs (rptinit.cpp). */ extern bool rpt_forward; extern int rpt_forward_apply; /* RPT_FORWARD_APPLY_* */ extern double rpt_forward_ms_per_mib_part; +extern double rpt_delivery_ms_per_mib; extern double rpt_forward_max_exchange_ms; extern double rpt_forward_min_root_rows; extern double rpt_forward_max_selectivity; @@ -382,6 +424,7 @@ extern void RptBuildForwardPlan(const RptTransferMgr *analysis, extern void RptAttachReducerInitPlan(PlannedStmt *stmt, List *children, int width); extern void RptCheckBackwardConsumers(PlannedStmt *stmt, RptTransferMgr *mgr); extern double RptEstimateDistinct(const RptVertexInfo &vertex, AttrNumber attno); -extern void RptDumpTransferPlan(const RptTransferMgr *mgr, StringInfo out); +extern void RptDumpTransferPlan(const PlannedStmt *stmt, + const RptTransferMgr *mgr, StringInfo out); #endif /* RPT_H */ diff --git a/gpcontrib/robust_predicate_transfer/include/rptinject.h b/gpcontrib/robust_predicate_transfer/include/rptinject.h new file mode 100644 index 00000000000..cffdd73dec7 --- /dev/null +++ b/gpcontrib/robust_predicate_transfer/include/rptinject.h @@ -0,0 +1,50 @@ +/*------------------------------------------------------------------------- + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * rptinject.h + * Shared scan-stack state and backward injection interface. + * + *------------------------------------------------------------------------- + */ +#ifndef RPTINJECT_H +#define RPTINJECT_H + +#include "rpt.h" + +/* One allocator and one relink point per original scan, shared by both passes. */ +struct RptInjectionState +{ + uint32 next_condition_id = 0; + int next_plan_node_id = 0; + std::map parents; + std::map tops; +}; + +extern AttrNumber RptScanKeyPosition(Plan *scan, const RptKey &key); +extern bool RptStackMatches(const RptInjectionState &state, Plan *scan, + Plan *expected_parent); +extern void RptRelinkStack(RptInjectionState *state, Plan *scan, Plan *top); +extern void RptInjectBackward(PlannedStmt *stmt, const RptTransferMgr &schedule, + const std::vector &forward, + const RptForwardParams ¶ms, + RptFilterSizeFn size_fn, bool materialize, + RptInjectionState *state, std::string *trace, + bool *injected); + +#endif /* RPTINJECT_H */ diff --git a/gpcontrib/robust_predicate_transfer/scripts/diagnose_forward.py b/gpcontrib/robust_predicate_transfer/scripts/diagnose_forward.py index e5052528141..5436a4b1d3e 100644 --- a/gpcontrib/robust_predicate_transfer/scripts/diagnose_forward.py +++ b/gpcontrib/robust_predicate_transfer/scripts/diagnose_forward.py @@ -16,132 +16,8 @@ # specific language governing permissions and limitations # under the License. -"""Run the forward regression with step markers and an independent watchdog. - -Use an isolated test database with both libraries installed and preloaded. -The runner never terminates a server backend or restarts the cluster. -""" -import argparse -import json -import os -from pathlib import Path -import subprocess -import time -import uuid - - -def instrument_sql(source): - """Keep regression assertions, but identify every dynamic query first.""" - source = source.replace("rpt_transfer_plan(", "pg_temp.rpt_diag_transfer_plan(") - anchor = "DO $$\nDECLARE\n opt text;" - if source.count(anchor) != 1: - raise ValueError("forward regression assertion block changed") - helper = """CREATE FUNCTION pg_temp.rpt_diag_transfer_plan(q text) RETURNS text -LANGUAGE plpgsql AS $diag$ -BEGIN - RAISE NOTICE 'RPT STEP transfer_plan SQL=%', q; - RETURN public.rpt_transfer_plan(q); -END -$diag$; -""" - source = source.replace(anchor, helper + anchor) - edits = { - " EXECUTE format('EXPLAIN (%s FORMAT JSON) %s',": - " RAISE NOTICE 'RPT STEP explain analyze=% SQL=%', do_analyze, q;\n", - " EXECUTE format('SELECT string_agg(t::text, %L ORDER BY t::text) FROM (%s) t',": - " RAISE NOTICE 'RPT STEP results rpt=% SQL=%', rpt, q;\n", - " EXECUTE 'PREPARE rpt_fwd_prep AS ' || star;": - " RAISE NOTICE 'RPT STEP prepare optimizer=%', opt;\n", - " EXECUTE 'EXECUTE rpt_fwd_prep' INTO prep_n, prep_s;": - " RAISE NOTICE 'RPT STEP execute prepared optimizer=% run=%', opt, i;\n", - " FOR r IN SELECT f.v FROM rpt_fwd_fact f JOIN rpt_fwd_d1": - " RAISE NOTICE 'RPT STEP SPI dimension scan';\n", - " FOR r IN SELECT f.v FROM rpt_fwd_fact f JOIN rpt_fwd_dl": - " RAISE NOTICE 'RPT STEP SPI local chain';\n", - } - for anchor, marker in edits.items(): - if source.count(anchor) != 1: - raise ValueError(f"forward regression instrumentation anchor changed: {anchor}") - source = source.replace(anchor, marker + anchor) - return source - - -def main(): - parser = argparse.ArgumentParser(description=__doc__) - parser.add_argument("--database", default="postgres") - parser.add_argument("--output", required=True, type=Path) - parser.add_argument("--psql", default="psql") - parser.add_argument("--wall-timeout", type=float, default=180) - parser.add_argument("--cancel-grace", type=float, default=15) - parser.add_argument("--emit-only", action="store_true") - args = parser.parse_args() - if args.wall_timeout <= 0 or args.cancel_grace < 0: - parser.error("wall-timeout must be positive and cancel-grace nonnegative") - args.output.mkdir(parents=True, exist_ok=True) - source = Path(__file__).resolve().parents[1] / "sql" / "rpt_forward.sql" - application = "rpt_diagnostic_" + uuid.uuid4().hex - workload = args.output / "workload.sql" - prefix = ("\\set ON_ERROR_STOP on\n\\timing on\n" - f"SET application_name = '{application}';\n" - "SET client_min_messages = notice;\n" - "SELECT pg_backend_pid() AS diagnostic_backend_pid;\n") - workload.write_text(prefix + instrument_sql(source.read_text())) - if args.emit_only: - print(workload) - return 0 - - command = [args.psql, "-X", "-v", "ON_ERROR_STOP=1", "-d", args.database] - env = dict(os.environ) - env["PGCONNECT_TIMEOUT"] = "5" - - def monitor(filename, sql): - try: - result = subprocess.run(command + ["-A", "-t", "-c", sql], - env=env, capture_output=True, text=True, timeout=8) - (args.output / filename).write_text(result.stdout + result.stderr) - return result.returncode - except subprocess.TimeoutExpired as error: - (args.output / filename).write_text(f"monitor timed out: {error.timeout}s\n") - return -1 - - with (args.output / "client.log").open("w") as output: - proc = subprocess.Popen(command + ["-e", "-f", str(workload)], - env=env, stdout=output, stderr=subprocess.STDOUT) - start = time.monotonic() - timed_out = False - try: - proc.wait(timeout=args.wall_timeout) - except subprocess.TimeoutExpired: - timed_out = True - monitor("activity-before-cancel.json", "SELECT coalesce(json_agg(t), '[]'::json) FROM " - "(SELECT pid, application_name, state, wait_event_type, wait_event, " - "query_start, query FROM pg_stat_activity WHERE application_name = " - f"'{application}') t;") - monitor("cancel.txt", "SELECT pid, pg_cancel_backend(pid) FROM pg_stat_activity " - f"WHERE application_name = '{application}';") - try: - proc.wait(timeout=args.cancel_grace) - except subprocess.TimeoutExpired: - monitor("activity-after-cancel.json", "SELECT coalesce(json_agg(t), '[]'::json) FROM " - "(SELECT pid, state, wait_event_type, wait_event, query FROM pg_stat_activity " - f"WHERE application_name = '{application}') t;") - # Stop only this client. Leave a stuck server available for inspection. - proc.terminate() - try: - proc.wait(timeout=5) - except subprocess.TimeoutExpired: - proc.kill() - proc.wait() - result = {"application_name": application, "timed_out": timed_out, - "client_returncode": proc.returncode, - "elapsed_seconds": time.monotonic() - start} - (args.output / "result.json").write_text(json.dumps(result, indent=2) + "\n") - print(f"Saved diagnostic output in {args.output}") - if timed_out: - print("Watchdog expired. Server backends were not terminated; inspect activity and server logs.") - return 2 - return proc.returncode - +"""Compatibility entry point; use diagnose_rpt.py for all RPT suites.""" +from diagnose_rpt import instrument_sql, main if __name__ == "__main__": raise SystemExit(main()) diff --git a/gpcontrib/robust_predicate_transfer/scripts/diagnose_rpt.py b/gpcontrib/robust_predicate_transfer/scripts/diagnose_rpt.py new file mode 100644 index 00000000000..ebe185ccb2a --- /dev/null +++ b/gpcontrib/robust_predicate_transfer/scripts/diagnose_rpt.py @@ -0,0 +1,212 @@ +#!/usr/bin/env python3 +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +"""Run RPT regressions with decision summaries and an independent watchdog. + +Use an isolated test database with both libraries installed and preloaded. +The runner never terminates a server backend or restarts the cluster. +""" +import argparse +import json +import os +import re +from collections import Counter +from pathlib import Path +import subprocess +import time +import uuid + + +def instrument_sql(source, suite="forward"): + """Keep regression assertions, but identify every dynamic query first.""" + source = source.replace("rpt_transfer_plan(", "pg_temp.rpt_diag_transfer_plan(") + anchor = "DO $$\nDECLARE\n opt text;" + if suite == "forward" and source.count(anchor) != 1: + raise ValueError("forward regression assertion block changed") + helper = """CREATE FUNCTION pg_temp.rpt_diag_transfer_plan(q text) RETURNS text +LANGUAGE plpgsql AS $diag$ +DECLARE + dump text; +BEGIN + RAISE NOTICE 'RPT STEP transfer_plan SQL=%', q; + dump := public.rpt_transfer_plan(q); + RAISE NOTICE 'RPT DUMP BEGIN SQL=%', replace(q, chr(10), ' '); + RAISE NOTICE '%', dump; + RAISE NOTICE 'RPT DUMP END'; + RETURN dump; +END +$diag$; +""" + if suite != "forward": + if "CREATE EXTENSION robust_predicate_transfer;" not in source: + raise ValueError("backward regression assertion block changed") + source = source.replace("CREATE EXTENSION robust_predicate_transfer;", + "CREATE EXTENSION robust_predicate_transfer;\n" + helper, 1) + source = source.replace(" EXECUTE q INTO r;", + " RAISE NOTICE 'RPT STEP results SQL=%', q;\n EXECUTE q INTO r;") + source = source.replace(" EXECUTE 'EXPLAIN (ANALYZE, FORMAT JSON) ' || q INTO plan;", + " RAISE NOTICE 'RPT STEP explain SQL=%', q;\n" + " EXECUTE 'EXPLAIN (ANALYZE, FORMAT JSON) ' || q INTO plan;") + return source + source = source.replace(anchor, helper + anchor) + edits = { + " EXECUTE format('EXPLAIN (%s FORMAT JSON) %s',": + " RAISE NOTICE 'RPT STEP explain analyze=% SQL=%', do_analyze, q;\n", + " EXECUTE format('SELECT string_agg(t::text, %L ORDER BY t::text) FROM (%s) t',": + " RAISE NOTICE 'RPT STEP results rpt=% SQL=%', rpt, q;\n", + " EXECUTE 'PREPARE rpt_fwd_prep AS ' || star;": + " RAISE NOTICE 'RPT STEP prepare optimizer=%', opt;\n", + " EXECUTE 'EXECUTE rpt_fwd_prep' INTO prep_n, prep_s;": + " RAISE NOTICE 'RPT STEP execute prepared optimizer=% run=%', opt, i;\n", + " FOR r IN SELECT f.v FROM rpt_fwd_fact f JOIN rpt_fwd_d1": + " RAISE NOTICE 'RPT STEP SPI dimension scan';\n", + " FOR r IN SELECT f.v FROM rpt_fwd_fact f JOIN rpt_fwd_dl": + " RAISE NOTICE 'RPT STEP SPI local chain';\n", + } + for anchor, marker in edits.items(): + if source.count(anchor) != 1: + raise ValueError(f"forward regression instrumentation anchor changed: {anchor}") + source = source.replace(anchor, marker + anchor) + return source + + +def summarize_dump(dump): + """Summarize only runtime decisions, excluding the legacy survey lines.""" + classes = [] + reasons = Counter() + injected = 0 + cost = 0.0 + for line in dump.splitlines(): + if line.startswith("backward class "): + classes.append(line) + match = re.search(r"est_cost_ms=([0-9.]+)", line) + if match and line.endswith("decision=inject"): + cost += float(match.group(1)) + elif line.startswith("backward consumer ") and " decision=" in line: + reason = line.split(" decision=", 1)[1] + if reason == "inject": + injected += 1 + else: + reasons[reason] += 1 + return {"classes": len(classes), "injected_consumers": injected, + "refused_consumers": dict(reasons), "estimated_cost_ms": cost, + "forward": [line for line in dump.splitlines() + if line.startswith("forward component ")]} + + +def summarize_log(log): + reports = [] + query = None + lines = [] + for line in log.splitlines(): + # psql prefixes the first line of a NOTICE, but not continuation lines. + line = re.sub(r"^.*NOTICE:\s+", "", line) + if line.startswith("RPT DUMP BEGIN SQL="): + query = line.split("SQL=", 1)[1] + lines = [] + elif line == "RPT DUMP END" and query is not None: + reports.append(dict(sql=query, **summarize_dump("\n".join(lines)))) + query = None + elif query is not None: + lines.append(line) + return reports + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--suite", choices=("forward", "backward", "backward_faults"), + default="forward") + parser.add_argument("--database", default="postgres") + parser.add_argument("--output", required=True, type=Path) + parser.add_argument("--psql", default="psql") + parser.add_argument("--wall-timeout", type=float, default=180) + parser.add_argument("--cancel-grace", type=float, default=15) + parser.add_argument("--emit-only", action="store_true") + args = parser.parse_args() + if args.wall_timeout <= 0 or args.cancel_grace < 0: + parser.error("wall-timeout must be positive and cancel-grace nonnegative") + args.output.mkdir(parents=True, exist_ok=True) + source = Path(__file__).resolve().parents[1] / "sql" / ("rpt_" + args.suite + ".sql") + application = "rpt_diagnostic_" + uuid.uuid4().hex + workload = args.output / "workload.sql" + prefix = ("\\set ON_ERROR_STOP on\n\\timing on\n" + f"SET application_name = '{application}';\n" + "SET client_min_messages = notice;\n" + "SELECT pg_backend_pid() AS diagnostic_backend_pid;\n") + workload.write_text(prefix + instrument_sql(source.read_text(), args.suite)) + if args.emit_only: + print(workload) + return 0 + + command = [args.psql, "-X", "-v", "ON_ERROR_STOP=1", "-d", args.database] + env = dict(os.environ) + env["PGCONNECT_TIMEOUT"] = "5" + + def monitor(filename, sql): + try: + result = subprocess.run(command + ["-A", "-t", "-c", sql], + env=env, capture_output=True, text=True, timeout=8) + (args.output / filename).write_text(result.stdout + result.stderr) + return result.returncode + except subprocess.TimeoutExpired as error: + (args.output / filename).write_text(f"monitor timed out: {error.timeout}s\n") + return -1 + + with (args.output / "client.log").open("w") as output: + proc = subprocess.Popen(command + ["-e", "-f", str(workload)], + env=env, stdout=output, stderr=subprocess.STDOUT) + start = time.monotonic() + timed_out = False + try: + proc.wait(timeout=args.wall_timeout) + except subprocess.TimeoutExpired: + timed_out = True + monitor("activity-before-cancel.json", "SELECT coalesce(json_agg(t), '[]'::json) FROM " + "(SELECT pid, application_name, state, wait_event_type, wait_event, " + "query_start, query FROM pg_stat_activity WHERE application_name = " + f"'{application}') t;") + monitor("cancel.txt", "SELECT pid, pg_cancel_backend(pid) FROM pg_stat_activity " + f"WHERE application_name = '{application}';") + try: + proc.wait(timeout=args.cancel_grace) + except subprocess.TimeoutExpired: + monitor("activity-after-cancel.json", "SELECT coalesce(json_agg(t), '[]'::json) FROM " + "(SELECT pid, state, wait_event_type, wait_event, query FROM pg_stat_activity " + f"WHERE application_name = '{application}') t;") + # Stop only this client. Leave a stuck server available for inspection. + proc.terminate() + try: + proc.wait(timeout=5) + except subprocess.TimeoutExpired: + proc.kill() + proc.wait() + result = {"application_name": application, "timed_out": timed_out, + "client_returncode": proc.returncode, + "elapsed_seconds": time.monotonic() - start} + (args.output / "result.json").write_text(json.dumps(result, indent=2) + "\n") + reports = summarize_log((args.output / "client.log").read_text()) + (args.output / "decisions.json").write_text(json.dumps(reports, indent=2) + "\n") + print(f"Saved diagnostic output in {args.output}") + if timed_out: + print("Watchdog expired. Server backends were not terminated; inspect activity and server logs.") + return 2 + return proc.returncode + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/gpcontrib/robust_predicate_transfer/scripts/survey.py b/gpcontrib/robust_predicate_transfer/scripts/survey.py index a700034a4b6..955057e9693 100644 --- a/gpcontrib/robust_predicate_transfer/scripts/survey.py +++ b/gpcontrib/robust_predicate_transfer/scripts/survey.py @@ -147,7 +147,7 @@ def survey_sql(query, optimizer, timeout_ms, search_path): "SET LOCAL optimizer = " + sql_literal(optimizer) + ";", "SELECT set_config('search_path', " + sql_literal(search_path) + ", true);", "SET LOCAL rpt.enable = off;", - "SET LOCAL rpt.backward = off;", + "SET LOCAL rpt.backward = on;", "SET LOCAL anser.enable = on;", "SET LOCAL anser.runtime_filter = off;", "SELECT " + sql_literal(BEGIN_DUMP) + ";", @@ -164,7 +164,9 @@ def parse_dump(dump): if dump.strip() == "no transfer regions": return dict(classes=0, root_classes=0, global_edges=0, rule9=0, strict=0, eligible=0, known_rows_removed=0.0, - unknown_estimates=0) + unknown_estimates=0, runtime_survey_available=True, + runtime_classes=0, runtime_root_classes=0, runtime_strict=0, + runtime_eligible=0, runtime_injected_consumers=0, runtime_refusals={}) lines = [line for line in dump.splitlines() if line.startswith("survey ")] if len(lines) != 1: raise ValueError("missing survey metrics; install the analysis/survey build") @@ -178,6 +180,23 @@ def parse_dump(dump): not result["eligible"] <= result["strict"] <= result["rule9"] <= result["global_edges"] or result["unknown_estimates"] > result["strict"]): raise ValueError("inconsistent survey metrics") + runtime = [line for line in dump.splitlines() if line.startswith("runtime survey ")] + result["runtime_survey_available"] = bool(runtime) + if runtime: + if len(runtime) != 1: + raise ValueError("duplicate runtime survey metrics") + fields = dict(re.findall(r"([a-z_]+)=([0-9]+)", runtime[0])) + for key in ("classes", "root_classes", "strict", "eligible"): + result["runtime_" + key] = int(fields[key]) + consumers = [line for line in dump.splitlines() + if line.startswith("backward consumer ") and " decision=" in line] + result["runtime_injected_consumers"] = sum(line.endswith(" decision=inject") for line in consumers) + reasons = {} + for line in consumers: + reason = line.split(" decision=", 1)[1] + if reason != "inject": + reasons[reason] = reasons.get(reason, 0) + 1 + result["runtime_refusals"] = reasons return result @@ -249,6 +268,19 @@ def summarize(records, complete): "table. unknown_estimates counts missing statistics; it is not zero benefit.", "", "The survey is evidence for the plan's go/no-go decision; this " "script never authorizes runtime injection."] + runtime = [row for row in records if row.get("runtime_survey_available")] + if runtime: + lines += ["", "Largest-root runtime schedule (successful runtime dumps only):", "", + "| Suite | Optimizer | Queries | Root classes | Proven consumers | Selected consumers |", + "| --- | --- | --- | --- | --- | --- |"] + for suite in sorted({row["suite"] for row in runtime}): + for optimizer in ("off", "on"): + group = [row for row in runtime if row["suite"] == suite and row["optimizer"] == optimizer] + lines.append("| %s | %s | %d | %d | %d | %d |" % ( + suite, optimizer, len(group), sum(row["runtime_root_classes"] for row in group), + sum(row["runtime_strict"] for row in group), + sum(row["runtime_injected_consumers"] for row in group))) + lines += ["", "Runtime refusal reasons are retained per query in results.json."] return "\n".join(lines) + "\n" diff --git a/gpcontrib/robust_predicate_transfer/sql/rpt_backward.sql b/gpcontrib/robust_predicate_transfer/sql/rpt_backward.sql new file mode 100644 index 00000000000..3dee383a2e7 --- /dev/null +++ b/gpcontrib/robust_predicate_transfer/sql/rpt_backward.sql @@ -0,0 +1,276 @@ +--------------------------------------------------------------------------- +-- +-- Licensed to the Apache Software Foundation (ASF) under one +-- or more contributor license agreements. See the NOTICE file +-- distributed with this work for additional information +-- regarding copyright ownership. The ASF licenses this file +-- to you under the Apache License, Version 2.0 (the +-- "License"); you may not use this file except in compliance +-- with the License. You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, +-- software distributed under the License is distributed on an +-- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +-- KIND, either express or implied. See the License for the +-- specific language governing permissions and limitations +-- under the License. +-- +-- Backward producers run in the main plan above forward consumers. Every +-- result query is dynamic SQL, so GUC changes cannot reuse an earlier plan. +CREATE EXTENSION robust_predicate_transfer; +SET anser.runtime_filter = off; +SET anser.timeout_ms = 600000; +-- This timeout covers the entire assertion DO block, under both optimizers. +SET statement_timeout = '120s'; +SET work_mem = '1MB'; +SET enable_nestloop = off; +SET enable_mergejoin = off; +SET join_collapse_limit = 1; +SET optimizer_join_order = query; +CREATE TABLE rpt_bw_root (k int, s int) DISTRIBUTED BY (k); +CREATE TABLE rpt_bw_member (k int, pad text) DISTRIBUTED RANDOMLY; +CREATE TABLE rpt_bw_dim (s int, v int) DISTRIBUTED RANDOMLY; +CREATE TABLE rpt_bw_bridge (k int, s int) DISTRIBUTED RANDOMLY; +CREATE TABLE rpt_bw_empty (k int) DISTRIBUTED RANDOMLY; +CREATE TABLE rpt_bw_local (k int) DISTRIBUTED BY (k); +CREATE TABLE rpt_bw_replica (k int) DISTRIBUTED REPLICATED; +CREATE TABLE rpt_bw_part (k int, pad text) DISTRIBUTED RANDOMLY + PARTITION BY RANGE (k) (START (0) END (2000) EVERY (1000)); +-- The root has more rows but a smaller hash footprint than the wide member. +-- Duplicate root keys also make its backward filter selective without a +-- root restriction that would change the largest-root choice. +INSERT INTO rpt_bw_root SELECT CASE WHEN i % 101 = 0 THEN NULL ELSE i % 10 END, + i % 100 FROM generate_series(1, 10000) i; +INSERT INTO rpt_bw_member SELECT i, repeat(md5(i::text), 100) + FROM generate_series(0, 999) i; +INSERT INTO rpt_bw_dim SELECT i, i FROM generate_series(0, 99) i; +INSERT INTO rpt_bw_bridge SELECT i, i % 100 FROM generate_series(0, 999) i; +INSERT INTO rpt_bw_local SELECT i FROM generate_series(0, 999) i; +INSERT INTO rpt_bw_replica SELECT i FROM generate_series(0, 999) i; +INSERT INTO rpt_bw_part SELECT * FROM rpt_bw_member; +ANALYZE rpt_bw_root; +ANALYZE rpt_bw_member; +ANALYZE rpt_bw_dim; +ANALYZE rpt_bw_bridge; +ANALYZE rpt_bw_empty; +ANALYZE rpt_bw_local; +ANALYZE rpt_bw_replica; +ANALYZE rpt_bw_part; +CREATE FUNCTION pg_temp.rpt_bw_result(q text, enabled boolean, backward boolean) +RETURNS text LANGUAGE plpgsql AS $$ +DECLARE + r text; +BEGIN + PERFORM set_config('rpt.enable', CASE WHEN enabled THEN 'on' ELSE 'off' END, true); + PERFORM set_config('rpt.backward', CASE WHEN backward THEN 'on' ELSE 'off' END, true); + q := format('SELECT coalesce(string_agg(t::text, %L ORDER BY t::text), %L) FROM (%s) t', + ';', '', q); + EXECUTE q INTO r; + PERFORM set_config('rpt.enable', 'on', true); + PERFORM set_config('rpt.backward', 'on', true); + RETURN r; +END +$$; +CREATE FUNCTION pg_temp.rpt_bw_plan(q text) RETURNS jsonb LANGUAGE plpgsql AS $$ +DECLARE + plan jsonb; +BEGIN + EXECUTE 'EXPLAIN (ANALYZE, FORMAT JSON) ' || q INTO plan; + RETURN plan; +END +$$; +CREATE FUNCTION pg_temp.rpt_bw_nodes(plan jsonb) +RETURNS SETOF jsonb LANGUAGE sql AS $$ + WITH RECURSIVE walk(node, in_init) AS ( + SELECT plan->0->'Plan', false + UNION ALL + SELECT child, in_init OR coalesce(child->>'Parent Relationship', '') = 'InitPlan' + FROM walk CROSS JOIN LATERAL jsonb_array_elements(coalesce(node->'Plans', '[]')) child + ) SELECT node FROM walk WHERE NOT in_init +$$; +CREATE FUNCTION pg_temp.rpt_bw_expect(q text, relation_name text, reason text) +RETURNS void LANGUAGE plpgsql AS $$ +DECLARE + dump text := rpt_transfer_plan(q); + member_rtindex text; +BEGIN + member_rtindex := substring(dump FROM ('vertex ([0-9]+) ' || relation_name || ' rows=')); + IF member_rtindex IS NULL OR NOT EXISTS ( + SELECT FROM regexp_split_to_table(dump, chr(10)) line + WHERE line LIKE ('backward consumer ' || member_rtindex || ' root=%') + AND line LIKE ('% decision=' || reason)) THEN + RAISE EXCEPTION 'missing backward decision % for %, optimizer=%: %', + reason, relation_name, current_setting('optimizer'), dump; + END IF; +END +$$; +DO $$ +DECLARE + opt text; + mode text; + q text; + dump text; + plan jsonb; + baseline text; + answer text; + cursor_name refcursor; + first_row record; + plain constant text := 'SELECT r.k, m.pad FROM rpt_bw_member m JOIN rpt_bw_root r ON m.k = r.k'; + chained constant text := 'SELECT r.k, m.pad FROM rpt_bw_member m JOIN + (rpt_bw_root r JOIN rpt_bw_dim d ON r.s = d.s) ON m.k = r.k WHERE d.v < 5'; +BEGIN + PERFORM set_config('rpt.enable', 'on', true); + PERFORM set_config('rpt.forward_min_root_rows', '0', true); + PERFORM set_config('rpt.forward_max_exchange_ms', '-1', true); + FOREACH opt IN ARRAY ARRAY['off', 'on'] LOOP + PERFORM set_config('optimizer', opt, true); + PERFORM set_config('rpt.forward_apply', 'root', true); + PERFORM set_config('rpt.forward', 'off', true); + PERFORM pg_temp.rpt_bw_expect(plain, 'm', 'inject'); + plan := pg_temp.rpt_bw_plan(plain); + IF NOT EXISTS (SELECT FROM pg_temp.rpt_bw_nodes(plan) n + WHERE n->>'Custom Plan Provider' = 'Anser Bloom Producer') OR + NOT EXISTS (SELECT FROM pg_temp.rpt_bw_nodes(plan) n + WHERE n->>'Custom Plan Provider' = 'Anser Bloom Consumer') OR + plan::text LIKE '%InitPlan%' THEN + RAISE EXCEPTION 'backward-only shape wrong (optimizer=%): %', opt, plan; + END IF; + IF (SELECT coalesce(sum(coalesce((n->>'Rows Removed by Bloom Filter')::float8, 0) + + coalesce((n->>'Rows Removed by Pushdown Runtime Filter')::float8, 0)), 0) + FROM pg_temp.rpt_bw_nodes(plan) n) <= 0 THEN + RAISE EXCEPTION 'backward consumer removed no rows (optimizer=%): %', opt, plan; + END IF; + -- The root producer must directly wrap the forward consumer stack. + PERFORM set_config('rpt.forward', 'on', true); + PERFORM pg_temp.rpt_bw_expect(chained, 'm', 'inject'); + plan := pg_temp.rpt_bw_plan(chained); + IF plan::text NOT LIKE '%InitPlan%' OR NOT EXISTS ( + SELECT FROM pg_temp.rpt_bw_nodes(plan) n + WHERE n->>'Custom Plan Provider' = 'Anser Bloom Producer' + AND n->'Plans'->0->>'Custom Plan Provider' = 'Anser Bloom Consumer') THEN + RAISE EXCEPTION 'backward producer is not above forward consumer (optimizer=%): %', opt, plan; + END IF; + -- Refusing forward must not suppress a useful backward filter. + PERFORM set_config('rpt.forward_min_root_rows', '1e12', true); + PERFORM pg_temp.rpt_bw_expect(chained, 'm', 'inject'); + IF rpt_transfer_plan(chained) NOT LIKE '%decision=root rows below rpt.forward_min_root_rows%' THEN + RAISE EXCEPTION 'forward gate fixture did not actually refuse'; + END IF; + PERFORM set_config('rpt.forward_min_root_rows', '0', true); + -- Runtime gates, with the same fixture known to inject above. + PERFORM set_config('rpt.forward_max_selectivity', '0', true); + PERFORM pg_temp.rpt_bw_expect(plain, 'm', + 'estimated member selectivity not below rpt.forward_max_selectivity'); + PERFORM set_config('rpt.forward_max_selectivity', '0.35', true); + PERFORM set_config('rpt.forward_max_exchange_ms', '0', true); + PERFORM pg_temp.rpt_bw_expect(plain, 'm', + 'estimated exchange cost above rpt.forward_max_exchange_ms'); + PERFORM set_config('rpt.forward_max_exchange_ms', '-1', true); + PERFORM pg_temp.rpt_bw_expect( + 'SELECT * FROM rpt_bw_root r JOIN rpt_bw_local l ON r.k = l.k', + 'l', 'local edge uses core runtime filtering'); + -- Narrow projection makes the smaller member the hash build side. + PERFORM pg_temp.rpt_bw_expect( + 'SELECT count(*) FROM rpt_bw_root r JOIN rpt_bw_member m ON r.k = m.k', + 'm', 'producer is not on a qualifying hash build side'); + -- LEFT's preserved root cannot be filtered by its nullable member. + -- Its member has no unprotected equivalence class for broadcasting. + PERFORM pg_temp.rpt_bw_expect( + 'SELECT r.k, m.pad FROM rpt_bw_root r LEFT JOIN rpt_bw_member m ON r.k = m.k', + 'm', 'edge has no unprotected equivalence class'); + dump := rpt_transfer_plan('SELECT * FROM rpt_bw_root r JOIN rpt_bw_bridge b + ON r.k = b.k JOIN rpt_bw_dim d ON b.s = d.s WHERE d.v < 5'); + IF dump NOT LIKE '%backward class %decision=bridge-led class is deferred%' THEN + RAISE EXCEPTION 'bridge-led backward class was not deferred (optimizer=%): %', opt, dump; + END IF; + -- Both joins consume the narrow root subtree on their build side. + -- Cross-relation join filters keep wide member columns in each join's + -- input, while only r.k survives into the next hash build. + PERFORM set_config('rpt.max_backward_consumers', '1', true); + dump := rpt_transfer_plan('SELECT count(*) FROM + (rpt_bw_member m1 JOIN rpt_bw_root r ON m1.k = r.k AND length(m1.pad) > r.k) + JOIN rpt_bw_member m2 ON m2.k = r.k AND length(m2.pad) > r.k'); + IF (SELECT count(*) FROM regexp_split_to_table(dump, chr(10)) line + WHERE line LIKE 'backward consumer % root=% decision=inject') <> 1 OR + dump NOT LIKE '%root=%decision=backward consumer cap exceeded%' THEN + RAISE EXCEPTION 'runtime backward fan-out cap not exercised (optimizer=%): %', opt, dump; + END IF; + PERFORM set_config('rpt.max_backward_consumers', '4', true); + -- Every runtime proof refusal is retained verbatim in the new dump; + -- real-plan proof cases also run in rpt_backward_eligibility. + dump := rpt_transfer_plan('SELECT r.k, (SELECT count(*) FROM rpt_bw_member m + JOIN rpt_bw_dim d ON m.k = d.s WHERE m.k < r.k AND random() < 1 OFFSET 0) + FROM rpt_bw_root r'); + IF dump NOT LIKE '%decision=scan inside a SubPlan%' THEN + RAISE EXCEPTION 'correlated producer was not refused (optimizer=%): %', opt, dump; + END IF; + FOREACH q IN ARRAY ARRAY[ + plain, chained, + 'SELECT count(*), sum(r.k) FROM rpt_bw_root r LEFT JOIN rpt_bw_member m ON r.k = m.k', + 'SELECT count(*), sum(r.k) FROM rpt_bw_member m RIGHT JOIN rpt_bw_root r ON r.k = m.k', + 'SELECT count(*), sum(r.k) FROM rpt_bw_root r FULL JOIN rpt_bw_member m ON r.k = m.k', + 'SELECT count(*), sum(r.k) FROM rpt_bw_root r WHERE EXISTS (SELECT FROM rpt_bw_member m WHERE m.k = r.k)', + 'SELECT count(*), sum(r.k) FROM rpt_bw_root r WHERE NOT EXISTS (SELECT FROM rpt_bw_member m WHERE m.k = r.k)', + 'SELECT count(*) FROM rpt_bw_root r JOIN rpt_bw_empty e ON r.k = e.k', + 'SELECT count(*) FROM rpt_bw_empty e JOIN rpt_bw_member m ON e.k = m.k', + 'SELECT count(*) FROM rpt_bw_root r JOIN rpt_bw_replica p ON r.k = p.k', + 'SELECT count(*) FROM rpt_bw_replica p JOIN rpt_bw_member m ON p.k = m.k', + 'SELECT count(*) FROM rpt_bw_root r JOIN rpt_bw_part p ON r.k = p.k', + 'SELECT r.k, m.pad FROM rpt_bw_member m JOIN rpt_bw_root r ON m.k = r.k LIMIT 1', + 'SELECT r.k, m.pad FROM rpt_bw_member m JOIN rpt_bw_root r ON m.k = r.k LIMIT 0' + ] LOOP + -- LIMIT 1 has no SQL ordering guarantee; compare a count instead. + IF q LIKE '%LIMIT 1' THEN q := 'SELECT count(*) FROM (' || q || ') limited'; END IF; + baseline := pg_temp.rpt_bw_result(q, false, false); + FOREACH mode IN ARRAY ARRAY['root', 'all'] LOOP + PERFORM set_config('rpt.forward_apply', mode, true); + IF pg_temp.rpt_bw_result(q, true, false) IS DISTINCT FROM baseline OR + pg_temp.rpt_bw_result(q, true, true) IS DISTINCT FROM baseline THEN + RAISE EXCEPTION 'backward result differs (optimizer=%, apply=%): %', opt, mode, q; + END IF; + END LOOP; + END LOOP; + PERFORM set_config('rpt.forward_apply', 'root', true); + q := 'SELECT count(*)::text || ''/'' || sum(length(m.pad || r.k::text))::text + FROM rpt_bw_member m JOIN rpt_bw_root r ON m.k = r.k'; + PERFORM pg_temp.rpt_bw_expect(q, 'm', 'inject'); + baseline := pg_temp.rpt_bw_result(q, false, false); + EXECUTE 'PREPARE rpt_bw_prepared AS ' || q; + FOR i IN 1..2 LOOP + EXECUTE 'EXECUTE rpt_bw_prepared' INTO answer; + IF '(' || answer || ')' IS DISTINCT FROM baseline THEN + RAISE EXCEPTION 'backward prepared answer differs'; + END IF; + END LOOP; + EXECUTE 'DEALLOCATE rpt_bw_prepared'; + cursor_name := 'rpt_bw_cursor'; + OPEN cursor_name FOR EXECUTE plain; + FETCH cursor_name INTO first_row; + IF NOT FOUND THEN RAISE EXCEPTION 'backward cursor returned no row'; END IF; + CLOSE cursor_name; + -- A new command must answer after the cursor squelches its producer. + IF pg_temp.rpt_bw_result(q, true, true) IS DISTINCT FROM baseline THEN + RAISE EXCEPTION 'next command after backward cursor'; + END IF; + PERFORM set_config('anser.runtime_filter', 'on', true); + dump := rpt_transfer_plan(chained); + IF dump LIKE '%key=rpt_b_%' OR dump NOT LIKE '%needs anser.enable=on and anser.runtime_filter=off%' THEN + RAISE EXCEPTION 'backward did not stand down for Anser: %', dump; + END IF; + PERFORM set_config('anser.runtime_filter', 'off', true); + END LOOP; +END +$$; +DROP TABLE rpt_bw_root, rpt_bw_member, rpt_bw_dim, rpt_bw_bridge, rpt_bw_empty, + rpt_bw_local, rpt_bw_replica, rpt_bw_part; +DROP EXTENSION robust_predicate_transfer; +RESET anser.runtime_filter; +RESET anser.timeout_ms; +RESET statement_timeout; +RESET enable_nestloop; +RESET enable_mergejoin; +RESET work_mem; +RESET join_collapse_limit; +RESET optimizer_join_order; diff --git a/gpcontrib/robust_predicate_transfer/sql/rpt_backward_eligibility.sql b/gpcontrib/robust_predicate_transfer/sql/rpt_backward_eligibility.sql index db9aa1e785d..292641d3eef 100644 --- a/gpcontrib/robust_predicate_transfer/sql/rpt_backward_eligibility.sql +++ b/gpcontrib/robust_predicate_transfer/sql/rpt_backward_eligibility.sql @@ -56,17 +56,17 @@ CREATE FUNCTION pg_temp.rpt_expect(q text, producer text, consumer text, RETURNS text LANGUAGE plpgsql AS $$ DECLARE dump text := rpt_transfer_plan(q); - producer_id text; - consumer_id text; + producer_rtindex text; + consumer_rtindex text; decision text; BEGIN - producer_id := substring(dump FROM ('vertex ([0-9]+) ' || producer || ' rows=')); - consumer_id := substring(dump FROM ('vertex ([0-9]+) ' || consumer || ' rows=')); - IF producer_id IS NULL OR consumer_id IS NULL THEN + producer_rtindex := substring(dump FROM ('vertex ([0-9]+) ' || producer || ' rows=')); + consumer_rtindex := substring(dump FROM ('vertex ([0-9]+) ' || consumer || ' rows=')); + IF producer_rtindex IS NULL OR consumer_rtindex IS NULL THEN RAISE EXCEPTION 'missing fixture scans % / %: %', producer, consumer, dump; END IF; SELECT line INTO decision FROM regexp_split_to_table(dump, chr(10)) line - WHERE line LIKE ('backward consumer ' || consumer_id || ' producer ' || producer_id || ' %') + WHERE line LIKE ('backward consumer ' || consumer_rtindex || ' producer ' || producer_rtindex || ' %') AND substring(line FROM ' reason=(.*)$') = ANY(reasons) AND (accepted IS NULL OR line LIKE ('% eligible=' || CASE WHEN accepted THEN 'yes' ELSE 'no' END || ' %')) LIMIT 1; @@ -102,7 +102,7 @@ DECLARE path text[]; producer_path text[]; split_at integer; - producer_id text; + producer_rtindex text; correlated text; BEGIN FOREACH opt IN ARRAY ARRAY['off', 'on'] LOOP @@ -201,13 +201,13 @@ BEGIN END IF; PERFORM set_config('rpt.max_backward_consumers', '1', true); dump := rpt_transfer_plan('SELECT * FROM rpt_elig_root JOIN rpt_elig_member ON rpt_elig_root.k = rpt_elig_member.k JOIN rpt_elig_member2 ON rpt_elig_root.k = rpt_elig_member2.k JOIN rpt_elig_member3 ON rpt_elig_root.k = rpt_elig_member3.k WHERE rpt_elig_root.v < 100'); - producer_id := substring(dump FROM 'vertex ([0-9]+) rpt_elig_root rows='); - IF producer_id IS NULL OR + producer_rtindex := substring(dump FROM 'vertex ([0-9]+) rpt_elig_root rows='); + IF producer_rtindex IS NULL OR NOT EXISTS (SELECT FROM regexp_split_to_table(dump, chr(10)) line - WHERE line LIKE ('backward consumer % producer ' || producer_id || ' %') + WHERE line LIKE ('backward consumer % producer ' || producer_rtindex || ' %') AND line LIKE '% reason=backward consumer cap exceeded') OR (SELECT count(*) FROM regexp_split_to_table(dump, chr(10)) line - WHERE line LIKE ('backward consumer % producer ' || producer_id || ' %') + WHERE line LIKE ('backward consumer % producer ' || producer_rtindex || ' %') AND line LIKE '% eligible=yes %') <> 1 THEN RAISE EXCEPTION 'fan-out cap fixture did not retain exactly one consumer (optimizer=%): %', opt, dump; END IF; diff --git a/gpcontrib/robust_predicate_transfer/sql/rpt_backward_faults.sql b/gpcontrib/robust_predicate_transfer/sql/rpt_backward_faults.sql new file mode 100644 index 00000000000..7bd6fd54b59 --- /dev/null +++ b/gpcontrib/robust_predicate_transfer/sql/rpt_backward_faults.sql @@ -0,0 +1,209 @@ +--------------------------------------------------------------------------- +-- +-- Licensed to the Apache Software Foundation (ASF) under one +-- or more contributor license agreements. See the NOTICE file +-- distributed with this work for additional information +-- regarding copyright ownership. The ASF licenses this file +-- to you under the Apache License, Version 2.0 (the +-- "License"); you may not use this file except in compliance +-- with the License. You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, +-- software distributed under the License is distributed on an +-- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +-- KIND, either express or implied. See the License for the +-- specific language governing permissions and limitations +-- under the License. +-- +-- Backward-only transport failures. Every armed fault must actually fire. +-- start_ignore +CREATE EXTENSION IF NOT EXISTS gp_inject_fault; +CREATE EXTENSION IF NOT EXISTS anser_test; +-- end_ignore +CREATE EXTENSION robust_predicate_transfer; +SET anser.runtime_filter = off; +SET rpt.enable = on; +SET rpt.forward = off; +SET rpt.backward = on; +SET rpt.forward_max_exchange_ms = -1; +SET statement_timeout = '120s'; +SET work_mem = '1MB'; +SET enable_nestloop = off; +SET enable_mergejoin = off; +CREATE TABLE rpt_bwf_root (k int) DISTRIBUTED BY (k); +CREATE TABLE rpt_bwf_member (k int, pad text) DISTRIBUTED RANDOMLY; +INSERT INTO rpt_bwf_root SELECT i % 10 FROM generate_series(1, 10000) i; +INSERT INTO rpt_bwf_member SELECT i, repeat(md5(i::text), 100) FROM generate_series(0, 999) i; +ANALYZE rpt_bwf_root; +ANALYZE rpt_bwf_member; +CREATE FUNCTION pg_temp.rpt_bwf_query() RETURNS text LANGUAGE sql AS $$ + SELECT 'SELECT count(*)::text || ''/'' || sum(length(m.pad || r.k::text))::text + FROM rpt_bwf_member m JOIN rpt_bwf_root r ON m.k = r.k' +$$; +CREATE FUNCTION pg_temp.rpt_bwf_run(enabled boolean) RETURNS text LANGUAGE plpgsql AS $$ +DECLARE + q text := pg_temp.rpt_bwf_query(); + r text; +BEGIN + PERFORM set_config('rpt.enable', CASE WHEN enabled THEN 'on' ELSE 'off' END, true); + EXECUTE q INTO r; + PERFORM set_config('rpt.enable', 'on', true); + RETURN r; +END +$$; +-- How often a fault fired, so a case cannot pass without exercising it. +CREATE FUNCTION pg_temp.rpt_bwf_hits(fault text, coordinator boolean) +RETURNS int LANGUAGE sql AS $$ + SELECT coalesce(sum(substring(gp_inject_fault(fault, 'status', dbid) + FROM 'num times hit:''([0-9]+)''')::int), 0)::int + FROM gp_segment_configuration + WHERE role = 'p' AND (content = -1) = coordinator +$$; +-- First occurrence only; on the coordinator a 'sleep' fault sleeps 1 s. +CREATE FUNCTION pg_temp.rpt_bwf_fault(fault text, action text, coordinator boolean) +RETURNS void LANGUAGE plpgsql AS $$ +BEGIN + IF action = 'reset' THEN + PERFORM gp_inject_fault(fault, 'reset', dbid) + FROM gp_segment_configuration + WHERE role = 'p' AND (content = -1) = coordinator; + ELSE + PERFORM gp_inject_fault(fault, action, '', '', '', 1, 1, + CASE WHEN coordinator THEN 1 ELSE 0 END, dbid) + FROM gp_segment_configuration + WHERE role = 'p' AND (content = -1) = coordinator; + END IF; +END +$$; +DO $$ +DECLARE + opt text; + baseline text; + answer text; + plan jsonb; + deliveries_before bigint; +BEGIN + FOREACH opt IN ARRAY ARRAY['off', 'on'] LOOP + PERFORM set_config('optimizer', opt, true); + baseline := pg_temp.rpt_bwf_run(false); + IF rpt_transfer_plan(pg_temp.rpt_bwf_query()) NOT LIKE '%backward class %decision=inject%' THEN + RAISE EXCEPTION 'backward fault fixture not selected (optimizer=%)', opt; + END IF; + PERFORM set_config('anser.timeout_ms', '100', true); + PERFORM pg_temp.rpt_bwf_fault('anser_before_deliver', 'sleep', true); + answer := pg_temp.rpt_bwf_run(true); + IF pg_temp.rpt_bwf_hits('anser_before_deliver', true) = 0 THEN + RAISE EXCEPTION 'backward delivery sleep never fired'; + END IF; + PERFORM pg_temp.rpt_bwf_fault('anser_before_deliver', 'reset', true); + IF answer IS DISTINCT FROM baseline OR pg_temp.rpt_bwf_run(true) IS DISTINCT FROM baseline THEN + RAISE EXCEPTION 'backward timeout or next command changed results'; + END IF; + PERFORM set_config('anser.timeout_ms', '1000', true); + PERFORM set_config('anser.debug_delivery_chunk_bytes', '8192', true); + PERFORM set_config('anser.debug_min_filter_bytes', '8388608', true); + PERFORM pg_temp.rpt_bwf_fault('anser_after_chunk', 'skip', false); + answer := pg_temp.rpt_bwf_run(true); + IF pg_temp.rpt_bwf_hits('anser_after_chunk', false) = 0 THEN + RAISE EXCEPTION 'backward consumer walk-away never fired'; + END IF; + PERFORM pg_temp.rpt_bwf_fault('anser_after_chunk', 'reset', false); + IF answer IS DISTINCT FROM baseline OR pg_temp.rpt_bwf_run(true) IS DISTINCT FROM baseline THEN + RAISE EXCEPTION 'backward walk-away or next command changed results'; + END IF; + -- Plan with an 8 MiB filter, then lower the runtime payload cap. + -- EXPLAIN EXECUTE forces the generic plan before the cap changes; + -- the second EXPLAIN proves the cached producer was not resized away. + EXECUTE 'PREPARE rpt_bwf_oversized AS ' || pg_temp.rpt_bwf_query(); + EXECUTE 'EXPLAIN (FORMAT JSON) EXECUTE rpt_bwf_oversized' INTO plan; + IF plan::text NOT LIKE '%8388608%' THEN + RAISE EXCEPTION 'oversized producer fixture was not planned: %', plan; + END IF; + PERFORM set_config('anser.max_info_size', '2097152', true); + EXECUTE 'EXPLAIN (FORMAT JSON) EXECUTE rpt_bwf_oversized' INTO plan; + IF plan::text NOT LIKE '%8388608%' THEN + RAISE EXCEPTION 'oversized producer fixture was replanned: %', plan; + END IF; + PERFORM pg_temp.rpt_bwf_fault('anser_before_deliver', 'skip', true); + deliveries_before := anser_test_deliveries_completed(); + EXECUTE 'EXECUTE rpt_bwf_oversized' INTO answer; + -- The fault proves delivery was attempted; only the counter can + -- distinguish a cancellation from a successfully delivered filter. + IF anser_test_deliveries_completed() <> deliveries_before THEN + RAISE EXCEPTION 'oversized producer delivered a filter instead of cancelling'; + END IF; + IF pg_temp.rpt_bwf_hits('anser_before_deliver', true) = 0 THEN + RAISE EXCEPTION 'oversized producer never delivered its cancellation'; + END IF; + PERFORM pg_temp.rpt_bwf_fault('anser_before_deliver', 'reset', true); + EXECUTE 'DEALLOCATE rpt_bwf_oversized'; + PERFORM set_config('anser.max_info_size', '68157440', true); + PERFORM set_config('anser.debug_min_filter_bytes', '0', true); + PERFORM set_config('anser.debug_delivery_chunk_bytes', '0', true); + IF answer IS DISTINCT FROM baseline OR pg_temp.rpt_bwf_run(true) IS DISTINCT FROM baseline THEN + RAISE EXCEPTION 'backward producer cancellation changed results'; + END IF; + END LOOP; +END +$$; +CREATE FUNCTION pg_temp.rpt_bwf_cancel() RETURNS void LANGUAGE plpgsql AS $$ +DECLARE + cancelled boolean := false; +BEGIN + BEGIN + PERFORM pg_temp.rpt_bwf_run(true); + EXCEPTION WHEN query_canceled THEN + cancelled := true; + END; + IF NOT cancelled THEN RAISE EXCEPTION 'backward wait was not cancelled'; END IF; +END +$$; +-- Separate top-level commands arm statement_timeout for each optimizer. +SET optimizer = off; +DO $$ BEGIN PERFORM pg_temp.rpt_bwf_fault('anser_before_deliver', 'sleep', true); END $$; +SET anser.timeout_ms = 600000; +SET statement_timeout = '300ms'; +DO $$ BEGIN PERFORM pg_temp.rpt_bwf_cancel(); END $$; +SET statement_timeout = '120s'; +DO $$ +BEGIN + IF pg_temp.rpt_bwf_hits('anser_before_deliver', true) = 0 THEN + RAISE EXCEPTION 'cancel test never reached backward delivery'; + END IF; + PERFORM pg_temp.rpt_bwf_fault('anser_before_deliver', 'reset', true); + IF pg_temp.rpt_bwf_run(true) IS DISTINCT FROM pg_temp.rpt_bwf_run(false) THEN + RAISE EXCEPTION 'session failed after backward cancellation'; + END IF; +END +$$; +SET optimizer = on; +DO $$ BEGIN PERFORM pg_temp.rpt_bwf_fault('anser_before_deliver', 'sleep', true); END $$; +SET statement_timeout = '300ms'; +DO $$ BEGIN PERFORM pg_temp.rpt_bwf_cancel(); END $$; +SET statement_timeout = '120s'; +DO $$ +BEGIN + IF pg_temp.rpt_bwf_hits('anser_before_deliver', true) = 0 THEN + RAISE EXCEPTION 'ORCA cancel test never reached backward delivery'; + END IF; + PERFORM pg_temp.rpt_bwf_fault('anser_before_deliver', 'reset', true); + IF pg_temp.rpt_bwf_run(true) IS DISTINCT FROM pg_temp.rpt_bwf_run(false) THEN + RAISE EXCEPTION 'ORCA session failed after backward cancellation'; + END IF; +END +$$; +DROP TABLE rpt_bwf_root, rpt_bwf_member; +DROP EXTENSION robust_predicate_transfer; +RESET rpt.enable; +RESET rpt.forward; +RESET rpt.backward; +RESET rpt.forward_max_exchange_ms; +RESET anser.runtime_filter; +RESET anser.timeout_ms; +RESET optimizer; +RESET statement_timeout; +RESET enable_nestloop; +RESET enable_mergejoin; +RESET work_mem; diff --git a/gpcontrib/robust_predicate_transfer/sql/rpt_forward.sql b/gpcontrib/robust_predicate_transfer/sql/rpt_forward.sql index 83d31b206ac..1743ebaf0e2 100644 --- a/gpcontrib/robust_predicate_transfer/sql/rpt_forward.sql +++ b/gpcontrib/robust_predicate_transfer/sql/rpt_forward.sql @@ -24,6 +24,7 @@ -- Faults live in rpt_forward_faults. CREATE EXTENSION robust_predicate_transfer; SET anser.runtime_filter = off; +SET rpt.backward = off; SET statement_timeout = '120s'; -- Star: two dimensions on two different fact keys (two hops into the root). CREATE TABLE rpt_fwd_fact (id int, d1 int, d2 int, v int) DISTRIBUTED RANDOMLY; @@ -350,4 +351,5 @@ DROP TABLE rpt_fwd_fact, rpt_fwd_d1, rpt_fwd_d2, rpt_fwd_empty, rpt_fwd_dl, rpt_fwd_dr, rpt_fwd_sub; DROP EXTENSION robust_predicate_transfer; RESET anser.runtime_filter; +RESET rpt.backward; RESET statement_timeout; diff --git a/gpcontrib/robust_predicate_transfer/sql/rpt_forward_faults.sql b/gpcontrib/robust_predicate_transfer/sql/rpt_forward_faults.sql index 08adbfbdd63..a972090b481 100644 --- a/gpcontrib/robust_predicate_transfer/sql/rpt_forward_faults.sql +++ b/gpcontrib/robust_predicate_transfer/sql/rpt_forward_faults.sql @@ -33,6 +33,7 @@ CREATE EXTENSION IF NOT EXISTS gp_inject_fault; CREATE EXTENSION robust_predicate_transfer; SET optimizer = off; SET anser.runtime_filter = off; +SET rpt.backward = off; SET anser.debug = on; SET rpt.enable = on; SET rpt.forward_min_root_rows = 0; @@ -170,5 +171,6 @@ RESET rpt.forward_min_root_rows; RESET rpt.forward_max_exchange_ms; RESET anser.debug; RESET anser.runtime_filter; +RESET rpt.backward; RESET optimizer; RESET statement_timeout; diff --git a/gpcontrib/robust_predicate_transfer/sql/rpt_graph.sql b/gpcontrib/robust_predicate_transfer/sql/rpt_graph.sql index 36d3316d525..3e2fe5c29a9 100644 --- a/gpcontrib/robust_predicate_transfer/sql/rpt_graph.sql +++ b/gpcontrib/robust_predicate_transfer/sql/rpt_graph.sql @@ -23,10 +23,10 @@ DECLARE opt text; dump text; q text; - distributed_id text; - replicated_id text; + distributed_rtindex text; + replicated_rtindex text; preserved_name text; - preserved_id text; + preserved_rtindex text; expected_members text; member_count int; BEGIN @@ -43,12 +43,12 @@ BEGIN 'SELECT * FROM rpt_a JOIN rpt_b ON rpt_a.a = rpt_b.a JOIN rpt_c ON rpt_b.a = rpt_c.a AND rpt_a.a = rpt_c.a WHERE rpt_a.b > 50' ] LOOP dump := rpt_transfer_plan(q); - distributed_id := substring(dump FROM 'vertex ([0-9]+) rpt_a rows='); + distributed_rtindex := substring(dump FROM 'vertex ([0-9]+) rpt_a rows='); SELECT count(*), string_agg(parts[1], ',' ORDER BY parts[1]::int) INTO member_count, expected_members FROM regexp_matches(dump, 'vertex ([0-9]+) (rpt_a|rpt_b|rpt_c) rows=', 'g') AS vertices(parts); - IF distributed_id IS NULL OR member_count <> 3 OR - dump NOT LIKE ('%members=' || expected_members || ' leader=' || distributed_id || chr(10) || '%') THEN + IF distributed_rtindex IS NULL OR member_count <> 3 OR + dump NOT LIKE ('%members=' || expected_members || ' leader=' || distributed_rtindex || chr(10) || '%') THEN RAISE EXCEPTION 'class/root mismatch (optimizer=%): %', opt, dump; END IF; IF dump NOT LIKE ('%reducer order:' || chr(10) || '%') THEN @@ -67,11 +67,11 @@ BEGIN -- Replicated -> hash is local; the reverse broadcast stays global. dump := rpt_transfer_plan('SELECT * FROM rpt_a JOIN rpt_r ON rpt_a.a = rpt_r.a WHERE rpt_a.b > 0 AND rpt_r.a > 0'); -- Resolve table names from vertex records: ORCA can reorder rtindexes. - distributed_id := substring(dump FROM 'vertex ([0-9]+) rpt_a rows='); - replicated_id := substring(dump FROM 'vertex ([0-9]+) rpt_r rows='); - IF distributed_id IS NULL OR replicated_id IS NULL OR - dump NOT LIKE ('%forward hop ' || replicated_id || '->' || distributed_id || ' merge=local%') OR - dump NOT LIKE ('%backward hop ' || distributed_id || '->' || replicated_id || ' merge=global%') THEN + distributed_rtindex := substring(dump FROM 'vertex ([0-9]+) rpt_a rows='); + replicated_rtindex := substring(dump FROM 'vertex ([0-9]+) rpt_r rows='); + IF distributed_rtindex IS NULL OR replicated_rtindex IS NULL OR + dump NOT LIKE ('%forward hop ' || replicated_rtindex || '->' || distributed_rtindex || ' merge=local%') OR + dump NOT LIKE ('%backward hop ' || distributed_rtindex || '->' || replicated_rtindex || ' merge=global%') THEN RAISE EXCEPTION 'replicated direction mismatch (optimizer=%): %', opt, dump; END IF; dump := rpt_transfer_plan('SELECT * FROM rpt_a JOIN rpt_d ON rpt_a.a = rpt_d.x AND rpt_a.b = rpt_d.y WHERE rpt_a.b > 50'); @@ -91,12 +91,12 @@ BEGIN ) AS cases(query, preserved) LOOP dump := rpt_transfer_plan(q); - preserved_id := substring(dump FROM ('vertex ([0-9]+) ' || preserved_name || ' rows=')); - IF preserved_id IS NULL THEN + preserved_rtindex := substring(dump FROM ('vertex ([0-9]+) ' || preserved_name || ' rows=')); + IF preserved_rtindex IS NULL THEN RAISE EXCEPTION 'missing preserved table % (optimizer=%): %', preserved_name, opt, dump; END IF; IF EXISTS (SELECT FROM regexp_split_to_table(dump, chr(10)) line - WHERE line LIKE ('vertex ' || preserved_id || ' %') + WHERE line LIKE ('vertex ' || preserved_rtindex || ' %') AND line LIKE '%apply from%') THEN RAISE EXCEPTION 'preserved side reduced: %', dump; END IF; diff --git a/gpcontrib/robust_predicate_transfer/src/rptbackward.cpp b/gpcontrib/robust_predicate_transfer/src/rptbackward.cpp new file mode 100644 index 00000000000..cb47b243a32 --- /dev/null +++ b/gpcontrib/robust_predicate_transfer/src/rptbackward.cpp @@ -0,0 +1,142 @@ +/*------------------------------------------------------------------------- + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * rptbackward.cpp + * Backward placement on the scan stacks built by the forward pass. + * + *------------------------------------------------------------------------- + */ +#include "rptinject.h" + +extern "C" +{ +#include "anserplan.h" +} + +/* The proof names the original parent, not a wrapper added by forward. */ +static bool +rpt_backward_endpoint(const RptTransferMgr &schedule, + const RptInjectionState &state, int rtindex, + const RptKey &key, Plan *parent, AttrNumber attno) +{ + const auto &vertex = schedule.vertices.at(schedule.vertex_index_by_rtindex.at(rtindex)); + return vertex.scan != NULL && IsA(vertex.scan, SeqScan) && + RptStackMatches(state, vertex.scan, parent) && + attno > 0 && RptScanKeyPosition(vertex.scan, key) == attno; +} + +void +RptInjectBackward(PlannedStmt *stmt, const RptTransferMgr &schedule, + const std::vector &forward, + const RptForwardParams ¶ms, RptFilterSizeFn size_fn, + bool materialize, RptInjectionState *state, + std::string *trace, bool *injected) +{ + size_t root_classes = 0, strict = 0, eligible = 0; + for (const auto &group : schedule.groups) + root_classes += schedule.root_rtindexes.count(group.second.leader_rtindex); + for (const auto &proof : schedule.backward_consumers) + { + strict += proof.strict_eligible; + eligible += proof.eligible; + } + *trace += "runtime survey classes=" + std::to_string(schedule.groups.size()) + + " root_classes=" + std::to_string(root_classes) + + " strict=" + std::to_string(strict) + " eligible=" + std::to_string(eligible) + "\n"; + std::map widths; + for (const auto &decision : schedule.backward_consumers) + if (decision.eligible) + { + for (int slice_index : {decision.producer_slice_index, decision.consumer_slice_index}) + if (!widths.count(slice_index)) + widths[slice_index] = RptPgCall([&]() { + return AnserSliceProducers(stmt, slice_index); + }); + } + std::vector classes; + RptSelectBackward(schedule, forward, params, widths, size_fn, &classes); + for (auto &cls : classes) + { + if (!cls.accepted) + continue; + /* Class-wide preflight before allocating any executable node. */ + for (const auto &target : cls.consumers) + { + if (!target.accepted) + continue; + const auto &proof = target.proof; + if (!(proof.producer_key == cls.producer_key) || + widths[proof.producer_slice_index] != cls.producer_processes || + !rpt_backward_endpoint(schedule, *state, cls.producer_rtindex, + cls.producer_key, proof.producer_parent, proof.producer_attno) || + !rpt_backward_endpoint(schedule, *state, proof.consumer_rtindex, + proof.consumer_key, proof.consumer_parent, proof.consumer_attno)) + { + cls.accepted = false; + cls.reason = "backward placement changed or key is unavailable"; + break; + } + } + if (!cls.accepted) + { + for (auto &target : cls.consumers) + if (target.accepted) + { + target.accepted = false; + target.reason = cls.reason; + } + continue; + } + cls.condition_id = state->next_condition_id++; + cls.condition_key = "rpt_b_" + std::to_string(cls.producer_rtindex) + "_" + + std::to_string(cls.class_key.rtindex) + "_" + std::to_string(cls.class_key.attno); + if (!materialize) + continue; + Plan *scan = schedule.vertices.at( + schedule.vertex_index_by_rtindex.at(cls.producer_rtindex)).scan; + Plan *producer = RptPgCall([&]() { + CustomScan *node = AnserBuildBloomProducerScan(state->tops.at(scan), + RptScanKeyPosition(scan, cls.producer_key), cls.condition_id, + cls.condition_key.c_str(), cls.total_elems, (Size) cls.max_payload, + cls.planned_bytes, cls.producer_processes); + node->scan.plan.plan_node_id = state->next_plan_node_id++; + return (Plan *) node; + }); + RptRelinkStack(state, scan, producer); + for (const auto &target : cls.consumers) + { + if (!target.accepted) + continue; + const auto &proof = target.proof; + scan = schedule.vertices.at( + schedule.vertex_index_by_rtindex.at(proof.consumer_rtindex)).scan; + Plan *consumer = RptPgCall([&]() { + CustomScan *node = AnserBuildBloomConsumerScan(state->tops.at(scan), + proof.consumer_attno, cls.condition_id, cls.condition_key.c_str(), + cls.total_elems, (Size) cls.max_payload, cls.planned_bytes, + cls.producer_processes, proof.defer_first); + node->scan.plan.plan_node_id = state->next_plan_node_id++; + return (Plan *) node; + }); + RptRelinkStack(state, scan, consumer); + } + *injected = true; + } + RptDumpBackward(classes, trace); +} diff --git a/gpcontrib/robust_predicate_transfer/src/rptbackwardplan.cpp b/gpcontrib/robust_predicate_transfer/src/rptbackwardplan.cpp new file mode 100644 index 00000000000..4985f234779 --- /dev/null +++ b/gpcontrib/robust_predicate_transfer/src/rptbackwardplan.cpp @@ -0,0 +1,159 @@ +/*------------------------------------------------------------------------- + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * rptbackwardplan.cpp + * Pure backward selection over the shared schedule and eligibility proof. + * + *------------------------------------------------------------------------- + */ +#include "rpt.h" +#include + +static int +rpt_slice_processes(const std::map &widths, int slice_index) +{ + auto found = widths.find(slice_index); + return found == widths.end() ? 0 : found->second; +} + +void +RptSelectBackward(const RptTransferMgr &schedule, + const std::vector &forward, + const RptForwardParams ¶ms, + const std::map &slice_processes, + RptFilterSizeFn size_fn, std::vector *out) +{ + out->clear(); + std::map > decisions; + for (const auto &decision : schedule.backward_consumers) + decisions[decision.class_key].push_back(decision); + for (const auto &entry : decisions) + { + RptBackwardClass cls; + cls.class_key = entry.first; + auto chosen = std::find_if(entry.second.begin(), entry.second.end(), + [](const RptBackwardConsumer &d) { return d.eligible; }); + const auto &first = chosen == entry.second.end() ? entry.second.front() : *chosen; + cls.producer_rtindex = first.producer_rtindex; + cls.producer_key = first.producer_key; + cls.producer_processes = rpt_slice_processes(slice_processes, + first.producer_slice_index); + const auto &producer = schedule.vertices.at( + schedule.vertex_index_by_rtindex.at(cls.producer_rtindex)); + double root_keep = 1; + for (const auto &comp : forward) + if (comp.accepted && comp.root_rtindex == cls.producer_rtindex) + root_keep = comp.est_selectivity; + cls.estimated_producer_rows = producer.cardinality * root_keep; + bool any = false; + for (const auto &decision : entry.second) + { + RptBackwardTarget target; + target.proof = decision; + target.reason = decision.reason; + target.processes = rpt_slice_processes(slice_processes, + decision.consumer_slice_index); + double rows = schedule.vertices.at( + schedule.vertex_index_by_rtindex.at(decision.consumer_rtindex)).cardinality; + double kept = 1; + if (decision.estimated_rows_removed >= 0 && rows > 0) + { + kept = std::max(0.0, std::min(1.0, + 1 - decision.estimated_rows_removed / rows)) * root_keep; + target.estimated_rows_removed = rows * (1 - kept); + } + if (decision.eligible) + { + if (target.processes <= 0) + target.reason = "unknown consumer process count"; + else if (params.max_selectivity < 1 && kept >= params.max_selectivity) + target.reason = "estimated member selectivity not below rpt.forward_max_selectivity"; + else + { + target.accepted = true; + target.reason = "inject"; + any = true; + } + } + cls.consumers.push_back(target); + } + if (!schedule.root_rtindexes.count(cls.producer_rtindex)) + cls.reason = "bridge-led class is deferred"; + else if (!any) + cls.reason = "no eligible consumers"; + else if (cls.producer_processes <= 0) + cls.reason = "unknown producer process count"; + else if (!size_fn(cls.estimated_producer_rows, &cls.total_elems, + &cls.max_payload, &cls.planned_bytes)) + cls.reason = "backward filter sizing declined"; + else + { + int64 consumer_processes = 0; + for (const auto &target : cls.consumers) + if (target.accepted) + consumer_processes += target.processes; + double mib = cls.planned_bytes / (1024.0 * 1024.0); + cls.est_cost_ms = mib * + (params.ms_per_mib_part * cls.producer_processes + + params.delivery_ms_per_mib * consumer_processes); + if (params.max_exchange_ms >= 0 && cls.est_cost_ms > params.max_exchange_ms) + cls.reason = "estimated exchange cost above rpt.forward_max_exchange_ms"; + else + { + cls.accepted = true; + cls.reason = "inject"; + } + } + if (!cls.accepted) + for (auto &target : cls.consumers) + if (target.accepted) + { + target.accepted = false; + target.reason = cls.reason; + } + out->push_back(cls); + } +} + +void +RptDumpBackward(const std::vector &classes, std::string *out) +{ + std::ostringstream text; + text << std::fixed << std::setprecision(3); + for (const auto &cls : classes) + { + size_t accepted = 0; + for (const auto &target : cls.consumers) + accepted += target.accepted; + text << "backward class root=" << cls.producer_rtindex + << " rep=" << cls.class_key.rtindex << '.' << cls.class_key.attno + << " consumers=" << accepted << " bytes=" << cls.planned_bytes + << " est_cost_ms=" << cls.est_cost_ms + << " decision=" << cls.reason << '\n'; + for (const auto &target : cls.consumers) + text << "backward consumer " << target.proof.consumer_rtindex + << " root=" << cls.producer_rtindex + << " rep=" << cls.class_key.rtindex << '.' << cls.class_key.attno + << " key=" << (cls.condition_key.empty() ? "-" : cls.condition_key) + << " defer_first=" << (target.proof.defer_first ? "yes" : "no") + << " rows_removed=" << target.estimated_rows_removed + << " decision=" << target.reason << '\n'; + } + *out += text.str(); +} diff --git a/gpcontrib/robust_predicate_transfer/src/rpteligible.cpp b/gpcontrib/robust_predicate_transfer/src/rpteligible.cpp index dc481d85740..653394192f1 100644 --- a/gpcontrib/robust_predicate_transfer/src/rpteligible.cpp +++ b/gpcontrib/robust_predicate_transfer/src/rpteligible.cpp @@ -18,8 +18,7 @@ * under the License. * * rpteligible.cpp - * Analysis-only backward eligibility and refusal diagnostics. These - * proofs are used by the backward eligibility survey. + * Backward eligibility proof shared by runtime injection and the survey. * * IDENTIFICATION * gpcontrib/robust_predicate_transfer/src/rpteligible.cpp @@ -39,7 +38,7 @@ extern "C" struct RptPlacement { std::vector path; - int slice = -1; + int slice_index = -1; bool ambiguous = false; bool in_subplan = false; }; @@ -50,7 +49,7 @@ struct RptPlacementContext std::set scans; std::map placements; std::vector path; - int slice = 0; + int slice_index = 0; bool in_subplan = false; }; @@ -63,9 +62,9 @@ rpt_placement_walker(Node *node, void *context) if (node == NULL) return false; bool list = IsA(node, List) || IsA(node, IntList) || IsA(node, OidList); - int saved_slice = ctx->slice; + int saved_slice_index = ctx->slice_index; if (IsA(node, Motion)) - ctx->slice = ((Motion *) node)->motionID; + ctx->slice_index = ((Motion *) node)->motionID; if (!list) ctx->path.push_back((Plan *) node); if (ctx->scans.count((Plan *) node)) @@ -77,7 +76,7 @@ rpt_placement_walker(Node *node, void *context) else { placement.path = ctx->path; - placement.slice = ctx->slice; + placement.slice_index = ctx->slice_index; } } bool result = RptPgCall([&]() { @@ -85,7 +84,7 @@ rpt_placement_walker(Node *node, void *context) }); if (!list) ctx->path.pop_back(); - ctx->slice = saved_slice; + ctx->slice_index = saved_slice_index; return result; } @@ -144,19 +143,19 @@ rpt_path_key_slot(const RptPlacement &placement, size_t top, const RptKey &key) } static int -rpt_path_slice(const RptPlacement &placement, size_t top) +rpt_path_slice_index(const RptPlacement &placement, size_t top) { - int slice = 0; + int slice_index = 0; for (size_t i = 0; i <= top; ++i) if (IsA(placement.path[i], Motion)) - slice = ((Motion *) placement.path[i])->motionID; - return slice; + slice_index = ((Motion *) placement.path[i])->motionID; + return slice_index; } static bool rpt_resolve_placement(const RptPlacement &placement, size_t top, const RptKey &key, bool consumer, - Plan **parent, AttrNumber *attno, int *slice) + Plan **parent, AttrNumber *attno, int *slice_index) { AttrNumber slot = rpt_path_key_slot(placement, top, key); if (slot <= 0 || placement.path.size() < 2) @@ -167,14 +166,14 @@ rpt_resolve_placement(const RptPlacement &placement, size_t top, { *parent = placement.path[top - 1]; *attno = slot; - *slice = placement.slice; + *slice_index = placement.slice_index; return outerPlan(*parent) == scan; } bool ok = RptPgCall([&]() { return AnserResolveKeyScan(placement.path[top], slot, - rpt_path_slice(placement, top), consumer, parent, &resolved, attno, slice); + rpt_path_slice_index(placement, top), consumer, parent, &resolved, attno, slice_index); }); - return ok && resolved == scan && *slice == placement.slice; + return ok && resolved == scan && *slice_index == placement.slice_index; } static const char * @@ -242,7 +241,7 @@ rpt_prove_consumer(RptBackwardConsumer *decision, const RptKey &producer_key, decision->reason = "scan has multiple plan placements"; return; } - if (producer.slice < 0 || consumer.slice < 0 || producer.slice == consumer.slice) + if (producer.slice_index < 0 || consumer.slice_index < 0 || producer.slice_index == consumer.slice_index) { decision->reason = "same or unknown slice"; return; @@ -276,14 +275,14 @@ rpt_prove_consumer(RptBackwardConsumer *decision, const RptKey &producer_key, } if (!rpt_resolve_placement(producer, build - producer.path.begin(), producer_key, false, &decision->producer_parent, - &decision->producer_attno, &decision->producer_slice)) + &decision->producer_attno, &decision->producer_slice_index)) { decision->reason = "unsupported producer key path"; continue; } if (!rpt_resolve_placement(consumer, i + 1, consumer_key, true, &decision->consumer_parent, &decision->consumer_attno, - &decision->consumer_slice)) + &decision->consumer_slice_index)) { decision->reason = "unsupported consumer key path"; continue; @@ -309,10 +308,10 @@ rpt_cap_backward_consumers(RptTransferMgr *mgr) const auto &left = mgr->backward_consumers[a]; const auto &right = mgr->backward_consumers[b]; double l = left.estimated_rows_removed >= 0 ? left.estimated_rows_removed : - mgr->vertices[mgr->by_rti.at(left.consumer)].cardinality; + mgr->vertices[mgr->vertex_index_by_rtindex.at(left.consumer_rtindex)].cardinality; double r = right.estimated_rows_removed >= 0 ? right.estimated_rows_removed : - mgr->vertices[mgr->by_rti.at(right.consumer)].cardinality; - return l > r || (l == r && left.consumer < right.consumer); + mgr->vertices[mgr->vertex_index_by_rtindex.at(right.consumer_rtindex)].cardinality; + return l > r || (l == r && left.consumer_rtindex < right.consumer_rtindex); }); for (size_t i = rpt_max_backward_consumers; i < indices.size(); ++i) { @@ -327,20 +326,20 @@ static void rpt_consumer_verdicts(RptTransferMgr *mgr) { for (const auto &vertex : mgr->vertices) - mgr->consumer_ok[vertex.id] = false; + mgr->consumer_ok_by_rtindex[vertex.rtindex] = false; std::map all_global; for (const auto &decision : mgr->backward_consumers) { if (decision.local) continue; - auto found = all_global.find(decision.consumer); + auto found = all_global.find(decision.consumer_rtindex); if (found == all_global.end()) - all_global[decision.consumer] = decision.eligible; + all_global[decision.consumer_rtindex] = decision.eligible; else found->second = found->second && decision.eligible; } for (const auto &entry : all_global) - mgr->consumer_ok[entry.first] = entry.second; + mgr->consumer_ok_by_rtindex[entry.first] = entry.second; } void @@ -349,7 +348,7 @@ RptCheckBackwardConsumers(PlannedStmt *stmt, RptTransferMgr *mgr) RptPlacementContext context; for (const auto &vertex : mgr->vertices) { - mgr->consumer_ok[vertex.id] = false; + mgr->consumer_ok_by_rtindex[vertex.rtindex] = false; context.scans.insert(vertex.scan); } mgr->backward_consumers.clear(); @@ -360,7 +359,7 @@ RptCheckBackwardConsumers(PlannedStmt *stmt, RptTransferMgr *mgr) ListCell *lc; foreach(lc, stmt->subplans) { - context.slice = -1; + context.slice_index = -1; context.in_subplan = true; /* Motions must not clear the SubPlan refusal. */ rpt_placement_walker((Node *) lfirst(lc), &context); } @@ -374,32 +373,34 @@ RptCheckBackwardConsumers(PlannedStmt *stmt, RptTransferMgr *mgr) RptKey source = edge.right_keys[key]; RptKey target = edge.left_keys[key]; RptKey rep = RptFindKeyRoot(source, mgr->uf_parent); - if (!seen.insert(std::make_pair(edge.destination, rep)).second) + if (!seen.insert(std::make_pair(edge.other_rtindex, rep)).second) continue; RptBackwardConsumer decision; decision.class_key = rep; - decision.producer = node.first; - decision.consumer = edge.destination; + decision.producer_key = source; + decision.consumer_key = target; + decision.producer_rtindex = node.first; + decision.consumer_rtindex = edge.other_rtindex; decision.local = edge.local; auto group = mgr->groups.find(rep); - auto source_placement = context.placements.find(mgr->vertices[mgr->by_rti.at(node.first)].scan); - auto target_placement = context.placements.find(mgr->vertices[mgr->by_rti.at(edge.destination)].scan); + auto source_placement = context.placements.find(mgr->vertices[mgr->vertex_index_by_rtindex.at(node.first)].scan); + auto target_placement = context.placements.find(mgr->vertices[mgr->vertex_index_by_rtindex.at(edge.other_rtindex)].scan); if ((source_placement != context.placements.end() && source_placement->second.in_subplan) || (target_placement != context.placements.end() && target_placement->second.in_subplan)) decision.reason = "scan inside a SubPlan"; else if (edge.local) decision.reason = "local edge uses core runtime filtering"; - else if (group == mgr->groups.end() || group->second.leader_id != node.first) + else if (group == mgr->groups.end() || group->second.leader_rtindex != node.first) decision.reason = "edge has no unprotected equivalence class"; - else if (!mgr->roots.count(node.first)) + else if (!mgr->root_rtindexes.count(node.first)) decision.reason = "bridge-led class is deferred"; - else if (!context.placements.count(mgr->vertices[mgr->by_rti.at(node.first)].scan) || - !context.placements.count(mgr->vertices[mgr->by_rti.at(edge.destination)].scan)) + else if (!context.placements.count(mgr->vertices[mgr->vertex_index_by_rtindex.at(node.first)].scan) || + !context.placements.count(mgr->vertices[mgr->vertex_index_by_rtindex.at(edge.other_rtindex)].scan)) decision.reason = "scan placement not found"; else { - const auto &producer = mgr->vertices[mgr->by_rti.at(node.first)]; - const auto &consumer = mgr->vertices[mgr->by_rti.at(edge.destination)]; + const auto &producer = mgr->vertices[mgr->vertex_index_by_rtindex.at(node.first)]; + const auto &consumer = mgr->vertices[mgr->vertex_index_by_rtindex.at(edge.other_rtindex)]; rpt_prove_consumer(&decision, source, target, context.placements.at(producer.scan), context.placements.at(consumer.scan)); if (decision.strict_eligible) diff --git a/gpcontrib/robust_predicate_transfer/src/rptforward.cpp b/gpcontrib/robust_predicate_transfer/src/rptforward.cpp index b768e76a1b1..b7d30dbf1d8 100644 --- a/gpcontrib/robust_predicate_transfer/src/rptforward.cpp +++ b/gpcontrib/robust_predicate_transfer/src/rptforward.cpp @@ -19,15 +19,17 @@ * * rptforward.cpp * Build the forward reducer as an InitPlan of the statement - * and apply its filters to the root scans of the main plan. + * and apply its filters to the root scans of the main plan, then add + * backward filters through the shared injection entry point. * * A transfer is an Anser channel, except a local hop consumed only inside * the reducer, which is handed over in-process (rptlocal.c). The reducer * InitPlan is dispatched by * preprocess_initplans() before the main plan, under the same * gp_command_count, and the coordinator keeps a command's channels until - * ExecutorEnd -- so every channel is complete before a main-plan consumer - * subscribes, and main-plan consumers never wait on anything still running. + * ExecutorEnd -- so every forward channel is complete before a main-plan consumer + * subscribes. Backward channels are produced in the main plan and use the + * survey proof to place consumers that can safely wait. * * Reducer scans are copies of the main plan's scans (same scanrelid, so the * same RTE, locks and permission checks) with a key-only targetlist. Their @@ -48,6 +50,7 @@ *------------------------------------------------------------------------- */ #include "rpt.h" +#include "rptinject.h" extern "C" { @@ -81,6 +84,8 @@ typedef struct RptParentContext const std::set *targets; std::map *parents; std::vector *stack; /* enclosing plan nodes */ + std::map *scan_slices; + int slice_index; } RptParentContext; static bool @@ -93,20 +98,30 @@ rpt_parent_walker(Node *node, void *context) if (!is_plan_node(node)) return plan_tree_walker(node, rpt_parent_walker, context, false); + int saved_slice_index = ctx->slice_index; + if (IsA(node, Motion)) + ctx->slice_index = ((Motion *) node)->motionID; if (ctx->targets->count((Plan *) node) && !ctx->stack->empty()) - (*ctx->parents)[(Plan *) node] = ctx->stack->back(); + { + auto inserted = ctx->parents->emplace((Plan *) node, ctx->stack->back()); + if (!inserted.second) + inserted.first->second = NULL; /* ambiguous placement */ + (*ctx->scan_slices)[(Plan *) node] = ctx->slice_index; + } ctx->stack->push_back((Plan *) node); bool result = plan_tree_walker(node, rpt_parent_walker, context, false); ctx->stack->pop_back(); + ctx->slice_index = saved_slice_index; return result; } -/* Parents of the targets in the main plan tree only; SubPlans are not - * entered, so a scan inside one is simply not found. */ +/* Preserve original parents and slice indexes in the main tree. SubPlan IDs + * are resolved while visiting expressions, but their plans are not entered. */ static std::map -rpt_find_parents(PlannedStmt *stmt, const std::set &targets) +rpt_find_parents(PlannedStmt *stmt, const std::set &targets, + std::map *scan_slices) { std::map parents; std::vector stack; @@ -115,6 +130,8 @@ rpt_find_parents(PlannedStmt *stmt, const std::set &targets) ctx.targets = &targets; ctx.parents = &parents; ctx.stack = &stack; + ctx.scan_slices = scan_slices; + ctx.slice_index = 0; RptPgCall([&]() { exec_init_plan_tree_base(&ctx.base, stmt); (void) rpt_parent_walker((Node *) stmt->planTree, &ctx); @@ -154,9 +171,11 @@ rpt_relink(Plan *parent, Plan *child, Plan *replacement) } /* Output position of a base column in a scan's targetlist, or 0. */ -static AttrNumber -rpt_scan_key_position(Plan *scan, const RptKey &key) +AttrNumber +RptScanKeyPosition(Plan *scan, const RptKey &key) { + if (!IsA(scan, SeqScan) || ((Scan *) scan)->scanrelid != (Index) key.rtindex) + return 0; ListCell *lc; foreach(lc, scan->targetlist) @@ -171,6 +190,36 @@ rpt_scan_key_position(Plan *scan, const RptKey &key) return 0; } +/* Verify the original parent still holds the current top, exactly once. */ +bool +RptStackMatches(const RptInjectionState &state, Plan *scan, Plan *expected_parent) +{ + auto parent = state.parents.find(scan); + auto top = state.tops.find(scan); + if (parent == state.parents.end() || parent->second == NULL || + parent->second != expected_parent || top == state.tops.end()) + return false; + int matches = (parent->second->lefttree == top->second) + + (parent->second->righttree == top->second); + if (IsA(parent->second, CustomScan)) + { + ListCell *lc; + foreach(lc, ((CustomScan *) parent->second)->custom_plans) + matches += lfirst(lc) == top->second; + } + return matches == 1; +} + +void +RptRelinkStack(RptInjectionState *state, Plan *scan, Plan *top) +{ + Plan *parent = state->parents.at(scan); + if (!RptStackMatches(*state, scan, parent) || + !rpt_relink(parent, state->tops.at(scan), top)) + RptPgCall([]() { elog(ERROR, "RPT scan stack changed after preflight"); }); + state->tops.at(scan) = top; +} + /* ---- quals a reducer scan may evaluate ---- */ /* Only explicitly trusted built-in comparisons and their hash support may @@ -277,7 +326,7 @@ rpt_vertex_estimates(const RptTransferMgr &forward) e.rows = base > 0 ? base : (double) v.cardinality; if (v.scan->qual != NIL && !e.quals_dropped && base > 0) e.selectivity = std::min(1.0, std::max(0.0, v.scan->plan_rows / base)); - est[v.id] = e; + est[v.rtindex] = e; } return est; } @@ -285,9 +334,9 @@ rpt_vertex_estimates(const RptTransferMgr &forward) /* ---- node construction ---- */ static void -rpt_reset_copied_plan(Plan *plan, int *next_id) +rpt_reset_copied_plan(Plan *plan, int *next_plan_node_id) { - plan->plan_node_id = (*next_id)++; + plan->plan_node_id = (*next_plan_node_id)++; plan->lefttree = NULL; plan->righttree = NULL; plan->initPlan = NIL; @@ -301,7 +350,7 @@ rpt_reset_copied_plan(Plan *plan, int *next_id) static Plan * rpt_reducer_scan(Plan *main_scan, Oid relid, const std::vector &attnos, - bool *quals_dropped, int *next_id) + bool *quals_dropped, int *next_plan_node_id) { Plan *scan = (Plan *) copyObjectImpl(main_scan); List *tlist = NIL; @@ -322,13 +371,13 @@ rpt_reducer_scan(Plan *main_scan, Oid relid, const std::vector &attn scan->targetlist = tlist; scan->qual = rpt_reducer_quals(main_scan->qual); *quals_dropped = (main_scan->qual != NIL && scan->qual == NIL); - rpt_reset_copied_plan(scan, next_id); + rpt_reset_copied_plan(scan, next_plan_node_id); return scan; } static Plan * rpt_wrap_consumer(Plan *child, AttrNumber position, const RptForwardChannel &ch, - int width, int *next_id) + int width, int *next_plan_node_id) { CustomScan *cs = AnserBuildBloomConsumerScan(child, position, ch.condition_id, ch.condition_key.c_str(), @@ -336,20 +385,20 @@ rpt_wrap_consumer(Plan *child, AttrNumber position, const RptForwardChannel &ch, ch.planned_bytes, width, false /* channels are complete */ ); - cs->scan.plan.plan_node_id = (*next_id)++; + cs->scan.plan.plan_node_id = (*next_plan_node_id)++; return (Plan *) cs; } static Plan * rpt_wrap_producer(Plan *child, AttrNumber position, const RptForwardChannel &ch, - int width, int *next_id) + int width, int *next_plan_node_id) { CustomScan *cs = AnserBuildBloomProducerScan(child, position, ch.condition_id, ch.condition_key.c_str(), ch.total_elems, (Size) ch.max_payload, ch.planned_bytes, width); - cs->scan.plan.plan_node_id = (*next_id)++; + cs->scan.plan.plan_node_id = (*next_plan_node_id)++; return (Plan *) cs; } @@ -392,207 +441,260 @@ rpt_has_runtime_filters(PlannedStmt *stmt) static bool rpt_apply_in_main(const RptVertexInfo &vertex, const std::vector &channels, - const std::map &parents, int width, - int *next_id, bool dry_run) + RptInjectionState *state, int width, + bool dry_run) { - auto parent = parents.find(vertex.scan); + auto parent = state->parents.find(vertex.scan); - if (!IsA(vertex.scan, SeqScan) || parent == parents.end()) + if (!IsA(vertex.scan, SeqScan) || parent == state->parents.end() || + !RptStackMatches(*state, vertex.scan, parent->second)) return false; for (const RptForwardChannel &ch : channels) - if (ch.to == vertex.id && !ch.in_process && - rpt_scan_key_position(vertex.scan, ch.to_key) == 0) + if (ch.target_rtindex == vertex.rtindex && !ch.in_process && + RptScanKeyPosition(vertex.scan, ch.target_key) == 0) return false; if (dry_run) return true; - Plan *node = vertex.scan; + Plan *node = state->tops.at(vertex.scan); for (const RptForwardChannel &ch : channels) - if (ch.to == vertex.id && !ch.in_process) + if (ch.target_rtindex == vertex.rtindex && !ch.in_process) node = RptPgCall([&]() { - return rpt_wrap_consumer(node, rpt_scan_key_position(vertex.scan, ch.to_key), - ch, width, next_id); + return rpt_wrap_consumer(node, RptScanKeyPosition(vertex.scan, ch.target_key), + ch, width, &state->next_plan_node_id); }); - return node != vertex.scan && rpt_relink(parent->second, vertex.scan, node); + if (node == state->tops.at(vertex.scan)) + return false; + RptRelinkStack(state, vertex.scan, node); + return true; } -PlannedStmt * -RptInjectForward(PlannedStmt *stmt, bool materialize, std::string *trace) +/* Channel identities are assigned only after placement accepts a component. */ +static void +rpt_assign_forward_channels(std::vector &comps, + RptInjectionState &state) { - if (!gp_anser_enable || gp_anser_runtime_filter) - { - *trace += "forward: needs anser.enable=on and anser.runtime_filter=off\n"; - return stmt; - } - if (rpt_has_runtime_filters(stmt)) - { - *trace += "forward: plan already carries Anser nodes\n"; - return stmt; - } - - PlannedStmt *work = materialize - ? RptPgCall([&]() { return (PlannedStmt *) copyObjectImpl(stmt); }) - : stmt; - - RptTransferMgr analysis; - - RptCollectGraph(work, &analysis); - if (analysis.vertices.empty()) - { - *trace += "forward: no transfer regions\n"; - return stmt; - } - RptBuildClasses(&analysis); - RptBuildTransferPlan(work, &analysis); - - RptTransferMgr forward; - - RptBuildForwardPlan(&analysis, &forward); - - RptForwardParams params; - - params.width = RptPgCall([]() { return getgpsegmentCount(); }); - params.ms_per_mib_part = rpt_forward_ms_per_mib_part; - params.max_exchange_ms = rpt_forward_max_exchange_ms; - params.min_root_rows = rpt_forward_min_root_rows; - params.max_selectivity = rpt_forward_max_selectivity; - params.apply_all = (rpt_forward_apply == RPT_FORWARD_APPLY_ALL); - - std::map est = rpt_vertex_estimates(forward); - std::vector comps; + for (auto &comp : comps) + if (comp.accepted) + for (auto &ch : comp.channels) + { + ch.condition_id = state.next_condition_id++; + ch.condition_key = "rpt_f_" + std::to_string(ch.source_rtindex) + "_" + + std::to_string(ch.target_rtindex); + } +} +/* Build the forward part using the statement's shared IDs and scan stacks. + * The selected components also supply root selectivity to backward sizing. */ +static List * +RptInjectForward(const RptTransferMgr &forward, + const RptForwardParams ¶ms, + const std::map &est, + bool materialize, RptInjectionState &state, + std::vector &comps, + std::string *trace, bool *injected) +{ RptSelectForward(forward, params, est, rpt_anser_size, RptEstimateDistinct, &comps); - /* Preflight against the plan: everything that could refuse a component - * is checked before any node is built, so a refusal changes nothing. */ - std::set targets; - - for (const RptVertexInfo &v : forward.vertices) - targets.insert(v.scan); - - std::map parents = rpt_find_parents(work, targets); - int next_id = 0; /* unused by dry runs */ - for (RptForwardComponent &comp : comps) { if (!comp.accepted) continue; - for (int id : comp.chain) + for (int rtindex : comp.chain_rtindexes) { - const RptVertexInfo &v = forward.vertices[forward.by_rti.at(id)]; + const RptVertexInfo &v = forward.vertices[forward.vertex_index_by_rtindex.at(rtindex)]; if (!IsA(v.scan, SeqScan) || !OidIsValid(v.relid)) { comp.accepted = false; - comp.reason = "chain table " + std::to_string(id) + " is not a plain SeqScan"; + comp.reason = "chain table " + std::to_string(rtindex) + " is not a plain SeqScan"; break; } } if (comp.accepted && - !rpt_apply_in_main(forward.vertices[forward.by_rti.at(comp.root)], - comp.channels, parents, params.width, &next_id, true)) + !rpt_apply_in_main(forward.vertices[forward.vertex_index_by_rtindex.at(comp.root_rtindex)], + comp.channels, &state, params.width, true)) { comp.accepted = false; comp.reason = "root scan not found, not a SeqScan, or key not in its output"; } } + rpt_assign_forward_channels(comps, state); RptDumpForward(comps, trace); for (const RptForwardComponent &comp : comps) - for (int id : comp.chain) - if (est[id].quals_dropped) - *trace += "forward: quals of " + std::to_string(id) + + for (int rtindex : comp.chain_rtindexes) + if (est.at(rtindex).quals_dropped) + *trace += "forward: quals of " + std::to_string(rtindex) + " dropped in the reducer (not provably error-free)\n"; - bool any = false; - for (const RptForwardComponent &comp : comps) - any = any || comp.accepted; - if (!materialize || !any) - return stmt; - - next_id = RptPgCall([&]() { return AnserMaxPlanNodeId(work); }) + 1; + *injected = *injected || comp.accepted; List *children = NIL; for (const RptForwardComponent &comp : comps) { - if (!comp.accepted) + if (!materialize || !comp.accepted) continue; - for (int id : comp.chain) + for (int rtindex : comp.chain_rtindexes) { - const RptVertexInfo &v = forward.vertices[forward.by_rti.at(id)]; + const RptVertexInfo &v = forward.vertices[forward.vertex_index_by_rtindex.at(rtindex)]; std::vector attnos; for (const RptForwardChannel &ch : comp.channels) { AttrNumber a = 0; - if (ch.to == id) - a = ch.to_key.attno; - else if (ch.from == id) - a = ch.from_key.attno; + if (ch.target_rtindex == rtindex) + a = ch.target_key.attno; + else if (ch.source_rtindex == rtindex) + a = ch.source_key.attno; if (a != 0 && std::find(attnos.begin(), attnos.end(), a) == attnos.end()) attnos.push_back(a); } bool dropped = false; /* reported with the dump */ Plan *node = RptPgCall([&]() { - return rpt_reducer_scan(v.scan, v.relid, attnos, &dropped, &next_id); + return rpt_reducer_scan(v.scan, v.relid, attnos, &dropped, &state.next_plan_node_id); }); /* Consumers below producers: a filter is built from rows the * inbound filters already reduced. */ for (const RptForwardChannel &ch : comp.channels) { - if (ch.to != id) + if (ch.target_rtindex != rtindex) continue; AttrNumber pos = (AttrNumber) (std::find(attnos.begin(), attnos.end(), - ch.to_key.attno) - attnos.begin()) + 1; + ch.target_key.attno) - attnos.begin()) + 1; node = RptPgCall([&]() { if (!ch.in_process) - return rpt_wrap_consumer(node, pos, ch, params.width, &next_id); + return rpt_wrap_consumer(node, pos, ch, params.width, &state.next_plan_node_id); CustomScan *cs = RptBuildLocalConsumerScan(node, pos, ch.condition_id, ch.total_elems); - cs->scan.plan.plan_node_id = next_id++; + cs->scan.plan.plan_node_id = state.next_plan_node_id++; return (Plan *) cs; }); } for (const RptForwardChannel &ch : comp.channels) { - if (ch.from != id) + if (ch.source_rtindex != rtindex) continue; AttrNumber pos = (AttrNumber) (std::find(attnos.begin(), attnos.end(), - ch.from_key.attno) - attnos.begin()) + 1; + ch.source_key.attno) - attnos.begin()) + 1; node = RptPgCall([&]() { if (!ch.in_process) - return rpt_wrap_producer(node, pos, ch, params.width, &next_id); + return rpt_wrap_producer(node, pos, ch, params.width, &state.next_plan_node_id); CustomScan *cs = RptBuildLocalProducerScan(node, pos, ch.condition_id, ch.total_elems); - cs->scan.plan.plan_node_id = next_id++; + cs->scan.plan.plan_node_id = state.next_plan_node_id++; return (Plan *) cs; }); } children = RptPgCall([&]() { return lappend(children, node); }); } - if (!rpt_apply_in_main(forward.vertices[forward.by_rti.at(comp.root)], - comp.channels, parents, params.width, &next_id, false)) + if (!rpt_apply_in_main(forward.vertices[forward.vertex_index_by_rtindex.at(comp.root_rtindex)], + comp.channels, &state, params.width, false)) RptPgCall([]() { elog(ERROR, "RPT forward: root relink failed after preflight"); }); - if (rpt_forward_apply == RPT_FORWARD_APPLY_ALL) - for (int id : comp.chain) - (void) rpt_apply_in_main(forward.vertices[forward.by_rti.at(id)], - comp.channels, parents, params.width, - &next_id, false); + if (params.apply_all) + for (int rtindex : comp.chain_rtindexes) + (void) rpt_apply_in_main(forward.vertices[forward.vertex_index_by_rtindex.at(rtindex)], + comp.channels, &state, params.width, false); + } + + return children; +} + +PlannedStmt * +RptInject(PlannedStmt *stmt, bool materialize, std::string *trace) +{ + if (!gp_anser_enable || gp_anser_runtime_filter) + { + *trace += "forward: needs anser.enable=on and anser.runtime_filter=off\n"; + return stmt; + } + if (rpt_has_runtime_filters(stmt)) + { + *trace += "forward: plan already carries Anser nodes\n"; + return stmt; } - RptAttachReducerInitPlan(work, children, params.width); - return work; + PlannedStmt *work = materialize + ? RptPgCall([&]() { return (PlannedStmt *) copyObjectImpl(stmt); }) + : stmt; + + RptTransferMgr analysis; + + RptCollectGraph(work, &analysis); + if (analysis.vertices.empty()) + { + *trace += "forward: no transfer regions\n"; + return stmt; + } + RptBuildClasses(&analysis); + + RptTransferMgr forward; + + RptBuildForwardPlan(&analysis, &forward); + RptCheckBackwardConsumers(work, &forward); + + /* Preflight against the plan: everything that could refuse a component + * is checked before any node is built, so a refusal changes nothing. */ + std::set targets; + + for (const RptVertexInfo &v : forward.vertices) + targets.insert(v.scan); + + RptInjectionState state; + std::map scan_slices; + state.parents = rpt_find_parents(work, targets, &scan_slices); + for (Plan *scan : targets) + state.tops[scan] = scan; + state.next_plan_node_id = materialize + ? RptPgCall([&]() { return AnserMaxPlanNodeId(work); }) + 1 : 0; + + RptForwardParams params; + + params.width = RptPgCall([]() { return getgpsegmentCount(); }); + params.ms_per_mib_part = rpt_forward_ms_per_mib_part; + params.delivery_ms_per_mib = rpt_delivery_ms_per_mib; + params.max_exchange_ms = rpt_forward_max_exchange_ms; + params.min_root_rows = rpt_forward_min_root_rows; + params.max_selectivity = rpt_forward_max_selectivity; + params.apply_all = (rpt_forward_apply == RPT_FORWARD_APPLY_ALL); + for (const auto &vertex : forward.vertices) + { + auto found = scan_slices.find(vertex.scan); + if (found != scan_slices.end()) + params.main_consumer_processes[vertex.rtindex] = RptPgCall([&]() { + return AnserSliceProducers(work, found->second); + }); + } + + std::map est = rpt_vertex_estimates(forward); + std::vector comps; + bool any = false; + List *children = NIL; + if (rpt_forward) + children = RptInjectForward(forward, params, est, materialize, + state, comps, trace, &any); + else + *trace += "forward: disabled\n"; + + if (rpt_backward) + RptInjectBackward(work, forward, comps, params, rpt_anser_size, + materialize, &state, trace, &any); + else + *trace += "backward: disabled\n"; + if (materialize && children != NIL) + RptAttachReducerInitPlan(work, children, params.width); + return materialize && any ? work : stmt; } diff --git a/gpcontrib/robust_predicate_transfer/src/rptforwardplan.cpp b/gpcontrib/robust_predicate_transfer/src/rptforwardplan.cpp index 3059057e5db..bf7824a236a 100644 --- a/gpcontrib/robust_predicate_transfer/src/rptforwardplan.cpp +++ b/gpcontrib/robust_predicate_transfer/src/rptforwardplan.cpp @@ -51,26 +51,26 @@ extern "C" /* Every vertex whose filters can reach `root` through forward edges. */ static std::set -rpt_forward_members(const RptTransferMgr &forward, int root) +rpt_forward_member_rtindexes(const RptTransferMgr &forward, int root_rtindex) { - std::set members; - std::vector queue(1, root); + std::set member_rtindexes; + std::vector pending_rtindexes(1, root_rtindex); - members.insert(root); - while (!queue.empty()) + member_rtindexes.insert(root_rtindex); + while (!pending_rtindexes.empty()) { - int node = queue.back(); - auto it = forward.graph.find(node); + int rtindex = pending_rtindexes.back(); + auto it = forward.graph.find(rtindex); - queue.pop_back(); + pending_rtindexes.pop_back(); if (it == forward.graph.end()) continue; - /* An in-edge's destination field names the producing side. */ + /* An in-edge's other_rtindex names the producing side. */ for (const RptGraphEdge &in : it->second.forward_stage.in) - if (members.insert(in.destination).second) - queue.push_back(in.destination); + if (member_rtindexes.insert(in.other_rtindex).second) + pending_rtindexes.push_back(in.other_rtindex); } - return members; + return member_rtindexes; } /* The fraction of `to` rows a filter from `from` keeps. */ @@ -78,10 +78,10 @@ static double rpt_transfer_keep(const RptTransferMgr &forward, const RptForwardChannel &ch, double from_keep, RptNdvFn ndv_fn) { - double d = ndv_fn ? ndv_fn(forward.vertices[forward.by_rti.at(ch.from)], - ch.from_key.attno) : -1; - double n = ndv_fn ? ndv_fn(forward.vertices[forward.by_rti.at(ch.to)], - ch.to_key.attno) : -1; + double d = ndv_fn ? ndv_fn(forward.vertices[forward.vertex_index_by_rtindex.at(ch.source_rtindex)], + ch.source_key.attno) : -1; + double n = ndv_fn ? ndv_fn(forward.vertices[forward.vertex_index_by_rtindex.at(ch.target_rtindex)], + ch.target_key.attno) : -1; if (d > 0 && n > 0 && d < n) return from_keep * d / n; @@ -94,38 +94,37 @@ RptSelectForward(const RptTransferMgr &forward, const RptForwardParams ¶ms, RptFilterSizeFn size_fn, RptNdvFn ndv_fn, std::vector *out) { - uint32 next_condition = 0; out->clear(); - for (int root : forward.roots) + for (int root_rtindex : forward.root_rtindexes) { RptForwardComponent comp; - std::set members = rpt_forward_members(forward, root); + std::set member_rtindexes = rpt_forward_member_rtindexes(forward, root_rtindex); std::map keep; /* chain vertex -> fraction it keeps */ bool sized = true; - comp.root = root; + comp.root_rtindex = root_rtindex; /* reducer_order is already topological: producers before consumers. */ - for (int id : forward.reducer_order) - if (id != root && members.count(id)) - comp.chain.push_back(id); + for (int rtindex : forward.reducer_order_rtindexes) + if (rtindex != root_rtindex && member_rtindexes.count(rtindex)) + comp.chain_rtindexes.push_back(rtindex); - for (int id : comp.chain) + for (int rtindex : comp.chain_rtindexes) { - const RptGraphNode &node = forward.graph.at(id); - const RptVertexInfo &source = forward.vertices[forward.by_rti.at(id)]; - auto e = est.find(id); + const RptGraphNode &node = forward.graph.at(rtindex); + const RptVertexInfo &source = forward.vertices[forward.vertex_index_by_rtindex.at(rtindex)]; + auto e = est.find(rtindex); double rows = (e != est.end() && e->second.rows >= 0) ? e->second.rows : (double) source.cardinality; double own = e != est.end() ? e->second.selectivity : 1.0; double k = own; - /* In-channels of id were made by earlier chain vertices. */ + /* In-channels of this rtindex were made by earlier chain vertices. */ for (const RptForwardChannel &in : comp.channels) - if (in.to == id) - k *= rpt_transfer_keep(forward, in, keep[in.from], ndv_fn); - keep[id] = k; + if (in.target_rtindex == rtindex) + k *= rpt_transfer_keep(forward, in, keep[in.source_rtindex], ndv_fn); + keep[rtindex] = k; for (const RptGraphEdge &edge : node.forward_stage.out) { @@ -133,12 +132,12 @@ RptSelectForward(const RptTransferMgr &forward, const RptForwardParams ¶ms, if (edge.left_keys.empty() || edge.right_keys.empty()) continue; - ch.from = id; - ch.to = edge.destination; - ch.from_key = edge.left_keys[0]; /* out-edge: left = this side */ - ch.to_key = edge.right_keys[0]; + ch.source_rtindex = rtindex; + ch.target_rtindex = edge.other_rtindex; + ch.source_key = edge.left_keys[0]; /* out-edge: left = this side */ + ch.target_key = edge.right_keys[0]; ch.local = edge.local; - ch.in_process = ch.local && ch.to != root && !params.apply_all; + ch.in_process = ch.local && ch.target_rtindex != root_rtindex && !params.apply_all; /* Sized for what the reducer scan inserts: its rows after its * own quals -- all of them when the quals were dropped. */ @@ -146,13 +145,24 @@ RptSelectForward(const RptTransferMgr &forward, const RptForwardParams ¶ms, &ch.max_payload, &ch.planned_bytes)) { comp.reason = "filter sizing declined for " + - std::to_string(ch.from) + "->" + std::to_string(ch.to); + std::to_string(ch.source_rtindex) + "->" + std::to_string(ch.target_rtindex); sized = false; break; } if (!ch.in_process) - ch.est_cost_ms = params.ms_per_mib_part * params.width * + { + /* One reducer consumer, plus a main-plan consumer for + * root edges or forward_apply=all. */ + auto processes = params.main_consumer_processes.find(ch.target_rtindex); + int main_width = processes == params.main_consumer_processes.end() + ? params.width : processes->second; + int deliveries = (ch.target_rtindex != root_rtindex ? params.width : 0) + + (ch.target_rtindex == root_rtindex || params.apply_all ? main_width : 0); + ch.est_cost_ms = + (params.ms_per_mib_part * params.width + + params.delivery_ms_per_mib * deliveries) * ((double) ch.planned_bytes / (1024.0 * 1024.0)); + } comp.est_cost_ms += ch.est_cost_ms; comp.channels.push_back(ch); } @@ -161,10 +171,10 @@ RptSelectForward(const RptTransferMgr &forward, const RptForwardParams ¶ms, } for (const RptForwardChannel &ch : comp.channels) - if (ch.to == root) - comp.est_selectivity *= rpt_transfer_keep(forward, ch, keep[ch.from], ndv_fn); + if (ch.target_rtindex == root_rtindex) + comp.est_selectivity *= rpt_transfer_keep(forward, ch, keep[ch.source_rtindex], ndv_fn); - const RptVertexInfo &rootv = forward.vertices[forward.by_rti.at(root)]; + const RptVertexInfo &rootv = forward.vertices[forward.vertex_index_by_rtindex.at(root_rtindex)]; if (!sized) ; /* reason already set */ @@ -182,12 +192,6 @@ RptSelectForward(const RptTransferMgr &forward, const RptForwardParams ¶ms, { comp.accepted = true; comp.reason = "inject"; - for (RptForwardChannel &ch : comp.channels) - { - ch.condition_id = next_condition++; - ch.condition_key = "rpt_f_" + std::to_string(ch.from) + "_" + - std::to_string(ch.to); - } } out->push_back(comp); } @@ -201,16 +205,16 @@ RptDumpForward(const std::vector &components, std::string * text << std::fixed << std::setprecision(3); for (const RptForwardComponent &comp : components) { - text << "forward component root=" << comp.root << " chain=["; - for (size_t i = 0; i < comp.chain.size(); ++i) - text << (i ? " " : "") << comp.chain[i]; + text << "forward component root=" << comp.root_rtindex << " chain=["; + for (size_t i = 0; i < comp.chain_rtindexes.size(); ++i) + text << (i ? " " : "") << comp.chain_rtindexes[i]; text << "] channels=" << comp.channels.size() << " est_cost_ms=" << comp.est_cost_ms << " est_sel=" << comp.est_selectivity << " decision=" << comp.reason << '\n'; for (const RptForwardChannel &ch : comp.channels) - text << "forward channel " << ch.from_key.rtindex << '.' << ch.from_key.attno - << "->" << ch.to_key.rtindex << '.' << ch.to_key.attno + text << "forward channel " << ch.source_key.rtindex << '.' << ch.source_key.attno + << "->" << ch.target_key.rtindex << '.' << ch.target_key.attno << " key=" << (ch.condition_key.empty() ? "-" : ch.condition_key) << " bytes=" << ch.planned_bytes << " est_ms=" << ch.est_cost_ms diff --git a/gpcontrib/robust_predicate_transfer/src/rptgraph.cpp b/gpcontrib/robust_predicate_transfer/src/rptgraph.cpp index 3c3e9324f03..24867b76b44 100644 --- a/gpcontrib/robust_predicate_transfer/src/rptgraph.cpp +++ b/gpcontrib/robust_predicate_transfer/src/rptgraph.cpp @@ -81,18 +81,18 @@ RptKey RptFindKeyRoot(const RptKey &key, std::map &uf_parent) { std::map::iterator it = uf_parent.find(key); - RptKey root; + RptKey representative_key; if (it == uf_parent.end()) return key; - root = it->second; - if (!(root == key)) + representative_key = it->second; + if (!(representative_key == key)) { - root = RptFindKeyRoot(root, uf_parent); - uf_parent[key] = root; /* path compression */ + representative_key = RptFindKeyRoot(representative_key, uf_parent); + uf_parent[key] = representative_key; /* path compression */ } - return root; + return representative_key; } /* @@ -122,9 +122,9 @@ rpt_union_keys(const RptKey &a, const RptKey &b, Oid key_type, { RptKeyGroup group; - group.leader_id = -1; + group.leader_rtindex = -1; group.key_type = key_type; - group.members.insert(a.rtindex); + group.member_rtindexes.insert(a.rtindex); ga = mgr->groups.insert(std::make_pair(root_a, group)).first; } @@ -133,14 +133,14 @@ rpt_union_keys(const RptKey &a, const RptKey &b, Oid key_type, { RptKeyGroup group; - group.leader_id = -1; + group.leader_rtindex = -1; group.key_type = key_type; - group.members.insert(b.rtindex); + group.member_rtindexes.insert(b.rtindex); gb = mgr->groups.insert(std::make_pair(root_b, group)).first; } - ga->second.members.insert(gb->second.members.begin(), - gb->second.members.end()); + ga->second.member_rtindexes.insert(gb->second.member_rtindexes.begin(), + gb->second.member_rtindexes.end()); mgr->groups.erase(root_b); } @@ -199,27 +199,27 @@ rpt_classify_tables(RptTransferMgr *mgr) /* Helper: check whether data can flow from one table to another */ static bool -rpt_can_flow(const RptEdgeInfo *e, int from_v, int to_v) +rpt_can_flow(const RptEdgeInfo *e, int source_rtindex, int target_rtindex) { - if (e->left_v == from_v && e->right_v == to_v) + if (e->left_rtindex == source_rtindex && e->right_rtindex == target_rtindex) return !e->protect_right; /* Left → Right allowed if not protected */ - if (e->right_v == from_v && e->left_v == to_v) + if (e->right_rtindex == source_rtindex && e->left_rtindex == target_rtindex) return !e->protect_left; /* Right → Left allowed if not protected */ return false; } /* Helper: get the key on vertex t's side of one clause of edge e */ static const RptKey & -rpt_key_at(const RptEdgeInfo *e, size_t clause, int t) +rpt_key_at(const RptEdgeInfo *e, size_t clause, int rtindex) { - return (e->left_v == t) ? e->clauses[clause].first : e->clauses[clause].second; + return (e->left_rtindex == rtindex) ? e->clauses[clause].first : e->clauses[clause].second; } /* Helper: get the key on the side opposite vertex t in one clause of edge e */ static const RptKey & -rpt_key_opposite(const RptEdgeInfo *e, size_t clause, int t) +rpt_key_opposite(const RptEdgeInfo *e, size_t clause, int rtindex) { - return (e->left_v == t) ? e->clauses[clause].second : e->clauses[clause].first; + return (e->left_rtindex == rtindex) ? e->clauses[clause].second : e->clauses[clause].first; } /* @@ -263,8 +263,8 @@ rpt_merge_clauses(RptEdgeInfo *old, const RptEdgeInfo &other, bool reversed) static bool rpt_upsert_edge(RptTransferMgr *mgr, const RptEdgeInfo &ne) { - int i = ne.left_v; - int j = ne.right_v; + int i = ne.left_rtindex; + int j = ne.right_rtindex; std::map::iterator ij; bool exists = false; bool changed = false; @@ -280,10 +280,10 @@ rpt_upsert_edge(RptTransferMgr *mgr, const RptEdgeInfo &ne) */ RptEdgeInfo before = ij->second; - bool same = (ij->second.left_v == ne.left_v && - ij->second.right_v == ne.right_v); - bool rev = (ij->second.left_v == ne.right_v && - ij->second.right_v == ne.left_v); + bool same = (ij->second.left_rtindex == ne.left_rtindex && + ij->second.right_rtindex == ne.right_rtindex); + bool rev = (ij->second.left_rtindex == ne.right_rtindex && + ij->second.right_rtindex == ne.left_rtindex); if (same) { @@ -328,31 +328,31 @@ rpt_upsert_edge(RptTransferMgr *mgr, const RptEdgeInfo &ne) static void rpt_skip_unfiltered(RptTransferMgr *mgr) { - std::vector unfiltered_ids; - std::vector intermediate_ids; + std::vector unfiltered_rtindexes; + std::vector intermediate_rtindexes; size_t i; bool changed; - /* Sorted id lists keep the iteration (and hence the dumps) deterministic. */ + /* Sorted range-table indexes keep iteration and dumps deterministic. */ for (i = 0; i < mgr->vertices.size(); i++) { if (mgr->vertices[i].unfiltered) - unfiltered_ids.push_back(mgr->vertices[i].id); + unfiltered_rtindexes.push_back(mgr->vertices[i].rtindex); else if (mgr->vertices[i].intermediate) - intermediate_ids.push_back(mgr->vertices[i].id); + intermediate_rtindexes.push_back(mgr->vertices[i].rtindex); } - std::sort(unfiltered_ids.begin(), unfiltered_ids.end()); - std::sort(intermediate_ids.begin(), intermediate_ids.end()); + std::sort(unfiltered_rtindexes.begin(), unfiltered_rtindexes.end()); + std::sort(intermediate_rtindexes.begin(), intermediate_rtindexes.end()); /* 1) Iteratively skip unfiltered tables until no new edges are added. */ do { changed = false; - for (i = 0; i < unfiltered_ids.size(); i++) + for (i = 0; i < unfiltered_rtindexes.size(); i++) { - int t = unfiltered_ids[i]; - std::map &adj = mgr->neighbor_matrix[t]; + int rtindex = unfiltered_rtindexes[i]; + std::map &adj = mgr->neighbor_matrix[rtindex]; std::map > incoming_by_col; std::vector outgoing; std::map::iterator pit; @@ -365,8 +365,8 @@ rpt_skip_unfiltered(RptTransferMgr *mgr) RptEdgeInfo *e = &pit->second; size_t c; - if (mgr->vertices[mgr->by_rti[pit->first]].unfiltered || - !rpt_can_flow(e, pit->first, t)) + if (mgr->vertices[mgr->vertex_index_by_rtindex[pit->first]].unfiltered || + !rpt_can_flow(e, pit->first, rtindex)) continue; for (c = 0; c < e->clauses.size(); c++) { @@ -374,14 +374,14 @@ rpt_skip_unfiltered(RptTransferMgr *mgr) ref.edge = e; ref.clause = c; - incoming_by_col[rpt_key_at(e, c, t)].push_back(ref); + incoming_by_col[rpt_key_at(e, c, rtindex)].push_back(ref); } } /* ---- Step 2: collect all outgoing edges t → neighbor ---- */ for (pit = adj.begin(); pit != adj.end(); ++pit) { - if (rpt_can_flow(&pit->second, t, pit->first)) + if (rpt_can_flow(&pit->second, rtindex, pit->first)) outgoing.push_back(&pit->second); } @@ -405,32 +405,32 @@ rpt_skip_unfiltered(RptTransferMgr *mgr) int src_v; int dst_v; - if (!(rpt_key_at(out_e, oc, t) == git->first)) + if (!(rpt_key_at(out_e, oc, rtindex) == git->first)) continue; /* must be same join key */ /* src is the carrier: in_e's side opposite t */ - src_v = (in_e->left_v == t) ? in_e->right_v : in_e->left_v; + src_v = (in_e->left_rtindex == rtindex) ? in_e->right_rtindex : in_e->left_rtindex; - ne.left_v = src_v; + ne.left_rtindex = src_v; ne.key_type = out_e->key_type; ne.protect_left = false; ne.protect_right = false; - if (out_e->left_v == t) + if (out_e->left_rtindex == rtindex) { /* t is left side of out_e */ - dst_v = out_e->right_v; - ne.right_v = dst_v; - ne.clauses.push_back(std::make_pair(rpt_key_opposite(in_e, rep.clause, t), + dst_v = out_e->right_rtindex; + ne.right_rtindex = dst_v; + ne.clauses.push_back(std::make_pair(rpt_key_opposite(in_e, rep.clause, rtindex), out_e->clauses[oc].second)); ne.protect_left = true; /* disable reverse flow (R → L) */ } else { /* t is right side of out_e */ - dst_v = out_e->left_v; - ne.right_v = dst_v; - ne.clauses.push_back(std::make_pair(rpt_key_opposite(in_e, rep.clause, t), + dst_v = out_e->left_rtindex; + ne.right_rtindex = dst_v; + ne.clauses.push_back(std::make_pair(rpt_key_opposite(in_e, rep.clause, rtindex), out_e->clauses[oc].first)); ne.protect_left = true; /* disable dst -> src */ @@ -449,7 +449,7 @@ rpt_skip_unfiltered(RptTransferMgr *mgr) { RptEdgeInfo *e = outgoing[j]; - if (e->left_v == t) + if (e->left_rtindex == rtindex) { if (!e->protect_right) { @@ -476,14 +476,14 @@ rpt_skip_unfiltered(RptTransferMgr *mgr) if (e->protect_left && e->protect_right) { ++pit; - mgr->neighbor_matrix[t].erase(nbr); - mgr->neighbor_matrix[nbr].erase(t); + mgr->neighbor_matrix[rtindex].erase(nbr); + mgr->neighbor_matrix[nbr].erase(rtindex); changed = true; } else { /* keep the reverse matrix entry in sync */ - mgr->neighbor_matrix[nbr][t] = *e; + mgr->neighbor_matrix[nbr][rtindex] = *e; ++pit; } } @@ -495,10 +495,10 @@ rpt_skip_unfiltered(RptTransferMgr *mgr) * should not have out-edge. (The artifact's comment says "any in-edge" * but its code -- and ours -- does this when no in-edge is left.) */ - for (i = 0; i < intermediate_ids.size(); i++) + for (i = 0; i < intermediate_rtindexes.size(); i++) { - int t = intermediate_ids[i]; - std::map &adj = mgr->neighbor_matrix[t]; + int rtindex = intermediate_rtindexes[i]; + std::map &adj = mgr->neighbor_matrix[rtindex]; std::map > incoming_by_col; std::map::iterator pit; size_t c; @@ -508,7 +508,7 @@ rpt_skip_unfiltered(RptTransferMgr *mgr) { RptEdgeInfo *e = &pit->second; - if (!rpt_can_flow(e, pit->first, t)) + if (!rpt_can_flow(e, pit->first, rtindex)) continue; for (c = 0; c < e->clauses.size(); c++) { @@ -516,7 +516,7 @@ rpt_skip_unfiltered(RptTransferMgr *mgr) ref.edge = e; ref.clause = c; - incoming_by_col[rpt_key_at(e, c, t)].push_back(ref); + incoming_by_col[rpt_key_at(e, c, rtindex)].push_back(ref); } } @@ -526,9 +526,9 @@ rpt_skip_unfiltered(RptTransferMgr *mgr) { RptEdgeInfo *e = &pit->second; - if (!rpt_can_flow(e, t, pit->first)) + if (!rpt_can_flow(e, rtindex, pit->first)) continue; - if (e->left_v == t) + if (e->left_rtindex == rtindex) e->protect_right = true; /* block Left→Right */ else e->protect_left = true; /* block Right→Left */ @@ -542,13 +542,13 @@ rpt_skip_unfiltered(RptTransferMgr *mgr) if (e->protect_left && e->protect_right) { ++pit; - mgr->neighbor_matrix[t].erase(nbr); - mgr->neighbor_matrix[nbr].erase(t); + mgr->neighbor_matrix[rtindex].erase(nbr); + mgr->neighbor_matrix[nbr].erase(rtindex); } else { /* keep the reverse matrix entry in sync */ - mgr->neighbor_matrix[nbr][t] = *e; + mgr->neighbor_matrix[nbr][rtindex] = *e; ++pit; } } @@ -610,18 +610,18 @@ RptBuildClasses(RptTransferMgr *mgr) if (keys[i].rtindex == keys[j].rtindex) continue; RptEdgeInfo edge; - edge.left_v = keys[i].rtindex; - edge.right_v = keys[j].rtindex; + edge.left_rtindex = keys[i].rtindex; + edge.right_rtindex = keys[j].rtindex; edge.key_type = group.second.key_type; edge.protect_left = edge.protect_right = false; edge.clauses.push_back(std::make_pair(keys[i], keys[j])); - const auto &existing_row = mgr->neighbor_matrix.find(edge.left_v); + const auto &existing_row = mgr->neighbor_matrix.find(edge.left_rtindex); if (existing_row != mgr->neighbor_matrix.end()) { - const auto &existing = existing_row->second.find(edge.right_v); + const auto &existing = existing_row->second.find(edge.right_rtindex); if (existing != existing_row->second.end()) { - bool reversed = existing->second.left_v != edge.left_v; + bool reversed = existing->second.left_rtindex != edge.left_rtindex; auto clause = reversed ? std::make_pair(keys[j], keys[i]) : std::make_pair(keys[i], keys[j]); if (std::find(existing->second.clauses.begin(), diff --git a/gpcontrib/robust_predicate_transfer/src/rptinit.cpp b/gpcontrib/robust_predicate_transfer/src/rptinit.cpp index f30ff8584ec..45bf601c574 100644 --- a/gpcontrib/robust_predicate_transfer/src/rptinit.cpp +++ b/gpcontrib/robust_predicate_transfer/src/rptinit.cpp @@ -25,7 +25,7 @@ * first: the AnserXxx entry points we call are resolved at load time from * the global symbol table (dfmgr loads modules RTLD_GLOBAL). * - * The planner hook builds forward reducers and logs transfer decisions at + * The planner hook injects forward and backward filters and logs decisions at * DEBUG1 when rpt.debug is on. rpt_transfer_plan() exposes analysis without * injecting filters. debug_noop_initplan adds an empty distributed InitPlan * for overhead measurement. @@ -75,6 +75,7 @@ bool rpt_forward = true; int rpt_forward_apply = RPT_FORWARD_APPLY_ROOT; /* Measured 2026-11-10 on one host with three primaries. */ double rpt_forward_ms_per_mib_part = 3.5; +double rpt_delivery_ms_per_mib = 0.8; double rpt_forward_max_exchange_ms = 100.0; double rpt_forward_min_root_rows = 10000.0; /* The RPT+ paper's lambda (its Algorithm 2 threshold). */ @@ -117,13 +118,13 @@ rpt_analyze(PlannedStmt *stmt, StringInfo out) { RptBuildClasses(&mgr); RptBuildTransferPlan(stmt, &mgr); - RptDumpTransferPlan(&mgr, out); + RptDumpTransferPlan(stmt, &mgr, out); - /* What the forward pass would do with this plan, built nothing. */ - std::string forward_trace; + /* Dry-run the configured runtime passes without changing the plan. */ + std::string injection_trace; - (void) RptInjectForward(stmt, false, &forward_trace); - RptPgCall([&]() { appendStringInfoString(out, forward_trace.c_str()); }); + (void) RptInject(stmt, false, &injection_trace); + RptPgCall([&]() { appendStringInfoString(out, injection_trace.c_str()); }); } return; } @@ -186,7 +187,7 @@ static void rpt_define_gucs(void) { DefineCustomBoolVariable("rpt.enable", - "Enables RPT+ transfer-plan analysis over Anser.", + "Enables RPT+ runtime filters over Anser.", "When disabled the planner hook is a pass-through.", &rpt_enable, false, @@ -210,8 +211,8 @@ rpt_define_gucs(void) NULL, NULL, NULL); DefineCustomBoolVariable("rpt.backward", - "Reserves the backward runtime switch.", - "QD-only; backward runtime injection is not implemented.", + "Builds root-to-member backward runtime filters.", + "QD-only; only consumers proven safe to wait are injected.", &rpt_backward, true, PGC_USERSET, 0, NULL, NULL, NULL); @@ -235,13 +236,19 @@ rpt_define_gucs(void) NULL, NULL, NULL); DefineCustomRealVariable("rpt.forward_ms_per_mib_part", - "Estimated cost of one global hop per MiB of bitset per producer.", - "The c in c * n_producers * filter_MiB; QD-only.", + "Estimated collection cost per MiB of bitset per producer.", + "Shared by forward and backward exchanges; QD-only.", &rpt_forward_ms_per_mib_part, 3.5, 0.0, 1.0e6, PGC_USERSET, 0, NULL, NULL, NULL); + DefineCustomRealVariable("rpt.delivery_ms_per_mib", + "Estimated delivery cost per consumer process per MiB.", + "Shared by forward and backward exchanges; QD-only.", + &rpt_delivery_ms_per_mib, 0.8, 0.0, 1.0e6, + PGC_USERSET, 0, NULL, NULL, NULL); + DefineCustomRealVariable("rpt.forward_max_exchange_ms", - "Skips a forward chain whose estimated exchange cost is higher.", + "Skips a forward chain or backward class above this exchange cost.", "-1 disables the limit; QD-only.", &rpt_forward_max_exchange_ms, 100.0, -1.0, 1.0e9, PGC_USERSET, 0, NULL, NULL, NULL); @@ -253,8 +260,8 @@ rpt_define_gucs(void) PGC_USERSET, 0, NULL, NULL, NULL); DefineCustomRealVariable("rpt.forward_max_selectivity", - "Skips a forward chain expected to keep at least this fraction of the root's rows.", - "Estimated from the reducer quals and column NDVs; 1 disables the check; QD-only.", + "Skips transfers expected to keep at least this fraction of target rows.", + "Shared selectivity threshold for forward roots and backward members; 1 disables the check; QD-only.", &rpt_forward_max_selectivity, 0.35, 0.0, 1.0, PGC_USERSET, 0, NULL, NULL, NULL); @@ -394,7 +401,7 @@ rpt_try_noop_initplan(PlannedStmt *stmt) /* Contain C++ exceptions before returning to the PostgreSQL error handler. */ static PlannedStmt * -rpt_build_forward(PlannedStmt *stmt) +rpt_build_injection(PlannedStmt *stmt) { bool pg_error = false; bool out_of_memory = false; @@ -403,10 +410,10 @@ rpt_build_forward(PlannedStmt *stmt) try { std::string trace; - PlannedStmt *result = RptInjectForward(stmt, true, &trace); + PlannedStmt *result = RptInject(stmt, true, &trace); if (rpt_debug) - RptPgCall([&]() { elog(DEBUG1, "rpt forward:\n%s", trace.c_str()); }); + RptPgCall([&]() { elog(DEBUG1, "rpt injection:\n%s", trace.c_str()); }); return result; } catch (const RptPgError &) @@ -428,15 +435,15 @@ rpt_build_forward(PlannedStmt *stmt) PG_RE_THROW(); if (out_of_memory) ereport(ERROR, (errcode(ERRCODE_OUT_OF_MEMORY), - errmsg("out of memory building the RPT forward reducer"))); - ereport(ERROR, (errmsg("RPT forward reducer failed: %s", message))); + errmsg("out of memory building the RPT runtime filters"))); + ereport(ERROR, (errmsg("RPT runtime filters failed: %s", message))); return NULL; } /* Same contract as rpt_try_noop_initplan: the original plan survives any * failure, which is reported as a WARNING; only cancellation propagates. */ static PlannedStmt * -rpt_try_forward(PlannedStmt *stmt) +rpt_try_injection(PlannedStmt *stmt) { MemoryContext saved_context = CurrentMemoryContext; MemoryContext volatile build_context = NULL; @@ -445,10 +452,10 @@ rpt_try_forward(PlannedStmt *stmt) PG_TRY(); { build_context = AllocSetContextCreate(saved_context, - "RPT forward reducer", + "RPT runtime filters", ALLOCSET_DEFAULT_SIZES); MemoryContextSwitchTo(build_context); - result = rpt_build_forward(stmt); + result = rpt_build_injection(stmt); MemoryContextSwitchTo(saved_context); /* Nothing injected: the scratch context holds only garbage. */ if (result == stmt) @@ -467,7 +474,7 @@ rpt_try_forward(PlannedStmt *stmt) MemoryContextDelete(build_context); if (error->sqlerrcode == ERRCODE_QUERY_CANCELED) ReThrowError(error); - ereport(WARNING, (errmsg("RPT forward reducer skipped: %s", error->message))); + ereport(WARNING, (errmsg("RPT runtime filters skipped: %s", error->message))); FreeErrorData(error); result = stmt; } @@ -501,12 +508,12 @@ rpt_planner(Query *parse, const char *query_string, int cursorOptions, result->planTree != NULL) rpt_log_analysis(result); - if (rpt_enable && rpt_forward && !rpt_suppress_injection && + if (rpt_enable && (rpt_forward || rpt_backward) && !rpt_suppress_injection && Gp_role == GP_ROLE_DISPATCH && result != NULL && result->commandType == CMD_SELECT && result->planTree != NULL && result->intoClause == NULL && result->copyIntoClause == NULL && result->refreshClause == NULL) - result = rpt_try_forward(result); + result = rpt_try_injection(result); if (rpt_debug_noop_initplan && Gp_role == GP_ROLE_DISPATCH && result != NULL && result->commandType == CMD_SELECT && result->planTree != NULL) diff --git a/gpcontrib/robust_predicate_transfer/src/rptinject.cpp b/gpcontrib/robust_predicate_transfer/src/rptinject.cpp index 04d6dfec6b0..504e6582469 100644 --- a/gpcontrib/robust_predicate_transfer/src/rptinject.cpp +++ b/gpcontrib/robust_predicate_transfer/src/rptinject.cpp @@ -47,37 +47,37 @@ RptAttachReducerInitPlan(PlannedStmt *stmt, List *children, int width) (stmt->subplans != NIL && !stmt->subplan_sliceIds)) elog(ERROR, "invalid RPT reducer InitPlan input"); - int max_id = AnserMaxPlanNodeId(stmt); + int max_plan_node_id = AnserMaxPlanNodeId(stmt); ListCell *lc; foreach(lc, children) { PlannedStmt child_stmt = {}; child_stmt.planTree = (Plan *) lfirst(lc); - max_id = Max(max_id, AnserMaxPlanNodeId(&child_stmt)); + max_plan_node_id = Max(max_plan_node_id, AnserMaxPlanNodeId(&child_stmt)); } - int next_id = max_id + 1; - int root_slice = stmt->numSlices; - int sender_slice = root_slice + 1; + int next_plan_node_id = max_plan_node_id + 1; + int root_slice_index = stmt->numSlices; + int sender_slice_index = root_slice_index + 1; int old_subplans = list_length(stmt->subplans); - int param_id = list_length(stmt->paramExecTypes); + int param_exec_id = list_length(stmt->paramExecTypes); Flow *distributed = makeNode(Flow); distributed->flotype = FLOW_PARTITIONED; distributed->locustype = CdbLocusType_Strewn; distributed->numsegments = width; Result *empty = makeNode(Result); - empty->plan.plan_node_id = next_id++; + empty->plan.plan_node_id = next_plan_node_id++; empty->plan.flow = distributed; /* ExecInitResult passes this field to ExecInitQual as implicit AND. */ empty->resconstantqual = (Node *) list_make1(makeBoolConst(false, false)); Sequence *sequence = makeNode(Sequence); - sequence->plan.plan_node_id = next_id++; + sequence->plan.plan_node_id = next_plan_node_id++; sequence->plan.flow = distributed; sequence->subplans = lappend(list_copy(children), empty); Motion *gather = makeNode(Motion); - gather->plan.plan_node_id = next_id++; + gather->plan.plan_node_id = next_plan_node_id++; gather->plan.lefttree = (Plan *) sequence; gather->motionType = MOTIONTYPE_GATHER; - gather->motionID = sender_slice; + gather->motionID = sender_slice_index; Flow *singleton = makeNode(Flow); singleton->flotype = FLOW_SINGLETON; singleton->locustype = CdbLocusType_Entry; @@ -88,19 +88,19 @@ RptAttachReducerInitPlan(PlannedStmt *stmt, List *children, int width) PlanSlice *slices = (PlanSlice *) palloc0( sizeof(PlanSlice) * (stmt->numSlices + 2)); memcpy(slices, stmt->slices, sizeof(PlanSlice) * stmt->numSlices); - slices[root_slice].sliceIndex = root_slice; - slices[root_slice].parentIndex = -1; - slices[root_slice].gangType = GANGTYPE_UNALLOCATED; - slices[root_slice].segindex = -1; - slices[sender_slice].sliceIndex = sender_slice; - slices[sender_slice].parentIndex = root_slice; - slices[sender_slice].gangType = GANGTYPE_PRIMARY_READER; - slices[sender_slice].segindex = 0; - slices[sender_slice].numsegments = width; - int *slice_ids = (int *) palloc(sizeof(int) * (old_subplans + 1)); + slices[root_slice_index].sliceIndex = root_slice_index; + slices[root_slice_index].parentIndex = -1; + slices[root_slice_index].gangType = GANGTYPE_UNALLOCATED; + slices[root_slice_index].segindex = -1; + slices[sender_slice_index].sliceIndex = sender_slice_index; + slices[sender_slice_index].parentIndex = root_slice_index; + slices[sender_slice_index].gangType = GANGTYPE_PRIMARY_READER; + slices[sender_slice_index].segindex = 0; + slices[sender_slice_index].numsegments = width; + int *subplan_slice_indexes = (int *) palloc(sizeof(int) * (old_subplans + 1)); if (old_subplans) - memcpy(slice_ids, stmt->subplan_sliceIds, sizeof(int) * old_subplans); - slice_ids[old_subplans] = root_slice; + memcpy(subplan_slice_indexes, stmt->subplan_sliceIds, sizeof(int) * old_subplans); + subplan_slice_indexes[old_subplans] = root_slice_index; SubPlan *subplan = makeNode(SubPlan); subplan->subLinkType = EXISTS_SUBLINK; @@ -109,11 +109,11 @@ RptAttachReducerInitPlan(PlannedStmt *stmt, List *children, int width) subplan->firstColType = VOIDOID; subplan->firstColTypmod = -1; subplan->is_initplan = true; - subplan->setParam = list_make1_int(param_id); + subplan->setParam = list_make1_int(param_exec_id); stmt->subplans = lappend(stmt->subplans, gather); stmt->paramExecTypes = lappend_oid(stmt->paramExecTypes, BOOLOID); stmt->planTree->initPlan = lappend(stmt->planTree->initPlan, subplan); - stmt->subplan_sliceIds = slice_ids; + stmt->subplan_sliceIds = subplan_slice_indexes; stmt->slices = slices; stmt->numSlices += 2; }); diff --git a/gpcontrib/robust_predicate_transfer/src/rptplan.cpp b/gpcontrib/robust_predicate_transfer/src/rptplan.cpp index ec005a4107c..70e6c4f5e83 100644 --- a/gpcontrib/robust_predicate_transfer/src/rptplan.cpp +++ b/gpcontrib/robust_predicate_transfer/src/rptplan.cpp @@ -59,6 +59,7 @@ extern "C" #include "postgres.h" #include "optimizer/walkers.h" +#include "parser/parsetree.h" } @@ -69,7 +70,7 @@ extern "C" * given stage and direction. */ static RptGraphEdge * -rpt_node_add(RptGraphNode *node, int other, bool is_forward, bool is_in_edge) +rpt_node_add(RptGraphNode *node, int other_rtindex, bool is_forward, bool is_in_edge) { std::vector &edges = is_forward ? (is_in_edge ? node->forward_stage.in : node->forward_stage.out) @@ -78,23 +79,23 @@ rpt_node_add(RptGraphNode *node, int other, bool is_forward, bool is_in_edge) for (i = 0; i < edges.size(); i++) { - if (edges[i].destination == other) + if (edges[i].other_rtindex == other_rtindex) return &edges[i]; } edges.push_back(RptGraphEdge()); - edges.back().destination = other; + edges.back().other_rtindex = other_rtindex; return &edges.back(); } /* GraphNode::Add with keys (dag.cpp:21-28). */ static RptGraphEdge * -rpt_node_add_keys(RptGraphNode *node, int other, +rpt_node_add_keys(RptGraphNode *node, int other_rtindex, const std::vector &left_keys, const std::vector &right_keys, const std::vector &key_types, bool is_forward, bool is_in_edge) { - RptGraphEdge *edge = rpt_node_add(node, other, is_forward, is_in_edge); + RptGraphEdge *edge = rpt_node_add(node, other_rtindex, is_forward, is_in_edge); edge->left_keys = left_keys; edge->right_keys = right_keys; @@ -108,9 +109,9 @@ rpt_node_add_keys(RptGraphNode *node, int other, * broadcast builds its single filter on. */ static void -rpt_register_leader(RptTransferMgr *mgr, int vertex_id) +rpt_register_leader(RptTransferMgr *mgr, int rtindex) { - const RptVertexInfo &v = mgr->vertices[mgr->by_rti[vertex_id]]; + const RptVertexInfo &v = mgr->vertices[mgr->vertex_index_by_rtindex[rtindex]]; std::set::const_iterator kit; for (kit = v.join_keys.begin(); kit != v.join_keys.end(); ++kit) @@ -118,9 +119,9 @@ rpt_register_leader(RptTransferMgr *mgr, int vertex_id) RptKey rep = RptFindKeyRoot(*kit, mgr->uf_parent); std::map::iterator git = mgr->groups.find(rep); - if (git != mgr->groups.end() && git->second.leader_id == -1) + if (git != mgr->groups.end() && git->second.leader_rtindex == -1) { - git->second.leader_id = vertex_id; + git->second.leader_rtindex = rtindex; git->second.leader_key = *kit; } } @@ -136,26 +137,26 @@ rpt_find_edge(const RptTransferMgr *mgr, const std::set &constructed, const std::set &unconstructed, const std::map &forward_depth, const std::map &backward_depth, - int *forward_from, int *backward_from, int *to) + int *forward_parent_rtindex, int *backward_parent_rtindex, int *target_rtindex) { size_t best_weight = 0; int64 best_rows = -1; bool found = false; /* Algorithm 1: select the frontier vertex, then each tree's parent. */ - for (int candidate : unconstructed) + for (int candidate_rtindex : unconstructed) { - for (int parent : constructed) + for (int parent_rtindex : constructed) { - const auto &row = mgr->neighbor_matrix.find(parent); + const auto &row = mgr->neighbor_matrix.find(parent_rtindex); if (row == mgr->neighbor_matrix.end()) continue; - const auto &eit = row->second.find(candidate); + const auto &eit = row->second.find(candidate_rtindex); if (eit == row->second.end() || (eit->second.protect_left && eit->second.protect_right)) continue; size_t weight = eit->second.clauses.size(); - int64 rows = mgr->vertices[mgr->by_rti.find(candidate)->second].cardinality; + int64 rows = mgr->vertices[mgr->vertex_index_by_rtindex.find(candidate_rtindex)->second].cardinality; if (!found || weight > best_weight || (weight == best_weight && rows > best_rows)) @@ -163,44 +164,44 @@ rpt_find_edge(const RptTransferMgr *mgr, const std::set &constructed, found = true; best_weight = weight; best_rows = rows; - *to = candidate; + *target_rtindex = candidate_rtindex; } } } if (!found) return false; - *forward_from = *backward_from = -1; + *forward_parent_rtindex = *backward_parent_rtindex = -1; size_t forward_weight = 0; size_t backward_weight = 0; - for (int parent : constructed) + for (int parent_rtindex : constructed) { - const auto &row = mgr->neighbor_matrix.find(parent); + const auto &row = mgr->neighbor_matrix.find(parent_rtindex); if (row == mgr->neighbor_matrix.end()) continue; - const auto &eit = row->second.find(*to); + const auto &eit = row->second.find(*target_rtindex); if (eit == row->second.end() || (eit->second.protect_left && eit->second.protect_right)) continue; const RptEdgeInfo &edge = eit->second; size_t weight = edge.clauses.size(); - bool forward_open = edge.left_v == *to ? !edge.protect_right : !edge.protect_left; - bool backward_open = edge.left_v == parent ? !edge.protect_right : !edge.protect_left; + bool forward_open = edge.left_rtindex == *target_rtindex ? !edge.protect_right : !edge.protect_left; + bool backward_open = edge.left_rtindex == parent_rtindex ? !edge.protect_right : !edge.protect_left; /* A protected edge is not a parent candidate for that stage. Prefer * its best open alternative, even if it has lower weight. */ - if (forward_open && (*forward_from == -1 || weight > forward_weight || + if (forward_open && (*forward_parent_rtindex == -1 || weight > forward_weight || (weight == forward_weight && - forward_depth.at(parent) > forward_depth.at(*forward_from)))) + forward_depth.at(parent_rtindex) > forward_depth.at(*forward_parent_rtindex)))) { - *forward_from = parent; + *forward_parent_rtindex = parent_rtindex; forward_weight = weight; } - if (backward_open && (*backward_from == -1 || weight > backward_weight || + if (backward_open && (*backward_parent_rtindex == -1 || weight > backward_weight || (weight == backward_weight && - backward_depth.at(parent) < backward_depth.at(*backward_from)))) + backward_depth.at(parent_rtindex) < backward_depth.at(*backward_parent_rtindex)))) { - *backward_from = parent; + *backward_parent_rtindex = parent_rtindex; backward_weight = weight; } } @@ -211,7 +212,7 @@ rpt_find_edge(const RptTransferMgr *mgr, const std::set &constructed, * LargestRootUpdated (transfer_graph_manager.cpp:557-626): grow one * component of the spanning forest by Prim's algorithm, rooted at the * largest filtered-or-intermediate vertex, falling back to the largest - * vertex. sorted_ids lists every vertex by ascending cardinality; only + * vertex. sorted_rtindexes lists every vertex by ascending cardinality; only * those still in `remaining` are candidates, and placed vertices are removed * from it (the artifact instead erases from its operator map and can pick a * stale root from its sorted list on the next component; keeping `remaining` @@ -219,7 +220,7 @@ rpt_find_edge(const RptTransferMgr *mgr, const std::set &constructed, */ static void rpt_largest_root_updated(RptTransferMgr *mgr, std::set &remaining, - const std::vector &sorted_ids, + const std::vector &sorted_rtindexes, std::vector > &forward_edges, std::vector > &backward_edges, int32 &next_order, bool largest_relation) @@ -230,80 +231,80 @@ rpt_largest_root_updated(RptTransferMgr *mgr, std::set &remaining, std::set unconstructed_set; std::vector::const_reverse_iterator rit; std::set::iterator sit; - int root = -1; + int root_rtindex = -1; /* Try to choose the largest filtered or intermediate table as the root */ - for (rit = sorted_ids.rbegin(); rit != sorted_ids.rend(); ++rit) + for (rit = sorted_rtindexes.rbegin(); rit != sorted_rtindexes.rend(); ++rit) { - const RptVertexInfo &v = mgr->vertices[mgr->by_rti[*rit]]; + const RptVertexInfo &v = mgr->vertices[mgr->vertex_index_by_rtindex[*rit]]; if (remaining.count(*rit) && (largest_relation || v.filtered || v.intermediate)) { - root = *rit; + root_rtindex = *rit; break; } } /* If we cannot find it, all tables have no filter */ - if (root == -1) + if (root_rtindex == -1) { - for (rit = sorted_ids.rbegin(); rit != sorted_ids.rend(); ++rit) + for (rit = sorted_rtindexes.rbegin(); rit != sorted_rtindexes.rend(); ++rit) { if (remaining.count(*rit)) { - root = *rit; + root_rtindex = *rit; break; } } } - Assert(root != -1); + Assert(root_rtindex != -1); /* Initialize nodes */ for (sit = remaining.begin(); sit != remaining.end(); ++sit) { - if (*sit == root) + if (*sit == root_rtindex) constructed_set.insert(*sit); else unconstructed_set.insert(*sit); - mgr->graph[*sit].id = *sit; + mgr->graph[*sit].rtindex = *sit; mgr->graph[*sit].cardinality_order = -1; } /* Add root */ - mgr->roots.insert(root); - forward_depth[root] = backward_depth[root] = 0; - mgr->graph[root].cardinality_order = next_order++; - mgr->transfer_order.push_back(root); - rpt_register_leader(mgr, root); - remaining.erase(root); + mgr->root_rtindexes.insert(root_rtindex); + forward_depth[root_rtindex] = backward_depth[root_rtindex] = 0; + mgr->graph[root_rtindex].cardinality_order = next_order++; + mgr->transfer_order_rtindexes.push_back(root_rtindex); + rpt_register_leader(mgr, root_rtindex); + remaining.erase(root_rtindex); /* Build graph */ while (!unconstructed_set.empty()) { - int forward_from; - int backward_from; - int to_id; + int forward_parent_rtindex; + int backward_parent_rtindex; + int target_rtindex; if (!rpt_find_edge(mgr, constructed_set, unconstructed_set, forward_depth, backward_depth, - &forward_from, &backward_from, &to_id)) + &forward_parent_rtindex, &backward_parent_rtindex, &target_rtindex)) break; - if (forward_from != -1) - forward_edges.push_back(std::make_pair(forward_from, to_id)); - if (backward_from != -1) - backward_edges.push_back(std::make_pair(backward_from, to_id)); - forward_depth[to_id] = forward_from == -1 ? 0 : forward_depth[forward_from] + 1; - backward_depth[to_id] = backward_from == -1 ? 0 : backward_depth[backward_from] + 1; - mgr->graph[to_id].cardinality_order = next_order++; - mgr->transfer_order.push_back(to_id); - rpt_register_leader(mgr, to_id); - - unconstructed_set.erase(to_id); - constructed_set.insert(to_id); - remaining.erase(to_id); + if (forward_parent_rtindex != -1) + forward_edges.push_back(std::make_pair(forward_parent_rtindex, target_rtindex)); + if (backward_parent_rtindex != -1) + backward_edges.push_back(std::make_pair(backward_parent_rtindex, target_rtindex)); + forward_depth[target_rtindex] = forward_parent_rtindex == -1 ? 0 : forward_depth[forward_parent_rtindex] + 1; + backward_depth[target_rtindex] = backward_parent_rtindex == -1 ? 0 : backward_depth[backward_parent_rtindex] + 1; + mgr->graph[target_rtindex].cardinality_order = next_order++; + mgr->transfer_order_rtindexes.push_back(target_rtindex); + rpt_register_leader(mgr, target_rtindex); + + unconstructed_set.erase(target_rtindex); + constructed_set.insert(target_rtindex); + remaining.erase(target_rtindex); } /* @@ -345,8 +346,8 @@ rpt_create_transfer_plan(RptTransferMgr *mgr, continue; edge = &eit->second; - left_node = &mgr->graph[edge->left_v]; - right_node = &mgr->graph[edge->right_v]; + left_node = &mgr->graph[edge->left_rtindex]; + right_node = &mgr->graph[edge->right_rtindex]; protect_left = edge->protect_left; protect_right = edge->protect_right; @@ -366,13 +367,13 @@ rpt_create_transfer_plan(RptTransferMgr *mgr, } /* forward: from the smaller to the larger */ - if (forward && !protect_right && !mgr->roots.count(left_node->id) && - (mgr->vertices[mgr->by_rti[left_node->id]].filtered || - mgr->bridges.count(left_node->id))) + if (forward && !protect_right && !mgr->root_rtindexes.count(left_node->rtindex) && + (mgr->vertices[mgr->vertex_index_by_rtindex[left_node->rtindex]].filtered || + mgr->bridge_rtindexes.count(left_node->rtindex))) { - rpt_node_add_keys(left_node, right_node->id, left_keys, right_keys, + rpt_node_add_keys(left_node, right_node->rtindex, left_keys, right_keys, key_types, true, false); - rpt_node_add_keys(right_node, left_node->id, left_keys, right_keys, + rpt_node_add_keys(right_node, left_node->rtindex, left_keys, right_keys, key_types, true, true); } @@ -394,23 +395,23 @@ rpt_create_transfer_plan(RptTransferMgr *mgr, { RptKey rep = RptFindKeyRoot(right_keys[c], mgr->uf_parent); std::map::iterator git = mgr->groups.find(rep); - int dest; + int target_rtindex; RptKey dest_key; RptGraphEdge *bucket; - if (git != mgr->groups.end() && git->second.leader_id != -1) + if (git != mgr->groups.end() && git->second.leader_rtindex != -1) { - dest = git->second.leader_id; + target_rtindex = git->second.leader_rtindex; dest_key = git->second.leader_key; } else { - dest = right_node->id; + target_rtindex = right_node->rtindex; dest_key = right_keys[c]; } - bucket = &by_dest[dest]; - bucket->destination = dest; + bucket = &by_dest[target_rtindex]; + bucket->other_rtindex = target_rtindex; bucket->left_keys.push_back(left_keys[c]); bucket->right_keys.push_back(dest_key); bucket->key_types.push_back(key_types[c]); @@ -420,10 +421,10 @@ rpt_create_transfer_plan(RptTransferMgr *mgr, { const RptGraphEdge &bucket = dit->second; - rpt_node_add_keys(left_node, bucket.destination, + rpt_node_add_keys(left_node, bucket.other_rtindex, bucket.left_keys, bucket.right_keys, bucket.key_types, false, true); - rpt_node_add_keys(&mgr->graph[bucket.destination], left_node->id, + rpt_node_add_keys(&mgr->graph[bucket.other_rtindex], left_node->rtindex, bucket.left_keys, bucket.right_keys, bucket.key_types, false, false); } @@ -436,10 +437,10 @@ rpt_create_transfer_plan(RptTransferMgr *mgr, * change where the reducer reads a key. Parallel workers still need a * segment-local fold (or a non-parallel reducer). */ static bool -rpt_local_hop(const RptTransferMgr *mgr, int source, const RptGraphEdge &hop) +rpt_local_hop(const RptTransferMgr *mgr, int source_rtindex, const RptGraphEdge &hop) { - const auto &src = mgr->vertices[mgr->by_rti.at(source)]; - const auto &dst = mgr->vertices[mgr->by_rti.at(hop.destination)]; + const auto &src = mgr->vertices[mgr->vertex_index_by_rtindex.at(source_rtindex)]; + const auto &dst = mgr->vertices[mgr->vertex_index_by_rtindex.at(hop.other_rtindex)]; /* A replicated destination needs the complete key set on every segment. */ if (dst.replicated || src.numsegments <= 0 || dst.numsegments <= 0) @@ -458,10 +459,10 @@ rpt_local_hop(const RptTransferMgr *mgr, int source, const RptGraphEdge &hop) { const RptKey &a = hop.left_keys[key]; const RptKey &b = hop.right_keys[key]; - if ((a == RptKey{source, src.distkeys[col]} && - b == RptKey{hop.destination, dst.distkeys[col]}) || - (b == RptKey{source, src.distkeys[col]} && - a == RptKey{hop.destination, dst.distkeys[col]})) + if ((a == RptKey{source_rtindex, src.distkeys[col]} && + b == RptKey{hop.other_rtindex, dst.distkeys[col]}) || + (b == RptKey{source_rtindex, src.distkeys[col]} && + a == RptKey{hop.other_rtindex, dst.distkeys[col]})) matched = true; } if (!matched) @@ -471,7 +472,7 @@ rpt_local_hop(const RptTransferMgr *mgr, int source, const RptGraphEdge &hop) } /* Persist the decision on both copies of every stage edge. In/out edges - * carry the same key pairs, but destination means producer for an in-edge. */ + * carry the same key pairs; other_rtindex is the producer on an in-edge. */ static void rpt_classify_hops(RptTransferMgr *mgr) { @@ -483,10 +484,10 @@ rpt_classify_hops(RptTransferMgr *mgr) for (auto &edge : outs) { edge.local = rpt_local_hop(mgr, node.first, edge); - auto &dest = mgr->graph.at(edge.destination); - auto &ins = forward ? dest.forward_stage.in : dest.backward_stage.in; + auto &target_rtindex = mgr->graph.at(edge.other_rtindex); + auto &ins = forward ? target_rtindex.forward_stage.in : target_rtindex.backward_stage.in; for (auto &in : ins) - if (in.destination == node.first) + if (in.other_rtindex == node.first) in.local = edge.local; } } @@ -503,22 +504,22 @@ rpt_build_transfer_plan(PlannedStmt *stmt, RptTransferMgr *mgr, { std::vector > forward_edges; std::vector > backward_edges; - std::vector sorted_ids; + std::vector sorted_rtindexes; std::set remaining; int32 next_order = 0; size_t i; - /* vertices by ascending cardinality, ties by id, for a stable plan */ + /* vertices by ascending cardinality, ties by rtindex, for a stable plan */ for (i = 0; i < mgr->vertices.size(); i++) { - sorted_ids.push_back(mgr->vertices[i].id); - remaining.insert(mgr->vertices[i].id); + sorted_rtindexes.push_back(mgr->vertices[i].rtindex); + remaining.insert(mgr->vertices[i].rtindex); } - std::sort(sorted_ids.begin(), sorted_ids.end(), + std::sort(sorted_rtindexes.begin(), sorted_rtindexes.end(), [&mgr] (int a, int b) { - const RptVertexInfo &va = mgr->vertices[mgr->by_rti[a]]; - const RptVertexInfo &vb = mgr->vertices[mgr->by_rti[b]]; + const RptVertexInfo &va = mgr->vertices[mgr->vertex_index_by_rtindex[a]]; + const RptVertexInfo &vb = mgr->vertices[mgr->vertex_index_by_rtindex[b]]; if (va.cardinality != vb.cardinality) return va.cardinality < vb.cardinality; @@ -531,12 +532,12 @@ rpt_build_transfer_plan(PlannedStmt *stmt, RptTransferMgr *mgr, * forest -- one tree per connected component of the join graph. */ while (!remaining.empty()) - rpt_largest_root_updated(mgr, remaining, sorted_ids, forward_edges, backward_edges, + rpt_largest_root_updated(mgr, remaining, sorted_rtindexes, forward_edges, backward_edges, next_order, largest_relation); for (const auto &group : mgr->groups) - if (group.second.leader_id != -1 && !mgr->roots.count(group.second.leader_id)) - mgr->bridges.insert(group.second.leader_id); + if (group.second.leader_rtindex != -1 && !mgr->root_rtindexes.count(group.second.leader_rtindex)) + mgr->bridge_rtindexes.insert(group.second.leader_rtindex); rpt_create_transfer_plan(mgr, forward_edges, true); rpt_create_transfer_plan(mgr, backward_edges, false); @@ -544,27 +545,27 @@ rpt_build_transfer_plan(PlannedStmt *stmt, RptTransferMgr *mgr, /* Transfers run in reverse insertion order. Cap actual dependency hops, * keeping every inbound edge mirrored in its producer's out list. */ std::map depth; - for (auto it = mgr->transfer_order.rbegin(); it != mgr->transfer_order.rend(); ++it) + for (auto it = mgr->transfer_order_rtindexes.rbegin(); it != mgr->transfer_order_rtindexes.rend(); ++it) { auto &outs = mgr->graph[*it].forward_stage.out; for (auto edge = outs.begin(); edge != outs.end();) { - int dest = edge->destination; + int target_rtindex = edge->other_rtindex; if (depth[*it] >= rpt_max_chain_depth) { - auto &ins = mgr->graph[dest].forward_stage.in; + auto &ins = mgr->graph[target_rtindex].forward_stage.in; ins.erase(std::remove_if(ins.begin(), ins.end(), - [&](const RptGraphEdge &in) { return in.destination == *it; }), ins.end()); + [&](const RptGraphEdge &in) { return in.other_rtindex == *it; }), ins.end()); edge = outs.erase(edge); } else { - depth[dest] = std::max(depth[dest], depth[*it] + 1); + depth[target_rtindex] = std::max(depth[target_rtindex], depth[*it] + 1); ++edge; } } if (!outs.empty()) - mgr->reducer_order.push_back(*it); + mgr->reducer_order_rtindexes.push_back(*it); } rpt_classify_hops(mgr); @@ -579,31 +580,24 @@ RptBuildTransferPlan(PlannedStmt *stmt, RptTransferMgr *mgr) rpt_build_transfer_plan(stmt, mgr, false); } -/* The reducer targets the largest relation, including an unfiltered fact. - * Build a separate schedule: changing the backward leaders would change the - * eligibility survey and its independently gated injection decisions. */ +/* Runtime transfers use one largest-relation forest for both passes. + * Keep the filtered-root survey schedule separate for diagnostic continuity. + * Eligibility is evaluated by the caller against this forest before surgery. */ void RptBuildForwardPlan(const RptTransferMgr *analysis, RptTransferMgr *forward) { *forward = *analysis; forward->graph.clear(); - forward->roots.clear(); - forward->bridges.clear(); - forward->reducer_order.clear(); - forward->transfer_order.clear(); - forward->consumer_ok.clear(); + forward->root_rtindexes.clear(); + forward->bridge_rtindexes.clear(); + forward->reducer_order_rtindexes.clear(); + forward->transfer_order_rtindexes.clear(); + forward->consumer_ok_by_rtindex.clear(); forward->backward_consumers.clear(); for (auto &group : forward->groups) - group.second.leader_id = -1; + group.second.leader_rtindex = -1; rpt_build_transfer_plan(NULL, forward, true); - /* This schedule is not a backward eligibility decision. */ - forward->consumer_ok.clear(); - forward->backward_consumers.clear(); - for (auto &node : forward->graph) - { - node.second.backward_stage.in.clear(); - node.second.backward_stage.out.clear(); - } + } /* @@ -615,8 +609,8 @@ RptBuildForwardPlan(const RptTransferMgr *analysis, RptTransferMgr *forward) static bool rpt_edge_less(const RptGraphEdge &a, const RptGraphEdge &b) { - if (a.destination != b.destination) - return a.destination < b.destination; + if (a.other_rtindex != b.other_rtindex) + return a.other_rtindex < b.other_rtindex; if (a.left_keys != b.left_keys) return a.left_keys < b.left_keys; return a.right_keys < b.right_keys; @@ -636,7 +630,7 @@ rpt_dump_key_pairs(std::ostream &out, const RptGraphEdge &edge) /* A stage's sorted build/apply entries, preserving the diagnostic format. */ static void -rpt_dump_stage(std::ostream &out, int vertex_id, const char *stage, +rpt_dump_stage(std::ostream &out, int rtindex, const char *stage, const std::vector &in_edges, const std::vector &out_edges) { @@ -646,17 +640,17 @@ rpt_dump_stage(std::ostream &out, int vertex_id, const char *stage, std::sort(outs.begin(), outs.end(), rpt_edge_less); std::sort(ins.begin(), ins.end(), rpt_edge_less); - out << "vertex " << vertex_id << ' ' << stage << ':'; + out << "vertex " << rtindex << ' ' << stage << ':'; for (const auto &edge : outs) { - out << (any ? "; " : " ") << "build for " << edge.destination << " on ("; + out << (any ? "; " : " ") << "build for " << edge.other_rtindex << " on ("; rpt_dump_key_pairs(out, edge); out << ')'; any = true; } for (const auto &edge : ins) { - out << (any ? "; " : " ") << "apply from " << edge.destination << " on ("; + out << (any ? "; " : " ") << "apply from " << edge.other_rtindex << " on ("; rpt_dump_key_pairs(out, edge); out << ')'; any = true; @@ -669,33 +663,43 @@ rpt_dump_stage(std::ostream &out, int vertex_id, const char *stage, /* Build in C++ storage, then cross the PostgreSQL allocation boundary once. * RptPgCall ensures a failed append unwinds the stream and string owners. */ void -RptDumpTransferPlan(const RptTransferMgr *mgr, StringInfo output) +RptDumpTransferPlan(const PlannedStmt *stmt, const RptTransferMgr *mgr, StringInfo output) { std::ostringstream out; - std::vector ids; + std::vector rtindexes; out.exceptions(std::ios::badbit | std::ios::failbit); out.imbue(std::locale::classic()); for (const auto &vertex : mgr->vertices) - ids.push_back(vertex.id); - std::sort(ids.begin(), ids.end()); + rtindexes.push_back(vertex.rtindex); + std::sort(rtindexes.begin(), rtindexes.end()); out << "transfer order:"; - for (int id : mgr->transfer_order) - out << ' ' << id; + for (int rtindex : mgr->transfer_order_rtindexes) + out << ' ' << rtindex; out << "\nforward depth limit: " << rpt_max_chain_depth << "\nreducer order:"; - for (int id : mgr->reducer_order) - out << ' ' << id; + for (int rtindex : mgr->reducer_order_rtindexes) + out << ' ' << rtindex; out << "\nbridge tables:"; - for (int id : mgr->bridges) - out << ' ' << id; + for (int rtindex : mgr->bridge_rtindexes) + out << ' ' << rtindex; out << '\n'; - for (int id : ids) + for (int rtindex : rtindexes) { - const auto &v = mgr->vertices[mgr->by_rti.at(id)]; + const auto &v = mgr->vertices[mgr->vertex_index_by_rtindex.at(rtindex)]; + const char *alias = ""; - out << "vertex " << v.id << ' ' << v.relname << " rows=" << v.cardinality + /* Use the query alias, so self-join references remain distinct. No + * name is copied or looked up until a diagnostic dump is requested. */ + if (stmt != NULL && rtindex > 0 && rtindex <= list_length(stmt->rtable)) + { + RangeTblEntry *rte = rt_fetch(rtindex, stmt->rtable); + + if (rte->eref != NULL && rte->eref->aliasname != NULL) + alias = rte->eref->aliasname; + } + out << "vertex " << v.rtindex << ' ' << alias << " rows=" << v.cardinality << ' ' << (v.filtered ? "filtered" : v.intermediate ? "intermediate" : "unfiltered") << '\n'; } @@ -704,44 +708,44 @@ RptDumpTransferPlan(const RptTransferMgr *mgr, StringInfo output) bool first = true; out << "class " << group.first.rtindex << '.' << group.first.attno << ": members="; - for (int member : group.second.members) + for (int member_rtindex : group.second.member_rtindexes) { if (!first) out << ','; - out << member; + out << member_rtindex; first = false; } - out << " leader=" << group.second.leader_id << '\n'; + out << " leader=" << group.second.leader_rtindex << '\n'; } - for (int id : ids) + for (int rtindex : rtindexes) { - const auto &node = mgr->graph.find(id); + const auto &node = mgr->graph.find(rtindex); if (node == mgr->graph.end()) { - out << "vertex " << id << " forward: none\n" - << "vertex " << id << " backward: none\n"; + out << "vertex " << rtindex << " forward: none\n" + << "vertex " << rtindex << " backward: none\n"; continue; } - rpt_dump_stage(out, id, "forward", node->second.forward_stage.in, + rpt_dump_stage(out, rtindex, "forward", node->second.forward_stage.in, node->second.forward_stage.out); - rpt_dump_stage(out, id, "backward", node->second.backward_stage.in, + rpt_dump_stage(out, rtindex, "backward", node->second.backward_stage.in, node->second.backward_stage.out); } for (const auto &node : mgr->graph) { for (const auto &edge : node.second.forward_stage.out) - out << "forward hop " << node.first << "->" << edge.destination + out << "forward hop " << node.first << "->" << edge.other_rtindex << " merge=" << (edge.local ? "local" : "global") << '\n'; for (const auto &edge : node.second.backward_stage.out) - out << "backward hop " << node.first << "->" << edge.destination + out << "backward hop " << node.first << "->" << edge.other_rtindex << " merge=" << (edge.local ? "local" : "global") << '\n'; } int global_edges = 0, rule9 = 0, strict = 0, eligible = 0, root_classes = 0; int unknown_estimates = 0; double rows_removed = 0; for (const auto &group : mgr->groups) - if (mgr->roots.count(group.second.leader_id)) + if (mgr->root_rtindexes.count(group.second.leader_rtindex)) ++root_classes; for (const auto &decision : mgr->backward_consumers) { @@ -756,15 +760,15 @@ RptDumpTransferPlan(const RptTransferMgr *mgr, StringInfo output) else rows_removed += decision.estimated_rows_removed; } - out << "backward consumer " << decision.consumer << " producer " << decision.producer + out << "backward consumer " << decision.consumer_rtindex << " producer " << decision.producer_rtindex << " class " << decision.class_key.rtindex << '.' << decision.class_key.attno << " merge=" << (decision.local ? "local" : "global") << " rule9=" << (decision.rule9 ? "yes" : "no") << " strict=" << (decision.strict_eligible ? "yes" : "no") << " eligible=" << (decision.eligible ? "yes" : "no") << " defer_first=" << (decision.defer_first ? "yes" : "no") - << " producer_slice=" << decision.producer_slice - << " consumer_slice=" << decision.consumer_slice + << " producer_slice=" << decision.producer_slice_index + << " consumer_slice=" << decision.consumer_slice_index << " producer_attno=" << decision.producer_attno << " consumer_attno=" << decision.consumer_attno << " rows_removed="; @@ -778,16 +782,16 @@ RptDumpTransferPlan(const RptTransferMgr *mgr, StringInfo output) << " global_edges=" << global_edges << " rule9=" << rule9 << " strict=" << strict << " eligible=" << eligible << " known_rows_removed=" << rows_removed << " unknown_estimates=" << unknown_estimates << '\n'; - for (int id : ids) + for (int rtindex : rtindexes) { - const auto &consumer = mgr->consumer_ok.find(id); - bool ok = consumer != mgr->consumer_ok.end() && consumer->second; - out << "consumer_ok " << id << ' ' << (ok ? "yes" : "no"); + const auto &consumer = mgr->consumer_ok_by_rtindex.find(rtindex); + bool ok = consumer != mgr->consumer_ok_by_rtindex.end() && consumer->second; + out << "consumer_ok " << rtindex << ' ' << (ok ? "yes" : "no"); if (!ok) { std::string reason = "no backward edge"; for (const auto &decision : mgr->backward_consumers) - if (decision.consumer == id && !decision.eligible) + if (decision.consumer_rtindex == rtindex && !decision.eligible) { reason = decision.reason; if (!decision.local) diff --git a/gpcontrib/robust_predicate_transfer/src/rptregion.cpp b/gpcontrib/robust_predicate_transfer/src/rptregion.cpp index 9e9994e31d2..114c1280278 100644 --- a/gpcontrib/robust_predicate_transfer/src/rptregion.cpp +++ b/gpcontrib/robust_predicate_transfer/src/rptregion.cpp @@ -124,27 +124,25 @@ rpt_scan_is_filtered(const Plan *plan) static RptVertexInfo * rpt_vertex_for_scan(PlannedStmt *stmt, RptTransferMgr *mgr, Plan *scan) { - int rti = ((Scan *) scan)->scanrelid; + int rtindex = ((Scan *) scan)->scanrelid; std::map::iterator it; RptVertexInfo v; - it = mgr->by_rti.find(rti); - if (it != mgr->by_rti.end()) + it = mgr->vertex_index_by_rtindex.find(rtindex); + if (it != mgr->vertex_index_by_rtindex.end()) return &mgr->vertices[it->second]; - v.id = rti; + v.rtindex = rtindex; v.scan = scan; v.cardinality = (int64) scan->plan_rows; v.filtered = rpt_scan_is_filtered(scan); v.intermediate = false; v.unfiltered = false; - if (rti >= 1 && rti <= list_length(stmt->rtable)) + if (rtindex >= 1 && rtindex <= list_length(stmt->rtable)) { - RangeTblEntry *rte = rt_fetch(rti, stmt->rtable); + RangeTblEntry *rte = rt_fetch(rtindex, stmt->rtable); - if (rte->eref != NULL) - v.relname = rte->eref->aliasname; if (rte->rtekind == RTE_RELATION) { v.relid = rte->relid; @@ -160,7 +158,7 @@ rpt_vertex_for_scan(PlannedStmt *stmt, RptTransferMgr *mgr, Plan *scan) } } - mgr->by_rti[rti] = (int) mgr->vertices.size(); + mgr->vertex_index_by_rtindex[rtindex] = (int) mgr->vertices.size(); mgr->vertices.push_back(v); return &mgr->vertices.back(); } @@ -391,7 +389,7 @@ rpt_equality_clause(Node *clause, Var **outer_var, Var **inner_var, * Resolve one validated equijoin clause to its two base scans and record the * edge between their vertices. * - * left_v/right_v are canonicalized (lower rtindex on the left), not the + * left_rtindex/right_rtindex are canonicalized (lower rtindex on the left), not the * join's outer/inner: every clause of a vertex pair merges into one * RptEdgeInfo and the neighbor matrix is symmetric, so a second join over * the same pair with the opposite orientation must land on the same edge @@ -417,15 +415,15 @@ rpt_add_edge(PlannedStmt *stmt, RptTransferMgr *mgr, JoinType jointype, Plan *inner_scan; AttrNumber outer_key; AttrNumber inner_key; - int outer_id; - int inner_id; + int outer_rtindex; + int inner_rtindex; RptVertexInfo *outer_v; RptVertexInfo *inner_v; - int left_id; - int right_id; + int left_rtindex; + int right_rtindex; RptKey left_key; RptKey right_key; - int protect_id = 0; + int protected_rtindex = 0; RptEdgeInfo *edge; /* ANTI, FULL and the GP extras make no usable edge at all. */ @@ -440,8 +438,8 @@ rpt_add_edge(PlannedStmt *stmt, RptTransferMgr *mgr, JoinType jointype, &inner_scan, &inner_key)) return; - outer_id = ((Scan *) outer_scan)->scanrelid; - inner_id = ((Scan *) inner_scan)->scanrelid; + outer_rtindex = ((Scan *) outer_scan)->scanrelid; + inner_rtindex = ((Scan *) inner_scan)->scanrelid; /* * Create both vertices before taking pointers into mgr.vertices: creating @@ -449,54 +447,54 @@ rpt_add_edge(PlannedStmt *stmt, RptTransferMgr *mgr, JoinType jointype, */ rpt_vertex_for_scan(stmt, mgr, outer_scan); rpt_vertex_for_scan(stmt, mgr, inner_scan); - outer_v = &mgr->vertices[mgr->by_rti[outer_id]]; - inner_v = &mgr->vertices[mgr->by_rti[inner_id]]; + outer_v = &mgr->vertices[mgr->vertex_index_by_rtindex[outer_rtindex]]; + inner_v = &mgr->vertices[mgr->vertex_index_by_rtindex[inner_rtindex]]; /* The preserved side of an outer join is protected from reduction. */ if (jointype == JOIN_LEFT) - protect_id = outer_id; + protected_rtindex = outer_rtindex; else if (jointype == JOIN_RIGHT) - protect_id = inner_id; + protected_rtindex = inner_rtindex; - if (outer_id <= inner_id) + if (outer_rtindex <= inner_rtindex) { - left_id = outer_id; - left_key.rtindex = outer_id; + left_rtindex = outer_rtindex; + left_key.rtindex = outer_rtindex; left_key.attno = outer_key; - right_id = inner_id; - right_key.rtindex = inner_id; + right_rtindex = inner_rtindex; + right_key.rtindex = inner_rtindex; right_key.attno = inner_key; } else { - left_id = inner_id; - left_key.rtindex = inner_id; + left_rtindex = inner_rtindex; + left_key.rtindex = inner_rtindex; left_key.attno = inner_key; - right_id = outer_id; - right_key.rtindex = outer_id; + right_rtindex = outer_rtindex; + right_key.rtindex = outer_rtindex; right_key.attno = outer_key; } - edge = &mgr->neighbor_matrix[left_id][right_id]; + edge = &mgr->neighbor_matrix[left_rtindex][right_rtindex]; if (edge->clauses.empty()) { - edge->left_v = left_id; - edge->right_v = right_id; + edge->left_rtindex = left_rtindex; + edge->right_rtindex = right_rtindex; edge->key_type = key_type; edge->protect_left = false; edge->protect_right = false; } edge->clauses.push_back(std::make_pair(left_key, right_key)); - if (protect_id == left_id) + if (protected_rtindex == left_rtindex) edge->protect_left = true; - else if (protect_id == right_id) + else if (protected_rtindex == right_rtindex) edge->protect_right = true; - outer_v->join_keys.insert(outer_id == left_id ? left_key : right_key); - inner_v->join_keys.insert(inner_id == left_id ? left_key : right_key); + outer_v->join_keys.insert(outer_rtindex == left_rtindex ? left_key : right_key); + inner_v->join_keys.insert(inner_rtindex == left_rtindex ? left_key : right_key); /* The matrix is symmetric: both directions name the same edge value. */ - mgr->neighbor_matrix[right_id][left_id] = *edge; + mgr->neighbor_matrix[right_rtindex][left_rtindex] = *edge; } /* diff --git a/gpcontrib/robust_predicate_transfer/test/diagnose_forward_test.py b/gpcontrib/robust_predicate_transfer/test/diagnose_rpt_test.py similarity index 71% rename from gpcontrib/robust_predicate_transfer/test/diagnose_forward_test.py rename to gpcontrib/robust_predicate_transfer/test/diagnose_rpt_test.py index 22622bb8e41..b4aeeeb4ed4 100644 --- a/gpcontrib/robust_predicate_transfer/test/diagnose_forward_test.py +++ b/gpcontrib/robust_predicate_transfer/test/diagnose_rpt_test.py @@ -26,8 +26,8 @@ import unittest ROOT = Path(__file__).resolve().parents[1] -SCRIPT = ROOT / "scripts" / "diagnose_forward.py" -spec = importlib.util.spec_from_file_location("diagnose_forward", SCRIPT) +SCRIPT = ROOT / "scripts" / "diagnose_rpt.py" +spec = importlib.util.spec_from_file_location("diagnose_rpt", SCRIPT) diag = importlib.util.module_from_spec(spec) spec.loader.exec_module(diag) @@ -40,13 +40,34 @@ def test_instrumentation_retains_assertions_and_reports_dynamic_queries(self): self.assertIn("RPT STEP explain analyze=% SQL=%", output) self.assertIn("RPT STEP results rpt=% SQL=%", output) self.assertIn("RPT STEP transfer_plan SQL=%", output) - self.assertIn("RETURN public.rpt_transfer_plan(q);", output) + self.assertIn("dump := public.rpt_transfer_plan(q);", output) self.assertIn("RPT STEP SPI local chain", output) def test_changed_fixture_fails_instead_of_silently_losing_markers(self): with self.assertRaises(ValueError): diag.instrument_sql("SELECT 1;") + def test_backward_decision_summary(self): + dump = """backward consumer 2 producer 1 eligible=yes reason=eligible +backward class root=1 rep=1.2 consumers=1 bytes=1048576 est_cost_ms=12.900 decision=inject +backward consumer 2 root=1 rep=1.2 key=rpt_b_1_1_2 defer_first=yes rows_removed=98 decision=inject +backward consumer 3 root=1 rep=1.2 key=- defer_first=no rows_removed=-1 decision=sort between consumer and join +""" + report = diag.summarize_dump(dump) + self.assertEqual(report["classes"], 1) + self.assertEqual(report["injected_consumers"], 1) + self.assertEqual(report["refused_consumers"], {"sort between consumer and join": 1}) + self.assertAlmostEqual(report["estimated_cost_ms"], 12.9) + log = "NOTICE: RPT DUMP BEGIN SQL=SELECT 1\nNOTICE: " + dump + "NOTICE: RPT DUMP END\n" + self.assertEqual(diag.summarize_log(log), [dict(sql="SELECT 1", **report)]) + + def test_backward_instrumentation(self): + for suite in ("backward", "backward_faults"): + output = diag.instrument_sql((ROOT / "sql" / ("rpt_" + suite + ".sql")).read_text(), suite) + self.assertIn("RPT DUMP BEGIN SQL=%", output) + self.assertIn("RPT STEP results SQL=%", output) + self.assertIn("RAISE EXCEPTION", output) + def run_fixture(self, hangs): with tempfile.TemporaryDirectory() as directory: root = Path(directory) diff --git a/gpcontrib/robust_predicate_transfer/test/forward_walk_test.cpp b/gpcontrib/robust_predicate_transfer/test/forward_walk_test.cpp index ef0f3ee0d70..013027b0404 100644 --- a/gpcontrib/robust_predicate_transfer/test/forward_walk_test.cpp +++ b/gpcontrib/robust_predicate_transfer/test/forward_walk_test.cpp @@ -93,6 +93,18 @@ int main() CHECK(!rpt_has_runtime_filters(&stmt)); anser_node = &marker.scan.plan; CHECK(rpt_has_runtime_filters(&stmt)); + /* The parent walk shares the statement lookup context and preserves + * ambiguous placements as refusals rather than choosing the last one. */ + std::set targets = {&marker.scan.plan}; + std::map slices; + auto parents = rpt_find_parents(&stmt, targets, &slices); + CHECK(parents.empty()); /* SubPlan lookup does not enter its plan. */ + root.plan.lefttree = &marker.scan.plan; + parents = rpt_find_parents(&stmt, targets, &slices); + CHECK(parents.at(&marker.scan.plan) == &root.plan); + root.plan.righttree = &marker.scan.plan; + parents = rpt_find_parents(&stmt, targets, &slices); + CHECK(parents.at(&marker.scan.plan) == NULL); CHECK(PG_exception_stack == NULL && error_context_stack == NULL); return 0; } diff --git a/gpcontrib/robust_predicate_transfer/test/graph_test.cpp b/gpcontrib/robust_predicate_transfer/test/graph_test.cpp index 51a5a5f28c7..71402779597 100644 --- a/gpcontrib/robust_predicate_transfer/test/graph_test.cpp +++ b/gpcontrib/robust_predicate_transfer/test/graph_test.cpp @@ -34,6 +34,7 @@ #include "../src/rpteligible.cpp" #include "../include/rptqual.h" #include "../src/rptforwardplan.cpp" +#include "../src/rptbackwardplan.cpp" #include #include @@ -54,8 +55,8 @@ struct TestResolution Plan *parent; Plan *scan; AttrNumber attno; - int slice; - bool consumer; + int slice_index; + bool for_consumer; }; static std::map test_resolutions; @@ -67,17 +68,17 @@ HeapTuple SearchSysCache1(int, Datum) { abort(); } HeapTuple SearchSysCache3(int, Datum, Datum, Datum) { abort(); } void ReleaseSysCache(HeapTuple) { abort(); } bool AnserResolveKeyScan(Plan *top, AttrNumber slot, int, bool consumer, - Plan **parent, Plan **scan, AttrNumber *attno, int *slice) + Plan **parent, Plan **scan, AttrNumber *attno, int *slice_index) { CHECK(slot > 0); auto found = test_resolutions.find(top); if (found == test_resolutions.end()) return false; - CHECK(found->second.consumer == consumer); + CHECK(found->second.for_consumer == consumer); *parent = found->second.parent; *scan = found->second.scan; *attno = found->second.attno; - *slice = found->second.slice; + *slice_index = found->second.slice_index; return true; } Oid getBaseType(Oid type) { return type; } @@ -119,13 +120,13 @@ bool rpt_backward = true; int rpt_max_backward_consumers = 4; int rpt_max_chain_depth = 4; -static void vertex(RptTransferMgr &mgr, int id, int rows, bool filtered) +static void vertex(RptTransferMgr &mgr, int rtindex, int rows, bool filtered) { RptVertexInfo v = {}; - v.id = id; + v.rtindex = rtindex; v.cardinality = rows; v.filtered = filtered; - mgr.by_rti[id] = mgr.vertices.size(); + mgr.vertex_index_by_rtindex[rtindex] = mgr.vertices.size(); mgr.vertices.push_back(v); } @@ -133,8 +134,8 @@ static RptEdgeInfo edge(int a, int b, bool protect_a = false, bool protect_b = false, int col_a = 1, int col_b = 1) { RptEdgeInfo e = {}; - e.left_v = a; - e.right_v = b; + e.left_rtindex = a; + e.right_rtindex = b; e.key_type = 23; e.protect_left = protect_a; e.protect_right = protect_b; @@ -146,8 +147,8 @@ static RptEdgeInfo edge(int a, int b, bool protect_a = false, static void connect(RptTransferMgr &mgr, const RptEdgeInfo &e) { rpt_upsert_edge(&mgr, e); - mgr.vertices[mgr.by_rti[e.left_v]].join_keys.insert(e.clauses[0].first); - mgr.vertices[mgr.by_rti[e.right_v]].join_keys.insert(e.clauses[0].second); + mgr.vertices[mgr.vertex_index_by_rtindex[e.left_rtindex]].join_keys.insert(e.clauses[0].first); + mgr.vertices[mgr.vertex_index_by_rtindex[e.right_rtindex]].join_keys.insert(e.clauses[0].second); } static void bypass_direction(bool reverse) @@ -182,10 +183,10 @@ static RptTransferMgr star(int cap) { RptTransferMgr mgr; rpt_max_chain_depth = cap; - for (int id = 1; id <= 4; ++id) - vertex(mgr, id, 100 / id, true); - for (int id = 2; id <= 4; ++id) - connect(mgr, edge(1, id)); + for (int rtindex = 1; rtindex <= 4; ++rtindex) + vertex(mgr, rtindex, 100 / rtindex, true); + for (int rtindex = 2; rtindex <= 4; ++rtindex) + connect(mgr, edge(1, rtindex)); RptBuildClasses(&mgr); RptBuildTransferPlan(NULL, &mgr); return mgr; @@ -195,14 +196,14 @@ static void asymmetric_shape() { auto mgr = star(4); CHECK(mgr.groups.size() == 1); - CHECK(mgr.transfer_order == std::vector({1, 2, 3, 4})); - CHECK(mgr.reducer_order == std::vector({4, 3, 2})); - for (int id = 2; id <= 4; ++id) + CHECK(mgr.transfer_order_rtindexes == std::vector({1, 2, 3, 4})); + CHECK(mgr.reducer_order_rtindexes == std::vector({4, 3, 2})); + for (int rtindex = 2; rtindex <= 4; ++rtindex) { - CHECK(mgr.graph[id].forward_stage.out.size() == 1); - CHECK(mgr.graph[id].forward_stage.out[0].destination == id - 1); - CHECK(mgr.graph[id].backward_stage.in.size() == 1); - CHECK(mgr.graph[id].backward_stage.in[0].destination == 1); + CHECK(mgr.graph[rtindex].forward_stage.out.size() == 1); + CHECK(mgr.graph[rtindex].forward_stage.out[0].other_rtindex == rtindex - 1); + CHECK(mgr.graph[rtindex].backward_stage.in.size() == 1); + CHECK(mgr.graph[rtindex].backward_stage.in[0].other_rtindex == 1); } CHECK(mgr.graph[1].forward_stage.out.empty()); CHECK(mgr.graph[1].backward_stage.out.size() == 3); @@ -215,8 +216,8 @@ static void asymmetric_shape() static void unfiltered_cycle() { RptTransferMgr mgr; - for (int id = 1; id <= 4; ++id) - vertex(mgr, id, 10, id == 1); + for (int rtindex = 1; rtindex <= 4; ++rtindex) + vertex(mgr, rtindex, 10, rtindex == 1); connect(mgr, edge(1, 2)); connect(mgr, edge(2, 3)); connect(mgr, edge(3, 4)); @@ -300,12 +301,12 @@ static void strict_backward_eligibility() CHECK(plan.mgr.backward_consumers.size() == 1); auto decision = plan.mgr.backward_consumers[0]; CHECK(decision.eligible && decision.rule9 && decision.defer_first); - CHECK(decision.producer == 2 && decision.consumer == 1); + CHECK(decision.producer_rtindex == 2 && decision.consumer_rtindex == 1); CHECK(decision.consumer_attno == 1 && decision.producer_attno == 1); CHECK(decision.consumer_parent == (Plan *) &plan.join); CHECK(decision.producer_parent == (Plan *) &plan.motion); - CHECK(decision.producer_slice == 1 && decision.consumer_slice == 0); - CHECK(plan.mgr.consumer_ok[1]); + CHECK(decision.producer_slice_index == 1 && decision.consumer_slice_index == 0); + CHECK(plan.mgr.consumer_ok_by_rtindex[1]); for (NodeTag tag : {T_Sort, T_Agg, T_Material, T_Unique, T_WindowAgg, T_Hash, T_SetOp}) { @@ -368,7 +369,7 @@ static void strict_backward_eligibility() RptCheckBackwardConsumers(&plan.stmt, &plan.mgr); CHECK(plan.mgr.backward_consumers[0].reason == "same or unknown slice"); plan.motion.motionID = 1; - plan.mgr.roots.clear(); + plan.mgr.root_rtindexes.clear(); RptCheckBackwardConsumers(&plan.stmt, &plan.mgr); CHECK(plan.mgr.backward_consumers[0].reason == "bridge-led class is deferred"); plan.mgr.graph[2].backward_stage.out[0].local = true; @@ -417,21 +418,21 @@ static void every_global_producer_is_required() RptTransferMgr mgr; vertex(mgr, 1, 100, false); RptBackwardConsumer first; - first.consumer = 1; - first.producer = 2; + first.consumer_rtindex = 1; + first.producer_rtindex = 2; first.eligible = true; RptBackwardConsumer second = first; - second.producer = 3; + second.producer_rtindex = 3; second.eligible = false; mgr.backward_consumers = {first, second}; rpt_consumer_verdicts(&mgr); - CHECK(!mgr.consumer_ok[1]); + CHECK(!mgr.consumer_ok_by_rtindex[1]); mgr.backward_consumers[1].local = true; rpt_consumer_verdicts(&mgr); - CHECK(mgr.consumer_ok[1]); + CHECK(mgr.consumer_ok_by_rtindex[1]); mgr.backward_consumers[0].local = true; rpt_consumer_verdicts(&mgr); - CHECK(!mgr.consumer_ok[1]); + CHECK(!mgr.consumer_ok_by_rtindex[1]); } static void backward_consumer_cap() @@ -442,7 +443,7 @@ static void backward_consumer_cap() vertex(mgr, i, 100 * i, false); RptBackwardConsumer decision; decision.class_key = {9, 1}; - decision.consumer = i; + decision.consumer_rtindex = i; decision.strict_eligible = decision.eligible = true; decision.estimated_rows_removed = 100 * i; mgr.backward_consumers.push_back(decision); @@ -468,8 +469,13 @@ static void bridges_and_locality() rpt_max_chain_depth = 4; RptBuildTransferPlan(NULL, &mgr); CHECK(mgr.groups.size() == 2); - CHECK(mgr.bridges == std::set({2})); - CHECK(mgr.reducer_order == std::vector({3, 2})); + CHECK(mgr.bridge_rtindexes == std::set({2})); + CHECK(mgr.reducer_order_rtindexes == std::vector({3, 2})); + RptTransferMgr runtime; + RptBuildForwardPlan(&mgr, &runtime); + CHECK(runtime.root_rtindexes.count(1)); + CHECK(runtime.bridge_rtindexes.count(2)); + CHECK(!runtime.graph.at(2).backward_stage.out.empty()); for (auto &v : mgr.vertices) { v.numsegments = 3; @@ -477,7 +483,7 @@ static void bridges_and_locality() v.distopclasses = {1978}; } RptGraphEdge hop = {}; - hop.destination = 2; + hop.other_rtindex = 2; hop.left_keys = {{1, 1}}; hop.right_keys = {{2, 1}}; CHECK(rpt_local_hop(&mgr, 1, hop)); @@ -512,13 +518,13 @@ static void local_star_and_backward() { ++forward; CHECK(hop.local); /* Includes synthetic 4->3 and 3->2. */ - CHECK(mgr.graph.at(hop.destination).forward_stage.in[0].local); + CHECK(mgr.graph.at(hop.other_rtindex).forward_stage.in[0].local); } for (const auto &hop : node.second.backward_stage.out) { ++backward; CHECK(hop.local); - CHECK(mgr.graph.at(hop.destination).backward_stage.in[0].local); + CHECK(mgr.graph.at(hop.other_rtindex).backward_stage.in[0].local); } } CHECK(forward == 3 && backward == 3); @@ -526,18 +532,46 @@ static void local_star_and_backward() StringInfoData dump = {}; dump.data = buffer; dump.maxlen = sizeof(buffer); - RptDumpTransferPlan(&mgr, &dump); + PlannedStmt stmt = {}; + stmt.type = T_PlannedStmt; + RangeTblEntry entries[4] = {}; + Alias aliases[4] = {}; + ListCell cells[4] = {}; + List rtable = {}; + rtable.type = T_List; + rtable.length = rtable.max_length = 4; + rtable.elements = cells; + stmt.rtable = &rtable; + char employee[] = "employee", manager[] = "manager", other[] = "other"; + for (int i = 0; i < 4; ++i) + { + entries[i].type = T_RangeTblEntry; + entries[i].relid = 99; /* Same relation, distinct query references. */ + entries[i].eref = &aliases[i]; + aliases[i].type = T_Alias; + aliases[i].aliasname = i == 0 ? employee : i == 1 ? manager : other; + cells[i].ptr_value = &entries[i]; + } + RptDumpTransferPlan(&stmt, &mgr, &dump); std::string text(buffer); + CHECK(text.find("vertex 1 employee rows=") != std::string::npos); + CHECK(text.find("vertex 2 manager rows=") != std::string::npos); + /* Changing the RTE after graph construction is reflected immediately. */ + aliases[0].aliasname = manager; + dump.len = 0; + buffer[0] = '\0'; + RptDumpTransferPlan(&stmt, &mgr, &dump); + CHECK(std::string(buffer).find("vertex 1 manager rows=") != std::string::npos); CHECK(text.find("merge=global") == std::string::npos); CHECK(text.find("forward hop 3->2 merge=local\n") != std::string::npos); CHECK(text.find("forward hop 4->3 merge=local\n") != std::string::npos); CHECK(text.find("backward hop 1->4 merge=local\n") != std::string::npos); /* A class can mix local and global broadcast recipients. */ - mgr.vertices[mgr.by_rti[4]].numsegments = 2; + mgr.vertices[mgr.vertex_index_by_rtindex[4]].numsegments = 2; rpt_classify_hops(&mgr); for (const auto &hop : mgr.graph[1].backward_stage.out) - CHECK(hop.local == (hop.destination != 4)); + CHECK(hop.local == (hop.other_rtindex != 4)); /* Bypassing an unfiltered table preserves policy-local transfer too. */ RptTransferMgr bypass; @@ -552,7 +586,7 @@ static void local_star_and_backward() rpt_skip_unfiltered(&bypass); const auto &e = bypass.neighbor_matrix.at(1).at(2); RptGraphEdge hop = {}; - hop.destination = 2; + hop.other_rtindex = 2; hop.left_keys = {e.clauses[0].first}; hop.right_keys = {e.clauses[0].second}; CHECK(rpt_local_hop(&bypass, 1, hop)); @@ -566,7 +600,7 @@ static void replicated_and_compound_policies() for (auto &v : mgr.vertices) hash_policy(v); RptGraphEdge hop = {}; - hop.destination = 2; + hop.other_rtindex = 2; hop.left_keys = {{1, 1}}; hop.right_keys = {{2, 1}}; /* No neighbor-matrix edge is needed to prove policy locality. */ @@ -682,6 +716,10 @@ static void join_protection() int preserved = type == JOIN_LEFT ? 1 : 2; CHECK(mgr.graph[preserved].forward_stage.in.empty()); CHECK(mgr.graph[preserved].backward_stage.in.empty()); + RptTransferMgr runtime; + RptBuildForwardPlan(&mgr, &runtime); + CHECK(runtime.graph[preserved].forward_stage.in.empty()); + CHECK(runtime.graph[preserved].backward_stage.in.empty()); } } } @@ -689,26 +727,26 @@ static void join_protection() static void directed_parent_choice() { RptTransferMgr mgr; - for (int id = 1; id <= 3; ++id) - vertex(mgr, id, 100 / id, true); + for (int rtindex = 1; rtindex <= 3; ++rtindex) + vertex(mgr, rtindex, 100 / rtindex, true); std::set constructed = {1, 2}; std::set remaining = {3}; std::map forward_depth = {{1, 0}, {2, 3}}; std::map backward_depth = forward_depth; - int forward_from, backward_from, to; + int forward_parent_rtindex, backward_parent_rtindex, target_rtindex; connect(mgr, edge(1, 3)); connect(mgr, edge(2, 3, true)); /* deepest parent blocks 3 -> 2 */ CHECK(rpt_find_edge(&mgr, constructed, remaining, forward_depth, backward_depth, - &forward_from, &backward_from, &to)); - CHECK(to == 3 && forward_from == 1 && backward_from == 1); + &forward_parent_rtindex, &backward_parent_rtindex, &target_rtindex)); + CHECK(target_rtindex == 3 && forward_parent_rtindex == 1 && backward_parent_rtindex == 1); mgr.neighbor_matrix.clear(); connect(mgr, edge(1, 3, false, true)); /* shallowest blocks 1 -> 3 */ connect(mgr, edge(2, 3)); CHECK(rpt_find_edge(&mgr, constructed, remaining, forward_depth, backward_depth, - &forward_from, &backward_from, &to)); - CHECK(forward_from == 2 && backward_from == 2); + &forward_parent_rtindex, &backward_parent_rtindex, &target_rtindex)); + CHECK(forward_parent_rtindex == 2 && backward_parent_rtindex == 2); mgr.neighbor_matrix.clear(); RptEdgeInfo heavy = edge(1, 3, true); @@ -716,22 +754,22 @@ static void directed_parent_choice() connect(mgr, heavy); connect(mgr, edge(2, 3)); CHECK(rpt_find_edge(&mgr, constructed, remaining, forward_depth, backward_depth, - &forward_from, &backward_from, &to)); - CHECK(forward_from == 2 && backward_from == 1); /* lower-weight open edge */ + &forward_parent_rtindex, &backward_parent_rtindex, &target_rtindex)); + CHECK(forward_parent_rtindex == 2 && backward_parent_rtindex == 1); /* lower-weight open edge */ mgr.neighbor_matrix.clear(); connect(mgr, edge(1, 3, true)); connect(mgr, edge(2, 3, true)); CHECK(rpt_find_edge(&mgr, constructed, remaining, forward_depth, backward_depth, - &forward_from, &backward_from, &to)); - CHECK(forward_from == -1 && backward_from == 1); + &forward_parent_rtindex, &backward_parent_rtindex, &target_rtindex)); + CHECK(forward_parent_rtindex == -1 && backward_parent_rtindex == 1); mgr.neighbor_matrix.clear(); connect(mgr, edge(1, 3, false, true)); connect(mgr, edge(2, 3, false, true)); CHECK(rpt_find_edge(&mgr, constructed, remaining, forward_depth, backward_depth, - &forward_from, &backward_from, &to)); - CHECK(forward_from == 2 && backward_from == -1); + &forward_parent_rtindex, &backward_parent_rtindex, &target_rtindex)); + CHECK(forward_parent_rtindex == 2 && backward_parent_rtindex == -1); } static void forward_root_includes_unfiltered_fact() @@ -744,22 +782,22 @@ static void forward_root_includes_unfiltered_fact() connect(analysis, edge(1, 3)); RptBuildClasses(&analysis); RptBuildTransferPlan(NULL, &analysis); - CHECK(analysis.roots.count(2)); + CHECK(analysis.root_rtindexes.count(2)); RptTransferMgr forward; RptBuildForwardPlan(&analysis, &forward); - CHECK(forward.roots.size() == 1 && forward.roots.count(1)); + CHECK(forward.root_rtindexes.size() == 1 && forward.root_rtindexes.count(1)); CHECK(!forward.graph.at(1).forward_stage.in.empty()); CHECK(forward.graph.at(1).forward_stage.out.empty()); - CHECK(forward.reducer_order.size() == 2); - CHECK(analysis.roots.count(2)); - CHECK(analysis.groups.begin()->second.leader_id == 2); + CHECK(forward.reducer_order_rtindexes.size() == 2); + CHECK(analysis.root_rtindexes.count(2)); + CHECK(analysis.groups.begin()->second.leader_rtindex == 2); CHECK(forward.backward_consumers.empty()); - for (const auto &node : forward.graph) - CHECK(node.second.backward_stage.in.empty() && - node.second.backward_stage.out.empty()); + CHECK(!forward.graph.at(1).backward_stage.out.empty()); + for (const auto &group : forward.groups) + CHECK(group.second.leader_rtindex == 1); /* Rebuilding must discard the previous schedule. */ RptBuildForwardPlan(&analysis, &forward); - CHECK(forward.reducer_order.size() == 2); + CHECK(forward.reducer_order_rtindexes.size() == 2); } static void error_unwinds() @@ -830,33 +868,30 @@ static void forward_selection_star() RptSelectForward(forward, forward_params(-1, 0), {}, size_one_mib, nullptr, &comps); CHECK(comps.size() == 1); const RptForwardComponent &c = comps[0]; - CHECK(c.root == 1 && c.accepted && c.reason == "inject"); - CHECK(!c.chain.empty() && !c.channels.empty()); - CHECK(std::find(c.chain.begin(), c.chain.end(), 1) == c.chain.end()); - std::set ids; + CHECK(c.root_rtindex == 1 && c.accepted && c.reason == "inject"); + CHECK(!c.chain_rtindexes.empty() && !c.channels.empty()); + CHECK(std::find(c.chain_rtindexes.begin(), c.chain_rtindexes.end(), 1) == c.chain_rtindexes.end()); double cost = 0; bool reaches_root = false; for (const RptForwardChannel &ch : c.channels) { /* producers before consumers: a channel's source precedes its target */ - auto from = std::find(c.chain.begin(), c.chain.end(), ch.from); - CHECK(from != c.chain.end()); - if (ch.to == c.root) + auto source_position = std::find(c.chain_rtindexes.begin(), c.chain_rtindexes.end(), ch.source_rtindex); + CHECK(source_position != c.chain_rtindexes.end()); + if (ch.target_rtindex == c.root_rtindex) reaches_root = true; else { - auto to = std::find(c.chain.begin(), c.chain.end(), ch.to); - CHECK(to != c.chain.end() && from < to); + auto target_position = std::find(c.chain_rtindexes.begin(), c.chain_rtindexes.end(), ch.target_rtindex); + CHECK(target_position != c.chain_rtindexes.end() && source_position < target_position); } - CHECK(ids.insert(ch.condition_id).second); - CHECK(ch.condition_key == "rpt_f_" + std::to_string(ch.from) + "_" + std::to_string(ch.to)); - CHECK(ch.from_key.rtindex == ch.from && ch.to_key.rtindex == ch.to); + CHECK(ch.condition_key.empty()); /* assigned by the shared injector */ + CHECK(ch.source_key.rtindex == ch.source_rtindex && ch.target_key.rtindex == ch.target_rtindex); CHECK(ch.est_cost_ms > 10.49 && ch.est_cost_ms < 10.51); /* 3.5 * 3 * 1 MiB */ cost += ch.est_cost_ms; } CHECK(reaches_root); CHECK(c.est_cost_ms > cost - 1e-9 && c.est_cost_ms < cost + 1e-9); - CHECK(*ids.begin() == 0 && *ids.rbegin() == ids.size() - 1); std::string dump; RptDumpForward(comps, &dump); CHECK(dump.find("forward component root=1") == 0); @@ -910,7 +945,7 @@ static RptTransferMgr forward_chain(bool colocated) vertex(analysis, 3, 50, true); for (auto &v : analysis.vertices) hash_policy(v); - analysis.vertices[analysis.by_rti[2]].distkeys = {(AttrNumber) (colocated ? 2 : 3)}; + analysis.vertices[analysis.vertex_index_by_rtindex[2]].distkeys = {(AttrNumber) (colocated ? 2 : 3)}; connect(analysis, edge(1, 2)); connect(analysis, edge(2, 3, false, false, 2, 1)); RptBuildClasses(&analysis); @@ -931,12 +966,12 @@ static bool size_recording(double rows, int64 *elems, int64 *payload, int64 *pla static double ndv_small_dims(const RptVertexInfo &v, AttrNumber) { - return v.id == 1 ? 1000 : 100; + return v.rtindex == 1 ? 1000 : 100; } static double ndv_large_dims(const RptVertexInfo &v, AttrNumber) { - return v.id == 1 ? 100 : 1000; + return v.rtindex == 1 ? 100 : 1000; } static void forward_selection_chain_and_selectivity() @@ -952,10 +987,10 @@ static void forward_selection_chain_and_selectivity() sized_rows.clear(); RptSelectForward(forward, params, est, size_recording, nullptr, &comps); CHECK(comps.size() == 1 && comps[0].accepted); - CHECK(comps[0].chain == std::vector({3, 2})); + CHECK(comps[0].chain_rtindexes == std::vector({3, 2})); CHECK(comps[0].channels.size() == 2); - CHECK(comps[0].channels[0].from == 3 && comps[0].channels[0].to == 2); - CHECK(comps[0].channels[1].from == 2 && comps[0].channels[1].to == 1); + CHECK(comps[0].channels[0].source_rtindex == 3 && comps[0].channels[0].target_rtindex == 2); + CHECK(comps[0].channels[1].source_rtindex == 2 && comps[0].channels[1].target_rtindex == 1); /* 2 keeps 0.1 of itself times 0.5 from 3; the root keeps what 2 keeps */ CHECK(comps[0].est_selectivity > 0.0499 && comps[0].est_selectivity < 0.0501); /* filters are sized for rows after the vertex's own reducer quals */ @@ -1004,8 +1039,8 @@ static void forward_selection_in_process() CHECK(comps[0].accepted && comps[0].channels.size() == 2); const RptForwardChannel &inner = comps[0].channels[0]; const RptForwardChannel &into_root = comps[0].channels[1]; - CHECK(inner.to == 2 && inner.local && inner.in_process && inner.est_cost_ms == 0); - CHECK(into_root.to == 1 && !into_root.local && !into_root.in_process); + CHECK(inner.target_rtindex == 2 && inner.local && inner.in_process && inner.est_cost_ms == 0); + CHECK(into_root.target_rtindex == 1 && !into_root.local && !into_root.in_process); CHECK(comps[0].est_cost_ms > 10.49 && comps[0].est_cost_ms < 10.51); std::string dump; RptDumpForward(comps, &dump); @@ -1024,6 +1059,23 @@ static void forward_selection_in_process() CHECK(!ch.local && !ch.in_process); } +static void forward_delivery_process_counts() +{ + auto schedule = forward_chain(false); + auto params = forward_params(-1, 0); + params.delivery_ms_per_mib = 0.8; + for (const auto &vertex : schedule.vertices) + params.main_consumer_processes[vertex.rtindex] = 6; + std::vector components; + RptSelectForward(schedule, params, {}, size_one_mib, nullptr, &components); + for (const auto &channel : components[0].channels) + { + double expected = 10.5 + 0.8 * + (channel.target_rtindex == components[0].root_rtindex ? 6 : 3); + CHECK(std::abs(channel.est_cost_ms - expected) < 1e-6); + } +} + static void qual_function_allowlist() { CHECK(RptQualFunctionSafe(F_INT4LT)); @@ -1070,8 +1122,93 @@ static void raw_datum_key_types() CHECK(!rpt_equality_clause((Node *) &equality, &out, &in, &keytype)); } +static void backward_selection() +{ + RptTransferMgr schedule; + vertex(schedule, 1, 1000, true); + vertex(schedule, 2, 10000, false); + vertex(schedule, 3, 10000, false); + schedule.root_rtindexes.insert(1); + RptBackwardConsumer decision; + decision.class_key = decision.producer_key = {1, 1}; + decision.consumer_key = {2, 1}; + decision.producer_rtindex = 1; + decision.consumer_rtindex = 2; + decision.producer_slice_index = 1; + decision.consumer_slice_index = 2; + decision.eligible = decision.strict_eligible = true; + decision.estimated_rows_removed = 9000; + decision.reason = "eligible"; + schedule.backward_consumers.push_back(decision); + RptForwardParams params = forward_params(-1, 0); + params.max_selectivity = 0.35; + params.delivery_ms_per_mib = 0.8; + std::vector classes; + std::map widths = {{1, 3}, {2, 6}}; /* parallel consumer */ + auto select = [&](RptFilterSizeFn size_fn = size_one_mib, + const std::vector &forward = {}) { + RptSelectBackward(schedule, forward, params, widths, size_fn, &classes); + }; + select(); + CHECK(classes.size() == 1 && classes[0].accepted); + CHECK(classes[0].consumers[0].accepted); + CHECK(classes[0].est_cost_ms > 15.29 && classes[0].est_cost_ms < 15.31); + CHECK(classes[0].estimated_producer_rows == 1000); + params.max_exchange_ms = 15; + select(); + CHECK(!classes[0].accepted && classes[0].reason.find("exchange cost") != std::string::npos); + CHECK(!classes[0].consumers[0].accepted); + params.max_exchange_ms = -1; + schedule.backward_consumers[0].estimated_rows_removed = 0; + select(); + CHECK(!classes[0].accepted); + CHECK(classes[0].consumers[0].reason.find("member selectivity") != std::string::npos); + RptForwardComponent forward; + forward.root_rtindex = 1; forward.accepted = true; forward.est_selectivity = 0.1; + select(size_one_mib, {forward}); + CHECK(classes[0].accepted && classes[0].estimated_producer_rows == 100); + CHECK(classes[0].consumers[0].estimated_rows_removed == 9000); + select([](double rows, int64 *elems, int64 *payload, int64 *bytes) { + *elems = rows; *payload = *bytes = rows * 2; return true; + }, {forward}); + CHECK(classes[0].total_elems == 100 && classes[0].planned_bytes == 200); + forward.accepted = false; + select(size_one_mib, {forward}); + CHECK(!classes[0].accepted && classes[0].estimated_producer_rows == 1000); + schedule.backward_consumers[0].estimated_rows_removed = -1; + select(); + CHECK(!classes[0].accepted); /* unknown NDV */ + params.max_selectivity = 1; + select(); + CHECK(classes[0].accepted); + params.max_exchange_ms = 0; + select(); CHECK(!classes[0].accepted); /* unknown NDV still obeys budget */ + params.max_exchange_ms = -1; + select([](double, int64 *, int64 *, int64 *) { return false; }); + CHECK(!classes[0].accepted && classes[0].reason == "backward filter sizing declined"); + widths[1] = 0; + select(); CHECK(!classes[0].accepted); + widths[1] = 3; + widths[2] = 0; + select(); CHECK(!classes[0].accepted); + widths[2] = 3; + schedule.root_rtindexes.clear(); + select(); CHECK(classes[0].reason == "bridge-led class is deferred"); + schedule.root_rtindexes.insert(1); + /* A refused first member must not hide a later member's producer width. */ + auto refused = schedule.backward_consumers[0]; + refused.eligible = false; refused.reason = "backward consumer cap exceeded"; + refused.producer_slice_index = -1; + schedule.backward_consumers.insert(schedule.backward_consumers.begin(), refused); + select(); + CHECK(classes[0].accepted && !classes[0].consumers[0].accepted); + CHECK(classes[0].consumers[1].accepted); +} + int main() { + forward_delivery_process_counts(); + backward_selection(); raw_datum_key_types(); qual_function_allowlist(); forward_selection_chain_and_selectivity(); diff --git a/gpcontrib/robust_predicate_transfer/test/inject_test.cpp b/gpcontrib/robust_predicate_transfer/test/inject_test.cpp index faef37d52b0..3c200129d53 100644 --- a/gpcontrib/robust_predicate_transfer/test/inject_test.cpp +++ b/gpcontrib/robust_predicate_transfer/test/inject_test.cpp @@ -27,6 +27,9 @@ *------------------------------------------------------------------------- */ #include "../src/rptinject.cpp" +#include "../src/rptforward.cpp" +#include "../src/rptbackwardplan.cpp" +#include "../src/rptbackward.cpp" #include #include #include @@ -36,6 +39,17 @@ #define CHECK(c) do { if (!(c)) { fprintf(stderr, "check failed: %s:%d: %s\n", __FILE__, __LINE__, #c); abort(); } } while (0) static std::vector allocations; +struct BuiltFilter +{ + bool producer; + Plan *child; + AttrNumber attno; + uint32 condition_id; + int processes; + bool defer_first; +}; +static std::vector built_filters; + extern "C" { sigjmp_buf *PG_exception_stack = NULL; ErrorContextCallback *error_context_stack = NULL; @@ -80,34 +94,54 @@ Node *makeBoolConst(bool value, bool isnull) Const *c = makeNode(Const); c->consttype = BOOLOID; c->constvalue = BoolGetDatum(value); c->constisnull = isnull; return (Node *) c; } -static int max_id(Plan *p) +static int max_plan_node_id(Plan *p) { if (!p) return 0; - int m = Max(p->plan_node_id, Max(max_id(p->lefttree), max_id(p->righttree))); + int m = Max(p->plan_node_id, Max(max_plan_node_id(p->lefttree), max_plan_node_id(p->righttree))); if (IsA(p, Sequence)) { ListCell *lc; - foreach(lc, ((Sequence *) p)->subplans) m = Max(m, max_id((Plan *) lfirst(lc))); + foreach(lc, ((Sequence *) p)->subplans) m = Max(m, max_plan_node_id((Plan *) lfirst(lc))); } return m; } +int AnserSliceProducers(PlannedStmt *, int slice_index) +{ + return slice_index >= 0 ? 3 : 0; +} +CustomScan *AnserBuildBloomProducerScan(Plan *child, AttrNumber attno, uint32 condition_id, + const char *, int64, Size, int64, int processes) +{ + built_filters.push_back({true, child, attno, condition_id, processes, false}); + CustomScan *cs = makeNode(CustomScan); + cs->custom_plans = list_make1(child); + return cs; +} +CustomScan *AnserBuildBloomConsumerScan(Plan *child, AttrNumber attno, uint32 condition_id, + const char *, int64, Size, int64, int processes, bool defer_first) +{ + built_filters.push_back({false, child, attno, condition_id, processes, defer_first}); + CustomScan *cs = makeNode(CustomScan); + cs->custom_plans = list_make1(child); + return cs; +} int AnserMaxPlanNodeId(PlannedStmt *s) { - int m = max_id(s->planTree); ListCell *lc; - foreach(lc, s->subplans) m = Max(m, max_id((Plan *) lfirst(lc))); + int m = max_plan_node_id(s->planTree); ListCell *lc; + foreach(lc, s->subplans) m = Max(m, max_plan_node_id((Plan *) lfirst(lc))); return m; } } -static void collect_ids(Plan *p, std::set &ids) +static void collect_plan_node_ids(Plan *p, std::set &plan_node_ids) { if (!p) return; - CHECK(ids.insert(p->plan_node_id).second); - collect_ids(p->lefttree, ids); collect_ids(p->righttree, ids); + CHECK(plan_node_ids.insert(p->plan_node_id).second); + collect_plan_node_ids(p->lefttree, plan_node_ids); collect_plan_node_ids(p->righttree, plan_node_ids); if (IsA(p, Sequence)) { ListCell *lc; - foreach(lc, ((Sequence *) p)->subplans) collect_ids((Plan *) lfirst(lc), ids); + foreach(lc, ((Sequence *) p)->subplans) collect_plan_node_ids((Plan *) lfirst(lc), plan_node_ids); } } @@ -174,16 +208,141 @@ static void layout(bool existing, bool noop) Const *qual = (Const *) linitial(quals); CHECK(qual->consttype == BOOLOID && !qual->constisnull); CHECK(!DatumGetBool(qual->constvalue)); - std::set ids; - collect_ids(stmt.planTree, ids); - ListCell *lc; foreach(lc, stmt.subplans) collect_ids((Plan *) lfirst(lc), ids); - CHECK(ids.size() == (existing ? 6U : 5U) - (noop ? 1U : 0U)); + std::set plan_node_ids; + collect_plan_node_ids(stmt.planTree, plan_node_ids); + ListCell *lc; foreach(lc, stmt.subplans) collect_plan_node_ids((Plan *) lfirst(lc), plan_node_ids); + CHECK(plan_node_ids.size() == (existing ? 6U : 5U) - (noop ? 1U : 0U)); int previous_max = noop ? (existing ? 90 : 5) : 110; CHECK(empty->plan.plan_node_id > previous_max && seq->plan.plan_node_id > previous_max && motion->plan.plan_node_id > previous_max); } +static void backward_stacks(bool refused, bool materialize, bool with_forward) +{ + RptTransferMgr schedule; + SeqScan scans[2] = {}; + TargetEntry entries[2] = {}; + Var keys[2] = {}; + Hash hash = {}; hash.plan.type = T_Hash; + Motion motion = {}; motion.plan.type = T_Motion; + HashJoin join = {}; join.join.plan.type = T_HashJoin; + join.join.plan.righttree = &hash.plan; + join.join.plan.lefttree = &motion.plan; + PlannedStmt stmt = {}; stmt.type = T_PlannedStmt; + stmt.planTree = &join.join.plan; + PlanSlice slices[3] = {}; stmt.slices = slices; stmt.numSlices = 3; + for (int i = 0; i < 2; ++i) + { + scans[i].scan.plan.type = T_SeqScan; + scans[i].scan.scanrelid = i + 1; + keys[i].xpr.type = T_Var; + keys[i].varno = i + 1; keys[i].varattno = 2; + entries[i].xpr.type = T_TargetEntry; entries[i].expr = (Expr *) &keys[i]; + entries[i].resno = 1; /* physical attno differs from output position */ + scans[i].scan.plan.targetlist = list_make1(&entries[i]); + RptVertexInfo vertex = {}; + vertex.rtindex = i + 1; vertex.scan = &scans[i].scan.plan; + vertex.cardinality = i == 0 ? 1000 : 10000; + schedule.vertex_index_by_rtindex[i + 1] = i; + schedule.vertices.push_back(vertex); + } + hash.plan.lefttree = &scans[0].scan.plan; + motion.plan.lefttree = &scans[1].scan.plan; + schedule.root_rtindexes.insert(1); + RptBackwardConsumer decision; + decision.class_key = decision.producer_key = {1, 2}; + decision.consumer_key = {2, 2}; + decision.producer_rtindex = 1; decision.consumer_rtindex = 2; + decision.producer_parent = &hash.plan; decision.consumer_parent = &motion.plan; + decision.producer_attno = decision.consumer_attno = 1; + decision.producer_slice_index = 1; decision.consumer_slice_index = 2; + decision.eligible = true; decision.defer_first = true; + decision.estimated_rows_removed = 9000; + schedule.backward_consumers.push_back(decision); + /* A second class on the same scans exercises repeated stacking/relinking. */ + decision.class_key.attno = 3; + schedule.backward_consumers.push_back(decision); + RptInjectionState state; + state.parents[&scans[0].scan.plan] = &hash.plan; + state.parents[&scans[1].scan.plan] = &motion.plan; + for (auto &scan : scans) state.tops[&scan.scan.plan] = &scan.scan.plan; + state.next_plan_node_id = 100; + std::vector forward_components(1); + forward_components[0].accepted = true; + for (int i = 0; i < 7; ++i) + { + RptForwardChannel channel; + channel.source_rtindex = i + 1; channel.target_rtindex = i + 2; + forward_components[0].channels.push_back(channel); + } + rpt_assign_forward_channels(forward_components, state); + CHECK(state.next_condition_id == 7); + for (int i = 0; i < 7; ++i) + CHECK(forward_components[0].channels[i].condition_id == uint32(i)); + CustomScan forward[2] = {}; + if (with_forward) + for (int i = 0; i < 2; ++i) + { + forward[i].scan.plan.type = T_CustomScan; + forward[i].custom_plans = list_make1(&scans[i]); + RptRelinkStack(&state, &scans[i].scan.plan, &forward[i].scan.plan); + } + if (refused) + schedule.backward_consumers[0].consumer_attno = + schedule.backward_consumers[1].consumer_attno = 99; + Hash hash_before = hash; Motion motion_before = motion; + auto tops_before = state.tops; + PlanSlice slices_before[3]; memcpy(slices_before, slices, sizeof(slices)); + size_t allocations_before = allocations.size(); + built_filters.clear(); + RptForwardParams params; params.max_exchange_ms = -1; + std::string trace; + bool injected = false; + RptInjectBackward(&stmt, schedule, {}, params, + [](double, int64 *elems, int64 *payload, int64 *bytes) { + *elems = 1000; *payload = *bytes = 1024; return true; + }, materialize, &state, &trace, &injected); + CHECK(stmt.slices == slices && stmt.numSlices == 3); + CHECK(memcmp(slices_before, slices, sizeof(slices)) == 0); + CHECK(stmt.subplans == NIL && stmt.paramExecTypes == NIL); + if (refused || !materialize) + { + CHECK(!injected && built_filters.empty()); + CHECK(memcmp(&hash_before, &hash, sizeof(hash)) == 0); + CHECK(memcmp(&motion_before, &motion, sizeof(motion)) == 0); + CHECK(state.tops == tops_before && allocations.size() == allocations_before); + CHECK(state.next_plan_node_id == 100); + CHECK(trace.find(refused ? "placement changed" : "decision=inject") != std::string::npos); + } + else + { + CHECK(injected && built_filters.size() == 4); + CHECK(state.next_condition_id == 9 && state.next_plan_node_id == 104); + for (int i = 0; i < 2; ++i) + { + CHECK(built_filters[2*i].producer && !built_filters[2*i+1].producer); + CHECK(built_filters[2*i].condition_id == uint32(7+i)); + CHECK(built_filters[2*i+1].condition_id == uint32(7+i)); + CHECK(built_filters[2*i].processes == 3 && built_filters[2*i+1].defer_first); + CHECK(built_filters[2*i].attno == 1 && built_filters[2*i+1].attno == 1); + } + Plan *producer = hash.plan.lefttree, *consumer = motion.plan.lefttree; + CHECK(producer == state.tops.at(&scans[0].scan.plan)); + CHECK(consumer == state.tops.at(&scans[1].scan.plan)); + CHECK(producer->plan_node_id == 102 && consumer->plan_node_id == 103); + producer = (Plan *) linitial(((CustomScan *) producer)->custom_plans); + consumer = (Plan *) linitial(((CustomScan *) consumer)->custom_plans); + CHECK(producer->plan_node_id == 100 && consumer->plan_node_id == 101); + CHECK(linitial(((CustomScan *) producer)->custom_plans) == tops_before.at(&scans[0].scan.plan)); + CHECK(linitial(((CustomScan *) consumer)->custom_plans) == tops_before.at(&scans[1].scan.plan)); + } +} + int main() { + backward_stacks(false, true, true); + backward_stacks(false, true, false); + backward_stacks(true, true, true); + backward_stacks(false, false, true); layout(false, false); layout(true, false); layout(false, true); layout(true, true); for (void *p : allocations) free(p); diff --git a/gpcontrib/robust_predicate_transfer/test/survey_test.py b/gpcontrib/robust_predicate_transfer/test/survey_test.py index dbf71b3d51b..5979cf7011e 100644 --- a/gpcontrib/robust_predicate_transfer/test/survey_test.py +++ b/gpcontrib/robust_predicate_transfer/test/survey_test.py @@ -55,6 +55,19 @@ def test_comparison_counts_channels_not_both_endpoints(self): self.assertEqual(metrics["anser_optimizer_used"], "GPORCA") self.assertEqual(metrics["known_rows_removed"], 250) + def test_largest_root_runtime_metrics_are_separate(self): + dump = ("survey classes=1 root_classes=1 global_edges=1 rule9=1 strict=1 eligible=1 " + "known_rows_removed=90 unknown_estimates=0\n" + "runtime survey classes=2 root_classes=1 strict=2 eligible=1\n" + "backward consumer 2 root=1 rep=1.1 decision=inject\n" + "backward consumer 3 root=1 rep=1.1 decision=backward consumer cap exceeded\n") + result = survey.parse_dump(dump) + self.assertEqual(result["classes"], 1) + self.assertEqual(result["runtime_classes"], 2) + self.assertEqual(result["runtime_strict"], 2) + self.assertEqual(result["runtime_injected_consumers"], 1) + self.assertEqual(result["runtime_refusals"], {"backward consumer cap exceeded": 1}) + def test_incomplete_output_cannot_be_reported_as_zero(self): with self.assertRaises(ValueError): survey.parse_output(survey.BEGIN_DUMP + "\nno transfer regions\n") @@ -66,7 +79,7 @@ def test_queries_are_planned_in_read_only_transaction(self): sql = survey.survey_sql("SELECT 'O''Brien' -- trailing comment", "on", 1000, "imdb,public") self.assertIn("BEGIN READ ONLY;", sql) self.assertIn("SET LOCAL rpt.enable = off;", sql) - self.assertIn("SET LOCAL rpt.backward = off;", sql) + self.assertIn("SET LOCAL rpt.backward = on;", sql) self.assertIn("SET LOCAL anser.runtime_filter = on;", sql) self.assertIn("EXPLAIN (FORMAT JSON) SELECT 'O''Brien' -- trailing comment\n;", sql) self.assertNotIn("EXPLAIN ANALYZE", sql) From eb8fd7b32c8afb9c1516d15134e51d55470f46ad Mon Sep 17 00:00:00 2001 From: Leonid Borchuk Date: Fri, 13 Nov 2026 13:03:33 +0300 Subject: [PATCH 25/27] Fix RPT installcheck test-extension dependency Install anser_test through Anser's install-anser-test target before running RPT regressions. Normal Anser installation omits this extension, but backward fault tests require its delivery counter. The prerequisite also covers installcheck-with-install through its installcheck call. Validated the recursive installation target with make -n and checked whitespace with git diff --check. Cluster regressions were not run. Assisted-by: ChatGPT --- gpcontrib/robust_predicate_transfer/Makefile | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/gpcontrib/robust_predicate_transfer/Makefile b/gpcontrib/robust_predicate_transfer/Makefile index 1fa6a2c20ed..bb49f63721d 100644 --- a/gpcontrib/robust_predicate_transfer/Makefile +++ b/gpcontrib/robust_predicate_transfer/Makefile @@ -73,7 +73,12 @@ COMPILER = $(CXX) $(CXXFLAGS) # resolves AnserIsRuntimeFilterScan from it at load time (RTLD_NOW), and # load order must be anser first. Mirrors gpcontrib/anser's preload # machinery. -installcheck: preload-rpt +# Backward fault tests use anser_test, which Anser's normal install omits. +installcheck: install-anser-test preload-rpt + +.PHONY: install-anser-test +install-anser-test: + $(MAKE) -C ../anser install-anser-test .PHONY: preload-rpt preload-rpt: From 532164fce44529bd0f863ecfc2ff55537601d2a5 Mon Sep 17 00:00:00 2001 From: Leonid Borchuk Date: Fri, 13 Nov 2026 15:47:38 +0300 Subject: [PATCH 26/27] Fix RPT graph pruning and regression fixtures Keep the logical join graph before SkipUnfilteredTable rewrites it for forward transfers. Grow one forest over both views and choose each stage's parent from its own edges, so unfiltered roots retain backward consumers without re-enabling prohibited outer-join directions. Use Anser's resolved targetlist helper for local filter stacks and add native coverage for deep stacks, reordered outputs and unfiltered backward edges. Exercise stacked consumers and producer/consumer stacks with EXPLAIN VERBOSE in the SQL regressions. Stop treating InitPlan presence alone as evidence of external producer parameters. Make backward fixtures use incompressible padding and the hash-size heuristic, and verify a cached injected plan before arming the cancellation timeout. The reported ORCA cancellation cause remains unconfirmed pending a new cluster run. Validation: compiler checks; native tests with -DNDEBUG; graph and injection tests with ASan/UBSan; 11 survey and 6 diagnostic tests; SQL/expected consistency and git diff --check. Cluster SQL regressions remain pending. Assisted-by: ChatGPT --- gpcontrib/robust_predicate_transfer/Makefile | 2 +- gpcontrib/robust_predicate_transfer/README.md | 3 +- .../expected/rpt_backward.out | 16 ++- .../expected/rpt_backward_eligibility.out | 16 +-- .../expected/rpt_backward_faults.out | 34 +++++- .../expected/rpt_forward.out | 10 +- .../robust_predicate_transfer/include/rpt.h | 4 +- .../scripts/diagnose_rpt.py | 6 +- .../sql/rpt_backward.sql | 16 ++- .../sql/rpt_backward_eligibility.sql | 16 +-- .../sql/rpt_backward_faults.sql | 34 +++++- .../sql/rpt_forward.sql | 10 +- .../src/rptgraph.cpp | 1 + .../robust_predicate_transfer/src/rptlocal.c | 4 +- .../robust_predicate_transfer/src/rptplan.cpp | 107 +++++++++--------- .../test/graph_test.cpp | 22 ++++ .../test/inject_test.cpp | 86 ++++++++++++++ 17 files changed, 299 insertions(+), 88 deletions(-) diff --git a/gpcontrib/robust_predicate_transfer/Makefile b/gpcontrib/robust_predicate_transfer/Makefile index bb49f63721d..c059b90b47f 100644 --- a/gpcontrib/robust_predicate_transfer/Makefile +++ b/gpcontrib/robust_predicate_transfer/Makefile @@ -124,7 +124,7 @@ unitcheck: test/graph_test test/inject_test test/forward_walk_test test/graph_test: $(srcdir)/test/graph_test.cpp $(wildcard $(srcdir)/src/*.cpp) $(srcdir)/include/rpt.h $(srcdir)/include/rptqual.h $(CXX) $(CXXFLAGS) $(CPPFLAGS) $(PG_CPPFLAGS) $< -o $@ -test/inject_test: $(srcdir)/test/inject_test.cpp $(wildcard $(srcdir)/src/*.cpp) $(srcdir)/include/rpt.h $(srcdir)/include/rptinject.h +test/inject_test: $(srcdir)/test/inject_test.cpp $(wildcard $(srcdir)/src/*.cpp) $(srcdir)/include/rpt.h $(srcdir)/include/rptinject.h $(srcdir)/../anser/include/ansertlist.h $(CXX) $(CXXFLAGS) $(CPPFLAGS) $(PG_CPPFLAGS) -ffunction-sections -fdata-sections \ $(WALK_TEST_LDFLAGS) $< -o $@ diff --git a/gpcontrib/robust_predicate_transfer/README.md b/gpcontrib/robust_predicate_transfer/README.md index 003da4300d2..c369ac4d58c 100644 --- a/gpcontrib/robust_predicate_transfer/README.md +++ b/gpcontrib/robust_predicate_transfer/README.md @@ -144,7 +144,8 @@ The structures are declared in [rpt.h](include/rpt.h): | `RptInjectionState` | Shared channel/node allocators and original-parent/current-top maps, declared in [rptinject.h](include/rptinject.h) | `RptTransferMgr.vertex_index_by_rtindex` maps a range-table index to a -position in `vertices`. `neighbor_matrix` maps relation pairs to edges; +position in `vertices`. `neighbor_matrix` holds the forward graph after skip rules; +`logical_neighbor_matrix` retains the logical edges for backward scheduling; `uf_parent` represents the key union-find; `groups` contains the resulting classes. `graph` maps each range-table index to its transfer node. `root_rtindexes`, `bridge_rtindexes`, `reducer_order_rtindexes` and diff --git a/gpcontrib/robust_predicate_transfer/expected/rpt_backward.out b/gpcontrib/robust_predicate_transfer/expected/rpt_backward.out index 3dee383a2e7..9371e8b8a31 100644 --- a/gpcontrib/robust_predicate_transfer/expected/rpt_backward.out +++ b/gpcontrib/robust_predicate_transfer/expected/rpt_backward.out @@ -27,6 +27,7 @@ SET statement_timeout = '120s'; SET work_mem = '1MB'; SET enable_nestloop = off; SET enable_mergejoin = off; +SET gp_enable_hashjoin_size_heuristic = on; SET join_collapse_limit = 1; SET optimizer_join_order = query; CREATE TABLE rpt_bw_root (k int, s int) DISTRIBUTED BY (k); @@ -43,7 +44,9 @@ CREATE TABLE rpt_bw_part (k int, pad text) DISTRIBUTED RANDOMLY -- root restriction that would change the largest-root choice. INSERT INTO rpt_bw_root SELECT CASE WHEN i % 101 = 0 THEN NULL ELSE i % 10 END, i % 100 FROM generate_series(1, 10000) i; -INSERT INTO rpt_bw_member SELECT i, repeat(md5(i::text), 100) +-- Incompressible padding keeps the member's hash footprint genuinely wide. +INSERT INTO rpt_bw_member SELECT i, + (SELECT string_agg(md5(i::text || ':' || j::text), '') FROM generate_series(1, 100) j) FROM generate_series(0, 999) i; INSERT INTO rpt_bw_dim SELECT i, i FROM generate_series(0, 99) i; INSERT INTO rpt_bw_bridge SELECT i, i % 100 FROM generate_series(0, 999) i; @@ -77,7 +80,7 @@ CREATE FUNCTION pg_temp.rpt_bw_plan(q text) RETURNS jsonb LANGUAGE plpgsql AS $$ DECLARE plan jsonb; BEGIN - EXECUTE 'EXPLAIN (ANALYZE, FORMAT JSON) ' || q INTO plan; + EXECUTE 'EXPLAIN (ANALYZE, VERBOSE, FORMAT JSON) ' || q INTO plan; RETURN plan; END $$; @@ -129,6 +132,14 @@ BEGIN PERFORM set_config('rpt.forward_apply', 'root', true); PERFORM set_config('rpt.forward', 'off', true); PERFORM pg_temp.rpt_bw_expect(plain, 'm', 'inject'); + -- Forward skipping must not suppress an unfiltered root's broadcast. + -- Redundant safe predicates must not be required to recover its edges. + q := plain || ' WHERE m.k >= 0 AND r.s >= 0'; + PERFORM pg_temp.rpt_bw_expect(q, 'm', 'inject'); + IF pg_temp.rpt_bw_result(plain, true, true) IS DISTINCT FROM + pg_temp.rpt_bw_result(q, true, true) THEN + RAISE EXCEPTION 'redundant qualifiers changed backward results'; + END IF; plan := pg_temp.rpt_bw_plan(plain); IF NOT EXISTS (SELECT FROM pg_temp.rpt_bw_nodes(plan) n WHERE n->>'Custom Plan Provider' = 'Anser Bloom Producer') OR @@ -271,6 +282,7 @@ RESET anser.timeout_ms; RESET statement_timeout; RESET enable_nestloop; RESET enable_mergejoin; +RESET gp_enable_hashjoin_size_heuristic; RESET work_mem; RESET join_collapse_limit; RESET optimizer_join_order; diff --git a/gpcontrib/robust_predicate_transfer/expected/rpt_backward_eligibility.out b/gpcontrib/robust_predicate_transfer/expected/rpt_backward_eligibility.out index 292641d3eef..b904b96cdad 100644 --- a/gpcontrib/robust_predicate_transfer/expected/rpt_backward_eligibility.out +++ b/gpcontrib/robust_predicate_transfer/expected/rpt_backward_eligibility.out @@ -172,15 +172,15 @@ BEGIN PERFORM pg_temp.rpt_expect(q, 'rpt_elig_root', 'rpt_elig_member', ARRAY['eligible'], true); END IF; END IF; - -- The aggregate scalar subquery commonly becomes an InitPlan in the - -- PostgreSQL planner. ORCA may instead use a join, without extParam. + -- An InitPlan is evaluated once, and its presence alone does not + -- imply a nonempty extParam on the qualifying hash build. Depending + -- on planner placement it can be internal to that build or external. + -- The native test checks an explicit extParam bitmap; the correlated + -- SubPlan below exercises the rescan refusal on a real SQL plan. q := 'SELECT * FROM rpt_elig_root JOIN rpt_elig_member USING (k) WHERE rpt_elig_root.v < (SELECT max(k) FROM rpt_elig_outer)'; - IF pg_temp.rpt_explain(q)::text LIKE '%InitPlan%' THEN - PERFORM pg_temp.rpt_expect(q, 'rpt_elig_root', 'rpt_elig_member', ARRAY['parameterized producer can be rescanned'], false); - ELSE - PERFORM pg_temp.rpt_expect(q, 'rpt_elig_root', 'rpt_elig_member', - ARRAY['eligible', 'unsupported producer key path', 'producer is not on a qualifying hash build side']); - END IF; + PERFORM pg_temp.rpt_expect(q, 'rpt_elig_root', 'rpt_elig_member', + ARRAY['eligible', 'parameterized producer can be rescanned', + 'unsupported producer key path', 'producer is not on a qualifying hash build side']); -- Volatility and OFFSET retain a correlated SubPlan instead of -- allowing a decorrelating join rewrite. Its internal Motion must -- not undo the explicit SubPlan refusal. diff --git a/gpcontrib/robust_predicate_transfer/expected/rpt_backward_faults.out b/gpcontrib/robust_predicate_transfer/expected/rpt_backward_faults.out index 7bd6fd54b59..8a07325c628 100644 --- a/gpcontrib/robust_predicate_transfer/expected/rpt_backward_faults.out +++ b/gpcontrib/robust_predicate_transfer/expected/rpt_backward_faults.out @@ -32,10 +32,13 @@ SET statement_timeout = '120s'; SET work_mem = '1MB'; SET enable_nestloop = off; SET enable_mergejoin = off; +SET gp_enable_hashjoin_size_heuristic = on; CREATE TABLE rpt_bwf_root (k int) DISTRIBUTED BY (k); CREATE TABLE rpt_bwf_member (k int, pad text) DISTRIBUTED RANDOMLY; INSERT INTO rpt_bwf_root SELECT i % 10 FROM generate_series(1, 10000) i; -INSERT INTO rpt_bwf_member SELECT i, repeat(md5(i::text), 100) FROM generate_series(0, 999) i; +INSERT INTO rpt_bwf_member SELECT i, + (SELECT string_agg(md5(i::text || ':' || j::text), '') FROM generate_series(1, 100) j) + FROM generate_series(0, 999) i; ANALYZE rpt_bwf_root; ANALYZE rpt_bwf_member; CREATE FUNCTION pg_temp.rpt_bwf_query() RETURNS text LANGUAGE sql AS $$ @@ -151,17 +154,38 @@ $$; CREATE FUNCTION pg_temp.rpt_bwf_cancel() RETURNS void LANGUAGE plpgsql AS $$ DECLARE cancelled boolean := false; + answer text; BEGIN BEGIN - PERFORM pg_temp.rpt_bwf_run(true); + EXECUTE 'EXECUTE rpt_bwf_cancel_plan' INTO answer; EXCEPTION WHEN query_canceled THEN cancelled := true; END; - IF NOT cancelled THEN RAISE EXCEPTION 'backward wait was not cancelled'; END IF; + IF NOT cancelled THEN + RAISE EXCEPTION 'backward wait was not cancelled (optimizer=%, delivery hits=%)', + current_setting('optimizer'), pg_temp.rpt_bwf_hits('anser_before_deliver', true); + END IF; +END +$$; +-- Prepare and verify each optimizer's plan before starting the short timer. +-- The cancellation check then measures executor/transport cleanup, not planning. +CREATE FUNCTION pg_temp.rpt_bwf_prepare_cancel() RETURNS void LANGUAGE plpgsql AS $$ +DECLARE + plan jsonb; +BEGIN + IF rpt_transfer_plan(pg_temp.rpt_bwf_query()) NOT LIKE '%backward class %decision=inject%' THEN + RAISE EXCEPTION 'cancel fixture has no backward injection (optimizer=%)', current_setting('optimizer'); + END IF; + EXECUTE 'PREPARE rpt_bwf_cancel_plan AS ' || pg_temp.rpt_bwf_query(); + EXECUTE 'EXPLAIN (VERBOSE, FORMAT JSON) EXECUTE rpt_bwf_cancel_plan' INTO plan; + IF plan::text NOT LIKE '%Anser Bloom Producer%' OR plan::text NOT LIKE '%Anser Bloom Consumer%' THEN + RAISE EXCEPTION 'cancel fixture lost its runtime nodes: %', plan; + END IF; END $$; -- Separate top-level commands arm statement_timeout for each optimizer. SET optimizer = off; +DO $$ BEGIN PERFORM pg_temp.rpt_bwf_prepare_cancel(); END $$; DO $$ BEGIN PERFORM pg_temp.rpt_bwf_fault('anser_before_deliver', 'sleep', true); END $$; SET anser.timeout_ms = 600000; SET statement_timeout = '300ms'; @@ -173,12 +197,14 @@ BEGIN RAISE EXCEPTION 'cancel test never reached backward delivery'; END IF; PERFORM pg_temp.rpt_bwf_fault('anser_before_deliver', 'reset', true); + EXECUTE 'DEALLOCATE rpt_bwf_cancel_plan'; IF pg_temp.rpt_bwf_run(true) IS DISTINCT FROM pg_temp.rpt_bwf_run(false) THEN RAISE EXCEPTION 'session failed after backward cancellation'; END IF; END $$; SET optimizer = on; +DO $$ BEGIN PERFORM pg_temp.rpt_bwf_prepare_cancel(); END $$; DO $$ BEGIN PERFORM pg_temp.rpt_bwf_fault('anser_before_deliver', 'sleep', true); END $$; SET statement_timeout = '300ms'; DO $$ BEGIN PERFORM pg_temp.rpt_bwf_cancel(); END $$; @@ -189,6 +215,7 @@ BEGIN RAISE EXCEPTION 'ORCA cancel test never reached backward delivery'; END IF; PERFORM pg_temp.rpt_bwf_fault('anser_before_deliver', 'reset', true); + EXECUTE 'DEALLOCATE rpt_bwf_cancel_plan'; IF pg_temp.rpt_bwf_run(true) IS DISTINCT FROM pg_temp.rpt_bwf_run(false) THEN RAISE EXCEPTION 'ORCA session failed after backward cancellation'; END IF; @@ -206,4 +233,5 @@ RESET optimizer; RESET statement_timeout; RESET enable_nestloop; RESET enable_mergejoin; +RESET gp_enable_hashjoin_size_heuristic; RESET work_mem; diff --git a/gpcontrib/robust_predicate_transfer/expected/rpt_forward.out b/gpcontrib/robust_predicate_transfer/expected/rpt_forward.out index 1743ebaf0e2..6db0b165b76 100644 --- a/gpcontrib/robust_predicate_transfer/expected/rpt_forward.out +++ b/gpcontrib/robust_predicate_transfer/expected/rpt_forward.out @@ -66,7 +66,7 @@ LANGUAGE plpgsql AS $$ DECLARE plan jsonb; BEGIN - EXECUTE format('EXPLAIN (%s FORMAT JSON) %s', + EXECUTE format('EXPLAIN (%s VERBOSE, FORMAT JSON) %s', CASE WHEN do_analyze THEN 'ANALYZE,' ELSE '' END, q) INTO plan; RETURN plan; END @@ -148,6 +148,14 @@ BEGIN RAISE EXCEPTION 'reducer shape missing (optimizer=%): %', opt, plan; END IF; + -- Two root consumers must really be stacked. VERBOSE deparses their + -- INDEX_VAR outputs; copying the child's identity tlist here used to + -- make EXPLAIN loop forever in resolve_special_varno. + IF NOT EXISTS (SELECT FROM pg_temp.rpt_fwd_nodes(plan, 'Anser Bloom Consumer', false) n + WHERE n->'Plans'->0->>'Custom Plan Provider' = 'Anser Bloom Consumer') THEN + RAISE EXCEPTION 'star did not exercise stacked consumers (optimizer=%): %', opt, plan; + END IF; + -- The property the design rests on: filters produced in the InitPlan's -- dispatch are applied by the main plan's dispatch. plan := pg_temp.rpt_fwd_plan(star, true); diff --git a/gpcontrib/robust_predicate_transfer/include/rpt.h b/gpcontrib/robust_predicate_transfer/include/rpt.h index ef51e22af17..c8795644eb2 100644 --- a/gpcontrib/robust_predicate_transfer/include/rpt.h +++ b/gpcontrib/robust_predicate_transfer/include/rpt.h @@ -251,8 +251,10 @@ struct RptTransferMgr std::vector vertices; /* rtindex -> vector index in vertices */ std::map vertex_index_by_rtindex; - /* rtindex -> (rtindex -> edge) */ + /* rtindex -> (rtindex -> edge); rewritten/pruned for forward transfers */ std::map > neighbor_matrix; + /* Logical edges before forward-only SkipUnfilteredTable pruning. */ + std::map > logical_neighbor_matrix; /* rptgraph.cpp: classes and classification. */ std::map groups; /* by class representative key */ diff --git a/gpcontrib/robust_predicate_transfer/scripts/diagnose_rpt.py b/gpcontrib/robust_predicate_transfer/scripts/diagnose_rpt.py index ebe185ccb2a..5d3a1775e45 100644 --- a/gpcontrib/robust_predicate_transfer/scripts/diagnose_rpt.py +++ b/gpcontrib/robust_predicate_transfer/scripts/diagnose_rpt.py @@ -59,13 +59,13 @@ def instrument_sql(source, suite="forward"): "CREATE EXTENSION robust_predicate_transfer;\n" + helper, 1) source = source.replace(" EXECUTE q INTO r;", " RAISE NOTICE 'RPT STEP results SQL=%', q;\n EXECUTE q INTO r;") - source = source.replace(" EXECUTE 'EXPLAIN (ANALYZE, FORMAT JSON) ' || q INTO plan;", + source = source.replace(" EXECUTE 'EXPLAIN (ANALYZE, VERBOSE, FORMAT JSON) ' || q INTO plan;", " RAISE NOTICE 'RPT STEP explain SQL=%', q;\n" - " EXECUTE 'EXPLAIN (ANALYZE, FORMAT JSON) ' || q INTO plan;") + " EXECUTE 'EXPLAIN (ANALYZE, VERBOSE, FORMAT JSON) ' || q INTO plan;") return source source = source.replace(anchor, helper + anchor) edits = { - " EXECUTE format('EXPLAIN (%s FORMAT JSON) %s',": + " EXECUTE format('EXPLAIN (%s VERBOSE, FORMAT JSON) %s',": " RAISE NOTICE 'RPT STEP explain analyze=% SQL=%', do_analyze, q;\n", " EXECUTE format('SELECT string_agg(t::text, %L ORDER BY t::text) FROM (%s) t',": " RAISE NOTICE 'RPT STEP results rpt=% SQL=%', rpt, q;\n", diff --git a/gpcontrib/robust_predicate_transfer/sql/rpt_backward.sql b/gpcontrib/robust_predicate_transfer/sql/rpt_backward.sql index 3dee383a2e7..9371e8b8a31 100644 --- a/gpcontrib/robust_predicate_transfer/sql/rpt_backward.sql +++ b/gpcontrib/robust_predicate_transfer/sql/rpt_backward.sql @@ -27,6 +27,7 @@ SET statement_timeout = '120s'; SET work_mem = '1MB'; SET enable_nestloop = off; SET enable_mergejoin = off; +SET gp_enable_hashjoin_size_heuristic = on; SET join_collapse_limit = 1; SET optimizer_join_order = query; CREATE TABLE rpt_bw_root (k int, s int) DISTRIBUTED BY (k); @@ -43,7 +44,9 @@ CREATE TABLE rpt_bw_part (k int, pad text) DISTRIBUTED RANDOMLY -- root restriction that would change the largest-root choice. INSERT INTO rpt_bw_root SELECT CASE WHEN i % 101 = 0 THEN NULL ELSE i % 10 END, i % 100 FROM generate_series(1, 10000) i; -INSERT INTO rpt_bw_member SELECT i, repeat(md5(i::text), 100) +-- Incompressible padding keeps the member's hash footprint genuinely wide. +INSERT INTO rpt_bw_member SELECT i, + (SELECT string_agg(md5(i::text || ':' || j::text), '') FROM generate_series(1, 100) j) FROM generate_series(0, 999) i; INSERT INTO rpt_bw_dim SELECT i, i FROM generate_series(0, 99) i; INSERT INTO rpt_bw_bridge SELECT i, i % 100 FROM generate_series(0, 999) i; @@ -77,7 +80,7 @@ CREATE FUNCTION pg_temp.rpt_bw_plan(q text) RETURNS jsonb LANGUAGE plpgsql AS $$ DECLARE plan jsonb; BEGIN - EXECUTE 'EXPLAIN (ANALYZE, FORMAT JSON) ' || q INTO plan; + EXECUTE 'EXPLAIN (ANALYZE, VERBOSE, FORMAT JSON) ' || q INTO plan; RETURN plan; END $$; @@ -129,6 +132,14 @@ BEGIN PERFORM set_config('rpt.forward_apply', 'root', true); PERFORM set_config('rpt.forward', 'off', true); PERFORM pg_temp.rpt_bw_expect(plain, 'm', 'inject'); + -- Forward skipping must not suppress an unfiltered root's broadcast. + -- Redundant safe predicates must not be required to recover its edges. + q := plain || ' WHERE m.k >= 0 AND r.s >= 0'; + PERFORM pg_temp.rpt_bw_expect(q, 'm', 'inject'); + IF pg_temp.rpt_bw_result(plain, true, true) IS DISTINCT FROM + pg_temp.rpt_bw_result(q, true, true) THEN + RAISE EXCEPTION 'redundant qualifiers changed backward results'; + END IF; plan := pg_temp.rpt_bw_plan(plain); IF NOT EXISTS (SELECT FROM pg_temp.rpt_bw_nodes(plan) n WHERE n->>'Custom Plan Provider' = 'Anser Bloom Producer') OR @@ -271,6 +282,7 @@ RESET anser.timeout_ms; RESET statement_timeout; RESET enable_nestloop; RESET enable_mergejoin; +RESET gp_enable_hashjoin_size_heuristic; RESET work_mem; RESET join_collapse_limit; RESET optimizer_join_order; diff --git a/gpcontrib/robust_predicate_transfer/sql/rpt_backward_eligibility.sql b/gpcontrib/robust_predicate_transfer/sql/rpt_backward_eligibility.sql index 292641d3eef..b904b96cdad 100644 --- a/gpcontrib/robust_predicate_transfer/sql/rpt_backward_eligibility.sql +++ b/gpcontrib/robust_predicate_transfer/sql/rpt_backward_eligibility.sql @@ -172,15 +172,15 @@ BEGIN PERFORM pg_temp.rpt_expect(q, 'rpt_elig_root', 'rpt_elig_member', ARRAY['eligible'], true); END IF; END IF; - -- The aggregate scalar subquery commonly becomes an InitPlan in the - -- PostgreSQL planner. ORCA may instead use a join, without extParam. + -- An InitPlan is evaluated once, and its presence alone does not + -- imply a nonempty extParam on the qualifying hash build. Depending + -- on planner placement it can be internal to that build or external. + -- The native test checks an explicit extParam bitmap; the correlated + -- SubPlan below exercises the rescan refusal on a real SQL plan. q := 'SELECT * FROM rpt_elig_root JOIN rpt_elig_member USING (k) WHERE rpt_elig_root.v < (SELECT max(k) FROM rpt_elig_outer)'; - IF pg_temp.rpt_explain(q)::text LIKE '%InitPlan%' THEN - PERFORM pg_temp.rpt_expect(q, 'rpt_elig_root', 'rpt_elig_member', ARRAY['parameterized producer can be rescanned'], false); - ELSE - PERFORM pg_temp.rpt_expect(q, 'rpt_elig_root', 'rpt_elig_member', - ARRAY['eligible', 'unsupported producer key path', 'producer is not on a qualifying hash build side']); - END IF; + PERFORM pg_temp.rpt_expect(q, 'rpt_elig_root', 'rpt_elig_member', + ARRAY['eligible', 'parameterized producer can be rescanned', + 'unsupported producer key path', 'producer is not on a qualifying hash build side']); -- Volatility and OFFSET retain a correlated SubPlan instead of -- allowing a decorrelating join rewrite. Its internal Motion must -- not undo the explicit SubPlan refusal. diff --git a/gpcontrib/robust_predicate_transfer/sql/rpt_backward_faults.sql b/gpcontrib/robust_predicate_transfer/sql/rpt_backward_faults.sql index 7bd6fd54b59..8a07325c628 100644 --- a/gpcontrib/robust_predicate_transfer/sql/rpt_backward_faults.sql +++ b/gpcontrib/robust_predicate_transfer/sql/rpt_backward_faults.sql @@ -32,10 +32,13 @@ SET statement_timeout = '120s'; SET work_mem = '1MB'; SET enable_nestloop = off; SET enable_mergejoin = off; +SET gp_enable_hashjoin_size_heuristic = on; CREATE TABLE rpt_bwf_root (k int) DISTRIBUTED BY (k); CREATE TABLE rpt_bwf_member (k int, pad text) DISTRIBUTED RANDOMLY; INSERT INTO rpt_bwf_root SELECT i % 10 FROM generate_series(1, 10000) i; -INSERT INTO rpt_bwf_member SELECT i, repeat(md5(i::text), 100) FROM generate_series(0, 999) i; +INSERT INTO rpt_bwf_member SELECT i, + (SELECT string_agg(md5(i::text || ':' || j::text), '') FROM generate_series(1, 100) j) + FROM generate_series(0, 999) i; ANALYZE rpt_bwf_root; ANALYZE rpt_bwf_member; CREATE FUNCTION pg_temp.rpt_bwf_query() RETURNS text LANGUAGE sql AS $$ @@ -151,17 +154,38 @@ $$; CREATE FUNCTION pg_temp.rpt_bwf_cancel() RETURNS void LANGUAGE plpgsql AS $$ DECLARE cancelled boolean := false; + answer text; BEGIN BEGIN - PERFORM pg_temp.rpt_bwf_run(true); + EXECUTE 'EXECUTE rpt_bwf_cancel_plan' INTO answer; EXCEPTION WHEN query_canceled THEN cancelled := true; END; - IF NOT cancelled THEN RAISE EXCEPTION 'backward wait was not cancelled'; END IF; + IF NOT cancelled THEN + RAISE EXCEPTION 'backward wait was not cancelled (optimizer=%, delivery hits=%)', + current_setting('optimizer'), pg_temp.rpt_bwf_hits('anser_before_deliver', true); + END IF; +END +$$; +-- Prepare and verify each optimizer's plan before starting the short timer. +-- The cancellation check then measures executor/transport cleanup, not planning. +CREATE FUNCTION pg_temp.rpt_bwf_prepare_cancel() RETURNS void LANGUAGE plpgsql AS $$ +DECLARE + plan jsonb; +BEGIN + IF rpt_transfer_plan(pg_temp.rpt_bwf_query()) NOT LIKE '%backward class %decision=inject%' THEN + RAISE EXCEPTION 'cancel fixture has no backward injection (optimizer=%)', current_setting('optimizer'); + END IF; + EXECUTE 'PREPARE rpt_bwf_cancel_plan AS ' || pg_temp.rpt_bwf_query(); + EXECUTE 'EXPLAIN (VERBOSE, FORMAT JSON) EXECUTE rpt_bwf_cancel_plan' INTO plan; + IF plan::text NOT LIKE '%Anser Bloom Producer%' OR plan::text NOT LIKE '%Anser Bloom Consumer%' THEN + RAISE EXCEPTION 'cancel fixture lost its runtime nodes: %', plan; + END IF; END $$; -- Separate top-level commands arm statement_timeout for each optimizer. SET optimizer = off; +DO $$ BEGIN PERFORM pg_temp.rpt_bwf_prepare_cancel(); END $$; DO $$ BEGIN PERFORM pg_temp.rpt_bwf_fault('anser_before_deliver', 'sleep', true); END $$; SET anser.timeout_ms = 600000; SET statement_timeout = '300ms'; @@ -173,12 +197,14 @@ BEGIN RAISE EXCEPTION 'cancel test never reached backward delivery'; END IF; PERFORM pg_temp.rpt_bwf_fault('anser_before_deliver', 'reset', true); + EXECUTE 'DEALLOCATE rpt_bwf_cancel_plan'; IF pg_temp.rpt_bwf_run(true) IS DISTINCT FROM pg_temp.rpt_bwf_run(false) THEN RAISE EXCEPTION 'session failed after backward cancellation'; END IF; END $$; SET optimizer = on; +DO $$ BEGIN PERFORM pg_temp.rpt_bwf_prepare_cancel(); END $$; DO $$ BEGIN PERFORM pg_temp.rpt_bwf_fault('anser_before_deliver', 'sleep', true); END $$; SET statement_timeout = '300ms'; DO $$ BEGIN PERFORM pg_temp.rpt_bwf_cancel(); END $$; @@ -189,6 +215,7 @@ BEGIN RAISE EXCEPTION 'ORCA cancel test never reached backward delivery'; END IF; PERFORM pg_temp.rpt_bwf_fault('anser_before_deliver', 'reset', true); + EXECUTE 'DEALLOCATE rpt_bwf_cancel_plan'; IF pg_temp.rpt_bwf_run(true) IS DISTINCT FROM pg_temp.rpt_bwf_run(false) THEN RAISE EXCEPTION 'ORCA session failed after backward cancellation'; END IF; @@ -206,4 +233,5 @@ RESET optimizer; RESET statement_timeout; RESET enable_nestloop; RESET enable_mergejoin; +RESET gp_enable_hashjoin_size_heuristic; RESET work_mem; diff --git a/gpcontrib/robust_predicate_transfer/sql/rpt_forward.sql b/gpcontrib/robust_predicate_transfer/sql/rpt_forward.sql index 1743ebaf0e2..6db0b165b76 100644 --- a/gpcontrib/robust_predicate_transfer/sql/rpt_forward.sql +++ b/gpcontrib/robust_predicate_transfer/sql/rpt_forward.sql @@ -66,7 +66,7 @@ LANGUAGE plpgsql AS $$ DECLARE plan jsonb; BEGIN - EXECUTE format('EXPLAIN (%s FORMAT JSON) %s', + EXECUTE format('EXPLAIN (%s VERBOSE, FORMAT JSON) %s', CASE WHEN do_analyze THEN 'ANALYZE,' ELSE '' END, q) INTO plan; RETURN plan; END @@ -148,6 +148,14 @@ BEGIN RAISE EXCEPTION 'reducer shape missing (optimizer=%): %', opt, plan; END IF; + -- Two root consumers must really be stacked. VERBOSE deparses their + -- INDEX_VAR outputs; copying the child's identity tlist here used to + -- make EXPLAIN loop forever in resolve_special_varno. + IF NOT EXISTS (SELECT FROM pg_temp.rpt_fwd_nodes(plan, 'Anser Bloom Consumer', false) n + WHERE n->'Plans'->0->>'Custom Plan Provider' = 'Anser Bloom Consumer') THEN + RAISE EXCEPTION 'star did not exercise stacked consumers (optimizer=%): %', opt, plan; + END IF; + -- The property the design rests on: filters produced in the InitPlan's -- dispatch are applied by the main plan's dispatch. plan := pg_temp.rpt_fwd_plan(star, true); diff --git a/gpcontrib/robust_predicate_transfer/src/rptgraph.cpp b/gpcontrib/robust_predicate_transfer/src/rptgraph.cpp index 24867b76b44..359e4365015 100644 --- a/gpcontrib/robust_predicate_transfer/src/rptgraph.cpp +++ b/gpcontrib/robust_predicate_transfer/src/rptgraph.cpp @@ -635,5 +635,6 @@ RptBuildClasses(RptTransferMgr *mgr) } rpt_classify_tables(mgr); + mgr->logical_neighbor_matrix = mgr->neighbor_matrix; rpt_skip_unfiltered(mgr); } diff --git a/gpcontrib/robust_predicate_transfer/src/rptlocal.c b/gpcontrib/robust_predicate_transfer/src/rptlocal.c index 22f7a74fc39..36a6c91dc76 100644 --- a/gpcontrib/robust_predicate_transfer/src/rptlocal.c +++ b/gpcontrib/robust_predicate_transfer/src/rptlocal.c @@ -45,6 +45,8 @@ */ #include "postgres.h" +#include "ansertlist.h" + #include "commands/explain.h" #include "executor/executor.h" #include "lib/bloomfilter.h" @@ -186,7 +188,7 @@ rpt_local_build(const CustomScanMethods *methods, Plan *child, cs->scan.plan.flow = (Flow *) copyObject(child->flow); cs->scan.scanrelid = 0; cs->custom_plans = list_make1(child); - cs->custom_scan_tlist = copyObject(child->targetlist); + cs->custom_scan_tlist = AnserFilterScanTlist(child); cs->methods = methods; return cs; } diff --git a/gpcontrib/robust_predicate_transfer/src/rptplan.cpp b/gpcontrib/robust_predicate_transfer/src/rptplan.cpp index 70e6c4f5e83..2862f13a001 100644 --- a/gpcontrib/robust_predicate_transfer/src/rptplan.cpp +++ b/gpcontrib/robust_predicate_transfer/src/rptplan.cpp @@ -142,67 +142,66 @@ rpt_find_edge(const RptTransferMgr *mgr, const std::set &constructed, size_t best_weight = 0; int64 best_rows = -1; bool found = false; - - /* Algorithm 1: select the frontier vertex, then each tree's parent. */ + auto edge_at = [&](int parent_rtindex, int candidate_rtindex, bool forward) + -> const RptEdgeInfo * { + const auto &matrix = forward || mgr->logical_neighbor_matrix.empty() + ? mgr->neighbor_matrix : mgr->logical_neighbor_matrix; + auto row = matrix.find(parent_rtindex); + if (row == matrix.end()) + return NULL; + auto edge = row->second.find(candidate_rtindex); + if (edge == row->second.end() || + (edge->second.protect_left && edge->second.protect_right)) + return NULL; + return &edge->second; + }; + + /* Grow one forest over both views. Forward pruning must not disconnect + * tables that can still receive a useful backward filter. */ for (int candidate_rtindex : unconstructed) - { for (int parent_rtindex : constructed) - { - const auto &row = mgr->neighbor_matrix.find(parent_rtindex); - if (row == mgr->neighbor_matrix.end()) - continue; - const auto &eit = row->second.find(candidate_rtindex); - if (eit == row->second.end() || - (eit->second.protect_left && eit->second.protect_right)) - continue; - size_t weight = eit->second.clauses.size(); - int64 rows = mgr->vertices[mgr->vertex_index_by_rtindex.find(candidate_rtindex)->second].cardinality; - - if (!found || weight > best_weight || - (weight == best_weight && rows > best_rows)) + for (bool forward : {true, false}) { - found = true; - best_weight = weight; - best_rows = rows; - *target_rtindex = candidate_rtindex; + const RptEdgeInfo *edge = edge_at(parent_rtindex, candidate_rtindex, forward); + if (edge == NULL) + continue; + size_t weight = edge->clauses.size(); + int64 rows = mgr->vertices[mgr->vertex_index_by_rtindex.at(candidate_rtindex)].cardinality; + if (!found || weight > best_weight || + (weight == best_weight && rows > best_rows)) + { + found = true; + best_weight = weight; + best_rows = rows; + *target_rtindex = candidate_rtindex; + } } - } - } if (!found) return false; *forward_parent_rtindex = *backward_parent_rtindex = -1; - size_t forward_weight = 0; - size_t backward_weight = 0; - for (int parent_rtindex : constructed) + for (bool forward : {true, false}) { - const auto &row = mgr->neighbor_matrix.find(parent_rtindex); - if (row == mgr->neighbor_matrix.end()) - continue; - const auto &eit = row->second.find(*target_rtindex); - if (eit == row->second.end() || - (eit->second.protect_left && eit->second.protect_right)) - continue; - const RptEdgeInfo &edge = eit->second; - size_t weight = edge.clauses.size(); - bool forward_open = edge.left_rtindex == *target_rtindex ? !edge.protect_right : !edge.protect_left; - bool backward_open = edge.left_rtindex == parent_rtindex ? !edge.protect_right : !edge.protect_left; - - /* A protected edge is not a parent candidate for that stage. Prefer - * its best open alternative, even if it has lower weight. */ - if (forward_open && (*forward_parent_rtindex == -1 || weight > forward_weight || - (weight == forward_weight && - forward_depth.at(parent_rtindex) > forward_depth.at(*forward_parent_rtindex)))) - { - *forward_parent_rtindex = parent_rtindex; - forward_weight = weight; - } - if (backward_open && (*backward_parent_rtindex == -1 || weight > backward_weight || - (weight == backward_weight && - backward_depth.at(parent_rtindex) < backward_depth.at(*backward_parent_rtindex)))) + int *selected = forward ? forward_parent_rtindex : backward_parent_rtindex; + const auto &depth = forward ? forward_depth : backward_depth; + size_t best_parent_weight = 0; + for (int parent_rtindex : constructed) { - *backward_parent_rtindex = parent_rtindex; - backward_weight = weight; + const RptEdgeInfo *edge = edge_at(parent_rtindex, *target_rtindex, forward); + if (edge == NULL) + continue; + int source_rtindex = forward ? *target_rtindex : parent_rtindex; + bool open = edge->left_rtindex == source_rtindex + ? !edge->protect_right : !edge->protect_left; + size_t weight = edge->clauses.size(); + if (open && (*selected == -1 || weight > best_parent_weight || + (weight == best_parent_weight && + (forward ? depth.at(parent_rtindex) > depth.at(*selected) + : depth.at(parent_rtindex) < depth.at(*selected))))) + { + *selected = parent_rtindex; + best_parent_weight = weight; + } } } return true; @@ -341,8 +340,10 @@ rpt_create_transfer_plan(RptTransferMgr *mgr, bool protect_right; size_t c; - eit = mgr->neighbor_matrix[selected_edges[i].first].find(selected_edges[i].second); - if (eit == mgr->neighbor_matrix[selected_edges[i].first].end()) + auto &matrix = forward || mgr->logical_neighbor_matrix.empty() + ? mgr->neighbor_matrix : mgr->logical_neighbor_matrix; + eit = matrix[selected_edges[i].first].find(selected_edges[i].second); + if (eit == matrix[selected_edges[i].first].end()) continue; edge = &eit->second; diff --git a/gpcontrib/robust_predicate_transfer/test/graph_test.cpp b/gpcontrib/robust_predicate_transfer/test/graph_test.cpp index 71402779597..9f2dee61b57 100644 --- a/gpcontrib/robust_predicate_transfer/test/graph_test.cpp +++ b/gpcontrib/robust_predicate_transfer/test/graph_test.cpp @@ -1205,8 +1205,30 @@ static void backward_selection() CHECK(classes[0].consumers[1].accepted); } +static void unfiltered_backward_edges() +{ + for (bool filtered : {false, true}) + { + RptTransferMgr analysis; + vertex(analysis, 1, 10000, filtered); + vertex(analysis, 2, 1000, filtered); + connect(analysis, edge(1, 2)); + RptBuildClasses(&analysis); + CHECK(!analysis.logical_neighbor_matrix.at(1).empty()); + if (!filtered) CHECK(analysis.neighbor_matrix.at(1).empty()); + RptTransferMgr runtime; + RptBuildForwardPlan(&analysis, &runtime); + CHECK(runtime.root_rtindexes == std::set({1})); + CHECK(runtime.graph.at(1).backward_stage.out.size() == 1); + CHECK(runtime.graph.at(1).backward_stage.out[0].other_rtindex == 2); + CHECK(runtime.graph.at(2).backward_stage.in.size() == 1); + if (!filtered) CHECK(runtime.graph.at(1).forward_stage.in.empty()); + } +} + int main() { + unfiltered_backward_edges(); forward_delivery_process_counts(); backward_selection(); raw_datum_key_types(); diff --git a/gpcontrib/robust_predicate_transfer/test/inject_test.cpp b/gpcontrib/robust_predicate_transfer/test/inject_test.cpp index 3c200129d53..74976f58cf2 100644 --- a/gpcontrib/robust_predicate_transfer/test/inject_test.cpp +++ b/gpcontrib/robust_predicate_transfer/test/inject_test.cpp @@ -30,6 +30,9 @@ #include "../src/rptforward.cpp" #include "../src/rptbackwardplan.cpp" #include "../src/rptbackward.cpp" +extern "C" { +#include "ansertlist.h" +} #include #include #include @@ -105,6 +108,49 @@ static int max_plan_node_id(Plan *p) } return m; } +TargetEntry *get_tle_by_resno(List *list, AttrNumber resno) +{ + ListCell *lc; + foreach(lc, list) + if (((TargetEntry *) lfirst(lc))->resno == resno) + return (TargetEntry *) lfirst(lc); + return NULL; +} +void *copyObjectImpl(const void *input) +{ + if (input == NULL) return NULL; + const Node *node = (const Node *) input; + if (IsA(node, Var)) + { + Var *out = (Var *) palloc(sizeof(Var)); + memcpy(out, input, sizeof(Var)); return out; + } + if (IsA(node, TargetEntry)) + { + TargetEntry *out = (TargetEntry *) palloc(sizeof(TargetEntry)); + memcpy(out, input, sizeof(TargetEntry)); + out->expr = (Expr *) copyObjectImpl(out->expr); return out; + } + CHECK(IsA(node, List)); + List *out = NIL; ListCell *lc; + foreach(lc, (List *) input) out = lappend(out, copyObjectImpl(lfirst(lc))); + return out; +} +Node *expression_tree_mutator_impl(Node *node, tree_mutator_callback mutator, void *ctx) +{ + if (!node) return NULL; + if (IsA(node, Var)) return (Node *) copyObjectImpl(node); + if (IsA(node, TargetEntry)) + { + TargetEntry *out = (TargetEntry *) copyObjectImpl(node); + out->expr = (Expr *) mutator((Node *) ((TargetEntry *) node)->expr, ctx); + return (Node *) out; + } + CHECK(IsA(node, List)); + List *out = NIL; ListCell *lc; + foreach(lc, (List *) node) out = lappend(out, mutator((Node *) lfirst(lc), ctx)); + return (Node *) out; +} int AnserSliceProducers(PlannedStmt *, int slice_index) { return slice_index >= 0 ? 3 : 0; @@ -337,8 +383,48 @@ static void backward_stacks(bool refused, bool materialize, bool with_forward) } } +static void stacked_deparse_targetlists() +{ + SeqScan base = {}; base.scan.plan.type = T_SeqScan; + Var columns[2] = {}; TargetEntry entries[2] = {}; + for (int i = 0; i < 2; ++i) + { + columns[i].xpr.type = T_Var; columns[i].varno = 42; + columns[i].varattno = i + 7; + entries[i].xpr.type = T_TargetEntry; entries[i].expr = (Expr *) &columns[i]; + entries[i].resno = i + 1; entries[i].resjunk = i == 1; + base.scan.plan.targetlist = lappend(base.scan.plan.targetlist, &entries[i]); + } + CustomScan stack[8] = {}; Plan *child = &base.scan.plan; + for (int depth = 0; depth < 8; ++depth) + { + CustomScan *node = &stack[depth]; node->scan.plan.type = T_CustomScan; + node->custom_scan_tlist = AnserFilterScanTlist(child); + for (int i = 0; i < 2; ++i) + { + TargetEntry *resolved = get_tle_by_resno(node->custom_scan_tlist, i + 1); + CHECK(((Var *) resolved->expr)->varno == 42); + CHECK(((Var *) resolved->expr)->varattno == i + 7); + CHECK(resolved->resjunk == (i == 1)); + Var *var = (Var *) copyObjectImpl(&columns[i]); + var->varno = INDEX_VAR; var->varattno = i + 1; + TargetEntry *tle = (TargetEntry *) copyObjectImpl(&entries[i]); + tle->expr = (Expr *) var; + node->scan.plan.targetlist = lappend(node->scan.plan.targetlist, tle); + } + node->custom_plans = list_make1(child); + child = &node->scan.plan; + } + /* Resolve the referenced slot, not the tlist's positional order. */ + ((Var *) ((TargetEntry *) linitial(stack[7].scan.plan.targetlist))->expr)->varattno = 2; + List *resolved = AnserFilterScanTlist(&stack[7].scan.plan); + CHECK(((Var *) ((TargetEntry *) linitial(resolved))->expr)->varattno == 8); + CHECK(columns[0].varno == 42 && columns[0].varattno == 7); +} + int main() { + stacked_deparse_targetlists(); backward_stacks(false, true, true); backward_stacks(false, true, false); backward_stacks(true, true, true); From 3f1923375a082b81f40ef8319af4f849ccc01aab Mon Sep 17 00:00:00 2001 From: Leonid Borchuk Date: Fri, 13 Nov 2026 18:16:31 +0300 Subject: [PATCH 27/27] Enhancement: Simplify RPT controls and examples Always apply forward filters only to main-plan roots. Remove the forward_apply switch, its extra consumer placement and cost branches, and the corresponding SQL and native test variants. Keep intermediate local transfers inside the reducer and retain the real reducer InitPlan. Remove debug_noop_initplan and its measurement script. Mark forward and backward as session-settable developer controls with GUC_NOT_IN_SAMPLE; rpt.enable remains the user-facing master switch. Refresh the README with the current planner and executor call chains, graph views, structures, injection order and diagnostic workflow. Add a four-table star collector and a step-by-step guide for gathering graph decisions, results, actual text/JSON EXPLAIN ANALYZE plans and rotating baseline/forward/backward/full timing samples under both optimizers. Validation: compiler checks; native tests with -DNDEBUG; 11 survey and 6 diagnostic tests; SQL/expected and README link checks; git diff --check. The collector was statically checked. Cluster regressions and example measurements remain pending on the development cluster. Assisted-by: ChatGPT --- gpcontrib/robust_predicate_transfer/README.md | 683 +++++++++++++----- .../expected/rpt_backward.out | 14 +- .../expected/rpt_forward.out | 34 +- .../robust_predicate_transfer/include/rpt.h | 5 - .../scripts/collect_star_example.sql | 437 +++++++++++ .../scripts/measure_initplan.py | 58 -- .../sql/rpt_backward.sql | 14 +- .../sql/rpt_forward.sql | 34 +- .../src/rptforward.cpp | 6 - .../src/rptforwardplan.cpp | 18 +- .../robust_predicate_transfer/src/rptinit.cpp | 120 +-- .../src/rptinject.cpp | 5 +- .../test/graph_test.cpp | 9 +- .../test/inject_test.cpp | 30 +- 14 files changed, 992 insertions(+), 475 deletions(-) create mode 100644 gpcontrib/robust_predicate_transfer/scripts/collect_star_example.sql delete mode 100644 gpcontrib/robust_predicate_transfer/scripts/measure_initplan.py diff --git a/gpcontrib/robust_predicate_transfer/README.md b/gpcontrib/robust_predicate_transfer/README.md index c369ac4d58c..87ef91371b3 100644 --- a/gpcontrib/robust_predicate_transfer/README.md +++ b/gpcontrib/robust_predicate_transfer/README.md @@ -78,214 +78,332 @@ server configuration and restart the cluster. In a database, create the `rpt_transfer_plan(text)` reports the transfer analysis without injecting filters into the query it describes. `rpt.debug=on` logs planning decisions. -## Call chain and data structures +## Planning workflow and call stack -### PostgreSQL planning entry point +RPT transforms a finished distributed `PlannedStmt` on the coordinator. +PostgreSQL or ORCA chooses the joins, scans and Motions first. RPT then +adds filter nodes and, when forward transfer is selected, a reducer InitPlan. +It does not replace the optimizer or execute queries during planning. -At preload, `_PG_init()` registers RPT's local CustomScan methods and -installs `rpt_planner()` as a planner hook. The query follows this path: +With `anser,robust_predicate_transfer` preloaded in that order, the planning +call sequence is: ```text -SQL → parse → analyze and rewrite → pg_plan_query() → planner() - → rpt_planner() - → previous planner hook (Anser) - → standard_planner() → PostgreSQL planner or ORCA - ← PlannedStmt - → RPT processing - ← final PlannedStmt -→ executor +pg_plan_query() + planner() + rpt_planner() [RPT planner hook] + anser_planner() [previous hook] + standard_planner() [PostgreSQL planner or ORCA] + AnserApplyRuntimeFilters() [inactive with runtime_filter=off] + rpt_log_analysis() [only with rpt.enable + rpt.debug] + rpt_try_injection() [eligible coordinator SELECT] + rpt_build_injection() [C++ exception boundary] + RptInject(stmt, materialize=true) + return final PlannedStmt ``` -RPT analyzes a finished distributed plan, after the previous hook returns. -Runtime injection runs on the coordinator for eligible SELECT statements -when `rpt.enable` and either `rpt.forward` or `rpt.backward` are enabled. It requires -`anser.enable=on` and `anser.runtime_filter=off`. +The hook injects only when `rpt.enable` is on and at least one of +`rpt.forward` or `rpt.backward` is on. It excludes SELECT INTO, COPY INTO and +refresh plans. `RptInject()` also requires `anser.enable=on`, +`anser.runtime_filter=off`, and a plan without existing Anser or RPT filter +nodes. See [rptinit.cpp](src/rptinit.cpp) and +[Anser's hooks](../anser/src/anserinit.c). -The hook calls `rpt_try_injection()` → `rpt_build_injection()` → -`RptInject(stmt, true, ...)`. Injection works on a private copy of -`PlannedStmt`. An ordinary construction error produces a warning and retains -the original plan; query cancellation still propagates. See -[rptinit.cpp](src/rptinit.cpp) and [rptforward.cpp](src/rptforward.cpp). +`rpt_try_injection()` creates a memory context for construction. +`RptInject()` copies the statement and edits that copy. If construction +fails, the hook discards the context, reports a warning and returns the +original plan. Query cancellation propagates. `RptPgCall()` translates +PostgreSQL errors into C++ exceptions while STL owners unwind; the entry +boundary then returns the error to PostgreSQL's handler. -### Analysis and schedule selection +### Inside the shared injector -Inside `RptInject()`, the analysis proceeds as follows: +[`RptInject()`](src/rptforward.cpp) runs the following sequence once for +runtime injection. Forward and backward use the same largest-root forest +and the same scan pointers into the copied statement. -| Call | Product | Use | +```text +RptInject() + check Anser settings and existing filter nodes + copy PlannedStmt + RptCollectGraph() → analysis: RptTransferMgr + RptBuildClasses(analysis) + RptBuildForwardPlan(analysis) → runtime schedule: RptTransferMgr + RptCheckBackwardConsumers(stmt, schedule) → backward_consumers proofs + rpt_find_parents() → original parents and scan slices + initialize RptInjectionState → IDs and current scan-stack tops + collect process counts and row/selectivity estimates + + RptInjectForward() [when rpt.forward=on] + RptSelectForward() → RptForwardComponent[] + preflight scans and key output positions + rpt_assign_forward_channels() → IDs from shared state + build reducer scans and producer/consumer wrappers + rpt_apply_in_main() → forward consumers in main plan + return reducer children and selected components + + RptInjectBackward() [when rpt.backward=on] + AnserSliceProducers() → actual slice process counts + RptSelectBackward() → RptBackwardClass[] + preflight each accepted class against original parents/current stack tops + assign channel ID from shared state + AnserBuildBloomProducerScan() → wrap root's current top + AnserBuildBloomConsumerScan() → wrap each accepted member's top + RptRelinkStack() → update original parent and top map + + RptAttachReducerInitPlan() [only if reducer children exist] + return copied statement if anything was injected, otherwise original +``` + +`RptInjectForward()` is an internal function in +[rptforward.cpp](src/rptforward.cpp). Backward placement lives in +[rptbackward.cpp](src/rptbackward.cpp); its selector is in +[rptbackwardplan.cpp](src/rptbackwardplan.cpp). A dry run follows selection +and preflight without constructing executable nodes. + +### Graphs, schedules and placement state + +The analysis structures are declared in [rpt.h](include/rpt.h): + +| Structure | Produced by | Used for | | --- | --- | --- | -| `RptCollectGraph()` | Scan vertices and join edges in `RptTransferMgr` | Records relations, keys, predicates, distribution policies and allowed transfer directions | -| `RptBuildClasses()` | Key equivalence classes and table classifications | Finds relations connected through equal keys and possible bypass edges | -| `RptBuildForwardPlan()` | Largest-root schedule for both runtime passes | Retains forward and backward edges and their local/global classifications | -| `RptCheckBackwardConsumers()` | Per-edge eligibility proof | Records original parents, key positions, slice indexes, refusal reasons and the fan-out cap | -| `RptSelectForward()` | `vector` | Selects chains using cost, root-size and selectivity gates | -| `RptSelectBackward()` | `vector` | Groups proven consumers by class and applies sizing, selectivity and exchange-cost gates | - -The implementations are in [rptregion.cpp](src/rptregion.cpp), -[rptgraph.cpp](src/rptgraph.cpp), [rptplan.cpp](src/rptplan.cpp), -[rpteligible.cpp](src/rpteligible.cpp) and -[rptforwardplan.cpp](src/rptforwardplan.cpp) and -[rptbackwardplan.cpp](src/rptbackwardplan.cpp). - -The structures are declared in [rpt.h](include/rpt.h): - -| Structure | Meaning | +| `RptVertexInfo` | `RptCollectGraph()` | One base scan, relation OID, row estimate, restriction/distribution properties and join keys; `scan` points into the working statement | +| `RptKey` | Graph collection | Physical column identity `{rtindex, attno}` | +| `RptEdgeInfo` | Graph collection and class building | Equality clauses between two vertices and transfer directions prohibited by join semantics | +| `RptKeyGroup` | `RptBuildClasses()` | An equivalence class, its members and selected leader/key | +| `RptGraphNode` / `RptGraphEdge` | `RptBuildForwardPlan()` | Per-vertex incoming/outgoing transfers for both passes and their local/global classification | +| `RptBackwardConsumer` | `RptCheckBackwardConsumers()` | Per-edge proof or refusal, original parents, output key positions, slice indexes, `defer_first` and estimated benefit | +| `RptForwardComponent` / `RptForwardChannel` | `RptSelectForward()` | One root's ordered reducer chain, channels, sizing, estimates and acceptance decision | +| `RptBackwardClass` / `RptBackwardTarget` | `RptSelectBackward()` | One root producer and its selected/refused members, sizing and exchange-cost decision | +| `RptTransferMgr` | Collection, class building and scheduling | Owns the temporary graph, classes, forest and eligibility decisions | + +`RptBuildClasses()` forms unprotected key equivalence classes and makes +implied equality edges explicit. It retains those logical edges in +`logical_neighbor_matrix`, then applies `SkipUnfilteredTable` to the +forward view, `neighbor_matrix`. Forward-only skipping therefore cannot +erase a useful backward edge from an unfiltered root. + +Despite its name, `RptBuildForwardPlan()` constructs the shared runtime +forest for **both** passes. It selects the largest scan by estimated +cardinality as each component's root. Frontier selection considers both +graph views; forward parents use the pruned forward edges, and backward +parents use the logical edges. Original join-direction protections remain +in force. `graph`, `root_rtindexes`, `bridge_rtindexes`, +`transfer_order_rtindexes` and `reducer_order_rtindexes` hold the result. +See [rptgraph.cpp](src/rptgraph.cpp) and [rptplan.cpp](src/rptplan.cpp). + +Executable endpoints must be plain SeqScans exposing the selected key in +their output. Join keys must pass the supported built-in, canonical by-value +type/equality checks used for raw-Datum Bloom filtering. + +The backward proof runs before wrapping any scan. It permits a blocking +consumer only on a supported hash probe path whose producer is in the +build subtree and in a different slice. It checks intervening blocking +nodes, outer-join paths, scans inside SubPlans and external producer +parameters, then +applies the per-class consumer cap. Local edges use core runtime filtering; +bridge-led classes are deferred. See [rpteligible.cpp](src/rpteligible.cpp). + +[`RptInjectionState`](include/rptinject.h) holds the condition/node counters +and two maps keyed by the original scan pointer: `parents` records the +original relink point, and `tops` records the outermost wrapper so far. +Forward consumers are added first. Backward producers and consumers wrap +these current tops, preserving forward filters below the backward producer. +Preflight checks that each original parent still holds that top exactly +once and that the key remains available in the scan's output. + +These identities have distinct meanings: + +| Name | Meaning | | --- | --- | -| `RptVertexInfo` | One scan, its relation OID, row estimate, predicates and distribution properties; `scan` points into the working plan | -| `RptKey` | A column identified by `{rtindex, attno}` | -| `RptEdgeInfo` | Equality connection between two vertices, key pairs and directions prohibited by join semantics | -| `RptKeyGroup` | An equivalence class of join keys and its leader | -| `RptGraphNode` | Incoming and outgoing forward/backward transfers for one vertex | -| `RptGraphEdge` | One transfer; `other_rtindex` names the producer for an incoming edge and the consumer for an outgoing edge | -| `RptTransferMgr` | Temporary owner of the vertices, edges, classes, schedules and backward eligibility decisions | -| `RptForwardComponent` | One root, its ordered reducer chain, channels, estimates, and acceptance or refusal reason | -| `RptForwardChannel` | Source and target keys, filter sizing, channel identity and local/global execution decisions | -| `RptBackwardConsumer` | One survey proof or refusal for a class member | -| `RptBackwardClass` | One root producer, its sizing and selected consumers, estimated cost and decision | -| `RptInjectionState` | Shared channel/node allocators and original-parent/current-top maps, declared in [rptinject.h](include/rptinject.h) | - -`RptTransferMgr.vertex_index_by_rtindex` maps a range-table index to a -position in `vertices`. `neighbor_matrix` holds the forward graph after skip rules; -`logical_neighbor_matrix` retains the logical edges for backward scheduling; -`uf_parent` represents the key union-find; `groups` contains the resulting -classes. `graph` maps each range-table index to its transfer node. -`root_rtindexes`, `bridge_rtindexes`, `reducer_order_rtindexes` and -`transfer_order_rtindexes` describe the schedule. These are planning -structures; the executor receives the resulting plan nodes instead. - -Keep the identities distinct when reading or extending the code: - -| Name | Identity | -| --- | --- | -| `rtindex` | Relation occurrence in `PlannedStmt.rtable`; distinguishes self-join aliases | -| `relid` | Relation OID; identifies the underlying relation | -| `attno` | Column number within the relation or the relevant node's output | -| `plan_node_id` | Executable plan node | +| `rtindex` | Relation occurrence in `PlannedStmt.rtable`; self-join aliases have different indexes | +| `relid` | Underlying relation OID; aliases of one table share it | +| `RptKey.attno` | Physical column number in that relation | +| `producer_attno` / `consumer_attno` | Key position in the scan's projected output tuple | +| `class_key` | Representative `RptKey` identifying an equivalence class; distinct from its leader's key | +| `plan_node_id` | Executable plan-node identity | | `slice_index` | Distributed execution slice | -| `condition_id` | Runtime filter channel connecting a producer and its consumers | +| `condition_id` | Filter-channel identity, allocated across both runtime passes | -After selection, preflight checks that the scans and required key outputs -can be used. A rejected component adds no executable nodes. +`vertex_index_by_rtindex` maps a relation occurrence to its position in +`vertices`. Relation aliases are obtained from the statement's range table +when dumping, rather than stored in each vertex. The executor receives the +modified `PlannedStmt` and its CustomScan parameters; it does not receive +these C++ analysis containers. -### Constructing the executable plan +## Executable plan and execution workflow -For an accepted chain `A → B → C`, where C is the largest relation, RPT -builds this conceptual plan: +For a selected forward chain `A → B → C`, with C as the root, the conceptual +plan is: ```text Reducer InitPlan: - Sequence - Producer(filter A) - copied scan A + safe restriction quals - Producer(filter B) - Consumer(filter A) - copied scan B + safe restriction quals - empty Result - -Main plan: - original joins - Producer(backward filter C) + Gather Motion to coordinator + Sequence on segments + Producer(filter A) + copied scan A + safe restriction quals + Producer(filter B) + Consumer(filter A) + copied scan B + safe restriction quals + empty Result + +Main plan, under the original joins and Motions: + root subtree: + Producer(backward filter C) [one per accepted root-led class] Consumer(filter B) original scan C - Consumer(backward filter C) [only on proven class members] + + accepted class-member subtree: + Consumer(backward filter C) original member scan ``` -Reducer scans output the required key columns. Restriction quals that -cannot be proven safe for this extra execution are omitted, reducing filter -selectivity. Consumers sit below producers, so B's outgoing filter contains -keys from rows that have already passed A's filter. - -In-process reducer channels use `RptBuildLocalProducerScan()` and -`RptBuildLocalConsumerScan()` from [rptlocal.c](src/rptlocal.c). Channels -requiring Anser use `AnserBuildBloomProducerScan()` and -`AnserBuildBloomConsumerScan()`. Policy locality and in-process execution -are separate decisions: a filter consumed by the main plan must cross the -InitPlan dispatch boundary even when its keys are co-distributed. - -By default, only the main-plan root receives forward filters. -`rpt.forward_apply=all` also attempts to wrap applicable chain scans. -Backward producers and consumers are added by -[`RptInjectBackward()`](src/rptbackward.cpp) after forward wrappers. A class -is preflighted before any of its nodes are built: every endpoint must occur -once, remain under its proven original parent, and expose the required key. -Each wrapper uses the current stack top and relinks only the original parent. -One allocator supplies condition IDs across both passes, and one supplies -plan-node IDs. If forward is disabled or refused, backward may still run on -the plain root scan. - -`RptAttachReducerInitPlan()` in [rptinject.cpp](src/rptinject.cpp) attaches -the reducer: +The reducer excludes the root. Its scans output the required key columns +and copy restriction quals only when they are proven safe for this extra, +eager execution; otherwise the whole qual list is dropped. An intermediate +consumer sits below its producer, so the outgoing filter summarizes rows +that survived incoming filters. + +`RptAttachReducerInitPlan()` in [rptinject.cpp](src/rptinject.cpp) adds the +Gather/Sequence/empty-Result tree to `PlannedStmt.subplans`, attaches a +`SubPlan` to the main tree's `initPlan`, adds a `PARAM_EXEC` slot and extends +`slices`, `subplan_sliceIds` and `numSlices`. It runs after both injection +passes. Backward-only injection adds no reducer or extra scan. If forward +is refused, backward can still build a filter from the plain root scan. + +### Core executor and RPT callbacks + +RPT installs a planner hook, not an executor hook. Its execution work is +performed by the injected CustomScan callbacks registered at preload. +The coordinator's core execution path is: ```text -Coordinator InitPlan slice - Gather Motion - Segment sender slice - Sequence - reducer subtree 1 - reducer subtree 2 - ... - Result with constant false +ExecutorStart() + standard_ExecutorStart() + InitPlan() [initialize plan states] + preprocess_initplans() + ExecSetParamPlan() [dispatch/run reducer InitPlan] + CdbDispatchPlan() [dispatch main plan] +ExecutorRun() + anser_executor_run() [Anser executor hook] + standard_ExecutorRun() + execute main plan +ExecutorFinish() + anser_executor_finish() + standard_ExecutorFinish() +ExecutorEnd() + anser_executor_end() [channel cleanup] + standard_ExecutorEnd() ``` -It appends the reducer tree to `PlannedStmt.subplans`, attaches a `SubPlan` -descriptor to the main tree's `initPlan`, adds a `PARAM_EXEC` slot in -`paramExecTypes`, and extends `slices`, `subplan_sliceIds` and `numSlices`. -New nodes receive unique `plan_node_id` values. The modified `PlannedStmt` -is the lasting output of RPT planning. - -### Execution +The reducer's segment `ExecSequence()` drains its children in order before +returning the final empty Result. Its useful output is the filters, not +query rows. Forward filters are produced before the main plan starts; +Anser keeps them available across the InitPlan/main-plan dispatch boundary. + +During main execution, the root scan passes through its forward consumers +and then its backward producers. Each backward producer accumulates keys +from those surviving rows and publishes on exhaustion. A member consumer +uses the existing proof's `defer_first` flag: where the hash join can fetch +an outer tuple before building, it passes that first tuple unfiltered and +receives the filter on the next call. Backward adds no scan of either root +or member. The original joins still enforce the exact SQL conditions. + +### Local handoff and Anser exchange + +A policy-local hop consumed only inside the reducer uses +`RptBuildLocalProducerScan()` / `RptBuildLocalConsumerScan()` and the +[rptlocal.c](src/rptlocal.c) callbacks. The producer stores a Bloom filter +under `(EState, condition_id)` and marks it complete when drained. The +consumer uses only a complete filter; otherwise it passes rows through. + +Other hops use Anser's producer/consumer builders. Policy locality alone +does not permit in-process handoff across separate dispatches: forward +filters consumed by the main-plan root use Anser. Intermediate main-plan +scans do not consume forward filters; eligible members can receive the +root's backward filter. -Cloudberry initializes the plan and processes distributed InitPlans through -`preprocess_initplans()` before main execution. The reducer's `Sequence` -drains its children in producer-before-consumer order. The final empty -`Result` returns no query rows; the reducer's useful output is its filters. +```text +QE ExecProcNode() + anser_produce_exec() → anser_produce_next() + ExecProcNode(child), then ExecAnserBloomFilterProduceAddDatum() + on exhaustion: ExecAnserBloomFilterProducePublish() + AnserSidebandPublish() + coordinator AnserDispatchNotifyHandler() + combine producer parts, then deliver the completed filter + +QE ExecProcNode() + anser_consume_exec() → anser_consume_next() + anser_consume_receive() → ExecAnserBloomFilterConsume() + push filter into the scan when supported, otherwise probe returned tuples +``` -For the example above, execution scans A and builds its filter, scans B -through A's filter and builds B's filter, then executes the main plan with -B's filter applied to C. C's backward producer summarizes the rows that -pass these consumers and publishes to the proven class-member consumers. -Anser's `defer_first` preserves the probe tuple a hash join may fetch before -building its hash table. The original joins still check the exact SQL -conditions. Abandoned producers cancel their channels, and exchange failure -or timeout makes consumers run unfiltered. +These are distributed call paths, not one process's stack. The transport +uses existing dispatch connections. Cancelled, unavailable or timed-out +exchanges leave consumers unfiltered; local backward edges are not given +blocking Anser consumers. See [Anser's scan callbacks](../anser/src/anserplanexec.c). -An in-process producer stores its Bloom filter in executor-owned state, -identified by `EState` and `condition_id`. A local consumer uses only a -complete filter; otherwise it passes rows through. Anser-backed nodes -handle filter publication, transport and consumption across dispatches. +Both Anser and local wrappers keep identity `INDEX_VAR` output lists for +execution. Their deparse lists are built through +[`AnserFilterScanTlist()`](../anser/include/ansertlist.h), which resolves a +child CustomScan's output expressions before copying them. This allows +`EXPLAIN`, including `VERBOSE`, to describe stacked filters without a +self-referencing deparse list. -### Diagnostic entry point +## Diagnostic workflow -`rpt_transfer_plan(text)` follows a separate path: +`rpt_transfer_plan(text)` plans one SELECT and returns its analysis without +executing the supplied query or injecting filters into its plan: ```text rpt_transfer_plan() - → pg_parse_query() - → pg_analyze_and_rewrite_fixedparams() - → pg_plan_query() [RPT injection suppressed] - → rpt_analyze() - → RptCollectGraph() - → RptBuildClasses() - → RptBuildTransferPlan() - → RptDumpTransferPlan() - → RptInject(materialize=false) - → text containing analysis and forward decisions + rpt_transfer_plan_impl() + pg_parse_query() + pg_analyze_and_rewrite_fixedparams() + pg_plan_query() [automatic RPT injection suppressed] + rpt_analyze() + RptCollectGraph() + RptBuildClasses() + RptBuildTransferPlan() [filtered-root survey schedule] + RptDumpTransferPlan() + RptInject(materialize=false) [shared largest-root runtime schedule] + return text ``` -This function plans the supplied query but does not execute it, run reducer -scans or build filters. +The survey schedule retains its filtered-root heuristic for comparison; +it is not the schedule used for runtime injection. `forward component`, +`runtime survey`, `backward class` and `backward consumer ... root=` lines +show current runtime decisions on the largest-root forest, including +refusal reasons. Dry runs honor the forward/backward switches and Anser +settings even when `rpt.enable` is off. -The diagnostic dump retains the filtered-root survey schedule for comparison. -The `forward component`, `backward class` and `backward consumer ... root=` -lines describe runtime decisions on the shared largest-root schedule. +With `rpt.debug=on`, the planner also calls `rpt_log_analysis()` before +actual injection. That log builds its own survey and dry-run analysis; +runtime injection does not reuse those objects. Debug mode therefore adds +planning work. -## Backward controls and cost model +## Runtime controls and cost model -`rpt.backward` defaults to `on`; `rpt.enable` remains `off` by default. -`rpt.forward=off` enables a backward-only comparison. The backward producer -is sized from the root's estimated per-segment rows multiplied by the -accepted forward component's estimated selectivity, or by 1 when no forward -component was accepted. These estimates affect selection and sizing, not +`rpt.enable` is the normal user-facing switch and defaults to `off`. +`rpt.forward` and `rpt.backward` are developer controls for testing, +measurement and diagnosis. Both default to `on`, remain session-settable +(`PGC_USERSET`), and are marked `GUC_NOT_IN_SAMPLE`. Normal use leaves both +enabled. With the master switch enabled, developers can isolate either pass: + +| Settings | Runtime work | +| --- | --- | +| `rpt.forward=on`, `rpt.backward=on` | Selected forward reducer, then selected in-plan backward filters | +| `rpt.forward=on`, `rpt.backward=off` | Forward reducer and main-plan consumers | +| `rpt.forward=off`, `rpt.backward=on` | In-plan backward filters; no reducer | +| Both off, or `rpt.enable=off` | No runtime injection | + +`rpt.forward_min_root_rows` (10000, per segment) gates only the forward +reducer, and `rpt.max_chain_depth` (4) bounds forward dependency hops. +Forward filters are always applied only to the root in the main plan. +Intermediate forward consumers exist only inside the reducer. + +The backward producer is sized from the root's estimated per-segment rows +multiplied by the accepted forward component's estimated selectivity, or by +1 when no forward component was accepted. These estimates affect selection and sizing, not SQL correctness. Both passes use the exchange model: @@ -298,11 +416,11 @@ collection_ms_per_MiB × producer_processes × filter_MiB `rpt.forward_ms_per_mib_part` (3.5) controls collection, and `rpt.delivery_ms_per_mib` (0.8) controls delivery. The defaults come from the 2026-11-10 measurement on one host with three primaries. The existing -`rpt.forward_max_exchange_ms` and `rpt.forward_max_selectivity` names are -retained and apply to both passes. The exchange limit is per forward chain -or backward class; `-1` disables it. A selectivity threshold of 1 disables +`rpt.forward_max_exchange_ms` (100 ms) and +`rpt.forward_max_selectivity` (0.35) names are retained and apply to both +passes. The exchange limit is per forward chain or backward class; `-1` disables it. A selectivity threshold of 1 disables the benefit gate. Unknown backward NDV is treated as a kept fraction of 1. -`rpt.max_backward_consumers` caps each class after the proof, preferring +`rpt.max_backward_consumers` (4) caps each class after the proof, preferring larger estimated rows removed. Backward process counts come from the actual producer and consumer slices, @@ -310,7 +428,189 @@ including direct dispatch, singleton gangs and parallel workers. Reducer scans remain nonparallel; a worker's partial filter must never be treated as a complete segment-local filter. -## Validation and experiments +## Worked example: collect a four-table star + +The worked example will use real output from your development cluster. +The procedure below gathers the evidence for its graph, selection, +injection and final `EXPLAIN ANALYZE`; the annotated execution plan and +measured timings will be added from those artifacts. + +### 1. Build the current code and prepare the run + +Build and install both Anser and RPT on all hosts, and restart the cluster +so the preloaded libraries include the latest changes. Use an otherwise +idle cluster and a dedicated database, with a role allowed to create the +extension and tables and call `rpt_transfer_plan(text)`. + +Use your normal cluster connection environment. Run the collector from an +empty output directory and use a fresh database name. Set `rpt_repo` to +this checkout's absolute path on the development machine: + +```sh +rpt_repo=/path/to/cloudberry-leborchuk +rpt_output=/tmp/rpt-star-example +mkdir -p "$rpt_output" +createdb rpt_readme_example +cd "$rpt_output" +git -C "$rpt_repo" rev-parse HEAD > revision.txt +git -C "$rpt_repo" diff -- gpcontrib/anser gpcontrib/robust_predicate_transfer > source.diff +cp "$rpt_repo/gpcontrib/robust_predicate_transfer/scripts/collect_star_example.sql" collector.sql +set -o pipefail +psql -X -q -d rpt_readme_example \ + -v fact_rows=1000000 -v warmups=2 -v samples=7 \ + -f collector.sql \ + 2>&1 | tee collection.log +``` + +[`collect_star_example.sql`](scripts/collect_star_example.sql) creates the +`rpt_readme_star` schema and four randomly distributed tables. It stops +rather than overwrite an existing fixture schema. It keeps the fixture for +inspection after collection. The fact has one million rows by default; +customer, product and store have 1009, 503 and 211 rows. Fact keys cycle +through those three domains, and each dimension has a separate restriction +attribute cycling from 0 through 9. + +The script disables Anser's independent injection policy and debug logging. +It keeps the core runtime-filter settings and RPT cost/selectivity gates +at their current values and records them. The baseline therefore includes +whatever core filtering those settings enable. Each statement has a +120-second timeout. If the run stops, keep the partial directory and log. + +### 2. Inspect the query and input data + +The query is saved as `query.sql`: + +```sql +SELECT count(*) AS matched_rows, + coalesce(sum(f.amount_cents), 0) AS total_amount_cents +FROM rpt_readme_star.fact f +JOIN rpt_readme_star.customer c ON f.customer_key = c.k +JOIN rpt_readme_star.product p ON f.product_key = p.k +JOIN rpt_readme_star.store s ON f.store_key = s.k +WHERE c.region < 2 AND p.category < 2 AND s.zone < 2; +``` + +`cardinalities.csv` records the dimensions' full/selected row counts and +key NDVs. Their predicates select 202 customer keys, 102 product keys and +43 store keys. `fact-cardinalities.csv` records actual fact rows and key +NDVs, while `statistics.csv` contains the catalog estimates used to explain +planner decisions. `distribution.csv` records actual segment counts and +policies. `environment.txt` records the build, topology and effective +settings. + +### 3. Read the graph and forward selection + +For each optimizer, inspect `off-full.transfer.txt` or +`on-full.transfer.txt`. Here `off`/`on` names the requested **optimizer** +setting; `full` means both RPT passes are enabled. + +Resolve `vertex` numbers using aliases `f`, `c`, `p`, `s`. Range-table +indexes can differ between optimizers. The logical graph is: + +```text +customer c.k ── fact f.customer_key +product p.k ── fact f.product_key +store s.k ── fact f.store_key +``` + +These are three separate equivalence classes, each containing a dimension +key and its corresponding fact key. The filtered dimensions are forward +candidates; the fact is the expected runtime root because its scan has the +largest estimated cardinality. Read the root from `forward component`, +not from the filtered-root survey's earlier `leader=` lines. + +`RptBuildClasses()` preserves the logical graph and prepares its forward +view. `RptBuildForwardPlan()` builds the shared forest, and +`RptSelectForward()` evaluates the dimension-to-fact channels using scan +estimates, NDVs, filter sizing, selectivity and exchange cost. Record: + +- The runtime root, reducer order and three source/target key pairs. +- `est_sel`, `est_cost_ms`, filter bytes and the final `decision`. +- Each backward class/member decision and its refusal reason, if any. + +The classes do not equate customer, product and store keys to one another, +so this example should not be illustrated as a dimension-to-dimension +chain. The dump's `chain=[...]` lists the reducer scans; the selected +channels show the actual dependencies. + +Do not force acceptance for the first run. A gate refusal is useful data, +including on wider clusters where collection/delivery costs increase. +Send that dump before changing the budget or selectivity threshold. + +### 4. Match selection to the injected plan + +For accepted forward transfers, RPT creates one copied, key-only scan per +selected dimension in the reducer. Each copied scan retains its safe +restriction and gets a producer. The main-plan fact scan gets the three +corresponding consumers; the dimension scans in the main plan are not +wrapped in forward consumers. + +The injector proves backward eligibility before adding these wrappers. +It then adds any accepted root producers above the fact's forward +consumers and member consumers at proven relink points. Finally, +`RptAttachReducerInitPlan()` attaches the reducer's Gather/Sequence tree. + +In a common star plan the fact is a hash probe input and the dimensions +are build inputs. In that case the current in-plan backward proof can +refuse all three members. Record that outcome: this example should explain +the optimizer's real plan, including why a transfer is absent. + +Compare the captured plan files for the same optimizer: + +| Arm | Settings | Purpose | +| --- | --- | --- | +| `baseline` | `rpt.enable=off` | Original optimizer plan and answer | +| `forward` | RPT enabled, forward on, backward off | Reducer and root-consumer placement | +| `backward` | RPT enabled, forward off, backward on | In-plan backward decisions without a reducer | +| `full` | RPT enabled, both passes on | Combined selected plan | + +For example, `on-full.explain.txt` is the actual text +`EXPLAIN (ANALYZE, VERBOSE, BUFFERS, SETTINGS, SUMMARY)` for the requested +ORCA run, and `on-full.explain.json` is its structured counterpart. Check +the reported optimizer to identify any fallback. These are separate +executions, so their timing and segment-level row counts can differ. + +### 5. Verify answers and measure the effect + +The collector compares count and integer sum for all eight optimizer/arm +combinations against the first baseline and stops on a mismatch. Each +answer is also saved in `*.result.csv`. + +After plan collection it runs two warmups and seven measured queries per +arm per optimizer. Arm order rotates between rounds. These are fresh, +unprepared query executions without EXPLAIN or debug logging; all arms run +in the same psql session. `collection.log` contains markers such as: + +```text +RPT_SAMPLE optimizer=on arm=full phase=sample run=1 +``` + +Use the `Time:` immediately following the two-column aggregate result as +the query sample. The log also contains timings for SET and marker +statements; exclude those and all `phase=warmup` queries. Report median +and minimum/maximum for each arm. These are warm-cache psql elapsed times, +including planning, execution and client round-trip; EXPLAIN's planning +and execution times are recorded separately, not used as substitutes. + +In the plans, inspect reducer producers, stacked fact consumers, actual +rows entering joins, filter-removal counters, buffers and any backward +producer/consumer nodes. A tuple-level filter can reduce join work while +the underlying scan still reads all heap pages; use the actual counters +rather than infer an I/O saving from fewer join rows. + +### 6. Return the evidence for the final README example + +Send the complete output directory, including `collector.sql`, `revision.txt`, +`source.diff`, `collection.log`, metadata CSVs, transfer dumps, result CSVs and both plan +formats. Also note CPU/RAM, storage type, number of physical hosts, whether +other workloads ran, and whether the data fit in cache. + +We can then annotate the real vertex/class IDs, candidate decisions, +producer/consumer placement, final `EXPLAIN ANALYZE`, row reduction and +measured overhead or benefit. After inspection, remove the dedicated +database when its fixture is no longer needed. + +## Build, test and diagnostic workflow Run native checks without a cluster: @@ -318,17 +618,29 @@ Run native checks without a cluster: make -C gpcontrib/robust_predicate_transfer unitcheck surveycheck diagnosticcheck ``` -With both extensions installed and preloaded on a running cluster: +After changing either preloaded library, build and install both extensions +on every host, then restart the cluster so backends load the new code: + +```sh +make -C gpcontrib/anser install +make -C gpcontrib/robust_predicate_transfer install +``` + +With the updated libraries preloaded on a running cluster: ```sh make -C gpcontrib/anser installcheck make -C gpcontrib/robust_predicate_transfer installcheck ``` +RPT's `installcheck` installs Anser's `anser_test` extension before running +regressions. The fault tests also require `gp_inject_fault`. + `rpt_backward` checks runtime shape, row reduction, gates and result equality under both optimizers. `rpt_backward_faults` requires `gp_inject_fault` and checks held delivery, consumer walk-away, oversized-producer cancellation, -query cancellation and a succeeding next command. Assertions use dynamic +query cancellation and a succeeding next command. Cancellation cases prepare +and verify an injected plan before starting the short timeout. Assertions use dynamic SQL so changes to RPT settings cannot silently reuse an earlier plan. For a step-by-step run in an isolated test database: @@ -343,22 +655,13 @@ and watchdog/cancellation evidence. `--suite forward` and `--suite backward_faults` run the other workloads. The legacy `diagnose_forward.py` entry point remains supported. -Before choosing production defaults, measure: - -| Experiment | Measurement | -| --- | --- | -| B0 | Largest-root eligibility and runtime refusal reasons on JOB/TPC-H/TPC-DS, under both optimizers | -| B1 | Delivery scaling with 1/2/4/8 consumers, 1/8/64 MiB filters and available cluster widths | -| B2 | Probe first-tuple time and consumer wait cost with backward off/on | -| B3 | No filters / Anser filters / RPT forward / RPT forward plus backward on snowflake and multi-fact queries | -| B4 | Per-query correctness and latency changes across JOB and TPC-DS SF100 | - -Use `scripts/survey.py` for B0; it plans queries without executing them and -reports both the original survey and largest-root runtime decisions. Run as -a superuser or a role granted EXECUTE on `rpt_transfer_plan(text)`. TPC-DS -14/23/24/39 contain two SELECTs each: split them into a/b variants or retain -two SELECT statements per file. Use the CTE form of TPC-H Q15. Expected query -counts are statements (103 for TPC-DS, 22 for TPC-H), not SQL file counts. +For benchmark planning surveys, use [scripts/survey.py](scripts/survey.py). +It plans queries without executing them and reports both the filtered-root +survey and largest-root runtime decisions. Run as a superuser or a role +granted EXECUTE on `rpt_transfer_plan(text)`. TPC-DS 14/23/24/39 contain two +SELECTs each: split them into a/b variants or retain two SELECT statements +per file. Use the CTE form of TPC-H Q15. Expected query counts are statements +(103 for TPC-DS, 22 for TPC-H), not SQL file counts. A reducer-side backward fallback, bridge-chained backward filters, dynamic abandonment and sharing delivered filters between consumers in one process diff --git a/gpcontrib/robust_predicate_transfer/expected/rpt_backward.out b/gpcontrib/robust_predicate_transfer/expected/rpt_backward.out index 9371e8b8a31..64fb1383841 100644 --- a/gpcontrib/robust_predicate_transfer/expected/rpt_backward.out +++ b/gpcontrib/robust_predicate_transfer/expected/rpt_backward.out @@ -112,7 +112,6 @@ $$; DO $$ DECLARE opt text; - mode text; q text; dump text; plan jsonb; @@ -129,7 +128,6 @@ BEGIN PERFORM set_config('rpt.forward_max_exchange_ms', '-1', true); FOREACH opt IN ARRAY ARRAY['off', 'on'] LOOP PERFORM set_config('optimizer', opt, true); - PERFORM set_config('rpt.forward_apply', 'root', true); PERFORM set_config('rpt.forward', 'off', true); PERFORM pg_temp.rpt_bw_expect(plain, 'm', 'inject'); -- Forward skipping must not suppress an unfiltered root's broadcast. @@ -235,15 +233,11 @@ BEGIN -- LIMIT 1 has no SQL ordering guarantee; compare a count instead. IF q LIKE '%LIMIT 1' THEN q := 'SELECT count(*) FROM (' || q || ') limited'; END IF; baseline := pg_temp.rpt_bw_result(q, false, false); - FOREACH mode IN ARRAY ARRAY['root', 'all'] LOOP - PERFORM set_config('rpt.forward_apply', mode, true); - IF pg_temp.rpt_bw_result(q, true, false) IS DISTINCT FROM baseline OR - pg_temp.rpt_bw_result(q, true, true) IS DISTINCT FROM baseline THEN - RAISE EXCEPTION 'backward result differs (optimizer=%, apply=%): %', opt, mode, q; - END IF; - END LOOP; + IF pg_temp.rpt_bw_result(q, true, false) IS DISTINCT FROM baseline OR + pg_temp.rpt_bw_result(q, true, true) IS DISTINCT FROM baseline THEN + RAISE EXCEPTION 'backward result differs (optimizer=%): %', opt, q; + END IF; END LOOP; - PERFORM set_config('rpt.forward_apply', 'root', true); q := 'SELECT count(*)::text || ''/'' || sum(length(m.pad || r.k::text))::text FROM rpt_bw_member m JOIN rpt_bw_root r ON m.k = r.k'; PERFORM pg_temp.rpt_bw_expect(q, 'm', 'inject'); diff --git a/gpcontrib/robust_predicate_transfer/expected/rpt_forward.out b/gpcontrib/robust_predicate_transfer/expected/rpt_forward.out index 6db0b165b76..f57fb56709a 100644 --- a/gpcontrib/robust_predicate_transfer/expected/rpt_forward.out +++ b/gpcontrib/robust_predicate_transfer/expected/rpt_forward.out @@ -112,7 +112,7 @@ DO $$ DECLARE opt text; q text; - mode text; + selectivity_limit text; dump text; plan jsonb; prep_n bigint; @@ -135,7 +135,6 @@ BEGIN PERFORM set_config('rpt.forward_max_exchange_ms', '-1', true); FOREACH opt IN ARRAY ARRAY['off', 'on'] LOOP PERFORM set_config('optimizer', opt, true); - PERFORM set_config('rpt.forward_apply', 'root', true); -- Shape: a reducer InitPlan with producers, a consumer in the main plan. dump := rpt_transfer_plan(star); @@ -172,6 +171,7 @@ BEGIN END IF; plan := pg_temp.rpt_fwd_plan(flake_global, true); IF pg_temp.rpt_fwd_count(plan, 'Anser Bloom Consumer', true) = 0 OR + pg_temp.rpt_fwd_count(plan, 'Anser Bloom Consumer', false) <> 1 OR pg_temp.rpt_fwd_count(plan, 'Anser Bloom Producer', true) < 2 OR pg_temp.rpt_fwd_removed(plan) <= 0 THEN RAISE EXCEPTION 'global chain shape or effect missing (optimizer=%): %', opt, plan; @@ -184,6 +184,7 @@ BEGIN END IF; plan := pg_temp.rpt_fwd_plan(flake_local, true); IF pg_temp.rpt_fwd_count(plan, 'RPT Local Bloom Producer', true) <> 1 OR + pg_temp.rpt_fwd_count(plan, 'Anser Bloom Consumer', false) <> 1 OR pg_temp.rpt_fwd_count(plan, 'RPT Local Bloom Consumer', true) <> 1 OR pg_temp.rpt_fwd_count(plan, 'Anser Bloom Consumer', true) <> 0 OR (SELECT sum((n->>'Rows Removed by Local Bloom Filter')::float8) @@ -192,18 +193,7 @@ BEGIN RAISE EXCEPTION 'local chain shape or effect missing (optimizer=%): %', opt, plan; END IF; - -- forward_apply=all: the chain's main-plan scans consume too, so the - -- local hop is published through Anser instead. - PERFORM set_config('rpt.forward_apply', 'all', true); - dump := rpt_transfer_plan(flake_local); - plan := pg_temp.rpt_fwd_plan(flake_local, false); - IF dump LIKE '%in-process%' OR - pg_temp.rpt_fwd_count(plan, 'Anser Bloom Consumer', false) < 2 OR - pg_temp.rpt_fwd_count(plan, 'RPT Local Bloom Consumer', true) <> 0 THEN - RAISE EXCEPTION 'forward_apply=all shape wrong (optimizer=%): % %', opt, dump, plan; - END IF; - - -- Results never change, under both apply modes: star, chains, empty + -- Results never change: star, chains, empty -- filters, NULL keys, a volatile qual (dropped whole in the reducer), -- an empty dimension, a preserved side. FOREACH q IN ARRAY ARRAY[ @@ -225,14 +215,10 @@ BEGIN 'SELECT d.k, count(*) FROM rpt_fwd_fact f JOIN rpt_fwd_dl d ON f.d1 = d.k JOIN rpt_fwd_sub u ON d.s = u.k WHERE u.c IN (1, 7) GROUP BY d.k' ] LOOP - FOREACH mode IN ARRAY ARRAY['root', 'all'] LOOP - PERFORM set_config('rpt.forward_apply', mode, true); - IF pg_temp.rpt_fwd_result(q, true) IS DISTINCT FROM pg_temp.rpt_fwd_result(q, false) THEN - RAISE EXCEPTION 'results differ with RPT (optimizer=%, apply=%): %', opt, mode, q; - END IF; - END LOOP; + IF pg_temp.rpt_fwd_result(q, true) IS DISTINCT FROM pg_temp.rpt_fwd_result(q, false) THEN + RAISE EXCEPTION 'results differ with RPT (optimizer=%): %', opt, q; + END IF; END LOOP; - PERFORM set_config('rpt.forward_apply', 'root', true); -- A prepared statement keeps its reducer and its answer. EXECUTE 'PREPARE rpt_fwd_prep AS ' || star; @@ -283,12 +269,12 @@ BEGIN IF rpt_transfer_plan(q) NOT LIKE '%dropped in the reducer (not provably error-free)%' THEN RAISE EXCEPTION 'leakproof user function accepted by reducer (optimizer=%)', opt; END IF; - FOREACH mode IN ARRAY ARRAY['0.35', '1'] LOOP - PERFORM set_config('rpt.forward_max_selectivity', mode, true); + FOREACH selectivity_limit IN ARRAY ARRAY['0.35', '1'] LOOP + PERFORM set_config('rpt.forward_max_selectivity', selectivity_limit, true); -- The planner keeps the LIMIT 0 join, so with lambda off the -- reducer really runs (GPORCA may fold the query away instead). q := 'SELECT f.id FROM rpt_fwd_fact f JOIN rpt_fwd_d1 d1 ON f.d1 = d1.k WHERE 100 / d1.a > 1 LIMIT 0'; - IF mode = '1' AND opt = 'off' AND rpt_transfer_plan(q) NOT LIKE '%decision=inject%' THEN + IF selectivity_limit = '1' AND opt = 'off' AND rpt_transfer_plan(q) NOT LIKE '%decision=inject%' THEN RAISE EXCEPTION 'LIMIT 0 reducer not built with lambda off: %', rpt_transfer_plan(q); END IF; FOREACH q IN ARRAY ARRAY[ diff --git a/gpcontrib/robust_predicate_transfer/include/rpt.h b/gpcontrib/robust_predicate_transfer/include/rpt.h index c8795644eb2..4d6a9bf1782 100644 --- a/gpcontrib/robust_predicate_transfer/include/rpt.h +++ b/gpcontrib/robust_predicate_transfer/include/rpt.h @@ -321,7 +321,6 @@ struct RptForwardParams double min_root_rows = 0; /* root scan plan_rows below this: no reducer */ double max_selectivity = 1; /* lambda; >= 1 disables the check */ std::map main_consumer_processes; /* rtindex -> main scan width */ - bool apply_all = false; /* chain scans of the main plan consume too */ }; /* @@ -399,16 +398,12 @@ extern PlannedStmt *RptInject(PlannedStmt *stmt, bool materialize, /* Forward reducer GUCs (rptinit.cpp). */ extern bool rpt_forward; -extern int rpt_forward_apply; /* RPT_FORWARD_APPLY_* */ extern double rpt_forward_ms_per_mib_part; extern double rpt_delivery_ms_per_mib; extern double rpt_forward_max_exchange_ms; extern double rpt_forward_min_root_rows; extern double rpt_forward_max_selectivity; -#define RPT_FORWARD_APPLY_ROOT 0 -#define RPT_FORWARD_APPLY_ALL 1 - /* rptregion.cpp: extract the join graph from the finished statement. */ extern void RptCollectGraph(PlannedStmt *stmt, RptTransferMgr *mgr); diff --git a/gpcontrib/robust_predicate_transfer/scripts/collect_star_example.sql b/gpcontrib/robust_predicate_transfer/scripts/collect_star_example.sql new file mode 100644 index 00000000000..38ff36dd184 --- /dev/null +++ b/gpcontrib/robust_predicate_transfer/scripts/collect_star_example.sql @@ -0,0 +1,437 @@ +--------------------------------------------------------------------------- +-- +-- Licensed to the Apache Software Foundation (ASF) under one +-- or more contributor license agreements. See the NOTICE file +-- distributed with this work for additional information +-- regarding copyright ownership. The ASF licenses this file +-- to you under the Apache License, Version 2.0 (the +-- "License"); you may not use this file except in compliance +-- with the License. You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, +-- software distributed under the License is distributed on an +-- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +-- KIND, either express or implied. See the License for the +-- specific language governing permissions and limitations +-- under the License. +-- +-- Run from an empty output directory; see the README's worked-example guide. +-- Use a dedicated database. Existing fixture schemas are never overwritten. +\set ON_ERROR_STOP on +\pset pager off +\pset format unaligned +\pset tuples_only on +\timing off +\if :{?fact_rows} +\else +\set fact_rows 1000000 +\endif +\if :{?samples} +\else +\set samples 7 +\endif +\if :{?warmups} +\else +\set warmups 2 +\endif +SELECT :'fact_rows'::bigint >= 100000 AND :'samples'::int >= 3 + AND :'warmups'::int >= 1 AS valid_input \gset +\if :valid_input +\else +\echo 'Require fact_rows >= 100000, samples >= 3, warmups >= 1.' +DO $$ BEGIN RAISE EXCEPTION 'invalid collection settings or preload configuration'; END $$; +\endif +SELECT coalesce(current_setting('anser.enable', true) = 'on' + AND array_position(libraries, 'anser') + < array_position(libraries, 'robust_predicate_transfer'), false) AS ready +FROM (SELECT string_to_array(replace(current_setting('shared_preload_libraries'), ' ', ''), ',') + AS libraries) preload \gset +\if :ready +\else +\echo 'Preload anser,robust_predicate_transfer in that order and enable Anser; restart first.' +DO $$ BEGIN RAISE EXCEPTION 'invalid collection settings or preload configuration'; END $$; +\endif +SET application_name = 'rpt_readme_star_capture'; +SET statement_timeout = '120s'; +SET anser.runtime_filter = off; +SET anser.debug = off; +SET rpt.enable = off; +SET rpt.debug = off; +\if :{?exchange_budget_ms} +SET rpt.forward_max_exchange_ms = :'exchange_budget_ms'; +\endif +CREATE EXTENSION IF NOT EXISTS robust_predicate_transfer WITH SCHEMA public; +\echo RPT_STAR setup +BEGIN; +CREATE SCHEMA rpt_readme_star; +CREATE TABLE rpt_readme_star.fact ( + sale_id int, customer_key int, product_key int, store_key int, amount_cents int +) DISTRIBUTED RANDOMLY; +CREATE TABLE rpt_readme_star.customer (k int, region int) DISTRIBUTED RANDOMLY; +CREATE TABLE rpt_readme_star.product (k int, category int) DISTRIBUTED RANDOMLY; +CREATE TABLE rpt_readme_star.store (k int, zone int) DISTRIBUTED RANDOMLY; +INSERT INTO rpt_readme_star.customer SELECT k, k % 10 FROM generate_series(0, 1008) k; +INSERT INTO rpt_readme_star.product SELECT k, k % 10 FROM generate_series(0, 502) k; +INSERT INTO rpt_readme_star.store SELECT k, k % 10 FROM generate_series(0, 210) k; +INSERT INTO rpt_readme_star.fact + SELECT i, (i - 1) % 1009, (i - 1) % 503, (i - 1) % 211, i % 10000 + FROM generate_series(1, :'fact_rows'::int) i; +ANALYZE rpt_readme_star.fact; +ANALYZE rpt_readme_star.customer; +ANALYZE rpt_readme_star.product; +ANALYZE rpt_readme_star.store; +COMMIT; +SELECT $query$ +SELECT count(*) AS matched_rows, + coalesce(sum(f.amount_cents), 0) AS total_amount_cents +FROM rpt_readme_star.fact f +JOIN rpt_readme_star.customer c ON f.customer_key = c.k +JOIN rpt_readme_star.product p ON f.product_key = p.k +JOIN rpt_readme_star.store s ON f.store_key = s.k +WHERE c.region < 2 AND p.category < 2 AND s.zone < 2 +$query$ AS star_query \gset +\o query.sql +\qecho :star_query ; +\o +\pset tuples_only off +\o environment.txt +SELECT version(); +SELECT pg_backend_pid() AS backend_pid; +SELECT count(*) FILTER (WHERE content >= 0 AND role = 'p') AS primaries, + count(DISTINCT hostname) FILTER (WHERE content >= 0 AND role = 'p') AS primary_hosts +FROM gp_segment_configuration; +SELECT content, role, status, mode FROM gp_segment_configuration WHERE role = 'p' ORDER BY content; +SELECT extname, extversion FROM pg_extension +WHERE extname IN ('robust_predicate_transfer', 'anser') ORDER BY extname; +SELECT name, setting, unit, source FROM pg_settings +WHERE name LIKE 'rpt.%' OR name LIKE 'anser.%' OR name IN ( + 'shared_preload_libraries', 'optimizer', 'optimizer_join_order', + 'gp_enable_hashjoin_size_heuristic', 'gp_enable_runtime_filter', + 'gp_enable_runtime_filter_pushdown', 'work_mem', 'statement_mem', + 'max_parallel_workers_per_gather', 'max_parallel_workers', + 'gp_resource_manager', 'gp_interconnect_type', 'statement_timeout' +) ORDER BY name; +\o +\pset format csv +\pset tuples_only off +\pset footer off +\o distribution.csv +SELECT c.relname, p.policytype, p.numsegments, p.distkey::text, p.distclass::text +FROM gp_distribution_policy p JOIN pg_class c ON c.oid = p.localoid +WHERE c.relnamespace = 'rpt_readme_star'::regnamespace ORDER BY c.relname; +\o +\o statistics.csv +SELECT tablename, attname, null_frac, avg_width, n_distinct, + most_common_vals::text, most_common_freqs::text +FROM pg_stats WHERE schemaname = 'rpt_readme_star' ORDER BY tablename, attname; +\o +\o cardinalities.csv +SELECT 'customer' AS relation, count(*) AS rows, count(*) FILTER (WHERE region < 2) AS selected, + count(DISTINCT k) AS key_ndv, count(DISTINCT k) FILTER (WHERE region < 2) AS selected_ndv +FROM rpt_readme_star.customer +UNION ALL +SELECT 'product', count(*), count(*) FILTER (WHERE category < 2), + count(DISTINCT k), count(DISTINCT k) FILTER (WHERE category < 2) FROM rpt_readme_star.product +UNION ALL +SELECT 'store', count(*), count(*) FILTER (WHERE zone < 2), + count(DISTINCT k), count(DISTINCT k) FILTER (WHERE zone < 2) FROM rpt_readme_star.store; +\o +\o fact-cardinalities.csv +SELECT count(*) AS rows, count(DISTINCT customer_key) AS customer_ndv, + count(DISTINCT product_key) AS product_ndv, count(DISTINCT store_key) AS store_ndv +FROM rpt_readme_star.fact; +\o +\pset format unaligned +\pset tuples_only on + +\echo RPT_STAR optimizer=off +SET optimizer = off; +\echo RPT_STAR capture=off-baseline +SET rpt.enable = off; +SET rpt.forward = on; +SET rpt.backward = on; +:star_query \gset got_ +\set expected_matched_rows :got_matched_rows +\set expected_total_amount_cents :got_total_amount_cents +SELECT :got_matched_rows::bigint = :expected_matched_rows::bigint + AND :got_total_amount_cents::bigint = :expected_total_amount_cents::bigint AS answer_matches \gset +\if :answer_matches +\else +\echo 'Result mismatch; retain all artifacts and stop.' +DO $$ BEGIN RAISE EXCEPTION 'star example result mismatch'; END $$; +\endif +\pset format csv +\pset tuples_only off +\o off-baseline.result.csv +SELECT 'off' AS optimizer_requested, 'baseline' AS arm, + :got_matched_rows::bigint AS matched_rows, + :got_total_amount_cents::bigint AS total_amount_cents; +\o +\pset format unaligned +\pset tuples_only on +\o off-baseline.explain.txt +EXPLAIN (ANALYZE, VERBOSE, BUFFERS, SETTINGS, SUMMARY) :star_query; +\o +\o off-baseline.explain.json +EXPLAIN (ANALYZE, VERBOSE, BUFFERS, SETTINGS, SUMMARY, FORMAT JSON) :star_query; +\o +\echo RPT_STAR capture=off-forward +SET rpt.enable = on; +SET rpt.forward = on; +SET rpt.backward = off; +:star_query \gset got_ +SELECT :got_matched_rows::bigint = :expected_matched_rows::bigint + AND :got_total_amount_cents::bigint = :expected_total_amount_cents::bigint AS answer_matches \gset +\if :answer_matches +\else +\echo 'Result mismatch; retain all artifacts and stop.' +DO $$ BEGIN RAISE EXCEPTION 'star example result mismatch'; END $$; +\endif +\pset format csv +\pset tuples_only off +\o off-forward.result.csv +SELECT 'off' AS optimizer_requested, 'forward' AS arm, + :got_matched_rows::bigint AS matched_rows, + :got_total_amount_cents::bigint AS total_amount_cents; +\o +\pset format unaligned +\pset tuples_only on +\o off-forward.transfer.txt +SELECT public.rpt_transfer_plan(:'star_query'); +\o +\o off-forward.explain.txt +EXPLAIN (ANALYZE, VERBOSE, BUFFERS, SETTINGS, SUMMARY) :star_query; +\o +\o off-forward.explain.json +EXPLAIN (ANALYZE, VERBOSE, BUFFERS, SETTINGS, SUMMARY, FORMAT JSON) :star_query; +\o +\echo RPT_STAR capture=off-backward +SET rpt.enable = on; +SET rpt.forward = off; +SET rpt.backward = on; +:star_query \gset got_ +SELECT :got_matched_rows::bigint = :expected_matched_rows::bigint + AND :got_total_amount_cents::bigint = :expected_total_amount_cents::bigint AS answer_matches \gset +\if :answer_matches +\else +\echo 'Result mismatch; retain all artifacts and stop.' +DO $$ BEGIN RAISE EXCEPTION 'star example result mismatch'; END $$; +\endif +\pset format csv +\pset tuples_only off +\o off-backward.result.csv +SELECT 'off' AS optimizer_requested, 'backward' AS arm, + :got_matched_rows::bigint AS matched_rows, + :got_total_amount_cents::bigint AS total_amount_cents; +\o +\pset format unaligned +\pset tuples_only on +\o off-backward.transfer.txt +SELECT public.rpt_transfer_plan(:'star_query'); +\o +\o off-backward.explain.txt +EXPLAIN (ANALYZE, VERBOSE, BUFFERS, SETTINGS, SUMMARY) :star_query; +\o +\o off-backward.explain.json +EXPLAIN (ANALYZE, VERBOSE, BUFFERS, SETTINGS, SUMMARY, FORMAT JSON) :star_query; +\o +\echo RPT_STAR capture=off-full +SET rpt.enable = on; +SET rpt.forward = on; +SET rpt.backward = on; +:star_query \gset got_ +SELECT :got_matched_rows::bigint = :expected_matched_rows::bigint + AND :got_total_amount_cents::bigint = :expected_total_amount_cents::bigint AS answer_matches \gset +\if :answer_matches +\else +\echo 'Result mismatch; retain all artifacts and stop.' +DO $$ BEGIN RAISE EXCEPTION 'star example result mismatch'; END $$; +\endif +\pset format csv +\pset tuples_only off +\o off-full.result.csv +SELECT 'off' AS optimizer_requested, 'full' AS arm, + :got_matched_rows::bigint AS matched_rows, + :got_total_amount_cents::bigint AS total_amount_cents; +\o +\pset format unaligned +\pset tuples_only on +\o off-full.transfer.txt +SELECT public.rpt_transfer_plan(:'star_query'); +\o +\o off-full.explain.txt +EXPLAIN (ANALYZE, VERBOSE, BUFFERS, SETTINGS, SUMMARY) :star_query; +\o +\o off-full.explain.json +EXPLAIN (ANALYZE, VERBOSE, BUFFERS, SETTINGS, SUMMARY, FORMAT JSON) :star_query; +\o +\echo RPT_STAR timings optimizer=off +\timing on +-- gexec sends each column as a separate statement: settings, marker, query. +-- Only the Time line following the two-column aggregate result is a query sample. +SELECT format('SET rpt.enable = %L', enabled), + format('SET rpt.forward = %L', forward), + format('SET rpt.backward = %L', backward), + format('SELECT %L', format('RPT_SAMPLE optimizer=off arm=%s phase=%s run=%s', arm, + CASE WHEN iteration < :'warmups'::int THEN 'warmup' ELSE 'sample' END, + CASE WHEN iteration < :'warmups'::int THEN iteration + 1 + ELSE iteration - :'warmups'::int + 1 END)), + :'star_query' +FROM generate_series(0, :'warmups'::int + :'samples'::int - 1) iteration +CROSS JOIN (VALUES + (0, 'baseline', 'off', 'on', 'on'), + (1, 'forward', 'on', 'on', 'off'), + (2, 'backward', 'on', 'off', 'on'), + (3, 'full', 'on', 'on', 'on') +) arms(arm_index, arm, enabled, forward, backward) +ORDER BY iteration, (arm_index + iteration) % 4 +\gexec +\timing off + +\echo RPT_STAR optimizer=on +SET optimizer = on; +\echo RPT_STAR capture=on-baseline +SET rpt.enable = off; +SET rpt.forward = on; +SET rpt.backward = on; +:star_query \gset got_ +SELECT :got_matched_rows::bigint = :expected_matched_rows::bigint + AND :got_total_amount_cents::bigint = :expected_total_amount_cents::bigint AS answer_matches \gset +\if :answer_matches +\else +\echo 'Result mismatch; retain all artifacts and stop.' +DO $$ BEGIN RAISE EXCEPTION 'star example result mismatch'; END $$; +\endif +\pset format csv +\pset tuples_only off +\o on-baseline.result.csv +SELECT 'on' AS optimizer_requested, 'baseline' AS arm, + :got_matched_rows::bigint AS matched_rows, + :got_total_amount_cents::bigint AS total_amount_cents; +\o +\pset format unaligned +\pset tuples_only on +\o on-baseline.explain.txt +EXPLAIN (ANALYZE, VERBOSE, BUFFERS, SETTINGS, SUMMARY) :star_query; +\o +\o on-baseline.explain.json +EXPLAIN (ANALYZE, VERBOSE, BUFFERS, SETTINGS, SUMMARY, FORMAT JSON) :star_query; +\o +\echo RPT_STAR capture=on-forward +SET rpt.enable = on; +SET rpt.forward = on; +SET rpt.backward = off; +:star_query \gset got_ +SELECT :got_matched_rows::bigint = :expected_matched_rows::bigint + AND :got_total_amount_cents::bigint = :expected_total_amount_cents::bigint AS answer_matches \gset +\if :answer_matches +\else +\echo 'Result mismatch; retain all artifacts and stop.' +DO $$ BEGIN RAISE EXCEPTION 'star example result mismatch'; END $$; +\endif +\pset format csv +\pset tuples_only off +\o on-forward.result.csv +SELECT 'on' AS optimizer_requested, 'forward' AS arm, + :got_matched_rows::bigint AS matched_rows, + :got_total_amount_cents::bigint AS total_amount_cents; +\o +\pset format unaligned +\pset tuples_only on +\o on-forward.transfer.txt +SELECT public.rpt_transfer_plan(:'star_query'); +\o +\o on-forward.explain.txt +EXPLAIN (ANALYZE, VERBOSE, BUFFERS, SETTINGS, SUMMARY) :star_query; +\o +\o on-forward.explain.json +EXPLAIN (ANALYZE, VERBOSE, BUFFERS, SETTINGS, SUMMARY, FORMAT JSON) :star_query; +\o +\echo RPT_STAR capture=on-backward +SET rpt.enable = on; +SET rpt.forward = off; +SET rpt.backward = on; +:star_query \gset got_ +SELECT :got_matched_rows::bigint = :expected_matched_rows::bigint + AND :got_total_amount_cents::bigint = :expected_total_amount_cents::bigint AS answer_matches \gset +\if :answer_matches +\else +\echo 'Result mismatch; retain all artifacts and stop.' +DO $$ BEGIN RAISE EXCEPTION 'star example result mismatch'; END $$; +\endif +\pset format csv +\pset tuples_only off +\o on-backward.result.csv +SELECT 'on' AS optimizer_requested, 'backward' AS arm, + :got_matched_rows::bigint AS matched_rows, + :got_total_amount_cents::bigint AS total_amount_cents; +\o +\pset format unaligned +\pset tuples_only on +\o on-backward.transfer.txt +SELECT public.rpt_transfer_plan(:'star_query'); +\o +\o on-backward.explain.txt +EXPLAIN (ANALYZE, VERBOSE, BUFFERS, SETTINGS, SUMMARY) :star_query; +\o +\o on-backward.explain.json +EXPLAIN (ANALYZE, VERBOSE, BUFFERS, SETTINGS, SUMMARY, FORMAT JSON) :star_query; +\o +\echo RPT_STAR capture=on-full +SET rpt.enable = on; +SET rpt.forward = on; +SET rpt.backward = on; +:star_query \gset got_ +SELECT :got_matched_rows::bigint = :expected_matched_rows::bigint + AND :got_total_amount_cents::bigint = :expected_total_amount_cents::bigint AS answer_matches \gset +\if :answer_matches +\else +\echo 'Result mismatch; retain all artifacts and stop.' +DO $$ BEGIN RAISE EXCEPTION 'star example result mismatch'; END $$; +\endif +\pset format csv +\pset tuples_only off +\o on-full.result.csv +SELECT 'on' AS optimizer_requested, 'full' AS arm, + :got_matched_rows::bigint AS matched_rows, + :got_total_amount_cents::bigint AS total_amount_cents; +\o +\pset format unaligned +\pset tuples_only on +\o on-full.transfer.txt +SELECT public.rpt_transfer_plan(:'star_query'); +\o +\o on-full.explain.txt +EXPLAIN (ANALYZE, VERBOSE, BUFFERS, SETTINGS, SUMMARY) :star_query; +\o +\o on-full.explain.json +EXPLAIN (ANALYZE, VERBOSE, BUFFERS, SETTINGS, SUMMARY, FORMAT JSON) :star_query; +\o +\echo RPT_STAR timings optimizer=on +\timing on +-- gexec sends each column as a separate statement: settings, marker, query. +-- Only the Time line following the two-column aggregate result is a query sample. +SELECT format('SET rpt.enable = %L', enabled), + format('SET rpt.forward = %L', forward), + format('SET rpt.backward = %L', backward), + format('SELECT %L', format('RPT_SAMPLE optimizer=on arm=%s phase=%s run=%s', arm, + CASE WHEN iteration < :'warmups'::int THEN 'warmup' ELSE 'sample' END, + CASE WHEN iteration < :'warmups'::int THEN iteration + 1 + ELSE iteration - :'warmups'::int + 1 END)), + :'star_query' +FROM generate_series(0, :'warmups'::int + :'samples'::int - 1) iteration +CROSS JOIN (VALUES + (0, 'baseline', 'off', 'on', 'on'), + (1, 'forward', 'on', 'on', 'off'), + (2, 'backward', 'on', 'off', 'on'), + (3, 'full', 'on', 'on', 'on') +) arms(arm_index, arm, enabled, forward, backward) +ORDER BY iteration, (arm_index + iteration) % 4 +\gexec +\timing off + +SET rpt.enable = off; +\echo RPT_STAR complete +-- The fixture remains for inspection. Drop the dedicated database when finished. diff --git a/gpcontrib/robust_predicate_transfer/scripts/measure_initplan.py b/gpcontrib/robust_predicate_transfer/scripts/measure_initplan.py deleted file mode 100644 index c9c44cf0dfc..00000000000 --- a/gpcontrib/robust_predicate_transfer/scripts/measure_initplan.py +++ /dev/null @@ -1,58 +0,0 @@ -#!/usr/bin/env python3 -# Licensed to the Apache Software Foundation (ASF) under one -# or more contributor license agreements. See the NOTICE file -# distributed with this work for additional information -# regarding copyright ownership. The ASF licenses this file -# to you under the Apache License, Version 2.0 (the -# "License"); you may not use this file except in compliance -# with the License. You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, -# software distributed under the License is distributed on an -# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -# KIND, either express or implied. See the License for the -# specific language governing permissions and limitations -# under the License. - -"""Emit a psql workload comparing the exact empty reducer InitPlan off/on.""" -import argparse - -parser = argparse.ArgumentParser(description=__doc__) -parser.add_argument("--samples", type=int, default=100) -parser.add_argument("--warmups", type=int, default=10) -args = parser.parse_args() -if args.samples < 1 or args.warmups < 0: - parser.error("samples must be positive and warmups nonnegative") - -print(r"\set ON_ERROR_STOP on") -print("SET anser.runtime_filter = off;") -print("SET anser.debug = off;") -print("SET rpt.enable = off;") -print("SET rpt.debug = off;") -print("SET rpt.debug_noop_initplan = off;") -print("SET statement_timeout = '120s';") -print("CREATE TEMP TABLE rpt_initplan_bench (k int) DISTRIBUTED RANDOMLY;") -print("INSERT INTO rpt_initplan_bench SELECT i FROM generate_series(1, 3000) g(i);") -print("ANALYZE rpt_initplan_bench;") -query = "SELECT count(*) FROM rpt_initplan_bench" -for optimizer in ("off", "on"): - print(f"SET optimizer = {optimizer};") - print("SET rpt.debug_noop_initplan = on;") - print(f"EXPLAIN (COSTS OFF) {query};") - # Also exercise a pre-existing InitPlan and its PARAM_EXEC numbering. - print("SELECT count(*) FROM rpt_initplan_bench " - "WHERE k <= (SELECT max(k) FROM rpt_initplan_bench);") - print(r"\timing on") - for phase, count in (("warmup", args.warmups), ("measured", args.samples)): - for index in range(1, count + 1): - # Alternate which arm goes first to reduce systematic drift. - arms = ("off", "on") if index % 2 else ("on", "off") - for arm in arms: - print(f"SET rpt.debug_noop_initplan = {arm};") - print(f"\\echo SAMPLE initplan {optimizer} {phase} {index} {arm}") - print(f"EXPLAIN (ANALYZE, TIMING OFF, SUMMARY ON) {query};") - print(r"\timing off") -print("SET rpt.debug_noop_initplan = off;") -print("DROP TABLE rpt_initplan_bench;") diff --git a/gpcontrib/robust_predicate_transfer/sql/rpt_backward.sql b/gpcontrib/robust_predicate_transfer/sql/rpt_backward.sql index 9371e8b8a31..64fb1383841 100644 --- a/gpcontrib/robust_predicate_transfer/sql/rpt_backward.sql +++ b/gpcontrib/robust_predicate_transfer/sql/rpt_backward.sql @@ -112,7 +112,6 @@ $$; DO $$ DECLARE opt text; - mode text; q text; dump text; plan jsonb; @@ -129,7 +128,6 @@ BEGIN PERFORM set_config('rpt.forward_max_exchange_ms', '-1', true); FOREACH opt IN ARRAY ARRAY['off', 'on'] LOOP PERFORM set_config('optimizer', opt, true); - PERFORM set_config('rpt.forward_apply', 'root', true); PERFORM set_config('rpt.forward', 'off', true); PERFORM pg_temp.rpt_bw_expect(plain, 'm', 'inject'); -- Forward skipping must not suppress an unfiltered root's broadcast. @@ -235,15 +233,11 @@ BEGIN -- LIMIT 1 has no SQL ordering guarantee; compare a count instead. IF q LIKE '%LIMIT 1' THEN q := 'SELECT count(*) FROM (' || q || ') limited'; END IF; baseline := pg_temp.rpt_bw_result(q, false, false); - FOREACH mode IN ARRAY ARRAY['root', 'all'] LOOP - PERFORM set_config('rpt.forward_apply', mode, true); - IF pg_temp.rpt_bw_result(q, true, false) IS DISTINCT FROM baseline OR - pg_temp.rpt_bw_result(q, true, true) IS DISTINCT FROM baseline THEN - RAISE EXCEPTION 'backward result differs (optimizer=%, apply=%): %', opt, mode, q; - END IF; - END LOOP; + IF pg_temp.rpt_bw_result(q, true, false) IS DISTINCT FROM baseline OR + pg_temp.rpt_bw_result(q, true, true) IS DISTINCT FROM baseline THEN + RAISE EXCEPTION 'backward result differs (optimizer=%): %', opt, q; + END IF; END LOOP; - PERFORM set_config('rpt.forward_apply', 'root', true); q := 'SELECT count(*)::text || ''/'' || sum(length(m.pad || r.k::text))::text FROM rpt_bw_member m JOIN rpt_bw_root r ON m.k = r.k'; PERFORM pg_temp.rpt_bw_expect(q, 'm', 'inject'); diff --git a/gpcontrib/robust_predicate_transfer/sql/rpt_forward.sql b/gpcontrib/robust_predicate_transfer/sql/rpt_forward.sql index 6db0b165b76..f57fb56709a 100644 --- a/gpcontrib/robust_predicate_transfer/sql/rpt_forward.sql +++ b/gpcontrib/robust_predicate_transfer/sql/rpt_forward.sql @@ -112,7 +112,7 @@ DO $$ DECLARE opt text; q text; - mode text; + selectivity_limit text; dump text; plan jsonb; prep_n bigint; @@ -135,7 +135,6 @@ BEGIN PERFORM set_config('rpt.forward_max_exchange_ms', '-1', true); FOREACH opt IN ARRAY ARRAY['off', 'on'] LOOP PERFORM set_config('optimizer', opt, true); - PERFORM set_config('rpt.forward_apply', 'root', true); -- Shape: a reducer InitPlan with producers, a consumer in the main plan. dump := rpt_transfer_plan(star); @@ -172,6 +171,7 @@ BEGIN END IF; plan := pg_temp.rpt_fwd_plan(flake_global, true); IF pg_temp.rpt_fwd_count(plan, 'Anser Bloom Consumer', true) = 0 OR + pg_temp.rpt_fwd_count(plan, 'Anser Bloom Consumer', false) <> 1 OR pg_temp.rpt_fwd_count(plan, 'Anser Bloom Producer', true) < 2 OR pg_temp.rpt_fwd_removed(plan) <= 0 THEN RAISE EXCEPTION 'global chain shape or effect missing (optimizer=%): %', opt, plan; @@ -184,6 +184,7 @@ BEGIN END IF; plan := pg_temp.rpt_fwd_plan(flake_local, true); IF pg_temp.rpt_fwd_count(plan, 'RPT Local Bloom Producer', true) <> 1 OR + pg_temp.rpt_fwd_count(plan, 'Anser Bloom Consumer', false) <> 1 OR pg_temp.rpt_fwd_count(plan, 'RPT Local Bloom Consumer', true) <> 1 OR pg_temp.rpt_fwd_count(plan, 'Anser Bloom Consumer', true) <> 0 OR (SELECT sum((n->>'Rows Removed by Local Bloom Filter')::float8) @@ -192,18 +193,7 @@ BEGIN RAISE EXCEPTION 'local chain shape or effect missing (optimizer=%): %', opt, plan; END IF; - -- forward_apply=all: the chain's main-plan scans consume too, so the - -- local hop is published through Anser instead. - PERFORM set_config('rpt.forward_apply', 'all', true); - dump := rpt_transfer_plan(flake_local); - plan := pg_temp.rpt_fwd_plan(flake_local, false); - IF dump LIKE '%in-process%' OR - pg_temp.rpt_fwd_count(plan, 'Anser Bloom Consumer', false) < 2 OR - pg_temp.rpt_fwd_count(plan, 'RPT Local Bloom Consumer', true) <> 0 THEN - RAISE EXCEPTION 'forward_apply=all shape wrong (optimizer=%): % %', opt, dump, plan; - END IF; - - -- Results never change, under both apply modes: star, chains, empty + -- Results never change: star, chains, empty -- filters, NULL keys, a volatile qual (dropped whole in the reducer), -- an empty dimension, a preserved side. FOREACH q IN ARRAY ARRAY[ @@ -225,14 +215,10 @@ BEGIN 'SELECT d.k, count(*) FROM rpt_fwd_fact f JOIN rpt_fwd_dl d ON f.d1 = d.k JOIN rpt_fwd_sub u ON d.s = u.k WHERE u.c IN (1, 7) GROUP BY d.k' ] LOOP - FOREACH mode IN ARRAY ARRAY['root', 'all'] LOOP - PERFORM set_config('rpt.forward_apply', mode, true); - IF pg_temp.rpt_fwd_result(q, true) IS DISTINCT FROM pg_temp.rpt_fwd_result(q, false) THEN - RAISE EXCEPTION 'results differ with RPT (optimizer=%, apply=%): %', opt, mode, q; - END IF; - END LOOP; + IF pg_temp.rpt_fwd_result(q, true) IS DISTINCT FROM pg_temp.rpt_fwd_result(q, false) THEN + RAISE EXCEPTION 'results differ with RPT (optimizer=%): %', opt, q; + END IF; END LOOP; - PERFORM set_config('rpt.forward_apply', 'root', true); -- A prepared statement keeps its reducer and its answer. EXECUTE 'PREPARE rpt_fwd_prep AS ' || star; @@ -283,12 +269,12 @@ BEGIN IF rpt_transfer_plan(q) NOT LIKE '%dropped in the reducer (not provably error-free)%' THEN RAISE EXCEPTION 'leakproof user function accepted by reducer (optimizer=%)', opt; END IF; - FOREACH mode IN ARRAY ARRAY['0.35', '1'] LOOP - PERFORM set_config('rpt.forward_max_selectivity', mode, true); + FOREACH selectivity_limit IN ARRAY ARRAY['0.35', '1'] LOOP + PERFORM set_config('rpt.forward_max_selectivity', selectivity_limit, true); -- The planner keeps the LIMIT 0 join, so with lambda off the -- reducer really runs (GPORCA may fold the query away instead). q := 'SELECT f.id FROM rpt_fwd_fact f JOIN rpt_fwd_d1 d1 ON f.d1 = d1.k WHERE 100 / d1.a > 1 LIMIT 0'; - IF mode = '1' AND opt = 'off' AND rpt_transfer_plan(q) NOT LIKE '%decision=inject%' THEN + IF selectivity_limit = '1' AND opt = 'off' AND rpt_transfer_plan(q) NOT LIKE '%decision=inject%' THEN RAISE EXCEPTION 'LIMIT 0 reducer not built with lambda off: %', rpt_transfer_plan(q); END IF; FOREACH q IN ARRAY ARRAY[ diff --git a/gpcontrib/robust_predicate_transfer/src/rptforward.cpp b/gpcontrib/robust_predicate_transfer/src/rptforward.cpp index b7d30dbf1d8..3a01ad28df8 100644 --- a/gpcontrib/robust_predicate_transfer/src/rptforward.cpp +++ b/gpcontrib/robust_predicate_transfer/src/rptforward.cpp @@ -603,11 +603,6 @@ RptInjectForward(const RptTransferMgr &forward, if (!rpt_apply_in_main(forward.vertices[forward.vertex_index_by_rtindex.at(comp.root_rtindex)], comp.channels, &state, params.width, false)) RptPgCall([]() { elog(ERROR, "RPT forward: root relink failed after preflight"); }); - - if (params.apply_all) - for (int rtindex : comp.chain_rtindexes) - (void) rpt_apply_in_main(forward.vertices[forward.vertex_index_by_rtindex.at(rtindex)], - comp.channels, &state, params.width, false); } return children; @@ -669,7 +664,6 @@ RptInject(PlannedStmt *stmt, bool materialize, std::string *trace) params.max_exchange_ms = rpt_forward_max_exchange_ms; params.min_root_rows = rpt_forward_min_root_rows; params.max_selectivity = rpt_forward_max_selectivity; - params.apply_all = (rpt_forward_apply == RPT_FORWARD_APPLY_ALL); for (const auto &vertex : forward.vertices) { auto found = scan_slices.find(vertex.scan); diff --git a/gpcontrib/robust_predicate_transfer/src/rptforwardplan.cpp b/gpcontrib/robust_predicate_transfer/src/rptforwardplan.cpp index bf7824a236a..c9718454924 100644 --- a/gpcontrib/robust_predicate_transfer/src/rptforwardplan.cpp +++ b/gpcontrib/robust_predicate_transfer/src/rptforwardplan.cpp @@ -137,7 +137,7 @@ RptSelectForward(const RptTransferMgr &forward, const RptForwardParams ¶ms, ch.source_key = edge.left_keys[0]; /* out-edge: left = this side */ ch.target_key = edge.right_keys[0]; ch.local = edge.local; - ch.in_process = ch.local && ch.target_rtindex != root_rtindex && !params.apply_all; + ch.in_process = ch.local && ch.target_rtindex != root_rtindex; /* Sized for what the reducer scan inserts: its rows after its * own quals -- all of them when the quals were dropped. */ @@ -150,14 +150,16 @@ RptSelectForward(const RptTransferMgr &forward, const RptForwardParams ¶ms, break; } if (!ch.in_process) + { + /* Each hop delivers either to a reducer scan or to the + * root scan in the main plan. */ + int deliveries = params.width; + if (ch.target_rtindex == root_rtindex) { - /* One reducer consumer, plus a main-plan consumer for - * root edges or forward_apply=all. */ - auto processes = params.main_consumer_processes.find(ch.target_rtindex); - int main_width = processes == params.main_consumer_processes.end() - ? params.width : processes->second; - int deliveries = (ch.target_rtindex != root_rtindex ? params.width : 0) + - (ch.target_rtindex == root_rtindex || params.apply_all ? main_width : 0); + auto processes = params.main_consumer_processes.find(root_rtindex); + if (processes != params.main_consumer_processes.end()) + deliveries = processes->second; + } ch.est_cost_ms = (params.ms_per_mib_part * params.width + params.delivery_ms_per_mib * deliveries) * diff --git a/gpcontrib/robust_predicate_transfer/src/rptinit.cpp b/gpcontrib/robust_predicate_transfer/src/rptinit.cpp index 45bf601c574..f17e91477d8 100644 --- a/gpcontrib/robust_predicate_transfer/src/rptinit.cpp +++ b/gpcontrib/robust_predicate_transfer/src/rptinit.cpp @@ -27,8 +27,7 @@ * * The planner hook injects forward and backward filters and logs decisions at * DEBUG1 when rpt.debug is on. rpt_transfer_plan() exposes analysis without - * injecting filters. debug_noop_initplan adds an empty distributed InitPlan - * for overhead measurement. + * injecting filters. * * IDENTIFICATION * gpcontrib/robust_predicate_transfer/src/rptinit.cpp @@ -42,7 +41,6 @@ extern "C" #include "postgres.h" #include "cdb/cdbvars.h" -#include "cdb/cdbutil.h" #include "fmgr.h" #include "miscadmin.h" #include "nodes/pg_list.h" @@ -72,7 +70,6 @@ int rpt_max_backward_consumers = 4; int rpt_max_chain_depth = 4; bool rpt_forward = true; -int rpt_forward_apply = RPT_FORWARD_APPLY_ROOT; /* Measured 2026-11-10 on one host with three primaries. */ double rpt_forward_ms_per_mib_part = 3.5; double rpt_delivery_ms_per_mib = 0.8; @@ -81,14 +78,7 @@ double rpt_forward_min_root_rows = 10000.0; /* The RPT+ paper's lambda (its Algorithm 2 threshold). */ double rpt_forward_max_selectivity = 0.35; -static const struct config_enum_entry rpt_forward_apply_options[] = { - {"root", RPT_FORWARD_APPLY_ROOT, false}, - {"all", RPT_FORWARD_APPLY_ALL, false}, - {NULL, 0, false} -}; - static planner_hook_type prev_planner_hook = NULL; -static bool rpt_debug_noop_initplan = false; /* Set while rpt_transfer_plan() plans its query: the dump must describe the * uninjected plan, so the hook leaves it alone. */ @@ -204,16 +194,10 @@ rpt_define_gucs(void) GUC_GPDB_NEED_SYNC, NULL, NULL, NULL); - DefineCustomBoolVariable("rpt.debug_noop_initplan", - "Adds an empty distributed InitPlan for overhead measurement.", - "QD-only, independent of rpt.enable. No filters are built or applied.", - &rpt_debug_noop_initplan, false, PGC_SUSET, 0, - NULL, NULL, NULL); - DefineCustomBoolVariable("rpt.backward", - "Builds root-to-member backward runtime filters.", + "Developer control for backward runtime-filter injection.", "QD-only; only consumers proven safe to wait are injected.", - &rpt_backward, true, PGC_USERSET, 0, + &rpt_backward, true, PGC_USERSET, GUC_NOT_IN_SAMPLE, NULL, NULL, NULL); DefineCustomIntVariable("rpt.max_backward_consumers", @@ -223,16 +207,9 @@ rpt_define_gucs(void) PGC_USERSET, 0, NULL, NULL, NULL); DefineCustomBoolVariable("rpt.forward", - "Builds the RPT+ forward reducer when rpt.enable is on.", + "Developer control for forward reducer injection.", "QD-only. Needs anser.enable=on and anser.runtime_filter=off.", - &rpt_forward, true, PGC_USERSET, 0, - NULL, NULL, NULL); - - DefineCustomEnumVariable("rpt.forward_apply", - "Which main-plan scans apply forward filters.", - "root: only each chain's root; all: chain tables too.", - &rpt_forward_apply, RPT_FORWARD_APPLY_ROOT, - rpt_forward_apply_options, PGC_USERSET, 0, + &rpt_forward, true, PGC_USERSET, GUC_NOT_IN_SAMPLE, NULL, NULL, NULL); DefineCustomRealVariable("rpt.forward_ms_per_mib_part", @@ -320,85 +297,6 @@ rpt_log_analysis(PlannedStmt *stmt) MemoryContextDelete(analysis_context); } -/* Contain C++ exceptions before returning to the PostgreSQL error handler. */ -static PlannedStmt * -rpt_build_noop_initplan(PlannedStmt *stmt) -{ - bool pg_error = false; - bool out_of_memory = false; - char message[256] = "unknown C++ exception"; - - try - { - PlannedStmt *copy = RptPgCall([&]() { - return (PlannedStmt *) copyObjectImpl(stmt); - }); - int width = RptPgCall([]() { return getgpsegmentCount(); }); - - RptAttachReducerInitPlan(copy, NIL, width); - return copy; - } - catch (const RptPgError &) - { - pg_error = true; - } - catch (const std::bad_alloc &) - { - out_of_memory = true; - } - catch (const std::exception &e) - { - strlcpy(message, e.what(), sizeof(message)); - } - catch (...) - { - } - if (pg_error) - PG_RE_THROW(); - if (out_of_memory) - ereport(ERROR, (errcode(ERRCODE_OUT_OF_MEMORY), - errmsg("out of memory building RPT measurement InitPlan"))); - ereport(ERROR, (errmsg("RPT measurement InitPlan failed: %s", message))); - return NULL; -} - -/* Atomic finished-plan surgery: retain the original on construction failure. - * This switch is only a measurement harness, independent of runtime filters. - * Successful allocations stay under the planner's memory context. */ -static PlannedStmt * -rpt_try_noop_initplan(PlannedStmt *stmt) -{ - MemoryContext saved_context = CurrentMemoryContext; - MemoryContext volatile build_context = NULL; - PlannedStmt *volatile result = stmt; - - PG_TRY(); - { - build_context = AllocSetContextCreate(saved_context, - "RPT measurement InitPlan", - ALLOCSET_DEFAULT_SIZES); - MemoryContextSwitchTo(build_context); - result = rpt_build_noop_initplan(stmt); - MemoryContextSwitchTo(saved_context); - } - PG_CATCH(); - { - MemoryContextSwitchTo(saved_context); - ErrorData *error = CopyErrorData(); - - FlushErrorState(); - if (build_context != NULL) - MemoryContextDelete(build_context); - if (error->sqlerrcode == ERRCODE_QUERY_CANCELED) - ReThrowError(error); - ereport(WARNING, (errmsg("RPT measurement InitPlan skipped: %s", error->message))); - FreeErrorData(error); - } - PG_END_TRY(); - - return result; -} - /* Contain C++ exceptions before returning to the PostgreSQL error handler. */ static PlannedStmt * rpt_build_injection(PlannedStmt *stmt) @@ -440,8 +338,8 @@ rpt_build_injection(PlannedStmt *stmt) return NULL; } -/* Same contract as rpt_try_noop_initplan: the original plan survives any - * failure, which is reported as a WARNING; only cancellation propagates. */ +/* The original plan survives construction failure, reported as a WARNING; + * only cancellation propagates. */ static PlannedStmt * rpt_try_injection(PlannedStmt *stmt) { @@ -515,10 +413,6 @@ rpt_planner(Query *parse, const char *query_string, int cursorOptions, result->copyIntoClause == NULL && result->refreshClause == NULL) result = rpt_try_injection(result); - if (rpt_debug_noop_initplan && Gp_role == GP_ROLE_DISPATCH && - result != NULL && result->commandType == CMD_SELECT && result->planTree != NULL) - result = rpt_try_noop_initplan(result); - return result; } diff --git a/gpcontrib/robust_predicate_transfer/src/rptinject.cpp b/gpcontrib/robust_predicate_transfer/src/rptinject.cpp index 504e6582469..376ed95bdb8 100644 --- a/gpcontrib/robust_predicate_transfer/src/rptinject.cpp +++ b/gpcontrib/robust_predicate_transfer/src/rptinject.cpp @@ -36,13 +36,12 @@ extern "C" /* The caller owns a private copy of the statement. This function may throw; * publish that copy only after the entire injection pass has succeeded. * Children must be non-parallel reducer producers that return NULL at EOF. - * The final empty Result makes EXISTS drain the Sequence completely. - * NIL children builds the no-op shape used to measure dispatch overhead. */ + * The final empty Result makes EXISTS drain the Sequence completely. */ void RptAttachReducerInitPlan(PlannedStmt *stmt, List *children, int width) { RptPgCall([&]() { - if (!stmt->planTree || width <= 0 || + if (children == NIL || !stmt->planTree || width <= 0 || stmt->numSlices <= 0 || !stmt->slices || (stmt->subplans != NIL && !stmt->subplan_sliceIds)) elog(ERROR, "invalid RPT reducer InitPlan input"); diff --git a/gpcontrib/robust_predicate_transfer/test/graph_test.cpp b/gpcontrib/robust_predicate_transfer/test/graph_test.cpp index 9f2dee61b57..513f0d12c30 100644 --- a/gpcontrib/robust_predicate_transfer/test/graph_test.cpp +++ b/gpcontrib/robust_predicate_transfer/test/graph_test.cpp @@ -1028,7 +1028,7 @@ static void forward_selection_chain_and_selectivity() } /* A local hop consumed only inside the reducer is in-process and free; the - * hop into the root, or any hop the main plan consumes, stays on Anser. */ + * hop into the root stays on Anser. */ static void forward_selection_in_process() { RptTransferMgr forward = forward_chain(true); @@ -1046,14 +1046,7 @@ static void forward_selection_in_process() RptDumpForward(comps, &dump); CHECK(dump.find(" local in-process\n") != std::string::npos); - params.apply_all = true; - RptSelectForward(forward, params, {}, size_one_mib, nullptr, &comps); - CHECK(comps[0].channels[0].local); - for (const RptForwardChannel &ch : comps[0].channels) - CHECK(!ch.in_process && ch.est_cost_ms > 0); - forward = forward_chain(false); - params.apply_all = false; RptSelectForward(forward, params, {}, size_one_mib, nullptr, &comps); for (const RptForwardChannel &ch : comps[0].channels) CHECK(!ch.local && !ch.in_process); diff --git a/gpcontrib/robust_predicate_transfer/test/inject_test.cpp b/gpcontrib/robust_predicate_transfer/test/inject_test.cpp index 74976f58cf2..20f8b683bf8 100644 --- a/gpcontrib/robust_predicate_transfer/test/inject_test.cpp +++ b/gpcontrib/robust_predicate_transfer/test/inject_test.cpp @@ -191,7 +191,7 @@ static void collect_plan_node_ids(Plan *p, std::set &plan_node_ids) } } -static void layout(bool existing, bool noop) +static void layout(bool existing) { PlannedStmt stmt = {}; Result main = {}; main.plan.type = T_Result; main.plan.plan_node_id = 5; @@ -211,7 +211,7 @@ static void layout(bool existing, bool noop) stmt.planTree->initPlan = list_make1(&old_init); } Result child = {}; child.plan.type = T_Result; child.plan.plan_node_id = 110; - List *children = noop ? NIL : list_make1(&child); + List *children = list_make1(&child); int old_slices = stmt.numSlices; RptAttachReducerInitPlan(&stmt, children, 8); CHECK(stmt.numSlices == old_slices + 2); @@ -243,9 +243,9 @@ static void layout(bool existing, bool noop) CHECK(motion->motionType == MOTIONTYPE_GATHER); CHECK(motion->plan.flow->flotype == FLOW_SINGLETON); Sequence *seq = (Sequence *) motion->plan.lefttree; - CHECK(IsA(seq, Sequence) && list_length(seq->subplans) == (noop ? 1 : 2)); - CHECK(list_length(children) == (noop ? 0 : 1)); - if (!noop) CHECK(linitial(seq->subplans) == &child); + CHECK(IsA(seq, Sequence) && list_length(seq->subplans) == 2); + CHECK(list_length(children) == 1); + CHECK(linitial(seq->subplans) == &child); CHECK(seq->plan.flow->flotype == FLOW_PARTITIONED && seq->plan.flow->numsegments == 8); Result *empty = (Result *) llast(seq->subplans); CHECK(IsA(empty, Result) && IsA(empty->resconstantqual, List)); @@ -257,8 +257,8 @@ static void layout(bool existing, bool noop) std::set plan_node_ids; collect_plan_node_ids(stmt.planTree, plan_node_ids); ListCell *lc; foreach(lc, stmt.subplans) collect_plan_node_ids((Plan *) lfirst(lc), plan_node_ids); - CHECK(plan_node_ids.size() == (existing ? 6U : 5U) - (noop ? 1U : 0U)); - int previous_max = noop ? (existing ? 90 : 5) : 110; + CHECK(plan_node_ids.size() == (existing ? 6U : 5U)); + int previous_max = 110; CHECK(empty->plan.plan_node_id > previous_max && seq->plan.plan_node_id > previous_max && motion->plan.plan_node_id > previous_max); } @@ -324,14 +324,13 @@ static void backward_stacks(bool refused, bool materialize, bool with_forward) CHECK(state.next_condition_id == 7); for (int i = 0; i < 7; ++i) CHECK(forward_components[0].channels[i].condition_id == uint32(i)); - CustomScan forward[2] = {}; + CustomScan forward = {}; if (with_forward) - for (int i = 0; i < 2; ++i) - { - forward[i].scan.plan.type = T_CustomScan; - forward[i].custom_plans = list_make1(&scans[i]); - RptRelinkStack(&state, &scans[i].scan.plan, &forward[i].scan.plan); - } + { + forward.scan.plan.type = T_CustomScan; + forward.custom_plans = list_make1(&scans[0]); + RptRelinkStack(&state, &scans[0].scan.plan, &forward.scan.plan); + } if (refused) schedule.backward_consumers[0].consumer_attno = schedule.backward_consumers[1].consumer_attno = 99; @@ -429,8 +428,7 @@ int main() backward_stacks(false, true, false); backward_stacks(true, true, true); backward_stacks(false, false, true); - layout(false, false); layout(true, false); - layout(false, true); layout(true, true); + layout(false); layout(true); for (void *p : allocations) free(p); return 0; }