Skip to content

Commit fd6192e

Browse files
author
Ben Iofel
committed
Merge remote-tracking branch 'origin/master' into node-name
2 parents 2473d7a + 7a8ac4d commit fd6192e

File tree

4 files changed

+72
-15
lines changed

4 files changed

+72
-15
lines changed

cmd/pg_unregister/main.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@ import (
55
"encoding/base64"
66
"errors"
77
"fmt"
8+
"log"
89
"os"
10+
"time"
911

1012
"github.com/fly-apps/postgres-flex/internal/flypg"
13+
"github.com/fly-apps/postgres-flex/internal/flypg/admin"
1114
"github.com/fly-apps/postgres-flex/internal/utils"
1215
"github.com/jackc/pgx/v5"
1316
)
@@ -56,5 +59,43 @@ func processUnregistration(ctx context.Context) error {
5659
return fmt.Errorf("failed to unregister member: %v", err)
5760
}
5861

62+
slotName := fmt.Sprintf("repmgr_slot_%d", member.ID)
63+
if err := removeReplicationSlot(ctx, conn, slotName); err != nil {
64+
return err
65+
}
66+
5967
return nil
6068
}
69+
70+
func removeReplicationSlot(ctx context.Context, conn *pgx.Conn, slotName string) error {
71+
ticker := time.NewTicker(1 * time.Second)
72+
timeout := time.After(10 * time.Second)
73+
defer ticker.Stop()
74+
for {
75+
select {
76+
case <-ctx.Done():
77+
return ctx.Err()
78+
case <-timeout:
79+
return fmt.Errorf("timed out trying to drop replication slot")
80+
case <-ticker.C:
81+
slot, err := admin.GetReplicationSlot(ctx, conn, slotName)
82+
if err != nil {
83+
if err == pgx.ErrNoRows {
84+
return nil
85+
}
86+
return fmt.Errorf("failed to get replication slot %s: %v", slotName, err)
87+
}
88+
89+
if slot.Active {
90+
log.Printf("Slot %s is still active, waiting...", slotName)
91+
continue
92+
}
93+
94+
if err := admin.DropReplicationSlot(ctx, conn, slotName); err != nil {
95+
return fmt.Errorf("failed to drop replication slot %s: %v", slotName, err)
96+
}
97+
98+
return nil
99+
}
100+
}
101+
}

internal/flypg/admin/admin.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,18 @@ type ReplicationSlot struct {
130130
RetainedWalInBytes int
131131
}
132132

133+
func GetReplicationSlot(ctx context.Context, pg *pgx.Conn, slotName string) (*ReplicationSlot, error) {
134+
sql := fmt.Sprintf("SELECT slot_name, active, wal_status, pg_wal_lsn_diff(pg_current_wal_lsn(), restart_lsn) AS retained_wal FROM pg_replication_slots where slot_name = '%s';", slotName)
135+
row := pg.QueryRow(ctx, sql)
136+
137+
var slot ReplicationSlot
138+
if err := row.Scan(&slot.Name, &slot.Active, &slot.WalStatus, &slot.RetainedWalInBytes); err != nil {
139+
return nil, err
140+
}
141+
142+
return &slot, nil
143+
}
144+
133145
func ListReplicationSlots(ctx context.Context, pg *pgx.Conn) ([]ReplicationSlot, error) {
134146
sql := "SELECT slot_name, active, wal_status, pg_wal_lsn_diff(pg_current_wal_lsn(), restart_lsn) AS retained_wal FROM pg_replication_slots;"
135147
rows, err := pg.Query(ctx, sql)
@@ -167,7 +179,6 @@ func ListReplicationSlots(ctx context.Context, pg *pgx.Conn) ([]ReplicationSlot,
167179

168180
func DropReplicationSlot(ctx context.Context, pg *pgx.Conn, name string) error {
169181
sql := fmt.Sprintf("SELECT pg_drop_replication_slot('%s');", name)
170-
171182
_, err := pg.Exec(ctx, sql)
172183
if err != nil {
173184
return err

pg16/Dockerfile

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,19 @@ COPY ./bin/* /fly/bin/
2121

2222
FROM ubuntu:24.04
2323

24-
ENV PGDATA=/data/postgresql
25-
ENV PGPASSFILE=/data/.pgpass
26-
ENV AWS_SHARED_CREDENTIALS_FILE=/data/.aws/credentials
2724
ARG VERSION
2825
ARG PG_MAJOR_VERSION
2926
ARG POSTGIS_MAJOR=3
3027
ARG HAPROXY_VERSION=2.8
3128
ARG REPMGR_VERSION=5.4.1-1build2
3229

30+
ENV PGDATA=/data/postgresql
31+
ENV PGPASSFILE=/data/.pgpass
32+
ENV AWS_SHARED_CREDENTIALS_FILE=/data/.aws/credentials
33+
ENV PG_MAJOR_VERSION=${PG_MAJOR_VERSION}
34+
ENV PATH="/usr/lib/postgresql/${PG_MAJOR_VERSION}/bin:$PATH"
35+
36+
3337
LABEL fly.app_role=postgres_cluster
3438
LABEL fly.version=${VERSION}
3539
LABEL fly.pg-version=${PG_VERSION}
@@ -63,16 +67,15 @@ RUN apt-get update && apt-get install --no-install-recommends -y \
6367
haproxy=$HAPROXY_VERSION.\* \
6468
&& apt autoremove -y && apt clean
6569

66-
67-
# Add PostgreSQL bin directory to PATH
68-
ENV PATH="/usr/lib/postgresql/${PG_MAJOR_VERSION}/bin:$PATH"
69-
7070
# Copy Go binaries from the builder stage
7171
COPY --from=builder /fly/bin/* /usr/local/bin
7272

7373
# Copy Postgres exporter
7474
COPY --from=wrouesnel/postgres_exporter:latest /postgres_exporter /usr/local/bin/
7575

76+
# Move pg_rewind into path.
77+
RUN ln -s /usr/lib/postgresql/${PG_MAJOR_VERSION}/bin/pg_rewind /usr/bin/pg_rewind
78+
7679
ADD /config/* /fly/
7780
RUN mkdir -p /run/haproxy/
7881
RUN usermod -d /data postgres

pg16/Dockerfile-timescaledb

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,18 @@ COPY ./bin/* /fly/bin/
2121

2222
FROM ubuntu:24.04
2323

24-
ENV PGDATA=/data/postgresql
25-
ENV PGPASSFILE=/data/.pgpass
26-
ENV AWS_SHARED_CREDENTIALS_FILE=/data/.aws/credentials
2724
ARG VERSION
2825
ARG PG_MAJOR_VERSION
2926
ARG POSTGIS_MAJOR=3
3027
ARG HAPROXY_VERSION=2.8
3128
ARG REPMGR_VERSION=5.4.1-1build2
3229

30+
ENV PGDATA=/data/postgresql
31+
ENV PGPASSFILE=/data/.pgpass
32+
ENV AWS_SHARED_CREDENTIALS_FILE=/data/.aws/credentials
33+
ENV PG_MAJOR_VERSION=${PG_MAJOR_VERSION}
34+
ENV PATH="/usr/lib/postgresql/${PG_MAJOR_VERSION}/bin:$PATH"
35+
3336
LABEL fly.app_role=postgres_cluster
3437
LABEL fly.version=${VERSION}
3538
LABEL fly.pg-version=${PG_VERSION}
@@ -69,16 +72,15 @@ RUN apt-get update && apt-get install --no-install-recommends -y \
6972
haproxy=$HAPROXY_VERSION.\* \
7073
&& apt autoremove -y && apt clean
7174

72-
73-
# Add PostgreSQL bin directory to PATH
74-
ENV PATH="/usr/lib/postgresql/${PG_MAJOR_VERSION}/bin:$PATH"
75-
7675
# Copy Go binaries from the builder stage
7776
COPY --from=builder /fly/bin/* /usr/local/bin
7877

7978
# Copy Postgres exporter
8079
COPY --from=wrouesnel/postgres_exporter:latest /postgres_exporter /usr/local/bin/
8180

81+
# Move pg_rewind into path.
82+
RUN ln -s /usr/lib/postgresql/${PG_MAJOR_VERSION}/bin/pg_rewind /usr/bin/pg_rewind
83+
8284
ADD /config/* /fly/
8385
RUN mkdir -p /run/haproxy/
8486
RUN usermod -d /data postgres

0 commit comments

Comments
 (0)