From c9f397759e5ca91cedebebb4cc490995dd7e498a Mon Sep 17 00:00:00 2001 From: Graham Date: Tue, 29 Mar 2022 17:06:33 +0100 Subject: [PATCH] Use advisory locks to prevent concurrent view refreshes Signed-off-by: Graham --- .../org/openrs2/archive/cache/CacheImporter.kt | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/archive/src/main/kotlin/org/openrs2/archive/cache/CacheImporter.kt b/archive/src/main/kotlin/org/openrs2/archive/cache/CacheImporter.kt index 3e73373999..f52a3dc2b8 100644 --- a/archive/src/main/kotlin/org/openrs2/archive/cache/CacheImporter.kt +++ b/archive/src/main/kotlin/org/openrs2/archive/cache/CacheImporter.kt @@ -1330,6 +1330,21 @@ public class CacheImporter @Inject constructor( public suspend fun refreshViews() { database.execute { connection -> + connection.prepareStatement(""" + SELECT pg_try_advisory_lock(0) + """.trimIndent()).use { stmt -> + stmt.executeQuery().use { rows -> + if (!rows.next()) { + throw IllegalStateException() + } + + val locked = rows.getBoolean(1) + if (!locked) { + return@execute + } + } + } + connection.prepareStatement( """ REFRESH MATERIALIZED VIEW CONCURRENTLY index_stats