Ensure total order in table via constraint Suppose I have a table

create table log (id serial, logged_at timestamp, event_type integer)


Can I efficiently (without making every row writable only once or by some special user) ensure that
∄ (r1, r2) ∈ log X log (r1.id &gt; r2.id ∧ r2.logged_at &gt; r1.logged_at)

So events in log partially ordered by time and we can always have total order via monotonically increasing sequence.
This sounds like an occurrence of: can the database enforce constraints across rows?
(I assume that the X sign in the formula means "such that")

As a general rule, the answer is no, because when a row is being modified by a transaction, the state of the other rows is always uncertain due to possible modifications by not-yet-committed concurrent transactions.

An application may handle this correctly by implementing an adequate locking strategy preventing concurrent writes, or checking the constraint at the serializable isolation level, but that does not prevent a random session to just issue an UPDATE in the table without these safety measures, that would potentially result in quietly violating the constraint.

There are few kinds of table-level constraints that the SQL engine knows how to enforce, like unique constraints and exclusion constraints, but your formula doesn't look like an expression that could be used by such constraints.