Skip to content

Commit ae557bd

Browse files
committed
Add basic code for support of DSM cache.
1 parent 303a9d9 commit ae557bd

File tree

4 files changed

+92
-2
lines changed

4 files changed

+92
-2
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ PGFILEDESC = "AQO - Adaptive Query Optimization"
66
MODULE_big = aqo
77
OBJS = aqo.o auto_tuning.o cardinality_estimation.o cardinality_hooks.o \
88
hash.o machine_learning.o path_utils.o postprocessing.o preprocessing.o \
9-
selectivity_cache.o storage.o utils.o learn_cache.o $(WIN32RES)
9+
selectivity_cache.o storage.o utils.o learn_cache.o aqo_shared.o $(WIN32RES)
1010

1111
TAP_TESTS = 1
1212

aqo.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "utils/selfuncs.h"
1919

2020
#include "aqo.h"
21+
#include "aqo_shared.h"
2122
#include "cardinality_hooks.h"
2223
#include "path_utils.h"
2324
#include "preprocessing.h"
@@ -125,7 +126,7 @@ _PG_init(void)
125126
{
126127
/*
127128
* In order to create our shared memory area, we have to be loaded via
128-
* shared_preload_libraries. If not, report an ERROR.
129+
* shared_preload_libraries. If not, report an ERROR.
129130
*/
130131
if (!process_shared_preload_libraries_in_progress)
131132
ereport(ERROR,
@@ -198,6 +199,8 @@ _PG_init(void)
198199
NULL
199200
);
200201

202+
prev_shmem_startup_hook = shmem_startup_hook;
203+
shmem_startup_hook = aqo_init_shmem;
201204
prev_planner_hook = planner_hook;
202205
planner_hook = aqo_planner;
203206
prev_ExecutorStart_hook = ExecutorStart_hook;
@@ -239,6 +242,10 @@ _PG_init(void)
239242
ALLOCSET_DEFAULT_SIZES);
240243
RegisterResourceReleaseCallback(aqo_free_callback, NULL);
241244
RegisterAQOPlanNodeMethods();
245+
246+
MarkGUCPrefixReserved("aqo");
247+
RequestAddinShmemSpace(MAXALIGN(sizeof(AQOSharedState)));
248+
242249
lc_init();
243250
}
244251

aqo_shared.c

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
*
3+
*/
4+
5+
#include "postgres.h"
6+
7+
#include "storage/shmem.h"
8+
9+
#include "aqo_shared.h"
10+
11+
shmem_startup_hook_type prev_shmem_startup_hook = NULL;
12+
static AQOSharedState *aqo_state = NULL;
13+
unsigned long temp_storage_size = 1024 * 1024; /* Storage size, in bytes */
14+
void *temp_storage = NULL;
15+
16+
static void
17+
attach_dsm_segment(void)
18+
{
19+
dsm_segment *seg;
20+
21+
LWLockAcquire(&aqo_state->lock, LW_EXCLUSIVE);
22+
23+
if (aqo_state->dsm_handler != DSM_HANDLE_INVALID)
24+
{
25+
seg = dsm_attach(aqo_state->dsm_handler);
26+
}
27+
else
28+
{
29+
seg = dsm_create(temp_storage_size, 0);
30+
aqo_state->dsm_handler = dsm_segment_handle(seg);
31+
}
32+
33+
temp_storage = dsm_segment_address(seg);
34+
LWLockRelease(&aqo_state->lock);
35+
}
36+
37+
static void
38+
aqo_detach_shmem(int code, Datum arg)
39+
{
40+
dsm_handle handler = *(dsm_handle *) arg;
41+
dsm_detach(dsm_find_mapping(handler));
42+
}
43+
44+
void
45+
aqo_init_shmem(void)
46+
{
47+
bool found;
48+
49+
LWLockAcquire(AddinShmemInitLock, LW_EXCLUSIVE);
50+
aqo_state = ShmemInitStruct("aqo", sizeof(AQOSharedState), &found);
51+
if (!found)
52+
{
53+
/* First time through ... */
54+
LWLockInitialize(&aqo_state->lock, LWLockNewTrancheId());
55+
aqo_state->dsm_handler = DSM_HANDLE_INVALID;
56+
}
57+
LWLockRelease(AddinShmemInitLock);
58+
59+
LWLockRegisterTranche(aqo_state->lock.tranche, "aqo");
60+
on_shmem_exit(aqo_detach_shmem, (Datum) &aqo_state->dsm_handler);
61+
}

aqo_shared.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#ifndef AQO_SHARED_H
2+
#define AQO_SHARED_H
3+
4+
5+
#include "storage/dsm.h"
6+
#include "storage/ipc.h"
7+
#include "storage/lwlock.h"
8+
9+
10+
typedef struct AQOSharedState
11+
{
12+
LWLock lock; /* mutual exclusion */
13+
dsm_handle dsm_handler;
14+
} AQOSharedState;
15+
16+
17+
extern shmem_startup_hook_type prev_shmem_startup_hook;
18+
19+
20+
extern void aqo_init_shmem(void);
21+
22+
#endif /* AQO_SHARED_H */

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy