@@ -446,43 +446,61 @@ append_child_relation(PlannerInfo *root, RelOptInfo *rel, Index rti,
446
446
memcpy (childrel -> attr_widths ,rel -> attr_widths ,
447
447
(rel -> max_attr - rel -> min_attr + 1 )* sizeof (int32 ));
448
448
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
+ */
451
453
childrel -> baserestrictinfo = NIL ;
452
- forboth ( lc , wrappers , lc2 , rel -> baserestrictinfo )
454
+ if ( rte -> relid != childOid )
453
455
{
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 )
460
457
{
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 );
464
462
465
- if (and_clause ((Node * )new_clause ))
466
- {
467
- ListCell * alc ;
463
+ if (alwaysTrue )
464
+ {
465
+ continue ;
466
+ }
467
+ Assert (new_clause );
468
468
469
- foreach ( alc , (( BoolExpr * )new_clause )-> args )
469
+ if ( and_clause (( Node * )new_clause ))
470
470
{
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 );
473
477
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 */
474
488
change_varnos ((Node * )new_rinfo ,rel -> relid ,childrel -> relid );
489
+
475
490
childrel -> baserestrictinfo = lappend (childrel -> baserestrictinfo ,
476
- new_rinfo );
491
+ ( void * ) new_rinfo );
477
492
}
478
493
}
479
- else
494
+ }
495
+ /* If it's the parent table then copy all restrictions */
496
+ else
497
+ {
498
+ foreach (lc ,rel -> baserestrictinfo )
480
499
{
481
- RestrictInfo * new_rinfo = rebuild_restrictinfo (new_clause ,old_rinfo );
500
+ RestrictInfo * rinfo = (RestrictInfo * )lfirst (lc );
501
+ RestrictInfo * new_rinfo = (RestrictInfo * )copyObject (rinfo );
482
502
483
- /* Replace old relids with new ones */
484
503
change_varnos ((Node * )new_rinfo ,rel -> relid ,childrel -> relid );
485
-
486
504
childrel -> baserestrictinfo = lappend (childrel -> baserestrictinfo ,
487
505
(void * )new_rinfo );
488
506
}