Skip to content

Commit 05208d9

Browse files
Daniil Anisimovdanolivo
Daniil Anisimov
authored andcommitted
Removed the learn_cache routine.
Now it is not needed, because non-transactional storage is used.
1 parent f2e63a5 commit 05208d9

13 files changed

+50
-574
lines changed

Diff for: Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ MODULE_big = aqo
77
OBJS = $(WIN32RES) \
88
aqo.o auto_tuning.o cardinality_estimation.o cardinality_hooks.o \
99
hash.o machine_learning.o path_utils.o postprocessing.o preprocessing.o \
10-
selectivity_cache.o storage.o utils.o learn_cache.o aqo_shared.o
10+
selectivity_cache.o storage.o utils.o aqo_shared.o
1111

1212
TAP_TESTS = 1
1313

Diff for: aqo.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
#include "path_utils.h"
2727
#include "postmaster/bgworker.h"
2828
#include "preprocessing.h"
29-
#include "learn_cache.h"
3029
#include "storage.h"
3130

3231

@@ -322,7 +321,7 @@ _PG_init(void)
322321
PGC_USERSET,
323322
0,
324323
NULL,
325-
lc_assign_hook,
324+
NULL,
326325
NULL
327326
);
328327

Diff for: aqo.h

+3-4
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ extern bool aqo_show_hash;
174174
extern bool aqo_show_details;
175175
extern int aqo_join_threshold;
176176
extern bool use_wide_search;
177+
extern bool aqo_learn_statement_timeout;
177178

178179
/* Parameters for current query */
179180
typedef struct QueryContextData
@@ -256,10 +257,8 @@ int get_clause_hash(Expr *clause, int nargs, int *args_hash, int *eclass_hash);
256257

257258

258259
/* Storage interaction */
259-
extern bool load_fss_ext(uint64 fs, int fss, OkNNrdata *data, List **reloids,
260-
bool isSafe);
261-
extern bool update_fss_ext(uint64 fs, int fss, OkNNrdata *data,
262-
List *reloids, bool isTimedOut);
260+
extern bool load_fss_ext(uint64 fs, int fss, OkNNrdata *data, List **reloids);
261+
extern bool update_fss_ext(uint64 fs, int fss, OkNNrdata *data, List *reloids);
263262

