Repository navigation
Description
What type of bug is this?
Unexpected error
What subsystems and features are affected?
Query executor, Query planner
What happened?
Starting with 2.30.0, a query on a hypertable with ORDER BY <time> ... LIMIT fails with operator does not exist when its WHERE clause calls an inlinable SQL function (no SET search_path) whose body uses an unqualified extension operator (here ltree @>, installed in public). The same query works on 2.29.2, works on a plain table, and works with SET timescaledb.enable_deferred_chunk_append = off.
Cause: this comes from the new DeferredChunkAppend node (#10292). It deparses the outer quals to SQL text at plan time, and then in open_next_chunk() re-parses and plans a per-chunk query under RestrictSearchPath() (pg_catalog, pg_temp). The deparse step qualifies the names in the outer expression. The plan shows Filter: (s.has_access(...)) and OPERATOR(public.=). The deparse step can't qualify names inside the body of a called SQL function. When the per-chunk query is planned, inline_function() parses the function body under the restricted search_path, the unqualified @> doesn't resolve, and the query fails with an error (SQL function "..." during inlining).
The result is that whether a plain SELECT succeeds depends on which plan the planner picks: the same function works in every other context. The caller's search_path doesn't help. It fails with the default "$user", public and also inside a function declared SET search_path TO pg_catalog, <schema>, public, pg_temp. This affects any SQL function that uses unqualified operators or functions from an extension (ltree, PostGIS, pg_trgm, citext, ...) and is called in a filter on a hypertable that qualifies for DeferredChunkAppend.
The hardening commit on main (6237f74, "Harden the DeferredChunkAppend deparse and parse cycle") covers GUCs that change literal output. It doesn't seem to cover this: main still calls RestrictSearchPath() around the per-chunk parse and plan.
Possible fixes: plan the per-chunk query with the search_path that was active when the outer query was planned (the deparsed text is already fully qualified, so restricting the path isn't needed for the outer quals). Or don't choose DeferredChunkAppend when the quals contain calls to non-builtin SQL functions.
Downstream impact and workaround: we hit this in Memory Engine. Our CI image picked up 2.30.x, and an access-check helper used on a hypertable audit log started to fail. We worked around it by schema-qualifying the operator (operator(public.@>)): timescale/memory-engine#216
TimescaleDB version affected
2.30.0, 2.30.1, 2.30.2 (2.28.3, 2.29.0, and 2.29.2 are not affected)
PostgreSQL version used
18.6
What operating system did you use?
Debian 13 (trixie), aarch64 (pgvector/pgvector:0.8.2-pg18-trixie image + timescaledb-2-postgresql-18 from packagecloud). Also seen on GitHub Actions ubuntu-latest (x86_64) with the same image.
What installation method did you use?
Docker, Deb/Apt
What platform did you run on?
On prem/Self-hosted
Relevant log output and stack trace
ERROR: operator does not exist: public.ltree @> public.ltree
LINE 1: select exists (select 1 where _grant @> _path)
^
HINT: No operator matches the given name and argument types. You might need to add explicit type casts.
QUERY: select exists (select 1 where _grant @> _path)
CONTEXT: SQL function "has_access" during inlining
Plan, when the function body is made to resolve:
Limit
-> Custom Scan (DeferredChunkAppend) on ev e
Order: at DESC
Filter: (s.has_tree_access('[...]'::jsonb, tree, 1)) AND ((tree OPERATOR(public.=) 'a.b'::public.ltree))
How can we reproduce the bug?
Run in a fresh database with the default search_path. Query 1 fails on 2.30.x and succeeds on 2.29.2. Query 2 succeeds on both.
create extension if not exists timescaledb;
create extension if not exists ltree; -- installed in public
create schema s;
create table s.ev (tree ltree not null, at timestamptz not null default now());
select create_hypertable('s.ev', by_range('at', interval '1 month'));
insert into s.ev (tree) values ('a.b');
-- Inlinable SQL function (no SET search_path) that uses an extension operator.
create function s.has_access(_grant ltree, _path ltree) returns bool
as $$ select exists (select 1 where _grant @> _path) $$
language sql immutable strict;
-- 1. Plain session search_path ("$user", public): fails on 2.30.x
select e.tree from s.ev e
where s.has_access('a', e.tree)
order by e.at desc limit 1;
-- 2. Same query with the new node disabled: works
set timescaledb.enable_deferred_chunk_append = off;
select e.tree from s.ev e
where s.has_access('a', e.tree)
order by e.at desc limit 1;
Activity
Fix search_path used for planning DeferredChunkAppend chunk queries
Fix search_path used for planning DeferredChunkAppend chunk queries
Fix search_path used for planning DeferredChunkAppend chunk queries
Release 2.31.0
Release 2.31.0