forked frompostgres/postgres
- Notifications
You must be signed in to change notification settings - Fork6
Commit18b87b2
committed
Fix possible HOT corruption when RECENTLY_DEAD changes to DEAD while pruning.
Sincedc7420c the horizon used for pruning is determined "lazily". A moreaccurate horizon is built on-demand, rather than in GetSnapshotData(). If ahorizon computation is triggered between two HeapTupleSatisfiesVacuum() callsfor the same tuple, the result can change from RECENTLY_DEAD to DEAD.heap_page_prune() can process the same tid multiple times (once following anupdate chain, once "directly"). When the result of HeapTupleSatisfiesVacuum()of a tuple changes from RECENTLY_DEAD during the first access, to DEAD in thesecond, the "tuple is DEAD and doesn't chain to anything else" path inheap_prune_chain() can end up marking the target of a LP_REDIRECT ItemIdunused.Initially not easily visible,Once the target of a LP_REDIRECT ItemId is marked unused, a new tuple versioncan reuse it. At that point the corruption may become visible, as indexentries pointing to the "original" redirect item, now point to a unrelatedtuple.To fix, compute HTSV for all tuples on a page only once. This fixes the entireclass of problems of HTSV changing inside heap_page_prune(). However,visibility changes can obviously still occur between HTSV checks insideheap_page_prune() and outside (e.g. in lazy_scan_prune()).The computation of HTSV is now done in bulk, in heap_page_prune(), rather thanon-demand in heap_prune_chain(). Besides being a bit simpler, it also isfaster: Memory accesses can happen sequentially, rather than in the order ofHOT chains.There are other causes of HeapTupleSatisfiesVacuum() results changing betweentwo visibility checks for the same tuple, even beforedc7420c. E.g.HEAPTUPLE_INSERT_IN_PROGRESS can change to HEAPTUPLE_DEAD when a transactionaborts between the two checks. None of the these other visibility statuschanges are known to cause corruption, but heap_page_prune()'s approach makesit hard to be confident.A patch implementing a more fundamental redesign of heap_page_prune(), whichfixes this bug and simplifies pruning substantially, has been proposed byPeter Geoghegan inhttps://postgr.es/m/CAH2-WzmNk6V6tqzuuabxoxM8HJRaWU6h12toaS-bqYcLiht16A@mail.gmail.comHowever, that redesign is larger change than desirable for backpatching. Asthe new design still benefits from the batched visibility determinationintroduced in this commit, it makes sense to commit this narrower fix to 14and master, and then commit Peter's improvement in master.The precise sequence required to trigger the bug is complicated and hard to doexercise in an isolation test (until we have wait points). Due to that theisolation test initially posted athttps://postgr.es/m/20211119003623.d3jusiytzjqwb62p%40alap3.anarazel.deand updated inhttps://postgr.es/m/20211122175914.ayk6gg6nvdwuhrzb%40alap3.anarazel.deisn't committable.A followup commit will introduce additional assertions, to detect problemslike this more easily.Bug: #17255Reported-By: Alexander Lakhin <exclusion@gmail.com>Debugged-By: Andres Freund <andres@anarazel.de>Debugged-By: Peter Geoghegan <pg@bowt.ie>Author: Andres Freund <andres@andres@anarazel.de>Reviewed-By: Peter Geoghegan <pg@bowt.ie>Discussion:https://postgr.es/m/20211122175914.ayk6gg6nvdwuhrzb@alap3.anarazel.deBackpatch: 14-, the oldest branch containingdc7420c1 parent43c2175 commit18b87b2
1 file changed
+82
-26
lines changedLines changed: 82 additions & 26 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
56 | 56 |
| |
57 | 57 |
| |
58 | 58 |
| |
59 |
| - | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
60 | 66 |
| |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
61 | 77 |
| |
62 | 78 |
| |
63 | 79 |
| |
| 80 | + | |
| 81 | + | |
| 82 | + | |
64 | 83 |
| |
65 | 84 |
| |
66 | 85 |
| |
| |||
252 | 271 |
| |
253 | 272 |
| |
254 | 273 |
| |
| 274 | + | |
255 | 275 |
| |
256 | 276 |
| |
257 | 277 |
| |
| |||
274 | 294 |
| |
275 | 295 |
| |
276 | 296 |
| |
277 |
| - | |
278 | 297 |
| |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
279 | 351 |
| |
280 | 352 |
| |
281 | 353 |
| |
| |||
286 | 358 |
| |
287 | 359 |
| |
288 | 360 |
| |
289 |
| - | |
290 |
| - | |
291 |
| - | |
292 |
| - | |
| 361 | + | |
293 | 362 |
| |
294 | 363 |
| |
295 | 364 |
| |
| |||
491 | 560 |
| |
492 | 561 |
| |
493 | 562 |
| |
494 |
| - | |
| 563 | + | |
495 | 564 |
| |
496 | 565 |
| |
497 | 566 |
| |
| |||
505 | 574 |
| |
506 | 575 |
| |
507 | 576 |
| |
508 |
| - | |
509 |
| - | |
510 |
| - | |
511 |
| - | |
512 |
| - | |
| 577 | + | |
513 | 578 |
| |
514 | 579 |
| |
515 | 580 |
| |
| |||
531 | 596 |
| |
532 | 597 |
| |
533 | 598 |
| |
534 |
| - | |
535 |
| - | |
536 |
| - | |
537 | 599 |
| |
538 | 600 |
| |
539 | 601 |
| |
| |||
542 | 604 |
| |
543 | 605 |
| |
544 | 606 |
| |
| 607 | + | |
545 | 608 |
| |
546 | 609 |
| |
547 |
| - | |
548 |
| - | |
549 |
| - | |
550 |
| - | |
551 | 610 |
| |
552 | 611 |
| |
553 | 612 |
| |
| |||
568 | 627 |
| |
569 | 628 |
| |
570 | 629 |
| |
571 |
| - | |
572 |
| - | |
| 630 | + | |
| 631 | + | |
573 | 632 |
| |
574 | 633 |
| |
575 | 634 |
| |
| |||
636 | 695 |
| |
637 | 696 |
| |
638 | 697 |
| |
| 698 | + | |
639 | 699 |
| |
640 | 700 |
| |
641 |
| - | |
642 |
| - | |
643 |
| - | |
644 |
| - | |
645 | 701 |
| |
646 | 702 |
| |
647 | 703 |
| |
| |||
659 | 715 |
| |
660 | 716 |
| |
661 | 717 |
| |
662 |
| - | |
| 718 | + | |
663 | 719 |
| |
664 | 720 |
| |
665 | 721 |
| |
|
0 commit comments
Comments
(0)