Skip to content

Commit 943f7ae

Browse files
committed
Add lookup table for replication slot conflict reasons
This commit switches the handling of the conflict cause strings for replication slots to use a table rather than being explicitly listed, using a C99-designated initializer syntax for the array elements. This makes the whole more readable while easing future maintenance with less areas to update when adding a new conflict reason. This is similar to 74a7306, but the scale of the change is smaller as there are less conflict causes than LWLock builtin tranche names. Author: Bharath Rupireddy Reviewed-by: Jelte Fennema-Nio Discussion: https://postgr.es/m/CALj2ACUxSLA91QGFrJsWNKs58KXb1C03mbuwKmzqqmoAKLwJaw@mail.gmail.com
1 parent 28f3915 commit 943f7ae

File tree

3 files changed

+43
-39
lines changed

3 files changed

+43
-39
lines changed

src/backend/replication/slot.c

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,22 @@ typedef struct ReplicationSlotOnDisk
7777
ReplicationSlotPersistentData slotdata;
7878
} ReplicationSlotOnDisk;
7979

80+
/*
81+
* Lookup table for slot invalidation causes.
82+
*/
83+
const char *const SlotInvalidationCauses[] = {
84+
[RS_INVAL_NONE] = "none",
85+
[RS_INVAL_WAL_REMOVED] = "wal_removed",
86+
[RS_INVAL_HORIZON] = "rows_removed",
87+
[RS_INVAL_WAL_LEVEL] = "wal_level_insufficient",
88+
};
89+
90+
/* Maximum number of invalidation causes */
91+
#define RS_INVAL_MAX_CAUSES RS_INVAL_WAL_LEVEL
92+
93+
StaticAssertDecl(lengthof(SlotInvalidationCauses) == (RS_INVAL_MAX_CAUSES + 1),
94+
"array length mismatch");
95+
8096
/* size of version independent data */
8197
#define ReplicationSlotOnDiskConstantSize \
8298
offsetof(ReplicationSlotOnDisk, slotdata)
@@ -2290,23 +2306,26 @@ RestoreSlotFromDisk(const char *name)
22902306
}
22912307

22922308
/*
2293-
* Maps the pg_replication_slots.conflict_reason text value to
2294-
* ReplicationSlotInvalidationCause enum value
2309+
* Maps a conflict reason for a replication slot to
2310+
* ReplicationSlotInvalidationCause.
22952311
*/
22962312
ReplicationSlotInvalidationCause
2297-
GetSlotInvalidationCause(char *conflict_reason)
2313+
GetSlotInvalidationCause(const char *conflict_reason)
22982314
{
2315+
ReplicationSlotInvalidationCause cause;
2316+
bool found PG_USED_FOR_ASSERTS_ONLY = false;
2317+
22992318
Assert(conflict_reason);
23002319

2301-
if (strcmp(conflict_reason, SLOT_INVAL_WAL_REMOVED_TEXT) == 0)
2302-
return RS_INVAL_WAL_REMOVED;
2303-
else if (strcmp(conflict_reason, SLOT_INVAL_HORIZON_TEXT) == 0)
2304-
return RS_INVAL_HORIZON;
2305-
else if (strcmp(conflict_reason, SLOT_INVAL_WAL_LEVEL_TEXT) == 0)
2306-
return RS_INVAL_WAL_LEVEL;
2307-
else
2308-
Assert(0);
2320+
for (cause = RS_INVAL_NONE; cause <= RS_INVAL_MAX_CAUSES; cause++)
2321+
{
2322+
if (strcmp(SlotInvalidationCauses[cause], conflict_reason) == 0)
2323+
{
2324+
found = true;
2325+
break;
2326+
}
2327+
}
23092328

2310-
/* Keep compiler quiet */
2311-
return RS_INVAL_NONE;
2329+
Assert(found);
2330+
return cause;
23122331
}

src/backend/replication/slotfuncs.c

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -413,24 +413,12 @@ pg_get_replication_slots(PG_FUNCTION_ARGS)
413413
nulls[i++] = true;
414414
else
415415
{
416-
switch (slot_contents.data.invalidated)
417-
{
418-
case RS_INVAL_NONE:
419-
nulls[i++] = true;
420-
break;
421-
422-
case RS_INVAL_WAL_REMOVED:
423-
values[i++] = CStringGetTextDatum(SLOT_INVAL_WAL_REMOVED_TEXT);
424-
break;
425-
426-
case RS_INVAL_HORIZON:
427-
values[i++] = CStringGetTextDatum(SLOT_INVAL_HORIZON_TEXT);
428-
break;
429-
430-
case RS_INVAL_WAL_LEVEL:
431-
values[i++] = CStringGetTextDatum(SLOT_INVAL_WAL_LEVEL_TEXT);
432-
break;
433-
}
416+
ReplicationSlotInvalidationCause cause = slot_contents.data.invalidated;
417+
418+
if (cause == RS_INVAL_NONE)
419+
nulls[i++] = true;
420+
else
421+
values[i++] = CStringGetTextDatum(SlotInvalidationCauses[cause]);
434422
}
435423

436424
values[i++] = BoolGetDatum(slot_contents.data.failover);

src/include/replication/slot.h

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ typedef enum ReplicationSlotPersistency
4040
/*
4141
* Slots can be invalidated, e.g. due to max_slot_wal_keep_size. If so, the
4242
* 'invalidated' field is set to a value other than _NONE.
43+
*
44+
* When adding a new invalidation cause here, remember to update
45+
* SlotInvalidationCauses and RS_INVAL_MAX_CAUSES.
4346
*/
4447
typedef enum ReplicationSlotInvalidationCause
4548
{
@@ -52,13 +55,7 @@ typedef enum ReplicationSlotInvalidationCause
5255
RS_INVAL_WAL_LEVEL,
5356
} ReplicationSlotInvalidationCause;
5457

55-
/*
56-
* The possible values for 'conflict_reason' returned in
57-
* pg_get_replication_slots.
58-
*/
59-
#define SLOT_INVAL_WAL_REMOVED_TEXT "wal_removed"
60-
#define SLOT_INVAL_HORIZON_TEXT "rows_removed"
61-
#define SLOT_INVAL_WAL_LEVEL_TEXT "wal_level_insufficient"
58+
extern PGDLLIMPORT const char *const SlotInvalidationCauses[];
6259

6360
/*
6461
* On-Disk data of a replication slot, preserved across restarts.
@@ -275,6 +272,6 @@ extern void CheckPointReplicationSlots(bool is_shutdown);
275272
extern void CheckSlotRequirements(void);
276273
extern void CheckSlotPermissions(void);
277274
extern ReplicationSlotInvalidationCause
278-
GetSlotInvalidationCause(char *conflict_reason);
275+
GetSlotInvalidationCause(const char *conflict_reason);
279276

280277
#endif /* SLOT_H */

0 commit comments

Comments
 (0)