Skip to content

Commit 98b3624

Browse files
committed
Minor performance optimizations and code improvements
1 parent e4cef06 commit 98b3624

File tree

8 files changed

+78
-117
lines changed

8 files changed

+78
-117
lines changed

aqo.c

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ void _PG_init(void);
3434

3535
/* Strategy of determining feature space for new queries. */
3636
int aqo_mode;
37-
bool aqo_enabled = false; /* Signals that CREATE EXTENSION have executed and
38-
all extension tables is ready for use. */
3937
bool force_collect_stat;
4038

4139
/*
@@ -200,18 +198,6 @@ _PG_init(void)
200198
NULL
201199
);
202200

203-
DefineCustomBoolVariable(
204-
"aqo.use_file_storage",
205-
"Used for smooth transition from table storage",
206-
NULL,
207-
&aqo_use_file_storage,
208-
true,
209-
PGC_USERSET,
210-
0,
211-
NULL,
212-
NULL,
213-
NULL
214-
);
215201
DefineCustomIntVariable("aqo.join_threshold",
216202
"Sets the threshold of number of JOINs in query beyond which AQO is used.",
217203
NULL,

aqo.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,6 @@ typedef enum
169169
} AQO_MODE;
170170

171171
extern int aqo_mode;
172-
extern bool aqo_enabled;
173172
extern bool force_collect_stat;
174173
extern bool aqo_show_hash;
175174
extern bool aqo_show_details;

cardinality_estimation.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "aqo.h"
2323
#include "hash.h"
2424
#include "machine_learning.h"
25+
#include "storage.h"
2526

2627
#ifdef AQO_DEBUG_PRINT
2728
static void

expected/forced_stat_collection.out

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
\set citizens 1000
22
SET aqo.join_threshold = 0;
33
SET aqo.mode = 'disabled';
4-
SET aqo.force_collect_stat = 'on';
4+
SET aqo.force_collect_stat = 'off';
55
CREATE TABLE person (
66
id serial PRIMARY KEY,
77
age integer,
@@ -20,6 +20,7 @@ INSERT INTO person (id,age,gender,passport)
2020
FROM (SELECT *, 14+(id % 60) AS age FROM generate_series(1, :citizens) id) AS q1
2121
);
2222
CREATE EXTENSION aqo;
23+
SET aqo.force_collect_stat = 'on';
2324
SELECT count(*) FROM person WHERE age<18;
2425
count
2526
-------

preprocessing.c

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -98,30 +98,18 @@ call_default_planner(Query *parse,
9898
}
9999

100100
/*
101-
* Check, that a 'CREATE EXTENSION aqo' command has been executed.
102-
* This function allows us to execute the get_extension_oid routine only once
103-
* at each backend.
104-
* If any AQO-related table is missed we will set aqo_enabled to false (see
105-
* a storage implementation module).
101+
* Can AQO be used for the query?
106102
*/
107103
static bool
108-
aqoIsEnabled(void)
104+
aqoIsEnabled(Query *parse)
109105
{
110-
if (creating_extension)
111-
/* Nothing to tell in this mode. */
106+
if (creating_extension ||
107+
(aqo_mode == AQO_MODE_DISABLED && !force_collect_stat) ||
108+
(parse->commandType != CMD_SELECT && parse->commandType != CMD_INSERT &&
109+
parse->commandType != CMD_UPDATE && parse->commandType != CMD_DELETE))
112110
return false;
113111

114-
if (aqo_enabled)
115-
/*
116-
* Fast path. Dropping should be detected by absence of any AQO-related
117-
* table.
118-
*/
119-
return true;
120-
121-
if (get_extension_oid("aqo", true) != InvalidOid)
122-
aqo_enabled = true;
123-
124-
return aqo_enabled;
112+
return true;
125113
}
126114

