From 54b8ed6c24736d7ad55f56dbaa2696ae05c4bded Mon Sep 17 00:00:00 2001
From: Tom Lane <tgl@sss.pgh.pa.us>
Date: Wed, 24 Sep 2014 15:59:37 -0400
Subject: [PATCH] Fix bogus variable-mangling in
 security_barrier_replace_vars().

This function created new Vars with varno different from varnoold, which
is a condition that should never prevail before setrefs.c does the final
variable-renumbering pass.  The created Vars could not be seen as equal()
to normal Vars, which among other things broke equivalence-class processing
for them.  The consequences of this were indeed visible in the regression
tests, in the form of failure to propagate constants as one would expect.
I stumbled across it while poking at bug #11457 --- after intentionally
disabling join equivalence processing, the security-barrier regression
tests started falling over with fun errors like "could not find pathkey
item to sort", because of failure to match the corrupted Vars to normal
ones.
---
 src/backend/optimizer/prep/prepsecurity.c     |  2 +-
 src/test/regress/expected/updatable_views.out | 32 +++++++++----------
 2 files changed, 17 insertions(+), 17 deletions(-)

diff --git a/src/backend/optimizer/prep/prepsecurity.c b/src/backend/optimizer/prep/prepsecurity.c
index 2420f97a21..51f10a488a 100644
--- a/src/backend/optimizer/prep/prepsecurity.c
+++ b/src/backend/optimizer/prep/prepsecurity.c
@@ -432,7 +432,7 @@ security_barrier_replace_vars_walker(Node *node,
 
 			/* New variable for subquery targetlist */
 			newvar = copyObject(var);
-			newvar->varno = 1;
+			newvar->varno = newvar->varnoold = 1;
 
 			attno = list_length(context->targetlist) + 1;
 			tle = makeTargetEntry((Expr *) newvar,
diff --git a/src/test/regress/expected/updatable_views.out b/src/test/regress/expected/updatable_views.out
index 9ed48962b3..507b6a2a3e 100644
--- a/src/test/regress/expected/updatable_views.out
+++ b/src/test/regress/expected/updatable_views.out
@@ -2071,10 +2071,10 @@ UPDATE v1 SET a=100 WHERE snoop(a) AND leakproof(a) AND a = 3;
                ->  Append
                      ->  Seq Scan on public.t12
                            Output: t12.a
-                           Filter: (t1_5.a = t12.a)
+                           Filter: (t12.a = 3)
                      ->  Seq Scan on public.t111
                            Output: t111.a
-                           Filter: (t1_5.a = t111.a)
+                           Filter: (t111.a = 3)
    ->  Subquery Scan on t1_1
          Output: 100, t1_1.b, t1_1.c, t1_1.d, t1_1.ctid
          Filter: snoop(t1_1.a)
@@ -2086,10 +2086,10 @@ UPDATE v1 SET a=100 WHERE snoop(a) AND leakproof(a) AND a = 3;
                ->  Append
                      ->  Seq Scan on public.t12 t12_1
                            Output: t12_1.a
-                           Filter: (t11.a = t12_1.a)
+                           Filter: (t12_1.a = 3)
                      ->  Seq Scan on public.t111 t111_1
                            Output: t111_1.a
-                           Filter: (t11.a = t111_1.a)
+                           Filter: (t111_1.a = 3)
    ->  Subquery Scan on t1_2
          Output: 100, t1_2.b, t1_2.c, t1_2.e, t1_2.ctid
          Filter: snoop(t1_2.a)
@@ -2101,10 +2101,10 @@ UPDATE v1 SET a=100 WHERE snoop(a) AND leakproof(a) AND a = 3;
                ->  Append
                      ->  Seq Scan on public.t12 t12_3
                            Output: t12_3.a
-                           Filter: (t12_2.a = t12_3.a)
+                           Filter: (t12_3.a = 3)
                      ->  Seq Scan on public.t111 t111_2
                            Output: t111_2.a
-                           Filter: (t12_2.a = t111_2.a)
+                           Filter: (t111_2.a = 3)
    ->  Subquery Scan on t1_3
          Output: 100, t1_3.b, t1_3.c, t1_3.d, t1_3.e, t1_3.ctid
          Filter: snoop(t1_3.a)
@@ -2116,10 +2116,10 @@ UPDATE v1 SET a=100 WHERE snoop(a) AND leakproof(a) AND a = 3;
                ->  Append
                      ->  Seq Scan on public.t12 t12_4
                            Output: t12_4.a
-                           Filter: (t111_3.a = t12_4.a)
+                           Filter: (t12_4.a = 3)
                      ->  Seq Scan on public.t111 t111_4
                            Output: t111_4.a
-                           Filter: (t111_3.a = t111_4.a)
+                           Filter: (t111_4.a = 3)
 (61 rows)
 
 UPDATE v1 SET a=100 WHERE snoop(a) AND leakproof(a) AND a = 3;
@@ -2149,10 +2149,10 @@ UPDATE v1 SET a=a+1 WHERE snoop(a) AND leakproof(a) AND a = 8;
                ->  Append
                      ->  Seq Scan on public.t12
                            Output: t12.a
-                           Filter: (t1_5.a = t12.a)
+                           Filter: (t12.a = 8)
                      ->  Seq Scan on public.t111
                            Output: t111.a
-                           Filter: (t1_5.a = t111.a)
+                           Filter: (t111.a = 8)
    ->  Subquery Scan on t1_1
          Output: (t1_1.a + 1), t1_1.b, t1_1.c, t1_1.d, t1_1.ctid
          Filter: snoop(t1_1.a)
@@ -2164,10 +2164,10 @@ UPDATE v1 SET a=a+1 WHERE snoop(a) AND leakproof(a) AND a = 8;
                ->  Append
                      ->  Seq Scan on public.t12 t12_1
                            Output: t12_1.a
-                           Filter: (t11.a = t12_1.a)
+                           Filter: (t12_1.a = 8)
                      ->  Seq Scan on public.t111 t111_1
                            Output: t111_1.a
-                           Filter: (t11.a = t111_1.a)
+                           Filter: (t111_1.a = 8)
    ->  Subquery Scan on t1_2
          Output: (t1_2.a + 1), t1_2.b, t1_2.c, t1_2.e, t1_2.ctid
          Filter: snoop(t1_2.a)
@@ -2179,10 +2179,10 @@ UPDATE v1 SET a=a+1 WHERE snoop(a) AND leakproof(a) AND a = 8;
                ->  Append
                      ->  Seq Scan on public.t12 t12_3
                            Output: t12_3.a
-                           Filter: (t12_2.a = t12_3.a)
+                           Filter: (t12_3.a = 8)
                      ->  Seq Scan on public.t111 t111_2
                            Output: t111_2.a
-                           Filter: (t12_2.a = t111_2.a)
+                           Filter: (t111_2.a = 8)
    ->  Subquery Scan on t1_3
          Output: (t1_3.a + 1), t1_3.b, t1_3.c, t1_3.d, t1_3.e, t1_3.ctid
          Filter: snoop(t1_3.a)
@@ -2194,10 +2194,10 @@ UPDATE v1 SET a=a+1 WHERE snoop(a) AND leakproof(a) AND a = 8;
                ->  Append
                      ->  Seq Scan on public.t12 t12_4
                            Output: t12_4.a
-                           Filter: (t111_3.a = t12_4.a)
+                           Filter: (t12_4.a = 8)
                      ->  Seq Scan on public.t111 t111_4
                            Output: t111_4.a
-                           Filter: (t111_3.a = t111_4.a)
+                           Filter: (t111_4.a = 8)
 (61 rows)
 
 UPDATE v1 SET a=a+1 WHERE snoop(a) AND leakproof(a) AND a = 8;
-- 
2.40.0