jed

Tables & constraints

CREATE TABLE declares typed columns and constraints: PRIMARY KEY, NOT NULL, DEFAULT, CHECK, UNIQUE, and FOREIGN KEY. Constraints are enforced on every write, with a structured error code when violated.

Two tables below: account (with a CHECK (balance >= 0) and a NOT NULL owner) and txn, whose account_id is a FOREIGN KEY that REFERENCES account. Run the query, then try editing it to break a constraint:

Ctrl/⌘ + Enter to run · in-memory

No results yet. Run a query to see output.

Things to try in the panel above:

  • CHECK — INSERT INTO account VALUES (3, 'Bob', -5); → error 23514
  • PRIMARY KEY uniqueness — INSERT INTO account VALUES (1, 'Dup', 1); → error 23505
  • NOT NULL — INSERT INTO account VALUES (4, NULL, 1); → error 23502
  • FOREIGN KEY — INSERT INTO txn VALUES (3, 99, 5); → error 23503 (no account 99)
  • FOREIGN KEY action — DELETE FROM account WHERE id = 1; SELECT * FROM txn; → the referencing txn row is deleted by ON DELETE CASCADE

Each constraint violation is rejected before anything is written — a statement is all-or-nothing. See the error reference for every code.

Creating a table whose name is already taken by a table, index, or sequence is error 42P07. CREATE TABLE IF NOT EXISTS makes it a no-op instead, so setup scripts can be re-run safely. It does not compare definitions: the existing table keeps its columns and rows, even if the new definition differs.

CREATE TABLE IF NOT EXISTS account (id i32 PRIMARY KEY, owner text);   -- no-op: account exists

Foreign keys accept ON DELETE and ON UPDATE with NO ACTION (the default), RESTRICT, CASCADE, SET NULL, or SET DEFAULT. Actions recurse through dependent tables. SET DEFAULT uses each referencing column’s declared default, or NULL when it has none; generated changes run the same type, NOT NULL, CHECK, uniqueness, and foreign-key checks as an ordinary statement.

A foreign-key column need not have exactly the referenced column’s type. As in PostgreSQL, any two integer types pair in either direction, an integer column may reference a decimal, and f32 and f64 pair either way. A referencing value matches a parent key it equals, so an i64 value too large for a referenced i16 matches nothing (23503), and ON UPDATE CASCADE converts the new key as an assignment would (an out-of-range value is error 22003). Other pairs are error 42804; unlike PostgreSQL, that includes an integer referencing a float and the date/timestamp/timestamptz pairs, which jed never compares with each other.

CREATE TABLE region (id i16 PRIMARY KEY);
CREATE TABLE store (id i32 PRIMARY KEY, region_id i64 REFERENCES region);

Deleting or re-keying a parent row checks every referencing table for rows that still point at it, and an action such as ON DELETE CASCADE finds the rows it changes the same way. As in PostgreSQL, jed does not index a foreign key’s referencing columns for you. Without such an index, each check scans the whole child table, and each action scans it once per statement. Create an index that starts with the foreign-key columns (or make them the leading primary-key columns), and each check or action becomes one index lookup per parent row. This work counts toward a session’s max_cost.

The referenced columns must be the parent’s primary key or the columns of a unique index (else error 42830), and the parent keeps that key for as long as the foreign key needs it: dropping the last unique index covering the referenced columns is error 2BP01, like dropping the parent table — drop the constraint first. The parent may be written main.account or temp.account to pin the scope; a table in an attached database cannot be referenced (0A000), and an unknown qualifier is error 42P01.

A multi-column foreign key with a NULL in any referencing column is not checked: that row references nothing (MATCH SIMPLE, the default). Write MATCH FULL after the referenced columns to allow only all-NULL or all-non-NULL keys; a row that mixes them is error 23503. MATCH PARTIAL is not supported (0A000), as in PostgreSQL.

