datum-persistence 0.11.3

Event sourcing, durable state, and CQRS persistence for Datum
Documentation
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
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
//! PostgreSQL projection offset store and exactly-once transaction support.
//!
//! The schema follows the Akka Projection JDBC/R2DBC offset-store shape:
//! `(projection_name, projection_key)` identifies a main row containing the
//! opaque current offset, provider manifest, mergeable marker, and update
//! time. Datum adds the `generation` required by projection fencing. The main
//! row records the generation that committed its current cursor; the
//! management row holds the current owner generation as well as the
//! independently mutable `paused` flag. Keeping ownership separate from the
//! locked cursor row lets a newer claimant fence a handler transaction that
//! is already in flight; the old transaction revalidates the owner immediately
//! before writing its cursor.
//!
//! Timestamp positions need more state than Akka's main offset row can carry.
//! Following Akka's separate `projection_timestamp_offset_store` precedent,
//! Datum normalizes every consumed `(persistence_id, seq_nr)` member at the
//! current boundary into [`PostgresProjectionTableNames::timestamp_offset_store`].
//! The main row remains authoritative for the provider manifest and boundary
//! position. Saving or clearing replaces the main cursor and every boundary
//! member in one transaction, so an empty boundary and a position shared by
//! multiple persistence ids both round-trip exactly.

use std::{fmt, sync::Arc, time::Duration};

use sqlx::{
    PgPool, Postgres, Row, Transaction,
    postgres::{PgPoolOptions, PgRow},
};
use tokio::sync::OnceCell;

use crate::{PersistenceId, SequenceNr};

use super::{
    BoundaryEvent, CursorPosition, DeduplicationBoundary, PostgresProjectionTx, ProjectionCursor,
    ProjectionError, ProjectionGeneration, ProjectionId, ProjectionOffsetStore, ProjectionResult,
    ProviderManifest, SaveCrashPoint,
};

const DEFAULT_MAX_CONNECTIONS: u32 = 16;
const DEFAULT_ACQUIRE_TIMEOUT: Duration = Duration::from_secs(30);
const DEFAULT_IDLE_TIMEOUT: Duration = Duration::from_secs(600);
const DEFAULT_MAX_LIFETIME: Duration = Duration::from_secs(1_800);
const MAX_TABLE_IDENTIFIER_BYTES: usize = 48;
const MAX_GENERATION: i64 = i64::MAX;

/// Configurable PostgreSQL projection table names.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PostgresProjectionTableNames {
    /// Main Akka-shaped offset and fencing table.
    pub offset_store: String,
    /// Per-event rows at an inclusive timestamp boundary.
    pub timestamp_offset_store: String,
    /// Durable paused-management table.
    pub management: String,
}

impl Default for PostgresProjectionTableNames {
    fn default() -> Self {
        Self {
            offset_store: "datum_projection_offset_store".into(),
            timestamp_offset_store: "datum_projection_timestamp_offset_store".into(),
            management: "datum_projection_management".into(),
        }
    }
}

/// Side-effect-free PostgreSQL projection offset-store configuration.
#[derive(Clone)]
pub struct PostgresProjectionOffsetStoreConfig {
    /// Connection URL used when the store owns its pool.
    ///
    /// This may be `None` when constructing with
    /// [`PostgresProjectionOffsetStore::from_pool`].
    pub database_url: Option<String>,
    /// Maximum connections in a store-owned pool.
    pub max_connections: u32,
    /// Minimum connections in a store-owned pool.
    pub min_connections: u32,
    /// Maximum wait for acquiring or establishing a connection.
    pub acquire_timeout: Duration,
    /// Idle lifetime for pooled connections, or `None` to disable reaping.
    pub idle_timeout: Option<Duration>,
    /// Maximum lifetime for pooled connections, or `None` to disable it.
    pub max_lifetime: Option<Duration>,
    /// PostgreSQL schema containing the projection tables.
    pub schema: String,
    /// Projection table names within [`Self::schema`].
    pub tables: PostgresProjectionTableNames,
}

impl PostgresProjectionOffsetStoreConfig {
    /// Creates a configuration with production-oriented defaults.
    #[must_use]
    pub fn new(database_url: impl Into<String>) -> Self {
        Self {
            database_url: Some(database_url.into()),
            max_connections: DEFAULT_MAX_CONNECTIONS,
            min_connections: 0,
            acquire_timeout: DEFAULT_ACQUIRE_TIMEOUT,
            idle_timeout: Some(DEFAULT_IDLE_TIMEOUT),
            max_lifetime: Some(DEFAULT_MAX_LIFETIME),
            schema: "public".into(),
            tables: PostgresProjectionTableNames::default(),
        }
    }

    /// Creates a configuration intended for an already-created [`PgPool`].
    #[must_use]
    pub fn for_pool() -> Self {
        let mut config = Self::new(String::new());
        config.database_url = None;
        config
    }
}

