]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Check that the tranche name is unique in RequestNamedLWLockTranche
authorHeikki Linnakangas <heikki.linnakangas@iki.fi>
Sun, 5 Apr 2026 18:05:20 +0000 (21:05 +0300)
committerHeikki Linnakangas <heikki.linnakangas@iki.fi>
Sun, 5 Apr 2026 18:05:20 +0000 (21:05 +0300)
You could request two tranches with same name, but things would get
confusing when you called GetNamedLWLockTranche() to get the LWLocks
allocated for them; it would always return the first tranche with the
name. That doesn't make sense, so forbid duplicates.

We still allow duplicates with LWLockNewTrancheId(). That works better
as you don't use the name to look up the tranche ID later. It's still
confusing in wait events, for example, but it's not dangerous in the
same way.

Reviewed-by: Sami Imseih <samimseih@gmail.com>
Discussion: https://www.postgresql.org/message-id/463a28db-0c0b-4af6-bac6-3891828bbbfe@iki.fi

src/backend/storage/lmgr/lwlock.c

index 5cb696490d696e5a1213017bcb20528eb1da74eb..98138cb09d10af52b5203c88eea05e74bff35014 100644 (file)
@@ -649,6 +649,13 @@ RequestNamedLWLockTranche(const char *tranche_name, int num_lwlocks)
                                 errdetail("No more than %d tranches may be registered.",
                                                   MAX_USER_DEFINED_TRANCHES)));
 
+       /* Check that the name isn't already in use */
+       foreach_ptr(NamedLWLockTrancheRequest, existing, NamedLWLockTrancheRequests)
+       {
+               if (strcmp(existing->tranche_name, tranche_name) == 0)
+                       elog(ERROR, "requested tranche \"%s\" is already registered", tranche_name);
+       }
+
        if (IsPostmasterEnvironment)
                oldcontext = MemoryContextSwitchTo(PostmasterContext);
        else