0001-Remove-any-WAL-logging-for-ON-COMMIT-DELETE-ROWS.patch

application/octet-stream

Filename: 0001-Remove-any-WAL-logging-for-ON-COMMIT-DELETE-ROWS.patch
Type: application/octet-stream
Part: 0
Message: RE: temp table on commit delete rows performance issue

Patch

Same data as JSON: GET /api/v1/attachments/:id/patch the parsed metadata as JSON — format, series position, per-file stats; never the diff bytes. API reference →
Format: format-patch
Series: patch 0001
Subject: Remove any WAL logging for ON COMMIT DELETE ROWS
File+
src/backend/catalog/heap.c 10 4
src/backend/storage/lmgr/lmgr.c 19 7
src/backend/storage/lmgr/lock.c 4 2
src/include/storage/lmgr.h 1 0
src/include/storage/lock.h 2 1
From 66be19dbb1291a2451e7eed5eb2f49c0ef0cdbc7 Mon Sep 17 00:00:00 2001
From: Floris van Nee <florisvannee@optiver.com>
Date: Thu, 18 Jul 2024 15:28:59 +0200
Subject: [PATCH] Remove any WAL logging for ON COMMIT DELETE ROWS

This makes it so that truncate due to ON COMMIT DELETE rows
will not do any WAL logging. The affected code paths
are only used for non-rollbackable truncates so other things
should not be affected.
---
 src/backend/catalog/heap.c      | 14 ++++++++++----
 src/backend/storage/lmgr/lmgr.c | 26 +++++++++++++++++++-------
 src/backend/storage/lmgr/lock.c |  6 ++++--
 src/include/storage/lmgr.h      |  1 +
 src/include/storage/lock.h      |  3 ++-
 5 files changed, 36 insertions(+), 14 deletions(-)

diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c
index 00074c8a94..489e06b43f 100644
--- a/src/backend/catalog/heap.c
+++ b/src/backend/catalog/heap.c
@@ -2979,7 +2979,8 @@ RelationTruncateIndexes(Relation heapRelation)
 		IndexInfo  *indexInfo;
 
 		/* Open the index relation; use exclusive lock, just to be sure */
-		currentIndex = index_open(indexId, AccessExclusiveLock);
+		LockRelationOidExtended(indexId, AccessExclusiveLock, true);
+		currentIndex = index_open(indexId, NoLock);
 
 		/*
 		 * Fetch info needed for index_build.  Since we know there are no
@@ -2998,7 +2999,8 @@ RelationTruncateIndexes(Relation heapRelation)
 
 		/* Initialize the index and rebuild */
 		/* Note: we do not need to re-establish pkey setting */
-		index_build(heapRelation, currentIndex, indexInfo, true, false);
+		currentIndex->rd_indam->ambuild(heapRelation, currentIndex,
+											 indexInfo);
 
 		/* We're done with this index */
 		index_close(currentIndex, NoLock);
@@ -3026,7 +3028,9 @@ heap_truncate(List *relids)
 		Oid			rid = lfirst_oid(cell);
 		Relation	rel;
 
-		rel = table_open(rid, AccessExclusiveLock);
+		/* Take lock beforehand without logging to wal */
+		LockRelationOidExtended(rid, AccessExclusiveLock, true);
+		rel = table_open(rid, NoLock);
 		relations = lappend(relations, rel);
 	}
 
@@ -3077,7 +3081,9 @@ heap_truncate_one_rel(Relation rel)
 	toastrelid = rel->rd_rel->reltoastrelid;
 	if (OidIsValid(toastrelid))
 	{
-		Relation	toastrel = table_open(toastrelid, AccessExclusiveLock);
+		Relation	toastrel;
+		LockRelationOidExtended(toastrelid, AccessExclusiveLock, true);
+		toastrel = table_open(toastrelid, NoLock);
 
 		table_relation_nontransactional_truncate(toastrel);
 		RelationTruncateIndexes(toastrel);
diff --git a/src/backend/storage/lmgr/lmgr.c b/src/backend/storage/lmgr/lmgr.c
index 094522acb4..334587e20b 100644
--- a/src/backend/storage/lmgr/lmgr.c
+++ b/src/backend/storage/lmgr/lmgr.c
@@ -106,6 +106,18 @@ SetLocktagRelationOid(LOCKTAG *tag, Oid relid)
  */
 void
 LockRelationOid(Oid relid, LOCKMODE lockmode)
+{
+	LockRelationOidExtended(relid, lockmode, false);
+}
+
+/*
+ *		LockRelationOidExtended
+ *
+ * Extended version of LockRelationOid only to be used
+ * when disabling WAL logging
+ */
+void
+LockRelationOidExtended(Oid relid, LOCKMODE lockmode, bool disablewal)
 {
 	LOCKTAG		tag;
 	LOCALLOCK  *locallock;
@@ -113,7 +125,7 @@ LockRelationOid(Oid relid, LOCKMODE lockmode)
 
 	SetLocktagRelationOid(&tag, relid);
 
-	res = LockAcquireExtended(&tag, lockmode, false, false, true, &locallock);
+	res = LockAcquireExtended(&tag, lockmode, false, false, true, &locallock, disablewal);
 
 	/*
 	 * Now that we have the lock, check for invalidation messages, so that we
@@ -156,7 +168,7 @@ ConditionalLockRelationOid(Oid relid, LOCKMODE lockmode)
 
 	SetLocktagRelationOid(&tag, relid);
 
-	res = LockAcquireExtended(&tag, lockmode, false, true, true, &locallock);
+	res = LockAcquireExtended(&tag, lockmode, false, true, true, &locallock, false);
 
 	if (res == LOCKACQUIRE_NOT_AVAIL)
 		return false;
@@ -189,7 +201,7 @@ LockRelationId(LockRelId *relid, LOCKMODE lockmode)
 
 	SET_LOCKTAG_RELATION(tag, relid->dbId, relid->relId);
 
-	res = LockAcquireExtended(&tag, lockmode, false, false, true, &locallock);
+	res = LockAcquireExtended(&tag, lockmode, false, false, true, &locallock, false);
 
 	/*
 	 * Now that we have the lock, check for invalidation messages; see notes
@@ -251,7 +263,7 @@ LockRelation(Relation relation, LOCKMODE lockmode)
 						 relation->rd_lockInfo.lockRelId.dbId,
 						 relation->rd_lockInfo.lockRelId.relId);
 
-	res = LockAcquireExtended(&tag, lockmode, false, false, true, &locallock);
+	res = LockAcquireExtended(&tag, lockmode, false, false, true, &locallock, false);
 
 	/*
 	 * Now that we have the lock, check for invalidation messages; see notes
@@ -282,7 +294,7 @@ ConditionalLockRelation(Relation relation, LOCKMODE lockmode)
 						 relation->rd_lockInfo.lockRelId.dbId,
 						 relation->rd_lockInfo.lockRelId.relId);
 
-	res = LockAcquireExtended(&tag, lockmode, false, true, true, &locallock);
+	res = LockAcquireExtended(&tag, lockmode, false, true, true, &locallock, false);
 
 	if (res == LOCKACQUIRE_NOT_AVAIL)
 		return false;
@@ -1028,7 +1040,7 @@ ConditionalLockDatabaseObject(Oid classid, Oid objid, uint16 objsubid,
 					   objid,
 					   objsubid);
 
-	res = LockAcquireExtended(&tag, lockmode, false, true, true, &locallock);
+	res = LockAcquireExtended(&tag, lockmode, false, true, true, &locallock, false);
 
 	if (res == LOCKACQUIRE_NOT_AVAIL)
 		return false;
@@ -1107,7 +1119,7 @@ ConditionalLockSharedObject(Oid classid, Oid objid, uint16 objsubid,
 					   objid,
 					   objsubid);
 
-	res = LockAcquireExtended(&tag, lockmode, false, true, true, &locallock);
+	res = LockAcquireExtended(&tag, lockmode, false, true, true, &locallock, false);
 
 	if (res == LOCKACQUIRE_NOT_AVAIL)
 		return false;
diff --git a/src/backend/storage/lmgr/lock.c b/src/backend/storage/lmgr/lock.c
index 0400a50777..094e50925d 100644
--- a/src/backend/storage/lmgr/lock.c
+++ b/src/backend/storage/lmgr/lock.c
@@ -759,7 +759,7 @@ LockAcquire(const LOCKTAG *locktag,
 			bool dontWait)
 {
 	return LockAcquireExtended(locktag, lockmode, sessionLock, dontWait,
-							   true, NULL);
+							   true, NULL, false);
 }
 
 /*
@@ -782,7 +782,8 @@ LockAcquireExtended(const LOCKTAG *locktag,
 					bool sessionLock,
 					bool dontWait,
 					bool reportMemoryError,
-					LOCALLOCK **locallockp)
+					LOCALLOCK **locallockp,
+					bool disablewal)
 {
 	LOCKMETHODID lockmethodid = locktag->locktag_lockmethodid;
 	LockMethod	lockMethodTable;
@@ -908,6 +909,7 @@ LockAcquireExtended(const LOCKTAG *locktag,
 	 * matches the one in GetRunningTransactionLocks().
 	 */
 	if (lockmode >= AccessExclusiveLock &&
+		!disablewal &&
 		locktag->locktag_type == LOCKTAG_RELATION &&
 		!RecoveryInProgress() &&
 		XLogStandbyInfoActive())
diff --git a/src/include/storage/lmgr.h b/src/include/storage/lmgr.h
index ce15125ac3..c4e39df3d4 100644
--- a/src/include/storage/lmgr.h
+++ b/src/include/storage/lmgr.h
@@ -38,6 +38,7 @@ extern void RelationInitLockInfo(Relation relation);
 
 /* Lock a relation */
 extern void LockRelationOid(Oid relid, LOCKMODE lockmode);
+extern void LockRelationOidExtended(Oid relid, LOCKMODE lockmode, bool disablewal);
 extern void LockRelationId(LockRelId *relid, LOCKMODE lockmode);
 extern bool ConditionalLockRelationOid(Oid relid, LOCKMODE lockmode);
 extern void UnlockRelationId(LockRelId *relid, LOCKMODE lockmode);
diff --git a/src/include/storage/lock.h b/src/include/storage/lock.h
index cc1f6e78c3..ab7658ffef 100644
--- a/src/include/storage/lock.h
+++ b/src/include/storage/lock.h
@@ -558,7 +558,8 @@ extern LockAcquireResult LockAcquireExtended(const LOCKTAG *locktag,
 											 bool sessionLock,
 											 bool dontWait,
 											 bool reportMemoryError,
-											 LOCALLOCK **locallockp);
+											 LOCALLOCK **locallockp,
+											 bool disablewal);
 extern void AbortStrongLockAcquire(void);
 extern void MarkLockClear(LOCALLOCK *locallock);
 extern bool LockRelease(const LOCKTAG *locktag,
-- 
2.30.1