77 * Portions Copyright (c) 1994, Regents of the University of California
88 *
99 * IDENTIFICATION
10- * $PostgreSQL: pgsql/src/backend/optimizer/plan/subselect.c,v 1.136 2008/08/2015:49:30 tgl Exp $
10+ * $PostgreSQL: pgsql/src/backend/optimizer/plan/subselect.c,v 1.137 2008/08/2019:58:24 tgl Exp $
1111 *
1212 *-------------------------------------------------------------------------
1313 */
@@ -1148,10 +1148,14 @@ process_sublinks_mutator(Node *node, process_sublinks_context *context)
11481148 * take steps to preserve AND/OR flatness of a qual. We assume the input
11491149 * has been AND/OR flattened and so we need no recursion here.
11501150 *
1151- * If we recurse down through anything other than an AND node, we are
1152- * definitely not at top qual level anymore. (Due to the coding here, we
1153- * will not get called on the List subnodes of an AND, so no check is
1154- * needed for List.)
1151+ * (Due to the coding here, we will not get called on the List subnodes of
1152+ * an AND; and the input is *not* yet in implicit-AND format. So no check
1153+ * is needed for a bare List.)
1154+ *
1155+ * Anywhere within the top-level AND/OR clause structure, we can tell
1156+ * make_subplan() that NULL and FALSE are interchangeable. So isTopQual
1157+ * propagates down in both cases. (Note that this is unlike the meaning
1158+ * of "top level qual" used in most other places in Postgres.)
11551159 */
11561160if (and_clause (node ))
11571161{
@@ -1174,14 +1178,14 @@ process_sublinks_mutator(Node *node, process_sublinks_context *context)
11741178return (Node * )make_andclause (newargs );
11751179}
11761180
1177- /* otherwise not at qual top-level */
1178- locContext .isTopQual = false;
1179-
11801181if (or_clause (node ))
11811182{
11821183List * newargs = NIL ;
11831184ListCell * l ;
11841185
1186+ /* Still at qual top-level */
1187+ locContext .isTopQual = context -> isTopQual ;
1188+
11851189foreach (l , ((BoolExpr * )node )-> args )
11861190{
11871191Node * newarg ;
@@ -1195,6 +1199,12 @@ process_sublinks_mutator(Node *node, process_sublinks_context *context)
11951199return (Node * )make_orclause (newargs );
11961200}
11971201
1202+ /*
1203+ * If we recurse down through anything other than an AND or OR node,
1204+ * we are definitely not at top qual level anymore.
1205+ */
1206+ locContext .isTopQual = false;
1207+
11981208return expression_tree_mutator (node ,
11991209process_sublinks_mutator ,
12001210 (void * )& locContext );