impl fmt::Debug for PostgresProjectionOffsetStoreConfig {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("PostgresProjectionOffsetStoreConfig")
            .field(
                "database_url",
                &self.database_url.as_ref().map(|_| "<redacted>"),
            )
            .field("max_connections", &self.max_connections)
            .field("min_connections", &self.min_connections)
            .field("acquire_timeout", &self.acquire_timeout)
            .field("idle_timeout", &self.idle_timeout)
            .field("max_lifetime", &self.max_lifetime)
            .field("schema", &self.schema)
            .field("tables", &self.tables)
            .finish()
    }
}

#[derive(Debug)]
struct PostgresProjectionSql {
    bootstrap_lock_key: i64,
    management: String,
    ddl: Vec<String>,
    load: String,
    lock: String,
    insert_claim_row: String,
    insert_claim_owner: String,
    load_generation: String,
    lock_generation: String,
    increment_generation: String,
    update_cursor: String,
    clear_cursor: String,
    delete_boundary: String,
    insert_boundary: String,
}

impl PostgresProjectionSql {
    fn new(config: &PostgresProjectionOffsetStoreConfig) -> ProjectionResult<Self> {
        validate_config(config)?;
        let schema = quote_identifier(&config.schema);
        let offset_store = qualified(&config.schema, &config.tables.offset_store);
        let timestamp_offset_store =
            qualified(&config.schema, &config.tables.timestamp_offset_store);
        let management = qualified(&config.schema, &config.tables.management);

        let ddl = vec![
            format!("CREATE SCHEMA IF NOT EXISTS {schema}"),
            format!(
                "CREATE TABLE IF NOT EXISTS {offset_store} (\
                 projection_name TEXT NOT NULL,\
                 projection_key TEXT NOT NULL,\
                 current_offset TEXT,\
                 manifest TEXT,\
                 mergeable BOOLEAN NOT NULL DEFAULT FALSE,\
                 last_updated BIGINT NOT NULL DEFAULT 0,\
                 generation BIGINT NOT NULL DEFAULT 0 CHECK (generation >= 0),\
                 PRIMARY KEY (projection_name, projection_key),\
                 CHECK ((current_offset IS NULL) = (manifest IS NULL))\
                 )"
            ),
            // CREATE TABLE IF NOT EXISTS does not add columns when reopening an
            // older table. Keep generation as an explicit additive migration.
            format!(
                "ALTER TABLE {offset_store} ADD COLUMN IF NOT EXISTS \
                 generation BIGINT NOT NULL DEFAULT 0"
            ),
            format!(
                "ALTER TABLE {offset_store} ADD COLUMN IF NOT EXISTS \
                 mergeable BOOLEAN NOT NULL DEFAULT FALSE"
            ),
            // A generation claim needs an offset row before the first cursor
            // exists. Reopen Akka-shaped/older Datum tables by widening the
            // payload columns and making that pre-cursor state representable.
            format!(
                "ALTER TABLE {offset_store} ALTER COLUMN current_offset TYPE TEXT, \
                 ALTER COLUMN current_offset DROP NOT NULL"
            ),
            format!(
                "ALTER TABLE {offset_store} ALTER COLUMN manifest TYPE TEXT, \
                 ALTER COLUMN manifest DROP NOT NULL"
            ),
            format!(
                "CREATE TABLE IF NOT EXISTS {timestamp_offset_store} (\
                 projection_name TEXT NOT NULL,\
                 projection_key TEXT NOT NULL,\
                 current_offset TEXT NOT NULL,\
                 persistence_id TEXT NOT NULL,\
                 seq_nr BIGINT NOT NULL CHECK (seq_nr >= 0),\
                 PRIMARY KEY (projection_name, projection_key, persistence_id, seq_nr),\
                 FOREIGN KEY (projection_name, projection_key) \
                 REFERENCES {offset_store} (projection_name, projection_key) \
                 ON DELETE CASCADE\
                 )"
            ),
            format!(
                "CREATE TABLE IF NOT EXISTS {management} (\
                 projection_name TEXT NOT NULL,\
                 projection_key TEXT NOT NULL,\
                 paused BOOLEAN NOT NULL,\
                 last_updated BIGINT NOT NULL DEFAULT 0,\
                 generation BIGINT NOT NULL DEFAULT 0 CHECK (generation >= 0),\
                 PRIMARY KEY (projection_name, projection_key)\
                 )"
            ),
            format!(
                "ALTER TABLE {management} ADD COLUMN IF NOT EXISTS \
                 generation BIGINT NOT NULL DEFAULT 0"
            ),
            // Older PJ4 schemas kept the owner generation only on the offset
            // row. Preserve it when adding the independently claimable owner
            // token to an existing management row.
            format!(
                "UPDATE {management} AS management SET generation = offsets.generation \
                 FROM {offset_store} AS offsets \
                 WHERE management.projection_name = offsets.projection_name \
                 AND management.projection_key = offsets.projection_key \
                 AND management.generation < offsets.generation"
            ),
        ];

        Ok(Self {
            bootstrap_lock_key: bootstrap_lock_key(config),
            load: format!(
                "SELECT offsets.current_offset, offsets.manifest, \
                 boundary.current_offset AS boundary_offset, \
                 boundary.persistence_id, boundary.seq_nr \
                 FROM {offset_store} AS offsets \
                 LEFT JOIN {timestamp_offset_store} AS boundary \
                 ON boundary.projection_name = offsets.projection_name \
                 AND boundary.projection_key = offsets.projection_key \
                 WHERE offsets.projection_name = $1 AND offsets.projection_key = $2 \
                 ORDER BY boundary.persistence_id, boundary.seq_nr"
            ),
            lock: format!(
                "SELECT current_offset FROM {offset_store} \
                 WHERE projection_name = $1 AND projection_key = $2 FOR UPDATE"
            ),
            insert_claim_row: format!(
                "INSERT INTO {offset_store} \
                 (projection_name, projection_key, current_offset, manifest, \
                  mergeable, last_updated, generation) \
                 VALUES ($1, $2, NULL, NULL, FALSE, 0, 0) \
                 ON CONFLICT (projection_name, projection_key) DO NOTHING"
            ),
            insert_claim_owner: format!(
                "INSERT INTO {management} \
                 (projection_name, projection_key, paused, last_updated, generation) \
                 SELECT $1, $2, FALSE, 0, generation FROM {offset_store} \
                 WHERE projection_name = $1 AND projection_key = $2 \
                 ON CONFLICT (projection_name, projection_key) DO NOTHING"
            ),
            load_generation: format!(
                "SELECT generation FROM {management} \
                 WHERE projection_name = $1 AND projection_key = $2"
            ),
            lock_generation: format!(
                "SELECT generation FROM {management} \
                 WHERE projection_name = $1 AND projection_key = $2 FOR UPDATE"
            ),
            increment_generation: format!(
                "UPDATE {management} SET generation = generation + 1 \
                 WHERE projection_name = $1 AND projection_key = $2 \
                 AND generation < {MAX_GENERATION} \
                 RETURNING generation"
            ),
            update_cursor: format!(
                "UPDATE {offset_store} SET current_offset = $3, manifest = $4, \
                 mergeable = FALSE, generation = $5, \
                 last_updated = floor(extract(epoch from clock_timestamp()) * 1000)::BIGINT \
                 WHERE projection_name = $1 AND projection_key = $2"
            ),
            clear_cursor: format!(
                "UPDATE {offset_store} SET current_offset = NULL, manifest = NULL, \
                 mergeable = FALSE, generation = $3, \
                 last_updated = floor(extract(epoch from clock_timestamp()) * 1000)::BIGINT \
                 WHERE projection_name = $1 AND projection_key = $2"
            ),
            delete_boundary: format!(
                "DELETE FROM {timestamp_offset_store} \
                 WHERE projection_name = $1 AND projection_key = $2"
            ),
            insert_boundary: format!(
                "INSERT INTO {timestamp_offset_store} \
                 (projection_name, projection_key, current_offset, persistence_id, seq_nr) \
                 VALUES ($1, $2, $3, $4, $5)"
            ),
            management,
            ddl,
        })
    }
}

