diff --git a/src/backend/optimizer/plan/initsplan.c b/src/backend/optimizer/plan/initsplan.c
index e2c68fe6f9..4198e9c83a 100644
--- a/src/backend/optimizer/plan/initsplan.c
+++ b/src/backend/optimizer/plan/initsplan.c
@@ -979,7 +979,7 @@ deconstruct_recurse(PlannerInfo *root, Node *jtnode,
 													right_item->inner_join_rels);
 				jtitem->left_rels = left_item->qualscope;
 				jtitem->right_rels = right_item->qualscope;
-				jtitem->nonnullable_rels = left_item->qualscope;
+				jtitem->nonnullable_rels = bms_copy(left_item->qualscope);
 				break;
 			case JOIN_SEMI:
 				/* This node belongs to parent_domain, as do its children */
@@ -1053,7 +1053,7 @@ deconstruct_recurse(PlannerInfo *root, Node *jtnode,
 				jtitem->left_rels = left_item->qualscope;
 				jtitem->right_rels = right_item->qualscope;
 				/* each side is both outer and inner */
-				jtitem->nonnullable_rels = jtitem->qualscope;
+				jtitem->nonnullable_rels = bms_copy(jtitem->qualscope);
 				break;
 			default:
 				/* JOIN_RIGHT was eliminated during reduce_outer_joins() */
@@ -1888,7 +1888,7 @@ deconstruct_distribute_oj_quals(PlannerInfo *root,
 	qualscope = bms_union(sjinfo->syn_lefthand, sjinfo->syn_righthand);
 	qualscope = bms_add_member(qualscope, sjinfo->ojrelid);
 	ojscope = bms_union(sjinfo->min_lefthand, sjinfo->min_righthand);
-	nonnullable_rels = sjinfo->syn_lefthand;
+	nonnullable_rels = bms_copy(sjinfo->syn_lefthand);
 
 	/*
 	 * If this join can commute with any other ones per outer-join identity 3,
