@@ -983,8 +983,10 @@ aqo_query_texts(PG_FUNCTION_ARGS)
983
983
hash_seq_init (& hash_seq , qtexts_htab );
984
984
while ((entry = hash_seq_search (& hash_seq )) != NULL )
985
985
{
986
+ char * ptr ;
987
+
986
988
Assert (DsaPointerIsValid (entry -> qtext_dp ));
987
- char * ptr = dsa_get_address (qtext_dsa , entry -> qtext_dp );
989
+ ptr = dsa_get_address (qtext_dsa , entry -> qtext_dp );
988
990
values [QT_QUERYID ] = Int64GetDatum (entry -> queryid );
989
991
values [QT_QUERY_STRING ] = CStringGetTextDatum (ptr );
990
992
tuplestore_putvalues (tupstore , tupDesc , values , nulls );
@@ -1170,7 +1172,7 @@ build_knn_matrix(OkNNrdata *data, const OkNNrdata *temp_data)
1170
1172
{
1171
1173
Assert (data -> cols == temp_data -> cols );
1172
1174
1173
- if (data -> rows >= 0 )
1175
+ if (data -> rows > 0 )
1174
1176
/* trivial strategy - use first suitable record and ignore others */
1175
1177
return ;
1176
1178
@@ -1201,8 +1203,7 @@ _fill_knn_data(const DataEntry *entry, List **reloids)
1201
1203
/* Check invariants */
1202
1204
Assert (entry -> rows < aqo_K );
1203
1205
Assert (ptr != NULL );
1204
- Assert (entry -> key .fs == ((data_key * )ptr )-> fs &&
1205
- entry -> key .fss == ((data_key * )ptr )-> fss );
1206
+ Assert (entry -> key .fss == ((data_key * )ptr )-> fss );
1206
1207
1207
1208
ptr += sizeof (data_key );
1208
1209
@@ -1227,6 +1228,7 @@ _fill_knn_data(const DataEntry *entry, List **reloids)
1227
1228
Assert (offset <= sz );
1228
1229
1229
1230
if (reloids == NULL )
1231
+ /* Isn't needed to load reloids list */
1230
1232
return data ;
1231
1233
1232
1234
/* store list of relations. XXX: optimize ? */
@@ -1260,26 +1262,72 @@ load_aqo_data(uint64 fs, int fss, OkNNrdata *data, List **reloids,
1260
1262
dsa_init ();
1261
1263
1262
1264
LWLockAcquire (& aqo_state -> data_lock , LW_EXCLUSIVE );
1263
- entry = (DataEntry * ) hash_search (data_htab , & key , HASH_FIND , & found );
1264
1265
1265
- if (!found )
1266
- goto end ;
1266
+ if (!wideSearch )
1267
+ {
1268
+ entry = (DataEntry * ) hash_search (data_htab , & key , HASH_FIND , & found );
1267
1269
1268
- /* One entry with all correctly filled fields is found */
1269
- Assert (entry );
1270
- Assert (DsaPointerIsValid (entry -> data_dp ));
1270
+ if (!found )
1271
+ goto end ;
1272
+
1273
+ /* One entry with all correctly filled fields is found */
1274
+ Assert (entry );
1275
+ Assert (DsaPointerIsValid (entry -> data_dp ));
1276
+
1277
+ if (entry -> cols != data -> cols )
1278
+ {
1279
+ /* Collision happened? */
1280
+ elog (LOG , "[AQO] Does a collision happened? Check it if possible (fs: %lu, fss: %d)." ,
1281
+ fs , fss );
1282
+ found = false;
1283
+ goto end ;
1284
+ }
1271
1285
1272
- if (entry -> cols != data -> cols )
1286
+ temp_data = _fill_knn_data (entry , reloids );
1287
+ build_knn_matrix (data , temp_data );
1288
+ }
1289
+ else
1290
+ /* Iterate across all elements of the table. XXX: Maybe slow. */
1273
1291
{
1274
- /* Collision happened? */
1275
- elog ( LOG , "[AQO] Does a collision happened? Check it if possible (fs: %lu, fss: %d)." ,
1276
- fs , fss );
1292
+ HASH_SEQ_STATUS hash_seq ;
1293
+ int noids = -1 ;
1294
+
1277
1295
found = false;
1278
- goto end ;
1296
+ hash_seq_init (& hash_seq , data_htab );
1297
+ while ((entry = hash_seq_search (& hash_seq )) != NULL )
1298
+ {
1299
+ List * tmp_oids = NIL ;
1300
+
1301
+ if (entry -> key .fss != fss || entry -> cols != data -> cols )
1302
+ continue ;
1303
+
1304
+ temp_data = _fill_knn_data (entry , & tmp_oids );
1305
+
1306
+ if (data -> rows > 0 && list_length (tmp_oids ) != noids )
1307
+ {
1308
+ /* Dubious case. So log it and skip these data */
1309
+ elog (LOG ,
1310
+ "[AQO] different number depended oids for the same fss %d: "
1311
+ "%d and %d correspondingly." ,
1312
+ fss , list_length (tmp_oids ), noids );
1313
+ Assert (noids >= 0 );
1314
+ list_free (tmp_oids );
1315
+ continue ;
1316
+ }
1317
+
1318
+ noids = list_length (tmp_oids );
1319
+
1320
+ if (reloids != NULL && * reloids == NIL )
1321
+ * reloids = tmp_oids ;
1322
+ else
1323
+ list_free (tmp_oids );
1324
+
1325
+ build_knn_matrix (data , temp_data );
1326
+ found = true;
1327
+ }
1279
1328
}
1280
1329
1281
- temp_data = _fill_knn_data (entry , reloids );
1282
- build_knn_matrix (data , temp_data );
1330
+ Assert (!found || (data -> rows > 0 && data -> rows <= aqo_K ));
1283
1331
end :
1284
1332
LWLockRelease (& aqo_state -> data_lock );
1285
1333
@@ -1364,7 +1412,10 @@ aqo_data(PG_FUNCTION_ARGS)
1364
1412
1365
1413
elems = palloc (sizeof (* elems ) * entry -> nrels );
1366
1414
for (i = 0 ; i < entry -> nrels ; i ++ )
1415
+ {
1367
1416
elems [i ] = ObjectIdGetDatum (* (Oid * )ptr );
1417
+ ptr += sizeof (Oid );
1418
+ }
1368
1419
1369
1420
array = construct_array (elems , entry -> nrels , OIDOID ,
1370
1421
sizeof (Oid ), true, TYPALIGN_INT );
0 commit comments