Repository navigation
[connectors] Write a Postgres update of a missing row as an insert - #8099
Conversation
|
What I ran
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):
|
|
|
||
| ## Unreleased | ||
|
|
||
| - The PostgreSQL output connector in materialized mode now writes an update |
There was a problem hiding this comment.
is this a breaking change?
if not, perhaps it does not need to be documented
There was a problem hiding this comment.
Not just that, we need to check with our customers ahead of release.
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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>
6fc1633 to
fdc4883
Compare
…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.
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
WITHclause runs theUPDATEand returns the keys that it found, and theINSERT ... ON CONFLICT (key) DO UPDATEof the insert statement writes only the other rows.INSERT ... ON CONFLICT (key) DO UPDATE SETeach columnUPDATEINSERT: writes the row, firesINSERTtriggers, fails on aNOT NULLcolumn with no default that the view does not writeon_conflict_do_nothingUPDATEON CONFLICT (key)needs oneDELETE ... USINGA first version wrote each update as
INSERT ... ON CONFLICT DO UPDATE. That checksNOT NULLon 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:
max_retrieschange ([connectors] Add max_retries to the Postgres output connector #8098) makes a drop rare.on_conflict_do_nothing, a dropped delete leaves a stale row, and an update of a missing row still does nothing.Describe Manual Test Plan
UPDATEwithon_conflict_do_nothingUPDATENOT NULLcolumn and abigserialcolumn that the view does not writeINSERTof each row (NOT NULLviolation)on_conflict_do_nothingon a table with no unique keyChecklist
Breaking Changes?
Behavior change, in the changelog: an update of a missing row now writes it, as an insert.