Skip to content

Use ReadOnly transaction when managing replication slots. #548

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions postgresql/helpers.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package postgresql

import (
"context"
"database/sql"
"fmt"
"log"
Expand Down Expand Up @@ -368,10 +369,10 @@ func setToPgIdentSimpleList(idents *schema.Set) string {
return strings.Join(quotedIdents, ",")
}

// startTransaction starts a new DB transaction on the specified database.
// startTransactionWithOpts starts a new DB transaction on the specified database.
// If the database is specified and different from the one configured in the provider,
// it will create a new connection pool if needed.
func startTransaction(client *Client, database string) (*sql.Tx, error) {
// it will create a new connection pool if needed
func startTransactionWithOpts(client *Client, database string, opts *sql.TxOptions) (*sql.Tx, error) {
if database != "" && database != client.databaseName {
client = client.config.NewClient(database)
}
Expand All @@ -380,14 +381,22 @@ func startTransaction(client *Client, database string) (*sql.Tx, error) {
return nil, err
}

txn, err := db.Begin()
txn, err := db.BeginTx(context.Background(), opts)
if err != nil {
return nil, fmt.Errorf("could not start transaction: %w", err)
}

return txn, nil
}

func startTransaction(client *Client, database string) (*sql.Tx, error) {
return startTransactionWithOpts(client, database, nil)
}

func startReadOnlyTransaction(client *Client, database string) (*sql.Tx, error) {
return startTransactionWithOpts(client, database, &sql.TxOptions{ReadOnly: true})
}

func dbExists(db QueryAble, dbname string) (bool, error) {
err := db.QueryRow("SELECT datname FROM pg_database WHERE datname=$1", dbname).Scan(&dbname)
switch {
Expand Down
8 changes: 4 additions & 4 deletions postgresql/resource_postgresql_replication_slot.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func resourcePostgreSQLReplicationSlotCreate(db *DBConnection, d *schema.Resourc
plugin := d.Get("plugin").(string)
databaseName := getDatabaseForReplicationSlot(d, db.client.databaseName)

txn, err := startTransaction(db.client, databaseName)
txn, err := startReadOnlyTransaction(db.client, databaseName)
if err != nil {
return err
}
Expand Down Expand Up @@ -83,7 +83,7 @@ func resourcePostgreSQLReplicationSlotExists(db *DBConnection, d *schema.Resourc
return false, err
}

txn, err := startTransaction(db.client, database)
txn, err := startReadOnlyTransaction(db.client, database)
if err != nil {
return false, err
}
Expand Down Expand Up @@ -111,7 +111,7 @@ func resourcePostgreSQLReplicationSlotReadImpl(db *DBConnection, d *schema.Resou
return err
}

txn, err := startTransaction(db.client, database)
txn, err := startReadOnlyTransaction(db.client, database)
if err != nil {
return err
}
Expand Down Expand Up @@ -144,7 +144,7 @@ func resourcePostgreSQLReplicationSlotDelete(db *DBConnection, d *schema.Resourc
replicationSlotName := d.Get("name").(string)
database := getDatabaseForReplicationSlot(d, db.client.databaseName)

txn, err := startTransaction(db.client, database)
txn, err := startReadOnlyTransaction(db.client, database)
if err != nil {
return err
}
Expand Down