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)
227228 Assert (LWLockHeldByMeInMode (& aqo_state -> lock , LW_EXCLUSIVE ) ||
228229 LWLockHeldByMeInMode (& aqo_state -> lock , LW_SHARED ));
229230 Assert (hdr -> magic == AQO_SHARED_MAGIC );
230- Assert (hdr && ptr );
231+ Assert (hdr && ptr && hdr -> rows > 0 );
231232
232233 data -> rows = hdr -> rows ;
233234 data -> 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+
248255 memcpy (data -> targets , ptr , sizeof (double ) * hdr -> rows );
249256 ptr += sizeof (double ) * aqo_K ;
250257 memcpy (data -> rfactors , ptr , sizeof (double ) * hdr -> rows );
@@ -261,7 +268,7 @@ init_with_dsm(OkNNrdata *data, dsm_block_hdr *hdr, List **reloids)
261268 return calculate_size (hdr -> cols , * reloids );
262269 }
263270
264- /* It is just read operation. No any interest in size calculation. */
271+ /* It is just a read operation. No any interest in size calculation. */
265272 return 0 ;
266273}
267274
@@ -310,20 +317,25 @@ lc_assign_hook(bool newval, void *extra)
310317 HASH_SEQ_STATUS status ;
311318 htab_entry * entry ;
312319
313- if (!fss_htab || !IsUnderPostmaster )
320+ if (!fss_htab || !IsUnderPostmaster || IsParallelWorker ())
321+ /* Clean this shared cache only in main backend process. */
314322 return ;
315323
316324 /* Remove all entries, reset memory context. */
317325
318326 elog (DEBUG5 , "[AQO] Cleanup local cache of ML data." );
319327
320- /* Remove all frozen plans from a plancache . */
328+ /* Remove all entries in the shared hash table . */
321329 LWLockAcquire (& aqo_state -> lock , LW_EXCLUSIVE );
322330 hash_seq_init (& status , fss_htab );
323331 while ((entry = (htab_entry * ) hash_seq_search (& status )) != NULL )
324332 {
325333 if (!hash_search (fss_htab , (void * ) & entry -> key , HASH_REMOVE , NULL ))
326334 elog (PANIC , "[AQO] The local ML cache is corrupted." );
327335 }
336+
337+ /* Now, clean additional DSM block */
338+ reset_dsm_cache ();
339+
328340 LWLockRelease (& aqo_state -> lock );
329341}
0 commit comments