4141
4242
4343typedef enum {
44- QUERYID = 0 , EXEC_TIME_AQO , EXEC_TIME , PLAN_TIME_AQO , PLAN_TIME ,
44+ QUERYID = 0 , DBID , EXEC_TIME_AQO , EXEC_TIME , PLAN_TIME_AQO , PLAN_TIME ,
4545 EST_ERROR_AQO , EST_ERROR , NEXECS_AQO , NEXECS , TOTAL_NCOLS
4646} aqo_stat_cols ;
4747
4848typedef enum {
49- QT_QUERYID = 0 , QT_QUERY_STRING , QT_TOTAL_NCOLS
49+ QT_QUERYID = 0 , QT_DBID , QT_QUERY_STRING , QT_TOTAL_NCOLS
5050} aqo_qtexts_cols ;
5151
5252typedef enum {
53- AD_FS = 0 , AD_FSS , AD_NFEATURES , AD_FEATURES , AD_TARGETS , AD_RELIABILITY ,
53+ AD_FS = 0 , AD_FSS , AD_DBID , AD_NFEATURES , AD_FEATURES , AD_TARGETS , AD_RELIABILITY ,
5454 AD_OIDS , AD_TOTAL_NCOLS
5555} aqo_data_cols ;
5656
5757typedef enum {
58- AQ_QUERYID = 0 , AQ_FS , AQ_LEARN_AQO , AQ_USE_AQO , AQ_AUTO_TUNING , AQ_SMART_TIMEOUT , AQ_COUNT_INCREASE_TIMEOUT ,
58+ AQ_QUERYID = 0 , AQ_DBID , AQ_FS , AQ_LEARN_AQO , AQ_USE_AQO , AQ_AUTO_TUNING , AQ_SMART_TIMEOUT , AQ_COUNT_INCREASE_TIMEOUT ,
5959 AQ_TOTAL_NCOLS
6060} aqo_queries_cols ;
6161
@@ -423,10 +423,8 @@ aqo_query_stat(PG_FUNCTION_ARGS)
423423 hash_seq_init (& hash_seq , stat_htab );
424424 while ((entry = hash_seq_search (& hash_seq )) != NULL )
425425 {
426- if (entry -> key .dbid != (uint64 ) MyDatabaseId )
427- continue ;
428-
429426 values [QUERYID ] = Int64GetDatum (entry -> key .queryid );
427+ values [DBID ] = ObjectIdGetDatum (entry -> key .dbid );
430428 values [NEXECS ] = Int64GetDatum (entry -> execs_without_aqo );
431429 values [NEXECS_AQO ] = Int64GetDatum (entry -> execs_with_aqo );
432430 values [EXEC_TIME_AQO ] = PointerGetDatum (form_vector (entry -> exec_time_aqo , entry -> cur_stat_slot_aqo ));
@@ -1201,12 +1199,10 @@ aqo_query_texts(PG_FUNCTION_ARGS)
12011199 {
12021200 char * ptr ;
12031201
1204- if (entry -> key .dbid != (uint64 ) (uint64 ) MyDatabaseId )
1205- continue ;
1206-
12071202 Assert (DsaPointerIsValid (entry -> qtext_dp ));
12081203 ptr = dsa_get_address (qtext_dsa , entry -> qtext_dp );
12091204 values [QT_QUERYID ] = Int64GetDatum (entry -> key .queryid );
1205+ values [QT_DBID ] = ObjectIdGetDatum (entry -> key .dbid );
12101206 values [QT_QUERY_STRING ] = CStringGetTextDatum (ptr );
12111207 tuplestore_putvalues (tupstore , tupDesc , values , nulls );
12121208 }
@@ -1220,13 +1216,14 @@ static bool
12201216_aqo_stat_remove (uint64 queryid )
12211217{
12221218 bool found ;
1223- stat_key key = {.queryid = queryid , .dbid = (uint64 ) (uint64 ) MyDatabaseId };
1219+ stat_key key = {.queryid = queryid , .dbid = (uint64 ) MyDatabaseId };
1220+ StatEntry * entry ;
12241221
12251222 Assert (!LWLockHeldByMe (& aqo_state -> stat_lock ));
12261223 LWLockAcquire (& aqo_state -> stat_lock , LW_EXCLUSIVE );
1227- ( void ) hash_search (stat_htab , & key , HASH_FIND , & found );
1224+ entry = ( StatEntry * ) hash_search (stat_htab , & key , HASH_FIND , & found );
12281225
1229- if (found )
1226+ if (found && entry -> key . dbid == ( uint64 ) MyDatabaseId )
12301227 {
12311228 (void ) hash_search (stat_htab , & key , HASH_REMOVE , NULL );
12321229 aqo_state -> stat_changed = true;
@@ -1239,15 +1236,16 @@ _aqo_stat_remove(uint64 queryid)
12391236static bool
12401237_aqo_queries_remove (uint64 queryid )
12411238{
1242- bool found ;
1243- queries_key key = {.queryid = queryid , .dbid = (uint64 ) (uint64 ) MyDatabaseId };
1239+ bool found ;
1240+ queries_key key = {.queryid = queryid , .dbid = (uint64 ) MyDatabaseId };
1241+ QueriesEntry * entry ;
12441242
12451243
12461244 Assert (!LWLockHeldByMe (& aqo_state -> queries_lock ));
12471245 LWLockAcquire (& aqo_state -> queries_lock , LW_EXCLUSIVE );
1248- ( void ) hash_search (queries_htab , & key , HASH_FIND , & found );
1246+ entry = ( QueriesEntry * ) hash_search (queries_htab , & key , HASH_FIND , & found );
12491247
1250- if (found )
1248+ if (found && entry -> key . dbid == ( uint64 ) MyDatabaseId )
12511249 {
12521250 (void ) hash_search (queries_htab , & key , HASH_REMOVE , NULL );
12531251 aqo_state -> queries_changed = true;
@@ -1262,7 +1260,7 @@ _aqo_qtexts_remove(uint64 queryid)
12621260{
12631261 bool found = false;
12641262 QueryTextEntry * entry ;
1265- qtext_key key = {.queryid = queryid , .dbid = (uint64 ) ( uint64 ) MyDatabaseId };
1263+ qtext_key key = {.queryid = queryid , .dbid = (uint64 ) MyDatabaseId };
12661264
12671265 dsa_init ();
12681266
@@ -1275,7 +1273,7 @@ _aqo_qtexts_remove(uint64 queryid)
12751273 */
12761274 entry = (QueryTextEntry * ) hash_search (qtexts_htab , & key , HASH_FIND ,
12771275 & found );
1278- if (found )
1276+ if (found && entry -> key . dbid == ( uint64 ) MyDatabaseId )
12791277 {
12801278 /* Free DSA memory, allocated for this record */
12811279 Assert (DsaPointerIsValid (entry -> qtext_dp ));
@@ -1299,7 +1297,7 @@ _aqo_data_remove(data_key *key)
12991297 LWLockAcquire (& aqo_state -> data_lock , LW_EXCLUSIVE );
13001298
13011299 entry = (DataEntry * ) hash_search (data_htab , key , HASH_FIND , & found );
1302- if (found )
1300+ if (found && entry -> key . dbid == ( uint64 ) MyDatabaseId )
13031301 {
13041302 /* Free DSA memory, allocated for this record */
13051303 Assert (DsaPointerIsValid (entry -> data_dp ));
@@ -1376,7 +1374,7 @@ aqo_data_store(uint64 fs, int fss, AqoDataArgs *data, List *reloids)
13761374{
13771375 DataEntry * entry ;
13781376 bool found ;
1379- data_key key = {.fs = fs , .fss = fss , .dbid = (uint64 ) ( uint64 ) MyDatabaseId };
1377+ data_key key = {.fs = fs , .fss = fss , .dbid = (uint64 ) MyDatabaseId };
13801378 int i ;
13811379 char * ptr ;
13821380 ListCell * lc ;
@@ -1827,13 +1825,11 @@ aqo_data(PG_FUNCTION_ARGS)
18271825 {
18281826 char * ptr ;
18291827
1830- if (entry -> key .dbid != (uint64 ) MyDatabaseId )
1831- continue ;
1832-
18331828 memset (nulls , 0 , AD_TOTAL_NCOLS );
18341829
18351830 values [AD_FS ] = Int64GetDatum (entry -> key .fs );
18361831 values [AD_FSS ] = Int32GetDatum ((int ) entry -> key .fss );
1832+ values [AD_DBID ] = ObjectIdGetDatum (entry -> key .dbid );
18371833 values [AD_NFEATURES ] = Int32GetDatum (entry -> cols );
18381834
18391835 /* Fill values from the DSA data chunk */
@@ -1994,10 +1990,8 @@ aqo_queries(PG_FUNCTION_ARGS)
19941990 hash_seq_init (& hash_seq , queries_htab );
19951991 while ((entry = hash_seq_search (& hash_seq )) != NULL )
19961992 {
1997- if (entry -> key .dbid != (uint64 ) MyDatabaseId )
1998- continue ;
1999-
20001993 values [AQ_QUERYID ] = Int64GetDatum (entry -> key .queryid );
1994+ values [AQ_DBID ] = ObjectIdGetDatum (entry -> key .dbid );
20011995 values [AQ_FS ] = Int64GetDatum (entry -> fs );
20021996 values [AQ_LEARN_AQO ] = BoolGetDatum (entry -> learn_aqo );
20031997 values [AQ_USE_AQO ] = BoolGetDatum (entry -> use_aqo );
@@ -2351,7 +2345,7 @@ cleanup_aqo_database(bool gentle, int *fs_num, int *fss_num)
23512345 List * actual_fss = NIL ;
23522346 ListCell * lc ;
23532347
2354- if (entry -> key .dbid != (uint64 ) MyDatabaseId )
2348+ if (entry -> key .dbid != (uint64 ) MyDatabaseId && entry -> key . queryid != 0 )
23552349 continue ;
23562350
23572351 /* Scan aqo_data for any junk records related to this FS */
@@ -2601,6 +2595,9 @@ aqo_cardinality_error(PG_FUNCTION_ARGS)
26012595 int64 nexecs ;
26022596 int nvals ;
26032597
2598+ if (qentry -> key .dbid != (uint64 ) MyDatabaseId && qentry -> key .queryid != 0 )
2599+ continue ;
2600+
26042601 sentry = (StatEntry * ) hash_search (stat_htab , & qentry -> key ,
26052602 HASH_FIND , & found );
26062603 if (!found )
@@ -2696,6 +2693,9 @@ aqo_execution_time(PG_FUNCTION_ARGS)
26962693 int nvals ;
26972694 double tm = 0 ;
26982695
2696+ if (qentry -> key .dbid != (uint64 ) MyDatabaseId && qentry -> key .queryid != 0 )
2697+ continue ;
2698+
26992699 sentry = (StatEntry * ) hash_search (stat_htab , & qentry -> key ,
27002700 HASH_FIND , & found );
27012701 if (!found )
0 commit comments