From 3976128fab1467fde8ee7e1bc1f54b023e96e35d Mon Sep 17 00:00:00 2001
From: Melanie Plageman <melanieplageman@gmail.com>
Date: Thu, 19 Jan 2023 15:45:54 -0500
Subject: [PATCH v50 1/5] Create regress_tblspc in test_setup

Other tests may want to use a tablespace. Now that we have
allow_in_place_tablespaces, move the tablespace test to the end of the
parallel schedule and create the main tablespace it uses in test setup.

Modify the tablespace tests a bit to check for specific relations in the
test and not simply for the absence or presence of any objects in the
tablespace in case other tests leave objects around in the tablespace.
---
 src/test/regress/expected/tablespace.out | 63 +++++++++++++-----------
 src/test/regress/expected/test_setup.out |  3 ++
 src/test/regress/parallel_schedule       |  9 ++--
 src/test/regress/sql/tablespace.sql      | 40 ++++++++++-----
 src/test/regress/sql/test_setup.sql      |  4 ++
 5 files changed, 73 insertions(+), 46 deletions(-)

diff --git a/src/test/regress/expected/tablespace.out b/src/test/regress/expected/tablespace.out
index c52cf1cfcf..a2aa95bd97 100644
--- a/src/test/regress/expected/tablespace.out
+++ b/src/test/regress/expected/tablespace.out
@@ -22,8 +22,6 @@ SELECT spcoptions FROM pg_tablespace WHERE spcname = 'regress_tblspacewith';
 
 -- drop the tablespace so we can re-use the location
 DROP TABLESPACE regress_tblspacewith;
--- create a tablespace we can use
-CREATE TABLESPACE regress_tblspace LOCATION '';
 -- This returns a relative path as of an effect of allow_in_place_tablespaces,
 -- masking the tablespace OID used in the path name.
 SELECT regexp_replace(pg_tablespace_location(oid), '(pg_tblspc)/(\d+)', '\1/NNN')
@@ -83,11 +81,14 @@ REINDEX (TABLESPACE regress_tblspace) INDEX regress_tblspace_test_tbl_idx;
 REINDEX (TABLESPACE regress_tblspace) TABLE regress_tblspace_test_tbl;
 ROLLBACK;
 -- no relation moved to the new tablespace
-SELECT c.relname FROM pg_class c, pg_tablespace s
+SELECT c.relname <> 'regress_tblspace_test_tbl_idx',
+       c.relname <> 'regress_tblspace_test_tbl'
+  FROM pg_class c, pg_tablespace s
   WHERE c.reltablespace = s.oid AND s.spcname = 'regress_tblspace';
- relname 
----------
-(0 rows)
+ ?column? | ?column? 
+----------+----------
+ t        | t
+(1 row)
 
 -- check that all indexes are moved to a new tablespace with different
 -- relfilenode.
@@ -102,40 +103,46 @@ SELECT relfilenode as toast_filenode FROM pg_class
        WHERE i.indrelid = c.reltoastrelid AND
              c.relname = 'regress_tblspace_test_tbl') \gset
 REINDEX (TABLESPACE regress_tblspace) TABLE regress_tblspace_test_tbl;
-SELECT c.relname FROM pg_class c, pg_tablespace s
-  WHERE c.reltablespace = s.oid AND s.spcname = 'regress_tblspace'
-  ORDER BY c.relname;
-            relname            
--------------------------------
- regress_tblspace_test_tbl_idx
+SELECT 'regress_tblspace_test_tbl_idx' IN
+  (SELECT c.relname
+  FROM pg_class c, pg_tablespace s
+  WHERE c.reltablespace = s.oid AND s.spcname = 'regress_tblspace');
+ ?column? 
+----------
+ t
 (1 row)
 
 ALTER TABLE regress_tblspace_test_tbl SET TABLESPACE regress_tblspace;
 ALTER TABLE regress_tblspace_test_tbl SET TABLESPACE pg_default;
-SELECT c.relname FROM pg_class c, pg_tablespace s
-  WHERE c.reltablespace = s.oid AND s.spcname = 'regress_tblspace'
-  ORDER BY c.relname;
-            relname            
--------------------------------
- regress_tblspace_test_tbl_idx
+SELECT 'regress_tblspace_test_tbl_idx' IN
+  (SELECT c.relname
+  FROM pg_class c, pg_tablespace s
+  WHERE c.reltablespace = s.oid AND s.spcname = 'regress_tblspace');
+ ?column? 
+----------
+ t
 (1 row)
 
 -- Move back to the default tablespace.
 ALTER INDEX regress_tblspace_test_tbl_idx SET TABLESPACE pg_default;
