forked frompostgres/postgres
- Notifications
You must be signed in to change notification settings - Fork6
Commit1afe31f
committed
Preserve CurrentMemoryContext across Start/CommitTransactionCommand.
Up to now, committing a transaction has caused CurrentMemoryContext toget set to TopMemoryContext. Most callers did not pay any particularheed to this, which is problematic because TopMemoryContext is along-lived context that never gets reset. If the caller assumes itcan leak memory because it's running in a limited-lifespan context,that behavior translates into a session-lifespan memory leak.The first-reported instance of this involved ProcessIncomingNotify,which is called from the main processing loop that normally runs inMessageContext. That outer-loop code assumes that whatever itallocates will be cleaned up when we're done processing the currentclient message --- but if we service a notify interrupt, then whatevergets allocated before the next switch to MessageContext will bepermanently leaked in TopMemoryContext. sinval catchup interruptshave a similar problem, and I strongly suspect that some places inlogical replication do too.To fix this in a generic way, let's redefine the behavior as"CommitTransactionCommand restores the memory context that was currentat entry to StartTransactionCommand". This clearly fixes the issuefor the notify and sinval cases, and it seems to match the mentalmodel that's in use in the logical replication code, to the extentthat anybody thought about it there at all.For consistency, likewise make subtransaction exit restore the contextthat was current at subtransaction start (rather than always selectingthe CurTransactionContext of the parent transaction level). This casehas less risk of resulting in a permanent leak than the outer-levelbehavior has, but it would not meet the principle of least surprisefor some CommitTransactionCommand calls to restore the previouscontext while others don't.While we're here, also change xact.c so that we resetTopTransactionContext at transaction exit and then re-use it in latertransactions, rather than dropping and recreating it in each cycle.This probably doesn't save a lot given the context recycling mechanismin aset.c, but it should save a little bit. Per suggestion from DavidRowley. (Parenthetically, the text in src/backend/utils/mmgr/READMEimplies that this is how I'd planned to implement it as far back ascommit1aebc36 --- but the code actually added in that commit justdrops and recreates it each time.)This commit also cleans up a few places outside xact.c that wereneedlessly making TopMemoryContext current, and thus risking moreleaks of the same kind. I don't think any of them representsignificant leak risks today, but let's deal with them while theissue is top-of-mind.Per bug #18512 from wizardbrony. Commit to HEAD only, as this changeseems to have some risk of breaking things for some callers. We'llapply a narrower fix for the known-broken cases in the back branches.Discussion:https://postgr.es/m/3478884.1718656625@sss.pgh.pa.us1 parent6e16b1e commit1afe31f
File tree
5 files changed
+80
-40
lines changed- src/backend
- access/transam
- replication
- tcop
- utils/mmgr
5 files changed
+80
-40
lines changedLines changed: 72 additions & 29 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
200 | 200 |
| |
201 | 201 |
| |
202 | 202 |
| |
| 203 | + | |
203 | 204 |
| |
204 | 205 |
| |
205 | 206 |
| |
| |||
1174 | 1175 |
| |
1175 | 1176 |
| |
1176 | 1177 |
| |
| 1178 | + | |
| 1179 | + | |
| 1180 | + | |
| 1181 | + | |
| 1182 | + | |
1177 | 1183 |
| |
1178 | 1184 |
| |
1179 | 1185 |
| |
| |||
1190 | 1196 |
| |
1191 | 1197 |
| |
1192 | 1198 |
| |
1193 |
| - | |
1194 |
| - | |
1195 |
| - | |
1196 |
| - | |
1197 |
| - | |
1198 |
| - | |
| 1199 | + | |
| 1200 | + | |
| 1201 | + | |
1199 | 1202 |
| |
1200 |
| - | |
1201 |
| - | |
1202 |
| - | |
1203 |
| - | |
| 1203 | + | |
| 1204 | + | |
| 1205 | + | |
| 1206 | + | |
| 1207 | + | |
1204 | 1208 |
| |
1205 | 1209 |
| |
1206 | 1210 |
| |
| |||
1251 | 1255 |
| |
1252 | 1256 |
| |
1253 | 1257 |
| |
| 1258 | + | |
| 1259 | + | |
| 1260 | + | |
| 1261 | + | |
| 1262 | + | |
1254 | 1263 |
| |
1255 | 1264 |
| |
1256 | 1265 |
| |
| |||
1576 | 1585 |
| |
1577 | 1586 |
| |
1578 | 1587 |
| |
| 1588 | + | |
| 1589 | + | |
1579 | 1590 |
| |
1580 |
| - | |
1581 |
| - | |
| 1591 | + | |
| 1592 | + | |
| 1593 | + | |
| 1594 | + | |
1582 | 1595 |
| |
1583 |
| - | |
| 1596 | + | |
1584 | 1597 |
| |
1585 | 1598 |
| |
1586 |
| - | |
| 1599 | + | |
| 1600 | + | |
1587 | 1601 |
| |
1588 | 1602 |
| |
1589 |
| - | |
1590 |
| - | |
| 1603 | + | |
| 1604 | + | |
| 1605 | + | |
| 1606 | + | |
| 1607 | + | |
| 1608 | + | |
| 1609 | + | |
1591 | 1610 |
| |
1592 |
| - | |
| 1611 | + | |
1593 | 1612 |
| |
1594 | 1613 |
| |
1595 | 1614 |
| |
| |||
1942 | 1961 |
| |
1943 | 1962 |
| |
1944 | 1963 |
| |
1945 |
| - | |
| 1964 | + | |
| 1965 | + | |
| 1966 | + | |
| 1967 | + | |
1946 | 1968 |
| |
1947 | 1969 |
| |
1948 |
| - | |
1949 |
| - | |
| 1970 | + | |
| 1971 | + | |
| 1972 | + | |
| 1973 | + | |
1950 | 1974 |
| |
1951 |
| - | |
| 1975 | + | |
1952 | 1976 |
| |
1953 | 1977 |
| |
1954 | 1978 |
| |
| |||
1957 | 1981 |
| |
1958 | 1982 |
| |
1959 | 1983 |
| |
1960 |
| - | |
| 1984 | + | |
| 1985 | + | |
| 1986 | + | |
1961 | 1987 |
| |
1962 | 1988 |
| |
1963 |
| - | |
1964 |
| - | |
| 1989 | + | |
| 1990 | + | |
| 1991 | + | |
| 1992 | + | |
| 1993 | + | |
| 1994 | + | |
| 1995 | + | |
1965 | 1996 |
| |
1966 |
| - | |
| 1997 | + | |
1967 | 1998 |
| |
1968 | 1999 |
| |
1969 | 2000 |
| |
| |||
1982 | 2013 |
| |
1983 | 2014 |
| |
1984 | 2015 |
| |
1985 |
| - | |
1986 |
| - | |
| 2016 | + | |
| 2017 | + | |
| 2018 | + | |
| 2019 | + | |
| 2020 | + | |
| 2021 | + | |
| 2022 | + | |
| 2023 | + | |
| 2024 | + | |
1987 | 2025 |
| |
1988 | 2026 |
| |
1989 | 2027 |
| |
| |||
4904 | 4942 |
| |
4905 | 4943 |
| |
4906 | 4944 |
| |
4907 |
| - | |
4908 |
| - | |
| 4945 | + | |
| 4946 | + | |
| 4947 | + | |
| 4948 | + | |
| 4949 | + | |
| 4950 | + | |
| 4951 | + | |
4909 | 4952 |
| |
4910 | 4953 |
| |
4911 | 4954 |
| |
|
Lines changed: 2 additions & 4 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
440 | 440 |
| |
441 | 441 |
| |
442 | 442 |
| |
443 |
| - | |
444 |
| - | |
445 | 443 |
| |
| 444 | + | |
| 445 | + | |
446 | 446 |
| |
447 |
| - | |
448 |
| - | |
449 | 447 |
| |
450 | 448 |
| |
451 | 449 |
| |
|
Lines changed: 3 additions & 5 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
196 | 196 |
| |
197 | 197 |
| |
198 | 198 |
| |
199 |
| - | |
200 |
| - | |
201 |
| - | |
| 199 | + | |
| 200 | + | |
202 | 201 |
| |
203 | 202 |
| |
204 | 203 |
| |
| |||
230 | 229 |
| |
231 | 230 |
| |
232 | 231 |
| |
233 |
| - | |
| 232 | + | |
234 | 233 |
| |
235 |
| - | |
236 | 234 |
| |
237 | 235 |
| |
238 | 236 |
| |
|
Lines changed: 1 addition & 1 deletion
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
4418 | 4418 |
| |
4419 | 4419 |
| |
4420 | 4420 |
| |
4421 |
| - | |
| 4421 | + | |
4422 | 4422 |
| |
4423 | 4423 |
| |
4424 | 4424 |
| |
|
Lines changed: 2 additions & 1 deletion
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
1148 | 1148 |
| |
1149 | 1149 |
| |
1150 | 1150 |
| |
1151 |
| - | |
| 1151 | + | |
| 1152 | + | |
1152 | 1153 |
| |
1153 | 1154 |
| |
1154 | 1155 |
| |
|
0 commit comments
Comments
(0)