Search code examples
postgresqllockingrow

Does a SELECT COUNT(*) on a PostgreSQL table lock it?


Does performing a SELECT COUNT(*) on a PostgreSQL table lock it? If so is there a way around it to behave like WITH (nolock) in SQL Server?


Solution

  • No.

    A SELECT never locks a table in Postgres.

    Or more precisely: it only takes a very weak shared lock that prevents DDL on the table.

    In Postgres, readers never block writers and writers never block readers.