Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit c19e4d0

Browse files
committed
Bugfix. Do away with possible conflict of hooks, declared as 'extern' in different libraries.
To avoid such a problem in future, refactor AQO interfaces: declare all hooks as static, reduce number of exporting functions and introduce concept of *_init() function for a module that needs some actions in the PG_init() routine. Reviewed by: @Anisimov-ds
1 parent 27f9b5b commit c19e4d0

14 files changed

+260
-375
lines changed

aqo.c

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@
1919

2020
#include "aqo.h"
2121
#include "aqo_shared.h"
22-
#include "cardinality_hooks.h"
2322
#include "path_utils.h"
24-
#include "preprocessing.h"
2523
#include "storage.h"
2624

2725

@@ -98,19 +96,6 @@ MemoryContext AQOLearnMemCtx = NULL;
9896
/* Additional plan info */
9997
int njoins;
10098

101-
/* Saved hook values */
102-
post_parse_analyze_hook_type prev_post_parse_analyze_hook;
103-
planner_hook_type prev_planner_hook;
104-
ExecutorStart_hook_type prev_ExecutorStart_hook;
105-
ExecutorRun_hook_type prev_ExecutorRun;
106-
ExecutorEnd_hook_type prev_ExecutorEnd_hook;
107-
set_baserel_rows_estimate_hook_type prev_set_foreign_rows_estimate_hook;
108-
set_baserel_rows_estimate_hook_type prev_set_baserel_rows_estimate_hook;
109-
get_parameterized_baserel_size_hook_type prev_get_parameterized_baserel_size_hook;
110-
set_joinrel_size_estimates_hook_type prev_set_joinrel_size_estimates_hook;
111-
get_parameterized_joinrel_size_hook_type prev_get_parameterized_joinrel_size_hook;
112-
ExplainOnePlan_hook_type prev_ExplainOnePlan_hook;
113-
ExplainOneNode_hook_type prev_ExplainOneNode_hook;
11499

115100
/*****************************************************************************
116101
*
@@ -324,6 +309,7 @@ _PG_init(void)
324309
NULL,
325310
NULL);
326311

312+
<<<<<<< HEAD
327313
prev_shmem_startup_hook = shmem_startup_hook;
328314
shmem_startup_hook = aqo_init_shmem;
329315
prev_planner_hook = planner_hook;
@@ -360,6 +346,13 @@ _PG_init(void)
360346

361347
prev_create_upper_paths_hook = create_upper_paths_hook;
362348
create_upper_paths_hook = aqo_store_upper_signature_hook;
349+
=======
350+
aqo_shmem_init();
351+
aqo_preprocessing_init();
352+
aqo_postprocessing_init();
353+
aqo_cardinality_hooks_init();
354+
aqo_path_utils_init();
355+
>>>>>>> daf05a0 (Bugfix. Do away with possible conflict of hooks, declared as 'extern' in)
363356

364357
init_deactivated_queries_storage();
365358

aqo.h

Lines changed: 6 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -132,19 +132,16 @@
132132
#include "nodes/nodeFuncs.h"
133133
#include "optimizer/pathnode.h"
134134
#include "optimizer/planner.h"
135-
#include "optimizer/cost.h"
136135
#include "parser/analyze.h"
137136
#include "parser/parsetree.h"
138137
#include "utils/builtins.h"
139138
#include "utils/guc.h"
140139
#include "utils/hsearch.h"
141140
#include "utils/memutils.h"
142141
#include "utils/rel.h"
143-
#include "utils/fmgroids.h"
144142
#include "utils/snapmgr.h"
145143

146144
#include "machine_learning.h"
147-
//#include "storage.h"
148145

149146
/* Check PostgreSQL version (9.6.0 contains important changes in planner) */
150147
#if PG_VERSION_NUM < 90600
@@ -237,58 +234,15 @@ extern MemoryContext AQOCacheMemCtx;
237234
extern MemoryContext AQOPredictMemCtx;
238235
extern MemoryContext AQOLearnMemCtx;
239236

240-
/* Saved hook values in case of unload */
241-
extern post_parse_analyze_hook_type prev_post_parse_analyze_hook;
242-
extern planner_hook_type prev_planner_hook;
243-
extern ExecutorStart_hook_type prev_ExecutorStart_hook;
244-
extern ExecutorRun_hook_type prev_ExecutorRun;
245-
extern ExecutorEnd_hook_type prev_ExecutorEnd_hook;
246-
extern set_baserel_rows_estimate_hook_type
247-
prev_set_foreign_rows_estimate_hook;
248-
extern set_baserel_rows_estimate_hook_type
249-
prev_set_baserel_rows_estimate_hook;
250-
extern get_parameterized_baserel_size_hook_type
251-
prev_get_parameterized_baserel_size_hook;
252-
extern set_joinrel_size_estimates_hook_type
253-
prev_set_joinrel_size_estimates_hook;
254-
extern get_parameterized_joinrel_size_hook_type
255-
prev_get_parameterized_joinrel_size_hook;
256-
extern ExplainOnePlan_hook_type prev_ExplainOnePlan_hook;
257-
extern ExplainOneNode_hook_type prev_ExplainOneNode_hook;
258-
259-
extern void ppi_hook(ParamPathInfo *ppi);
260237
extern int aqo_statement_timeout;
261238