-SELECT c.relname FROM pg_class c, pg_tablespace s
+SELECT c.relname <> 'regress_tblspace_test_tbl_idx',
+       c.relname <> 'regress_tblspace_test_tbl'
+  FROM pg_class c, pg_tablespace s
   WHERE c.reltablespace = s.oid AND s.spcname = 'regress_tblspace'
   ORDER BY c.relname;
- relname 
----------
-(0 rows)
+ ?column? | ?column? 
+----------+----------
+ t        | t
+(1 row)
 
 REINDEX (TABLESPACE regress_tblspace, CONCURRENTLY) TABLE regress_tblspace_test_tbl;
-SELECT c.relname FROM pg_class c, pg_tablespace s
-  WHERE c.reltablespace = s.oid AND s.spcname = 'regress_tblspace'
-  ORDER BY c.relname;
-            relname            
--------------------------------
- regress_tblspace_test_tbl_idx
+SELECT 'regress_tblspace_test_tbl_idx' IN
+  (SELECT c.relname
+  FROM pg_class c, pg_tablespace s
+  WHERE c.reltablespace = s.oid AND s.spcname = 'regress_tblspace');
+ ?column? 
+----------
+ t
 (1 row)
 
 SELECT relfilenode = :main_filenode AS main_same FROM pg_class
diff --git a/src/test/regress/expected/test_setup.out b/src/test/regress/expected/test_setup.out
index 391b36d131..4f54fe20ec 100644
--- a/src/test/regress/expected/test_setup.out
+++ b/src/test/regress/expected/test_setup.out
@@ -18,6 +18,9 @@ SET synchronous_commit = on;
 -- and most of the core regression tests still expect that.
 --
 GRANT ALL ON SCHEMA public TO public;
+-- Create a tablespace we can use in tests.
+SET allow_in_place_tablespaces = true;
+CREATE TABLESPACE regress_tblspace LOCATION '';
 --
 -- These tables have traditionally been referenced by many tests,
 -- so create and populate them.  Insert only non-error values here.
diff --git a/src/test/regress/parallel_schedule b/src/test/regress/parallel_schedule
index a930dfe48c..15e015b3d6 100644
--- a/src/test/regress/parallel_schedule
+++ b/src/test/regress/parallel_schedule
@@ -11,11 +11,6 @@
 # required setup steps
 test: test_setup
 
-# run tablespace by itself, and early, because it forces a checkpoint;
-# we'd prefer not to have checkpoints later in the tests because that
-# interferes with crash-recovery testing.
-test: tablespace
-
 # ----------
 # The first group of parallel tests
 # ----------
@@ -132,3 +127,7 @@ test: event_trigger oidjoins
 
 # this test also uses event triggers, so likewise run it by itself
 test: fast_default
+
+# run tablespace test at the end because it drops the tablespace created during
+# setup that other tests may use.
+test: tablespace
diff --git a/src/test/regress/sql/tablespace.sql b/src/test/regress/sql/tablespace.sql
index 21db433f2a..1e03d679b2 100644
--- a/src/test/regress/sql/tablespace.sql
+++ b/src/test/regress/sql/tablespace.sql
@@ -20,8 +20,6 @@ SELECT spcoptions FROM pg_tablespace WHERE spcname = 'regress_tblspacewith';
 -- drop the tablespace so we can re-use the location
 DROP TABLESPACE regress_tblspacewith;
 
--- create a tablespace we can use
-CREATE TABLESPACE regress_tblspace LOCATION '';
 -- This returns a relative path as of an effect of allow_in_place_tablespaces,
 -- masking the tablespace OID used in the path name.
 SELECT regexp_replace(pg_tablespace_location(oid), '(pg_tblspc)/(\d+)', '\1/NNN')
@@ -66,7 +64,9 @@ REINDEX (TABLESPACE regress_tblspace) INDEX regress_tblspace_test_tbl_idx;
 REINDEX (TABLESPACE regress_tblspace) TABLE regress_tblspace_test_tbl;
 ROLLBACK;
 -- no relation moved to the new tablespace
