Public repository: timo / feel

Reader

An interpreter of FEEL, written in Rust, with integrations for PostgreSQL, WASM, Lua, and Python.

perf(pg): set feel_eval COST to 500 from measured per-call cost (RD-10)

public

perf(pg): set feel_eval COST to 500 from measured per-call cost (RD-10)

Replace the placeholder COST 100 with a measured value. On pg18 a parse-cached
feel_eval of `if person.age >= 18 then "adult" else "minor"` over 3M rows runs
~13.5 µs/call — ≈ 560× a baseline procost-1 C call (one cpu_operator_cost ≈ 24 ns
here), so COST 500 is the by-the-book figure rounded down (conservative; the
plpgsql=100 scale would put it ~1400). The bulk is the pgrx JsonB boundary
(jsonb↔text↔serde_json, twice per call), not FEEL eval: a comparable jsonb plpgsql
UDF using native binary jsonb is ~1.2 µs. feel_eval_immutable gets the same COST
(identical per-row work).

Also document that the functions are STRICT (pgrx default for non-Option args): an
explicit SQL NULL context yields NULL, while the context DEFAULT '{}' keeps the
context-free feel_eval('1+1') working. Record the boundary marshalling cost and the
binary-jsonb / lazy JsonbForeign optimisation as the COST revisit trigger in RD-10.

Verified in the pg18 catalog: procost=500 for both functions.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Changeset672a0dec202f20e33a02990ab93b7117719d4ef4
Author (claimed)Timo Stolz <timo.stolz@gmail.com>,
Parentsadfbbb7a2f7a
Children
Pushlog
by timo via ssh

modified DECISIONS.md +15 −6

@@ -991,9 +991,16 @@
991991
 - **Decision (2026-06-17, implemented):** keep one STABLE `feel_eval` and add a
992992
   second, IMMUTABLE `feel_eval_immutable`, rather than overloading volatility on one
993993
   function — a function has a single volatility class, and mislabelling one silently
994
-  corrupts any index built on it. Both are PARALLEL SAFE and `COST 100` (PostgreSQL's
995
-  default for a non-trivial/procedural function; a parse-cached eval of a low-to-medium
996
-  FEEL expression sits there, so the planner orders cheaper quals ahead of it).
994
+  corrupts any index built on it. Both are PARALLEL SAFE and `COST 500` — measured, not
995
+  guessed: a parse-cached eval of a low-to-medium expression runs ~13.5 µs/call on pg18,
996
+  ≈ 560× a baseline `procost`-1 C function (one `cpu_operator_cost` ≈ 24 ns on this box),
997
+  so 500 is the by-the-book figure rounded down (the plpgsql=100 scale would put it
998
+  ~1400; 500 is the conservative middle). The bulk is **not** FEEL evaluation but the
999
+  pgrx `JsonB` boundary, which round-trips jsonb↔text↔`serde_json` twice per call
1000
+  (`jsonb_out`+parse in, serialize+`jsonb_in` out); a comparable jsonb plpgsql UDF using
1001
+  native binary jsonb is ~1.2 µs. `feel_eval` is also STRICT (pgrx default for
1002
+  non-`Option` args): an explicit SQL `NULL` context yields `NULL`, but the `DEFAULT '{}'`
1003
+  keeps the context-free `feel_eval('1+1')` working — an accepted edge.
9971004
   `feel_eval_immutable` enforces determinism in two layers, both in feel-core:
9981005
   a *static* clock check (`CompiledExpr::is_clock_free`, cached in `fn_extra`) that
9991006
   **hard-ERRORs** on `now()`/`today()`; and a *runtime* named-zone ban
@@ -1005,9 +1012,11 @@
10051012
   that can mint one — the `@"…"` literal and the `time()`/`date and time()` string
10061013
   constructors — so a named zone can never exist to leak through a later comparison.
