@@ -94,44 +94,53 @@ open_aqo_relation(char *heaprelnspname, char *heaprelname,
9494 *
9595 * Use dirty snapshot to see all (include in-progess) data. We want to prevent
9696 * wait in the XactLockTableWait routine.
97+ * If query is found in the knowledge base, fill the query context struct.
9798 */
9899bool
99- find_query (uint64 qhash ,Datum * search_values , bool * search_nulls )
100+ find_query (uint64 qhash ,QueryContextData * ctx )
100101{
101- Relation hrel ;
102- Relation irel ;
103- HeapTuple tuple ;
102+ Relation hrel ;
103+ Relation irel ;
104+ HeapTuple tuple ;
104105TupleTableSlot * slot ;
105- bool shouldFree ;
106- IndexScanDesc scan ;
107- ScanKeyData key ;
108- SnapshotData snap ;
109- bool find_ok = false;
106+ bool shouldFree = true;
107+ IndexScanDesc scan ;
108+ ScanKeyData key ;
109+ SnapshotData snap ;
110+ bool find_ok = false;
111+ Datum values [5 ];
112+ bool nulls [5 ]= {false, false, false, false, false};
110113
111114if (!open_aqo_relation ("public" ,"aqo_queries" ,"aqo_queries_query_hash_idx" ,
112115AccessShareLock ,& hrel ,& irel ))
113116return false;
114117
115118InitDirtySnapshot (snap );
116119scan = index_beginscan (hrel ,irel ,& snap ,1 ,0 );
117- ScanKeyInit (& key ,1 ,BTEqualStrategyNumber ,F_INT4EQ ,Int64GetDatum (qhash ));
120+ ScanKeyInit (& key ,1 ,BTEqualStrategyNumber ,F_INT8EQ ,Int64GetDatum (qhash ));
118121
119122index_rescan (scan ,& key ,1 ,NULL ,0 );
120123slot = MakeSingleTupleTableSlot (hrel -> rd_att ,& TTSOpsBufferHeapTuple );
121124find_ok = index_getnext_slot (scan ,ForwardScanDirection ,slot );
122125
123- if (find_ok && search_values != NULL )
126+ if (find_ok )
124127{
125128tuple = ExecFetchSlotHeapTuple (slot , true,& shouldFree );
126129Assert (shouldFree != true);
127- heap_deform_tuple (tuple ,hrel -> rd_att ,search_values ,search_nulls );
130+ heap_deform_tuple (tuple ,hrel -> rd_att ,values ,nulls );
131+
132+ /* Fill query context data */
133+ ctx -> learn_aqo = DatumGetBool (values [1 ]);
134+ ctx -> use_aqo = DatumGetBool (values [2 ]);
135+ ctx -> fspace_hash = DatumGetInt64 (values [3 ]);
136+ ctx -> auto_tuning = DatumGetBool (values [4 ]);
137+ ctx -> collect_stat = query_context .auto_tuning ;
128138}
129139
130140ExecDropSingleTupleTableSlot (slot );
131141index_endscan (scan );
132142index_close (irel ,AccessShareLock );
133143table_close (hrel ,AccessShareLock );
134-
135144return find_ok ;
136145}
137146
@@ -177,7 +186,7 @@ update_query(uint64 qhash, uint64 fhash,
177186 */
178187InitDirtySnapshot (snap );
179188scan = index_beginscan (hrel ,irel ,& snap ,1 ,0 );
180- ScanKeyInit (& key ,1 ,BTEqualStrategyNumber ,F_INT4EQ , Int32GetDatum (qhash ));
189+ ScanKeyInit (& key ,1 ,BTEqualStrategyNumber ,F_INT8EQ , Int64GetDatum (qhash ));
181190
182191index_rescan (scan ,& key ,1 ,NULL ,0 );
183192slot = MakeSingleTupleTableSlot (hrel -> rd_att ,& TTSOpsBufferHeapTuple );
@@ -222,7 +231,8 @@ update_query(uint64 qhash, uint64 fhash,
222231 * Ooops, somebody concurrently updated the tuple. It is possible
223232 * only in the case of changes made by third-party code.
224233 */
225- elog (ERROR ,"AQO feature space data for signature (%ld, %ld) concurrently"
234+ elog (ERROR ,"AQO feature space data for signature (" UINT64_FORMAT \
235+ ", " UINT64_FORMAT ") concurrently"
226236" updated by a stranger backend." ,
227237qhash ,fhash );
228238result = false;
@@ -284,7 +294,7 @@ add_query_text(uint64 qhash, const char *query_string)
284294 */
285295InitDirtySnapshot (snap );
286296scan = index_beginscan (hrel ,irel ,& snap ,1 ,0 );
287- ScanKeyInit (& key ,1 ,BTEqualStrategyNumber ,F_INT4EQ , Int32GetDatum (qhash ));
297+ ScanKeyInit (& key ,1 ,BTEqualStrategyNumber ,F_INT8EQ , Int64GetDatum (qhash ));
288298
289299index_rescan (scan ,& key ,1 ,NULL ,0 );
290300slot = MakeSingleTupleTableSlot (hrel -> rd_att ,& TTSOpsBufferHeapTuple );
@@ -391,7 +401,7 @@ load_fss(uint64 fhash, int fss_hash,
391401return false;
392402
393403scan = index_beginscan (hrel ,irel ,SnapshotSelf ,2 ,0 );
394- ScanKeyInit (& key [0 ],1 ,BTEqualStrategyNumber ,F_INT4EQ , Int32GetDatum (fhash ));
404+ ScanKeyInit (& key [0 ],1 ,BTEqualStrategyNumber ,F_INT8EQ , Int64GetDatum (fhash ));
395405ScanKeyInit (& key [1 ],2 ,BTEqualStrategyNumber ,F_INT4EQ ,Int32GetDatum (fss_hash ));
396406index_rescan (scan ,key ,2 ,NULL ,0 );
397407
@@ -423,9 +433,10 @@ load_fss(uint64 fhash, int fss_hash,
423433* relids = deform_oids_vector (values [5 ]);
424434}
425435else
426- elog (ERROR ,"unexpected number of features for hash (%ld, %d):\
427- expected %d features, obtained %d" ,
428- fhash ,fss_hash ,ncols ,DatumGetInt32 (values [2 ]));
436+ elog (ERROR ,"unexpected number of features for hash (" \
437+ UINT64_FORMAT ", %d):\
438+ expected %d features, obtained %d" ,
439+ fhash ,fss_hash ,ncols ,DatumGetInt32 (values [2 ]));
429440}
430441else
431442success = false;
@@ -484,7 +495,7 @@ update_fss(uint64 fhash, int fsshash, int nrows, int ncols,
484495InitDirtySnapshot (snap );
485496scan = index_beginscan (hrel ,irel ,& snap ,2 ,0 );
486497
487- ScanKeyInit (& key [0 ],1 ,BTEqualStrategyNumber ,F_INT4EQ , Int32GetDatum (fhash ));
498+ ScanKeyInit (& key [0 ],1 ,BTEqualStrategyNumber ,F_INT8EQ , Int64GetDatum (fhash ));
488499ScanKeyInit (& key [1 ],2 ,BTEqualStrategyNumber ,F_INT4EQ ,Int32GetDatum (fsshash ));
489500
490501index_rescan (scan ,key ,2 ,NULL ,0 );
@@ -494,7 +505,7 @@ update_fss(uint64 fhash, int fsshash, int nrows, int ncols,
494505
495506if (!find_ok )
496507{
497- values [0 ]= Int32GetDatum (fhash );
508+ values [0 ]= Int64GetDatum (fhash );
498509values [1 ]= Int32GetDatum (fsshash );
499510values [2 ]= Int32GetDatum (ncols );
500511
@@ -549,8 +560,8 @@ update_fss(uint64 fhash, int fsshash, int nrows, int ncols,
549560 * Ooops, somebody concurrently updated the tuple. It is possible
550561 * only in the case of changes made by third-party code.
551562 */
552- elog (ERROR ,"AQO data piece (%ld %d) concurrently updated "
553- " by a stranger backend." ,
563+ elog (ERROR ,"AQO data piece (" UINT64_FORMAT " %d) concurrently"
564+ "updated by a stranger backend." ,
554565fhash ,fsshash );
555566result = false;
556567}
@@ -596,7 +607,7 @@ get_aqo_stat(uint64 qhash)
596607return false;
597608
598609scan = index_beginscan (hrel ,irel ,SnapshotSelf ,1 ,0 );
599- ScanKeyInit (& key ,1 ,BTEqualStrategyNumber ,F_INT4EQ , Int32GetDatum (qhash ));
610+ ScanKeyInit (& key ,1 ,BTEqualStrategyNumber ,F_INT8EQ , Int64GetDatum (qhash ));
600611index_rescan (scan ,& key ,1 ,NULL ,0 );
601612slot = MakeSingleTupleTableSlot (hrel -> rd_att ,& TTSOpsBufferHeapTuple );
602613
@@ -667,7 +678,7 @@ update_aqo_stat(uint64 qhash, QueryStat *stat)
667678
668679InitDirtySnapshot (snap );
669680scan = index_beginscan (hrel ,irel ,& snap ,1 ,0 );
670- ScanKeyInit (& key ,1 ,BTEqualStrategyNumber ,F_INT4EQ ,Int64GetDatum (qhash ));
681+ ScanKeyInit (& key ,1 ,BTEqualStrategyNumber ,F_INT8EQ ,Int64GetDatum (qhash ));
671682index_rescan (scan ,& key ,1 ,NULL ,0 );
672683slot = MakeSingleTupleTableSlot (hrel -> rd_att ,& TTSOpsBufferHeapTuple );
673684
@@ -713,8 +724,8 @@ update_aqo_stat(uint64 qhash, QueryStat *stat)
713724 * Ooops, somebody concurrently updated the tuple. It is possible
714725 * only in the case of changes made by third-party code.
715726 */
716- elog (ERROR ,"AQO statistic data for query signature%ld concurrently"
717- " updated by a stranger backend." ,
727+ elog (ERROR ,"AQO statistic data for query signature" UINT64_FORMAT
728+ "concurrently updated by a stranger backend." ,
718729qhash );
719730}
720731}
@@ -914,8 +925,8 @@ init_deactivated_queries_storage(void)
914925
915926/* Create the hashtable proper */
916927MemSet (& hash_ctl ,0 ,sizeof (hash_ctl ));
917- hash_ctl .keysize = sizeof (int );
918- hash_ctl .entrysize = sizeof (int );
928+ hash_ctl .keysize = sizeof (uint64 );
929+ hash_ctl .entrysize = sizeof (uint64 );
919930deactivated_queries = hash_create ("aqo_deactivated_queries" ,
920931128 ,/* start small and extend */
921932& hash_ctl ,