Insert / Update transform Icon Insert / Update

Description

The Insert/Update transform first looks up a row in a table using one or more lookup keys. If a table row for the specified keys can’t be found, a new row is inserted.

If it can be found and the fields to update are the same, nothing is done.

If they are not all the same, the row in the table is updated.

To find which values the database would reject before writing, put Database value validation in front of this transform.

Options

Option Description

Commit size

The number of rows to change (insert / update) before running a commit.

Connection

The database connection to which data is written

Don’t perform any updates

If enabled, the values in the database are never updated;only inserts are performed.

Use batch updates

If enabled, the inserts and updates are sent to the database in batches of the commit size instead of one statement per row, on a database where Hop knows how its driver handles a failed batch and when every key condition is an equality. See Batches and error handling. Off by default.

Rows per statement

With Use batch updates, the number of rows one INSERT and one UPDATE statement carry, on a database type with a multi-row form of them whose failed batches are recovered. 100 for a new transform, but a new transform has Use batch updates off, so it still writes one row per statement until batching is switched on; a transform saved before this option existed writes one row per statement. Variables are allowed. See Several rows per statement.

Key Lookup table

Allows you to specify a list of field values and comparators. You can use the following comparators: =, = ~NULL, <>, <, ⇐, >, >=, LIKE, BETWEEN, IS NULL, IS NOT NULL

SQL button

Click SQL to generate the SQL to create the table and indexes for correct operation.

Transform name

Name of the transform; this name has to be unique in a single pipeline.

Target schema

The name of the Schema for the table to which data is written. This is important for data sources that allow for table names with periods in them.

Target table

Name of the table in which you want to do the insert or update.

Update Fields

Allows you to specify all fields in the table you want to insert/update including the keys. Avoid updates on certain fields by specifying N in the update column.

Batches and error handling

With Use batch updates off, the default, the rows are inserted or updated one by one, as before. On PostgreSQL and the other databases where a failed statement aborts the transaction, every row then runs behind a savepoint when an error hop is attached, so that only the row that failed goes to the error hop.

With Use batch updates on, every row is still looked up on its own, but the inserts and the updates are sent to the database in two batches of the commit size: an insert batch and an update batch. When a row in a batch fails, what happened to the other rows of the batch depends on the database’s JDBC driver: some run the other rows, some stop at the failing row, and some lose the whole batch. On a database whose driver’s behaviour has been measured, the transform uses it to work out which rows of a failed batch are in the database, and with an error hop attached it sends each row to exactly one stream: the main output for rows that were inserted or updated, the error hop for rows that were refused. That includes PostgreSQL, where the rows are then batched with an error hop instead of running one by one behind a savepoint.

  • A row only appears on the main output after the database has committed its insert or update. The rows keep their input order, also the rows that needed no update: they wait for the rows before them.

  • The rows of a commit window go out together: first the inserts are written and committed, then the updates. So when an update and a later insert of the same window both set a column outside the key that must be unique to the same value, the insert gets it and the update is refused, where row by row the update, which came first, would get it.

  • The lookup can’t see the rows that still wait in a batch. When a row’s key is the key of a row that waits, before or after its insert or update, the waiting batches are written first and the row is looked up after them, so a key that comes twice is inserted once and then updated, as row by row. Keys are compared without case, accents and surrounding spaces, and numbers by value, so that the transform never misses a key the database finds equal; two keys that only look alike cost an extra write of the batches.

  • Batches are only used when every key condition is =, = ~NULL, IS NULL or IS NOT NULL. With any other comparator a waiting row could match the lookup of any later row, so the rows are written one by one, and the log says so.

  • Rows refused in a batch go to the error hop with error code ISU002; a row of a batch that the database never ran and could not be asked about gets ISU003. A row that fails on its own, such as a lookup that fails, keeps ISU001.

  • Without an error hop a failed batch is rolled back and the transform stops. None of the rows of that batch is passed on. When the update batch of a window fails, the inserts of that window were committed before it ran, and are passed on.

  • When the transform is stopped or fails, the rows that still wait in its batches are not written.