struct PostgresProjectionOffsetStoreInner {
    config: PostgresProjectionOffsetStoreConfig,
    sql: PostgresProjectionSql,
    pool: OnceCell<PgPool>,
    initialized: OnceCell<()>,
}

/// Durable PostgreSQL projection cursor store with CAS and generation fencing.
///
/// Construction is a pure blueprint operation. [`Self::connect`] or the first
/// store operation initializes the idempotent schema. Clones share a pool and
/// initialization attempt.
#[derive(Clone)]
pub struct PostgresProjectionOffsetStore {
    inner: Arc<PostgresProjectionOffsetStoreInner>,
}

impl fmt::Debug for PostgresProjectionOffsetStore {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("PostgresProjectionOffsetStore")
            .field("config", &self.inner.config)
            .field("has_pool", &self.inner.pool.get().is_some())
            .field("initialized", &self.inner.initialized.get().is_some())
            .finish()
    }
}

impl PostgresProjectionOffsetStore {
    /// Creates a lazy, side-effect-free offset-store blueprint.
    pub fn new(config: PostgresProjectionOffsetStoreConfig) -> ProjectionResult<Self> {
        let sql = PostgresProjectionSql::new(&config)?;
        if config.database_url.as_deref().is_none_or(str::is_empty) {
            return Err(ProjectionError::invalid_config(
                "postgres.database_url",
                "database_url is required when no PgPool is supplied",
            ));
        }
        Ok(Self {
            inner: Arc::new(PostgresProjectionOffsetStoreInner {
                config,
                sql,
                pool: OnceCell::new(),
                initialized: OnceCell::new(),
            }),
        })
    }

    /// Creates a side-effect-free store around an application-owned pool.
    pub fn from_pool(
        pool: PgPool,
        config: PostgresProjectionOffsetStoreConfig,
    ) -> ProjectionResult<Self> {
        let sql = PostgresProjectionSql::new(&config)?;
        Ok(Self {
            inner: Arc::new(PostgresProjectionOffsetStoreInner {
                config,
                sql,
                pool: OnceCell::new_with(Some(pool)),
                initialized: OnceCell::new(),
            }),
        })
    }