CREATE TABLE shipment (
  id i32 PRIMARY KEY, region text, code i32,
  FOREIGN KEY (region, code) REFERENCES depot (region, code) MATCH FULL ON DELETE SET NULL
);
INSERT INTO shipment VALUES (1, NULL, NULL);   -- ok: references nothing
INSERT INTO shipment VALUES (2, 'west', NULL); -- error 23503 under MATCH FULL

Creating a table from a query

CREATE TABLE … AS query creates a table from a query’s result and fills it with the query’s rows in the same statement, as in PostgreSQL. Each column takes the query column’s name and type. A column copied straight from a table keeps its numeric(p,s) or varchar(n) limit; a computed column such as balance * 2 does not. Constraints, defaults, and keys are not copied. Try it in the panel above:

CREATE TABLE big_spender AS SELECT owner, balance FROM account WHERE balance > 60;
INSERT INTO big_spender VALUES ('Eve', 1.234);    -- balance keeps numeric(12,2): stored 1.23
SELECT owner, balance FROM big_spender ORDER BY owner;

A column list after the table name renames the columns, left to right. WITH NO DATA at the end creates the table empty without running the query. The query can be any query: joins, aggregates, UNION, VALUES, or a WITH clause.

CREATE TABLE totals (owner, spent) AS
  SELECT a.owner, sum(t.amount) FROM account a JOIN txn t ON t.account_id = a.id GROUP BY a.owner;
CREATE TABLE account_shape AS SELECT * FROM account WITH NO DATA;   -- same columns, no rows

The query is checked before the table name, so CREATE TABLE IF NOT EXISTS … AS still reports an error in the query even when the table exists. A query that reads tables needs the SELECT privilege on them, unless WITH NO DATA is used. See temporary tables for CREATE TEMP TABLE … AS.

The query’s WITH may change data, as in PostgreSQL. Its INSERT, UPDATE, or DELETE runs once, and its RETURNING rows can fill the new table. The rest of the query does not see those changes, only the returned rows:

CREATE TABLE closed AS
  WITH d AS (DELETE FROM txn WHERE amount < 20 RETURNING *) SELECT * FROM d;

SELECT … INTO is PostgreSQL’s older way to write the same statement. The INTO clause goes after the select list, and TEMP makes a temporary table:

SELECT owner, balance INTO rich FROM account WHERE balance > 60;
SELECT id INTO TEMP recent FROM txn;

Only the first SELECT of a statement can have INTO. In a subquery, a WITH query, or a later UNION branch it is error 42601, and in a view it is error 0A000. jed has no unlogged tables, so INTO UNLOGGED and CREATE UNLOGGED TABLE are error 0A000. EXPLAIN CREATE TABLE … AS shows the plan of the query; see EXPLAIN.

Names and quoted identifiers

Names follow PostgreSQL’s rules. A bare name is folded to lowercase, so Orders, ORDERS, and orders are the same table. A name in double quotes keeps its exact spelling, so it can hold uppercase letters, spaces, or a keyword such as order. Write a literal " inside one by doubling it. After folding, names match exactly: "userId" must be written with quotes everywhere you use it, and "Orders" is a different table from orders.

CREATE TABLE Orders (Id i32 PRIMARY KEY, "userId" i32, "order" text);
SELECT id, "userId", "order" FROM orders;   -- the table is orders, the column id
SELECT userId FROM orders;                  -- error 42703: userid does not exist
SELECT 1 AS Total, 2 AS "Total";            -- columns named total and Total

Built-in function and type names are lowercase, so LOWER(x) works but "LOWER"(x) does not. The name passed to nextval('…') is read the same way: nextval('MySeq') means myseq, and nextval('"MySeq"') means MySeq. A name may be at most 63 bytes (error 42622). Names jed makes up for unnamed constraints, indexes, and serial sequences (orders_pkey, orders_email_key, orders_id_seq) are shortened to fit the same 63 bytes, exactly as PostgreSQL shortens them, so you can always refer to them. If another constraint in the same database already has the name jed would make up, a number is added (orders_email_key1), as in PostgreSQL. Temporary tables and each attached database count as separate databases for this. Names that start with jed_ are reserved for jed’s own catalog relations (error 42939).

