1515#include "access/multixact.h"
1616#include "access/sysattr.h"
1717#if PG_VERSION_NUM >= 130000
18+ #include "access/toast_internals.h"
1819#include "access/xlogutils.h"
1920#endif
2021#if PG_VERSION_NUM >= 150000
@@ -136,7 +137,9 @@ static bool perform_final_merge(Oid relid_src, Oid *indexes_src, int nindexes,
136137static void swap_relation_files (Oid r1 , Oid r2 );
137138static void swap_toast_names (Oid relid1 , Oid toastrelid1 , Oid relid2 ,
138139 Oid toastrelid2 );
140+ #if PG_VERSION_NUM < 130000
139141static Oid get_toast_index (Oid toastrelid );
142+ #endif
140143
141144/*
142145 * The maximum time to hold AccessExclusiveLock during the final
@@ -3059,6 +3062,7 @@ swap_relation_files(Oid r1, Oid r2)
30593062 relform2 -> reltablespace = swaptemp ;
30603063
30613064 Assert (relform1 -> relpersistence == relform2 -> relpersistence );
3065+ Assert (relform1 -> relam == relform2 -> relam );
30623066
30633067 swaptemp = relform1 -> reltoastrelid ;
30643068 relform1 -> reltoastrelid = relform2 -> reltoastrelid ;
@@ -3207,23 +3211,19 @@ swap_toast_names(Oid relid1, Oid toastrelid1, Oid relid2, Oid toastrelid2)
32073211
32083212 /*
32093213 * Added underscore should be enough to keep names unique (at least within
3210- * the pg_toast tablespace ). This assumption makes name retrieval
3214+ * the pg_toast namespace ). This assumption makes name retrieval
32113215 * unnecessary.
32123216 */
32133217 snprintf (name , NAMEDATALEN , "pg_toast_%u_" , relid1 );
32143218 RenameRelationInternal (toastrelid2 , name , true, false);
32153219
3216- /*
3217- * XXX While toast_open_indexes (PG core) can retrieve multiple indexes,
3218- * get_toast_index() expects exactly one. If this restriction should be
3219- * released someday, either generate the underscore-terminated names as
3220- * above or copy names of the indexes of toastrel1 (the number of indexes
3221- * should be identical). Order should never be important, as toastrel2
3222- * will eventually be dropped.
3223- */
3224- toastidxid = get_toast_index (toastrelid2 );
32253220 snprintf (name , NAMEDATALEN , "pg_toast_%u_index_" , relid1 );
3226-
3221+ #if PG_VERSION_NUM < 130000
3222+ toastidxid = get_toast_index (toastrelid2 );
3223+ #else
3224+ /* NoLock as RenameRelationInternal() did not release its lock. */
3225+ toastidxid = toast_get_valid_index (toastrelid2 , NoLock );
3226+ #endif
32273227 /*
32283228 * Pass is_index=false so that even the index is locked in
32293229 * AccessExclusiveLock mode. ShareUpdateExclusiveLock mode (allowing
@@ -3238,12 +3238,18 @@ swap_toast_names(Oid relid1, Oid toastrelid1, Oid relid2, Oid toastrelid2)
32383238 /* Now set the desired names on the TOAST stuff of relid1. */
32393239 snprintf (name , NAMEDATALEN , "pg_toast_%u" , relid1 );
32403240 RenameRelationInternal (toastrelid1 , name , true, false);
3241+ /* NoLock as RenameRelationInternal() did not release its lock. */
3242+ #if PG_VERSION_NUM < 130000
32413243 toastidxid = get_toast_index (toastrelid1 );
3244+ #else
3245+ toastidxid = toast_get_valid_index (toastrelid1 , NoLock );
3246+ #endif
32423247 snprintf (name , NAMEDATALEN , "pg_toast_%u_index" , relid1 );
32433248 RenameRelationInternal (toastidxid , name , true, false);
32443249 CommandCounterIncrement ();
32453250}
32463251
3252+ #if PG_VERSION_NUM < 130000
32473253/*
32483254 * The function is called after RenameRelationInternal() which does not
32493255 * release the lock it acquired.
@@ -3266,6 +3272,7 @@ get_toast_index(Oid toastrelid)
32663272
32673273 return result ;
32683274}
3275+ #endif
32693276
32703277/*
32713278 * Retrieve the "fillfactor" storage option in a convenient way, so we don't
0 commit comments