From 83a9c07b0799ed22e65a12cc2e3ffed93d3ed1da Mon Sep 17 00:00:00 2001 From: Adelin Niculae <43118261+adelinn@users.noreply.github.com> Date: Tue, 1 Sep 2026 17:48:21 +0300 Subject: [PATCH 1/2] Add transaction handling for PostgreSQL schema creation Implement transaction handling for schema creation with a fallback mechanism. --- postgresql/resource_postgresql_schema.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/postgresql/resource_postgresql_schema.go b/postgresql/resource_postgresql_schema.go index 508fbc33..66a55c22 100644 --- a/postgresql/resource_postgresql_schema.go +++ b/postgresql/resource_postgresql_schema.go @@ -118,6 +118,26 @@ func resourcePostgreSQLSchema() *schema.Resource { func resourcePostgreSQLSchemaCreate(db *DBConnection, d *schema.ResourceData) error { database := getDatabase(d, db.client.databaseName) + + // Try without roles workaround first + txn1, err := startTransaction(db.client, database) + if err != nil { + return err + } + defer deferredRollback(txn1) + + if err := createSchema(db, txn1, d); err != nil { + return err + } + + if err := txn1.Commit(); err == nil { + // Creating worked so we return here + d.SetId(generateSchemaID(d, database)) + + return resourcePostgreSQLSchemaReadImpl(db, d) + } + + // Creating without workaround failed so we fallback to use it. txn, err := startTransaction(db.client, database) if err != nil { return err From 28c5fd0096b4302c016183da2e56699f8ad355ee Mon Sep 17 00:00:00 2001 From: Adelin Niculae <43118261+adelinn@users.noreply.github.com> Date: Tue, 1 Sep 2026 17:57:17 +0300 Subject: [PATCH 2/2] Better handle fallback Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- postgresql/resource_postgresql_schema.go | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/postgresql/resource_postgresql_schema.go b/postgresql/resource_postgresql_schema.go index 66a55c22..a8e4d510 100644 --- a/postgresql/resource_postgresql_schema.go +++ b/postgresql/resource_postgresql_schema.go @@ -124,17 +124,18 @@ func resourcePostgreSQLSchemaCreate(db *DBConnection, d *schema.ResourceData) er if err != nil { return err } - defer deferredRollback(txn1) + attemptErr := createSchema(db, txn1, d) + if attemptErr == nil { + if err := txn1.Commit(); err == nil { + // Creating worked so we return here + d.SetId(generateSchemaID(d, database)) - if err := createSchema(db, txn1, d); err != nil { - return err + return resourcePostgreSQLSchemaReadImpl(db, d) + } } - if err := txn1.Commit(); err == nil { - // Creating worked so we return here - d.SetId(generateSchemaID(d, database)) - - return resourcePostgreSQLSchemaReadImpl(db, d) + if err := txn1.Rollback(); err != nil && err != sql.ErrTxDone { + return fmt.Errorf("error rolling back initial schema transaction: %w", err) } // Creating without workaround failed so we fallback to use it.