Add a couple of functions for PostgreSQL advisory locks

Use symbol-hash to convert a symbol to the number for the lock. I'm hoping
this is OK, and it seems to be stable.
This commit is contained in:
Christopher Baines 2019-07-12 19:51:42 +01:00
parent 8f956aa6c2
commit fde1000cb3

View file

@ -18,7 +18,10 @@
(define-module (guix-data-service database)
#:use-module (squee)
#:export (with-postgresql-connection
with-postgresql-transaction))
with-postgresql-transaction
with-advisory-session-lock
obtain-advisory-transaction-lock))
;; TODO This isn't exported for some reason
(define pg-conn-finish
@ -54,3 +57,26 @@
result))
(lambda (key . args)
(exec-query conn "ROLLBACK;"))))
(define (with-advisory-session-lock conn lock f)
(let ((lock-number (number->string (symbol-hash lock))))
(exec-query conn
"SELECT pg_advisory_lock($1)"
(list lock-number))
(with-throw-handler #t
(lambda ()
(let ((result (f)))
(exec-query conn
"SELECT pg_advisory_unlock($1)"
(list lock-number))
result))
(lambda (key . args)
(exec-query conn
"SELECT pg_advisory_unlock($1)"
(list lock-number))))))
(define (obtain-advisory-transaction-lock conn lock)
(let ((lock-number (number->string (symbol-hash lock))))
(exec-query conn
"SELECT pg_advisory_xact_lock($1)"
(list lock-number))))