Home > front end >  Does a SELECT COUNT(*) on a PostgreSQL table lock it?
Does a SELECT COUNT(*) on a PostgreSQL table lock it?

Time:05-19

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?

CodePudding user response:

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.

CodePudding user response:

Every type of access to a table takes a lock. So Yes, it does take a lock. But it looks like you have a different question because a question like this only pops up when you have a problem. But we don't know what problem that is.

All the different types of locks and how they interact with each other, can be found in the manual. In most cases you don't have any issues with concurrency. You can have dozens of concurrent SELECT statements on the same table without any locking issues.

  • Related