@@ -1395,19 +1395,18 @@ aqo_data_store(uint64 fs, int fss, OkNNrdata *data, List *reloids)
13951395LWLockAcquire (& aqo_state -> neighbours_lock ,LW_EXCLUSIVE );
13961396
13971397prev = (NeighboursEntry * )hash_search (fss_neighbours ,& key .fss ,HASH_ENTER ,& found );
1398- if (! found )
1399- {
1400- entry -> list . prev = NULL ;
1401- entry -> list . next = NULL ;
1402- }
1403- else
1398+
1399+ /* A new element contains backward link to the first element list and
1400+ * the first element contains toward link to the new element.
1401+ * The new element has become the first element of the list.
1402+ */
1403+ if ( found )
14041404{
1405- prev -> data -> list .next = entry ;
1406- entry -> list .next = NULL ;
1407- entry -> list .prev = prev -> data ;
1405+ prev -> data -> neighbour_refs .next = entry ;
1406+ entry -> neighbour_refs .prev = prev -> data ;
14081407}
14091408prev -> data = entry ;
1410-
1409+ aqo_state -> neighbours_changed = true;
14111410LWLockRelease (& aqo_state -> neighbours_lock );
14121411}
14131412
@@ -1590,6 +1589,9 @@ load_aqo_data(uint64 fs, int fss, OkNNrdata *data, List **reloids,
15901589neighbour_entry = (NeighboursEntry * )hash_search (fss_neighbours ,& fss ,HASH_FIND ,& found );
15911590entry = found ?neighbour_entry -> data :NULL ;
15921591
1592+ /*
1593+ * Go through the list, starting from the first element and go to back.
1594+ */
15931595while (entry != NULL )
15941596{
15951597List * tmp_oids = NIL ;
@@ -1620,7 +1622,11 @@ load_aqo_data(uint64 fs, int fss, OkNNrdata *data, List **reloids,
16201622
16211623build_knn_matrix (data ,temp_data );
16221624
1623- entry = entry -> list .prev ;
1625+ /*
1626+ * We have a backward oriented list since the first element always stay
1627+ * the last element of list, so go to the next element on the backward link.
1628+ */
1629+ entry = entry -> neighbour_refs .prev ;
16241630}
16251631LWLockRelease (& aqo_state -> neighbours_lock );
16261632}
@@ -1758,15 +1764,15 @@ _aqo_data_clean(uint64 fs)
17581764entry -> data_dp = InvalidDsaPointer ;
17591765
17601766/* fix neighbour list */
1761- if (entry -> list .next )
1767+ if (entry -> neighbour_refs .next )
17621768has_next = true;
1763- if (entry -> list .prev )
1769+ if (entry -> neighbour_refs .prev )
17641770has_prev = true;
17651771
17661772if (has_prev )
1767- entry -> list .prev -> list .next = has_next ?entry -> list .next :NULL ;
1773+ entry -> neighbour_refs .prev -> neighbour_refs .next = has_next ?entry -> neighbour_refs .next :NULL ;
17681774if (has_next )
1769- entry -> list .next -> list .prev = has_prev ?entry -> list .prev :NULL ;
1775+ entry -> neighbour_refs .next -> neighbour_refs .prev = has_prev ?entry -> neighbour_refs .prev :NULL ;
17701776
17711777/* Fix or remove neighbours htab entry*/
17721778LWLockAcquire (& aqo_state -> neighbours_lock ,LW_EXCLUSIVE );
@@ -1775,15 +1781,20 @@ _aqo_data_clean(uint64 fs)
17751781{
17761782if (has_prev )
17771783{
1778- fss_htab_entry -> data = entry -> list .prev ;
1784+ fss_htab_entry -> data = entry -> neighbour_refs .prev ;
17791785}
17801786else
17811787{
17821788hash_search (fss_neighbours ,& entry -> key .fss ,HASH_REMOVE ,NULL );
17831789}
1790+ /*
1791+ * We found element in Neibours hash table and made change:
1792+ * either delete element of table or replace its value.
1793+ */
1794+ aqo_state -> neighbours_changed = true;
17841795}
17851796LWLockRelease (& aqo_state -> neighbours_lock );
1786-
1797+
17871798
17881799if (hash_search (data_htab ,& entry -> key ,HASH_REMOVE ,NULL )== NULL )
17891800elog (ERROR ,"[AQO] hash table corrupted" );
@@ -2324,15 +2335,15 @@ cleanup_aqo_database(bool gentle, int *fs_num, int *fss_num)
23242335entry = (DataEntry * )hash_search (data_htab ,& key ,HASH_FIND ,& found );
23252336if (found )
23262337{
2327- if (entry -> list .next )
2338+ if (entry -> neighbour_refs .next )
23282339has_next = true;
2329- if (entry -> list .prev )
2340+ if (entry -> neighbour_refs .prev )
23302341has_prev = true;
23312342
23322343if (has_prev )
2333- entry -> list .prev -> list .next = has_next ?entry -> list .next :NULL ;
2344+ entry -> neighbour_refs .prev -> neighbour_refs .next = has_next ?entry -> neighbour_refs .next :NULL ;
23342345if (has_next )
2335- entry -> list .next -> list .prev = has_prev ?entry -> list .prev :NULL ;
2346+ entry -> neighbour_refs .next -> neighbour_refs .prev = has_prev ?entry -> neighbour_refs .prev :NULL ;
23362347
23372348/* Fix or remove neighbours htab entry*/
23382349LWLockAcquire (& aqo_state -> neighbours_lock ,LW_EXCLUSIVE );
@@ -2341,12 +2352,17 @@ cleanup_aqo_database(bool gentle, int *fs_num, int *fss_num)
23412352{
23422353if (has_prev )
23432354{
2344- fss_htab_entry -> data = entry -> list .prev ;
2355+ fss_htab_entry -> data = entry -> neighbour_refs .prev ;
23452356}
23462357else
23472358{
23482359hash_search (fss_neighbours ,& key .fss ,HASH_REMOVE ,NULL );
23492360}
2361+ /*
2362+ * We found element in Neibours hash table and made change:
2363+ * either delete element of table or replace its value.
2364+ */
2365+ aqo_state -> neighbours_changed = true;
23502366}
23512367LWLockRelease (& aqo_state -> neighbours_lock );
23522368}