A quoted name or a string literal can also spell characters by code point, as in PostgreSQL. Put U& before the opening quote, then write \XXXX (4 hex digits) or \+XXXXXX (6 hex digits) for a character and \\ for a backslash. Add UESCAPE '!' after the literal to use another escape character. A malformed escape is error 42601.

Ctrl/⌘ + Enter to run · in-memory

No results yet. Run a query to see output.

Applying defaults

Omitting a column or using DEFAULT in a VALUES slot applies its declared default. To insert one row using defaults for every column, use DEFAULT VALUES; to reset an existing value, assign DEFAULT in an UPDATE — or in the DO UPDATE action of an upsert (see below):

CREATE TABLE job (
  id i32 PRIMARY KEY DEFAULT 1,
  state text NOT NULL DEFAULT 'queued',
  attempts i32 DEFAULT 0
);
INSERT INTO job DEFAULT VALUES;
UPDATE job SET state = DEFAULT, attempts = DEFAULT WHERE id = 1;
INSERT INTO job (id, state) VALUES (1, 'retry')
  ON CONFLICT (id) DO UPDATE SET state = excluded.state, attempts = DEFAULT;

A column with no declared default takes NULL, so the ordinary NOT NULL check still applies. Expression defaults are evaluated separately for each inserted or updated row; in an upsert, a DEFAULT assignment is evaluated only for rows the DO UPDATE actually updates, so a sequence-backed default advances only for those.

Expressions in VALUES

A VALUES slot can hold any expression: arithmetic, function calls, CASE, casts, and scalar subqueries.

INSERT INTO account VALUES
  ((SELECT max(id) FROM account) + 1, upper('linus'), 20 * 2.50);

An expression has no columns in scope, and its result must be assignable to the target column (error 42804 otherwise). A subquery runs once, against the table as it was before the statement. Every expression is evaluated before any row is written, so an error such as division by zero stores nothing.

Altering a table

ALTER TABLE can add or drop columns, rename a table, column, or constraint; change a column’s type, default, or NOT NULL status; add or drop CHECK, UNIQUE, FOREIGN KEY, and EXCLUDE constraints; and add or drop a primary key. Try these statements:

ALTER TABLE account RENAME COLUMN balance TO available_balance;
ALTER TABLE account ALTER COLUMN available_balance SET DEFAULT 0;
ALTER TABLE account ALTER COLUMN available_balance SET NOT NULL;
ALTER TABLE account ADD COLUMN opened_by text DEFAULT 'import';
ALTER TABLE account ALTER COLUMN balance TYPE i64 USING balance + 0;
ALTER TABLE account DROP COLUMN opened_by;
ALTER TABLE account RENAME CONSTRAINT balance_nonnegative TO nonnegative_balance;
ALTER TABLE account ADD CONSTRAINT owner_unique UNIQUE (owner);
ALTER TABLE account DROP CONSTRAINT owner_unique;
ALTER TABLE account DROP PRIMARY KEY;
ALTER TABLE account ADD PRIMARY KEY (id);
ALTER TABLE account RENAME TO ledger_account;

Column actions can be combined with commas. They run from left to right and publish only if every action succeeds:

ALTER TABLE account
  ALTER COLUMN balance SET DEFAULT 0,
  ALTER COLUMN balance SET NOT NULL;

SET DEFAULT affects future inserts, while SET NOT NULL first checks every existing row and reports 23502 if any value is null. Renaming a column also updates its stored CHECK and default expressions, plus expression and partial-index definitions. IF EXISTS is supported for a missing table.

