Skip to content

An idempotent script cannot carry a statement with the transaction suppressed on PostgreSQL: the migration is recorded without its index #155

Open

Description

@CaffeinatedCoder

Before preview.2: #27 brings concurrently:. Measured in spike D (#19, spikes/D.ScriptsAndBundles).

Problem

CREATE INDEX CONCURRENTLY, written with sql.Statement(…, suppressTransaction: true), applies from a migrations bundle and from migrations script. In migrations script --idempotent EF Core wraps it in a DO block like every other statement, and PostgreSQL 16 and 18 refuse it there:

DO $EF$
BEGIN
    IF NOT EXISTS(SELECT 1 FROM "__EFMigrationsHistory" WHERE "MigrationId" = '20261116125314_Initial') THEN
    CREATE INDEX CONCURRENTLY ix_things_name ON "Things" ("Name");
    END IF;
END $EF$;
ERROR:  CREATE INDEX CONCURRENTLY cannot be executed from a function   (25001)

psql goes on and exits 0. The block after it records the migration, so the database counts the migration as applied, has no index, and no later run of the script creates one. With ON_ERROR_STOP=1 the script stops with the table committed and the migration not recorded, and the second run fails on CREATE TABLE (42P07). The script cannot be applied either way.

Nothing that ships emits such a statement today. PgIndex's concurrently: will.

Change, to decide

The rule of #19 is that a combination the provider cannot wrap is refused at scaffold, with the declaration named. The spike measured two things about where that can stand:

  • Scaffold does not know how a migration will be applied. The migration that fails in the idempotent script is the one a bundle and the plain script apply. Refused at scaffold, concurrently: is refused for those paths as well.
  • The generator is told. SchemaKitSqlGenerator.Generate receives MigrationsSqlGenerationOptions.Idempotent together with the operations, and the operation says SuppressTransaction. A refusal there fails migrations script --idempotent and nothing else. To name the declaration, the operation has to say which object it is: point 4 of The plan as ordered actions with a name ledger, not two lists around EF Core's operations #115, and the same spike measured that an annotation on a SqlOperation reaches the generator on every path.

Open: which of the two, and whether the rule is the kernel's or the PostgreSQL package's. "Suppressed and idempotent" is not a failure on every provider: on SQL Server 2022, ALTER DATABASE CURRENT SET ALLOW_SNAPSHOT_ISOLATION ON, which the server refuses inside a transaction (Msg 226), applies from the idempotent script twice with the transaction suppressed, and fails on every path without the suppression. (An earlier version of this issue rested that on CREATE INDEX … WITH (ONLINE = ON), which proves nothing: SQL Server builds such an index inside a transaction too.)

Done when

migrations script --idempotent over a migration that holds a statement with the transaction suppressed fails on Npgsql, naming the object, and writes no script. The same migration still applies from a bundle and from migrations script. The consumer smoke test holds both on PostgreSQL 16 and 18.

Activity

  1. added this to the 11.0.0 milestone on Nov 16, 2026
  2. added
    enhancement
    package/core
    package/postgresql
    on Nov 16, 2026
  3. added
    area/migrations
    on Nov 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/migrationsenhancementpackage/corepackage/postgresql

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions