Skip to content

Commit 55b7e2f

Browse files
committed
Fix two bugs in MaintainOldSnapshotTimeMapping.
The previous coding was confused about whether head_timestamp was intended to represent the timestamp for the newest bucket in the mapping or the oldest timestamp for the oldest bucket in the mapping. Decide that it's intended to be the oldest one, and repair accordingly. To do that, we need to do two things. First, when advancing to a new bucket, don't categorically set head_timestamp to the new timestamp. Do this only if we're blowing out the map completely because a lot of time has passed since we last maintained it. If we're replacing entries one by one, advance head_timestamp by 1 minute for each; if we're filling in unused entries, don't advance head_timestamp at all. Second, fix the computation of how many buckets we need to advance. The previous formula would be correct if head_timestamp were the timestamp for the new bucket, but we're now making all the code agree that it's the timestamp for the oldest bucket, so adjust the formula accordingly. This is certainly a bug fix, but I don't feel good about back-patching it without the introspection tools added by commit aecf5ee, and perhaps also some actual tests. Since back-patching the introspection tools might not attract sufficient support and since there are no automated tests of these fixes yet, I'm just committing this to master for now. Patch by me, reviewed by Thomas Munro, Dilip Kumar, Hamid Akhtar. Discussion: http://postgr.es/m/CA+TgmoY=aqf0zjTD+3dUWYkgMiNDegDLFjo+6ze=Wtpik+3XqA@mail.gmail.com
1 parent c005eb0 commit 55b7e2f

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

src/backend/utils/time/snapmgr.c

+27-3
Original file line numberDiff line numberDiff line change
@@ -1949,17 +1949,40 @@ MaintainOldSnapshotTimeMapping(TimestampTz whenTaken, TransactionId xmin)
19491949
else
19501950
{
19511951
/* We need a new bucket, but it might not be the very next one. */
1952-
int advance = ((ts - oldSnapshotControl->head_timestamp)
1953-
/ USECS_PER_MINUTE);
1952+
int distance_to_new_tail;
1953+
int distance_to_current_tail;
1954+
int advance;
19541955

1955-
oldSnapshotControl->head_timestamp = ts;
1956+
/*
1957+
* Our goal is for the new "tail" of the mapping, that is, the entry
1958+
* which is newest and thus furthest from the "head" entry, to
1959+
* correspond to "ts". Since there's one entry per minute, the
1960+
* distance between the current head and the new tail is just the
1961+
* number of minutes of difference between ts and the current
1962+
* head_timestamp.
1963+
*
1964+
* The distance from the current head to the current tail is one
1965+
* less than the number of entries in the mapping, because the
1966+
* entry at the head_offset is for 0 minutes after head_timestamp.
1967+
*
1968+
* The difference between these two values is the number of minutes
1969+
* by which we need to advance the mapping, either adding new entries
1970+
* or rotating old ones out.
1971+
*/
1972+
distance_to_new_tail =
1973+
(ts - oldSnapshotControl->head_timestamp) / USECS_PER_MINUTE;
1974+
distance_to_current_tail =
1975+
oldSnapshotControl->count_used - 1;
1976+
advance = distance_to_new_tail - distance_to_current_tail;
1977+
Assert(advance > 0);
19561978

19571979
if (advance >= OLD_SNAPSHOT_TIME_MAP_ENTRIES)
19581980
{
19591981
/* Advance is so far that all old data is junk; start over. */
19601982
oldSnapshotControl->head_offset = 0;
19611983
oldSnapshotControl->count_used = 1;
19621984
oldSnapshotControl->xid_by_minute[0] = xmin;
1985+
oldSnapshotControl->head_timestamp = ts;
19631986
}
19641987
else
19651988
{
@@ -1978,6 +2001,7 @@ MaintainOldSnapshotTimeMapping(TimestampTz whenTaken, TransactionId xmin)
19782001
else
19792002
oldSnapshotControl->head_offset = old_head + 1;
19802003
oldSnapshotControl->xid_by_minute[old_head] = xmin;
2004+
oldSnapshotControl->head_timestamp += USECS_PER_MINUTE;
19812005
}
19822006
else
19832007
{

0 commit comments

Comments
 (0)