BUG #19885: pg_input_is_valid(tsquery) throws stack depth error instead of returning false

From: PG Bug reporting form <noreply(at)postgresql(dot)org>
To: pgsql-bugs(at)lists(dot)postgresql(dot)org
Cc: imchifan8(at)gmail(dot)com
Subject: BUG #19885: pg_input_is_valid(tsquery) throws stack depth error instead of returning false
Date: 2026-11-11 14:37:34
Message-ID: 19885-741df1acb48cdad1@postgresql.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

The following bug has been logged on the website:

Bug reference: 19885
Logged by: Noah
Email address: imchifan8(at)gmail(dot)com
PostgreSQL version: 18.6
Operating system: Linux/amd64
Description:

pg_input_is_valid() throws SQLSTATE 54001 while validating a deeply
parenthesized tsquery when max_stack_depth is 100kB. Ordinary malformed
tsquery input returns false through the soft-error interface, but the
recursive nesting path raises “stack depth limit exceeded.” Both inputs are
invalid, so pg_input_is_valid() should return false for both rather than
letting the input function's error escape.

How to repeat:

Save the following as case.sql and run it with `psql -X -f case.sql`:

```
\set ON_ERROR_STOP on
SET max_stack_depth = '100kB';

DO $case$
DECLARE
result boolean;
BEGIN
-- Control: ordinary bad input is reported through the soft-error API.
BEGIN
result := pg_input_is_valid(
'word &',
'tsquery');
RAISE NOTICE 'control=returned value=%', result;
EXCEPTION WHEN OTHERS THEN
RAISE NOTICE 'control=threw sqlstate=% message=%',
SQLSTATE, SQLERRM;
END;

-- Candidate: equivalent user-controlled complexity through recursion.
BEGIN
result := pg_input_is_valid(
repeat('(', 1000) || 'word' || repeat(')', 1000),
'tsquery');
RAISE NOTICE 'nested=returned value=%', result;
EXCEPTION WHEN OTHERS THEN
RAISE NOTICE 'nested=threw sqlstate=% message=%',
SQLSTATE, SQLERRM;
END;
END
$case$;

RESET max_stack_depth;
```

The recorded result was:

```
SET
NOTICE: control=returned value=f
NOTICE: nested=threw sqlstate=54001 message=stack depth limit exceeded
DO
RESET
```

The issue was reproduced on PostgreSQL 18.6 on Linux/amd64. The server
remained available after the statement, and no server-log error accompanied
the caught exception. This occurs when pg_input_is_valid checks a deeply
parenthesized tsquery under a low max_stack_depth setting.

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message PG Bug reporting form 2026-11-11 14:49:13 BUG #19886: pg_terminate_backend timeout expires early after latch wakeups
Previous Message PG Bug reporting form 2026-11-11 14:13:50 BUG #19884: pg_publication_tables omits a detach-pending partition in root mode