@@ -3197,49 +3197,44 @@ swap_toast_names(Oid relid1, Oid toastrelid1, Oid relid2, Oid toastrelid2)
31973197 Oid toastidxid ;
31983198
31993199 /*
3200- * As we haven't changed tuple descriptor, both relation do or both do not
3201- * have TOAST - see toasting.c:needs_toast_table().
3200+ * If relid1 no longer needs TOAST, we don't even rename that of relid2.
32023201 */
32033202 if (!OidIsValid (toastrelid1 ))
3204- {
3205- if (OidIsValid (toastrelid2 ))
3206- elog (ERROR , "Unexpected TOAST relation exists" );
32073203 return ;
3208- }
3209- if (!OidIsValid (toastrelid2 ))
3210- elog (ERROR , "Missing TOAST relation" );
32113204
3212- /*
3213- * Added underscore should be enough to keep names unique (at least within
3214- * the pg_toast namespace). This assumption makes name retrieval
3215- * unnecessary.
3216- */
3217- snprintf (name , NAMEDATALEN , "pg_toast_%u_" , relid1 );
3218- RenameRelationInternal (toastrelid2 , name , true, false);
3205+ if (OidIsValid (toastrelid2 ))
3206+ {
3207+ /*
3208+ * Added underscore should be enough to keep names unique (at least
3209+ * within the pg_toast namespace). This assumption makes name
3210+ * retrieval unnecessary.
3211+ */
3212+ snprintf (name , NAMEDATALEN , "pg_toast_%u_" , relid1 );
3213+ RenameRelationInternal (toastrelid2 , name , true, false);
32193214
3220- snprintf (name , NAMEDATALEN , "pg_toast_%u_index_" , relid1 );
3215+ snprintf (name , NAMEDATALEN , "pg_toast_%u_index_" , relid1 );
32213216#if PG_VERSION_NUM < 130000
3222- /* NoLock as RenameRelationInternal() did not release its lock. */
3223- toastidxid = get_toast_index (toastrelid2 );
3217+ /* NoLock as RenameRelationInternal() did not release its lock. */
3218+ toastidxid = get_toast_index (toastrelid2 );
32243219#else
3225- /* TOAST relation is locked, but not its indexes. */
3226- toastidxid = toast_get_valid_index (toastrelid2 , AccessExclusiveLock );
3220+ /* TOAST relation is locked, but not its indexes. */
3221+ toastidxid = toast_get_valid_index (toastrelid2 , AccessExclusiveLock );
32273222#endif
3228- /*
3229- * Pass is_index=false so that even the index is locked in
3230- * AccessExclusiveLock mode. ShareUpdateExclusiveLock mode (allowing
3231- * concurrent read / write access to the index or even its renaming)
3232- * should not be a problem at this stage of table squeezing, but it'd also
3233- * bring little benefit (the table is locked exclusively, so no one should
3234- * need read / write access to the TOAST indexes).
3235- */
3236- RenameRelationInternal (toastidxid , name , true, false);
3237- CommandCounterIncrement ();
3223+ /*
3224+ * Pass is_index=false so that even the index is locked in
3225+ * AccessExclusiveLock mode. ShareUpdateExclusiveLock mode (allowing
3226+ * concurrent read / write access to the index or even its renaming)
3227+ * should not be a problem at this stage of table squeezing, but it'd
3228+ * also bring little benefit (the table is locked exclusively, so no
3229+ * one should need read / write access to the TOAST indexes).
3230+ */
3231+ RenameRelationInternal (toastidxid , name , true, false);
3232+ CommandCounterIncrement ();
3233+ }
32383234
32393235 /* Now set the desired names on the TOAST stuff of relid1. */
32403236 snprintf (name , NAMEDATALEN , "pg_toast_%u" , relid1 );
32413237 RenameRelationInternal (toastrelid1 , name , true, false);
3242- /* NoLock as RenameRelationInternal() did not release its lock. */
32433238#if PG_VERSION_NUM < 130000
32443239 /* NoLock as RenameRelationInternal() did not release its lock. */
32453240 toastidxid = get_toast_index (toastrelid1 );
0 commit comments