Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Ways to Use Improv

Improv is one modeling engine with several front ends. They all work on the same model: categories, items, measures, formulas, input cells and saved views. A model built in one opens in every other. Pick the one that fits the job, and switch when the job changes.

You want to…UseWhere the model lives
explore a model, pivot it, edit numbers, add formulas with a mousethe desktop GUIa store file, or Postgres
do the same over SSH or in a terminalthe TUIa store file, or Postgres
script a model, build it in CI, diff it, load data in bulkthe CLIa store file, or Postgres
push a live feed through a model and get the changed results outstreaminga store file, or Postgres
model data that already lives in Postgres, and read results with SQLpg_improvPostgres tables plus the improv catalog
write a SQL function as a formulaLANGUAGE improvthe function body
run a formula or a model over data in DuckDB, from SQLthe DuckDB extensionnowhere: computed per query
serve a model to other programs over HTTPthe servera store file, or Postgres

What each front end can do compares them feature by feature.

The model, wherever it lives

A model is a store file or lives in a Postgres database; Concepts: where data lives says what each holds. Every front end takes a store file's path. A Postgres model is named by a URL instead:

postgres://user@host/database?model=plan

Pass that URL to the CLI, TUI, GUI or server. With pg_improv installed, the GUI's edits to a bound measure update your table's rows. See Improv in Postgres.

Two writers never silently overwrite each other. If someone else saved after you loaded, your save is refused with a conflict; choose File ▸ Revert to Saved (GUI) or press R (TUI) to pick up their changes, then redo yours. Both notice another program's save without waiting for a conflict: the GUI checks every two seconds and offers Reload, and the TUI says so on the next key press.

The desktop GUI (improv-gui)

The GUI

Use it for: building and exploring a model by hand. You see several views at once, pivot by dragging, chart, and watch derived cells recompute as you type.

improv-gui sample.db
improv-gui 'postgres://me@127.0.0.1/sales?sslmode=disable&model=plan'
  • Pivot: drag a category tile between a worksheet's strips. Page tiles go top-left, column tiles top-right, row tiles in the bottom strip.
  • Edit: double-click a cell. Derived cells are read-only; hover one to see its formula.
  • Add a formula: type Margin = Revenue - COGS on the last line of the formula list.
  • More views: View ▸ New View opens another worksheet on the same model; Save View keeps the arrangement.

Every change is saved as you make it. The Desktop GUI walks through each window.

The terminal UI (improv-tui)

improv-tui on the sample model: an input typed as 1,500, Revenue recomputed, # giving it two decimals and grouped thousands, paging, pivoting, a range's Sum, and a saved view the next start opens on.

Use it for: the same pivot-and-edit loop without a display, over SSH, or in a tmux pane next to your logs.

