Sobes.tech
Junior — Middle

What is the phenomenon of phantom reading and how does it manifest in database transactions?

sobes.tech AI

Answer from AI

Phantom read is an anomaly in database transactions that occurs when a repeated query within the same transaction returns different sets of rows because another transaction has added or deleted rows that match the query condition.

For example, transaction T1 executes a query to select all records with a certain condition. Between two such queries, another transaction T2 inserts a new record that meets the condition. On the second query in T1, a "phantom" appears — a new row that was not present during the first read.

This manifests at transaction isolation levels below Serializable, such as Read Committed or Repeatable Read (in some DBMS). To prevent phantom reads, the Serializable isolation level or special locking mechanisms are used.

What is the phenomenon of phantom reading and how… - sobes.tech