Skip to content

Commit

Permalink
Merge pull request #1218 from authzed/append-fix
Browse files Browse the repository at this point in the history
datastore/postgres: fix invalid slice appends in snapshots
  • Loading branch information
jakedt authored Mar 21, 2023
2 parents 03896f6 + ae4245a commit d7dbd20
Showing 1 changed file with 23 additions and 17 deletions.
40 changes: 23 additions & 17 deletions internal/datastore/postgres/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,21 +123,8 @@ func (s pgSnapshot) compare(rhs pgSnapshot) comparisonResult {
return uncomparable
}

var rhsHasMoreInfo bool
for _, txid := range append(s.xipList, s.xmax) {
if rhs.txVisible(txid) {
rhsHasMoreInfo = true
break
}
}

var lhsHasMoreInfo bool
for _, txid := range append(rhs.xipList, rhs.xmax) {
if s.txVisible(txid) {
lhsHasMoreInfo = true
break
}
}
rhsHasMoreInfo := rhs.anyTXVisible(s.xmax, s.xipList)
lhsHasMoreInfo := s.anyTXVisible(rhs.xmax, rhs.xipList)

switch {
case rhsHasMoreInfo && lhsHasMoreInfo:
Expand All @@ -151,6 +138,19 @@ func (s pgSnapshot) compare(rhs pgSnapshot) comparisonResult {
}
}

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.
Expand All @@ -160,10 +160,13 @@ func (s pgSnapshot) markComplete(txid uint64) pgSnapshot {
return s
}

xipListCopy := make([]uint64, len(s.xipList))
copy(xipListCopy, s.xipList)

newSnapshot := pgSnapshot{
s.xmin,
s.xmax,
s.xipList,
xipListCopy,
pgtype.Present,
}

Expand Down Expand Up @@ -202,10 +205,13 @@ func (s pgSnapshot) markInProgress(txid uint64) pgSnapshot {
return s
}

xipListCopy := make([]uint64, len(s.xipList))
copy(xipListCopy, s.xipList)

newSnapshot := pgSnapshot{
s.xmin,
s.xmax,
s.xipList,
xipListCopy,
pgtype.Present,
}

Expand Down

0 comments on commit d7dbd20

Please sign in to comment.