forked frompostgres/postgres
- Notifications
You must be signed in to change notification settings - Fork6
Commitbae868c
committed
Don't trust unvalidated xl_tot_len.
xl_tot_len comes first in a WAL record. Usually we don't trust it to bethe true length until we've validated the record header. If the recordheader was split across two pages, previously we wouldn't do thevalidation until after we'd already tried to allocate enough memory tohold the record, which was bad because it might actually be garbagebytes from a recycled WAL file, so we could try to allocate a lot ofmemory. Release 15 made it worse.Since70b4f82, we'd at least generate an end-of-WAL condition if thegarbage 4 byte value happened to be > 1GB, but we'd still try toallocate up to 1GB of memory bogusly otherwise. That was animprovement, but unfortunately release 15 tries to allocate anotherobject before that, so you could get a FATAL error and recovery couldfail.We can fix both variants of the problem more fundamentally usingpre-existing page-level validation, if we just re-order some logic.The new order of operations in the split-header case defers all memoryallocation based on xl_tot_len until we've read the following page. Atthat point we know that its first few bytes are not recycled data, bychecking its xlp_pageaddr, and that its xlp_rem_len agrees withxl_tot_len on the preceding page. That is strong evidence thatxl_tot_len was truly the start of a record that was logged.This problem was most likely to occur on a standby, becausewalreceiver.c recycles WAL files without zeroing out trailing regions ofeach page. We could fix that too, but it wouldn't protect us from rarecrash scenarios where the trailing zeroes don't make it to disk.With reliable xl_tot_len validation in place, the ancient policy ofconsidering malloc failure to indicate corruption at end-of-WAL seemsquite surprising, but changing that is left for later work.Also included is a new TAP test to exercise various cases of end-of-WALdetection by writing contrived data into the WAL from Perl.Back-patch to 12. We decided not to put this change into the finalrelease of 11.Author: Thomas Munro <thomas.munro@gmail.com>Author: Michael Paquier <michael@paquier.xyz>Reported-by: Alexander Lakhin <exclusion@gmail.com>Reviewed-by: Noah Misch <noah@leadboat.com> (the idea, not the code)Reviewed-by: Michael Paquier <michael@paquier.xyz>Reviewed-by: Sergei Kornilov <sk@zsrv.org>Reviewed-by: Alexander Lakhin <exclusion@gmail.com>Discussion:https://postgr.es/m/17928-aa92416a70ff44a2%40postgresql.org1 parent755eb44 commitbae868c
File tree
4 files changed
+569
-56
lines changed- src
- backend/access/transam
- test
- perl/PostgreSQL/Test
- recovery
- t
4 files changed
+569
-56
lines changedLines changed: 67 additions & 56 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
192 | 192 |
| |
193 | 193 |
| |
194 | 194 |
| |
| 195 | + | |
| 196 | + | |
| 197 | + | |
195 | 198 |
| |
196 | 199 |
| |
197 | 200 |
| |
| |||
201 | 204 |
| |
202 | 205 |
| |
203 | 206 |
| |
204 |
| - | |
205 |
| - | |
206 |
| - | |
207 |
| - | |
208 |
| - | |
209 |
| - | |
210 |
| - | |
211 |
| - | |
212 |
| - | |
213 |
| - | |
214 |
| - | |
215 |
| - | |
216 |
| - | |
217 |
| - | |
218 |
| - | |
219 |
| - | |
220 |
| - | |
221 |
| - | |
222 |
| - | |
223 | 207 |
| |
224 | 208 |
| |
225 | 209 |
| |
| |||
669 | 653 |
| |
670 | 654 |
| |
671 | 655 |
| |
672 |
| - | |
673 |
| - | |
674 |
| - | |
675 |
| - | |
676 |
| - | |
677 |
| - | |
678 |
| - | |
679 |
| - | |
680 |
| - | |
| 656 | + | |
681 | 657 |
| |
682 | 658 |
| |
683 | 659 |
| |
684 | 660 |
| |
685 |
| - | |
686 |
| - | |
687 |
| - | |
688 |
| - | |
689 |
| - | |
| 661 | + | |
| 662 | + | |
| 663 | + | |
690 | 664 |
| |
691 | 665 |
| |
692 | 666 |
| |
693 |
| - | |
694 |
| - | |
| 667 | + | |
| 668 | + | |
695 | 669 |
| |
696 | 670 |
| |
697 |
| - | |
698 |
| - | |
| 671 | + | |
| 672 | + | |
| 673 | + | |
699 | 674 |
| |
700 |
| - | |
701 |
| - | |
702 |
| - | |
703 |
| - | |
704 |
| - | |
705 |
| - | |
706 |
| - | |
| 675 | + | |
707 | 676 |
| |
708 | 677 |
| |
709 | 678 |
| |
| |||
718 | 687 |
| |
719 | 688 |
| |
720 | 689 |
| |
721 |
| - | |
| 690 | + | |
| 691 | + | |
722 | 692 |
| |
723 |
| - | |
724 |
| - | |
725 |
| - | |
726 |
| - | |
727 |
| - | |
728 |
| - | |
729 |
| - | |
730 |
| - | |
| 693 | + | |
| 694 | + | |
731 | 695 |
| |
732 | 696 |
| |
733 | 697 |
| |
| |||
824 | 788 |
| |
825 | 789 |
| |
826 | 790 |
| |
827 |
| - | |
828 | 791 |
| |
| 792 | + | |
| 793 | + | |
| 794 | + | |
| 795 | + | |
| 796 | + | |
| 797 | + | |
| 798 | + | |
| 799 | + | |
| 800 | + | |
| 801 | + | |
| 802 | + | |
| 803 | + | |
| 804 | + | |
| 805 | + | |
| 806 | + | |
| 807 | + | |
| 808 | + | |
| 809 | + | |
| 810 | + | |
| 811 | + | |
| 812 | + | |
| 813 | + | |
| 814 | + | |
| 815 | + | |
| 816 | + | |
| 817 | + | |
| 818 | + | |
| 819 | + | |
829 | 820 |
| |
830 | 821 |
| |
831 | 822 |
| |
| |||
867 | 858 |
| |
868 | 859 |
| |
869 | 860 |
| |
| 861 | + | |
| 862 | + | |
| 863 | + | |
| 864 | + | |
| 865 | + | |
| 866 | + | |
| 867 | + | |
| 868 | + | |
| 869 | + | |
| 870 | + | |
| 871 | + | |
| 872 | + | |
| 873 | + | |
| 874 | + | |
| 875 | + | |
| 876 | + | |
| 877 | + | |
| 878 | + | |
| 879 | + | |
| 880 | + | |
| 881 | + | |
| 882 | + | |
870 | 883 |
| |
871 | 884 |
| |
872 | 885 |
| |
| |||
895 | 908 |
| |
896 | 909 |
| |
897 | 910 |
| |
898 |
| - | |
899 |
| - | |
900 | 911 |
| |
901 | 912 |
| |
902 | 913 |
| |
|
Lines changed: 41 additions & 0 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
71 | 71 |
| |
72 | 72 |
| |
73 | 73 |
| |
| 74 | + | |
74 | 75 |
| |
75 | 76 |
| |
76 | 77 |
| |
| |||
702 | 703 |
| |
703 | 704 |
| |
704 | 705 |
| |
| 706 | + | |
| 707 | + | |
| 708 | + | |
| 709 | + | |
| 710 | + | |
| 711 | + | |
| 712 | + | |
| 713 | + | |
| 714 | + | |
| 715 | + | |
| 716 | + | |
| 717 | + | |
| 718 | + | |
| 719 | + | |
| 720 | + | |
| 721 | + | |
| 722 | + | |
| 723 | + | |
| 724 | + | |
| 725 | + | |
| 726 | + | |
| 727 | + | |
| 728 | + | |
| 729 | + | |
| 730 | + | |
| 731 | + | |
| 732 | + | |
| 733 | + | |
| 734 | + | |
| 735 | + | |
| 736 | + | |
| 737 | + | |
| 738 | + | |
| 739 | + | |
| 740 | + | |
| 741 | + | |
| 742 | + | |
| 743 | + | |
| 744 | + | |
| 745 | + | |
705 | 746 |
| |
706 | 747 |
| |
707 | 748 |
| |
|
Lines changed: 1 addition & 0 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
44 | 44 |
| |
45 | 45 |
| |
46 | 46 |
| |
| 47 | + | |
47 | 48 |
| |
48 | 49 |
| |
49 | 50 |
|
0 commit comments
Comments
(0)