porsager / porsager/postgres

Concurrent calls to notify in transaction results in deadlock, but pg_notify works

Open
#966 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
8.7k
Forks
374
Avg merge
11d 16h
Merged PRs (30d)
1

Description

PoC

  const sql = postgres({ ssl: "require", max: 10 });

  const promises = [];
  for (let i = 0; i < 10; i++) {
    promises.push((async () => {
      console.log(`beginning thread ${i}`)
      await sql.begin(async (sql) => {
        console.log(`notifying ${i}`)

        await sql.notify('test_channel', i.toString());

        console.log(`done ${i}`)
      })
      console.log(`exiting thread ${i}`)
    })());
  }

  await Promise.all(promises);

  console.log('done promises');

  await sql.end();

Output:

beginning thread 0
beginning thread 1
beginning thread 2
beginning thread 3
beginning thread 4
beginning thread 5
beginning thread 6
beginning thread 7
beginning thread 8
beginning thread 9
notifying 6
notifying 7
notifying 4
notifying 3
notifying 1
notifying 9
notifying 8
notifying 0
notifying 5
notifying 2
^C

Changing max=11 results in this

beginning thread 0
beginning thread 1
beginning thread 2
beginning thread 3
beginning thread 4
beginning thread 5
beginning thread 6
beginning thread 7
beginning thread 8
beginning thread 9
notifying 4
notifying 3
notifying 9
notifying 1
notifying 2
notifying 8
notifying 0
notifying 5
notifying 7
notifying 6
done 4
exiting thread 4
done 3
done 9
done 1
done 2
done 8
exiting thread 1
exiting thread 3
exiting thread 8
done 5
done 7
done 6
exiting thread 2
exiting thread 9
done 0
exiting thread 5
exiting thread 0
exiting thread 6
exiting thread 7
done promises

Interestingly, replacing with await sql`select pg_notify('test_channel', ${i})`; results in no deadlock

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the provided concurrent sql.begin/sql.notify PoC with max 10, then compare it with the direct pg_notify query and max 11. Trace the transaction-aware notify path involved in sql.notify; done means concurrent transactional notifications complete without deadlocking at max 10.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, nodejs, postgresql
Domain
backend, databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.