Fix deparsing of SUBSTRING SIMILAR with an operator as first argument

Started by 신성준2 days ago6 messageshackers

Thread notes

1 note#bug
hackorum-bot#bug
hackorum-bot
2 days ago

@global #bug

#bug
Jump to latest
#1신성준
shinsj4653@gmail.com

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

Attachments:

t259874_1
v1-0001-Fix-deparsing-of-SUBSTRING-SIMILAR-with-an-operat.patchapplication/octet-stream; name=v1-0001-Fix-deparsing-of-SUBSTRING-SIMILAR-with-an-operat.patchDownload+31-2
#2Osama Abdul Qader
osamaabdulqaderpostgres@gmail.com
In reply to: 신성준 (#1)
Re: Fix deparsing of SUBSTRING SIMILAR with an operator as first argument

Hi 신성준

On Thu, 26 Nov, 2026, 6:48 pm 신성준, <shinsj4653@gmail.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.

Show quoted text
#3Osama Abdul Qader
osamaabdulqaderpostgres@gmail.com
In reply to: Osama Abdul Qader (#2)
Re: Fix deparsing of SUBSTRING SIMILAR with an operator as first argument

Hi again

On Thu, 26 Nov 2026 at 19:01, Osama Abdul Qader <
osamaabdulqaderpostgres@gmail.com> wrote:

Hi 신성준

On Thu, 26 Nov, 2026, 6:48 pm 신성준, <shinsj4653@gmail.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

#4신성준
shinsj4653@gmail.com
In reply to: Osama Abdul Qader (#3)
Re: Fix deparsing of SUBSTRING SIMILAR with an operator as first argument

Hi Osama,

Thank you for testing the patch and verifying the regression tests and
reproduction steps.

Yes, I've registered this patch in the Commitfest. Here is the link:
https://commitfest.postgresql.org/patch/7726/

I really appreciate you taking the time to review it.

Best regards,

Seongjun Shin.

#5Chao Li
li.evan.chao@gmail.com
In reply to: 신성준 (#1)
Re: Fix deparsing of SUBSTRING SIMILAR with an operator as first argument

On Nov 26, 2026, at 21:18, 신성준 <shinsj4653@gmail.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/

#6Osama Abdul Qader
osamaabdulqaderpostgres@gmail.com
In reply to: 신성준 (#4)
Re: Fix deparsing of SUBSTRING SIMILAR with an operator as first argument

Hi Seongjun

On Fri, 27 Nov 2026 at 05:17, 신성준 <shinsj4653@gmail.com> wrote:

Hi Osama,

Thank you for testing the patch and verifying the regression tests and
reproduction steps.

Yes, I've registered this patch in the Commitfest. Here is the link:
https://commitfest.postgresql.org/patch/7726/

I really appreciate you taking the time to review it.

Best regards,

Seongjun Shin.

Thank you, I've successfully added myself as a reviewer there.

I hope that this patch of yours becomes ultra beneficial for the project.

Best Regards,
Osama Abdul Qader