npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

pglockanalyze

v0.1.0

Published

anzlye postgres locking behavior

Downloads

3

Readme

pglockanalyze: analyze postgres locking behavior

pglockanalyze examines queries from the Postgresql's "pg_locks" system catalog and produces some summary output about which backend processes are blocked on which others.

This was really a one-off written to deal with a specific problem, but may be helpful for others in the future.

Specifically, the Postgres wiki suggests using this query to get a "flat view of blocking [processes]":

SELECT 
    waiting.locktype           AS waiting_locktype,
    waiting.relation::regclass AS waiting_table,
    waiting_stm.query          AS waiting_query,
    waiting.mode               AS waiting_mode,
    waiting.pid                AS waiting_pid,
    other.locktype             AS other_locktype,
    other.relation::regclass   AS other_table,
    other_stm.query            AS other_query,
    other.mode                 AS other_mode,
    other.pid                  AS other_pid,
    other.GRANTED              AS other_granted
FROM
    pg_catalog.pg_locks AS waiting
JOIN
    pg_catalog.pg_stat_activity AS waiting_stm
    ON (
        waiting_stm.pid = waiting.pid
    )
JOIN
    pg_catalog.pg_locks AS other
    ON (
        (
            waiting."database" = other."database"
        AND waiting.relation  = other.relation
        )
        OR waiting.transactionid = other.transactionid
    )
JOIN
    pg_catalog.pg_stat_activity AS other_stm
    ON (
        other_stm.pid = other.pid
    )
WHERE
    NOT waiting.GRANTED
AND
    waiting.pid <> other.pid

Each row in the pg_locks view represents a Postgresql backend process either holding or attempting to take a lock. Processes holding or attempting to take multiple locks may appear multiple times in the view.

The query above essentially selects all pairs of rows from "pg_locks" that are operating on the same lock, and then joins that information with the "pg_activity" information for each backend. In principle, this can show you when one process is blocking another, but interpretation is complicated by the fact that this query reports rows where neither process holds the lock (indicating they're both contending for the lock, but not necessarily blocking each other). It also reports rows for lock types that don't conflict. For example, the pids in a given row may both be attempting to take a shared (non-exclusive) lock on a table, in which case it's not a problem that they're both contending for it.

Using pglockanalyze

pglockanalyze is a Node.js program. You install it with:

$ npm install -g pglockanalyze

pglockanalyze takes on stdin the textual output you'd get by running this query using psql(1), and produces on stdout a report like this:

$ psql -c "..." > query.out
$ pglockanalyze < query.out
...
pid 93744: query: SELECT *, '488f0950-bf92-405a-8b77-ae700accca48' AS req_id FROM manta WHERE _key=$1
    wants AccessShareLock: may contend with 1 pids wanting AccessExclusiveLock (e.g., 42367)
    wants AccessShareLock: may contend with 983 pids wanting AccessShareLock (e.g., 93618)
    wants AccessShareLock: may contend with 9 pids wanting RowShareLock (e.g., 93737)
pid 93759: query: SELECT *, '8dce62c2-4f42-4c70-87be-d62b1c1f84f4' AS req_id FROM manta WHERE _key=$1 FOR UPDATE
    wants RowShareLock: may contend with 1 pids wanting AccessExclusiveLock (e.g., 42367)
    wants RowShareLock: may contend with 984 pids wanting AccessShareLock (e.g., 93618)
    wants RowShareLock: may contend with 8 pids wanting RowShareLock (e.g., 93737)
pid 91544: query: autovacuum: VACUUM ANALYZE public.manta (to prevent wraparound)
    holds ShareUpdateExclusiveLock: may contend with 1 pids wanting AccessExclusiveLock (e.g., 42367)
    holds ShareUpdateExclusiveLock: may contend with 984 pids wanting AccessShareLock (e.g., 93618)
    holds ShareUpdateExclusiveLock: may contend with 9 pids wanting RowShareLock (e.g., 93737)
pid 42367: query: DROP TRIGGER IF EXISTS count_directories ON manta
    holds AccessShareLock: may contend with 984 pids wanting AccessShareLock (e.g., 93618)
    holds AccessShareLock: may contend with 9 pids wanting RowShareLock (e.g., 93737)
    wants AccessExclusiveLock: may contend with 984 pids wanting AccessShareLock (e.g., 93618)
    wants AccessExclusiveLock: may contend with 9 pids wanting RowShareLock (e.g., 93737)

In order to make sense of this, you're really going to have to read and understand the Postgresql documentation on explicit locking.

There's also a "json" mode that converts the rows to json. You can then query them with tools like json(1):

$ pglockanalyze json < query.out > locks.json

Wish list

  • It would be easier (both for users and for the implementation of this program) if the input were actually just the contents of the "pg_locks" and "pg_activity" views, rather than pairs of pids that might be related.
  • Relatedly, instead of looking at pairs of processes blocking each other, it would be useful to pool together all information about a single lock.