Re: Fix deparsing of SUBSTRING SIMILAR with an operator as first argument

From: Osama Abdul Qader <osamaabdulqaderpostgres(at)gmail(dot)com>
To: 신성준 <shinsj4653(at)gmail(dot)com>
Cc: pgsql-hackers mailing list <pgsql-hackers(at)lists(dot)postgresql(dot)org>, Henson Choi <assam258(at)gmail(dot)com>
Subject: Re: Fix deparsing of SUBSTRING SIMILAR with an operator as first argument
Date: 2026-11-26 19:20:08
Message-ID: CAAprpiDUG2P_xLF4e7woV_xmNaoZiS4+Pk32=9yW-9amnsMrbg@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi again

On Thu, 26 Nov 2026 at 19:01, Osama Abdul Qader <
osamaabdulqaderpostgres(at)gmail(dot)com> wrote:

> Hi 신성준
>
> On Thu, 26 Nov, 2026, 6:48 pm 신성준, <shinsj4653(at)gmail(dot)com> wrote:
>
>> Hi hackers,
>>
>> pg_get_viewdef() prints the first argument of SUBSTRING(... SIMILAR
>> ... ESCAPE ...) without parentheses. Since SIMILAR binds more tightly
>> than comparison operators, an argument like "s < 1" comes out in a
>> form that doesn't parse:
>>
>> CREATE FUNCTION f(text, int) RETURNS text
>> LANGUAGE sql IMMUTABLE AS 'SELECT $1 || $2';
>> CREATE OPERATOR < (LEFTARG = text, RIGHTARG = int, FUNCTION = f);
>> CREATE VIEW v AS
>> SELECT substring((s < 1) SIMILAR '%#"1#"' ESCAPE '#') AS a
>> FROM (VALUES ('x'::text)) t(s);
>>
>> pg_get_viewdef('v', true) gives
>>
>> SELECT SUBSTRING(s < 1 SIMILAR '%#"1#"'::text ESCAPE '#'::text) AS a
>> FROM ( VALUES ('x'::text)) t(s);
>>
>> which fails with a syntax error when run again, so a dump containing
>> such a view can't be restored.
>>
>> The attached patch prints that argument with get_rule_expr_paren(), as
>> the IS NORMALIZED case does. The other two arguments don't need it.
>>
>> Best regards,
>> Seongjun Shin
>>
>
> I'm on my way to review the patch once I reach to my den.
>

I've tested your patch regarding SUBSTRING SIMILAR deparsing.

I was able to reproduce the original issue on an unpatched PostgreSQL build.
`pg_get_viewdef()` omitted the parentheses around the operator expression,
and attempting to recreate the view from the generated definition resulted
in a syntax error.

After applying the patch:

- The `create_view` regression tests passed all 239 cases.
- pg_get_viewdef() correctly preserved the parentheses around `(s < 1)`.
-

I successfully recreated the view using the generated definition through
`\gexec`.

The only question I have is: *Whether this issue is registered in
commitfest?*, if yes then I'd like to register myself as reviewer there.

*The end-to-end reproduction now works as expected.*

Thank you for addressing the issue.

Best Regards,

Osama Abdul Qader

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Osama Abdul Qader 2026-11-26 19:28:58 Re: Preserve tablespace of exclusion constraint indexes in ALTER TYPE
Previous Message shihao zhong 2026-11-26 19:13:39 Re: Two fixes for parallel query cleanup: DSM detach order and a stray statement timeout