@@ -97,7 +97,7 @@ static void data_load(const char *filename, deform_record_t callback, void *ctx)
97
97
static size_t _compute_data_dsa (const DataEntry * entry );
98
98
99
99
static bool _aqo_stat_remove (uint64 queryid );
100
- static bool _aqo_queries_remove (uint64 queryid );
100
+ static bool _aqo_queries_remove (uint64 queryid , bool lock );
101
101
static bool _aqo_qtexts_remove (uint64 queryid );
102
102
static bool _aqo_data_remove (data_key * key );
103
103
static bool nearest_neighbor (double * * matrix , int old_rows , double * neighbor , int cols );
@@ -1186,7 +1186,7 @@ aqo_qtext_store(uint64 queryid, const char *query_string, bool *dsa_valid)
1186
1186
* that caller recognize it and don't try to call us more.
1187
1187
*/
1188
1188
(void ) hash_search (qtexts_htab , & queryid , HASH_REMOVE , NULL );
1189
- _aqo_queries_remove (queryid );
1189
+ _aqo_queries_remove (queryid , true );
1190
1190
LWLockRelease (& aqo_state -> qtexts_lock );
1191
1191
if (dsa_valid )
1192
1192
* dsa_valid = false;
@@ -1284,12 +1284,19 @@ _aqo_stat_remove(uint64 queryid)
1284
1284
}
1285
1285
1286
1286
static bool
1287
- _aqo_queries_remove (uint64 queryid )
1287
+ _aqo_queries_remove (uint64 queryid , bool lock )
1288
1288
{
1289
1289
bool found ;
1290
1290
1291
- Assert (!LWLockHeldByMe (& aqo_state -> queries_lock ));
1292
- LWLockAcquire (& aqo_state -> queries_lock , LW_EXCLUSIVE );
1291
+ if (lock )
1292
+ {
1293
+ LWLockAcquire (& aqo_state -> queries_lock , LW_EXCLUSIVE );
1294
+ }
1295
+ else
1296
+ {
1297
+ Assert (LWLockHeldByMeInMode (& aqo_state -> queries_lock , LW_EXCLUSIVE ));
1298
+ }
1299
+
1293
1300
(void ) hash_search (queries_htab , & queryid , HASH_FIND , & found );
1294
1301
1295
1302
if (found )
@@ -1298,7 +1305,8 @@ _aqo_queries_remove(uint64 queryid)
1298
1305
aqo_state -> queries_changed = true;
1299
1306
}
1300
1307
1301
- LWLockRelease (& aqo_state -> queries_lock );
1308
+ if (lock )
1309
+ LWLockRelease (& aqo_state -> queries_lock );
1302
1310
return found ;
1303
1311
}
1304
1312
@@ -2324,10 +2332,7 @@ cleanup_aqo_database(bool gentle, int *fs_num, int *fss_num)
2324
2332
* fs_num = 0 ;
2325
2333
* fss_num = 0 ;
2326
2334
2327
- /*
2328
- * It's a long haul. So, make seq scan without any lock. It is possible
2329
- * because only this operation can delete data from hash table.
2330
- */
2335
+ LWLockAcquire (& aqo_state -> queries_lock , LW_EXCLUSIVE );
2331
2336
hash_seq_init (& hash_seq , queries_htab );
2332
2337
while ((entry = hash_seq_search (& hash_seq )) != NULL )
2333
2338
{
@@ -2338,6 +2343,7 @@ cleanup_aqo_database(bool gentle, int *fs_num, int *fss_num)
2338
2343
ListCell * lc ;
2339
2344
2340
2345
/* Scan aqo_data for any junk records related to this FS */
2346
+ LWLockAcquire (& aqo_state -> data_lock , LW_SHARED );
2341
2347
hash_seq_init (& hash_seq2 , data_htab );
2342
2348
while ((dentry = hash_seq_search (& hash_seq2 )) != NULL )
2343
2349
{
@@ -2347,8 +2353,6 @@ cleanup_aqo_database(bool gentle, int *fs_num, int *fss_num)
2347
2353
/* Another FS */
2348
2354
continue ;
2349
2355
2350
- LWLockAcquire (& aqo_state -> data_lock , LW_SHARED );
2351
-
2352
2356
Assert (DsaPointerIsValid (dentry -> data_dp ));
2353
2357
ptr = dsa_get_address (data_dsa , dentry -> data_dp );
2354
2358
@@ -2388,10 +2392,10 @@ cleanup_aqo_database(bool gentle, int *fs_num, int *fss_num)
2388
2392
UINT64_FORMAT " fss=%d" ,
2389
2393
dentry -> key .fs , (int32 ) dentry -> key .fss )));
2390
2394
}
2391
-
2392
- LWLockRelease (& aqo_state -> data_lock );
2393
2395
}
2394
2396
2397
+ LWLockRelease (& aqo_state -> data_lock );
2398
+
2395
2399
/*
2396
2400
* In forced mode remove all child FSSes even some of them are still
2397
2401
* link to existed tables.
@@ -2419,10 +2423,12 @@ cleanup_aqo_database(bool gentle, int *fs_num, int *fss_num)
2419
2423
_aqo_qtexts_remove (entry -> queryid );
2420
2424
2421
2425
/* Query class preferences */
2422
- (* fs_num ) += (int ) _aqo_queries_remove (entry -> queryid );
2426
+ (* fs_num ) += (int ) _aqo_queries_remove (entry -> queryid , false );
2423
2427
}
2424
2428
}
2425
2429
2430
+ LWLockRelease (& aqo_state -> queries_lock );
2431
+
2426
2432
/*
2427
2433
* The best place to flush updated AQO storage: calling the routine, user
2428
2434
* realizes how heavy it is.
@@ -2504,7 +2510,7 @@ aqo_drop_class(PG_FUNCTION_ARGS)
2504
2510
"id = " INT64_FORMAT ", fs = " UINT64_FORMAT "." , (int64 ) queryid , fs );
2505
2511
2506
2512
/* Now, remove all data related to the class */
2507
- _aqo_queries_remove (queryid );
2513
+ _aqo_queries_remove (queryid , true );
2508
2514
_aqo_stat_remove (queryid );
2509
2515
_aqo_qtexts_remove (queryid );
2510
2516
cnt = _aqo_data_clean (fs );
0 commit comments