sql

grav 2025-01-24T22:32:35.115409Z

I'm trying to provoke a deadlock with Pg, but instead of erroring out, it just hangs indefinitely, nothing in the pg log [thread]

grav 2025-01-24T22:32:51.657999Z

(def config2
  {:dbtype "postgresql"
   :host "localhost"
   :dbname "postgres"
   :user "postgres"
   :password "postgres"})

(comment
  ;; example from Designing data-intensive applications, pg 235 + 236
  (let [ds (jdbc/get-datasource config2)
        conn1 (jdbc/get-connection ds)
        conn2 (jdbc/get-connection ds)
        _ (jdbc/execute! conn1 ["SET statement_timeout = '5s'"])
        _ (jdbc/execute! conn1 ["DROP TABLE IF EXISTS listings"])
        _ (jdbc/execute! conn1 ["DROP TABLE IF EXISTS invoices"])
        _ (jdbc/execute! conn1 ["CREATE TABLE listings (id serial, buyer text)"])
        _ (jdbc/execute! conn1 ["CREATE TABLE invoices (id serial, listing_id bigint, recipient text)"])
        [{:keys [id]}] (jdbc/execute! conn1 ["INSERT INTO listings DEFAULT VALUES RETURNING id"])
        _ (jdbc/execute! conn1 ["INSERT INTO invoices (listing_id) VALUES (?)" id])

        ;; go!
        _ (jdbc/execute! conn1 ["BEGIN"])
        _ (jdbc/execute! conn1 ["UPDATE listings SET buyer = ? WHERE id = ?" "alice" 1])
        _ (jdbc/execute! conn2 ["BEGIN"])
        _ (jdbc/execute! conn2 ["UPDATE listings SET buyer = ? WHERE id = ?" "bob" 1])
        _ (jdbc/execute! conn2 ["UPDATE invoices SET recipient = ? WHERE listing_id = ?" "bob" 1])
        _ (jdbc/execute! conn2 ["COMMIT"])
        _ (jdbc/execute! conn1 ["UPDATE invoices SET recipient = ? WHERE listing_id = ?" "alice" 1])
        _ (jdbc/execute! conn1 ["COMMIT"])
        _ 42]
    "ok"))

grav 2025-01-24T22:36:50.312009Z

This is pg_stat_activity:

SELECT wait_event_type,wait_event,state,query FROM pg_stat_activity where application_name='PostgreSQL JDBC Driver';
+-----------------+---------------+---------------------+----------------------------------------------+
| wait_event_type | wait_event    | state               | query                                        |
|-----------------+---------------+---------------------+----------------------------------------------|
| Client          | ClientRead    | idle in transaction | UPDATE listings SET buyer = $1 WHERE id = $2 |
| Lock            | transactionid | active              | UPDATE listings SET buyer = $1 WHERE id = $2 |
+-----------------+---------------+---------------------+----------------------------------------------+

grav 2025-01-24T22:37:35.049779Z

deadlock timeout is 1s (default), isolation level is default (also tried a couple of others, but I guess it shouldn't really change the fact that it'll deadlock

grav 2025-01-24T22:37:51.761039Z

There's something I'm not really getting 😅

grav 2025-01-24T22:44:42.892769Z

D'uh .. I need threads

p-himik 2025-01-24T22:44:50.646929Z

Was just about to mention. :)

😅 1
grav 2025-01-24T22:45:39.313329Z

I must have been programming too much with async in typescript these last days ... 😄 It's getting to me

p-himik 2025-01-24T22:46:00.717029Z

TS strikes again!

😆 1