/
postgres.go
614 lines (513 loc) · 17.5 KB
/
postgres.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
package postgres
import (
"context"
dbsql "database/sql"
"errors"
"fmt"
"os"
"sync/atomic"
"time"
"github.com/IBM/pgxpoolprometheus"
sq "github.com/Masterminds/squirrel"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgconn"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/jackc/pgx/v5/stdlib"
"github.com/mattn/go-isatty"
"github.com/ngrok/sqlmw"
"github.com/prometheus/client_golang/prometheus"
"github.com/schollz/progressbar/v3"
"go.opentelemetry.io/otel"
"golang.org/x/sync/errgroup"
datastoreinternal "github.com/authzed/spicedb/internal/datastore"
"github.com/authzed/spicedb/internal/datastore/common"
pgxcommon "github.com/authzed/spicedb/internal/datastore/postgres/common"
"github.com/authzed/spicedb/internal/datastore/postgres/migrations"
"github.com/authzed/spicedb/internal/datastore/revisions"
log "github.com/authzed/spicedb/internal/logging"
"github.com/authzed/spicedb/pkg/datastore"
"github.com/authzed/spicedb/pkg/datastore/options"
)
func init() {
datastore.Engines = append(datastore.Engines, Engine)
}
const (
Engine = "postgres"
tableNamespace = "namespace_config"
tableTransaction = "relation_tuple_transaction"
tableTuple = "relation_tuple"
tableCaveat = "caveat"
colXID = "xid"
colTimestamp = "timestamp"
colNamespace = "namespace"
colConfig = "serialized_config"
colCreatedXid = "created_xid"
colDeletedXid = "deleted_xid"
colSnapshot = "snapshot"
colObjectID = "object_id"
colRelation = "relation"
colUsersetNamespace = "userset_namespace"
colUsersetObjectID = "userset_object_id"
colUsersetRelation = "userset_relation"
colCaveatName = "name"
colCaveatDefinition = "definition"
colCaveatContextName = "caveat_name"
colCaveatContext = "caveat_context"
errUnableToInstantiate = "unable to instantiate datastore"
// The parameters to this format string are:
// 1: the created_xid or deleted_xid column name
//
// The placeholders are the snapshot and the expected boolean value respectively.
snapshotAlive = "pg_visible_in_snapshot(%[1]s, ?) = ?"
// This is the largest positive integer possible in postgresql
liveDeletedTxnID = uint64(9223372036854775807)
tracingDriverName = "postgres-tracing"
gcBatchDeleteSize = 1000
)
var livingTupleConstraints = []string{"uq_relation_tuple_living_xid", "pk_relation_tuple"}
func init() {
dbsql.Register(tracingDriverName, sqlmw.Driver(stdlib.GetDefaultDriver(), new(traceInterceptor)))
}
var (
psql = sq.StatementBuilder.PlaceholderFormat(sq.Dollar)
getRevision = psql.
Select(colXID, colSnapshot).
From(tableTransaction).
OrderByClause(fmt.Sprintf("%s DESC", colXID)).
Limit(1)
createTxn = fmt.Sprintf(
"INSERT INTO %s DEFAULT VALUES RETURNING %s, %s",
tableTransaction,
colXID,
colSnapshot,
)
getNow = psql.Select("NOW()")
tracer = otel.Tracer("spicedb/internal/datastore/postgres")
)
type sqlFilter interface {
ToSql() (string, []interface{}, error)
}
// NewPostgresDatastore initializes a SpiceDB datastore that uses a PostgreSQL
// database by leveraging manual book-keeping to implement revisioning.
//
// This datastore is also tested to be compatible with CockroachDB.
func NewPostgresDatastore(
ctx context.Context,
url string,
options ...Option,
) (datastore.Datastore, error) {
ds, err := newPostgresDatastore(ctx, url, options...)
if err != nil {
return nil, err
}
return datastoreinternal.NewSeparatingContextDatastoreProxy(ds), nil
}
func newPostgresDatastore(
ctx context.Context,
pgURL string,
options ...Option,
) (datastore.Datastore, error) {
config, err := generateConfig(options)
if err != nil {
return nil, common.RedactAndLogSensitiveConnString(ctx, errUnableToInstantiate, err, pgURL)
}
// Parse the DB URI into configuration.
parsedConfig, err := pgxpool.ParseConfig(pgURL)
if err != nil {
return nil, common.RedactAndLogSensitiveConnString(ctx, errUnableToInstantiate, err, pgURL)
}
// Setup the default custom plan setting, if applicable.
pgConfig, err := defaultCustomPlan(parsedConfig)
if err != nil {
return nil, common.RedactAndLogSensitiveConnString(ctx, errUnableToInstantiate, err, pgURL)
}
// Setup the config for each of the read and write pools.
readPoolConfig := pgConfig.Copy()
config.readPoolOpts.ConfigurePgx(readPoolConfig)
readPoolConfig.AfterConnect = func(ctx context.Context, conn *pgx.Conn) error {
RegisterTypes(conn.TypeMap())
return nil
}
writePoolConfig := pgConfig.Copy()
config.writePoolOpts.ConfigurePgx(writePoolConfig)
writePoolConfig.AfterConnect = func(ctx context.Context, conn *pgx.Conn) error {
RegisterTypes(conn.TypeMap())
return nil
}
if config.migrationPhase != "" {
log.Info().
Str("phase", config.migrationPhase).
Msg("postgres configured to use intermediate migration phase")
}
initializationContext, cancelInit := context.WithTimeout(context.Background(), 5*time.Second)
defer cancelInit()
readPool, err := pgxpool.NewWithConfig(initializationContext, readPoolConfig)
if err != nil {
return nil, common.RedactAndLogSensitiveConnString(ctx, errUnableToInstantiate, err, pgURL)
}
writePool, err := pgxpool.NewWithConfig(initializationContext, writePoolConfig)
if err != nil {
return nil, common.RedactAndLogSensitiveConnString(ctx, errUnableToInstantiate, err, pgURL)
}
// Verify that the server supports commit timestamps
var trackTSOn string
if err := readPool.
QueryRow(initializationContext, "SHOW track_commit_timestamp;").
Scan(&trackTSOn); err != nil {
return nil, err
}
watchEnabled := trackTSOn == "on"
if !watchEnabled {
log.Warn().Msg("watch API disabled, postgres must be run with track_commit_timestamp=on")
}
if config.enablePrometheusStats {
if err := prometheus.Register(pgxpoolprometheus.NewCollector(readPool, map[string]string{
"db_name": "spicedb",
"pool_usage": "read",
})); err != nil {
return nil, err
}
if err := prometheus.Register(pgxpoolprometheus.NewCollector(writePool, map[string]string{
"db_name": "spicedb",
"pool_usage": "write",
})); err != nil {
return nil, err
}
if err := common.RegisterGCMetrics(); err != nil {
return nil, err
}
}
gcCtx, cancelGc := context.WithCancel(context.Background())
quantizationPeriodNanos := config.revisionQuantization.Nanoseconds()
if quantizationPeriodNanos < 1 {
quantizationPeriodNanos = 1
}
revisionQuery := fmt.Sprintf(
querySelectRevision,
colXID,
tableTransaction,
colTimestamp,
quantizationPeriodNanos,
colSnapshot,
)
validTransactionQuery := fmt.Sprintf(
queryValidTransaction,
colXID,
tableTransaction,
colTimestamp,
config.gcWindow.Seconds(),
colSnapshot,
)
maxRevisionStaleness := time.Duration(float64(config.revisionQuantization.Nanoseconds())*
config.maxRevisionStalenessPercent) * time.Nanosecond
datastore := &pgDatastore{
CachedOptimizedRevisions: revisions.NewCachedOptimizedRevisions(
maxRevisionStaleness,
),
dburl: pgURL,
readPool: pgxcommon.MustNewInterceptorPooler(readPool, config.queryInterceptor),
writePool: pgxcommon.MustNewInterceptorPooler(writePool, config.queryInterceptor),
watchBufferLength: config.watchBufferLength,
watchBufferWriteTimeout: config.watchBufferWriteTimeout,
optimizedRevisionQuery: revisionQuery,
validTransactionQuery: validTransactionQuery,
gcWindow: config.gcWindow,
gcInterval: config.gcInterval,
gcTimeout: config.gcMaxOperationTime,
analyzeBeforeStatistics: config.analyzeBeforeStatistics,
watchEnabled: watchEnabled,
gcCtx: gcCtx,
cancelGc: cancelGc,
readTxOptions: pgx.TxOptions{IsoLevel: pgx.RepeatableRead, AccessMode: pgx.ReadOnly},
maxRetries: config.maxRetries,
}
datastore.SetOptimizedRevisionFunc(datastore.optimizedRevisionFunc)
// Start a goroutine for garbage collection.
if datastore.gcInterval > 0*time.Minute && config.gcEnabled {
datastore.gcGroup, datastore.gcCtx = errgroup.WithContext(datastore.gcCtx)
datastore.gcGroup.Go(func() error {
return common.StartGarbageCollector(
datastore.gcCtx,
datastore,
datastore.gcInterval,
datastore.gcWindow,
datastore.gcTimeout,
)
})
} else {
log.Warn().Msg("datastore background garbage collection disabled")
}
return datastore, nil
}
type pgDatastore struct {
*revisions.CachedOptimizedRevisions
dburl string
readPool, writePool pgxcommon.ConnPooler
watchBufferLength uint16
watchBufferWriteTimeout time.Duration
optimizedRevisionQuery string
validTransactionQuery string
gcWindow time.Duration
gcInterval time.Duration
gcTimeout time.Duration
analyzeBeforeStatistics bool
readTxOptions pgx.TxOptions
maxRetries uint8
watchEnabled bool
gcGroup *errgroup.Group
gcCtx context.Context
cancelGc context.CancelFunc
gcHasRun atomic.Bool
}
func (pgd *pgDatastore) SnapshotReader(revRaw datastore.Revision) datastore.Reader {
rev := revRaw.(postgresRevision)
queryFuncs := pgxcommon.QuerierFuncsFor(pgd.readPool)
executor := common.QueryExecutor{
Executor: pgxcommon.NewPGXExecutor(queryFuncs),
}
return &pgReader{
queryFuncs,
executor,
buildLivingObjectFilterForRevision(rev),
}
}
// ReadWriteTx starts a read/write transaction, which will be committed if no error is
// returned and rolled back if an error is returned.
func (pgd *pgDatastore) ReadWriteTx(
ctx context.Context,
fn datastore.TxUserFunc,
opts ...options.RWTOptionsOption,
) (datastore.Revision, error) {
config := options.NewRWTOptionsWithOptions(opts...)
var err error
for i := uint8(0); i <= pgd.maxRetries; i++ {
var newXID xid8
var newSnapshot pgSnapshot
err = wrapError(pgx.BeginTxFunc(ctx, pgd.writePool, pgx.TxOptions{IsoLevel: pgx.Serializable}, func(tx pgx.Tx) error {
var err error
newXID, newSnapshot, err = createNewTransaction(ctx, tx)
if err != nil {
return err
}
queryFuncs := pgxcommon.QuerierFuncsFor(pgd.readPool)
executor := common.QueryExecutor{
Executor: pgxcommon.NewPGXExecutor(queryFuncs),
}
rwt := &pgReadWriteTXN{
&pgReader{
queryFuncs,
executor,
currentlyLivingObjects,
},
tx,
newXID,
}
return fn(ctx, rwt)
}))
if err != nil {
if !config.DisableRetries && errorRetryable(err) {
pgxcommon.SleepOnErr(ctx, err, i)
continue
}
return datastore.NoRevision, err
}
if i > 0 {
log.Debug().Uint8("retries", i).Msg("transaction succeeded after retry")
}
return postgresRevision{newSnapshot.markComplete(newXID.Uint64)}, nil
}
if !config.DisableRetries {
err = fmt.Errorf("max retries exceeded: %w", err)
}
return datastore.NoRevision, err
}
const repairTransactionIDsOperation = "transaction-ids"
func (pgd *pgDatastore) Repair(ctx context.Context, operationName string, outputProgress bool) error {
switch operationName {
case repairTransactionIDsOperation:
return pgd.repairTransactionIDs(ctx, outputProgress)
default:
return fmt.Errorf("unknown operation")
}
}
const batchSize = 10000
func (pgd *pgDatastore) repairTransactionIDs(ctx context.Context, outputProgress bool) error {
conn, err := pgx.Connect(ctx, pgd.dburl)
if err != nil {
return err
}
defer conn.Close(ctx)
// Get the current transaction ID.
currentMaximumID := 0
if err := conn.QueryRow(ctx, queryCurrentTransactionID).Scan(¤tMaximumID); err != nil {
if !errors.Is(err, pgx.ErrNoRows) {
return err
}
}
// Find the maximum transaction ID referenced in the transactions table.
referencedMaximumID := 0
if err := conn.QueryRow(ctx, queryLatestXID).Scan(&referencedMaximumID); err != nil {
if !errors.Is(err, pgx.ErrNoRows) {
return err
}
}
// The delta is what this needs to fill in.
log.Ctx(ctx).Info().Int64("current-maximum", int64(currentMaximumID)).Int64("referenced-maximum", int64(referencedMaximumID)).Msg("found transactions")
counterDelta := referencedMaximumID - currentMaximumID
if counterDelta < 0 {
return nil
}
var bar *progressbar.ProgressBar
if isatty.IsTerminal(os.Stderr.Fd()) && outputProgress {
bar = progressbar.Default(int64(counterDelta), "updating transactions counter")
}
for i := 0; i < counterDelta; i++ {
var batch pgx.Batch
batchCount := min(batchSize, counterDelta-i)
for j := 0; j < batchCount; j++ {
batch.Queue("begin;")
batch.Queue("select pg_current_xact_id();")
batch.Queue("rollback;")
}
br := conn.SendBatch(ctx, &batch)
if err := br.Close(); err != nil {
return err
}
i += batchCount - 1
if bar != nil {
if err := bar.Add(batchCount); err != nil {
return err
}
}
}
if bar != nil {
if err := bar.Close(); err != nil {
return err
}
}
log.Ctx(ctx).Info().Msg("completed revisions repair")
return nil
}
// RepairOperations returns the available repair operations for the datastore.
func (pgd *pgDatastore) RepairOperations() []datastore.RepairOperation {
return []datastore.RepairOperation{
{
Name: repairTransactionIDsOperation,
Description: "Brings the Postgres database up to the expected transaction ID (Postgres v15+ only)",
},
}
}
func wrapError(err error) error {
// If a unique constraint violation is returned, then its likely that the cause
// was an existing relationship given as a CREATE.
if cerr := pgxcommon.ConvertToWriteConstraintError(livingTupleConstraints, err); cerr != nil {
return cerr
}
if pgxcommon.IsSerializationError(err) {
return common.NewSerializationError(err)
}
// hack: pgx asyncClose usually happens after cancellation,
// but the reason for it being closed is not propagated
// and all we get is attempting to perform an operation
// on cancelled connection. This keeps the same error,
// but wrapped along a cancellation so that:
// - pgx logger does not log it
// - response is sent as canceled back to the client
if err != nil && err.Error() == "conn closed" {
return errors.Join(err, context.Canceled)
}
return err
}
func (pgd *pgDatastore) Close() error {
pgd.cancelGc()
if pgd.gcGroup != nil {
err := pgd.gcGroup.Wait()
log.Warn().Err(err).Msg("completed shutdown of postgres datastore")
}
pgd.readPool.Close()
pgd.writePool.Close()
return nil
}
func errorRetryable(err error) bool {
if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
return false
}
if pgconn.SafeToRetry(err) {
return true
}
if pgxcommon.IsSerializationError(err) {
return true
}
log.Warn().Err(err).Msg("unable to determine if pgx error is retryable")
return false
}
func (pgd *pgDatastore) ReadyState(ctx context.Context) (datastore.ReadyState, error) {
headMigration, err := migrations.DatabaseMigrations.HeadRevision()
if err != nil {
return datastore.ReadyState{}, fmt.Errorf("invalid head migration found for postgres: %w", err)
}
pgDriver, err := migrations.NewAlembicPostgresDriver(ctx, pgd.dburl)
if err != nil {
return datastore.ReadyState{}, err
}
defer pgDriver.Close(ctx)
version, err := pgDriver.Version(ctx)
if err != nil {
return datastore.ReadyState{}, err
}
if version == headMigration {
// Ensure a datastore ID is present. This ensures the tables have not been truncated.
uniqueID, err := pgd.datastoreUniqueID(ctx)
if err != nil {
return datastore.ReadyState{}, fmt.Errorf("database validation failed: %w; if you have previously run `TRUNCATE`, this database is no longer valid and must be remigrated. See: https://spicedb.dev/d/truncate-unsupported", err)
}
log.Trace().Str("unique_id", uniqueID).Msg("postgres datastore unique ID")
return datastore.ReadyState{IsReady: true}, nil
}
return datastore.ReadyState{
Message: fmt.Sprintf(
"datastore is not migrated: currently at revision `%s`, but requires `%s`. Please run `spicedb migrate`. If you have previously run `TRUNCATE`, this database is no longer valid and must be remigrated. See: https://spicedb.dev/d/truncate-unsupported",
version,
headMigration,
),
IsReady: false,
}, nil
}
func (pgd *pgDatastore) Features(_ context.Context) (*datastore.Features, error) {
return &datastore.Features{Watch: datastore.Feature{Enabled: pgd.watchEnabled}}, nil
}
func buildLivingObjectFilterForRevision(revision postgresRevision) queryFilterer {
createdBeforeTXN := sq.Expr(fmt.Sprintf(
snapshotAlive,
colCreatedXid,
), revision.snapshot, true)
deletedAfterTXN := sq.Expr(fmt.Sprintf(
snapshotAlive,
colDeletedXid,
), revision.snapshot, false)
return func(original sq.SelectBuilder) sq.SelectBuilder {
return original.Where(createdBeforeTXN).Where(deletedAfterTXN)
}
}
func currentlyLivingObjects(original sq.SelectBuilder) sq.SelectBuilder {
return original.Where(sq.Eq{colDeletedXid: liveDeletedTxnID})
}
// defaultCustomPlan parses a Postgres URI and determines if a plan_cache_mode
// has been specified. If not, it defaults to "force_custom_plan".
// This works around a bug impacting performance documented here:
// https://spicedb.dev/d/force-custom-plan.
func defaultCustomPlan(poolConfig *pgxpool.Config) (*pgxpool.Config, error) {
if existing, ok := poolConfig.ConnConfig.Config.RuntimeParams["plan_cache_mode"]; ok {
log.Info().
Str("plan_cache_mode", existing).
Msg("found plan_cache_mode in DB URI; leaving as-is")
return poolConfig, nil
}
poolConfig.ConnConfig.Config.RuntimeParams["plan_cache_mode"] = "force_custom_plan"
log.Warn().
Str("details-url", "https://spicedb.dev/d/force-custom-plan").
Str("plan_cache_mode", "force_custom_plan").
Msg("defaulting value in Postgres DB URI")
return poolConfig, nil
}
var _ datastore.Datastore = &pgDatastore{}