Skip to content

Commit

Permalink
Retry dropping slot on errors
Browse files Browse the repository at this point in the history
Postgres will error when attempting to drop a slot while it's actively
being used. This can present a problem when a single Terraform plan will
drop the replication slot as well as stopping the service using it and
thus make the slot inactive.In that scenario we'd like to keep trying to
drop the slot until the service has come down.

See [terraform docs][1] for how to override the default configuration of
retrying up to one minute.

[1]: https://www.terraform.io/docs/configuration/resources.html#operation-timeouts
  • Loading branch information
Jamie English committed Jan 24, 2020
1 parent e31b316 commit 491a7f2
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions postgresreplication/resource_replication_slot.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package postgresreplication
import (
"fmt"
"net/url"
"time"

"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/jackc/pgx"
"github.com/pkg/errors"
Expand Down Expand Up @@ -44,6 +46,9 @@ func resourceReplicationSlot() *schema.Resource {
Description: "The name of the database this slot is associated with.",
},
},
Timeouts: &schema.ResourceTimeout{
Delete: schema.DefaultTimeout(1 * time.Minute),
},
}
}

Expand Down Expand Up @@ -131,16 +136,18 @@ func resourceReplicationSlotRead(d *schema.ResourceData, m interface{}) error {
}

func resourceReplicationSlotDelete(d *schema.ResourceData, m interface{}) error {
replConn, err := connect(d, m)
if err != nil {
return err
}
defer replConn.Close()
return resource.Retry(d.Timeout(schema.TimeoutDelete), func() *resource.RetryError {
replConn, err := connect(d, m)
if err != nil {
return resource.NonRetryableError(err)
}
defer replConn.Close()

err = replConn.DropReplicationSlot(d.Id())
if err != nil {
return errors.Wrap(err, "error dropping replication slot.")
}
err = replConn.DropReplicationSlot(d.Id())
if err != nil {
return resource.RetryableError(errors.Wrap(err, "error dropping replication slot."))
}

return nil
return nil
})
}

0 comments on commit 491a7f2

Please sign in to comment.