Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit943f7ae

Browse files
committed
Add lookup table for replication slot conflict reasons
This commit switches the handling of the conflict cause strings forreplication slots to use a table rather than being explicitly listed,using a C99-designated initializer syntax for the array elements. Thismakes the whole more readable while easing future maintenance with lessareas to update when adding a new conflict reason.This is similar to74a7306, but the scale of the change is smalleras there are less conflict causes than LWLock builtin tranche names.Author: Bharath RupireddyReviewed-by: Jelte Fennema-NioDiscussion:https://postgr.es/m/CALj2ACUxSLA91QGFrJsWNKs58KXb1C03mbuwKmzqqmoAKLwJaw@mail.gmail.com
1 parent28f3915 commit943f7ae

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
ReplicationSlotPersistentDataslotdata;
7878
}ReplicationSlotOnDisk;
7979

80+
/*
81+
* Lookup table for slot invalidation causes.
82+
*/
83+
constchar*constSlotInvalidationCauses[]= {
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+
#defineRS_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
#defineReplicationSlotOnDiskConstantSize \
8298
offsetof(ReplicationSlotOnDisk, slotdata)
@@ -2290,23 +2306,26 @@ RestoreSlotFromDisk(const char *name)
22902306
}
22912307

22922308
/*
2293-
* Mapsthe pg_replication_slots.conflict_reason text value to
2294-
* ReplicationSlotInvalidationCause enum value
2309+
* Mapsa conflict reason for a replication slot to
2310+
* ReplicationSlotInvalidationCause.
22952311
*/
22962312
ReplicationSlotInvalidationCause
2297-
GetSlotInvalidationCause(char*conflict_reason)
2313+
GetSlotInvalidationCause(constchar*conflict_reason)
22982314
{
2315+
ReplicationSlotInvalidationCausecause;
2316+
boolfoundPG_USED_FOR_ASSERTS_ONLY= false;
2317+
22992318
Assert(conflict_reason);
23002319

2301-
if (strcmp(conflict_reason,SLOT_INVAL_WAL_REMOVED_TEXT)==0)
2302-
returnRS_INVAL_WAL_REMOVED;
2303-
elseif (strcmp(conflict_reason,SLOT_INVAL_HORIZON_TEXT)==0)
2304-
returnRS_INVAL_HORIZON;
2305-
elseif (strcmp(conflict_reason,SLOT_INVAL_WAL_LEVEL_TEXT)==0)
2306-
returnRS_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-
returnRS_INVAL_NONE;
2329+
Assert(found);
2330+
returncause;
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-
caseRS_INVAL_NONE:
419-
nulls[i++]= true;
420-
break;
421-
422-
caseRS_INVAL_WAL_REMOVED:
423-
values[i++]=CStringGetTextDatum(SLOT_INVAL_WAL_REMOVED_TEXT);
424-
break;
425-
426-
caseRS_INVAL_HORIZON:
427-
values[i++]=CStringGetTextDatum(SLOT_INVAL_HORIZON_TEXT);
428-
break;
429-
430-
caseRS_INVAL_WAL_LEVEL:
431-
values[i++]=CStringGetTextDatum(SLOT_INVAL_WAL_LEVEL_TEXT);
432-
break;
433-
}
416+
ReplicationSlotInvalidationCausecause=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
typedefenumReplicationSlotInvalidationCause
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-
#defineSLOT_INVAL_WAL_REMOVED_TEXT "wal_removed"
60-
#defineSLOT_INVAL_HORIZON_TEXT "rows_removed"
61-
#defineSLOT_INVAL_WAL_LEVEL_TEXT "wal_level_insufficient"
58+
externPGDLLIMPORTconstchar*constSlotInvalidationCauses[];
6259

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

280277
#endif/* SLOT_H */

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp