|
| 1 | +# Writable tables, transactions and ATTACH options |
| 2 | + |
| 3 | +This page covers the main catalog's per-attach hooks: reading ATTACH options (secret ones |
| 4 | +included) in functions, deciding `supports_transactions` per attach, running code at |
| 5 | +transaction begin / commit / rollback, and declaring catalog tables DuckDB can write to. |
| 6 | +The `txfix` fixture worker (`vgi-example-worker/.../example/txfix/TxFixWorker.java`, which |
| 7 | +serves the vgi suite's `writable_tx/*.test`) uses every piece below and nothing else. |
| 8 | + |
| 9 | +## ATTACH options in functions |
| 10 | + |
| 11 | +Declare options with `Worker.attachOptions(...)`. Every table and table in-out function gets |
| 12 | +their values in its parameters, at bind and at init: |
| 13 | + |
| 14 | +```java |
| 15 | +String token = params.attachOptions().getString("token"); // a secret option |
| 16 | +boolean tx = params.attachOptions().getBoolean("transactional", false); |
| 17 | +``` |
| 18 | + |
| 19 | +`TableBindParams`, `TableInitParams`, `TableInOutBindParams` and `TableInOutInitParams` all |
| 20 | +carry `attachOptions()` (`farm.query.vgi.AttachOptions`). The values travel inside the |
| 21 | +attach's `attach_opaque_data`, so a process that never served the `ATTACH` (a pooled launcher, |
| 22 | +the subprocess transport, another HTTP replica) sees them too. Keep no per-attach state in |
| 23 | +process memory. |
| 24 | + |
| 25 | +**Secret options** (`AttachOptionSpec.asSecret()`) never appear in plaintext in that value |
| 26 | +(`vgi-opaque-data-sealing.md` rule 5). The framework encrypts them (ChaCha20-Poly1305, bound to |
| 27 | +the attach) with: |
| 28 | + |
| 29 | +1. the configured key (`Worker.signingKey`, `Worker.opaqueDataKey` or `VGI_SIGNING_KEY`), else |
| 30 | +2. over HTTP, the per-process sealing key (multi-replica deployments configure a key anyway), else |
| 31 | +3. on stdio / unix sockets, a per-user key file shared by the host's worker processes: |
| 32 | + `$XDG_RUNTIME_DIR/vgi/attach-secret-options.key`, else |
| 33 | + `<java.io.tmpdir>/vgi-<user>/attach-secret-options.key` (directory `0700`, file `0600`, |
| 34 | + both owned by the user; `VGI_ATTACH_SECRET_KEY_FILE` names another file). It is created on |
| 35 | + the first attach that gives a secret option a value. |
| 36 | + |
| 37 | +A process holding a different key sees secret options as absent and |
| 38 | +`AttachOptions.secretsAvailable()` is false. `AttachOptions.fromAttachId(attachId)` decodes the |
| 39 | +non-secret options from a plain attach id without the framework (secrets are never visible |
| 40 | +that way). `AttachOptions.toString()` redacts secret values. |
| 41 | + |
| 42 | +## Per-attach settings: `Worker.onAttach` |
| 43 | + |
| 44 | +```java |
| 45 | +worker.onAttach((attach, request) -> |
| 46 | + AttachSettings.transactional(attach.options().getBoolean("transactional", false))); |
| 47 | +``` |
| 48 | + |
| 49 | +The hook runs on every `ATTACH` of the main catalog, after required options are checked. It |
| 50 | +sees the catalog name, the plain attach id and every option value. It returns |
| 51 | +`AttachSettings(supportsTransactions, supportsTimeTravel)`; a `null` component keeps the |
| 52 | +worker-wide default, `Worker.supportsTransactions(boolean)` / `Worker.supportsTimeTravel(boolean)` |
| 53 | +(both `true` by default, as in earlier releases). Throwing fails the `ATTACH`. |
| 54 | + |
| 55 | +## Transaction hooks: `Worker.transactionHooks` |
| 56 | + |
| 57 | +On an attach whose result advertised `supports_transactions`, DuckDB calls |
| 58 | +`catalog_transaction_begin` / `_commit` / `_rollback` for every transaction, including the |
| 59 | +implicit one around each autocommit statement. `TransactionHooks` runs worker code there: |
| 60 | + |
| 61 | +```java |
| 62 | +worker.transactionHooks(new TransactionHooks() { |
| 63 | + @Override public byte[] begin(AttachContext attach) { ... return id; } // default: 16 random bytes |
| 64 | + @Override public void commit(AttachContext attach, byte[] id) { ... } // throw to fail COMMIT |
| 65 | + @Override public void rollback(AttachContext attach, byte[] id) { ... } |
| 66 | +}); |
| 67 | +``` |
| 68 | + |
| 69 | +- `begin` returns the transaction's **plaintext** id. Over HTTP the framework seals it on the |
| 70 | + way out and opens it on every later call, so `commit`, `rollback` and every function of the |
| 71 | + transaction receive exactly these bytes: `transactionId()` on the four parameter types |
| 72 | + above. A function can therefore stage work under the id and the hooks apply or discard it. |
| 73 | +- An exception from `commit` fails the client's `COMMIT` (or autocommit statement) with its |
| 74 | + message. Discard the staged work before throwing; do not count on a rollback call after a |
| 75 | + failed commit. |
| 76 | +- The hooks get an `AttachContext` with the attach's options, secret ones included. |
| 77 | +- They may run on any worker process: keep staged work somewhere all of them reach. |
| 78 | + |
| 79 | +A producer or exchange that needs the id while producing rows keeps it in a field of its |
| 80 | +state (state fields survive HTTP turns). `TableBindParams.transactionStorage()` remains a |
| 81 | +key/value store scoped to the transaction (it exists for autocommit statements too). |
| 82 | + |
| 83 | +Code-implemented catalogs (`Worker.registerCatalog`) have the same hooks as |
| 84 | +`CatalogInterface.supportsTransactions(attachId)` / `transactionBegin` / `transactionCommit` / |
| 85 | +`transactionRollback`. Auxiliary catalogs (`Worker.ExtraCatalog`) do not support transactions. |
| 86 | + |
| 87 | +## Writable catalog tables |
| 88 | + |
| 89 | +Name the write functions on the table descriptor: |
| 90 | + |
| 91 | +```java |
| 92 | +CatalogTable.builder("main", "items", Schemas.serialize(schemaWithRowId)) |
| 93 | + .scanFunction("items_scan") // emits rowid, declares projection pushdown |
| 94 | + .updateFunction("items_update") // a TableInOutFunction in this catalog |
| 95 | + .writeResultMode(WriteOperation.UPDATE, WriteResultMode.ROWS) |
| 96 | + .constraints(records, revision) |
| 97 | + .build(); |
| 98 | +``` |
| 99 | + |
| 100 | +The framework then advertises `write_result_modes` and the inline write functions in |
| 101 | +`TableInfo` and answers `catalog_table_{insert,update,delete}_function_get`; a table without |
| 102 | +the function for an operation makes DuckDB refuse it ("does not support UPDATE"). UPDATE and |
| 103 | +DELETE need a scan function emitting a row-id column (`WriteResults.rowIdField()`) through |
| 104 | +projection pushdown. |
| 105 | + |
| 106 | +A write function is an ordinary `TableInOutFunction`. DuckDB passes `write_options`: |
| 107 | + |
| 108 | +```java |
| 109 | +WriteResultMode mode = WriteOptions.from(params.arguments()).resultMode(); |
| 110 | +// bind: |
| 111 | +BindResponse.forSchema(Schemas.serialize(WriteResults.resultSchema(mode, userColumns))); |
| 112 | +// per input batch, after applying it: |
| 113 | +out.emit(WriteResults.resultBatch(mode, userColumns, oldRows, newRows, Allocators.root())); |
| 114 | +``` |
| 115 | + |
| 116 | +`COUNT` is a plain statement, `ROWS` is `RETURNING`; `CHANGES` (old / new images) is a maximum |
| 117 | +that satisfies both. UPDATE receives the `SET` columns (every column with `RETURNING`) plus the |
| 118 | +row id. |
| 119 | + |
| 120 | +**Constraint enforcement.** A catalog with a table that carries constraint records *and* a write |
| 121 | +function advertises and selects `vgi.constraint.enforce.v1` automatically (the write functions |
| 122 | +must keep the table's ENFORCED constraints). A provider's own `Worker.catalogCapabilities` |
| 123 | +decision is merged with it; a profile it advertises whose prerequisite only the framework |
| 124 | +supplies (enforce without records read) is still selected. |
0 commit comments