@@ -446,43 +446,61 @@ append_child_relation(PlannerInfo *root, RelOptInfo *rel, Index rti,
446446memcpy (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+ */
451453childrel -> 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 */
474488change_varnos ((Node * )new_rinfo ,rel -> relid ,childrel -> relid );
489+
475490childrel -> 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 );
500+ RestrictInfo * rinfo = (RestrictInfo * )lfirst (lc );
501+ RestrictInfo * new_rinfo = (RestrictInfo * )copyObject (rinfo );
482502
483- /* Replace old relids with new ones */
484503change_varnos ((Node * )new_rinfo ,rel -> relid ,childrel -> relid );
485-
486504childrel -> baserestrictinfo = lappend (childrel -> baserestrictinfo ,
487505 (void * )new_rinfo );
488506}