Read committed (especially section 13.2.1) is the default read level in PostgreSQL.
This read level will give you a snapshot of what has been committed before your transaction starts. It will allow other transactions to read and write to your table, you just won't be able to see any writes made after the start of your transaction.
Does this only apply to SELECT
s run in the context of a transaction, or also to SELECT
s that are run alone, without an explicit transaction around them (i.e., just a single SELECT
statement, e.g. manually entered to the REPL)?
Again from the documentation:
PostgreSQL actually treats every SQL statement as being executed within
transaction. If you do not issue a BEGIN
command, then each individual
statement has an implicit BEGIN
and (if successful) COMMIT
wrapped
around it. A group of statements surrounded by BEGIN
and COMMIT
is
sometimes called a transaction block.
So, individual SELECTs
(one after the other) may see different contexts. There will be a difference if another user has modified your tables in the meantime.
However (and this should clarify) in a query with subselects - the individual subqueries will be consistent i.e. see the same context - in the same statement! Within the same transaction, you will not see changes made by other transactions but you will see changes made by previous changes within a single transaction of your own.
If you do not want this, then repeatable read
or serializable
may be a better option.