From c399e185725319e501482e44574eab5830bad0b3 Mon Sep 17 00:00:00 2001
From: Vignesh C <vignesh21@gmail.com>
Date: Wed, 14 Jun 2023 11:35:27 +0530
Subject: [PATCH] pg_get_indexdef modification to access catalog based on the
 TxnSnapshot.

Change pg_get_indexdef() to access catalog based on transaction-snapshot by
setting historic snapshot which will prevent accessing a future catalog data
which might occur due to concurrent catalog change. The motivation is to fix
the issue mentioned atop pg_dump.c
---
 src/backend/utils/adt/ruleutils.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/src/backend/utils/adt/ruleutils.c b/src/backend/utils/adt/ruleutils.c
index d3a973d86b..3f80c77a00 100644
--- a/src/backend/utils/adt/ruleutils.c
+++ b/src/backend/utils/adt/ruleutils.c
@@ -1154,10 +1154,17 @@ pg_get_indexdef(PG_FUNCTION_ARGS)
 
 	prettyFlags = PRETTYFLAG_INDENT;
 
+	/*
+	 * Setup the snapshot to get the catalog information using this snapshot
+	 * which will prevent accessing a future catalog data which has occurred
+	 * due to concurrent catalog change.
+	 */
+	SetupHistoricSnapshot(GetActiveSnapshot(), NULL);
 	res = pg_get_indexdef_worker(indexrelid, 0, NULL,
 								 false, false,
 								 false, false,
 								 prettyFlags, true);
+	TeardownHistoricSnapshot(false);
 
 	if (res == NULL)
 		PG_RETURN_NULL();
-- 
2.34.1