-SELECT c.relname FROM pg_class c, pg_tablespace s
+SELECT c.relname <> 'regress_tblspace_test_tbl_idx',
+       c.relname <> 'regress_tblspace_test_tbl'
+  FROM pg_class c, pg_tablespace s
   WHERE c.reltablespace = s.oid AND s.spcname = 'regress_tblspace';
 
 -- check that all indexes are moved to a new tablespace with different
@@ -74,6 +74,7 @@ SELECT c.relname FROM pg_class c, pg_tablespace s
 -- Save first the existing relfilenode for the toast and main relations.
 SELECT relfilenode as main_filenode FROM pg_class
   WHERE relname = 'regress_tblspace_test_tbl_idx' \gset
+
 SELECT relfilenode as toast_filenode FROM pg_class
   WHERE oid =
     (SELECT i.indexrelid
@@ -81,24 +82,37 @@ SELECT relfilenode as toast_filenode FROM pg_class
             pg_index i
        WHERE i.indrelid = c.reltoastrelid AND
              c.relname = 'regress_tblspace_test_tbl') \gset
+
 REINDEX (TABLESPACE regress_tblspace) TABLE regress_tblspace_test_tbl;
-SELECT c.relname FROM pg_class c, pg_tablespace s
-  WHERE c.reltablespace = s.oid AND s.spcname = 'regress_tblspace'
-  ORDER BY c.relname;
+
+SELECT 'regress_tblspace_test_tbl_idx' IN
+  (SELECT c.relname
+  FROM pg_class c, pg_tablespace s
+  WHERE c.reltablespace = s.oid AND s.spcname = 'regress_tblspace');
+
 ALTER TABLE regress_tblspace_test_tbl SET TABLESPACE regress_tblspace;
 ALTER TABLE regress_tblspace_test_tbl SET TABLESPACE pg_default;
-SELECT c.relname FROM pg_class c, pg_tablespace s
-  WHERE c.reltablespace = s.oid AND s.spcname = 'regress_tblspace'
-  ORDER BY c.relname;
+
+SELECT 'regress_tblspace_test_tbl_idx' IN
+  (SELECT c.relname
+  FROM pg_class c, pg_tablespace s
+  WHERE c.reltablespace = s.oid AND s.spcname = 'regress_tblspace');
+
 -- Move back to the default tablespace.
 ALTER INDEX regress_tblspace_test_tbl_idx SET TABLESPACE pg_default;
-SELECT c.relname FROM pg_class c, pg_tablespace s
+SELECT c.relname <> 'regress_tblspace_test_tbl_idx',
+       c.relname <> 'regress_tblspace_test_tbl'
+  FROM pg_class c, pg_tablespace s
   WHERE c.reltablespace = s.oid AND s.spcname = 'regress_tblspace'
   ORDER BY c.relname;
+
 REINDEX (TABLESPACE regress_tblspace, CONCURRENTLY) TABLE regress_tblspace_test_tbl;
-SELECT c.relname FROM pg_class c, pg_tablespace s
-  WHERE c.reltablespace = s.oid AND s.spcname = 'regress_tblspace'
-  ORDER BY c.relname;
+
+SELECT 'regress_tblspace_test_tbl_idx' IN
+  (SELECT c.relname
+  FROM pg_class c, pg_tablespace s
+  WHERE c.reltablespace = s.oid AND s.spcname = 'regress_tblspace');
+
 SELECT relfilenode = :main_filenode AS main_same FROM pg_class
   WHERE relname = 'regress_tblspace_test_tbl_idx';
 SELECT relfilenode = :toast_filenode as toast_same FROM pg_class
diff --git a/src/test/regress/sql/test_setup.sql b/src/test/regress/sql/test_setup.sql
index 02c0c84c3a..8439b38d21 100644
--- a/src/test/regress/sql/test_setup.sql
+++ b/src/test/regress/sql/test_setup.sql
@@ -23,6 +23,10 @@ SET synchronous_commit = on;
 --
 GRANT ALL ON SCHEMA public TO public;
 
+-- Create a tablespace we can use in tests.
+SET allow_in_place_tablespaces = true;
+CREATE TABLESPACE regress_tblspace LOCATION '';
+
 --
 -- These tables have traditionally been referenced by many tests,
 -- so create and populate them.  Insert only non-error values here.
-- 
2.34.1