10071014
 - **Revisit trigger:** a need for named zones *inside* an index (then a
1008
-  tzdb-version-pinned variant, or a generated UTC-normalised column), or evidence that
1009
-  `COST 100` mis-orders plans for very heavy expressions (raise it, or make it
1010
-  expression-aware).
1015
+  tzdb-version-pinned variant, or a generated UTC-normalised column); evidence that
1016
+  `COST 500` mis-orders plans (re-tune it, or make it expression-aware); or — most
1017
+  likely — **optimising the jsonb boundary away** (a binary-jsonb traversal / lazy
1018
+  `JsonbForeign` per ADR-21, replacing the pgrx `JsonB` text round-trip), which would
1019
+  cut the real per-call cost toward feel-wasm's ~2 µs and warrant a much lower COST.
10111020
 - **If flipped (drop the immutable subset):** delete `feel_eval_immutable` and the two
10121021
   feel-core seams (`is_clock_free`, `forbidding_named_zones`/`vet_named_zone`); callers
10131022
   needing an index would precompute the value into a stored column via a trigger.

modified crates/feel-pg/src/lib.rs +14 −9

@@ -63,13 +63,18 @@
6363
 /// transaction — exactly why PostgreSQL's own `transaction_timestamp()` is STABLE.
6464
 /// **PARALLEL SAFE**: evaluation has no global mutable state, I/O or randomness,
6565
 /// and a parallel worker is a separate process with its own `fn_extra` cache.
66
-/// `COST 100` matches PostgreSQL's default for a non-trivial (procedural-language)
67
-/// function and is far above the C-function default of 1: a parse-cached eval of a
68
-/// low-to-medium FEEL expression (jsonb→context, a tree-walk, jsonb out) sits in
69
-/// that ballpark, so the planner orders cheaper quals ahead of it. It is *not*
70
-/// IMMUTABLE (`now()` varies across transactions) — see [`feel_eval_immutable`]
71
-/// for the deterministic subset usable in expression indexes (RD-10).
72
-#[pg_extern(stable, parallel_safe, cost = 100)]
66
+/// `COST 500`: a parse-cached eval of a low-to-medium expression measures
67
+/// ~13.5 µs/call on pg18 — ≈ 560× a baseline `procost`-1 C call (one
68
+/// `cpu_operator_cost` ≈ 24 ns here), rounded down (conservative). Most of that is
69
+/// the pgrx `JsonB` boundary (jsonb↔text↔`serde_json`, twice per call), not FEEL
70
+/// evaluation — so the planner keeps feel_eval out of hot loops and orders cheaper
71
+/// quals ahead of it (RD-10). The function is STRICT (pgrx default for non-`Option`
72
+/// args): a SQL `NULL` `expr` or `context` returns `NULL` unevaluated; the `context`
73
+/// `DEFAULT '{}'` keeps `feel_eval('1 + 1')` working — only an *explicit* `NULL`
74
+/// context short-circuits. It is *not* IMMUTABLE (`now()` varies across
75
+/// transactions) — see [`feel_eval_immutable`] for the deterministic subset usable
76
+/// in expression indexes (RD-10).
77
+#[pg_extern(stable, parallel_safe, cost = 500)]
7378
 fn feel_eval(
7479
     expr: &str,
7580
     context: default!(JsonB, "'{}'::jsonb"),
@@ -123,14 +128,14 @@
123128
 /// as immutable; their results are stable except across a libc/`regex` upgrade —
124129
 /// the same pragmatic stance PostgreSQL takes for its own such functions, after
125130
 /// which an index may need a one-off `REINDEX` (RD-10). `PARALLEL SAFE` and
126
-/// `COST 100` for the same reasons as [`feel_eval`].
131
+/// `COST 500` for the same reasons as [`feel_eval`].
127132
 ///
128133
 /// ```sql
129134
 /// -- Index a FEEL predicate over a jsonb column:
130135
 /// CREATE INDEX ON applications
131136
 ///   ((feel_eval_immutable('applicant.age >= 18 and applicant.income > 50000', data)));
132137
 /// ```
133
-#[pg_extern(immutable, parallel_safe, cost = 100)]
138
+#[pg_extern(immutable, parallel_safe, cost = 500)]
134139
 fn feel_eval_immutable(
135140
     expr: &str,
136141
     context: default!(JsonB, "'{}'::jsonb"),