Skip to content

[connectors] Write a Postgres update of a missing row as an insert - #8099

Merged
bestouff merged 2 commits into
mainfrom
issue-8014-upsert
Nov 27, 2026
Merged

bestouff merged 2 commits into
mainfrom
issue-8014-upsert

Conversation

@bestouff

@bestouff bestouff commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Linked Issue

Related to #8014, found in the review of #8017. Independent of the #8017 stack (based on main).

What changes

In materialized mode, the Postgres output connector wrote an update as UPDATE ... FROM ... WHERE key = key. When the row is not in the table, that statement does nothing. A batch that the connector dropped can leave a row out, and each later update of its key then did nothing: the row stayed out until the key was deleted and inserted again.

An update is now one statement: a WITH clause runs the UPDATE and returns the keys that it found, and the INSERT ... ON CONFLICT (key) DO UPDATE of the insert statement writes only the other rows.

Operation Before After
Insert INSERT ... ON CONFLICT (key) DO UPDATE SET each column Same
Update of a row that is there UPDATE Same: changes only the columns of the view, checks no other column, takes no sequence value
Update of a row that is not there Nothing INSERT: writes the row, fires INSERT triggers, fails on a NOT NULL column with no default that the view does not write
Update with on_conflict_do_nothing UPDATE Same. With this option the table can have no unique index on the key, and ON CONFLICT (key) needs one
Delete DELETE ... USING Same

A first version wrote each update as INSERT ... ON CONFLICT DO UPDATE. That checks NOT NULL on the columns that the view does not write, and takes a sequence value, also for a row that is there, so it broke tables that another writer fills.

Limits:

Describe Manual Test Plan

Test Result
Unit test: the update statement updates, then inserts the rows that it did not update; it stays an UPDATE with on_conflict_do_nothing Pass
Suite case: one batch updates 5 rows that are there and 5 that are not Pass. Fails with the old UPDATE
Suite case: update of rows that another writer filled, with a NOT NULL column and a bigserial column that the view does not write Pass: the other columns stay, the sequence does not move. Fails with an INSERT of each row (NOT NULL violation)
Suite case: on_conflict_do_nothing on a table with no unique key Pass: the statements prepare and the update changes the rows
Postgres suites (not TLS, not the CDC input cases), local PostgreSQL 15 49 of 49 pass

Checklist

  • Unit tests added/updated
  • Integration tests added/updated
  • Documentation updated
  • Changelog updated

Breaking Changes?

Behavior change, in the changelog: an update of a missing row now writes it, as an insert.

@mythical-fred-oss

Copy link
Copy Markdown
Contributor

What I ran

Check Result
cargo test -p dbsp_connectors --lib prepared_statements (4 tests, including the new one) pass
pre-commit run --files <changed> pass
Postgres suite case update_of_a_missing_row_writes_it not run: no Postgres in this environment

Gates: Tests, docs, changelog and commit message are all OK. The SQL type-coverage gate doesn't apply, because encoding is unchanged and updates now go through the insert statement, which existing tests already cover.

Missing tests (medium):

  • A batch that mixes present and missing keys. The new case only covers keys that are all missing.
  • Updates with on_conflict_do_nothing against a table that has no unique key, run against Postgres. This checks that the UPDATE fallback still prepares. Right now only a string assertion covers it.
  • Updates on a table with extra columns, a NOT NULL column or a serial column (see the inline comment).

@bestouff
bestouff requested review from gz and ryzhyk November 26, 2026 16:05

## Unreleased

- The PostgreSQL output connector in materialized mode now writes an update

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this a breaking change?
if not, perhaps it does not need to be documented

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not just that, we need to check with our customers ahead of release.

@bestouff bestouff Nov 26, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now, for an update of a missing row:

  • If the table has a NOT NULL column that the view does not write and that has no default, the INSERT fails with an error. Before, the update did nothing and gave no error.
  • The connector now writes rows that it did not write before, and INSERT triggers fire on them.

Tables whose columns all come from the view get only the fix.

Which customer potentially use that (materialized-mode Postgres sinks with extra or NOT NULL columns) ?

The connector writes an update as `INSERT ... ON CONFLICT (key) DO UPDATE`,
the same statement as an insert. So an update of a row that is not in the
table writes the row, for example a row that a batch that the connector
dropped did not insert. A `BEFORE INSERT` trigger on the table also fires for

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dropped or did not insert?
dropping stuff looks pretty bad, after that I am not sure what promises we can make about the output collection

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dropped. I'll change the wording for it not to look like it's normal.

In materialized mode, the Postgres output connector wrote an update as
UPDATE ... FROM ... WHERE key = key. That statement does nothing when
the row is not in the table. A batch that the connector dropped can
leave a row out, and each later update of its key then did nothing:
the row stayed out until the key was deleted and inserted again.

Write an update in one statement: a data-modifying WITH clause updates
the rows that are there, and the INSERT ... ON CONFLICT (key) DO UPDATE
of the insert statement writes only the rows that it did not update.
An INSERT of every row would check NOT NULL on the columns that the view
does not write, and take a value of each sequence, also for a row that
is there: an update of a table that another writer fills would fail.

With on_conflict_do_nothing, an update stays an UPDATE: the table can
then have no unique index on the key, and ON CONFLICT (key) needs one.

A unit test checks both statements. Suite cases update rows that are
there and rows that are not, rows of a table with a NOT NULL column and
a serial column that the view does not write, and rows of a table with
no unique key. The first fails with the UPDATE, the second with an
INSERT of every row.

Related: #8014
Signed-off-by: Xavier Bestel <xavier.bestel@feldera.com>
@bestouff bestouff changed the title [connectors] Write a Postgres update as an insert on conflict [connectors] Write a Postgres update of a missing row as an insert Nov 26, 2026
…was dropped

Review asked whether a dropped batch is something the connector does.
It does on a permanent error (#8014), but the docs should not present
it as normal. Say that an earlier batch failed to write, and that an
update repairs only the rows whose keys it changes.
@bestouff
bestouff added this pull request to the merge queue Nov 27, 2026
Merged via the queue into main with commit 90f04f1 Nov 27, 2026
1 of 2 checks passed
@bestouff
bestouff deleted the issue-8014-upsert branch November 27, 2026 14:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants