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

Commit 0148e20

Browse files
committed
Add a callback on an action of a transaction cleaning.
Now AQO could clean global data, such of query_text at the end of transaction. It is needed because errors during query execution phases. We need to be sure that all such data cleaned before the next query execution. Interface of the add_query_text routine changed.
1 parent 31fb3bd commit 0148e20

File tree

4 files changed

+33
-9
lines changed

4 files changed

+33
-9
lines changed

aqo.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,22 @@ ExplainOneNode_hook_type prev_ExplainOneNode_hook;
111111
*
112112
*****************************************************************************/
113113

114+
static void
115+
aqo_free_callback(ResourceReleasePhase phase,
116+
bool isCommit,
117+
bool isTopLevel,
118+
void *arg)
119+
{
120+
if (phase != RESOURCE_RELEASE_AFTER_LOCKS)
121+
return;
122+
123+
if (query_text != NULL)
124+
{
125+
pfree(query_text);
126+
query_text = NULL;
127+
}
128+
}
129+
114130
void
115131
_PG_init(void)
116132
{
@@ -220,6 +236,7 @@ _PG_init(void)
220236
AQOMemoryContext = AllocSetContextCreate(TopMemoryContext,
221237
"AQOMemoryContext",
222238
ALLOCSET_DEFAULT_SIZES);
239+
RegisterResourceReleaseCallback(aqo_free_callback, NULL);
223240
}
224241

225242
PG_FUNCTION_INFO_V1(invalidate_deactivated_queries_cache);

aqo.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ int get_clause_hash(Expr *clause, int nargs,
286286
extern bool find_query(int qhash, Datum *search_values, bool *search_nulls);
287287
extern bool update_query(int qhash, int fhash,
288288
bool learn_aqo, bool use_aqo, bool auto_tuning);
289-
extern bool add_query_text(int query_hash, char *query_text);
289+
extern bool add_query_text(int query_hash);
290290
extern bool load_fss(int fhash, int fss_hash,
291291
int ncols, double **matrix, double *targets, int *rows);
292292
extern bool update_fss(int fhash, int fss_hash, int nrows, int ncols,

preprocessing.c

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,16 +71,19 @@ static bool isQueryUsingSystemRelation_walker(Node *node, void *context);
7171
void
7272
get_query_text(ParseState *pstate, Query *query)
7373
{
74-
MemoryContext oldCxt;
75-
7674
/*
7775
* Duplicate query string into private AQO memory context for guard
7876
* from possible memory context switching.
7977
*/
80-
oldCxt = MemoryContextSwitchTo(AQOMemoryContext);
8178
if (pstate)
79+
{
80+
MemoryContext oldCxt = MemoryContextSwitchTo(AQOMemoryContext);
8281
query_text = pstrdup(pstate->p_sourcetext);
83-
MemoryContextSwitchTo(oldCxt);
82+
MemoryContextSwitchTo(oldCxt);
83+
}
84+
else
85+
/* Can't imagine such case. Still, throw an error. */
86+
elog(ERROR, "[AQO]: Query text is not found in post-parse step");
8487

8588
if (prev_post_parse_analyze_hook)
8689
prev_post_parse_analyze_hook(pstate, query);
@@ -97,7 +100,7 @@ call_default_planner(Query *parse, int cursorOptions, ParamListInfo boundParams)
97100
else
98101
return standard_planner(parse, cursorOptions, boundParams);
99102
}
100-
#include "tcop/tcopprot.h"
103+
101104
/*
102105
* Before query optimization we determine machine learning settings
103106
* for the query.
@@ -218,8 +221,12 @@ aqo_planner(Query *parse, int cursorOptions, ParamListInfo boundParams)
218221
query_context.use_aqo,
219222
query_context.auto_tuning);
220223

221-
222-
add_query_text(query_context.query_hash, query_text);
224+
/*
225+
* Add query text into the ML-knowledge base. Just for further
226+
* analysis. In the case of cached plans we could have NULL query text.
227+
*/
228+
if (query_text != NULL)
229+
add_query_text(query_context.query_hash);
223230
}
224231
}
225232
else

storage.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ update_query(int qhash, int fhash,
214214
* Returns false if the operation failed, true otherwise.
215215
*/
216216
bool
217-
add_query_text(int qhash, char *query_text)
217+
add_query_text(int qhash)
218218
{
219219
RangeVar *rv;
220220
Relation hrel;

0 commit comments

Comments
 (0)