    /// Connects a pool and runs idempotent schema creation and migration.
    pub async fn connect(config: PostgresProjectionOffsetStoreConfig) -> ProjectionResult<Self> {
        let store = Self::new(config)?;
        store.initialize().await?;
        Ok(store)
    }

    /// Connects lazily when needed and executes idempotent bootstrap DDL.
    ///
    /// This path handles both a missing schema and an existing older offset
    /// table without the generation column.
    pub async fn initialize(&self) -> ProjectionResult<()> {
        let pool = self.ready_pool_without_schema().await?;
        self.inner
            .initialized
            .get_or_try_init(|| async {
                bootstrap(pool, self.inner.sql.bootstrap_lock_key, &self.inner.sql.ddl).await
            })
            .await
            .map(|_| ())
    }

    /// Returns the configured pool after it has been connected.
    #[must_use]
    pub fn pool(&self) -> Option<&PgPool> {
        self.inner.pool.get()
    }

    /// Returns this store's immutable configuration.
    #[must_use]
    pub fn config(&self) -> &PostgresProjectionOffsetStoreConfig {
        &self.inner.config
    }

    /// Returns the exact idempotent DDL emitted by initialization.
    #[must_use]
    pub fn schema_ddl(&self) -> String {
        let mut ddl = self.inner.sql.ddl.join(";\n");
        ddl.push(';');
        ddl
    }

    async fn ready_pool_without_schema(&self) -> ProjectionResult<&PgPool> {
        self.inner
            .pool
            .get_or_try_init(|| async {
                let database_url = self.inner.config.database_url.as_deref().ok_or_else(|| {
                    ProjectionError::invalid_config(
                        "postgres.database_url",
                        "database_url is required when no PgPool is supplied",
                    )
                })?;
                pool_options(&self.inner.config)
                    .connect(database_url)
                    .await
                    .map_err(|error| postgres_error("connect PostgreSQL projection pool", error))
            })
            .await
    }

    async fn ready_pool(&self) -> ProjectionResult<&PgPool> {
        self.initialize().await?;
        self.inner.pool.get().ok_or_else(|| {
            ProjectionError::OffsetStore(
                "PostgreSQL pool unavailable after successful initialization".into(),
            )
        })
    }

    pub(crate) async fn begin_exactly_once(
        &self,
        projection_id: &ProjectionId,
        generation: ProjectionGeneration,
        expected_position: Option<&CursorPosition>,
    ) -> ProjectionResult<PostgresProjectionTx> {
        projection_id.validate()?;
        let mut transaction = self
            .ready_pool()
            .await?
            .begin()
            .await
            .map_err(|error| postgres_error("begin exactly-once transaction", error))?;
        self.lock_and_validate(
            &mut transaction,
            projection_id,
            generation,
            expected_position,
            true,
        )
        .await?;
        Ok(PostgresProjectionTx::new(transaction))
    }

    pub(crate) async fn commit_exactly_once(
        &self,
        mut transaction: PostgresProjectionTx,
        projection_id: &ProjectionId,
        generation: ProjectionGeneration,
        cursor: &ProjectionCursor,
    ) -> ProjectionResult<()> {
        let connection = transaction.connection_for_cursor()?;
        self.lock_generation_and_validate(connection, projection_id, generation)
            .await?;
        self.write_cursor(connection, projection_id, generation, cursor)
            .await?;
        transaction.commit().await
    }

    /// Runs a save and suspends at a real commit seam for process-kill tests.
    ///
    /// This hidden hook is intentionally separate from [`ProjectionOffsetStore::save`].
    /// The callback signals the supervising test process only after all cursor
    /// writes have been issued with the transaction still open, or after the
    /// database commit has completed but before this method can acknowledge
    /// success. The returned future then remains pending until the supervisor
    /// kills the disposable child process.
    #[doc(hidden)]
    pub async fn save_with_crash_hook_for_test<F>(
        &self,
        projection_id: &ProjectionId,
        generation: ProjectionGeneration,
        expected_position: Option<&CursorPosition>,
        cursor: ProjectionCursor,
        crash_point: SaveCrashPoint,
        reached_seam: F,
    ) -> ProjectionResult<()>
    where
        F: FnOnce(),
    {
        let mut transaction = self
            .begin_exactly_once(projection_id, generation, expected_position)
            .await?;
        self.lock_generation_and_validate(
            transaction.connection_for_cursor()?,
            projection_id,
            generation,
        )
        .await?;
        self.write_cursor(
            transaction.connection_for_cursor()?,
            projection_id,
            generation,
            &cursor,
        )
        .await?;
        match crash_point {
            SaveCrashPoint::BeforeCommit => {
                reached_seam();
                std::future::pending::<ProjectionResult<()>>().await
            }
            SaveCrashPoint::AfterCommitBeforeAcknowledgement => {
                transaction.commit().await?;
                reached_seam();
                std::future::pending::<ProjectionResult<()>>().await
            }
        }
    }

