From 789e1117119d2347a8b86a3201359a8d45d98865 Mon Sep 17 00:00:00 2001 From: "Anton A. Melnikov" Date: Mon, 24 Jul 2023 11:05:37 +0300 Subject: [PATCH 1/2] PGPRO-8546: Create targetlist in partition filter and partition router nodes right with the parent_rti indexes. In accordance with the pointing of Tom Lane: https://www.postgresql.org/message-id/71315.1686243488%40sss.pgh.pa.us Tags: pg_pathman --- src/include/partition_filter.h | 4 +--- src/partition_filter.c | 39 +++------------------------------- src/partition_overseer.c | 2 +- src/partition_router.c | 7 +----- 4 files changed, 6 insertions(+), 46 deletions(-) diff --git a/src/include/partition_filter.h b/src/include/partition_filter.h index 9b9f52f9..042b1d55 100644 --- a/src/include/partition_filter.h +++ b/src/include/partition_filter.h @@ -181,9 +181,7 @@ TupleConversionMap * build_part_tuple_map_child(Relation child_rel); void destroy_tuple_map(TupleConversionMap *tuple_map); -List * pfilter_build_tlist(Plan *subplan); - -void pfilter_tlist_fix_resjunk(CustomScan *subplan); +List * pfilter_build_tlist(Plan *subplan, Index varno); /* Find suitable partition using 'value' */ Oid * find_partitions_for_value(Datum value, Oid value_type, diff --git a/src/partition_filter.c b/src/partition_filter.c index d4cf8308..4391bcf3 100644 --- a/src/partition_filter.c +++ b/src/partition_filter.c @@ -812,12 +812,7 @@ make_partition_filter(Plan *subplan, cscan->scan.scanrelid = 0; /* Build an appropriate target list */ - cscan->scan.plan.targetlist = pfilter_build_tlist(subplan); - - /* Prepare 'custom_scan_tlist' for EXPLAIN (VERBOSE) */ - cscan->custom_scan_tlist = copyObject(cscan->scan.plan.targetlist); - ChangeVarNodes((Node *) cscan->custom_scan_tlist, INDEX_VAR, parent_rti, 0); - pfilter_tlist_fix_resjunk(cscan); + cscan->scan.plan.targetlist = pfilter_build_tlist(subplan, parent_rti); /* Pack partitioned table's Oid and conflict_action */ cscan->custom_private = list_make4(makeInteger(parent_relid), @@ -1076,7 +1071,7 @@ partition_filter_explain(CustomScanState *node, List *ancestors, ExplainState *e * Build partition filter's target list pointing to subplan tuple's elements. */ List * -pfilter_build_tlist(Plan *subplan) +pfilter_build_tlist(Plan *subplan, Index varno) { List *result_tlist = NIL; ListCell *lc; @@ -1096,7 +1091,7 @@ pfilter_build_tlist(Plan *subplan) } else { - Var *var = makeVar(INDEX_VAR, /* point to subplan's elements */ + Var *var = makeVar(varno, /* point to subplan's elements */ tle->resno, exprType((Node *) tle->expr), exprTypmod((Node *) tle->expr), @@ -1115,34 +1110,6 @@ pfilter_build_tlist(Plan *subplan) return result_tlist; } -/* - * resjunk Vars had its varattnos being set on nonexisting relation columns. - * For future processing service attributes should be indicated correctly. - */ -void -pfilter_tlist_fix_resjunk(CustomScan *css) -{ - ListCell *lc; - - foreach(lc, css->custom_scan_tlist) - { - TargetEntry *tle = (TargetEntry *) lfirst(lc); - - if (!IsA(tle->expr, Const)) - { - Var *var = (Var *) tle->expr; - - if (tle->resjunk) - { - /* To make Var recognizable as service attribute. */ - var->varattno = -1; - } - } - } - - return; -} - /* * ---------------------------------------------- * Additional init steps for ResultPartsStorage diff --git a/src/partition_overseer.c b/src/partition_overseer.c index ffa770ba..d858374a 100644 --- a/src/partition_overseer.c +++ b/src/partition_overseer.c @@ -46,7 +46,7 @@ make_partition_overseer(Plan *subplan) cscan->scan.scanrelid = 0; /* Build an appropriate target list */ - cscan->scan.plan.targetlist = pfilter_build_tlist(subplan); + cscan->scan.plan.targetlist = pfilter_build_tlist(subplan, INDEX_VAR); cscan->custom_scan_tlist = subplan->targetlist; return &cscan->scan.plan; diff --git a/src/partition_router.c b/src/partition_router.c index 4a597a13..5f00e9b1 100644 --- a/src/partition_router.c +++ b/src/partition_router.c @@ -134,12 +134,7 @@ make_partition_router(Plan *subplan, int epq_param, Index parent_rti) cscan->scan.scanrelid = 0; /* Build an appropriate target list */ - cscan->scan.plan.targetlist = pfilter_build_tlist(subplan); - - /* Fix 'custom_scan_tlist' for EXPLAIN (VERBOSE) */ - cscan->custom_scan_tlist = copyObject(cscan->scan.plan.targetlist); - ChangeVarNodes((Node *) cscan->custom_scan_tlist, INDEX_VAR, parent_rti, 0); - pfilter_tlist_fix_resjunk(cscan); + cscan->scan.plan.targetlist = pfilter_build_tlist(subplan, parent_rti); return &cscan->scan.plan; } From db938cca85e7dc42ee7090a5d9e12774e2cee782 Mon Sep 17 00:00:00 2001 From: "Anton A. Melnikov" Date: Wed, 27 Sep 2023 18:25:39 +0300 Subject: [PATCH 2/2] PGPRO-8546: Add core patch for REL_11_STABLE. Don't generate deforming jit code for tuples without user attributes. Without this patch an "ERROR: unknown alignment" may occur during jit compilation. Tags: pg_pathman --- README.md | 4 +- patches/REL_11_STABLE-pg_pathman-core.diff | 53 ++++++++++++++++++++++ 2 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 patches/REL_11_STABLE-pg_pathman-core.diff diff --git a/README.md b/README.md index 43d585ff..1394bc6f 100644 --- a/README.md +++ b/README.md @@ -13,8 +13,8 @@ The `pg_pathman` module provides optimized partitioning mechanism and functions The extension is compatible with: - * PostgreSQL 11, 12, 13; - * PostgreSQL with core-patch: 14, 15; + * PostgreSQL 12, 13; + * PostgreSQL with core-patch: 11, 14, 15; * Postgres Pro Standard 11, 12, 13, 14, 15; * Postgres Pro Enterprise; diff --git a/patches/REL_11_STABLE-pg_pathman-core.diff b/patches/REL_11_STABLE-pg_pathman-core.diff new file mode 100644 index 00000000..b3b08e0a --- /dev/null +++ b/patches/REL_11_STABLE-pg_pathman-core.diff @@ -0,0 +1,53 @@ +diff --git a/src/backend/jit/llvm/llvmjit_deform.c b/src/backend/jit/llvm/llvmjit_deform.c +index 6384ac940d8..8b4f731e7a8 100644 +--- a/src/backend/jit/llvm/llvmjit_deform.c ++++ b/src/backend/jit/llvm/llvmjit_deform.c +@@ -104,6 +104,10 @@ slot_compile_deform(LLVMJitContext *context, TupleDesc desc, int natts) + + int attnum; + ++ /* don't generate code for tuples without user attributes */ ++ if (desc->natts == 0) ++ return NULL; ++ + mod = llvm_mutable_module(context); + + funcname = llvm_expand_funcname(context, "deform"); +diff --git a/src/backend/jit/llvm/llvmjit_expr.c b/src/backend/jit/llvm/llvmjit_expr.c +index 12138e49577..8638ebc4ba1 100644 +--- a/src/backend/jit/llvm/llvmjit_expr.c ++++ b/src/backend/jit/llvm/llvmjit_expr.c +@@ -274,6 +274,7 @@ llvm_compile_expr(ExprState *state) + LLVMValueRef v_slot; + LLVMBasicBlockRef b_fetch; + LLVMValueRef v_nvalid; ++ LLVMValueRef l_jit_deform = NULL; + + b_fetch = l_bb_before_v(opblocks[i + 1], + "op.%d.fetch", i); +@@ -336,17 +337,20 @@ llvm_compile_expr(ExprState *state) + */ + if (desc && (context->base.flags & PGJIT_DEFORM)) + { +- LLVMValueRef params[1]; +- LLVMValueRef l_jit_deform; +- + l_jit_deform = +- slot_compile_deform(context, desc, ++ slot_compile_deform(context, ++ desc, + op->d.fetch.last_var); ++ } ++ ++ if (l_jit_deform) ++ { ++ LLVMValueRef params[1]; ++ + params[0] = v_slot; + + LLVMBuildCall(b, l_jit_deform, + params, lengthof(params), ""); +- + } + else + {