https://github.com/sharpvik/pgmaster
Find your PostgreSQL master node with a single function call
https://github.com/sharpvik/pgmaster
Last synced: 7 months ago
JSON representation
Find your PostgreSQL master node with a single function call
- Host: GitHub
- URL: https://github.com/sharpvik/pgmaster
- Owner: sharpvik
- License: mit
- Created: 2025-02-17T21:01:52.000Z (11 months ago)
- Default Branch: main
- Last Pushed: 2025-02-17T22:28:28.000Z (11 months ago)
- Last Synced: 2025-03-24T00:57:55.604Z (10 months ago)
- Language: Go
- Size: 12.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# `pgmaster`
Find your PostgreSQL master node with a single function call.
## Why?
I had a PostgreSQL cluster with multiple nodes that would rotate the _master_
title during regular maintenance and on master node failure. I also had a few
CRON jobs that relied on getting the _read-write_ connection and they would fail
otherwise.
I needed to make sure that they find the right _master_ on init. So I wrote this
library.
## Usage
```go
func main() {
const timeout = 5 * time.Second
master, err := pgmaster.Find(connect, timeout, []string{
"abc.db.example.net",
"def.db.example.net",
})
// ... use master
}
// This is just an example of what it might look like.
// Use your own connection settings in practice.
func connect(host string) (*sql.DB, error) {
const port = 5432
return sql.Open("postgres", connectionString(host, port))
}
```
**NOTE:** the `timeout` is going to apply to each host separately and it only
really matters if some of the hosts become unavailable. For instance, with the
example timeout of 5 seconds, the longest it will take `pgmaster.Find` to check
2 hosts is 5 seconds x 2 hosts = 10 seconds. Under normal circumstances, it is
not going to take that long, because all it takes to check for a master node is
running a simple `SELECT`.