@@ -446,45 +446,61 @@ append_child_relation(PlannerInfo *root, RelOptInfo *rel, Index rti,
446446 memcpy (childrel -> attr_widths , rel -> attr_widths ,
447447 (rel -> max_attr - rel -> min_attr + 1 ) * sizeof (int32 ));
448448
449-
450- /* Copy restrictions */
449+ /*
450+ * Copy restrictions. If it's not the parent table then copy only those
451+ * restrictions that reference to this partition
452+ */
451453 childrel -> baserestrictinfo = NIL ;
452- forboth ( lc , wrappers , lc2 , rel -> baserestrictinfo )
454+ if ( rte -> relid != childOid )
453455 {
454- bool alwaysTrue ;
455- WrapperNode * wrap = (WrapperNode * ) lfirst (lc );
456- Node * new_clause = wrapper_make_expression (wrap , index , & alwaysTrue );
457- RestrictInfo * old_rinfo = (RestrictInfo * ) lfirst (lc2 );
458-
459- if (alwaysTrue )
456+ forboth (lc , wrappers , lc2 , rel -> baserestrictinfo )
460457 {
461- continue ;
462- }
463- Assert (new_clause );
458+ bool alwaysTrue ;
459+ WrapperNode * wrap = (WrapperNode * ) lfirst (lc );
460+ Node * new_clause = wrapper_make_expression (wrap , index , & alwaysTrue );
461+ RestrictInfo * old_rinfo = (RestrictInfo * ) lfirst (lc2 );
464462
465- if (and_clause ((Node * ) new_clause ))
466- {
467- ListCell * alc ;
463+ if (alwaysTrue )
464+ {
465+ continue ;
466+ }
467+ Assert (new_clause );
468468
469- foreach ( alc , (( BoolExpr * ) new_clause )-> args )
469+ if ( and_clause (( Node * ) new_clause ))
470470 {
471- Node * arg = (Node * ) lfirst (alc );
472- RestrictInfo * new_rinfo = rebuild_restrictinfo (arg , old_rinfo );
471+ ListCell * alc ;
472+
473+ foreach (alc , ((BoolExpr * ) new_clause )-> args )
474+ {
475+ Node * arg = (Node * ) lfirst (alc );
476+ RestrictInfo * new_rinfo = rebuild_restrictinfo (arg , old_rinfo );
473477
478+ change_varnos ((Node * )new_rinfo , rel -> relid , childrel -> relid );
479+ childrel -> baserestrictinfo = lappend (childrel -> baserestrictinfo ,
480+ new_rinfo );
481+ }
482+ }
483+ else
484+ {
485+ RestrictInfo * new_rinfo = rebuild_restrictinfo (new_clause , old_rinfo );
486+
487+ /* Replace old relids with new ones */
474488 change_varnos ((Node * )new_rinfo , rel -> relid , childrel -> relid );
489+
475490 childrel -> baserestrictinfo = lappend (childrel -> baserestrictinfo ,
476- new_rinfo );
491+ ( void * ) new_rinfo );
477492 }
478493 }
479- else
494+ }
495+ /* If it's the parent table then copy all restrictions */
496+ else
497+ {
498+ foreach (lc , rel -> baserestrictinfo )
480499 {
481- RestrictInfo * new_rinfo = rebuild_restrictinfo (new_clause , old_rinfo );
482-
483- /* Replace old relids with new ones */
484- change_varnos ((Node * )new_rinfo , rel -> relid , childrel -> relid );
500+ RestrictInfo * rinfo = (RestrictInfo * ) lfirst (lc );
485501
486502 childrel -> baserestrictinfo = lappend (childrel -> baserestrictinfo ,
487- (void * ) new_rinfo );
503+ (RestrictInfo * ) copyObject ( rinfo ) );
488504 }
489505 }
490506
0 commit comments