The databases measured so far, with their default connection options, are listed in the development manual under "Creating a dialect". On every other database, and when a connection option changes how the driver handles a failed batch, the transform writes the rows one by one, as with Use batch updates off, and the log says why.

To go back to that behaviour on a measured database:

  • set When a batch fails, the driver on the Advanced tab of the connection to As before, or

  • set the variable HOP_DATABASE_BATCH_RECOVERY to LEGACY, for every connection.

When a failed batch is worked out this way, the insert or update of a row can be sent to the database more than once, so a failed batch can fire non-transactional triggers again.

With a commit size of 0, or on a connection shared by a transactional pipeline, the rows are written one by one.

Several rows per statement

With Use batch updates on and Rows per statement above 1, the insert batch and the update batch of a window are written as statements of several rows each: the inserts as INSERT INTO t (a, b) VALUES (?, ?), (?, ?), …​, the updates as one UPDATE that joins the table with the rows of the window (UPDATE t SET b = s.b FROM (VALUES (?, ?), …​) s (a, b) WHERE t.a = s.a on PostgreSQL, a join or a MERGE on other databases). Every row is still looked up on its own, so the choice between an insert, an update and nothing to do is the same as with one row per statement, and so is everything listed above: the order of the rows, the inserts before the updates, a key that waits in a batch, the counters and the error codes.

This is done on the database types whose statements of several rows have been checked. An INSERT of several rows is written on PostgreSQL, MySQL, Microsoft SQL Server, H2, DuckDB and Apache Derby; an UPDATE of several rows on PostgreSQL, MySQL, Microsoft SQL Server, Oracle and DuckDB. The other statement then writes one row per statement: on H2 and Derby the updates, on Oracle the inserts. A remainder shorter than the rows per statement is sent as a few statements of smaller sizes, and a database’s limits on rows or parameters per statement lower the rows per statement where needed. When a statement of several rows fails, its rows are run again one by one, so with an error hop exactly the refused rows go to the error hop (error code ISU002) and the others are committed; without one the transform stops as described above. A value that can’t be bound to the statement fails when the row arrives, as with one row per statement: the row goes to the error hop with error code ISU001, or the transform stops.

The UPDATE writes one row per statement, and the detailed log says why, when:

  • a key condition is not = (= ~NULL, IS NULL and IS NOT NULL keep the batches, but not several rows per UPDATE), or a key column is compared twice;

  • an update field sets a key column (with Update Y), except when it sets it from the key field itself and that field is a number, a date or a boolean. A text key can match a row whose stored value differs in case or trailing spaces, and the one-row UPDATE then changes the stored key; set Update to N for the key field to update several rows per statement;

  • the update fields set no column apart from the key columns.

Statement-level triggers fire once per statement and see all of its rows: every trigger on SQL Server, and FOR EACH STATEMENT triggers on PostgreSQL, H2 and Derby. A trigger that expects one row per INSERT or UPDATE needs Rows per statement 1. Row-level triggers still fire once per row.

One row per statement is also used, as before, on a database without transactions, when two fields write the same column, on MySQL outside strict SQL mode or into a table without transactions, or when the database type declares no statement of several rows or its limit on parameters leaves room for one row only. The detailed log says which, and how many rows per statement are used.

To go back to one row per statement without changing any transform, tick Write one row per statement on the Advanced tab of the connection, or set the variable HOP_DATABASE_MULTI_ROW_DML to DISABLED for every connection. HOP_DATABASE_BATCH_RECOVERY=LEGACY does the same, because several rows per statement are only written where failed batches are recovered. Setting Rows per statement to 1 does it for one transform.

Samples

The samples project has transforms/insertupdate-basic.hpl, which writes 7 rows from a Data grid to the table INSERTUPDATE of the hop-samples connection, with id as the key: 5 rows are inserted, and the 2 rows with an id that is already in the table update col_bool and col_string, but not col_date.