262-
/* Hash functions */
263-
void get_eclasses(List *clauselist, int *nargs, int **args_hash,
264-
int **eclass_hash);
265-
int get_clause_hash(Expr *clause, int nargs, int *args_hash, int *eclass_hash);
266-
267-
268-
/* Storage interaction */
269-
extern bool load_fss_ext(uint64 fs, int fss, OkNNrdata *data, List **reloids);
270-
extern bool update_fss_ext(uint64 fs, int fss, OkNNrdata *data, List *reloids);
271-
272-
/* Query preprocessing hooks */
273-
extern void print_into_explain(PlannedStmt *plannedstmt, IntoClause *into,
274-
ExplainState *es, const char *queryString,
275-
ParamListInfo params,
276-
const instr_time *planduration,
277-
QueryEnvironment *queryEnv);
278-
extern void print_node_explain(ExplainState *es, PlanState *ps, Plan *plan);
279-
280239
/* Cardinality estimation */
281240
extern double predict_for_relation(List *restrict_clauses, List *selectivities,
282241
List *relsigns, int *fss);
283242

284-
/* Query execution statistics collecting hooks */
285-
void aqo_ExecutorStart(QueryDesc *queryDesc, int eflags);
286-
void aqo_ExecutorRun(QueryDesc *queryDesc, ScanDirection direction,
287-
uint64 count, bool execute_once);
288-
void aqo_ExecutorEnd(QueryDesc *queryDesc);
289-
290243
/* Automatic query tuning */
291244
extern void automatical_query_tuning(uint64 query_hash, struct StatEntry *stat);
245+
extern double get_mean(double *elems, int nelems);
292246

293247
/* Utilities */
294248
extern int int_cmp(const void *a, const void *b);
@@ -306,8 +260,10 @@ extern void selectivity_cache_clear(void);
306260

307261
extern bool IsQueryDisabled(void);
308262

309-
extern bool update_query_timeout(uint64 queryid, int64 smart_timeout);
310-
extern double get_mean(double *elems, int nelems);
311-
312263
extern List *cur_classes;
264+
265+
extern void aqo_cardinality_hooks_init(void);
266+
extern void aqo_preprocessing_init(void);
267+
extern void aqo_postprocessing_init(void);
268+
313269
#endif

aqo_shared.c

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,30 @@
66

77
#include "lib/dshash.h"
88
#include "miscadmin.h"
9+
#include "storage/ipc.h"
910
#include "storage/shmem.h"
1011

1112
#include "aqo_shared.h"
1213
#include "storage.h"
1314

1415

15-
shmem_startup_hook_type prev_shmem_startup_hook = NULL;
1616
AQOSharedState *aqo_state = NULL;
1717
int fs_max_items = 10000; /* Max number of different feature spaces in ML model */
1818
int fss_max_items = 100000; /* Max number of different feature subspaces in ML model */
1919

20+
static shmem_startup_hook_type aqo_shmem_startup_next = NULL;
21+
static shmem_request_hook_type aqo_shmem_request_next = NULL;
22+
2023
static void on_shmem_shutdown(int code, Datum arg);
2124

22-
void
25+
static void
2326
aqo_init_shmem(void)
2427
{
2528
bool found;
2629
HASHCTL info;
2730

28-
if (prev_shmem_startup_hook)
29-
prev_shmem_startup_hook();
31+
if (aqo_shmem_startup_next)
32+
aqo_shmem_startup_next();
3033

3134
aqo_state = NULL;
3235
stat_htab = NULL;
@@ -116,10 +119,17 @@ on_shmem_shutdown(int code, Datum arg)
116119
return;
117120
}
118121

119-
Size
120-
aqo_memsize(void)
122+
123+
/*
124+
* Requests any additional shared memory required for aqo.
125+
*/
126+
static void
127+
aqo_shmem_request(void)
121128
{
122-
Size size;
129+
Size size;
130+
131+
if (aqo_shmem_request_next)
132+
aqo_shmem_request_next();
123133

124134
size = MAXALIGN(sizeof(AQOSharedState));
125135
size = add_size(size, hash_estimate_size(fs_max_items, sizeof(AQOSharedState)));
@@ -128,5 +138,14 @@ aqo_memsize(void)
128138
size = add_size(size, hash_estimate_size(fss_max_items, sizeof(DataEntry)));
129139
size = add_size(size, hash_estimate_size(fs_max_items, sizeof(QueriesEntry)));
130140

131-
return size;
141+
RequestAddinShmemSpace(size);
142+
}
143+
144+
void
145+
aqo_shmem_init(void)
146+
{
147+
aqo_shmem_startup_next = shmem_startup_hook;
148+
shmem_startup_hook = aqo_init_shmem;
149+
aqo_shmem_request_next = shmem_request_hook;
150+
shmem_request_hook = aqo_shmem_request;
132151
}

aqo_shared.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
#ifndef AQO_SHARED_H
22
#define AQO_SHARED_H
33

4-
#include "lib/dshash.h"
5-
#include "storage/dsm.h"
6-
#include "storage/ipc.h"
74
#include "storage/lwlock.h"
85
#include "utils/dsa.h"
96

@@ -31,13 +28,11 @@ typedef struct AQOSharedState
3128
} AQOSharedState;
3229

3330

34-
extern shmem_startup_hook_type prev_shmem_startup_hook;
3531
extern AQOSharedState *aqo_state;
3632

3733
extern int fs_max_items; /* Max number of feature spaces that AQO can operate */
3834
extern int fss_max_items;
3935

40-
extern Size aqo_memsize(void);
41-
extern void aqo_init_shmem(void);
36+
extern void aqo_shmem_init(void);
4237

4338
#endif /* AQO_SHARED_H */

0 commit comments

Comments
 (0)