MCPcopy Create free account
hub / github.com/Permify/permify / SnapshotQuery

Function SnapshotQuery

internal/storage/postgres/utils/common.go:41–83  ·  view source on GitHub ↗

SnapshotQuery adds conditions to a SELECT query for checking transaction visibility based on created and expired transaction IDs. Optimized version with parameterized queries for security.

(sl squirrel.SelectBuilder, value uint64, snapshotValue string)

Source from the content-addressed store, hash-verified

39// SnapshotQuery adds conditions to a SELECT query for checking transaction visibility based on created and expired transaction IDs.
40// Optimized version with parameterized queries for security.
41func SnapshotQuery(sl squirrel.SelectBuilder, value uint64, snapshotValue string) squirrel.SelectBuilder {
42 // Backward compatibility: if snapshot is empty, use old method
43 if snapshotValue == "" {
44 // Create a subquery for the snapshot associated with the provided value.
45 snapshotQuery := "(select snapshot from transactions where id = ?::xid8)"
46
47 // Records that were created and are visible in the snapshot
48 createdWhere := squirrel.Or{
49 squirrel.Expr("pg_visible_in_snapshot(created_tx_id, ?) = true", squirrel.Expr(snapshotQuery, value)),
50 squirrel.Expr("created_tx_id = ?::xid8", value), // Include current transaction
51 }
52
53 // Records that are still active (not expired) at snapshot time
54 expiredWhere := squirrel.And{
55 squirrel.Or{
56 squirrel.Expr("pg_visible_in_snapshot(expired_tx_id, ?) = false", squirrel.Expr(snapshotQuery, value)),
57 squirrel.Expr("expired_tx_id = ?::xid8", ActiveRecordTxnID), // Never expired
58 },
59 squirrel.Expr("expired_tx_id <> ?::xid8", value), // Not expired by current transaction
60 }
61
62 // Add the created and expired conditions to the SELECT query.
63 return sl.Where(createdWhere).Where(expiredWhere)
64 }
65
66 // Records that were created and are visible in the snapshot
67 createdWhere := squirrel.Or{
68 squirrel.Expr("pg_visible_in_snapshot(created_tx_id, ?) = true", snapshotValue),
69 squirrel.Expr("created_tx_id = ?::xid8", value), // Include current transaction
70 }
71
72 // Records that are still active (not expired) at snapshot time
73 expiredWhere := squirrel.And{
74 squirrel.Or{
75 squirrel.Expr("pg_visible_in_snapshot(expired_tx_id, ?) = false", snapshotValue),
76 squirrel.Expr("expired_tx_id = ?::xid8", ActiveRecordTxnID), // Never expired
77 },
78 squirrel.Expr("expired_tx_id <> ?::xid8", value), // Not expired by current transaction
79 }
80
81 // Add the created and expired conditions to the SELECT query.
82 return sl.Where(createdWhere).Where(expiredWhere)
83}
84
85// GenerateGCQuery generates a Squirrel DELETE query builder for garbage collection.
86// It constructs a query to delete expired records from the specified table

Callers 7

QueryRelationshipsMethod · 0.92
ReadRelationshipsMethod · 0.92
QuerySingleAttributeMethod · 0.92
QueryAttributesMethod · 0.92
ReadAttributesMethod · 0.92
common_test.goFile · 0.92

Calls

no outgoing calls

Tested by

no test coverage detected