    async fn lock_and_validate(
        &self,
        transaction: &mut Transaction<'_, Postgres>,
        projection_id: &ProjectionId,
        attempted: ProjectionGeneration,
        expected_position: Option<&CursorPosition>,
        compare_position: bool,
    ) -> ProjectionResult<()> {
        let row = sqlx::query(&self.inner.sql.lock)
            .bind(projection_id.name())
            .bind(projection_id.key())
            .fetch_optional(&mut **transaction)
            .await
            .map_err(|error| postgres_error("lock PostgreSQL projection offset row", error))?;
        let Some(row) = row else {
            return Err(ProjectionError::Fenced {
                projection_id: projection_id.clone(),
                attempted_generation: attempted.as_u64(),
                current_generation: 0,
            });
        };
        let generation_row = sqlx::query(&self.inner.sql.load_generation)
            .bind(projection_id.name())
            .bind(projection_id.key())
            .fetch_optional(&mut **transaction)
            .await
            .map_err(|error| {
                postgres_error("load PostgreSQL projection owner generation", error)
            })?;
        let current = generation_row
            .as_ref()
            .map(decode_generation)
            .transpose()?
            .unwrap_or_default();
        if attempted != current || attempted == ProjectionGeneration::default() {
            return Err(ProjectionError::Fenced {
                projection_id: projection_id.clone(),
                attempted_generation: attempted.as_u64(),
                current_generation: current.as_u64(),
            });
        }
        let actual: Option<String> = row
            .try_get("current_offset")
            .map_err(|error| postgres_error("decode PostgreSQL current offset", error))?;
        if compare_position && actual.as_deref() != expected_position.map(CursorPosition::as_str) {
            return Err(ProjectionError::UnexpectedPosition {
                projection_id: projection_id.clone(),
                expected: expected_position.map(ToString::to_string),
                actual,
            });
        }
        Ok(())
    }

    async fn lock_generation_and_validate(
        &self,
        connection: &mut sqlx::PgConnection,
        projection_id: &ProjectionId,
        attempted: ProjectionGeneration,
    ) -> ProjectionResult<()> {
        let row = sqlx::query(&self.inner.sql.lock_generation)
            .bind(projection_id.name())
            .bind(projection_id.key())
            .fetch_optional(connection)
            .await
            .map_err(|error| {
                postgres_error("lock PostgreSQL projection owner generation", error)
            })?;
        let current = row
            .as_ref()
            .map(decode_generation)
            .transpose()?
            .unwrap_or_default();
        if attempted != current || attempted == ProjectionGeneration::default() {
            return Err(ProjectionError::Fenced {
                projection_id: projection_id.clone(),
                attempted_generation: attempted.as_u64(),
                current_generation: current.as_u64(),
            });
        }
        Ok(())
    }

    async fn write_cursor(
        &self,
        connection: &mut sqlx::PgConnection,
        projection_id: &ProjectionId,
        generation: ProjectionGeneration,
        cursor: &ProjectionCursor,
    ) -> ProjectionResult<()> {
        let generation = encode_generation(generation)?;
        sqlx::query(&self.inner.sql.update_cursor)
            .bind(projection_id.name())
            .bind(projection_id.key())
            .bind(cursor.position().as_str())
            .bind(cursor.provider_manifest().as_str())
            .bind(generation)
            .execute(&mut *connection)
            .await
            .map_err(|error| postgres_error("update PostgreSQL projection cursor", error))?;
        self.delete_boundary(connection, projection_id).await?;
        for event in cursor.boundary().consumed() {
            let sequence_nr = i64::try_from(event.sequence_nr().as_u64()).map_err(|_| {
                ProjectionError::CursorCodec(format!(
                    "boundary sequence number {} exceeds PostgreSQL BIGINT",
                    event.sequence_nr()
                ))
            })?;
            sqlx::query(&self.inner.sql.insert_boundary)
                .bind(projection_id.name())
                .bind(projection_id.key())
                .bind(cursor.position().as_str())
                .bind(event.persistence_id().id())
                .bind(sequence_nr)
                .execute(&mut *connection)
                .await
                .map_err(|error| {
                    postgres_error("insert PostgreSQL projection boundary member", error)
                })?;
        }
        Ok(())
    }

    async fn delete_boundary(
        &self,
        connection: &mut sqlx::PgConnection,
        projection_id: &ProjectionId,
    ) -> ProjectionResult<()> {
        sqlx::query(&self.inner.sql.delete_boundary)
            .bind(projection_id.name())
            .bind(projection_id.key())
            .execute(connection)
            .await
            .map_err(|error| postgres_error("delete PostgreSQL projection boundary", error))?;
        Ok(())
    }
}

#[async_trait::async_trait]
impl ProjectionOffsetStore for PostgresProjectionOffsetStore {
    fn validate_exactly_once(&self) -> ProjectionResult<()> {
        Ok(())
    }

