Another multi-row VALUES bug
От | Dean Rasheed |
---|---|
Тема | Another multi-row VALUES bug |
Дата | |
Msg-id | CAEZATCV39OOW7LAR_Xq4i+Lc1Byux=eK3Q=HD_pF1o9LBt=phA@mail.gmail.com обсуждение исходный текст |
Ответы |
Re: Another multi-row VALUES bug
|
Список | pgsql-hackers |
I was thinking some more about the recent fix to multi-row VALUES handling in the rewriter (b8f2687fdc), and I realised that there is another bug in the way DEFAULT values are handled: In RewriteQuery(), the code assumes that in a multi-row INSERT query, the VALUES RTE will be the only thing in the query's fromlist. That's true for the original query, but it's not necessarily the case for product queries, if the rule action performs a multi-row insert, leading to a new VALUES RTE that the DEFAULT-handling code might fail to process. For example: CREATE TABLE foo(a int); INSERT INTO foo VALUES (1); CREATE TABLE foo_log(t timestamptz DEFAULT now(), a int, c text); CREATE RULE foo_r AS ON UPDATE TO foo DO ALSO INSERT INTO foo_log VALUES (DEFAULT, old.a, 'old'), (DEFAULT, new.a, 'new'); UPDATE foo SET a = 2 WHERE a = 1; ERROR: unrecognized node type: 43 There's a similar example to this in the regression tests, but it doesn't test DEFAULT-handling. It's also possible for the current code to cause the same VALUES RTE to be rewritten multiple times, when recursing into product queries (if the rule action doesn't add any more stuff to the query's fromlist). That turns out to be harmless, because the second time round it will no longer contain any defaults, but it's technically incorrect, and certainly a waste of cycles. So I think what the code needs to do is examine the targetlist, and identify the VALUES RTE that the current query is using as a source, and rewrite just that RTE (so any original VALUES RTE is rewritten at the top level, and any VALUES RTEs from rule actions are rewritten while recursing, and none are rewritten more than once), as in the attached patch. While at it, I noticed an XXX code comment questioning whether any of this applies to MERGE. The answer is "no", because MERGE actions don't allow multi-row inserts, so I think it's worth updating that comment to make that clearer. Regards, Dean
Вложения
В списке pgsql-hackers по дате отправления: