8000 Performance commit. The get_extension_oid routine is too heavy. · postgrespro/aqo@15b664f · GitHub
[go: up one dir, main page]

Skip to content

Commit 15b664f

Browse files
committed
Performance commit. The get_extension_oid routine is too heavy.
Pointlessly to call it on each planning hook execution.
1 parent 6671528 commit 15b664f

File tree

4 files changed

+45
-4
lines changed

4 files changed

+45
-4
lines changed

aqo.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ void _PG_init(void);
3131

3232
/* Strategy of determining feature space for new queries. */
3333
int aqo_mode;
34+
bool aqo_enabled = false; /* Signals that CREATE EXTENSION have executed and
35+
all extension tables is ready for use. */
3436
bool force_collect_stat;
3537

3638
/*

aqo.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ typedef enum
168168
} AQO_MODE;
169169

170170
extern int aqo_mode;
171+
extern bool aqo_enabled;
171172
extern bool force_collect_stat;
172173
extern bool aqo_show_hash;
173174
extern bool aqo_show_details;

preprocessing.c

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,30 @@ call_default_planner(Query *parse,
9393
boundParams);
9494
}
9595

96+
/*
97+
* Check, that a 'CREATE EXTENSION aqo' command has been executed.
98+
* This function allows us to execute the get_extension_oid routine only once
99+
* at each backend.
100+
* If any AQO-related table is missed we will set aqo_enabled to false (see
101+
* a storage implementation module).
102+
*/
103+
static bool
104+
aqoIsEnabled(void)
105+
{
106+
if (creating_extension)
107+
/* Nothing to tell in this mode. */
108+
return false;
109+
110+
if (aqo_enabled)
111+
/* Fast path. Dropping should be detected by absence of any table. */
112+
return true;
113+
114+
if (get_extension_oid("aqo", true) != InvalidOid)
115+
aqo_enabled = true;
116+
117+
return aqo_enabled;
118+
}
119+
96120
/*
97121
* Before query optimization we determine machine learning settings
98122
* for the query.
@@ -120,18 +144,23 @@ aqo_planner(Query *parse,
120144
* the heap during planning. Transactions is synchronized between parallel
121145
* sections. See GetCurrentCommandId() comments also.
122146
*/
123-
if ((parse->commandType != CMD_SELECT && parse->commandType != CMD_INSERT &&
147+
if (!aqoIsEnabled() ||
148+
(parse->commandType != CMD_SELECT && parse->commandType != CMD_INSERT &&
124149
parse->commandType != CMD_UPDATE && parse->commandType != CMD_DELETE) ||
125-
strstr(application_name, "postgres_fdw") != NULL || /* Prevent distributed deadlocks */
126-
strstr(application_name, "pgfdw:") != NULL || /* caused by fdw */
127-
get_extension_oid("aqo", true) == InvalidOid ||
128150
creating_extension ||
129151
IsInParallelMode() || IsParallelWorker() ||
130152
(aqo_mode == AQO_MODE_DISABLED && !force_collect_stat) ||
153+
strstr(application_name, "postgres_fdw") != NULL || /* Prevent distributed deadlocks */
154+
strstr(application_name, "pgfdw:") != NULL || /* caused by fdw */
131155
isQueryUsingSystemRelation(parse) ||
132156
RecoveryInProgress())
133157
{
158+
/*
159+
* We should disable AQO for this query to remember this decision along
160+
* all execution stages.
161+
*/
134162
disable_aqo_for_query();
163+
135164
return call_default_planner(parse,
136165
query_string,
137166
cursorOptions,

storage.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,16 @@ open_aqo_relation(char *heaprelnspname, char *heaprelname,
6060
*hrel = table_openrv_extended(rv, lockmode, true);
6161
if (!OidIsValid(reloid) || *hrel == NULL)
6262
{
63+
/*
64+
* Absence of any AQO-related table tell us that someone executed
65+
* a 'DROP EXTENSION aqo' command. We disable AQO for all future queries
66+
* in this backend. For performance reasons we do it locally.
67+
* Also, we gently disable AQO for the rest of the current query
68+
* execution process.
69+
*/
70+
aqo_enabled = false;
6371
disable_aqo_for_query();
72+
6473
return false;
6574
}
6675

0 commit comments

Comments
 (0)
0