Class PostgreSqlFlowOrchestratorMigrator
- Namespace
- FlowOrchestrator.PostgreSQL
- Assembly
- FlowOrchestrator.PostgreSQL.dll
Hosted service that runs idempotent PostgreSQL schema migrations at startup.
Creates the flow_definitions, flow_runs, flow_steps,
flow_step_attempts, flow_outputs, and related tables if they do not already exist.
Safe to run on every startup — all statements use IF NOT EXISTS guards.
public sealed class PostgreSqlFlowOrchestratorMigrator : IHostedService, IDisposable
- Inheritance
-
PostgreSqlFlowOrchestratorMigrator
- Implements
- Inherited Members
Remarks
Run search (2.0). With pg_trgm available, a trigram GIN index on the lower-cased run
search expression (ix_flow_runs_run_search_trgm, about 47 MB per 200k runs) is built
CONCURRENTLY in the background after startup: no table rewrite, writers are not blocked and
startup does not wait. An index left INVALID by an interrupted build is dropped and rebuilt on
the next start. Without pg_trgm (not allow-listed on Azure Database for PostgreSQL, or no
privilege) every trigram step is skipped with a warning and search runs the same SQL as a
sequential scan.
Open-step index (2.0, #189 P0-1). ix_flow_steps_run_id_open, a partial index on the
Running and Pending rows of flow_steps, is built the same way — CONCURRENTLY, in the background,
before the run-search index, with the same in-flight detection and INVALID-leftover repair. Until it
exists the completion gate's open-row check reads each run's rows through the primary key: correct, only
slower.
Starting beside running hosts. Every index and column statement is guarded by a catalog check, so a migration on an up-to-date schema requests no lock on any table. A second replica (or a restarted host) can therefore start during a rolling deployment, or while the run-search index is still being built, without stalling writers or deadlocking with the build; and it leaves another host's in-flight build alone instead of dropping and rebuilding it.
The flow_steps.output_json trigram index stays on by default: no measurable write cost at
p50 and 200x faster deep search in the default window, at about half the size of the step table.
Constructors
PostgreSqlFlowOrchestratorMigrator(string, ILogger<PostgreSqlFlowOrchestratorMigrator>)
Creates the migrator.
public PostgreSqlFlowOrchestratorMigrator(string connectionString, ILogger<PostgreSqlFlowOrchestratorMigrator> logger)
Parameters
connectionStringstringThe FlowOrchestrator database.
loggerILogger<PostgreSqlFlowOrchestratorMigrator>Logger.
PostgreSqlFlowOrchestratorMigrator(string, ILogger<PostgreSqlFlowOrchestratorMigrator>, bool)
Creates the migrator.
public PostgreSqlFlowOrchestratorMigrator(string connectionString, ILogger<PostgreSqlFlowOrchestratorMigrator> logger, bool runSearchIndexRequested)
Parameters
connectionStringstringThe FlowOrchestrator database.
loggerILogger<PostgreSqlFlowOrchestratorMigrator>Logger.
runSearchIndexRequestedboolUseRunSearchIndex()was called. The token index is not implemented on PostgreSQL (the trigram indexes already answer deep search); the migrator says so once instead of ignoring it silently.
Methods
Dispose()
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
public void Dispose()
StartAsync(CancellationToken)
Applies the schema; starts the background run-search index build when pg_trgm is available.
public Task StartAsync(CancellationToken cancellationToken)
Parameters
cancellationTokenCancellationTokenHost start-up cancellation.
Returns
StopAsync(CancellationToken)
Cancels a background index build that is still running (PostgreSQL leaves it INVALID; the next start rebuilds it).
public Task StopAsync(CancellationToken cancellationToken)
Parameters
cancellationTokenCancellationTokenHost shutdown budget.