SQL theory · page 10 of 10

Transactions and security

ACID, MVCC, roles, row-level security and backups.

A database is shared: many people and programs read and change it at once, and not all of them should see everything. Transactions keep concurrent changes correct; roles and privileges decide who may do what.

Transactions

A transaction groups statements so they succeed or fail together: BEGIN, the statements, then COMMIT to keep them or ROLLBACK to undo them. Moving a reserve from one claim to another is two updates that must happen together or not at all. The guarantees are called ACID:

  • Atomicity: all of the transaction happens, or none of it.
  • Consistency: every constraint holds when it commits.
  • Isolation: concurrent transactions do not see each other's unfinished work.
  • Durability: once committed, a change survives a crash.

PostgreSQL implements isolation with MVCC (multi-version concurrency control): an update writes a new version of the row, so readers keep seeing the old one until the writer commits, and readers never block writers. The default isolation level, read committed, lets each statement see what was committed before it began; repeatable read and serializable give the whole transaction one consistent view.

Roles and privileges

A role is a user or a group. Privileges on objects are granted to roles: SELECT, INSERT, UPDATE, DELETE on tables (or on single columns), USAGE on schemas, EXECUTE on functions. A role can be a member of another and inherit its privileges, so rights are granted to group roles, such as reporting, and people join the group.

Row-level security adds a filter per role to a table: once enabled, a role sees only the rows a policy allows, and nothing if no policy applies. The table's owner and superusers bypass it unless the table is set to force ROW LEVEL SECURITY.

Backups

KindHowRestores
Logicalpg_dump: SQL or an archive of the objects and dataa database or chosen tables, also into another version
Physicala copy of the data files, plus the write-ahead log (WAL)the whole server, to any moment: point-in-time recovery
SELECT rolname, rolsuper, rolcanlogin
FROM pg_roles
WHERE rolname NOT LIKE 'pg\_%'
ORDER BY rolname;
The roles in your database: postgres, and any you have created. Try it in the playground
A backup is proven only by restoring it. Test restores regularly, and time them.
Practise it on Day 10

Check your understanding

Answer every question, then check.

1. A transaction moves €500 of reserve from one claim to another, and the second update fails. What happens with rollback?

2. Row-level security is enabled on a table, and a role has no policy on it. What does the role see?

3. Which backup can restore the server to 10:42 yesterday?

An unhandled error has occurred. Reload 🗙

Reconnecting…

Your connection to Data Analytics SQL Lab dropped, probably a Wi-Fi blip. Hold on while it reconnects. Your work is kept.

The connection could not be restored. The page will reload in a few seconds.