skip to content
05, 10 chapters
changing data
everything.writes

changing data

INSERT, UPDATE, DELETE, upserts, MERGE, bulk loads, and code that runs inside the database.

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