PostgreSQL – Execute Multiple Commands on Table Simultaneously

postgresql

How robust is Postgres when it comes to running multiple commands on a table at the same time?

Can I for instance, set a primary key and create a couple of indexes using multiple separate commands at the same time?

How about a query that will only affect/change data in one column? Is it ok to run other commands as long as they don't interfere with the column affected by the first command?

Best Answer

You can't run multiple DDL statements at the same time because each statement will request an exclusive lock. So if you add a primary or foreign key, the table is locked.

You can however create multiple indexes at the same time using the concurrently option.

Without the concurrently this will also block write access to the table while the index is created (read access is still possible in any case).