improv-tui sample.db
improv-tui new.db     # a new file opens empty: build it with c, n and =
KeyDoes
arrows, clickmove
e / Enteredit the input cell (derived cells recompute live); e on a derived measure edits its formula, and a parse error is marked under the bad token, with a hint. Typing replaces the old value; an arrow first edits it in place; an empty edit clears the cell. A formula that is one value (5) takes a second Enter
a digit, -, .start editing the cell with that key
Tab / mnext measure
ppivot
[ ]page
Shift+arrowsselect a range; its sum shows in the status line
f / Fhide a row item / clear filters
S / v / Vsave the layout as a view, named as you type (Enter takes view N) / cycle views / rename (Old = New) or delete (-Name) a view
I / Eimport / export CSV
cadd a category and its items: Time: 2025, 2026
nadd an input measure over categories: Quantity: Time, Product
r / Drename / delete a category, item or measure: Time = Year, Product.WidgetA; formulas follow a rename, and a delete names the formulas that still need the thing first
=define: Revenue = Price * Quantity adds a derived measure, Margin = LAMBDA(p, q, p - q) a named LAMBDA
#the shown measure's number format: 2, 0 grouped, general
wsave now (each change is already saved as it is made; w retries one that failed, marked "not saved")
Rrevert to the saved model (the status line says when another program saved it); with edits or undo history to lose, press it twice
u / Ctrl+Rundo / redo
Tab (in a formula)complete a function, measure or category name; again to cycle
? / F1the keys, and a search over the functions; Enter inserts NAME( into the edit
qsave and quit; if the save fails (a full disk, another program saved), the TUI stays open with the reason, and q again quits without saving

The CLI (improv)

Use it for: anything you want to repeat. Build a model from a script, load a CSV or SQL query into a measure, evaluate a formula in CI, export results, or check a model into git as the script that builds it.

improv init m.db
improv add-category m.db 1 Time;    improv add-item m.db 10 1 Q1
improv add-category m.db 2 Product; improv add-item m.db 20 2 Widget
improv add-measure m.db 100 Price    number input Product
improv add-measure m.db 101 Quantity number input Time Product
improv set m.db 100 10  --at Product=Widget
improv set m.db 101 100 --at Time=Q1,Product=Widget
improv define m.db 102 "Revenue = Price * Quantity"
improv eval m.db 102

It also imports and exports CSV and SQLite (import-csv, import-sql, refresh-sql, export-csv, export-sql), runs Python functions in a sandbox (register-ext, CALL(...)), and keeps external measures fresh on a schedule (serve-refresh). Getting Started is a tutorial; man improv lists every command.

Streaming (improv stream)

Use it for: a live feed. Something produces new numbers, and you want the derived results that change, as they change, without recomputing the model.

tail -F prices.log | improv stream m.db Revenue Margin | your-consumer

Each input line is <measure> <value> [Cat=Item,...]. Improv applies it to a live incremental engine and prints only the target cells it changed:

$ printf 'Price 15 Product=Widget\nQuantity 8 Time=Q1,Product=Widget\n' \
    | improv stream m.db Revenue
Revenue Time=Q1, Product=Widget = 1500
Revenue Time=Q1, Product=Widget = 120

(Price 15 × the stored Quantity 100, then 15 × the new 8.)

A bad line is reported on stderr and skipped. At end of input the final state is saved to the model.

In Postgres (pg_improv)

pg_improv in psql: a model bound to a sales table, an UPDATE reaching the result table through the sync worker, improv.status, a LANGUAGE improv function, then the TUI editing the same model by URL, and the edit as a row of the table.

Use it for: data that already lives in Postgres. Bind your table's columns to categories and measures, define formulas, and read results from ordinary tables, with no export step and no second copy.

CREATE EXTENSION pg_improv;
SELECT improv.create_model('plan');
SELECT improv.bind_category('plan', 'Time',    'sales', 'quarter');
SELECT improv.bind_category('plan', 'Product', 'sales', 'product');
SELECT improv.bind_measure ('plan', 'Price',    'sales', 'price');
SELECT improv.bind_measure ('plan', 'Quantity', 'sales', 'qty');
SELECT improv.define('plan', 'Revenue = Price * Quantity');
SELECT * FROM improv_plan.revenue;     -- kept current as sales changes

How results follow your writes is the model's sync mode: Concepts: the sync modes is the decision.

Open the same model in the GUI or TUI with its postgres://…?model=plan URL: editing a cell there updates the row in sales. Improv in Postgres covers setup, value types, aggregated bindings and durability, and A Family's Finances is a complete worked example.

In DuckDB (the improv extension)

Use it for: analytics over data DuckDB already reads (tables, Parquet, CSV): a formula as a column, or a model over a table, inside one query. Nothing is stored; each query reads its source and computes.

LOAD improv;
SELECT improv_calc('ROUND(PMT(rate/12, n, -pv), 2)',
                   json_object('rate', rate, 'n', n, 'pv', pv)) FROM loans;
SELECT * FROM improv('sales', '
  category Time = year
  category Product = product
  input Price = price
  input Quantity = qty sum
  Revenue = Price * Quantity
  Share = Revenue / SUM(Revenue OVER Product)');

improv(...) returns one row per computed cell (measure, a column per category, value, value_text), so the result joins and pivots like any table. improv_functions() lists the built-in functions. Improv in DuckDB has the details.

SQL functions (LANGUAGE improv)

Use it for: a calculation you want in plain SQL, written as a formula. No model is needed.

CREATE FUNCTION bonus(sales float8, target float8) RETURNS float8
  LANGUAGE improv IMMUTABLE AS $$
    Ratio = sales / target
    MAX2(Ratio - 1, 0) * 1000
  $$;
SELECT rep, bonus(sales, target) FROM quota;

Arguments are the inputs, by name; each Name = expr line is an intermediate; the last line is the result. A call costs about 0.14–0.24 µs (this two-line bonus is the 0.24), about what PL/pgSQL costs.

The HTTP server (improv-server)

Use it for: letting another program read a model, ask what-if questions, or write input cells over HTTP.

With the model m.db from the CLI example, after the streaming example set Price to 15:

IMPROV_API_TOKEN=secret improv-server m.db 127.0.0.1:3000 &
curl -H 'Authorization: Bearer secret' -X POST localhost:3000/measures/102/eval \
  -H 'content-type: application/json' \
  -d '{"edits":[{"measure":101,"coord":[[1,10],[2,20]],"value":90}]}'
# [{"coord":[["Product","Widget"],["Time","Q1"]],"value":1350.0}]

An edit names its measure and coordinate by id ([[category, item], …]); the answer comes back by name. /eval computes through the live engine and writes nothing unless you ask it to ("persist": true). --static DIR serves a web front end's files beside the API. The HTTP Server lists the routes, tokens and limits.

What each front end can do

Checked against each tool on 2026-10-22 (0.11.0 development). yes: it does it; —: it does not; a note says how, or what is missing. The bold gaps are tracked as tasks for 1.0.

CLITUIGUIserverpg_improvDuckDB
Open a model (file or postgres://)yesfrom the command linefrom the command line (File ▸ Open is disabled)one per processits own catalog— (computed per query)
Create a modelinitopen a new file— (File ▸ New is disabled)—create_modelinline improv(source, model)
Add categories and itemsadd-category, add-itemcvia CSV import—bind_category (items from the column)category lines
Add an input measureadd-measurenvia CSV import—bind_measureinput lines
Open a model (file or postgres://)yesfrom the command lineFile ▸ Open…, or the command lineone per processits own catalog— (computed per query)
Create a modelinit—File ▸ New—create_modelinline improv(source, model)
Add categories and itemsadd-category, add-itemvia CSV importEdit ▸ Add Category / Add Item, or type in a tile—bind_category (items from the column)category lines
Add an input measureadd-measurevia CSV importEdit ▸ Add Input Measure—bind_measureinput lines
Rename, delete—r, DEdit ▸ Rename, Delete…—drop_measure—
Views (saved layouts)save-view, listS save, v cycle, V rename or deleteopen, save, several worksheets at once—— (a GUI's views are kept in the catalog)—
Pivotsave-view --rows --colspdrag tiles—— (result tables are flat)— (rows are flat)
Number formats (decimals, thousands separators)format; show, eval, stream print in it#Format menu— (values as numbers)kept in improv.measure.format; results are numbers—
Edit an input cellsetyesyesPOST /measures/:id/cellswrite your table—
Formulas: editdefine, add-derivede edits the viewed one, = adds oneedit and add—defineName = formula lines
Autocompleteshell completion of commands onlyTabas you type———
Located errorsparse error at Nmarked under the token, with a hintunderlined, explained—parse error at Nmodel line L: … at N
Function helpman improv-functions? / F1Help ▸ Functions…——improv_functions()
Import CSVimport-csvIwizard—COPY into a tableread_csv as the source
Import SQLimport-sql (SQLite), refresh-sql——refreshed by the schedulerbind any tableany query as the source
SQL("…") sourcedefine refuses it; use import-sql———refused—
Exportexport (JSON), export-csv, export-sqlE (CSV)wizard (CSV)GET /model (JSON)the result tablesit is a query
Scenariosscenario, eval --scenario—————
Undo—u, Ctrl+R to redoEdit ▸ Undo, Redo———
Named LAMBDAsdefine=calls work; define them in the CLIcomputes themdefine— (refused; inline LAMBDA works)
Undo——Edit ▸ Undo, Redo———
Named LAMBDAsdefinecalls work; define them in the CLIName = LAMBDA(…) on the formula list's last linecomputes themdefine— (refused; inline LAMBDA works)
FIXPOINTyesyes (the same parser and engine)yesyesyesin improv(…); not in improv_calc
what-if (compute, don't save)through a stored scenario——POST /measures/:id/evalwhat_if—
Live sync (see others' writes)each command reloadssays so on the next key; R reloadsoffers Reload within 2 seach request checks the high-water and reloadslogical, trigger, commit sync—
Controlled English—accepts it as a formulashows and accepts it/nl/parse, /nl/describe——
Keyboard only, screen readersyesyes; works under NO_COLORyes; every control named for AccessKit———

Every front end that saves refuses to overwrite another writer: the save fails with a conflict, and you reload and redo the change.

Choosing quickly

  • One person, a model to think with: GUI, or the TUI over SSH.
  • A model that is code: a CLI script in git, then open it in the GUI.
  • A feed of numbers: improv stream.
  • Data in Postgres, results for SQL users: pg_improv, logical mode. Use trigger if a writer must read its own results in the same transaction.
  • A formula as a SQL function: LANGUAGE improv.
  • Data in DuckDB, Parquet or CSV, answers in one query: the DuckDB extension (improv_calc, improv(source, model)).
  • Another service needs answers: improv-server.