8000 Add a callback on an action of a transaction cleaning. · postgrespro/aqo@b46f0bb · GitHub
[go: up one dir, main page]

Skip to content

Commit b46f0bb

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 7887c27 commit b46f0bb

File tree

4 files changed

+32
-8
lines changed

4 files changed

+32
-8
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
{
@@ -208,6 +224,7 @@ _PG_init(void)
208224
AQOMemoryContext = AllocSetContextCreate(TopMemoryContext,
209225
"AQOMemoryContext",
210226
ALLOCSET_DEFAULT_SIZES);
227+
RegisterResourceReleaseCallback(aqo_free_callback, NULL);
211228
}
212229

213230
PG_FUNCTION_INFO_V1(invalidate_deactivated_queries_cache);

aqo.h

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

preprocessing.c

Lines changed: 13 additions & 6 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, JumbleState *jstate)
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, jstate);
@@ -235,8 +238,12 @@ aqo_planner(Query *parse,
235238
query_context.use_aqo,
236239
query_context.auto_tuning);
237240

238-
239-
add_query_text(query_context.query_hash, query_text);
241+
/*
242+
* Add query text into the ML-knowledge base. Just for further
243+
* analysis. In the case of cached plans we could have NULL query text.
244+
*/
245+
if (query_text != NULL)
246+
add_query_text(query_context.query_hash);
240247
}
241248
}
242249
else

storage.c

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

0 commit comments

Comments
 (0)
0