Abstract

A query-result cache keyed on SQL text cannot serve a query whose result depends on the session that runs it: `current_user`, `current_setting('app.tenant')`, a STABLE helper function reading such a setting, or `date_trunc` under the session's time zone yield different rows for the same text, so such queries are conventionally forwarded to the origin. This disclosure describes a method by which a caching proxy admits them. First, at classification the proxy identifies the maximal subtrees built only of foldable operations over session terms; each becomes a keyed fragment. The connection tracks the settings the fragments read at session and transaction scope, probing the origin once in a batch when a value is unknown and resolving `set_config` values from bind parameters without a probe. At dispatch each fragment is evaluated against the session's values and the pair of fragment hash and literal enters the cache key; the writer substitutes the literals before fingerprinting, so sessions with equal values share one entry and a raw setting such as a token's claims never reaches the key. Second, the body of an eligible SQL-language function is inlined under the database planner's own rules, so its session terms fold like any other and its tables join change-stream maintenance. Third, a built-in function that is STABLE only because of a setting is rewritten per session to an immutable form carrying the setting's value as a literal.

Creative Commons License

Creative Commons License
This work is licensed under a Creative Commons Attribution 4.0 License.

Share

COinS