Adding a constraint validates existing rows before publishing it and builds the backing index for UNIQUE or EXCLUDE. Dropping a referenced UNIQUE constraint defaults to RESTRICT; CASCADE also removes dependent foreign keys. ADD COLUMN rewrites existing rows with the column’s default (or null), evaluates expression defaults once per row, and accepts the same inline constraints as CREATE TABLE; IF NOT EXISTS is available. A non-empty table cannot add a NOT NULL column with no usable default (23502). DROP COLUMN also rewrites the table, physically removes the row slot, and compacts surviving ordinals. As in PostgreSQL, same-table CHECK, index (including UNIQUE), and EXCLUDE dependents are removed automatically, as is a foreign key that owns the column on its local side. The default RESTRICT mode still reports 2BP01 for external dependents such as a foreign key that references the column from another table; CASCADE removes those too. ALTER COLUMN TYPE converts every row through jed’s explicit cast matrix, or through a per-row USING expression, then revalidates constraints and rebuilds indexes. ADD PRIMARY KEY validates NOT NULL and uniqueness before re-keying; DROP PRIMARY KEY re-keys to synthetic rowids and retains member NOT NULL status. An identity column’s default and NOT NULL belong to the identity, so SET DEFAULT, DROP DEFAULT, and DROP NOT NULL on it are errors; change it with the identity actions below. PRIMARY KEY has no persisted constraint object, so renaming the derived account_pkey handle reports 42704; an explicit name on an added primary key is accepted but the handle remains derived.

Exclusion constraints — EXCLUDE

An EXCLUDE constraint generalizes UNIQUE: instead of forbidding two rows with the equal key, it forbids two rows that make a list of comparisons all true at once. The classic use is no-double-booking — no two reservations may share a room and have overlapping time ranges. Paste this into the panel above:

CREATE TABLE booking (
  id i32 PRIMARY KEY,
  room i32,
  during i32range,
  EXCLUDE USING gist (room WITH =, during WITH &&)
);
INSERT INTO booking VALUES (1, 101, '[10,20)');
INSERT INTO booking VALUES (2, 101, '[15,25)');  -- error 23P01: same room, overlapping time

The second insert is rejected with 23P01 — room 101 is already booked for an overlapping range. A different room, or a non-overlapping range, is fine. The supported operators are = (over a fixed-width scalar) and && (range overlap); UNIQUE is the special all-= case. A row with NULL in an excluded column — or an empty range under && — never conflicts (it is exempt). The constraint is backed by a GiST index (USING gist) and enforced on every write, all-or-nothing like the others. (PostgreSQL needs CREATE EXTENSION btree_gist for the scalar = member; jed ships it in-core.)

Rescheduling is just an UPDATE — assigning a range (or array) column re-checks every constraint over the statement’s end state, so moving a booking to a free slot succeeds and one that would overlap a different same-room booking is rejected:

INSERT INTO booking VALUES (2, 101, '[30,40)');      -- a second slot in room 101
UPDATE booking SET during = '[20,28)' WHERE id = 1;  -- ok: still no overlap
UPDATE booking SET during = '[35,45)' WHERE id = 1;  -- error 23P01: now overlaps booking 2

The range literal on the right adapts to the column’s type; an i32range(20,28) constructor, a cast, or a during + '[5,8)'::i32range expression work too.

Only some rows — WHERE (predicate). A partial exclusion constraint applies to the rows its predicate admits; a row whose predicate is false or NULL neither conflicts nor is conflicted with. Here only active bookings may overlap nothing:

CREATE TABLE slot (
  id i32 PRIMARY KEY,
  during i32range,
  active boolean,
  EXCLUDE USING gist (during WITH &&) WHERE (active)
);
INSERT INTO slot VALUES (1, '[1,5)', true), (2, '[1,5)', false);  -- ok: row 2 is inactive
INSERT INTO slot VALUES (3, '[3,8)', true);                        -- error 23P01: overlaps row 1

The predicate follows the rules of a partial index’s (it must be immutable and read only the table’s own columns; the parentheses are required).

