[DEPARSE] An alias named 'set' on an UPDATE, DELETE or MERGE target does not read back

Поиск
Список
Период
Сортировка
Искать

[DEPARSE] An alias named 'set' on an UPDATE, DELETE or MERGE target does not read back

От:
Henson Choi <assam258@gmail.com>
Дата:
Вложения:
Hi hackers,

When the target of an UPDATE, DELETE or MERGE in a rule or in a
BEGIN ATOMIC function body has the alias "set", ruleutils.c prints
the alias bare, and the text no longer parses.  The two attached
patches add tests and fix it.

The problem
-----------

The example below shows the output before and after the patch
(only the body of the rule is shown):

    create table t (a int, b int);
    create rule r as on insert to t
      do also update t "set" set b = 1 where "set".a = 2;
    select pg_get_ruledef(oid) from pg_rewrite where rulename = 'r';

    before: ON INSERT TO public.t DO  UPDATE t set SET b = 1
              WHERE (set.a = 2);
             ERROR:  syntax error at or near "b"
    after:  ON INSERT TO public.t DO  UPDATE t AS set SET b = 1
              WHERE (set.a = 2);

DELETE FROM t "set" and MERGE INTO t "set" are printed the same
way and fail at "set".  pg_dump prints rules and function bodies
with the same code, so a dump of such a database does not restore.

The target of these three statements is parsed by
relation_expr_opt_alias.  On seeing "set" after the relation name,
the parser has to decide with one token of lookahead whether it is
an alias or the SET keyword of UPDATE, and gram.y resolves that in
favor of the keyword (see the comment above
relation_expr_opt_alias).  DELETE and MERGE use the same
production, so they are caught too, although no SET clause follows
there.  As SET is an unreserved keyword, quote_identifier() leaves
"set" bare.

The fix
-------

0002 gives get_rte_alias() a dml_target argument and prints AS
before an alias named "set" on the target of UPDATE, DELETE and
MERGE.  INSERT already prints AS always.  An alias named "set" in
FROM is not ambiguous, so it is printed as before.

Quoting the alias only at that position, as in
UPDATE t "set" SET ..., would work as well.  Which of the two
would people prefer?

Tests
-----

0001 adds the tests with the output before the fix, and 0002
updates it, so that the diff shows what changes.  The rules and
the function are printed, dropped and created again from the
printed text.

  statement  where "set" is          object
  ---------  ----------------------  -------------------------
  UPDATE     target alias            rule
  DELETE     target alias            rule
  MERGE      target alias            BEGIN ATOMIC function
  SELECT     FROM alias (unchanged)  view

Back branches
-------------

I think this should be back-patched to 15.  I applied both
patches to REL_15_STABLE through REL_19_STABLE.

git am applies them cleanly on each branch, and make check passes,
also with the test patch alone.

This came out of an AI-assisted audit of ruleutils.c.  I
will post the other problems it found as separate,
independent reports as I work through them.

Best regards,
Henson

Re: [DEPARSE] An alias named 'set' on an UPDATE, DELETE or MERGE target does not read back

От:
Tom Lane <tgl@sss.pgh.pa.us>
Дата:
Henson Choi  writes:
> When the target of an UPDATE, DELETE or MERGE in a rule or in a
> BEGIN ATOMIC function body has the alias "set", ruleutils.c prints
> the alias bare, and the text no longer parses.  The two attached
> patches add tests and fix it.

Another good catch, but here again I find your solution
overcomplicated and perhaps not very future-proof.  Let's just
emit AS all the time in these statements.

			regards, tom lane


Re: [DEPARSE] An alias named 'set' on an UPDATE, DELETE or MERGE target does not read back

От:
Henson Choi <assam258@gmail.com>
Дата:
Вложения:
Hi Tom,

> Another good catch, but here again I find your solution
> overcomplicated and perhaps not very future-proof. Let's just
> emit AS all the time in these statements.

Agreed. The attached v2 writes AS before the alias of an UPDATE,
DELETE or MERGE target every time, as INSERT already does.

The tests are trimmed to UPDATE and DELETE in rules and MERGE in
a BEGIN ATOMIC function body, each with a target aliased "set"
and recreated from its deparsed text. Existing output in rules and
returning changes too, as in UPDATE rules_log AS trgt SET ... . The
full regression suite passes on master.

This changes the deparsed text of every aliased target. AS has been
accepted there in all supported branches, and the code change applies
cleanly to REL_15 through REL_19. I built each branch and ran the
regression tests.

Best regards,
Henson
FAQ