forked frompostgres/postgres
- Notifications
You must be signed in to change notification settings - Fork6
Commit049e1e2
committed
Fix mishandling of resjunk columns in ON CONFLICT ... UPDATE tlists.
It's unusual to have any resjunk columns in an ON CONFLICT ... UPDATElist, but it can happen when MULTIEXPR_SUBLINK SubPlans are present.If it happens, the ON CONFLICT UPDATE code path would end up storingtuples that include the values of the extra resjunk columns. That'sfairly harmless in the short run, but if new columns are added tothe table then the values would become accessible, possibly leadingto malfunctions if they don't match the datatypes of the new columns.This had escaped notice through a confluence of missing sanity checks,including* There's no cross-check that a tuple presented to heap_insert orheap_update matches the table rowtype. While it's difficult tocheck that fully at reasonable cost, we can easily add assertionsthat there aren't too many columns.* The output-column-assignment cases in execExprInterp.c lackedany sanity checks on the output column numbers, which seems likean oversight considering there are plenty of assertion checks oninput column numbers. Add assertions there too.* We failed to apply nodeModifyTable's ExecCheckPlanOutput() tothe ON CONFLICT UPDATE tlist. That wouldn't have caught thisspecific error, since that function is chartered to ignore resjunkcolumns; but it sure seems like a bad omission now that we've seenthis bug.In HEAD, the right way to fix this is to make the processing ofON CONFLICT UPDATE tlists work the same as regular UPDATE tlistsnow do, that is don't add "SET x = x" entries, and useExecBuildUpdateProjection to evaluate the tlist and combine it withold values of the not-set columns. This adds a little complicationto ExecBuildUpdateProjection, but allows removal of a comparableamount of now-dead code from the planner.In the back branches, the most expedient solution seems to be to(a) use an output slot for the ON CONFLICT UPDATE projection thatactually matches the target table, and then (b) invent a variant ofExecBuildProjectionInfo that can be told to not store values resultingfrom resjunk columns, so it doesn't try to store into nonexistentcolumns of the output slot. (We can't simply ignore the resjunk columnsaltogether; they have to be evaluated for MULTIEXPR_SUBLINK to work.)This works back to v10. In 9.6, projections work much differently andwe can't cheaply give them such an option. The 9.6 version of thispatch works by inserting a JunkFilter when it's necessary to get ridof resjunk columns.In addition, v11 and up have the reverse problem when trying toperform ON CONFLICT UPDATE on a partitioned table. Through afurther oversight, adjust_partition_tlist() discarded resjunk columnswhen re-ordering the ON CONFLICT UPDATE tlist to match a partition.This accidentally prevented the storing-bogus-tuples problem, butat the cost that MULTIEXPR_SUBLINK cases didn't work, typicallycrashing if more than one row has to be updated. Fix by preservingresjunk columns in that routine. (I failed to resist the temptationto add more assertions there too, and to do some minor codebeautification.)Per report from Andres Freund. Back-patch to all supported branches.Security:CVE-2021-320281 parentf02b908 commit049e1e2
File tree
15 files changed
+265
-205
lines changed- src
- backend
- access/heap
- executor
- nodes
- optimizer
- plan
- prep
- include
- executor
- nodes
- optimizer
- test/regress
- expected
- sql
15 files changed
+265
-205
lines changedLines changed: 8 additions & 0 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
2070 | 2070 |
| |
2071 | 2071 |
| |
2072 | 2072 |
| |
| 2073 | + | |
| 2074 | + | |
| 2075 | + | |
| 2076 | + | |
2073 | 2077 |
| |
2074 | 2078 |
| |
2075 | 2079 |
| |
| |||
3255 | 3259 |
| |
3256 | 3260 |
| |
3257 | 3261 |
| |
| 3262 | + | |
| 3263 | + | |
| 3264 | + | |
| 3265 | + | |
3258 | 3266 |
| |
3259 | 3267 |
| |
3260 | 3268 |
| |
|
Lines changed: 83 additions & 29 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
485 | 485 |
| |
486 | 486 |
| |
487 | 487 |
| |
488 |
| - | |
489 |
| - | |
490 |
| - | |
491 |
| - | |
492 |
| - | |
493 |
| - | |
494 |
| - | |
495 |
| - | |
| 488 | + | |
| 489 | + | |
| 490 | + | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
| 494 | + | |
| 495 | + | |
| 496 | + | |
| 497 | + | |
| 498 | + | |
| 499 | + | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
496 | 503 |
| |
497 | 504 |
| |
498 | 505 |
| |
| |||
503 | 510 |
| |
504 | 511 |
| |
505 | 512 |
| |
506 |
| - | |
| 513 | + | |
| 514 | + | |
507 | 515 |
| |
508 | 516 |
| |
509 | 517 |
| |
| |||
525 | 533 |
| |
526 | 534 |
| |
527 | 535 |
| |
528 |
| - | |
| 536 | + | |
| 537 | + | |
| 538 | + | |
| 539 | + | |
529 | 540 |
| |
530 | 541 |
| |
531 | 542 |
| |
532 | 543 |
| |
533 | 544 |
| |
534 | 545 |
| |
535 |
| - | |
536 |
| - | |
| 546 | + | |
| 547 | + | |
537 | 548 |
| |
538 | 549 |
| |
539 | 550 |
| |
540 |
| - | |
| 551 | + | |
541 | 552 |
| |
542 | 553 |
| |
543 | 554 |
| |
| |||
569 | 580 |
| |
570 | 581 |
| |
571 | 582 |
| |
572 |
| - | |
573 |
| - | |
574 |
| - | |
| 583 | + | |
| 584 | + | |
| 585 | + | |
575 | 586 |
| |
576 |
| - | |
577 |
| - | |
578 | 587 |
| |
579 | 588 |
| |
580 | 589 |
| |
| |||
587 | 596 |
| |
588 | 597 |
| |
589 | 598 |
| |
| 599 | + | |
| 600 | + | |
| 601 | + | |
| 602 | + | |
| 603 | + | |
| 604 | + | |
| 605 | + | |
| 606 | + | |
| 607 | + | |
| 608 | + | |
590 | 609 |
| |
591 | 610 |
| |
592 | 611 |
| |
593 |
| - | |
594 |
| - | |
595 |
| - | |
| 612 | + | |
| 613 | + | |
| 614 | + | |
| 615 | + | |
596 | 616 |
| |
597 | 617 |
| |
598 |
| - | |
| 618 | + | |
599 | 619 |
| |
600 | 620 |
| |
601 | 621 |
| |
| |||
628 | 648 |
| |
629 | 649 |
| |
630 | 650 |
| |
631 |
| - | |
632 |
| - | |
633 |
| - | |
634 |
| - | |
635 |
| - | |
636 |
| - | |
637 |
| - | |
| 651 | + | |
| 652 | + | |
| 653 | + | |
| 654 | + | |
| 655 | + | |
| 656 | + | |
| 657 | + | |
| 658 | + | |
| 659 | + | |
| 660 | + | |
| 661 | + | |
| 662 | + | |
| 663 | + | |
| 664 | + | |
| 665 | + | |
| 666 | + | |
| 667 | + | |
| 668 | + | |
| 669 | + | |
| 670 | + | |
| 671 | + | |
| 672 | + | |
| 673 | + | |
| 674 | + | |
| 675 | + | |
| 676 | + | |
| 677 | + | |
| 678 | + | |
| 679 | + | |
| 680 | + | |
| 681 | + | |
| 682 | + | |
| 683 | + | |
| 684 | + | |
| 685 | + | |
| 686 | + | |
| 687 | + | |
| 688 | + | |
| 689 | + | |
| 690 | + | |
| 691 | + | |
638 | 692 |
| |
639 | 693 |
| |
640 | 694 |
| |
|
Lines changed: 9 additions & 1 deletion
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
626 | 626 |
| |
627 | 627 |
| |
628 | 628 |
| |
| 629 | + | |
629 | 630 |
| |
630 | 631 |
| |
631 | 632 |
| |
| |||
642 | 643 |
| |
643 | 644 |
| |
644 | 645 |
| |
| 646 | + | |
645 | 647 |
| |
646 | 648 |
| |
647 | 649 |
| |
| |||
658 | 660 |
| |
659 | 661 |
| |
660 | 662 |
| |
| 663 | + | |
661 | 664 |
| |
662 | 665 |
| |
663 | 666 |
| |
| |||
668 | 671 |
| |
669 | 672 |
| |
670 | 673 |
| |
| 674 | + | |
671 | 675 |
| |
672 | 676 |
| |
673 | 677 |
| |
| |||
678 | 682 |
| |
679 | 683 |
| |
680 | 684 |
| |
| 685 | + | |
681 | 686 |
| |
682 | 687 |
| |
683 | 688 |
| |
| |||
2091 | 2096 |
| |
2092 | 2097 |
| |
2093 | 2098 |
| |
2094 |
| - | |
| 2099 | + | |
| 2100 | + | |
2095 | 2101 |
| |
| 2102 | + | |
2096 | 2103 |
| |
2097 | 2104 |
| |
2098 | 2105 |
| |
| |||
2224 | 2231 |
| |
2225 | 2232 |
| |
2226 | 2233 |
| |
| 2234 | + | |
2227 | 2235 |
| |
2228 | 2236 |
| |
2229 | 2237 |
| |
|
0 commit comments
Comments
(0)