-
Notifications
You must be signed in to change notification settings - Fork 247
/
snapshot.go
287 lines (242 loc) · 7.09 KB
/
snapshot.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
package postgres
import (
"fmt"
"slices"
"strconv"
"strings"
"github.com/jackc/pgx/v5/pgtype"
)
// RegisterTypes registers pgSnapshot and xid8 with a pgtype.ConnInfo.
func RegisterTypes(m *pgtype.Map) {
m.RegisterType(&pgtype.Type{
Name: "snapshot",
OID: 5038,
Codec: SnapshotCodec{},
})
m.RegisterType(&pgtype.Type{
Name: "xid",
OID: 5069,
Codec: Uint64Codec{},
})
m.RegisterDefaultPgType(pgSnapshot{}, "snapshot")
m.RegisterDefaultPgType(xid8{}, "xid")
}
type SnapshotCodec struct {
pgtype.TextCodec
}
func (SnapshotCodec) DecodeValue(tm *pgtype.Map, oid uint32, format int16, src []byte) (interface{}, error) {
if src == nil {
return nil, nil
}
var target pgSnapshot
scanPlan := tm.PlanScan(oid, format, &target)
if scanPlan == nil {
return nil, fmt.Errorf("PlanScan did not find a plan")
}
err := scanPlan.Scan(src, &target)
if err != nil {
return nil, err
}
return target, nil
}
type pgSnapshot struct {
xmin, xmax uint64
xipList []uint64 // Must always be sorted
}
var (
_ pgtype.TextScanner = &pgSnapshot{}
_ pgtype.TextValuer = &pgSnapshot{}
)
func (s *pgSnapshot) ScanText(v pgtype.Text) error {
if !v.Valid {
return fmt.Errorf("cannot scan NULL into pgSnapshot")
}
components := strings.SplitN(v.String, ":", 3)
if len(components) != 3 {
return fmt.Errorf("wrong number of snapshot components: %s", v.String)
}
var err error
s.xmin, err = strconv.ParseUint(components[0], 10, 64)
if err != nil {
return fmt.Errorf("unable to parse xmin: %s", components[0])
}
s.xmax, err = strconv.ParseUint(components[1], 10, 64)
if err != nil {
return fmt.Errorf("unable to parse xmax: %s", components[1])
}
if components[2] != "" {
xipStrings := strings.Split(components[2], ",")
s.xipList = make([]uint64, len(xipStrings))
for i, xipStr := range xipStrings {
s.xipList[i], err = strconv.ParseUint(xipStr, 10, 64)
if err != nil {
return fmt.Errorf("unable to parse xip: %s", xipStr)
}
}
// Do a defensive sort in case the server is feeling out of sorts.
slices.Sort(s.xipList)
} else {
s.xipList = nil
}
return nil
}
func (s pgSnapshot) TextValue() (pgtype.Text, error) {
return pgtype.Text{String: s.String(), Valid: true}, nil
}
// String uses the official postgres encoding for snapshots, which is described here:
// https://www.postgresql.org/docs/current/functions-info.html#FUNCTIONS-PG-SNAPSHOT-PARTS
func (s pgSnapshot) String() string {
xipStrs := make([]string, len(s.xipList))
for i, xip := range s.xipList {
xipStrs[i] = strconv.FormatUint(xip, 10)
}
components := []string{
strconv.FormatUint(s.xmin, 10),
strconv.FormatUint(s.xmax, 10),
strings.Join(xipStrs, ","),
}
return strings.Join(components, ":")
}
func (s pgSnapshot) Equal(rhs pgSnapshot) bool {
return s.compare(rhs) == equal
}
func (s pgSnapshot) GreaterThan(rhs pgSnapshot) bool {
return s.compare(rhs) == gt
}
func (s pgSnapshot) LessThan(rhs pgSnapshot) bool {
return s.compare(rhs) == lt
}
type comparisonResult uint8
const (
_ comparisonResult = iota
equal
lt
gt
concurrent
)
// compare will return whether we can definitely assert that one snapshot was
// definitively created after, before, at the same time, or was executed
// concurrent with another transaction. We assess this based on whether a
// transaction has more, less, or conflicting information about the resolution
// of in-progress transactions. E.g. if one snapshot only sees txids 1 and 3 as
// visible but another transaction sees 1-3 as visible, that transaction is
// greater.
func (s pgSnapshot) compare(rhs pgSnapshot) comparisonResult {
rhsHasMoreInfo := rhs.anyTXVisible(s.xmax, s.xipList)
lhsHasMoreInfo := s.anyTXVisible(rhs.xmax, rhs.xipList)
switch {
case rhsHasMoreInfo && lhsHasMoreInfo:
return concurrent
case rhsHasMoreInfo:
return lt
case lhsHasMoreInfo:
return gt
default:
return equal
}
}
func (s pgSnapshot) anyTXVisible(first uint64, others []uint64) bool {
if s.txVisible(first) {
return true
}
for _, txid := range others {
if s.txVisible(txid) {
return true
}
}
return false
}
// markComplete will create a new snapshot where the specified transaction will be marked as
// complete and visible. For example, if txid was present in the xip list of this snapshot
// it will be removed and the xmin and xmax will be adjusted accordingly.
func (s pgSnapshot) markComplete(txid uint64) pgSnapshot {
if txid < s.xmin {
// Nothing to do
return s
}
xipListCopy := make([]uint64, len(s.xipList))
copy(xipListCopy, s.xipList)
newSnapshot := pgSnapshot{
s.xmin,
s.xmax,
xipListCopy,
}
// Adjust the xmax and running tx if necessary
if txid >= s.xmax {
for newIP := s.xmax; newIP < txid+1; newIP++ {
newSnapshot.xipList = append(newSnapshot.xipList, newIP)
}
newSnapshot.xmax = txid + 1
}
// Mark the tx complete if it's in the xipList
// Note: we only find the first if it was erroneously duplicate
pos, found := slices.BinarySearch(newSnapshot.xipList, txid)
if found {
newSnapshot.xipList = slices.Delete(newSnapshot.xipList, pos, pos+1)
}
// Adjust the xmin if necessary
if len(newSnapshot.xipList) > 0 {
newSnapshot.xmin = newSnapshot.xipList[0]
} else {
newSnapshot.xmin = newSnapshot.xmax
newSnapshot.xipList = nil
}
return newSnapshot
}
// markInProgress will create a new snapshot where the specified transaction will be marked as
// in-progress and therefore invisible. For example, if the specified xmin falls between two
// values in the xip list, it will be inserted in order.
func (s pgSnapshot) markInProgress(txid uint64) pgSnapshot {
if txid >= s.xmax {
// Nothing to do
return s
}
xipListCopy := make([]uint64, len(s.xipList))
copy(xipListCopy, s.xipList)
newSnapshot := pgSnapshot{
s.xmin,
s.xmax,
xipListCopy,
}
// Adjust the xmax and running tx if necessary
if txid < s.xmin {
// Adjust the xmin and prepend the newly running tx
newSnapshot.xmin = txid
newSnapshot.xipList = append([]uint64{txid}, newSnapshot.xipList...)
} else {
// Add the newly in-progress xip to the list of in-progress transactions
pos, found := slices.BinarySearch(newSnapshot.xipList, txid)
if !found {
newSnapshot.xipList = slices.Insert(newSnapshot.xipList, pos, txid)
}
}
// Adjust the xmax if necessary
var numToDrop int
startingXipLen := len(newSnapshot.xipList)
for numToDrop = 0; numToDrop < startingXipLen; numToDrop++ {
if newSnapshot.xipList[startingXipLen-1-numToDrop] != newSnapshot.xmax-uint64(numToDrop)-1 {
break
}
}
if numToDrop > 0 {
newSnapshot.xmax = newSnapshot.xipList[startingXipLen-numToDrop]
newSnapshot.xipList = newSnapshot.xipList[:startingXipLen-numToDrop]
if len(newSnapshot.xipList) == 0 {
newSnapshot.xipList = nil
}
}
return newSnapshot
}
// txVisible will return whether the specified txid has a disposition (i.e. committed or rolled back)
// in the specified snapshot, and is therefore txVisible to transactions using this snapshot.
func (s pgSnapshot) txVisible(txid uint64) bool {
switch {
case txid < s.xmin:
return true
case txid >= s.xmax:
return false
default:
_, txInProgress := slices.BinarySearch(s.xipList, txid)
return !txInProgress
}
}