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

From: Chao Li <li(dot)evan(dot)chao(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-27 02:42:01
Message-ID: B0634A39-1E91-4015-8FB0-14F16401E3CE@gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

> On Nov 26, 2026, at 21:18, 신성준 <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);

Following your repro, I confirmed this bug and your fix.

>
> which fails with a syntax error when run again, so a dump containing
> such a view can't be restored.
>

I’m not sure if you were referring to pg_dump and pg_restore. But pg_dump only uses the one-argument version of pg_get_viewdef(), which deparses views in non-pretty mode, so pg_dump should not be impacted by this bug. So, if you did, maybe clarify the commit message a little bit.

But I agree that a manual dump using pretty mode cannot be restored:
```
evantest=# create view v1 as SELECT SUBSTRING(s < 1 SIMILAR '%#"1#"'::text ESCAPE '#'::text) AS a FROM ( VALUES ('x'::text)) t(s);
ERROR: syntax error at or near "'%#"1#"'"
LINE 1: create view v1 as SELECT SUBSTRING(s < 1 SIMILAR '%#"1#"'::t...
^
```

> 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.

I think the solution is correct.

>
> Best regards,
> Seongjun Shin
> <v1-0001-Fix-deparsing-of-SUBSTRING-SIMILAR-with-an-operat.patch>

V1 looks good to me.

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Ayush Tiwari 2026-11-27 03:21:17 pg_receivewal doesn't truncate compressed WAL files on restart
Previous Message Hayato Kuroda (Fujitsu) 2026-11-27 02:31:25 RE: Wrong relation in apply worker "not updatable" errors PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>for partitions; relcache leak in check_and_init_gencol()