Skip to content
Merged
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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,4 @@ jobs:
run: echo ${{ steps.docker_build_14.outputs.digest }}
-
name: Postgres 15 Image digest
run: echo ${{ steps.docker_build_15.outputs.digest }}
run: echo ${{ steps.docker_build_15.outputs.digest }}
22 changes: 22 additions & 0 deletions .github/workflows/pre-commit.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: pre-commit

on:
pull_request:
push:
branches: [master]

jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v3
with:
go-version-file: "go.mod"
- uses: pre-commit/[email protected]
with:
extra_args: --all-files --hook-stage=manual
- uses: golangci/golangci-lint-action@master
with:
version: v1.49.0
- run: go mod download
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
.idea/
.idea/
125 changes: 125 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
issues:
# List of regexps of issue texts to exclude, empty list by default.
# But independently from this option we use default exclude patterns,
# it can be disabled by `exclude-use-default: false`. To list all
# excluded by default patterns execute `golangci-lint run --help`

exclude-rules:
# Exclude gosimple bool check
- linters:
- gosimple
text: "S(1002|1008|1021)"
# Exclude failing staticchecks for now
- linters:
- staticcheck
text: "SA(1006|1019|4006|4010|4017|5007|6005|9004):"
# Exclude lll issues for long lines with go:generate
- linters:
- lll
source: "^//go:generate "

# Maximum issues count per one linter. Set to 0 to disable. Default is 50.
max-issues-per-linter: 0

# Maximum count of issues with the same text. Set to 0 to disable. Default is 3.
max-same-issues: 0

linters:
disable-all: true
enable:
# - gofumpt
# - goimports
- gosimple
- govet
- ineffassign
- staticcheck
- unconvert
- unused
fast: true

# options for analysis running
run:
go: "1.19"

# default concurrency is a available CPU number
concurrency: 4

# timeout for analysis, e.g. 30s, 5m, default is 1m
timeout: 10m

# exit code when at least one issue was found, default is 1
issues-exit-code: 1

# include test files or not, default is true
tests: true

# list of build tags, all linters use it. Default is empty list.
#build-tags:
# - mytag

# which dirs to skip: issues from them won't be reported;
# can use regexp here: generated.*, regexp is applied on full path;
# default value is empty list, but default dirs are skipped independently
# from this option's value (see skip-dirs-use-default).
#skip-dirs:
# - src/external_libs
# - autogenerated_by_my_lib

# default is true. Enables skipping of directories:
# vendor$, third_party$, testdata$, examples$, Godeps$, builtin$
skip-dirs-use-default: true

# which files to skip: they will be analyzed, but issues from them
# won't be reported. Default value is empty list, but there is
# no need to include all autogenerated files, we confidently recognize
# autogenerated files. If it's not please let us know.
skip-files:
- ".*\\.hcl2spec\\.go$"
- "docstrings/gen.go"
# - lib/bad.go

# by default isn't set. If set we pass it to "go list -mod={option}". From "go help modules":
# If invoked with -mod=readonly, the go command is disallowed from the implicit
# automatic updating of go.mod described above. Instead, it fails when any changes
# to go.mod are needed. This setting is most useful to check that go.mod does
# not need updates, such as in a continuous integration and testing system.
# If invoked with -mod=vendor, the go command assumes that the vendor
# directory holds the correct copies of dependencies and ignores
# the dependency descriptions in go.mod.
# modules-download-mode: vendor

# output configuration options
output:
# colored-line-number|line-number|json|tab|checkstyle|code-climate, default is "colored-line-number"
format: colored-line-number

# print lines of code with issue, default is true
print-issued-lines: true

# print linter name in the end of issue text, default is true
print-linter-name: true

# make issues output unique by line, default is true
uniq-by-line: true

# all available settings of specific linters
linters-settings:
gofumpt:
module-path: github.com/fly-apps/postgres-flex
errcheck:
# report about not checking of errors in type assetions: `a := b.(MyStruct)`;
# default is false: such cases aren't reported by default.
check-type-assertions: false

# report about assignment of errors to blank identifier: `num, _ := strconv.Atoi(numStr)`;
# default is false: such cases aren't reported by default.
check-blank: false

# [deprecated] comma-separated list of pairs of the form pkg:regex
# the regex is used to ignore names within pkg. (default "fmt:.*").
# see https://github.com/kisielk/errcheck#the-deprecated-method for details
ignore: fmt:.*,io/ioutil:^Read.*,io:Close

