Losing Postgres connection on deploy of Rails app

I have a 2 machine setup that deploys a Rails app with the bluegreen strategy. Recently, I’ve been getting the following errors whenever I deploy. I feel like this wasn’t an issue when I was only on one machine. How would I go about fixing this?

ActiveRecord::StatementInvalid

PG::ConnectionBad: PQconsumeInput() server closed the connection unexpectedly This probably means the server terminated abnormally before or while processing the request.
PG::ConnectionBad

PQconsumeInput() server closed the connection unexpectedly This probably means the server terminated abnormally before or while processing the request.

Here’s my fly file:

app = "my-rails-api"
kill_signal = "SIGINT"
kill_timeout = 5
processes = []

[build]
  [build.args]
    BUILD_COMMAND = "bin/rails fly:build"
    SERVER_COMMAND = "bin/rails fly:server"

[deploy]
  release_command = "bin/rails fly:release"
  strategy = "bluegreen"

[env]
  PORT = "8080"

[experimental]
  auto_rollback = true

[[services]]
  http_checks = []
  internal_port = 8080
  processes = ["app"]
  protocol = "tcp"
  script_checks = []
  auto_start_machines = false
  auto_stop_machines = false
  min_machines_running = 1

  [services.concurrency]
    hard_limit = 100
    soft_limit = 90
    type = "connections"

  [[services.ports]]
    force_https = true
    handlers = ["http"]
    port = 80

  [[services.ports]]
    handlers = ["tls", "http"]
    port = 443

  [[services.tcp_checks]]
    grace_period = "1s"
    interval = "15s"
    restart_limit = 0
    timeout = "2s"

[[statics]]
  guest_path = "/app/public"
  url_prefix = "/"

From the ActiveRecord::StatementInvalid error, it seems that something went wrong with activerecord executing a sql statement. If there were no changes to the code, I’d assume there’s something happening with how your app is connecting to your postgres instance.

Can you c/p your logs?

fly logs -a my-postgres-app-db

Alternatively, you can also try running healthchecks to see if there’s anything amiss with either of the postgres instances

fly checks list -a my-postgres-app-db

Thank you so much for the response @shortdiv. I discovered that my Postgres server was out of memory, so I bumped it up and the errors stopped!

Guessing it was something to do with some memory buffers and perhaps queueing queries.

Thanks again!

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.