    async fn claim(&self, projection_id: &ProjectionId) -> ProjectionResult<ProjectionGeneration> {
        projection_id.validate()?;
        let mut transaction = self
            .ready_pool()
            .await?
            .begin()
            .await
            .map_err(|error| postgres_error("begin PostgreSQL projection claim", error))?;
        let row = sqlx::query(&self.inner.sql.increment_generation)
            .bind(projection_id.name())
            .bind(projection_id.key())
            .fetch_optional(&mut *transaction)
            .await
            .map_err(|error| postgres_error("increment PostgreSQL projection generation", error))?;
        if let Some(row) = row {
            let generation = decode_generation(&row)?;
            transaction
                .commit()
                .await
                .map_err(|error| postgres_error("commit PostgreSQL projection claim", error))?;
            return Ok(generation);
        }
        let existing_owner = sqlx::query(&self.inner.sql.load_generation)
            .bind(projection_id.name())
            .bind(projection_id.key())
            .fetch_optional(&mut *transaction)
            .await
            .map_err(|error| postgres_error("load PostgreSQL projection generation", error))?;
        if existing_owner.is_some() {
            return Err(ProjectionError::GenerationExhausted {
                projection_id: projection_id.clone(),
            });
        }

        sqlx::query(&self.inner.sql.insert_claim_row)
            .bind(projection_id.name())
            .bind(projection_id.key())
            .execute(&mut *transaction)
            .await
            .map_err(|error| postgres_error("create PostgreSQL projection claim row", error))?;
        sqlx::query(&self.inner.sql.insert_claim_owner)
            .bind(projection_id.name())
            .bind(projection_id.key())
            .execute(&mut *transaction)
            .await
            .map_err(|error| postgres_error("create PostgreSQL projection owner row", error))?;
        let row = sqlx::query(&self.inner.sql.increment_generation)
            .bind(projection_id.name())
            .bind(projection_id.key())
            .fetch_optional(&mut *transaction)
            .await
            .map_err(|error| postgres_error("increment PostgreSQL projection generation", error))?;
        let Some(row) = row else {
            return Err(ProjectionError::GenerationExhausted {
                projection_id: projection_id.clone(),
            });
        };
        let generation = decode_generation(&row)?;
        transaction
            .commit()
            .await
            .map_err(|error| postgres_error("commit PostgreSQL projection claim", error))?;
        Ok(generation)
    }

    async fn load(
        &self,
        projection_id: &ProjectionId,
    ) -> ProjectionResult<Option<ProjectionCursor>> {
        projection_id.validate()?;
        let rows = sqlx::query(&self.inner.sql.load)
            .bind(projection_id.name())
            .bind(projection_id.key())
            .fetch_all(self.ready_pool().await?)
            .await
            .map_err(|error| postgres_error("load PostgreSQL projection cursor", error))?;
        decode_cursor_rows(rows)
    }

    async fn save(
        &self,
        projection_id: &ProjectionId,
        generation: ProjectionGeneration,
        expected_position: Option<&CursorPosition>,
        cursor: ProjectionCursor,
    ) -> ProjectionResult<()> {
        let transaction = self
            .begin_exactly_once(projection_id, generation, expected_position)
            .await?;
        self.commit_exactly_once(transaction, projection_id, generation, &cursor)
            .await
    }

    async fn clear(
        &self,
        projection_id: &ProjectionId,
        generation: ProjectionGeneration,
    ) -> ProjectionResult<()> {
        projection_id.validate()?;
        let mut transaction = self
            .ready_pool()
            .await?
            .begin()
            .await
            .map_err(|error| postgres_error("begin PostgreSQL projection clear", error))?;
        self.lock_and_validate(&mut transaction, projection_id, generation, None, false)
            .await?;
        self.lock_generation_and_validate(&mut transaction, projection_id, generation)
            .await?;
        let generation = encode_generation(generation)?;
        sqlx::query(&self.inner.sql.clear_cursor)
            .bind(projection_id.name())
            .bind(projection_id.key())
            .bind(generation)
            .execute(&mut *transaction)
            .await
            .map_err(|error| postgres_error("clear PostgreSQL projection cursor", error))?;
        self.delete_boundary(&mut transaction, projection_id)
            .await?;
        transaction
            .commit()
            .await
            .map_err(|error| postgres_error("commit PostgreSQL projection clear", error))
    }

    async fn load_paused(&self, projection_id: &ProjectionId) -> ProjectionResult<bool> {
        projection_id.validate()?;
        let sql = format!(
            "SELECT paused FROM {} WHERE projection_name = $1 AND projection_key = $2",
            self.inner.sql.management
        );
        let row = sqlx::query(&sql)
            .bind(projection_id.name())
            .bind(projection_id.key())
            .fetch_optional(self.ready_pool().await?)
            .await
            .map_err(|error| postgres_error("load PostgreSQL projection management", error))?;
        row.map(|row| {
            row.try_get("paused")
                .map_err(|error| postgres_error("decode PostgreSQL paused flag", error))
        })
        .transpose()
        .map(Option::unwrap_or_default)
    }