EXCLUDE USING btree — the all-= form. With only = operators the constraint can use an ordinary ordered index, and btree is the default access method, as in PostgreSQL: EXCLUDE (code WITH =) means EXCLUDE USING btree (code WITH =). It forbids the same rows a UNIQUE (code) would — and works over any type an index can key, text included — but it stays an exclusion constraint: a conflict is 23P01 rather than 23505, and it is not a foreign-key target or an ON CONFLICT (code) arbiter. Combined with WHERE, it expresses “at most one primary contact per customer”:

CREATE TABLE contact (
  id i32 PRIMARY KEY,
  customer i32,
  is_primary boolean,
  EXCLUDE (customer WITH =) WHERE (is_primary)
);

INSERT … ON CONFLICT DO NOTHING skips a row any exclusion constraint rejects, and ON CONFLICT ON CONSTRAINT contact_customer_excl DO NOTHING names one; DO UPDATE cannot arbitrate an exclusion constraint (42809).

Auto-numbering with serial

A serial column (or bigserial / smallserial for i64 / i16) is shorthand for an auto-numbering integer: it creates a dedicated sequence and defaults the column to that sequence’s next value, and the column is NOT NULL. Omit it on insert and it fills in 1, 2, … Paste this into the panel above:

CREATE TABLE log (id serial PRIMARY KEY, msg text);
INSERT INTO log (msg) VALUES ('first'), ('second');
SELECT * FROM log;

id is assigned 1 then 2 automatically. The owned sequence is named log_id_seq; DROP TABLE log drops it too. (Supplying an explicit id overrides the default for that row without advancing the sequence — just like PostgreSQL.)

The backing sequence matches the column’s type: smallserial / bigserial (and a smallint / bigint identity column) get a sequence bounded to that integer type’s range, so it tops out exactly where the column does — a smallserial sequence stops at 32767. A standalone CREATE SEQUENCE can choose the type the same way with AS smallint | integer | bigint (default bigint).

Identity columns — GENERATED … AS IDENTITY

The SQL-standard spelling of an auto-numbered column. Like serial it creates an owned sequence and fills the column in, but it also records whether values may be supplied by hand:

CREATE TABLE event (id int GENERATED ALWAYS AS IDENTITY PRIMARY KEY, label text);
INSERT INTO event (label) VALUES ('login'), ('logout');
SELECT * FROM event;
  • GENERATED ALWAYS — the value always comes from the sequence. Supplying an explicit value is an error (428C9) unless you ask for it with OVERRIDING SYSTEM VALUE:

    INSERT INTO event (id, label) OVERRIDING SYSTEM VALUE VALUES (100, 'imported');
  • GENERATED BY DEFAULT — like serial: an explicit value is used when given (and does not advance the sequence), otherwise the sequence fills in. OVERRIDING USER VALUE forces the sequence even when a value is supplied.

The column must be smallint, integer, or bigint, and is implicitly NOT NULL. You can tune the backing sequence inline — GENERATED ALWAYS AS IDENTITY (START WITH 100 INCREMENT BY 5) — and, as with serial, the owned sequence is named event_id_seq and is dropped with the table. SEQUENCE NAME among those options picks a different name.

Changing an identity column

ALTER TABLE … ALTER COLUMN adds, changes, and removes identity on an existing column, as in PostgreSQL:

CREATE TABLE ticket (id int NOT NULL, title text);
INSERT INTO ticket VALUES (1, 'imported'), (2, 'imported too');

-- Make id an identity column, starting past the imported rows.
ALTER TABLE ticket ALTER COLUMN id ADD GENERATED BY DEFAULT AS IDENTITY (START WITH 3);

-- Switch to ALWAYS and change the sequence; options combine in one action.
ALTER TABLE ticket ALTER COLUMN id SET GENERATED ALWAYS SET INCREMENT BY 10 RESTART WITH 100;

