1212#include "runtimeappend.h"
1313#include "utils.h"
1414
15+ #include "access/sysattr.h"
1516#include "optimizer/restrictinfo.h"
17+ #include "optimizer/var.h"
1618#include "utils/memutils.h"
1719
1820
@@ -248,6 +250,38 @@ unpack_runtimeappend_private(RuntimeAppendState *scan_state, CustomScan *cscan)
248250 scan_state -> enable_parent = (bool ) linitial_int (lthird (runtimeappend_private ));
249251}
250252
253+ /*
254+ * Filter all available clauses and extract relevant ones.
255+ */
256+ List *
257+ get_partitioned_attr_clauses (List * restrictinfo_list ,
258+ const PartRelationInfo * prel ,
259+ Index partitioned_rel )
260+ {
261+ #define AdjustAttno (attno ) \
262+ ( (AttrNumber) (part_attno + FirstLowInvalidHeapAttributeNumber) )
263+
264+ List * result = NIL ;
265+ ListCell * l ;
266+
267+ foreach (l , restrictinfo_list )
268+ {
269+ RestrictInfo * rinfo = (RestrictInfo * ) lfirst (l );
270+ Bitmapset * varattnos = NULL ;
271+ int part_attno ;
272+
273+ Assert (IsA (rinfo , RestrictInfo ));
274+ pull_varattnos ((Node * ) rinfo -> clause , partitioned_rel , & varattnos );
275+
276+ if (bms_get_singleton_member (varattnos , & part_attno ) &&
277+ AdjustAttno (part_attno ) == prel -> attnum )
278+ {
279+ result = lappend (result , rinfo -> clause );
280+ }
281+ }
282+ return result ;
283+ }
284+
251285
252286/* Transform partition ranges into plain array of partition Oids */
253287Oid *
@@ -385,7 +419,7 @@ create_append_plan_common(PlannerInfo *root, RelOptInfo *rel,
385419 Plan * child_plan = (Plan * ) lfirst (lc2 );
386420 RelOptInfo * child_rel = ((Path * ) lfirst (lc1 ))-> parent ;
387421
388- /* Replace rel's tlist with a matching one */
422+ /* Replace rel's tlist with a matching one */
389423 if (!cscan -> scan .plan .targetlist )
390424 tlist = replace_tlist_varnos (child_plan -> targetlist , rel );
391425
@@ -407,7 +441,7 @@ create_append_plan_common(PlannerInfo *root, RelOptInfo *rel,
407441 /* Since we're not scanning any real table directly */
408442 cscan -> scan .scanrelid = 0 ;
409443
410- cscan -> custom_exprs = get_actual_clauses (clauses );
444+ cscan -> custom_exprs = get_partitioned_attr_clauses (clauses , prel , rel -> relid );
411445 cscan -> custom_plans = custom_plans ;
412446 cscan -> methods = scan_methods ;
413447
0 commit comments