Skip to content

Commit c90a15b

Browse files
authored
Add support for max replication slots (#184)
1 parent 901612f commit c90a15b

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

internal/flypg/admin/admin.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,25 @@ func ValidatePGSettings(ctx context.Context, conn *pgx.Conn, requested map[strin
405405
}
406406
}
407407
}
408+
409+
if k == "max_replication_slots" {
410+
maxReplicationSlotsStr := v.(string)
411+
412+
// Convert string to int
413+
maxReplicationSlots, err := strconv.ParseInt(maxReplicationSlotsStr, 10, 64)
414+
if err != nil {
415+
return fmt.Errorf("failed to parse max_replication_slots: %s", err)
416+
}
417+
418+
slots, err := ListReplicationSlots(ctx, conn)
419+
if err != nil {
420+
return fmt.Errorf("failed to verify replication slots: %s", err)
421+
}
422+
423+
if len(slots) > int(maxReplicationSlots) {
424+
return fmt.Errorf("max_replication_slots must be greater than or equal to the number of active replication slots (%d)", len(slots))
425+
}
426+
}
408427
}
409428

410429
return nil

internal/flypg/pg.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,25 @@ func (c *PGConfig) validateCompatibility(requested ConfigMap) (ConfigMap, error)
378378

379379
}
380380

381+
// Max-replication-slots
382+
if v, ok := requested["max_replication_slots"]; ok {
383+
{
384+
val := v.(string)
385+
386+
// Convert string to int
387+
maxReplicationSlots, err := strconv.ParseInt(val, 10, 64)
388+
if err != nil {
389+
return requested, fmt.Errorf("failed to parse max-replication-slots: %s", err)
390+
}
391+
392+
walLevel := resolveConfigValue(requested, current, "wal_level", "replica")
393+
394+
if maxReplicationSlots > 0 && walLevel == "minimal" {
395+
return requested, fmt.Errorf("wal_level must be set to replica or higher before replication slots can be used")
396+
}
397+
}
398+
}
399+
381400
return requested, nil
382401
}
383402

internal/flypg/pg_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,32 @@ func TestValidateCompatibility(t *testing.T) {
427427
}
428428
})
429429

430+
t.Run("maxReplicationSlots", func(t *testing.T) {
431+
valid := ConfigMap{
432+
"wal_level": "replica",
433+
"max_replication_slots": "10",
434+
}
435+
if _, err := pgConf.validateCompatibility(valid); err != nil {
436+
t.Fatal(err)
437+
}
438+
439+
valid = ConfigMap{
440+
"wal_level": "logical",
441+
"max_replication_slots": "12",
442+
}
443+
if _, err := pgConf.validateCompatibility(valid); err != nil {
444+
t.Fatal(err)
445+
}
446+
447+
invalid := ConfigMap{
448+
"wal_level": "minimal",
449+
"max_replication_slots": "20",
450+
}
451+
if _, err := pgConf.validateCompatibility(invalid); err == nil {
452+
t.Fatal(err)
453+
}
454+
})
455+
430456
}
431457

432458
func stubPGConfigFile() error {

0 commit comments

Comments
 (0)