-- Back to a plain NOT NULL column; the sequence is dropped.
ALTER TABLE ticket ALTER COLUMN id DROP IDENTITY;
  • ADD GENERATED { ALWAYS | BY DEFAULT } AS IDENTITY [( options )] needs a smallint, integer, or bigint column that is NOT NULL and has no default; otherwise it is an error (22023 for the type, 55000 for the rest). Existing rows keep their values, and the new sequence starts at its START, so set one past the existing values.
  • SET GENERATED { ALWAYS | BY DEFAULT }, SET <sequence option> (INCREMENT, MINVALUE, MAXVALUE, START, CACHE, [NO] CYCLE, …) and RESTART [[WITH] n] change an identity column; any of them can be combined, each once. The sequence keeps the column’s type, so SET MAXVALUE cannot exceed it and SET NO MAXVALUE goes back to the type’s maximum.
  • DROP IDENTITY [IF EXISTS] drops the sequence and the generated default; the column stays NOT NULL.

SET and DROP IDENTITY on a column that is not an identity column report 55000 (IF EXISTS makes the drop a no-op). Each action can be combined with the other ALTER TABLE actions, runs left to right, and rolls back with the statement or transaction.

Upsert with ON CONFLICT

Instead of trapping 23505 when an insert collides with a PRIMARY KEY or UNIQUE constraint, an ON CONFLICT clause takes a controlled action. DO NOTHING skips the offending row; DO UPDATE updates the existing conflicting row — the row you tried to insert is available as the special excluded relation, while a bare or table-qualified column reads the existing row. Run this in the panel above (after the account table exists):

INSERT INTO account VALUES (1, 'Ada', 100.00)
  ON CONFLICT (id) DO UPDATE SET balance = account.balance + excluded.balance;
SELECT id, owner, balance FROM account WHERE id = 1;

Account 1 already exists, so instead of erroring the row is updated — its balance becomes 100.00 + 100.00. The parenthesised conflict target (id) names which unique constraint to arbitrate on (matched by column set; you can also write ON CONSTRAINT account_pkey). More to try:

  • DO NOTHING — INSERT INTO account VALUES (1, 'Dup', 1) ON CONFLICT DO NOTHING; succeeds and changes nothing (with no target it skips a conflict on any unique constraint).
  • A filtered update — add WHERE excluded.balance > account.balance to a DO UPDATE to apply it only when the proposed balance is larger; otherwise the row is left untouched.
  • RETURNING — append RETURNING id, balance to see the affected (inserted or updated) rows.
  • Resetting to a default — DO UPDATE SET col = DEFAULT assigns the column’s declared default (or NULL), exactly like UPDATE … SET col = DEFAULT (see Applying defaults above).
  • Array, range, and composite columns — DO UPDATE assigns them as UPDATE does: NULL, a text literal ('{1,2}', '[1,5)', '(Main,1)'), a value of the column’s own type such as excluded.col, or — for a composite column — a ROW(…) of expressions such as ROW((excluded.home).street, 0).
  • A partial unique index — a column target never picks a partial unique index unless you add its predicate after the target, as in PostgreSQL. With CREATE UNIQUE INDEX account_open ON account (owner) WHERE balance > 0, write INSERT … ON CONFLICT (owner) WHERE balance > 0 DO NOTHING. The WHERE only has to imply the index predicate (WHERE balance > 10 works too). A row whose own predicate is false is not in that index, so it never conflicts there and is inserted.
  • A target alias — INSERT INTO account AS a VALUES (1, 'Ada', 5.00) ON CONFLICT (id) DO UPDATE SET balance = a.balance + excluded.balance RETURNING a.balance; names the existing row a. Once aliased, the table is referred to only by its alias (account.balance would be 42P01), in the DO UPDATE and in RETURNING alike.
  • Changing the key — INSERT INTO account VALUES (1, 'Ada', 0) ON CONFLICT (id) DO UPDATE SET id = 100; moves the existing row to key 100, as UPDATE … SET id = … would: its indexes and any foreign keys that reference it follow, and a key another row already holds raises 23505.
  • Collation and operator-class decorations — a target element may name the collation or B-tree operator class its unique index must use, as in PostgreSQL: ON CONFLICT (email COLLATE "C") or ON CONFLICT (id int4_ops). jed keys each column under its own collation and its type’s default class (text_ops, int4_ops, numeric_ops, …), so a decoration that names anything else matches no index (42P10); an unknown collation or class is 42704. Decorations combine with a partial index’s WHERE: ON CONFLICT (owner COLLATE "C") WHERE balance > 0.

