PostgreSQL: statement_timestamp() Misconceptions
PostgreSQL's statement_timestamp() doesn't always behave as its name suggests, and the official glossary doesn't clarify the semantics.
In a DO block, repeated calls to statement_timestamp() return the same value across the entire block. Even with a 3-second pg_sleep() between inserts, all timestamps remain identical — tied to when the whole block began execution.
Outside a DO block, inside an explicit transaction with BEGIN and END, the function does change per statement. Inserts separated by pg_sleep(3) get distinct timestamps, reflecting each statement's actual execution time.
The key difference: the DO block is received from the client as a single statement, so the timestamp is fixed at that point. In the explicit transaction example, each INSERT is a separate client-issued statement, so the timestamp updates.
This behavior depends entirely on how "statement" is defined in context — a nuance the PostgreSQL documentation doesn't address in its glossary.



