Why does PostgreSQL have pg_locks
John Nyingi

John Nyingi @j0nimost

About: Backend Dev

Location:
Nairobi, Kenya
Joined:
Feb 22, 2017

Why does PostgreSQL have pg_locks

Publish Date: Jan 27 '19
5 8

Debugging is frustating
Earlier this week I had an interview at Andela, they offered us a challenge to build an API using postgreSQL and Flask. In my 3 years working as a software developer I had never interacted with PostgreSQL.

Fast forward I started experiencing weird bugs in my API. Postgres would freeze from time to time and this was because postgres would lock my db requests. I still don't understand this, why? What is the purpose of pg_locks?

Comments 8 total

  • rhymes
    rhymesJan 27, 2019

    Hi John, can you expand on what you're doing or how you're using PostgreSQL through flask?

    pg_locks is a view of all the locks in the DB server.

    Are you manually setting locks?

    • John Nyingi
      John NyingiJan 27, 2019

      Not really, I was using Pyscopg2 to handle queries between Flask and PostgreSQL. I though ideally instead of locking requests and freezing it should throw an error

      • rhymes
        rhymesJan 27, 2019

        Psycopg2 directly? With no other libraries? It might be that you're not closing connections after the end of the request, hence the lock.

        • John Nyingi
          John NyingiJan 27, 2019

          I fixed that, I just want to know why pg_locks

          • rhymes
            rhymesJan 27, 2019

            Ok, got it. It's a list of all the locks held by various connections

            • John Nyingi
              John NyingiJan 27, 2019

              So, they lock requests to the DB? I thought a good practice would to return an error instead of locking requests.

              • rhymes
                rhymesJan 27, 2019

                Oh, sorry my bad, I thought you wanted to understand the purpose of the view pg_locks, which is the name of a special view where PostgreSQL lists all the active locks (a locked table, or a row or others). Instead I think that you're actually asking what locks are for.

                I thought a good practice would to return an error instead of locking requests.

                It depends. If every concurrent operation would result in an error sent to the caller, those database would quickly become a single user and single process server.

                Not very unlike locks in multithreading, database locks serve the purpose of protecting access to shared resources (tables, rows, data).

                In a system where tens if not hundreds of connections operate on the same dataset, there has to be a system to avoid that two connections invalidate each other's operation (or in other cases causing a deadlock). Most RDBMS system promise you they are ACID, which means atomic, consistent, isolated and durable.

                Locks are a way to do that. An operation comes to the DB, declares they need a resource, finishes its own modification, then releases such resource, so that the next operation can do the same. If they didn't lock their resource two operations might overwrite each other's data causing disasters.

                Locks are not errors, they are handled automatically by the DB, unless you're doing something that abuses resources so you need to monitor the pg_locks table and understand what you're doing wrong.

                Lock problems are not uncommon, but they are not that easy to trigger either.

Add comment