Skip to content

Commit 026ed8e

Browse files
committed
Remove code duplication for permission checks with replication slots
Two functions, both named check_permissions(), used the same checks to verify if a user had required privileges to work on replication slots. This commit removes the duplication, and moves the function doing the checks to slot.c to be centralized. Author: Bharath Rupireddy Reviewed-by: Nathan Bossart, Euler Taveira Discussion: https://postgr.es/m/CALj2ACUPpVw1u7sQocFVWrSs0n10pt_G_4NPZKSxXK6cW1dErw@mail.gmail.com
1 parent 138531f commit 026ed8e

File tree

4 files changed

+19
-24
lines changed

4 files changed

+19
-24
lines changed

src/backend/replication/logical/logicalfuncs.c

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -95,15 +95,6 @@ LogicalOutputWrite(LogicalDecodingContext *ctx, XLogRecPtr lsn, TransactionId xi
9595
p->returned_rows++;
9696
}
9797

98-
static void
99-
check_permissions(void)
100-
{
101-
if (!superuser() && !has_rolreplication(GetUserId()))
102-
ereport(ERROR,
103-
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
104-
errmsg("must be superuser or replication role to use replication slots")));
105-
}
106-
10798
/*
10899
* Helper function for the various SQL callable logical decoding functions.
109100
*/
@@ -124,7 +115,7 @@ pg_logical_slot_get_changes_guts(FunctionCallInfo fcinfo, bool confirm, bool bin
124115
List *options = NIL;
125116
DecodingOutputState *p;
126117

127-
check_permissions();
118+
CheckSlotPermissions();
128119

129120
CheckLogicalDecodingRequirements();
130121

src/backend/replication/slot.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,6 +1059,18 @@ CheckSlotRequirements(void)
10591059
errmsg("replication slots can only be used if wal_level >= replica")));
10601060
}
10611061

1062+
/*
1063+
* Check whether the user has privilege to use replication slots.
1064+
*/
1065+
void
1066+
CheckSlotPermissions(void)
1067+
{
1068+
if (!superuser() && !has_rolreplication(GetUserId()))
1069+
ereport(ERROR,
1070+
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
1071+
errmsg("must be superuser or replication role to use replication slots")));
1072+
}
1073+
10621074
/*
10631075
* Reserve WAL for the currently active slot.
10641076
*

src/backend/replication/slotfuncs.c

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,6 @@
2525
#include "utils/pg_lsn.h"
2626
#include "utils/resowner.h"
2727

28-
static void
29-
check_permissions(void)
30-
{
31-
if (!superuser() && !has_rolreplication(GetUserId()))
32-
ereport(ERROR,
33-
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
34-
errmsg("must be superuser or replication role to use replication slots")));
35-
}
36-
3728
/*
3829
* Helper function for creating a new physical replication slot with
3930
* given arguments. Note that this function doesn't release the created
@@ -85,7 +76,7 @@ pg_create_physical_replication_slot(PG_FUNCTION_ARGS)
8576
if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
8677
elog(ERROR, "return type must be a row type");
8778

88-
check_permissions();
79+
CheckSlotPermissions();
8980

9081
CheckSlotRequirements();
9182

@@ -188,7 +179,7 @@ pg_create_logical_replication_slot(PG_FUNCTION_ARGS)
188179
if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
189180
elog(ERROR, "return type must be a row type");
190181

191-
check_permissions();
182+
CheckSlotPermissions();
192183

193184
CheckLogicalDecodingRequirements();
194185

@@ -224,7 +215,7 @@ pg_drop_replication_slot(PG_FUNCTION_ARGS)
224215
{
225216
Name name = PG_GETARG_NAME(0);
226217

227-
check_permissions();
218+
CheckSlotPermissions();
228219

229220
CheckSlotRequirements();
230221

@@ -619,7 +610,7 @@ pg_replication_slot_advance(PG_FUNCTION_ARGS)
619610

620611
Assert(!MyReplicationSlot);
621612

622-
check_permissions();
613+
CheckSlotPermissions();
623614

624615
if (XLogRecPtrIsInvalid(moveto))
625616
ereport(ERROR,
@@ -718,7 +709,7 @@ copy_replication_slot(FunctionCallInfo fcinfo, bool logical_slot)
718709
if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
719710
elog(ERROR, "return type must be a row type");
720711

721-
check_permissions();
712+
CheckSlotPermissions();
722713

723714
if (logical_slot)
724715
CheckLogicalDecodingRequirements();

src/include/replication/slot.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,5 +222,6 @@ extern void StartupReplicationSlots(void);
222222
extern void CheckPointReplicationSlots(void);
223223

224224
extern void CheckSlotRequirements(void);
225+
extern void CheckSlotPermissions(void);
225226

226227
#endif /* SLOT_H */

0 commit comments

Comments
 (0)