forked frompostgres/postgres
- Notifications
You must be signed in to change notification settings - Fork6
Commit1d6a03e
committed
Fix race conditions with drop of reused pgstats entries
This fixes a set of race conditions with cumulative statistics where ashared stats entry could be dropped while it should still be valid inthe event when it is reused: an entry may refer to a different objectbut requires the same hash key. This can happen with various statskinds, like:- Replication slots that compute internally an index number, fordifferent slot names.- Stats kinds that use an OID in the object key, where a wraparoundcauses the same key to be used if an OID is used for the same object.- As of PostgreSQL 18, custom pgstats kinds could also be an issue,depending on their implementation.This issue is fixed by introducing a counter called "generation" in theshared entries via PgStatShared_HashEntry, initialized at 0 when anentry is created and incremented when the same entry is reused, to avoidconcurrent issues on drop because of other backends still holding areference to it. This "generation" is copied to the local copy that abackend holds when looking at an object, then cross-checked with theshared entry to make sure that the entry is not dropped even if its"refcount" justifies that if it has been reused.This problem could show up when a backend shuts down and needs todiscard any entries it still holds, causing statistics to be removedwhen they should not, or even an assertion failure. Another reportinvolved a failure in a standby after an OID wraparound, where thestartup process would FATAL on a "can only drop stats once", stoppingrecovery abruptly. The buildfarm has been sporadically complainingabout the problem, as well, but the window is hard to reach with thein-core tests.Note that the issue can be reproduced easily by adding a sleep beforedshash_find() in pgstat_release_entry_ref() to enlarge the problematicwindow while repeating test_decoding's isolation test oldest_xmin acouple of times, for example, as pointed out by Alexander Lakhin.Reported-by: Alexander Lakhin, Peter SmithAuthor: Kyotaro Horiguchi, Michael PaquierReviewed-by: Bertrand DrouvotDiscussion:https://postgr.es/m/CAA4eK1KxuMVyAryz_Vk5yq3ejgKYcL6F45Hj9ZnMNBS-g+PuZg@mail.gmail.comDiscussion:https://postgr.es/m/17947-b9554521ad963c9c@postgresql.orgBackpatch-through: 151 parent73731b2 commit1d6a03e
File tree
2 files changed
+54
-5
lines changed- src
- backend/utils/activity
- include/utils
2 files changed
+54
-5
lines changedLines changed: 35 additions & 5 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
278 | 278 |
| |
279 | 279 |
| |
280 | 280 |
| |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
281 | 286 |
| |
282 | 287 |
| |
283 | 288 |
| |
| |||
301 | 306 |
| |
302 | 307 |
| |
303 | 308 |
| |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
304 | 315 |
| |
305 | 316 |
| |
306 | 317 |
| |
| |||
341 | 352 |
| |
342 | 353 |
| |
343 | 354 |
| |
| 355 | + | |
344 | 356 |
| |
345 | 357 |
| |
346 | 358 |
| |
| |||
506 | 518 |
| |
507 | 519 |
| |
508 | 520 |
| |
509 |
| - | |
| 521 | + | |
| 522 | + | |
510 | 523 |
| |
511 | 524 |
| |
512 | 525 |
| |
| |||
573 | 586 |
| |
574 | 587 |
| |
575 | 588 |
| |
576 |
| - | |
577 |
| - | |
578 |
| - | |
579 |
| - | |
| 589 | + | |
| 590 | + | |
| 591 | + | |
| 592 | + | |
| 593 | + | |
| 594 | + | |
| 595 | + | |
| 596 | + | |
| 597 | + | |
| 598 | + | |
| 599 | + | |
| 600 | + | |
| 601 | + | |
| 602 | + | |
| 603 | + | |
| 604 | + | |
| 605 | + | |
| 606 | + | |
| 607 | + | |
| 608 | + | |
| 609 | + | |
580 | 610 |
| |
581 | 611 |
| |
582 | 612 |
| |
|
Lines changed: 19 additions & 0 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
93 | 93 |
| |
94 | 94 |
| |
95 | 95 |
| |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
96 | 109 |
| |
97 | 110 |
| |
98 | 111 |
| |
| |||
132 | 145 |
| |
133 | 146 |
| |
134 | 147 |
| |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
135 | 154 |
| |
136 | 155 |
| |
137 | 156 |
| |
|
0 commit comments
Comments
(0)