1313 */
1414
1515#include "postgres.h"
16+ #include "access/parallel.h" /* Just for IsParallelWorker() */
1617#include "miscadmin.h"
1718
1819#include "aqo.h"
@@ -227,7 +228,7 @@ init_with_dsm(OkNNrdata *data, dsm_block_hdr *hdr, List **reloids)
227228Assert (LWLockHeldByMeInMode (& aqo_state -> lock ,LW_EXCLUSIVE )||
228229LWLockHeldByMeInMode (& aqo_state -> lock ,LW_SHARED ));
229230Assert (hdr -> magic == AQO_SHARED_MAGIC );
230- Assert (hdr && ptr );
231+ Assert (hdr && ptr && hdr -> rows > 0 );
231232
232233data -> rows = hdr -> rows ;
233234data -> cols = hdr -> cols ;
@@ -245,6 +246,12 @@ init_with_dsm(OkNNrdata *data, dsm_block_hdr *hdr, List **reloids)
245246}
246247}
247248
249+ /*
250+ * Kludge code. But we should rewrite this code because now all knowledge
251+ * base lives in non-transactional shared memory.
252+ */
253+ ptr = (char * )hdr + sizeof (dsm_block_hdr )+ (sizeof (double )* data -> cols * aqo_K );
254+
248255memcpy (data -> targets ,ptr ,sizeof (double )* hdr -> rows );
249256ptr += sizeof (double )* aqo_K ;
250257memcpy (data -> rfactors ,ptr ,sizeof (double )* hdr -> rows );
@@ -261,7 +268,7 @@ init_with_dsm(OkNNrdata *data, dsm_block_hdr *hdr, List **reloids)
261268return calculate_size (hdr -> cols ,* reloids );
262269}
263270
264- /* It is just read operation. No any interest in size calculation. */
271+ /* It is justa read operation. No any interest in size calculation. */
265272return 0 ;
266273}
267274
@@ -310,20 +317,25 @@ lc_assign_hook(bool newval, void *extra)
310317HASH_SEQ_STATUS status ;
311318htab_entry * entry ;
312319
313- if (!fss_htab || !IsUnderPostmaster )
320+ if (!fss_htab || !IsUnderPostmaster || IsParallelWorker ())
321+ /* Clean this shared cache only in main backend process. */
314322return ;
315323
316324/* Remove all entries, reset memory context. */
317325
318326elog (DEBUG5 ,"[AQO] Cleanup local cache of ML data." );
319327
320- /* Remove allfrozen plans from a plancache . */
328+ /* Remove allentries in the shared hash table . */
321329LWLockAcquire (& aqo_state -> lock ,LW_EXCLUSIVE );
322330hash_seq_init (& status ,fss_htab );
323331while ((entry = (htab_entry * )hash_seq_search (& status ))!= NULL )
324332{
325333if (!hash_search (fss_htab , (void * )& entry -> key ,HASH_REMOVE ,NULL ))
326334elog (PANIC ,"[AQO] The local ML cache is corrupted." );
327335}
336+
337+ /* Now, clean additional DSM block */
338+ reset_dsm_cache ();
339+
328340LWLockRelease (& aqo_state -> lock );
329341}