    async fn set_paused(&self, projection_id: &ProjectionId, paused: bool) -> ProjectionResult<()> {
        projection_id.validate()?;
        let sql = format!(
            "INSERT INTO {} \
             (projection_name, projection_key, paused, last_updated) \
             VALUES ($1, $2, $3, \
                     floor(extract(epoch from clock_timestamp()) * 1000)::BIGINT) \
             ON CONFLICT (projection_name, projection_key) DO UPDATE \
             SET paused = EXCLUDED.paused, last_updated = EXCLUDED.last_updated",
            self.inner.sql.management
        );
        sqlx::query(&sql)
            .bind(projection_id.name())
            .bind(projection_id.key())
            .bind(paused)
            .execute(self.ready_pool().await?)
            .await
            .map_err(|error| postgres_error("save PostgreSQL projection management", error))?;
        Ok(())
    }

    fn postgres_store(&self) -> Option<&PostgresProjectionOffsetStore> {
        Some(self)
    }
}

fn decode_cursor_rows(rows: Vec<PgRow>) -> ProjectionResult<Option<ProjectionCursor>> {
    let Some(first) = rows.first() else {
        return Ok(None);
    };
    let position: Option<String> = first
        .try_get("current_offset")
        .map_err(|error| postgres_error("decode PostgreSQL projection position", error))?;
    let manifest: Option<String> = first
        .try_get("manifest")
        .map_err(|error| postgres_error("decode PostgreSQL provider manifest", error))?;
    let (position, manifest) = match (position, manifest) {
        (Some(position), Some(manifest)) => (position, manifest),
        (None, None) => {
            let mut has_boundary_member = false;
            for row in &rows {
                has_boundary_member |= boundary_row_has_member(row)?;
            }
            if has_boundary_member {
                return Err(ProjectionError::CursorCodec(
                    "PostgreSQL cursor boundary exists without a main cursor".into(),
                ));
            }
            return Ok(None);
        }
        _ => {
            return Err(ProjectionError::CursorCodec(
                "PostgreSQL cursor has only one of position and manifest".into(),
            ));
        }
    };

    let mut consumed = Vec::new();
    for row in &rows {
        let boundary_position: Option<String> = row
            .try_get("boundary_offset")
            .map_err(|error| postgres_error("decode PostgreSQL boundary position", error))?;
        let persistence_id: Option<String> = row
            .try_get("persistence_id")
            .map_err(|error| postgres_error("decode PostgreSQL boundary persistence id", error))?;
        let sequence_nr: Option<i64> = row
            .try_get("seq_nr")
            .map_err(|error| postgres_error("decode PostgreSQL boundary sequence", error))?;
        match (boundary_position, persistence_id, sequence_nr) {
            (None, None, None) => {}
            (Some(boundary_position), Some(persistence_id), Some(sequence_nr)) => {
                if boundary_position != position {
                    return Err(ProjectionError::CursorCodec(format!(
                        "PostgreSQL boundary position {boundary_position:?} disagrees with \
                         main cursor {position:?}"
                    )));
                }
                let sequence_nr = u64::try_from(sequence_nr).map_err(|_| {
                    ProjectionError::CursorCodec(format!(
                        "PostgreSQL boundary has negative sequence number {sequence_nr}"
                    ))
                })?;
                let persistence_id = PersistenceId::from_unique(persistence_id)
                    .map_err(|error| ProjectionError::CursorCodec(error.to_string()))?;
                consumed.push(BoundaryEvent::new(
                    persistence_id,
                    SequenceNr::new(sequence_nr),
                ));
            }
            _ => {
                return Err(ProjectionError::CursorCodec(
                    "PostgreSQL boundary row is partially null".into(),
                ));
            }
        }
    }
    Ok(Some(ProjectionCursor::new(
        ProviderManifest::new(manifest)
            .map_err(|error| ProjectionError::CursorCodec(error.to_string()))?,
        DeduplicationBoundary::with_consumed(position, consumed),
    )))
}

fn boundary_row_has_member(row: &PgRow) -> ProjectionResult<bool> {
    let persistence_id: Option<String> = row
        .try_get("persistence_id")
        .map_err(|error| postgres_error("decode PostgreSQL boundary persistence id", error))?;
    Ok(persistence_id.is_some())
}

fn decode_generation(row: &PgRow) -> ProjectionResult<ProjectionGeneration> {
    let generation: i64 = row
        .try_get("generation")
        .map_err(|error| postgres_error("decode PostgreSQL projection generation", error))?;
    let generation = u64::try_from(generation).map_err(|_| {
        ProjectionError::OffsetStore(format!(
            "PostgreSQL projection generation is negative: {generation}"
        ))
    })?;
    Ok(ProjectionGeneration::new(generation))
}

fn encode_generation(generation: ProjectionGeneration) -> ProjectionResult<i64> {
    i64::try_from(generation.as_u64()).map_err(|_| {
        ProjectionError::OffsetStore(format!(
            "PostgreSQL projection generation {} exceeds BIGINT",
            generation.as_u64()
        ))
    })
}

fn pool_options(config: &PostgresProjectionOffsetStoreConfig) -> PgPoolOptions {
    PgPoolOptions::new()
        .max_connections(config.max_connections)
        .min_connections(config.min_connections)
        .acquire_timeout(config.acquire_timeout)
        .idle_timeout(config.idle_timeout)
        .max_lifetime(config.max_lifetime)
}