264263
/* Query preprocessing hooks */
265264
extern void print_into_explain(PlannedStmt *plannedstmt, IntoClause *into,

Diff for: aqo_shared.c

-158
Original file line numberDiff line numberDiff line change
@@ -12,161 +12,13 @@
1212
#include "storage.h"
1313

1414

15-
typedef struct
16-
{
17-
int magic;
18-
uint32 total_size;
19-
uint32 delta;
20-
} dsm_seg_hdr;
21-
22-
#define free_space(hdr) (uint32) (temp_storage_size - sizeof(dsm_seg_hdr) - hdr->delta)
23-
#define addr(delta) ((char *) dsm_segment_address(seg) + sizeof(dsm_seg_hdr) + delta)
24-
2515
shmem_startup_hook_type prev_shmem_startup_hook = NULL;
2616
AQOSharedState *aqo_state = NULL;
27-
HTAB *fss_htab = NULL;
28-
static int aqo_htab_max_items = 1000;
2917
int fs_max_items = 10000; /* Max number of different feature spaces in ML model */
3018
int fss_max_items = 100000; /* Max number of different feature subspaces in ML model */
31-
static uint32 temp_storage_size = 1024 * 1024 * 10; /* Storage size, in bytes */
32-
static dsm_segment *seg = NULL;
33-
3419

35-
static void aqo_detach_shmem(int code, Datum arg);
3620
static void on_shmem_shutdown(int code, Datum arg);
3721

38-
39-
void *
40-
get_dsm_all(uint32 *size)
41-
{
42-
dsm_seg_hdr *hdr;
43-
44-
Assert(LWLockHeldByMeInMode(&aqo_state->lock, LW_EXCLUSIVE));
45-
46-
if (aqo_state->dsm_handler == DSM_HANDLE_INVALID)
47-
{
48-
/* Fast path. No any cached data exists. */
49-
*size = 0;
50-
return NULL;
51-
}
52-
53-
if (!seg)
54-
{
55-
/* if segment exists we should connect to */
56-
seg = dsm_attach(aqo_state->dsm_handler);
57-
Assert(seg);
58-
dsm_pin_mapping(seg);
59-
before_shmem_exit(aqo_detach_shmem, (Datum) &aqo_state->dsm_handler);
60-
}
61-
62-
hdr = (dsm_seg_hdr *) dsm_segment_address(seg);
63-
*size = hdr->delta;
64-
return (char *) hdr + sizeof(dsm_seg_hdr);
65-
}
66-
67-
/*
68-
* Cleanup of DSM cache: set header into default state and zero the memory block.
69-
* This operation can be coupled with the cache dump, so we do it under an external
70-
* hold of the lock.
71-
*/
72-
void
73-
reset_dsm_cache(void)
74-
{
75-
dsm_seg_hdr *hdr;
76-
char *start;
77-
78-
Assert(LWLockHeldByMeInMode(&aqo_state->lock, LW_EXCLUSIVE));
79-
80-
if (aqo_state->dsm_handler == DSM_HANDLE_INVALID || !seg)
81-
/* Fast path. No any cached data exists. */
82-
return;
83-
84-
hdr = (dsm_seg_hdr *) dsm_segment_address(seg);
85-
start = (char *) hdr + sizeof(dsm_seg_hdr);
86-
87-
/* Reset the cache */
88-
memset(start, 0, hdr->delta);
89-
90-
hdr->delta = 0;
91-
hdr->total_size = temp_storage_size - sizeof(dsm_seg_hdr);
92-
}
93-
94-
char *
95-
get_cache_address(void)
96-
{
97-
dsm_seg_hdr *hdr;
98-
99-
Assert(LWLockHeldByMeInMode(&aqo_state->lock, LW_EXCLUSIVE) ||
100-
LWLockHeldByMeInMode(&aqo_state->lock, LW_SHARED));
101-
102-
if (aqo_state->dsm_handler != DSM_HANDLE_INVALID)
103-
{
104-
if (!seg)
105-
{
106-
/* Another process created the segment yet. Just attach to. */
107-
seg = dsm_attach(aqo_state->dsm_handler);
108-
dsm_pin_mapping(seg);
109-
before_shmem_exit(aqo_detach_shmem, (Datum) &aqo_state->dsm_handler);
110-
}
111-
112-
hdr = (dsm_seg_hdr *) dsm_segment_address(seg);
113-
}
114-
else
115-
{
116-
/*
117-
* First request for DSM cache in this instance.
118-
* Create the DSM segment. Pin it to live up to instance shutdown.
119-
* Don't forget to detach DSM segment before an exit.
120-
*/
121-
seg = dsm_create(temp_storage_size, 0);
122-
dsm_pin_mapping(seg);
123-
dsm_pin_segment(seg);
124-
aqo_state->dsm_handler = dsm_segment_handle(seg);
125-
before_shmem_exit(aqo_detach_shmem, (Datum) &aqo_state->dsm_handler);
126-
127-
hdr = (dsm_seg_hdr *) dsm_segment_address(seg);
128-
hdr->magic = AQO_SHARED_MAGIC;
129-
hdr->delta = 0;
130-
hdr->total_size = temp_storage_size - sizeof(dsm_seg_hdr);
131-
}
132-
133-
Assert(seg);
134-
Assert(hdr->magic == AQO_SHARED_MAGIC && hdr->total_size > 0);
135-
136-
return (char *) hdr + sizeof(dsm_seg_hdr);
137-
}
138-
139-
uint32
140-
get_dsm_cache_pos(uint32 size)
141-
{
142-
dsm_seg_hdr *hdr;
143-
uint32 pos;
144-
145-
Assert(LWLockHeldByMeInMode(&aqo_state->lock, LW_EXCLUSIVE) ||
146-
LWLockHeldByMeInMode(&aqo_state->lock, LW_SHARED));
147-
148-
(void) get_cache_address();
149-
hdr = (dsm_seg_hdr *) dsm_segment_address(seg);
150-
151-
if (free_space(hdr) < size || size == 0)
152-
elog(ERROR,
153-
"DSM cache can't allcoate a mem block. Required: %u, free: %u",
154-
size, free_space(hdr));
155-
156-
pos = hdr->delta;
157-
hdr->delta += size;
158-
Assert(free_space(hdr) >= 0);
159-
return pos;
160-
}
161-
162-
static void
163-
aqo_detach_shmem(int code, Datum arg)
164-
{
165-
if (seg != NULL)
166-
dsm_detach(seg);
167-
seg = NULL;
168-
}
169-
17022
void
17123
aqo_init_shmem(void)
17224
{
@@ -177,7 +29,6 @@ aqo_init_shmem(void)
17729
prev_shmem_startup_hook();
17830

17931
aqo_state = NULL;
180-
fss_htab = NULL;
18132
stat_htab = NULL;
18233
qtexts_htab = NULL;
18334
data_htab = NULL;
@@ -189,7 +40,6 @@ aqo_init_shmem(void)
18940
{
19041
/* First time through ... */
19142

192-
aqo_state->dsm_handler = DSM_HANDLE_INVALID;
19343
aqo_state->qtexts_dsa_handler = DSM_HANDLE_INVALID;
19444
aqo_state->data_dsa_handler = DSM_HANDLE_INVALID;
19545

@@ -208,13 +58,6 @@ aqo_init_shmem(void)
20858
LWLockInitialize(&aqo_state->queries_lock, LWLockNewTrancheId());
20959
}
21060

211-
info.keysize = sizeof(htab_key);
212-
info.entrysize = sizeof(htab_entry);
213-
fss_htab = ShmemInitHash("AQO hash",
214-
aqo_htab_max_items, aqo_htab_max_items,
215-
&info,
216-
HASH_ELEM | HASH_BLOBS);
217-
21861
info.keysize = sizeof(((StatEntry *) 0)->queryid);
21962
info.entrysize = sizeof(StatEntry);
22063
stat_htab = ShmemInitHash("AQO Stat HTAB", fs_max_items, fs_max_items,
@@ -280,7 +123,6 @@ aqo_memsize(void)
280123
Size size;
281124

282125
size = MAXALIGN(sizeof(AQOSharedState));
283-
size = add_size(size, hash_estimate_size(aqo_htab_max_items, sizeof(htab_entry)));
284126
size = add_size(size, hash_estimate_size(fs_max_items, sizeof(AQOSharedState)));
285127
size = add_size(size, hash_estimate_size(fs_max_items, sizeof(StatEntry)));
286128
size = add_size(size, hash_estimate_size(fs_max_items, sizeof(QueryTextEntry)));

Diff for: aqo_shared.h

-19
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,9 @@
1010

1111
#define AQO_SHARED_MAGIC 0x053163
1212

13-
typedef struct
14-
{
15-
/* XXX we assume this struct contains no padding bytes */
16-
uint64 fs;
17-
int64 fss;
18-
} htab_key;
19-
20-
typedef struct
21-
{
22-
htab_key key;
23-
uint32 hdr_off; /* offset of data in DSM cache */
24-
} htab_entry;
25-
2613
typedef struct AQOSharedState
2714
{
2815
LWLock lock; /* mutual exclusion */
29-
dsm_handle dsm_handler;
3016

3117
/* Storage fields */
3218
LWLock stat_lock; /* lock for access to stat storage */
@@ -50,16 +36,11 @@ typedef struct AQOSharedState
5036

5137
extern shmem_startup_hook_type prev_shmem_startup_hook;
5238
extern AQOSharedState *aqo_state;
53-
extern HTAB *fss_htab;
5439

5540
extern int fs_max_items; /* Max number of feature spaces that AQO can operate */
5641
extern int fss_max_items;
5742

5843
extern Size aqo_memsize(void);
59-
extern void reset_dsm_cache(void);
60-
extern void *get_dsm_all(uint32 *size);
61-
extern char *get_cache_address(void);
62-
extern uint32 get_dsm_cache_pos(uint32 size);
6344
extern void aqo_init_shmem(void);
6445

6546
#endif /* AQO_SHARED_H */

Diff for: cardinality_estimation.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ predict_for_relation(List *clauses, List *selectivities, List *relsigns,
8181
&ncols, &features);
8282
data = OkNNr_allocate(ncols);
8383

84-
if (load_fss_ext(query_context.fspace_hash, *fss, data, NULL, true))
84+
if (load_fss_ext(query_context.fspace_hash, *fss, data, NULL))
8585
result = OkNNr_predict(data, features);
8686
else
8787
{

Diff for: cardinality_hooks.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ predict_num_groups(PlannerInfo *root, Path *subpath, List *group_exprs,
452452
*fss = get_grouped_exprs_hash(child_fss, group_exprs);
453453
memset(&data, 0, sizeof(OkNNrdata));
454454

455-
if (!load_fss_ext(query_context.fspace_hash, *fss, &data, NULL, true))
455+
if (!load_fss_ext(query_context.fspace_hash, *fss, &data, NULL))
456456
return -1;
457457

458458
Assert(data.rows == 1);

Diff for: expected/statement_timeout.out

+23
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,29 @@ SELECT check_estimated_rows('SELECT *, pg_sleep(1) FROM t;');
111111
5
112112
(1 row)
113113

114+
-- Interrupted query should immediately appear in aqo_data
115+
SELECT 1 FROM aqo_reset();
116+
?column?
117+
----------
118+
1
119+
(1 row)
120+
121+
SET statement_timeout = 500;
122+
SELECT count(*) FROM aqo_data; -- Must be zero
123+
count
124+
-------
125+
0
126+
(1 row)
127+
128+
SELECT x, pg_sleep(0.1) FROM t WHERE x > 0;
129+
NOTICE: [AQO] Time limit for execution of the statement was expired. AQO tried to learn on partial data.
130+
ERROR: canceling statement due to statement timeout
131+
SELECT count(*) FROM aqo_data; -- Must be one
132+
count
133+
-------
134+
1
135+
(1 row)
136+
114137
SELECT 1 FROM aqo_reset();
115138
?column?
116139
----------

0 commit comments

Comments
 (0)