41
41
42
42
43
43
typedef 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 ,
45
45
EST_ERROR_AQO , EST_ERROR , NEXECS_AQO , NEXECS , TOTAL_NCOLS
46
46
} aqo_stat_cols ;
47
47
48
48
typedef enum {
49
- QT_QUERYID = 0 , QT_QUERY_STRING , QT_TOTAL_NCOLS
49
+ QT_QUERYID = 0 , QT_DBID , QT_QUERY_STRING , QT_TOTAL_NCOLS
50
50
} aqo_qtexts_cols ;
51
51
52
52
typedef 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 ,
54
54
AD_OIDS , AD_TOTAL_NCOLS
55
55
} aqo_data_cols ;
56
56
57
57
typedef 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 ,
59
59
AQ_TOTAL_NCOLS
60
60
} aqo_queries_cols ;
61
61
@@ -423,10 +423,8 @@ aqo_query_stat(PG_FUNCTION_ARGS)
423
423
hash_seq_init (& hash_seq , stat_htab );
424
424
while ((entry = hash_seq_search (& hash_seq )) != NULL )
425
425
{
426
- if (entry -> key .dbid != (uint64 ) MyDatabaseId )
427
- continue ;
428
-
429
426
values [QUERYID ] = Int64GetDatum (entry -> key .queryid );
427
+ values [DBID ] = ObjectIdGetDatum (entry -> key .dbid );
430
428
values [NEXECS ] = Int64GetDatum (entry -> execs_without_aqo );
431
429
values [NEXECS_AQO ] = Int64GetDatum (entry -> execs_with_aqo );
432
430
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)
1201
1199
{
1202
1200
char * ptr ;
1203
1201
1204
- if (entry -> key .dbid != (uint64 ) (uint64 ) MyDatabaseId )
1205
- continue ;
1206
-
1207
1202
Assert (DsaPointerIsValid (entry -> qtext_dp ));
1208
1203
ptr = dsa_get_address (qtext_dsa , entry -> qtext_dp );
1209
1204
values [QT_QUERYID ] = Int64GetDatum (entry -> key .queryid );
1205
+ values [QT_DBID ] = ObjectIdGetDatum (entry -> key .dbid );
1210
1206
values [QT_QUERY_STRING ] = CStringGetTextDatum (ptr );
1211
1207
tuplestore_putvalues (tupstore , tupDesc , values , nulls );
1212
1208
}
@@ -1220,13 +1216,14 @@ static bool
1220
1216
_aqo_stat_remove (uint64 queryid )
1221
1217
{
1222
1218
bool found ;
1223
- stat_key key = {.queryid = queryid , .dbid = (uint64 ) (uint64 ) MyDatabaseId };
1219
+ stat_key key = {.queryid = queryid , .dbid = (uint64 ) MyDatabaseId };
1220
+ StatEntry * entry ;
1224
1221
1225
1222
Assert (!LWLockHeldByMe (& aqo_state -> stat_lock ));
1226
1223
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 );
1228
1225
1229
- if (found )
1226
+ if (found && entry -> key . dbid == ( uint64 ) MyDatabaseId )
1230
1227
{
1231
1228
(void ) hash_search (stat_htab , & key , HASH_REMOVE , NULL );
1232
1229
aqo_state -> stat_changed = true;
@@ -1239,15 +1236,16 @@ _aqo_stat_remove(uint64 queryid)
1239
1236
static bool
1240
1237
_aqo_queries_remove (uint64 queryid )
1241
1238
{
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 ;
1244
1242
1245
1243
1246
1244
Assert (!LWLockHeldByMe (& aqo_state -> queries_lock ));
1247
1245
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 );
1249
1247
1250
- if (found )
1248
+ if (found && entry -> key . dbid == ( uint64 ) MyDatabaseId )
1251
1249
{
1252
1250
(void ) hash_search (queries_htab , & key , HASH_REMOVE , NULL );
1253
1251
aqo_state -> queries_changed = true;
@@ -1262,7 +1260,7 @@ _aqo_qtexts_remove(uint64 queryid)
1262
1260
{
1263
1261
bool found = false;
1264
1262
QueryTextEntry * entry ;
1265
- qtext_key key = {.queryid = queryid , .dbid = (uint64 ) ( uint64 ) MyDatabaseId };
1263
+ qtext_key key = {.queryid = queryid , .dbid = (uint64 ) MyDatabaseId };
1266
1264
1267
1265
dsa_init ();
1268
1266
@@ -1275,7 +1273,7 @@ _aqo_qtexts_remove(uint64 queryid)
1275
1273
*/
1276
1274
entry = (QueryTextEntry * ) hash_search (qtexts_htab , & key , HASH_FIND ,
1277
1275
& found );
1278
- if (found )
1276
+ if (found && entry -> key . dbid == ( uint64 ) MyDatabaseId )
1279
1277
{
1280
1278
/* Free DSA memory, allocated for this record */
1281
1279
Assert (DsaPointerIsValid (entry -> qtext_dp ));
@@ -1299,7 +1297,7 @@ _aqo_data_remove(data_key *key)
1299
1297
LWLockAcquire (& aqo_state -> data_lock , LW_EXCLUSIVE );
1300
1298
1301
1299
entry = (DataEntry * ) hash_search (data_htab , key , HASH_FIND , & found );
1302
- if (found )
1300
+ if (found && entry -> key . dbid == ( uint64 ) MyDatabaseId )
1303
1301
{
1304
1302
/* Free DSA memory, allocated for this record */
1305
1303
Assert (DsaPointerIsValid (entry -> data_dp ));
@@ -1376,7 +1374,7 @@ aqo_data_store(uint64 fs, int fss, AqoDataArgs *data, List *reloids)
1376
1374
{
1377
1375
DataEntry * entry ;
1378
1376
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 };
1380
1378
int i ;
1381
1379
char * ptr ;
1382
1380
ListCell * lc ;
@@ -1827,13 +1825,11 @@ aqo_data(PG_FUNCTION_ARGS)
1827
1825
{
1828
1826
char * ptr ;
1829
1827
1830
- if (entry -> key .dbid != (uint64 ) MyDatabaseId )
1831
- continue ;
1832
-
1833
1828
memset (nulls , 0 , AD_TOTAL_NCOLS );
1834
1829
1835
1830
values [AD_FS ] = Int64GetDatum (entry -> key .fs );
1836
1831
values [AD_FSS ] = Int32GetDatum ((int ) entry -> key .fss );
1832
+ values [AD_DBID ] = ObjectIdGetDatum (entry -> key .dbid );
1837
1833
values [AD_NFEATURES ] = Int32GetDatum (entry -> cols );
1838
1834
1839
1835
/* Fill values from the DSA data chunk */
@@ -1994,10 +1990,8 @@ aqo_queries(PG_FUNCTION_ARGS)
1994
1990
hash_seq_init (& hash_seq , queries_htab );
1995
1991
while ((entry = hash_seq_search (& hash_seq )) != NULL )
1996
1992
{
1997
- if (entry -> key .dbid != (uint64 ) MyDatabaseId )
1998
- continue ;
1999
-
2000
1993
values [AQ_QUERYID ] = Int64GetDatum (entry -> key .queryid );
1994
+ values [AQ_DBID ] = ObjectIdGetDatum (entry -> key .dbid );
2001
1995
values [AQ_FS ] = Int64GetDatum (entry -> fs );
2002
1996
values [AQ_LEARN_AQO ] = BoolGetDatum (entry -> learn_aqo );
2003
1997
values [AQ_USE_AQO ] = BoolGetDatum (entry -> use_aqo );
@@ -2351,7 +2345,7 @@ cleanup_aqo_database(bool gentle, int *fs_num, int *fss_num)
2351
2345
List * actual_fss = NIL ;
2352
2346
ListCell * lc ;
2353
2347
2354
- if (entry -> key .dbid != (uint64 ) MyDatabaseId )
2348
+ if (entry -> key .dbid != (uint64 ) MyDatabaseId && entry -> key . queryid != 0 )
2355
2349
continue ;
2356
2350
2357
2351
/* Scan aqo_data for any junk records related to this FS */
@@ -2601,6 +2595,9 @@ aqo_cardinality_error(PG_FUNCTION_ARGS)
2601
2595
int64 nexecs ;
2602
2596
int nvals ;
2603
2597
2598
+ if (qentry -> key .dbid != (uint64 ) MyDatabaseId && qentry -> key .queryid != 0 )
2599
+ continue ;
2600
+
2604
2601
sentry = (StatEntry * ) hash_search (stat_htab , & qentry -> key ,
2605
2602
HASH_FIND , & found );
2606
2603
if (!found )
@@ -2696,6 +2693,9 @@ aqo_execution_time(PG_FUNCTION_ARGS)
2696
2693
int nvals ;
2697
2694
double tm = 0 ;
2698
2695
2696
+ if (qentry -> key .dbid != (uint64 ) MyDatabaseId && qentry -> key .queryid != 0 )
2697
+ continue ;
2698
+
2699
2699
sentry = (StatEntry * ) hash_search (stat_htab , & qentry -> key ,
2700
2700
HASH_FIND , & found );
2701
2701
if (!found )
0 commit comments