# path to a file containing a list of functions to exclude from checking
# see https://github.com/kisielk/errcheck#excluding-functions for details
#exclude: /path/to/file.txt
18 changes: 18 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.3.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
- id: check-added-large-files
- repo: https://github.com/golangci/golangci-lint
rev: v1.50.0
hooks:
- id: golangci-lint
# pre-commit github action runs as "manual" hook
# because golangci-lint's action is more useful than
# the pre-commit hook in that case
stages: [commit]
3 changes: 1 addition & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ FROM wrouesnel/postgres_exporter:latest AS postgres_exporter

FROM postgres:${PG_VERSION}
ENV PGDATA=/data/postgresql
ARG VERSION
ARG VERSION
ARG PG_MAJOR_VERSION

LABEL fly.app_role=postgres_cluster
Expand All @@ -42,4 +42,3 @@ EXPOSE 5432


CMD ["start"]

9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ To get started, run the following:
fly version update

# Provision a 3 member cluster
fly pg create --name <app-name> --initial-cluster-size 3 --region ord --repmgr
fly pg create --name <app-name> --initial-cluster-size 3 --region ord --repmgr
```

## High Availability
To ensure High Availability, it's recommended that your cluster has at least 3 members.
To ensure High Availability, it's recommended that your cluster has at least 3 members.

Automatic failovers will only consider members residing within your primary region. The primary region is represented as an environment variable defined within the `fly.toml` file. That being said, if you're running a 3 member setup at least 2 of the members should reside within your primary region.
Automatic failovers will only consider members residing within your primary region. The primary region is represented as an environment variable defined within the `fly.toml` file. That being said, if you're running a 3 member setup at least 2 of the members should reside within your primary region.

## Horizontal scaling
Use the clone command to scale up your cluster.
Expand All @@ -30,7 +30,7 @@ fly machines clone <machine-id> --region <target-region>
```

## Staying up-to-date!
This project is in active development so it's important to stay current with the latest changes and bug fixes.
This project is in active development so it's important to stay current with the latest changes and bug fixes.

