Skip to content

Commit 0040b61

Browse files
committed
Add shared memory structure
1 parent a279a39 commit 0040b61

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

pg_sqlwall.c

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,94 @@
11
#include "postgres.h"
22
#include "fmgr.h"
3+
#include "nodes/nodes.h"
4+
#include "miscadmin.h"
5+
#include "storage/ipc.h"
6+
#include "storage/shmem.h"
7+
#include "storage/lwlock.h"
8+
#include "utils/builtins.h"
39

10+
/* Constants and Macros */
411
PG_MODULE_MAGIC;
512

613
/* Abost compilation against unsupported versions. */
714
#if PG_VERSION_NUM < 130000
815
#error "Unsupported PostgreSQL Version"
916
#endif
1017

18+
/* Is there a better size candidate? */
19+
#define DENY_LIST_SIZE 128
20+
/* CMD_NOTHING tells us about enum size as it is the last enum member. */
21+
#define CMD_TYPE_COUNT CMD_NOTHING+1
22+
23+
typedef struct pgswUserPerm {
24+
Oid userId;
25+
/* Lists SQL command types the user is NOT allowed to run.
26+
* CmdType enum members are indexes for this array, i.e.
27+
* denyCmd[CMD_DELETE] = true to prohibit SQL DELETE statements. */
28+
bool denyCmd[CMD_TYPE_COUNT];
29+
} pgswUserPerm;
30+
31+
typedef struct pgswSharedState {
32+
LWLock* lock;
33+
pgswUserPerm denyList[DENY_LIST_SIZE];
34+
int denyListSize;
35+
} pgswSharedState;
36+
37+
/* Function Prototypes */
38+
void _PG_init(void);
39+
static void pgsw_shmem_request(void);
40+
static void pgsw_shmem_startup(void);
41+
1142
Datum my_function(PG_FUNCTION_ARGS);
1243
PG_FUNCTION_INFO_V1(my_function);
1344

45+
/* Global variables */
46+
static shmem_request_hook_type prev_shmem_request_hook = NULL;
47+
static shmem_startup_hook_type prev_shmem_startup_hook = NULL;
48+
pgswSharedState *pgsw_shmem = NULL;
49+
50+
void
51+
_PG_init(void)
52+
{
53+
if(!process_shared_preload_libraries_in_progress)
54+
elog(FATAL, "Please use shared_preload_libraries");
55+
56+
prev_shmem_request_hook = shmem_request_hook;
57+
shmem_request_hook = pgsw_shmem_request;
58+
59+
prev_shmem_startup_hook = shmem_startup_hook;
60+
shmem_startup_hook = pgsw_shmem_startup;
61+
}
62+
63+
void
64+
pgsw_shmem_request(void)
65+
{
66+
if(prev_shmem_request_hook)
67+
prev_shmem_request_hook();
68+
69+
RequestAddinShmemSpace(MAXALIGN(sizeof(pgswSharedState)));
70+
RequestNamedLWLockTranche("pg_sqlwall", 1);
71+
}
72+
73+
void
74+
pgsw_shmem_startup(void)
75+
{
76+
bool found;
77+
78+
if(prev_shmem_startup_hook)
79+
prev_shmem_startup_hook();
80+
81+
LWLockAcquire(AddinShmemInitLock, LW_EXCLUSIVE);
82+
83+
pgsw_shmem = ShmemInitStruct("pg_sqlwall", sizeof(pgswSharedState), &found);
84+
if(!found) {
85+
pgsw_shmem->denyListSize = 0;
86+
pgsw_shmem->lock = &(GetNamedLWLockTranche("pg_sqlwall"))->lock;
87+
}
88+
89+
LWLockRelease(AddinShmemInitLock);
90+
}
91+
1492
Datum
1593
my_function(PG_FUNCTION_ARGS)
1694
{

0 commit comments

Comments
 (0)