Skip to content

Commit 2970dcb

Browse files
authored
Ensure txn_participation is pruned corresponding with rows in txn. (#1685)
1 parent adb26b8 commit 2970dcb

3 files changed

Lines changed: 38 additions & 4 deletions

File tree

.golangci.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@ linters:
1212
exclusions:
1313
generated: lax
1414
rules:
15-
# revive: types is an acceptable package name in this codebase
15+
# revive: types and util are acceptable package names in this codebase
1616
- linters:
1717
- revive
18-
path: types/
18+
paths:
19+
- types/
20+
- util/
1921
text: 'var-naming: avoid meaningless package names'
2022
# ignore govet false positive fixed in https://github.com/golang/go/issues/45043
2123
- path: (.+)\.go$

idb/postgres/postgres.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2919,12 +2919,23 @@ func (db *IndexerDb) DeleteTransactions(ctx context.Context, keep uint64) error
29192919
// delete old transactions and update metastate
29202920
deleteTxns := func(tx pgx.Tx) error {
29212921
db.log.Infof("deleteTxns(): removing transactions before round %d", keep)
2922-
// delete query
2922+
2923+
// delete from txn
29232924
query := "DELETE FROM txn WHERE round < $1"
29242925
cmd, err2 := tx.Exec(ctx, query, keep)
29252926
if err2 != nil {
29262927
return fmt.Errorf("deleteTxns(): transaction delete err %w", err2)
29272928
}
2929+
db.log.Infof("%d transactions deleted", cmd.RowsAffected())
2930+
2931+
// delete from txn_participation
2932+
participationQuery := "DELETE FROM txn_participation WHERE round < $1"
2933+
participationCmd, err2 := tx.Exec(ctx, participationQuery, keep)
2934+
if err2 != nil {
2935+
return fmt.Errorf("deleteTxns(): txn_participation delete err %w", err2)
2936+
}
2937+
db.log.Infof("%d txn_participation records deleted", participationCmd.RowsAffected())
2938+
29282939
t := time.Now().UTC()
29292940
// update metastate
29302941
status := types.DeleteStatus{
@@ -2936,7 +2947,7 @@ func (db *IndexerDb) DeleteTransactions(ctx context.Context, keep uint64) error
29362947
if err2 != nil {
29372948
return fmt.Errorf("deleteTxns(): metastate update err %w", err2)
29382949
}
2939-
db.log.Infof("%d transactions deleted, last pruned at %s", cmd.RowsAffected(), status.LastPruned)
2950+
db.log.Infof("last pruned at %s", status.LastPruned)
29402951
return nil
29412952
}
29422953
err := db.txWithRetry(serializable, deleteTxns)

idb/postgres/postgres_integration_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2206,6 +2206,17 @@ func TestDeleteTransactions(t *testing.T) {
22062206
err := db.DeleteTransactions(context.Background(), 2)
22072207
assert.NoError(t, err)
22082208

2209+
// verify txn_participation only has records for rounds >= 2
2210+
var minRound uint64
2211+
err = db.db.QueryRow(context.Background(), "SELECT MIN(round) FROM txn_participation").Scan(&minRound)
2212+
require.NoError(t, err)
2213+
assert.GreaterOrEqual(t, minRound, uint64(2), "txn_participation should only have records for rounds >= 2")
2214+
2215+
var participationRoundsLessThan2 int
2216+
err = db.db.QueryRow(context.Background(), "SELECT COUNT(*) FROM txn_participation WHERE round < 2").Scan(&participationRoundsLessThan2)
2217+
require.NoError(t, err)
2218+
assert.Equal(t, 0, participationRoundsLessThan2, "txn_participation should not have any records for round < 2")
2219+
22092220
// query txns
22102221
rowsCh, _ := db.Transactions(context.Background(), idb.TransactionFilter{})
22112222

@@ -2237,6 +2248,16 @@ func TestDeleteTransactions(t *testing.T) {
22372248
err = db.DeleteTransactions(context.Background(), 5)
22382249
assert.NoError(t, err)
22392250

2251+
// verify txn_participation only has records for rounds >= 5
2252+
err = db.db.QueryRow(context.Background(), "SELECT MIN(round) FROM txn_participation").Scan(&minRound)
2253+
require.NoError(t, err)
2254+
assert.GreaterOrEqual(t, minRound, uint64(5), "txn_participation should only have records for rounds >= 5")
2255+
2256+
var participationRoundsLessThan5 int
2257+
err = db.db.QueryRow(context.Background(), "SELECT COUNT(*) FROM txn_participation WHERE round < 5").Scan(&participationRoundsLessThan5)
2258+
require.NoError(t, err)
2259+
assert.Equal(t, 0, participationRoundsLessThan5, "txn_participation should not have any records for round < 5")
2260+
22402261
// 2 txns in round 5
22412262
rowsCh, _ = db.Transactions(context.Background(), idb.TransactionFilter{})
22422263
i = 0

0 commit comments

Comments
 (0)