mirror of
https://github.com/postgres/postgres.git
synced 2025-04-22 23:02:54 +03:00
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 74a730631065, 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
This commit is contained in:
parent
28f3915b73
commit
943f7ae1c8
@ -77,6 +77,22 @@ typedef struct ReplicationSlotOnDisk
|
|||||||
ReplicationSlotPersistentData slotdata;
|
ReplicationSlotPersistentData slotdata;
|
||||||
} ReplicationSlotOnDisk;
|
} ReplicationSlotOnDisk;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Lookup table for slot invalidation causes.
|
||||||
|
*/
|
||||||
|
const char *const SlotInvalidationCauses[] = {
|
||||||
|
[RS_INVAL_NONE] = "none",
|
||||||
|
[RS_INVAL_WAL_REMOVED] = "wal_removed",
|
||||||
|
[RS_INVAL_HORIZON] = "rows_removed",
|
||||||
|
[RS_INVAL_WAL_LEVEL] = "wal_level_insufficient",
|
||||||
|
};
|
||||||
|
|
||||||
|
/* Maximum number of invalidation causes */
|
||||||
|
#define RS_INVAL_MAX_CAUSES RS_INVAL_WAL_LEVEL
|
||||||
|
|
||||||
|
StaticAssertDecl(lengthof(SlotInvalidationCauses) == (RS_INVAL_MAX_CAUSES + 1),
|
||||||
|
"array length mismatch");
|
||||||
|
|
||||||
/* size of version independent data */
|
/* size of version independent data */
|
||||||
#define ReplicationSlotOnDiskConstantSize \
|
#define ReplicationSlotOnDiskConstantSize \
|
||||||
offsetof(ReplicationSlotOnDisk, slotdata)
|
offsetof(ReplicationSlotOnDisk, slotdata)
|
||||||
@ -2290,23 +2306,26 @@ RestoreSlotFromDisk(const char *name)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Maps the pg_replication_slots.conflict_reason text value to
|
* Maps a conflict reason for a replication slot to
|
||||||
* ReplicationSlotInvalidationCause enum value
|
* ReplicationSlotInvalidationCause.
|
||||||
*/
|
*/
|
||||||
ReplicationSlotInvalidationCause
|
ReplicationSlotInvalidationCause
|
||||||
GetSlotInvalidationCause(char *conflict_reason)
|
GetSlotInvalidationCause(const char *conflict_reason)
|
||||||
{
|
{
|
||||||
|
ReplicationSlotInvalidationCause cause;
|
||||||
|
bool found PG_USED_FOR_ASSERTS_ONLY = false;
|
||||||
|
|
||||||
Assert(conflict_reason);
|
Assert(conflict_reason);
|
||||||
|
|
||||||
if (strcmp(conflict_reason, SLOT_INVAL_WAL_REMOVED_TEXT) == 0)
|
for (cause = RS_INVAL_NONE; cause <= RS_INVAL_MAX_CAUSES; cause++)
|
||||||
return RS_INVAL_WAL_REMOVED;
|
{
|
||||||
else if (strcmp(conflict_reason, SLOT_INVAL_HORIZON_TEXT) == 0)
|
if (strcmp(SlotInvalidationCauses[cause], conflict_reason) == 0)
|
||||||
return RS_INVAL_HORIZON;
|
{
|
||||||
else if (strcmp(conflict_reason, SLOT_INVAL_WAL_LEVEL_TEXT) == 0)
|
found = true;
|
||||||
return RS_INVAL_WAL_LEVEL;
|
break;
|
||||||
else
|
}
|
||||||
Assert(0);
|
}
|
||||||
|
|
||||||
/* Keep compiler quiet */
|
Assert(found);
|
||||||
return RS_INVAL_NONE;
|
return cause;
|
||||||
}
|
}
|
||||||
|
@ -413,24 +413,12 @@ pg_get_replication_slots(PG_FUNCTION_ARGS)
|
|||||||
nulls[i++] = true;
|
nulls[i++] = true;
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
switch (slot_contents.data.invalidated)
|
ReplicationSlotInvalidationCause cause = slot_contents.data.invalidated;
|
||||||
{
|
|
||||||
case RS_INVAL_NONE:
|
|
||||||
nulls[i++] = true;
|
|
||||||
break;
|
|
||||||
|
|
||||||
case RS_INVAL_WAL_REMOVED:
|
if (cause == RS_INVAL_NONE)
|
||||||
values[i++] = CStringGetTextDatum(SLOT_INVAL_WAL_REMOVED_TEXT);
|
nulls[i++] = true;
|
||||||
break;
|
else
|
||||||
|
values[i++] = CStringGetTextDatum(SlotInvalidationCauses[cause]);
|
||||||
case RS_INVAL_HORIZON:
|
|
||||||
values[i++] = CStringGetTextDatum(SLOT_INVAL_HORIZON_TEXT);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case RS_INVAL_WAL_LEVEL:
|
|
||||||
values[i++] = CStringGetTextDatum(SLOT_INVAL_WAL_LEVEL_TEXT);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
values[i++] = BoolGetDatum(slot_contents.data.failover);
|
values[i++] = BoolGetDatum(slot_contents.data.failover);
|
||||||
|
@ -40,6 +40,9 @@ typedef enum ReplicationSlotPersistency
|
|||||||
/*
|
/*
|
||||||
* Slots can be invalidated, e.g. due to max_slot_wal_keep_size. If so, the
|
* Slots can be invalidated, e.g. due to max_slot_wal_keep_size. If so, the
|
||||||
* 'invalidated' field is set to a value other than _NONE.
|
* 'invalidated' field is set to a value other than _NONE.
|
||||||
|
*
|
||||||
|
* When adding a new invalidation cause here, remember to update
|
||||||
|
* SlotInvalidationCauses and RS_INVAL_MAX_CAUSES.
|
||||||
*/
|
*/
|
||||||
typedef enum ReplicationSlotInvalidationCause
|
typedef enum ReplicationSlotInvalidationCause
|
||||||
{
|
{
|
||||||
@ -52,13 +55,7 @@ typedef enum ReplicationSlotInvalidationCause
|
|||||||
RS_INVAL_WAL_LEVEL,
|
RS_INVAL_WAL_LEVEL,
|
||||||
} ReplicationSlotInvalidationCause;
|
} ReplicationSlotInvalidationCause;
|
||||||
|
|
||||||
/*
|
extern PGDLLIMPORT const char *const SlotInvalidationCauses[];
|
||||||
* The possible values for 'conflict_reason' returned in
|
|
||||||
* pg_get_replication_slots.
|
|
||||||
*/
|
|
||||||
#define SLOT_INVAL_WAL_REMOVED_TEXT "wal_removed"
|
|
||||||
#define SLOT_INVAL_HORIZON_TEXT "rows_removed"
|
|
||||||
#define SLOT_INVAL_WAL_LEVEL_TEXT "wal_level_insufficient"
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* On-Disk data of a replication slot, preserved across restarts.
|
* On-Disk data of a replication slot, preserved across restarts.
|
||||||
@ -275,6 +272,6 @@ extern void CheckPointReplicationSlots(bool is_shutdown);
|
|||||||
extern void CheckSlotRequirements(void);
|
extern void CheckSlotRequirements(void);
|
||||||
extern void CheckSlotPermissions(void);
|
extern void CheckSlotPermissions(void);
|
||||||
extern ReplicationSlotInvalidationCause
|
extern ReplicationSlotInvalidationCause
|
||||||
GetSlotInvalidationCause(char *conflict_reason);
|
GetSlotInvalidationCause(const char *conflict_reason);
|
||||||
|
|
||||||
#endif /* SLOT_H */
|
#endif /* SLOT_H */
|
||||||
|
Loading…
x
Reference in New Issue
Block a user