Why am I getting old data?
-
29-09-2020 - |
Question
Today I discovered something weird on PostgreSQL 9.5. (I have no idea whether this is because of beta or not.) When I want to fetch data, I get old and deleted data from query. I then do VACUUM FULL
and then I get proper data (which is empty).
Am I missing something here? What might be the reason that PostgreSQL returns old data?
Note: Autovacuum is ON
.
Solution
You will find the chapter Read Committed Isolation Level of the Postgres 9.5 manual instructive:
Read Committed is the default isolation level in PostgreSQL. When a transaction uses this isolation level, a
SELECT
query (without aFOR UPDATE/SHARE
clause) sees only data committed before the query began; it never sees either uncommitted data or changes committed during query execution by concurrent transactions.
Bold emphasis mine.
Until your transaction is committed, changes are not visible to other transactions. VACUUM
has nothing to do with this, it only cleans up dead tuples after all transactions are finished with them.