toast_get_valid_index-lock-v35nm.patch

text/plain

Filename: toast_get_valid_index-lock-v35nm.patch
Type: text/plain
Part: 1
Message: Re: [HACKERS] WAL logging problem in 9.4.3?

Patch

Format: unified
File+
src/backend/access/common/toast_internals.c 2 2
src/backend/commands/cluster.c 1 1
Author:     Noah Misch <noah@leadboat.com>
Commit:     Noah Misch <noah@leadboat.com>

    During heap rebuild, lock any TOAST index until end of transaction.
    
    swap_relation_files() calls toast_get_valid_index() to find and lock
    this index, just before swapping with the rebuilt TOAST index.  The
    latter function releases the lock before returning.  Potential for
    mischief is low; a concurrent session can issue ALTER INDEX ... SET
    (fillfactor = ...), which is not alarming.  Nonetheless, changing
    pg_class.relfilenode without a lock is unconventional.  Back-patch to
    9.5 (all supported versions), because another fix needs this.
    
    Reviewed by FIXME.
    
    Discussion: https://postgr.es/m/20191226001521.GA1772687@rfd.leadboat.com

diff --git a/src/backend/access/common/toast_internals.c b/src/backend/access/common/toast_internals.c
index a971242..784028a 100644
--- a/src/backend/access/common/toast_internals.c
+++ b/src/backend/access/common/toast_internals.c
@@ -528,8 +528,8 @@ toast_get_valid_index(Oid toastoid, LOCKMODE lock)
 	validIndexOid = RelationGetRelid(toastidxs[validIndex]);
 
 	/* Close the toast relation and all its indexes */
-	toast_close_indexes(toastidxs, num_indexes, lock);
-	table_close(toastrel, lock);
+	toast_close_indexes(toastidxs, num_indexes, NoLock);
+	table_close(toastrel, NoLock);
 
 	return validIndexOid;
 }
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c
index b8c349f..650c926 100644
--- a/src/backend/commands/cluster.c
+++ b/src/backend/commands/cluster.c
@@ -1489,7 +1489,7 @@ finish_heap_swap(Oid OIDOldHeap, Oid OIDNewHeap,
 
 			/* Get the associated valid index to be renamed */
 			toastidx = toast_get_valid_index(newrel->rd_rel->reltoastrelid,
-											 AccessShareLock);
+											 NoLock);
 
 			/* rename the toast table ... */
 			snprintf(NewToastName, NAMEDATALEN, "pg_toast_%u",