This week in rngo 0.42.0 and rngo agent 0.8.0, I added a batch channel, which makes loading lots of backdated data much faster.
The Problem
Until now, the usual way to load a database was a stream channel that pipes individual SQL inserts into psql:
type: stream
command: psql -q $DATABASE_URL
format:
type: sqlThat works, but each statement is its own round trip and its own commit. So, if you want to backfill a couple years of realistic data to a cloud DB, it's too slow
`batch` Channel
The new batch channel appends inputs to temporary files in the eval directory, then delivers it via the specified command:
type: batch
command: >-
psql -X -q -1
-v ON_ERROR_STOP=1
-f "$RNGO_BATCH_FILE"
"$DATABASE_URL"
size: 5000
format:
type: sqlIn this example, the psql command runs once every 5000 inputs. It references the temporary file via the $RNGO_BATCH_FILE environment variable and loads the whole thing in a single transaction.
Bulk Loading
Batching statements helps a lot, but an INSERT per row is still slow compared to bulk loaders like Postgres COPY. These expect one CSV file per table, so I also added:
- a
csvformat, which writes an input's data as a singloe CSV row - a
split: effectoption onbatch, which writes one batch file per effect
Together, these let you load data with \copy:
type: batch
split: effect
command: >-
psql -X
-v ON_ERROR_STOP=1
"$DATABASE_URL"
-c "\copy $RNGO_BATCH_TABLE_QUOTED ($RNGO_BATCH_COLUMNS_QUOTED)
FROM pstdin WITH (FORMAT csv)"
size: 5000
format:
type: csvSo, inputs are batched into effect-specific temporary batch files. Once 5000 inputs are batched, command is called for each effect, with the table name and columns made available via $RNGO_BATCH_TABLE_QUOTED and $RNGO_BATCH_COLUMNS_QUOTED respectively, and this time batch file content comes via stdin.
The commands are run in an order based on each effect's reference generators and triggers so foreign keys hold.
Batch Logs
Each batch delivery is recorded as a batch record in the log's metadata table, with its size, input range, timing and exit status. So you can write a signal to make sure everything loaded:
batches-loaded:
type: sql
query: SELECT count(*) FROM metadata WHERE type = 'batch' AND (data ->> 'exit_status') IS NOT 0
expect: result == 0Honorable Mentions
- when a channel fails mid-eval,
rngo evalstill finishes the other channels and commits the log before reporting the error - on Windows,
rngo evalno longer needs administrator rights or Developer Mode to update.rngo/evals/last - for Rust users,
Channel::finishandProxy::finishnow return aResult, and there's a newChannel::flushhook for custom channels that hold inputs back
See the batch channel reference and the CHANGELOG for all the details.