Variable substitution in jsonb functions fails for jsonpath operator like_regex - Mailing list pgsql-bugs

From Erwin Brandstetter
Subject Variable substitution in jsonb functions fails for jsonpath operator like_regex
Date
Msg-id CAGHENJ4A8awD2uXHkf50eV+9wyiDm3eP+G10J09+0VRmXBQAdw@mail.gmail.com
Whole thread Raw
Responses Re: Variable substitution in jsonb functions fails for jsonpath operator like_regex  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-bugs
The functions jsonb_path_exists() and friends accept a "vars" parameter for parameter substitution in the jsonpath argument. This seems to work for all jsonpath operators except "like_regex":

SELECT * FROM tbl
WHERE  jsonb_path_exists(data, '$[*].value ? (@ like_regex $foo)', '{"foo": "CEO"}');

> ERROR:  syntax error at or near "$foo" of jsonpath input
> LINE 3: WHERE  jsonb_path_exists(data, '$[*].value ? (@ like_regex $...
                                 
Notably, the same works even for "starts with":

SELECT * FROM tbl
WHERE  jsonb_path_exists(data, '$[*].value ? (@ starts with $foo)', '{"foo": "CEO"}');

I also tested related jsonb functions including jsonb_path_query(), jsonb_path_query_first(), jsonb_path_query(). Always the same error message.

Here is the question on stackoverflow.com that brought the issue to my attention (plus my answer with more details):

Here is a related fiddle to play with:

Tested with Postgres 16.0.

Regards
Erwin

pgsql-bugs by date:

Previous
From: PG Bug reporting form
Date:
Subject: BUG #18161: ALTER TABLE ... ALTER COLUMN does not remove dependencies in automatically from column and sequence.
Next
From: Laurenz Albe
Date:
Subject: Re: BUG #18161: ALTER TABLE ... ALTER COLUMN does not remove dependencies in automatically from column and sequence.