```
# Use the following command to verify you're on the latest version.
Expand All @@ -46,4 +46,3 @@ Create an issue or ask a question here: https://community.fly.io/

## Contributing
If you're looking to get involved, fork the project and send pull requests.

2 changes: 1 addition & 1 deletion cmd/event_handler/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func main() {
success := false

for retry < maxRetries {
if err := reconfigurePGBouncer(*&newMemberID); err != nil {
if err := reconfigurePGBouncer(newMemberID); err != nil {
log.Printf("%s - failed to reconfigure pgbouncer: %s. (attempt: %d)\n", *event, err, retry)
retry++
time.Sleep(1 * time.Second)
Expand Down
11 changes: 4 additions & 7 deletions cmd/standby_cleaner/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,9 @@ func main() {

fmt.Printf("Pruning every %s...\n", deadMemberRemovalThreshold)

for {
select {
case <-ticker.C:
if err := handleTick(ctx, flypgNode, seenAt, deadMemberRemovalThreshold); err != nil {
fmt.Println(err)
}
for range ticker.C {
if err := handleTick(ctx, flypgNode, seenAt, deadMemberRemovalThreshold); err != nil {
fmt.Println(err)
}
}
}
Expand Down Expand Up @@ -91,7 +88,7 @@ func handleTick(ctx context.Context, node *flypg.Node, seenAt map[int]time.Time,
sConn, err := node.RepMgr.NewRemoteConnection(ctx, standby.Hostname)
if err != nil {
// TODO - Verify the exception that's getting thrown.
if time.Now().Sub(seenAt[standby.ID]) >= deadMemberRemovalThreshold {
if time.Since(seenAt[standby.ID]) >= deadMemberRemovalThreshold {
if err := node.RepMgr.UnregisterMember(ctx, standby); err != nil {
fmt.Printf("failed to unregister member %s: %v", standby.Hostname, err)
continue
Expand Down
2 changes: 1 addition & 1 deletion config/queries.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ pg_database:
description: "Name of the database"
- size_bytes:
usage: "GAUGE"
description: "Disk space used by the database"
description: "Disk space used by the database"
2 changes: 1 addition & 1 deletion fly.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ processes = []

[metrics]
path = "/metrics"
port = 9187
port = 9187
2 changes: 1 addition & 1 deletion grafanadash.example.json
Original file line number Diff line number Diff line change
Expand Up @@ -3159,4 +3159,4 @@
"uid": "v5ciIbUZz",
"version": 11,
"weekStart": ""
}
}
12 changes: 6 additions & 6 deletions internal/api/handle_admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (s *Server) handleUpdatePostgresSettings(w http.ResponseWriter, r *http.Req

var requiresRestart []string

for k, _ := range user {
for k := range user {
restart, err := admin.SettingRequiresRestart(r.Context(), conn, k)
if err != nil {
renderErr(w, err)
Expand Down Expand Up @@ -185,7 +185,7 @@ func (s *Server) handleViewPostgresSettings(w http.ResponseWriter, r *http.Reque

var out []admin.PGSetting

for key, _ := range all {
for key := range all {
if slices.Contains(in, key) {
setting, err := admin.GetSetting(r.Context(), conn, key)
if err != nil {
Expand Down Expand Up @@ -222,8 +222,8 @@ func (s *Server) handleViewBouncerSettings(w http.ResponseWriter, r *http.Reques

out := map[string]interface{}{}

for key, _ := range all {
val, _ := all[key]
for key := range all {
val := all[key]
if slices.Contains(in, key) {
out[key] = val
}
Expand Down Expand Up @@ -255,8 +255,8 @@ func (s *Server) handleViewRepmgrSettings(w http.ResponseWriter, r *http.Request

out := map[string]interface{}{}

for key, _ := range all {
val, _ := all[key]
for key := range all {
val := all[key]
if slices.Contains(in, key) {
out[key] = val
}
Expand Down
4 changes: 2 additions & 2 deletions internal/flycheck/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func checkPressure(name string) (string, error) {
return "", err
}

_, err = fmt.Sscanf(
fmt.Sscanf(
string(raw),
"some avg10=%f avg60=%f avg300=%f total=%f",
&avg10, &avg60, &avg300, &counter,
Expand Down Expand Up @@ -171,7 +171,7 @@ func dataSize(size uint64) string {
base := math.Log(float64(size)) / math.Log(1024)
getSize := round(math.Pow(1024, base-math.Floor(base)), .5, 2)
getSuffix := suffixes[int(math.Floor(base))]
return fmt.Sprint(strconv.FormatFloat(getSize, 'f', -1, 64) + " " + string(getSuffix))
return fmt.Sprint(strconv.FormatFloat(getSize, 'f', -1, 64) + " " + getSuffix)
}

func pressureToDuration(pressure float64, base float64) (time.Duration, error) {
Expand Down
14 changes: 7 additions & 7 deletions internal/flypg/admin/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,12 @@ type ReplicationSlot struct {
}

func ListReplicationSlots(ctx context.Context, pg *pgx.Conn) ([]ReplicationSlot, error) {
sql := fmt.Sprintf("SELECT slot_name, slot_type, active, wal_status from pg_replication_slots;")
sql := "SELECT slot_name, slot_type, active, wal_status from pg_replication_slots;"
rows, err := pg.Query(ctx, sql)
defer rows.Close()
if err != nil {
return nil, err
}
defer rows.Close()

var slots []ReplicationSlot

Expand Down Expand Up @@ -140,8 +140,8 @@ func EnableExtension(ctx context.Context, pg *pgx.Conn, extension string) error
func ListDatabases(ctx context.Context, pg *pgx.Conn) ([]DbInfo, error) {
sql := `
SELECT d.datname,
(SELECT array_agg(u.usename::text order by u.usename)
from pg_user u
(SELECT array_agg(u.usename::text order by u.usename)
from pg_user u
where has_database_privilege(u.usename, d.datname, 'CONNECT')) as allowed_users
from pg_database d where d.datistemplate = false
order by d.datname;
Expand All @@ -168,9 +168,9 @@ func ListDatabases(ctx context.Context, pg *pgx.Conn) ([]DbInfo, error) {

func FindDatabase(ctx context.Context, pg *pgx.Conn, name string) (*DbInfo, error) {
sql := `
SELECT
datname,
(SELECT array_agg(u.usename::text order by u.usename) FROM pg_user u WHERE has_database_privilege(u.usename, d.datname, 'CONNECT')) as allowed_users
SELECT
datname,
(SELECT array_agg(u.usename::text order by u.usename) FROM pg_user u WHERE has_database_privilege(u.usename, d.datname, 'CONNECT')) as allowed_users
FROM pg_database d WHERE d.datname='%s';
`

Expand Down
4 changes: 1 addition & 3 deletions internal/flypg/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,7 @@ func WriteConfigFiles(c Config) error {

for key, value := range c.UserConfig() {
entry := fmt.Sprintf("%s = %v\n", key, value)
if _, ok := internal[key]; ok {
delete(internal, key)
}
delete(internal, key)
userFile.Write([]byte(entry))
}

Expand Down
Loading