EnvelopeFlow.Preferences.Stores.Postgres
0.1.0-alpha.3.1
dotnet add package EnvelopeFlow.Preferences.Stores.Postgres --version 0.1.0-alpha.3.1
NuGet\Install-Package EnvelopeFlow.Preferences.Stores.Postgres -Version 0.1.0-alpha.3.1
<PackageReference Include="EnvelopeFlow.Preferences.Stores.Postgres" Version="0.1.0-alpha.3.1" />
<PackageVersion Include="EnvelopeFlow.Preferences.Stores.Postgres" Version="0.1.0-alpha.3.1" />
<PackageReference Include="EnvelopeFlow.Preferences.Stores.Postgres" />
paket add EnvelopeFlow.Preferences.Stores.Postgres --version 0.1.0-alpha.3.1
#r "nuget: EnvelopeFlow.Preferences.Stores.Postgres, 0.1.0-alpha.3.1"
#:package EnvelopeFlow.Preferences.Stores.Postgres@0.1.0-alpha.3.1
#addin nuget:?package=EnvelopeFlow.Preferences.Stores.Postgres&version=0.1.0-alpha.3.1&prerelease
#tool nuget:?package=EnvelopeFlow.Preferences.Stores.Postgres&version=0.1.0-alpha.3.1&prerelease
PostgreSQL recipient policy
This feature exposes an immutable Postgres*Migration*.Catalog for explicit schema inspection/planning/adoption with an application-owned PostgresMigrationRunner. See upgrade operations. Existing initializers stay explicit; construction and worker startup never apply migrations. Select only the installed feature catalog, independently from conversation/history storage.
This optional package implements IRecipientPolicyStore and IRecipientPolicyRetentionStore. Applications supply and own the NpgsqlDataSource, authorization, recipient identifiers, signing keys and submission guards. It installs no endpoints or user schema, and does not depend on conversations, audience storage or a provider.
Catalog version 2 records reservation category/channel dimensions and supports the weekly quiet windows and simultaneous global/category/channel caps in RecipientPolicy. Every matching cap is checked in one SQL round trip while holding the same scoped recipient row lock; one accepted logical delivery creates one reservation. Existing reservations retain unknown dimensions and conservatively count toward every matching cap until expiry. They are never assigned guessed categories. New reservations bind category/channel to the delivery ID and reject changed replay dimensions.
Apply the versioned catalog or call explicit InitializeAsync during a maintenance window after stopping older binaries. Initialization atomically adopts a validated unversioned version-1 schema and applies the pending migration. Historical version-1 SQL and policy documents remain supported. Documents containing richer rules use format 2 so old readers reject them instead of silently ignoring limits; legacy-only documents stay format 1 and omit the optional fields. Mixed old/new binaries are unsupported because the reservation layout changes.
It also implements optional IRecipientPolicyEvaluationStore. EvaluateAsync uses a read-only repeatable-read transaction to evaluate one coherent policy/reservation snapshot, including UTC quiet-hour and frequency retry floors. It never creates a recipient, edits policy, prunes rows or reserves a slot. Another client can edit policy after that snapshot; always use final TryAdmitAsync for current atomic reservation. Rich preflight accounts for reduced frequency maxima by waiting until enough reservations expire, combines quiet/frequency floors, and checks quiet eligibility at the candidate. The existing Boolean path is unchanged. Quiet-hour release conventions, including DST gap/overlap and all-day windows, are documented in EnvelopeFlow.Preferences.
Optional IRecipientPolicyReservationStore.TryReserveAsync provides a rich final atomic check under the same recipient-row lock. It combines current quiet/frequency floors before writing a reservation, so newly quiet or rate-limited work can defer without spending a slot. Only Allowed inserts or reuses the scoped logical-delivery reservation. The non-reserving preflight cannot grant permission against a later opt-out; final policy and live installation/claim checks remain necessary. Neither this capability nor its store makes scheduling durable by itself.
await using var dataSource = NpgsqlDataSource.Create(connectionString);
var policies = new PostgresRecipientPolicyStore(dataSource);
await policies.InitializeAsync(cancellationToken); // Explicit installation before serving requests.
IRecipientPolicyStore policyStore = policies;
var tokens = new SuppressionTokens(applicationSigningKey, policyStore, trustedTimeProvider);
Initialization is transactional and serialized across clients. Initial schema version 1 and policy-document version 1 are checked explicitly; unknown versions require an application-controlled upgrade. Initialization creates tables in the data source's configured search path. It does not silently upgrade or replace existing policies.
GetAsync returns the default revision-zero policy for an unknown recipient. SetAsync compares the expected revision under a recipient-row lock; exactly one competing edit can win. Returned policies are immutable copies, with the existing bounded permission, timezone, quiet-hour and frequency validation. Category/channel explicit denial also applies to RequiresConsent=false; list membership, contact addresses and browser permission confer no consent.
Admission and suppression consumption create the recipient row as needed, then lock it in one transaction. That lock serializes current policy evaluation, revision edits, reservation insertion and one-time token consumption across independent processes. A successful suppression consumption updates only the supplied category/channel and increments the revision together with its replay record. A failed or canceled transaction rolls back both policy changes and operational records. Token authentication belongs to SuppressionTokens or the application's equivalent; the store accepts already-authorized consumption data.
TryAdmitAsync preserves the existing Boolean policy semantics. Suppression, missing consent and quiet hours deny admission before reserving a frequency slot. Repeated delivery IDs still recheck live policy and reuse their retained reservation. An allowed decision reserves before provider I/O; failed, abandoned or uncertain provider attempts conservatively retain that reservation. This package supplies neither refunds nor durable deferral. Installing it does not change the behavior of the legacy Boolean submission guard. Checks cannot retract a send already in flight.
Time is explicit: all admission, expiry and cleanup methods use the supplied trusted DateTimeOffset, never database time. Hosts must supply one authoritative clock across workers and must not accept client-provided dates. UTC .NET ticks are persisted as bigint, retaining 100-nanosecond boundaries and avoiding PostgreSQL timestamp rounding. Quiet hours use the current policy timezone at that instant, including repeated DST instants. Frequency retry instants use the oldest active reservation plus the current window. A later longer frequency window still sees earlier retained reservations.
Every key includes tenant → project → environment → app and recipient. Length-framed, strict UTF-8 SHA-256 keys keep B-tree entries bounded even when all identifiers contain the maximum Unicode length. Original keys remain stored and are checked on reads; case, composed/decomposed Unicode and field boundaries remain distinct. Invalid surrogate sequences are rejected explicitly because PostgreSQL text requires well-formed Unicode. No identifiers, policy contents or token material are logged.
The tables are ef_recipient_policy_schema, ef_recipient_policies, ef_policy_frequency_reservations and ef_policy_suppression_tokens. Compact recipient/scope hashes index policies; per-recipient timestamp and primary-key indexes support admission, replay and cleanup. The policy document carries its own format version. This initial installer is not general schema-migration tooling.
Reservations are retained for at least the maximum supported frequency window, 30 days; per-delivery reservation idempotency is bounded to that horizon. Tokens are retained until their original expiry. Admission/consumption may prune expired rows for their own locked recipient. Use the optional explicit CleanupAsync(scope, trustedNow, batchSize) for broader cleanup: batches 1–1000 delete at most that many operational rows total, keep every policy and all live reservations/tokens, preserve complete scope, and skip recipients currently locked by another client. Selection is stable by recipient hash, record kind and item hash; a zero result can mean no eligible rows or busy recipients. Never schedule cleanup with an untrusted future date or extend an already-consumed token's original expiry. Expired signed tokens remain invalid after their replay record is removed.
PostgresRecipientPolicyOptions defaults to 10,000 reservations and 10,000 unexpired tokens per recipient, independently configurable from 1 to 1,000,000. Reaching capacity fails closed and rolls back the transaction. Configure capacity for the workload and invoke retention deliberately; no scheduled task is installed.
Focused tests use two independently pooled database clients, task-owned schemas, the reusable recipient-policy contract, reopened clients, revision/frequency races, real SQL rollback, cancellation, Unicode keys, serialization, signed tokens, exact boundaries and cleanup. Set ENVELOPEFLOW_TEST_POSTGRES and run:
dotnet test tests/Preferences/EnvelopeFlow.Preferences.Stores.Postgres.Tests/EnvelopeFlow.Preferences.Stores.Postgres.Tests.csproj -c Release
An absent connection string is recorded as inconclusive, not live database evidence. Reopened-client checks prove committed state is durable across adapter/data-source lifetimes; they do not simulate a process kill, database crash, failover or provider delivery.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net11.0 is compatible. |
-
net11.0
- EnvelopeFlow.Preferences (>= 0.1.0-alpha.3.1)
- EnvelopeFlow.Stores.Migrations.Postgres (>= 0.1.0-alpha.3.1)
- Npgsql (>= 10.0.3)
NuGet packages (2)
Showing the top 2 NuGet packages that depend on EnvelopeFlow.Preferences.Stores.Postgres:
| Package | Downloads |
|---|---|
|
EnvelopeFlow.Integrations.Feedback.Stores.Postgres
Atomic durable provider feedback correlation, recipient policy effects and replay retention. |
|
|
EnvelopeFlow.Throttling.Stores.Postgres
Optional PostgreSQL provider budgets and atomic provider/policy/delivery submission admission. |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 0.1.0-alpha.3.1 | 50 | 11/22/2026 |
| 0.1.0-alpha.2.1 | 66 | 11/9/2026 |