127115
/*
@@ -147,12 +135,8 @@ aqo_planner(Query *parse,
147135
* the heap during planning. Transactions are synchronized between parallel
148136
* sections. See GetCurrentCommandId() comments also.
149137
*/
150-
if (!aqoIsEnabled() ||
151-
(parse->commandType != CMD_SELECT && parse->commandType != CMD_INSERT &&
152-
parse->commandType != CMD_UPDATE && parse->commandType != CMD_DELETE) ||
153-
creating_extension ||
138+
if (!aqoIsEnabled(parse) ||
154139
IsInParallelMode() || IsParallelWorker() ||
155-
(aqo_mode == AQO_MODE_DISABLED && !force_collect_stat) ||
156140
strstr(application_name, "postgres_fdw") != NULL || /* Prevent distributed deadlocks */
157141
strstr(application_name, "pgfdw:") != NULL || /* caused by fdw */
158142
isQueryUsingSystemRelation(parse) ||

sql/forced_stat_collection.sql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
SET aqo.join_threshold = 0;
44
SET aqo.mode = 'disabled';
5-
SET aqo.force_collect_stat = 'on';
5+
SET aqo.force_collect_stat = 'off';
66

77
CREATE TABLE person (
88
id serial PRIMARY KEY,
@@ -24,6 +24,7 @@ INSERT INTO person (id,age,gender,passport)
2424
);
2525

2626
CREATE EXTENSION aqo;
27+
SET aqo.force_collect_stat = 'on';
2728

2829
SELECT count(*) FROM person WHERE age<18;
2930
SELECT count(*) FROM person WHERE age<18 AND passport IS NOT NULL;

storage.c

Lines changed: 65 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,9 @@
1919

2020
#include <unistd.h>
2121

22-
#include "access/heapam.h"
23-
#include "access/table.h"
24-
#include "access/tableam.h"
22+
#include "funcapi.h"
2523
#include "miscadmin.h"
24+
#include "pgstat.h"
2625

2726
#include "aqo.h"
2827
#include "aqo_shared.h"
@@ -31,12 +30,73 @@
3130
#include "learn_cache.h"
3231
#include "storage.h"
3332

34-
#define AQO_DATA_COLUMNS (7)
33+
34+
/* AQO storage file names */
35+
#define PGAQO_STAT_FILE PGSTAT_STAT_PERMANENT_DIRECTORY "/pgaqo_statistics.stat"
36+
#define PGAQO_TEXT_FILE PGSTAT_STAT_PERMANENT_DIRECTORY "/pgaqo_query_texts.stat"
37+
#define PGAQO_DATA_FILE PGSTAT_STAT_PERMANENT_DIRECTORY "/pgaqo_data.stat"
38+
#define PGAQO_QUERIES_FILE PGSTAT_STAT_PERMANENT_DIRECTORY "/pgaqo_queries.stat"
39+
40+
#define AQO_DATA_COLUMNS (7)
41+
#define FormVectorSz(v_name) (form_vector((v_name), (v_name ## _size)))
42+
43+
44+
typedef enum {
45+
QUERYID = 0, EXEC_TIME_AQO, EXEC_TIME, PLAN_TIME_AQO, PLAN_TIME,
46+
EST_ERROR_AQO, EST_ERROR, NEXECS_AQO, NEXECS, TOTAL_NCOLS
47+
} aqo_stat_cols;
48+
49+
typedef enum {
50+
QT_QUERYID = 0, QT_QUERY_STRING, QT_TOTAL_NCOLS
51+
} aqo_qtexts_cols;
52+
53+
typedef enum {
54+
AD_FS = 0, AD_FSS, AD_NFEATURES, AD_FEATURES, AD_TARGETS, AD_RELIABILITY,
55+
AD_OIDS, AD_TOTAL_NCOLS
56+
} aqo_data_cols;
57+
58+
typedef enum {
59+
AQ_QUERYID = 0, AQ_FS, AQ_LEARN_AQO, AQ_USE_AQO, AQ_AUTO_TUNING,
60+
AQ_TOTAL_NCOLS
61+
} aqo_queries_cols;
62+
63+
typedef void* (*form_record_t) (void *ctx, size_t *size);
64+
typedef void (*deform_record_t) (void *data, size_t size);
65+
66+
67+
HTAB *stat_htab = NULL;
68+
HTAB *queries_htab = NULL;
69+
HTAB *qtexts_htab = NULL;
70+
dsa_area *qtext_dsa = NULL;
71+
HTAB *data_htab = NULL;
72+
dsa_area *data_dsa = NULL;
3573
HTAB *deactivated_queries = NULL;
3674

75+
/* Used to check data file consistency */
76+
static const uint32 PGAQO_FILE_HEADER = 123467589;
77+
static const uint32 PGAQO_PG_MAJOR_VERSION = PG_VERSION_NUM / 100;
78+
79+
3780
static ArrayType *form_matrix(double *matrix, int nrows, int ncols);
81+
static void dsa_init(void);
82+
static int data_store(const char *filename, form_record_t callback,
83+
long nrecs, void *ctx);
84+
static void data_load(const char *filename, deform_record_t callback, void *ctx);
85+
static size_t _compute_data_dsa(const DataEntry *entry);
86+
87+
PG_FUNCTION_INFO_V1(aqo_query_stat);
88+
PG_FUNCTION_INFO_V1(aqo_query_texts);
89+
PG_FUNCTION_INFO_V1(aqo_data);
90+
PG_FUNCTION_INFO_V1(aqo_queries);
91+
PG_FUNCTION_INFO_V1(aqo_stat_remove);
92+
PG_FUNCTION_INFO_V1(aqo_qtexts_remove);
93+
PG_FUNCTION_INFO_V1(aqo_data_remove);
94+
PG_FUNCTION_INFO_V1(aqo_queries_remove);
95+
PG_FUNCTION_INFO_V1(aqo_enable_query);
96+
PG_FUNCTION_INFO_V1(aqo_disable_query);
97+
PG_FUNCTION_INFO_V1(aqo_queries_update);
98+
PG_FUNCTION_INFO_V1(aqo_reset);
3899

39-
#define FormVectorSz(v_name) (form_vector((v_name), (v_name ## _size)))
40100

41101
bool
42102
load_fss_ext(uint64 fs, int fss, OkNNrdata *data, List **reloids, bool isSafe)
@@ -141,75 +201,6 @@ add_deactivated_query(uint64 queryid)
141201
hash_search(deactivated_queries, &queryid, HASH_ENTER, NULL);
142202
}
143203

144-
/* *****************************************************************************
145-
*
146-
* Implementation of the AQO file storage
147-
*
148-
**************************************************************************** */
149-
150-
#include "funcapi.h"
151-
#include "pgstat.h"
152-
153-
#define PGAQO_STAT_FILE PGSTAT_STAT_PERMANENT_DIRECTORY "/pgaqo_statistics.stat"
154-
#define PGAQO_TEXT_FILE PGSTAT_STAT_PERMANENT_DIRECTORY "/pgaqo_query_texts.stat"
155-
#define PGAQO_DATA_FILE PGSTAT_STAT_PERMANENT_DIRECTORY "/pgaqo_data.stat"
156-
#define PGAQO_QUERIES_FILE PGSTAT_STAT_PERMANENT_DIRECTORY "/pgaqo_queries.stat"
157-
158-
PG_FUNCTION_INFO_V1(aqo_query_stat);
159-
PG_FUNCTION_INFO_V1(aqo_query_texts);
160-
PG_FUNCTION_INFO_V1(aqo_data);
161-
PG_FUNCTION_INFO_V1(aqo_queries);
162-
PG_FUNCTION_INFO_V1(aqo_stat_remove);
163-
PG_FUNCTION_INFO_V1(aqo_qtexts_remove);
164-
PG_FUNCTION_INFO_V1(aqo_data_remove);
165-
PG_FUNCTION_INFO_V1(aqo_queries_remove);
166-
PG_FUNCTION_INFO_V1(aqo_enable_query);
167-
PG_FUNCTION_INFO_V1(aqo_disable_query);
168-
PG_FUNCTION_INFO_V1(aqo_queries_update);
169-
PG_FUNCTION_INFO_V1(aqo_reset);
170-
171-
typedef enum {
172-
QUERYID = 0, EXEC_TIME_AQO, EXEC_TIME, PLAN_TIME_AQO, PLAN_TIME,
173-
EST_ERROR_AQO, EST_ERROR, NEXECS_AQO, NEXECS, TOTAL_NCOLS
174-
} aqo_stat_cols;
175-
176-
typedef enum {
177-
QT_QUERYID = 0, QT_QUERY_STRING, QT_TOTAL_NCOLS
178-
} aqo_qtexts_cols;
179-
180-
typedef enum {
181-
AD_FS = 0, AD_FSS, AD_NFEATURES, AD_FEATURES, AD_TARGETS, AD_RELIABILITY,
182-
AD_OIDS, AD_TOTAL_NCOLS
183-
} aqo_data_cols;
184-
185-
typedef enum {
186-
AQ_QUERYID = 0, AQ_FS, AQ_LEARN_AQO, AQ_USE_AQO, AQ_AUTO_TUNING,
187-
AQ_TOTAL_NCOLS
188-
} aqo_queries_cols;
189-
190-
typedef void* (*form_record_t) (void *ctx, size_t *size);
191-
typedef void (*deform_record_t) (void *data, size_t size);
192-
193-
bool aqo_use_file_storage;
194-
195-
HTAB *stat_htab = NULL;
196-
HTAB *queries_htab = NULL;
197-
198-
HTAB *qtexts_htab = NULL;
199-
dsa_area *qtext_dsa = NULL;
200-
201-
HTAB *data_htab = NULL;
202-
dsa_area *data_dsa = NULL;
203-
204-
/* Used to check data file consistency */
205-
static const uint32 PGAQO_FILE_HEADER = 123467589;
206-
static const uint32 PGAQO_PG_MAJOR_VERSION = PG_VERSION_NUM / 100;
207-
208-
static void dsa_init(void);
209-
static int data_store(const char *filename, form_record_t callback,
210-
long nrecs, void *ctx);
211-
static void data_load(const char *filename, deform_record_t callback, void *ctx);
212-
static size_t _compute_data_dsa(const DataEntry *entry);
213204
/*
214205
* Update AQO statistics.
215206
*

storage.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,6 @@ typedef struct QueriesEntry
8282
bool auto_tuning;
8383
} QueriesEntry;
8484

85-
extern bool aqo_use_file_storage;
86-
8785
extern HTAB *stat_htab;
8886
extern HTAB *qtexts_htab;
8987
extern HTAB *queries_htab; /* TODO */

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