05, 10 chapters
changing data
everything.writes
changing data
INSERT, UPDATE, DELETE, upserts, MERGE, bulk loads, and code that runs inside the database.
- 05.01INSERTone row, many rows, INSERT ... SELECT, DEFAULT, and your first CREATE TABLE.
- 05.02UPDATE and DELETEthe WHERE you forgot, UPDATE ... FROM, DELETE ... USING, and TRUNCATE.
- 05.03RETURNING, with OLD and NEWpostgres 18+get back the rows you changed, and since postgres 18, what they looked like before.
- 05.04INSERT ... ON CONFLICTinsert or update in one statement. EXCLUDED, the arbiter index, and two sessions racing for the same key.
- 05.05MERGEthe standard's way to sync one table into another. WHEN MATCHED, NOT MATCHED BY SOURCE, and RETURNING.
- 05.06COPY and bulk loadingwhy one COPY beats 10,000 INSERTs, and what to drop before a big load.
- 05.07parameters, not string gluetype ' OR 1=1 -- into a login form and watch it become code. bound parameters and prepared statements.
- 05.08writing functions in SQLCREATE FUNCTION in plain SQL, IMMUTABLE vs STABLE vs VOLATILE, and when postgres inlines your function.
- 05.09PL/pgSQL and proceduresvariables, loops and exceptions inside the database, CALL with COMMIT, and when to keep logic in the app.
- 05.10triggerscode that runs on every INSERT, UPDATE or DELETE. BEFORE vs AFTER, row vs statement, and an audit table.