async fn bootstrap(pool: &PgPool, lock_key: i64, ddl: &[String]) -> ProjectionResult<()> {
    let mut transaction = pool
        .begin()
        .await
        .map_err(|error| postgres_error("begin PostgreSQL projection schema bootstrap", error))?;
    sqlx::query("SELECT pg_advisory_xact_lock($1)")
        .bind(lock_key)
        .execute(&mut *transaction)
        .await
        .map_err(|error| {
            postgres_error(
                "lock PostgreSQL projection schema bootstrap namespace",
                error,
            )
        })?;
    for statement in ddl {
        sqlx::query(statement)
            .execute(&mut *transaction)
            .await
            .map_err(|error| {
                postgres_error("execute PostgreSQL projection schema bootstrap", error)
            })?;
    }
    transaction
        .commit()
        .await
        .map_err(|error| postgres_error("commit PostgreSQL projection schema bootstrap", error))
}

fn bootstrap_lock_key(config: &PostgresProjectionOffsetStoreConfig) -> i64 {
    const FNV_OFFSET_BASIS: u64 = 0xcbf2_9ce4_8422_2325;
    const FNV_PRIME: u64 = 0x0000_0100_0000_01b3;

    let mut hash = FNV_OFFSET_BASIS;
    for component in [
        "datum-projection-postgres-bootstrap-v1",
        config.schema.as_str(),
        config.tables.offset_store.as_str(),
        config.tables.timestamp_offset_store.as_str(),
        config.tables.management.as_str(),
    ] {
        for byte in component.bytes().chain(std::iter::once(0xff)) {
            hash ^= u64::from(byte);
            hash = hash.wrapping_mul(FNV_PRIME);
        }
    }
    hash as i64
}

fn validate_config(config: &PostgresProjectionOffsetStoreConfig) -> ProjectionResult<()> {
    validate_identifier("schema", &config.schema, 63)?;
    validate_identifier(
        "offset table",
        &config.tables.offset_store,
        MAX_TABLE_IDENTIFIER_BYTES,
    )?;
    validate_identifier(
        "timestamp-offset table",
        &config.tables.timestamp_offset_store,
        MAX_TABLE_IDENTIFIER_BYTES,
    )?;
    validate_identifier(
        "management table",
        &config.tables.management,
        MAX_TABLE_IDENTIFIER_BYTES,
    )?;
    if config.max_connections == 0 {
        return Err(ProjectionError::invalid_config(
            "postgres.max_connections",
            "max_connections must be greater than zero",
        ));
    }
    if config.min_connections > config.max_connections {
        return Err(ProjectionError::invalid_config(
            "postgres.min_connections",
            format!(
                "min_connections {} exceeds max_connections {}",
                config.min_connections, config.max_connections
            ),
        ));
    }
    if config.acquire_timeout.is_zero() {
        return Err(ProjectionError::invalid_config(
            "postgres.acquire_timeout",
            "acquire_timeout must be greater than zero",
        ));
    }
    Ok(())
}

fn validate_identifier(name: &'static str, value: &str, max_bytes: usize) -> ProjectionResult<()> {
    let mut characters = value.chars();
    let valid_first = characters
        .next()
        .is_some_and(|character| character == '_' || character.is_ascii_alphabetic());
    if !valid_first
        || !characters.all(|character| character == '_' || character.is_ascii_alphanumeric())
        || value.len() > max_bytes
    {
        return Err(ProjectionError::invalid_config(
            name,
            format!(
                "invalid PostgreSQL identifier {value:?}; expected an ASCII identifier \
                 of at most {max_bytes} bytes"
            ),
        ));
    }
    Ok(())
}

fn quote_identifier(identifier: &str) -> String {
    format!("\"{identifier}\"")
}

fn qualified(schema: &str, table: &str) -> String {
    format!("{}.{}", quote_identifier(schema), quote_identifier(table))
}

fn postgres_error(context: &str, error: sqlx::Error) -> ProjectionError {
    ProjectionError::OffsetStore(format!("{context}: {error}"))
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn config_is_lazy_redacts_url_and_uses_akka_shaped_tables() {
        let config = PostgresProjectionOffsetStoreConfig::new(
            "postgres://datum:secret@example.invalid/datum",
        );
        let store = PostgresProjectionOffsetStore::new(config.clone())
            .expect("default PostgreSQL projection config is valid");
        assert!(store.pool().is_none());
        assert!(!format!("{config:?}").contains("secret"));
        assert_eq!(
            config.tables,
            PostgresProjectionTableNames {
                offset_store: "datum_projection_offset_store".into(),
                timestamp_offset_store: "datum_projection_timestamp_offset_store".into(),
                management: "datum_projection_management".into(),
            }
        );
        let ddl = store.schema_ddl();
        assert!(ddl.contains("current_offset TEXT"));
        assert!(ddl.contains("generation BIGINT"));
        assert!(ddl.contains("persistence_id TEXT"));
        assert!(ddl.contains("paused BOOLEAN"));
    }
}