2
0
Fork 0
mirror of git://git.savannah.gnu.org/guix/data-service.git synced 2023-12-14 03:23:03 +01:00

Remove redundant postgresql connection when deleting derivations

This commit is contained in:
Christopher Baines 2023-02-14 20:59:21 +00:00
parent ebbcf36dc4
commit 1266d3d336

View file

@ -512,16 +512,13 @@ DELETE FROM derivations WHERE id = $1"
1))) 1)))
(with-postgresql-connection (define (delete-batch conn)
"data-deletion" (let* ((derivations
(lambda (conn) (with-time-logging "fetching batch of derivations"
(define (delete-batch conn) (map car
(let* ((derivations (exec-query
(with-time-logging "fetching batch of derivations" conn
(map car "
(exec-query
conn
"
SELECT DISTINCT derivation_id SELECT DISTINCT derivation_id
FROM derivation_outputs FROM derivation_outputs
WHERE NOT EXISTS ( WHERE NOT EXISTS (
@ -541,51 +538,53 @@ WHERE NOT EXISTS (
SELECT 1 FROM guix_revision_system_test_derivations SELECT 1 FROM guix_revision_system_test_derivations
WHERE derivation_id = derivation_outputs.derivation_id WHERE derivation_id = derivation_outputs.derivation_id
) LIMIT $1" ) LIMIT $1"
(list (number->string batch-size)))))) (list (number->string batch-size))))))
(derivations-count (length derivations))) (derivations-count (length derivations)))
(let ((deleted-count (let ((deleted-count
(with-time-logging (with-time-logging
(simple-format #f (simple-format #f
"Looking at ~A derivations" "Looking at ~A derivations"
derivations-count) derivations-count)
(fold (fold
(lambda (count result) (lambda (count result)
(+ result count)) (+ result count))
0 0
(map (map
(lambda (derivation-id) (lambda (derivation-id)
(unless (string->number derivation-id) (unless (string->number derivation-id)
(error (error
(simple-format #f "derivation-id: ~A is not a number" (simple-format #f "derivation-id: ~A is not a number"
derivation-id))) derivation-id)))
(with-thread-postgresql-connection (with-thread-postgresql-connection
(lambda (conn)
(with-postgresql-transaction
conn
(lambda (conn) (lambda (conn)
(with-postgresql-transaction (obtain-advisory-transaction-lock
conn conn
(lambda (conn) 'delete-unreferenced-derivations)
(obtain-advisory-transaction-lock
conn
'delete-unreferenced-derivations)
(exec-query (exec-query
conn conn
" "
SET CONSTRAINTS derivations_by_output_details_set_derivation_id_fkey DEFERRED") SET CONSTRAINTS derivations_by_output_details_set_derivation_id_fkey DEFERRED")
(maybe-delete-derivation conn (maybe-delete-derivation conn
derivation-id)))))) derivation-id))))))
derivations))))) derivations)))))
(simple-format (current-error-port) (simple-format (current-error-port)
"Deleted ~A derivations\n" "Deleted ~A derivations\n"
deleted-count) deleted-count)
deleted-count))) deleted-count)))
(with-postgresql-connection-per-thread (with-postgresql-connection-per-thread
"data-deletion-thread" "data-deletion-thread"
(lambda ()
(run-fibers
(lambda () (lambda ()
(run-fibers (with-thread-postgresql-connection
(lambda () (lambda (conn)
(let loop ((total-deleted 0)) (let loop ((total-deleted 0))
(let ((batch-deleted-count (delete-batch conn))) (let ((batch-deleted-count (delete-batch conn)))
(if (eq? 0 batch-deleted-count) (if (eq? 0 batch-deleted-count)