RETURNING can also compare row versions. old.balance is the value before the change and new.balance the value after it; an INSERT has an all-NULL old row and a DELETE an all-NULL new row. Use aliases when the names would be clearer—or when the target table itself is named old or new:

UPDATE account SET balance = balance + 25
  WHERE id = 1
  RETURNING WITH (OLD AS before, NEW AS after) before.balance, after.balance;

A conflict on a constraint other than the arbiter still raises 23505, and a single statement whose proposed row conflicts with a row that same statement already inserted or updated raises 21000. The whole statement is all-or-nothing.

Synchronizing a table with MERGE

MERGE joins a source — a table, a view, a subquery, a VALUES list, a CTE, or a join of these — to the target table (or an updatable view) and, for each joined row, runs the first WHEN clause that applies: update or delete a matched row, insert a row for an unmatched source row, or update or delete a target row that no source row matched. Run this in the panel above:

MERGE INTO account a
USING (VALUES (1, 30.00), (3, 75.00)) AS p(id, amount) ON a.id = p.id
WHEN MATCHED THEN UPDATE SET balance = a.balance + p.amount
WHEN NOT MATCHED THEN INSERT VALUES (p.id, 'new', p.amount)
RETURNING merge_action(), a.id, a.balance;

Account 1 is matched and gains 30.00; account 3 is new and inserted. merge_action() names what happened to each row, and RETURNING reads the source’s columns too (p.amount), as well as old. and new. row versions. More to try:

  • Conditions — WHEN MATCHED AND p.amount < 0 THEN DELETE before an unconditional WHEN MATCHED clause; the first clause whose condition is true wins, and a clause after an unconditional one of the same kind is an error (42601).
  • DO NOTHING — skip a row explicitly: WHEN NOT MATCHED THEN DO NOTHING.
  • Rows the source does not mention — WHEN NOT MATCHED BY SOURCE THEN DELETE removes every target row no source row matched (or UPDATE SET … flags them).
  • Defaults — INSERT (id, owner) VALUES (…) fills the remaining columns from their defaults, and UPDATE SET col = DEFAULT resets a column.

Each clause sees only what it can: a WHEN NOT MATCHED clause reads the source row only, and a WHEN NOT MATCHED BY SOURCE clause the target row only (42P01 otherwise). A target row may be updated or deleted at most once — two source rows that both act on it raise 21000 — and conditions always read the row as it was before the statement. Like every write, MERGE is all-or-nothing, and its constraints are checked against the statement’s end state, so a MERGE may delete one row and insert another with the same key. A MERGE can be prefixed by WITH, or itself be a WITH query whose RETURNING rows feed the rest of the statement.

More shapes:

  • A joined source — MERGE INTO account a USING txn t JOIN account x ON t.account_id = x.id ON a.id = x.id …: the join’s own labels (t, x) stay usable in the clauses and RETURNING.
  • Into a view — the view’s WHERE decides which rows are targets (a row the view hides is never matched), its WITH CHECK OPTION checks every inserted and updated row, and SET / INSERT name the view’s columns.
  • EXPLAIN MERGE — shows the source plan and how each source row finds its target rows: a key or index lookup per row, or, when no index covers the join columns and the inputs are large, a Hash Join that reads the target once.
jed — an embeddable, strictly-typed SQL database.