From 5ccb5d30665507b898de76b3eb5bec1bfee9c226 Mon Sep 17 00:00:00 2001
From: Julien Rouhaud <julien.rouhaud@free.fr>
Date: Fri, 16 Sep 2022 01:28:08 +0800
Subject: [PATCH v20220922 11/13] FIXUP regress-tests-for-session-variables

---
 .../isolation/expected/session-variable.out   | 30 +++++++++++++++++--
 .../isolation/specs/session-variable.spec     | 23 ++++++++++++--
 .../regress/expected/session_variables.out    | 22 ++++++++++++++
 src/test/regress/sql/session_variables.sql    | 16 ++++++++++
 4 files changed, 87 insertions(+), 4 deletions(-)

diff --git a/src/test/isolation/expected/session-variable.out b/src/test/isolation/expected/session-variable.out
index b0059910a1..88d3b6c31d 100644
--- a/src/test/isolation/expected/session-variable.out
+++ b/src/test/isolation/expected/session-variable.out
@@ -41,7 +41,7 @@ public|myvar|f
 (1 row)
 
 step drop: DROP VARIABLE myvar;
-step create: CREATE VARIABLE myvar AS text
+step create: CREATE VARIABLE myvar AS text;
 step dbg: SELECT schema, name, removed FROM pg_debug_show_used_session_variables();
 schema|name|removed
 ------+----+-------
@@ -70,7 +70,7 @@ public|myvar|f
 (1 row)
 
 step drop: DROP VARIABLE myvar;
-step create: CREATE VARIABLE myvar AS text
+step create: CREATE VARIABLE myvar AS text;
 step dbg: SELECT schema, name, removed FROM pg_debug_show_used_session_variables();
 schema|name |removed
 ------+-----+-------
@@ -84,3 +84,29 @@ myvar
 (1 row)
 
 step sr1: ROLLBACK;
+
+starting permutation: create3 let3 s3 o_c_d o_eox_r create4 let4 drop4 drop3 inval3 discard sc3 clean state
+step create3: CREATE VARIABLE myvar3 AS text;
+step let3: LET myvar3 = 'test';
+step s3: BEGIN;
+step o_c_d: CREATE TEMP VARIABLE myvar_o_c_d AS text ON COMMIT DROP;
+step o_eox_r: CREATE VARIABLE myvar_o_eox_r AS text ON TRANSACTION END RESET; LET myvar_o_eox_r = 'test';
+step create4: CREATE VARIABLE myvar4 AS text;
+step let4: LET myvar4 = 'test';
+step drop4: DROP VARIABLE myvar4;
+step drop3: DROP VARIABLE myvar3;
+step inval3: SELECT COUNT(*) >= 0 FROM pg_foreign_table;
+?column?
+--------
+t       
+(1 row)
+
+step discard: DISCARD VARIABLES;
+step sc3: COMMIT;
+step clean: DROP VARIABLE myvar_o_eox_r;
+step state: SELECT varname FROM pg_variable;
++varname
++-----------
+myvar
+(1 row)
+
diff --git a/src/test/isolation/specs/session-variable.spec b/src/test/isolation/specs/session-variable.spec
index fe47339110..5a1035a2b2 100644
--- a/src/test/isolation/specs/session-variable.spec
+++ b/src/test/isolation/specs/session-variable.spec
@@ -17,10 +17,27 @@ step val	{ SELECT myvar; }
 step dbg	{ SELECT schema, name, removed FROM pg_debug_show_used_session_variables(); }
 step sr1	{ ROLLBACK; }
 
-
 session s2
 step drop		{ DROP VARIABLE myvar; }
-step create		{ CREATE VARIABLE myvar AS text }
+step create		{ CREATE VARIABLE myvar AS text; }
+
+session s3
+step s3			{ BEGIN; }
+step let3		{ LET myvar3 = 'test'; }
+step o_c_d		{ CREATE TEMP VARIABLE myvar_o_c_d AS text ON COMMIT DROP; }
+step o_eox_r	{ CREATE VARIABLE myvar_o_eox_r AS text ON TRANSACTION END RESET; LET myvar_o_eox_r = 'test'; }
+step create4	{ CREATE VARIABLE myvar4 AS text; }
+step let4		{ LET myvar4 = 'test'; }
+step drop4		{ DROP VARIABLE myvar4; }
+step inval3		{ SELECT COUNT(*) >= 0 FROM pg_foreign_table; }
+step discard	{ DISCARD VARIABLES; }
+step sc3		{ COMMIT; }
+step clean		{ DROP VARIABLE myvar_o_eox_r; }
+step state		{ SELECT varname FROM pg_variable; }
+
+session s4
+step create3	{ CREATE VARIABLE myvar3 AS text; }
+step drop3		{ DROP VARIABLE myvar3; }
 
 # Concurrent drop of a known variable should lead to an error
 permutation let val drop val
@@ -32,3 +49,5 @@ permutation let val dbg drop create dbg val
 # We need a transaction to make sure that we won't accept invalidation when
 # calling the dbg step after the concurrent drop
 permutation let val s1 dbg drop create dbg val sr1
+# test for DISCARD ALL when all internal queues have actions registered
+permutation create3 let3 s3 o_c_d o_eox_r create4 let4 drop4 drop3 inval3 discard sc3 clean state
diff --git a/src/test/regress/expected/session_variables.out b/src/test/regress/expected/session_variables.out
index d411f0440d..a601829884 100644
--- a/src/test/regress/expected/session_variables.out
+++ b/src/test/regress/expected/session_variables.out
@@ -480,6 +480,28 @@ SELECT count(*) FROM pg_variable;
      0
 (1 row)
 
+BEGIN;
+  CREATE TEMP VARIABLE g AS int ON COMMIT DROP;
+  LET g = 1;
+  DISCARD VARIABLES;
+COMMIT;
+SELECT count(*) FROM pg_variable;
+ count 
+-------
+     0
+(1 row)
+
+BEGIN;
+  CREATE TEMP VARIABLE g AS int ON COMMIT DROP;
+  LET g = 1;
+  DISCARD VARIABLES;
+ROLLBACK;
+SELECT count(*) FROM pg_variable;
+ count 
+-------
+     0
+(1 row)
+
 -- Encourage use of parallel plans
 SET parallel_setup_cost = 0;
 SET parallel_tuple_cost = 0;
diff --git a/src/test/regress/sql/session_variables.sql b/src/test/regress/sql/session_variables.sql
index d32bb5b042..6e023d1bbc 100644
--- a/src/test/regress/sql/session_variables.sql
+++ b/src/test/regress/sql/session_variables.sql
@@ -329,6 +329,22 @@ ROLLBACK;
 
 SELECT count(*) FROM pg_variable;
 
+BEGIN;
+  CREATE TEMP VARIABLE g AS int ON COMMIT DROP;
+  LET g = 1;
+  DISCARD VARIABLES;
+COMMIT;
+
+SELECT count(*) FROM pg_variable;
+
+BEGIN;
+  CREATE TEMP VARIABLE g AS int ON COMMIT DROP;
+  LET g = 1;
+  DISCARD VARIABLES;
+ROLLBACK;
+
+SELECT count(*) FROM pg_variable;
+
 -- Encourage use of parallel plans
 SET parallel_setup_cost = 0;
 SET parallel_tuple_cost = 0;
-- 
2.37.0

