Public repository: timo / feel

Reader

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

docs: record RD-10 (feel-pg volatility & immutable subset)

public

docs: record RD-10 (feel-pg volatility & immutable subset)

Document the STABLE feel_eval / IMMUTABLE feel_eval_immutable split: why
feel_eval is STABLE PARALLEL SAFE COST 100 and cannot be IMMUTABLE, why the
immutable subset is a separate function (a function has one volatility class),
the two-layer determinism enforcement (static clock check + runtime named-zone
ban), and the kept/rejected zone and math/regex trade-offs. Add the
expression-index example to the README and list both functions in the overview.

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

modified DECISIONS.md +40 −0

@@ -972,6 +972,46 @@
972972
 - **If flipped:** offer an opt-in eager snapshot (force the whole subtree at the boundary)
973973
   while keeping lazy the default — the same escape hatch RD-6 describes.
974974
 
975
+### RD-10: feel-pg volatility — STABLE `feel_eval`, plus an IMMUTABLE `feel_eval_immutable`
976
+
977
+- **Weighed:** `feel_eval` shipped as the pgrx default (VOLATILE, PARALLEL UNSAFE),
978
+  which over-constrains the planner and bars the function from index expressions.
979
+  But the evaluator is referentially transparent: no global mutable state, I/O or
980
+  randomness (ADR-1), context output is `BTreeMap`-ordered, and the *only* ambient
981
+  input is the instant behind `now()`/`today()` (ADR-18), which feel-pg pins to the
982
+  transaction start. That makes `feel_eval` precisely **STABLE** (the same reason
983
+  PostgreSQL's own `transaction_timestamp()` is STABLE) and **PARALLEL SAFE** (a
984
+  parallel worker is a separate process with its own `fn_extra` cache). It cannot be
985
+  IMMUTABLE — `now()` differs across transactions. The pull toward an immutable
986
+  surface is concrete: expression indexes (and generated columns, constant folding)
987
+  over a `jsonb` column require an IMMUTABLE callee. Three hazards to a "pure" subset:
988
+  (1) `now()`/`today()`; (2) *named* IANA zones, whose offset is read from the
989
+  updatable tzdb; (3) `exp`/`log`/`sqrt` and the regex builtins, stable only up to a
990
+  libc / `regex`-crate version.
991
+- **Decision (2026-06-17, implemented):** keep one STABLE `feel_eval` and add a
992
+  second, IMMUTABLE `feel_eval_immutable`, rather than overloading volatility on one
993
+  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).
997
+  `feel_eval_immutable` enforces determinism in two layers, both in feel-core:
998
+  a *static* clock check (`CompiledExpr::is_clock_free`, cached in `fn_extra`) that
999
+  **hard-ERRORs** on `now()`/`today()`; and a *runtime* named-zone ban
1000
+  (`EvalEnv::forbidding_named_zones`) that turns a named zone into null (ERROR under
1001
+  `strict => true`). Fixed offsets and `Z` are kept — they are tzdb-independent and
1002
+  all an index needs. Math/regex are accepted as immutable: a libc/`regex` upgrade is
1003
+  rare and at worst wants a one-off `REINDEX`, the same pragmatic stance PostgreSQL
1004
+  takes for its own such functions. The named-zone ban is applied at the only sites
1005
+  that can mint one — the `@"…"` literal and the `time()`/`date and time()` string
1006
+  constructors — so a named zone can never exist to leak through a later comparison.
1007
+- **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).
1011
+- **If flipped (drop the immutable subset):** delete `feel_eval_immutable` and the two
1012
+  feel-core seams (`is_clock_free`, `forbidding_named_zones`/`vet_named_zone`); callers
1013
+  needing an index would precompute the value into a stored column via a trigger.
1014
+
9751015
 ## Roadmap: deferred scope (recorded, not yet built)
9761016
 
9771017
 These were considered and deliberately deferred to keep the slice thin. Listed so

modified README.md +6 −1

@@ -9,7 +9,7 @@
99
 
1010
 ```text
1111
 crates/feel-core   pure engine (lalrpop grammar + AST + evaluator); no target deps
12
-crates/feel-pg     pgrx wrapper:  feel_eval(text, jsonb) RETURNS jsonb
12
+crates/feel-pg     pgrx wrapper:  feel_eval / feel_eval_immutable (text, jsonb) RETURNS jsonb
1313
 crates/feel-wasm   wasm-bindgen wrapper:  evaluate(expr, context) -> JS value
1414
 crates/feel-py     PyO3 wrapper:  evaluate(expr, context) -> Python value
1515
 crates/feel-lua    mlua wrapper:  feel.evaluate(expr, context) -> Lua value
@@ -187,6 +187,11 @@
187187
 #   SELECT feel_eval('5 in [1..10]', '{}');             -- true
188188
 #   SELECT feel_eval('if 1 < 2 then 10 else 20');       -- 10
189189
 #   SELECT feel_eval('for i in [1,2,3] return i + 1');  -- [2, 3, 4]
190
+#
191
+#   -- feel_eval is STABLE PARALLEL SAFE; feel_eval_immutable is the IMMUTABLE,
192
+#   -- deterministic subset (no now()/today(), no named time zones) for indexing:
193
+#   CREATE INDEX ON applications
194
+#     ((feel_eval_immutable('applicant.age >= 18 and applicant.income > 50000', data)));
190195
 ```
191196
 
192197
 Note: the pgrx extension name is `feel_pg` (underscored) — see