Query Caching Behavior Updated
Hyperdrive now correctly treats queries containing PostgreSQL STABLE functions as uncacheable, in addition to previously detected VOLATILE functions. This change prevents queries from returning stale or incorrect data when STABLE functions are used.
What Changed
Previously, Hyperdrive only marked queries containing VOLATILE functions (such as RANDOM() or LASTVAL()) as uncacheable. STABLE functions like NOW(), CURRENT_TIMESTAMP, and CURRENT_DATE were incorrectly allowed to be cached.
Since STABLE functions can return different results across different SQL statements within the same transaction, caching their results could serve stale or incorrect data to your application.
Migration Path
If your queries use STABLE functions and were relying on caching behavior, you'll need to adjust your approach:
- Move the function call to your application code and pass the result as a query parameter
- Example: Instead of
WHERE created_at > NOW(), compute the timestamp in your Worker and pass it asWHERE created_at > $1
Implementation Details
Hyperdrive uses text-based pattern matching to detect uncacheable functions. Note that references to function names (like NOW()) in SQL comments will also cause the entire query to be marked as uncacheable.
For more information, see the Query caching documentation and troubleshooting guide.