From 1c9a15c9a4c6398160c5b568735ec89b186b9eb5 Mon Sep 17 00:00:00 2001
From: Justin Pryzby <pryzbyj@telsasoft.com>
Date: Fri, 6 Mar 2020 12:18:02 -0600
Subject: [PATCH v10 5/5] Fix issue moving system tables to new tablespace (if
 allow_system_table_mods=1)

I reported here PANIC and smgr corruption:
https://www.postgresql.org/message-id/20200229145304.GI29456%40telsasoft.com

The original logic from Alexey's v3 patch seems to work fine.
The minimal fix appears to be to set rd_rel->relfilenode.
https://www.postgresql.org/message-id/attachment/104272/v3-0001-Allow-REINDEX-and-REINDEX-CONCURRENTLY-to-change-.patch

Alexey had updated RelationSetNewRelfilenode to take a tablespace parameter,
but Michael didn't like that (although I don't understand why).
https://www.postgresql.org/message-id/827a9139-e02b-2dbf-5c6c-a6fbcaa1739a%40postgrespro.ru

So the later patches call RelationDropStorage() directly, but I reported issue
with system relations, which otherwise seem to work okay.
---
 src/backend/catalog/index.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/src/backend/catalog/index.c b/src/backend/catalog/index.c
index 4942a5513c..12c7e771d4 100644
--- a/src/backend/catalog/index.c
+++ b/src/backend/catalog/index.c
@@ -3531,6 +3531,8 @@ reindex_index(Oid indexId, Oid tablespaceOid, bool skip_constraint_checks,
 		/* Update the pg_class row */
 		rd_rel->reltablespace = (tablespaceOid == MyDatabaseTableSpace) ?
 			InvalidOid : tablespaceOid;
+		rd_rel->relfilenode = GetNewRelFileNode(tablespaceOid, pg_class,
+				heapRelation->rd_rel->relpersistence);
 		CatalogTupleUpdate(pg_class, &tuple->t_self, tuple);
 
 		heap_freetuple(tuple);
-- 
2.17.0

