@@ -414,11 +414,11 @@ typedef struct
414414 ProfileItem * items ;
415415} Profile ;
416416
417- static void
418- init_lock_tag (LOCKTAG * tag )
417+ void
418+ init_lock_tag (LOCKTAG * tag , uint32 lock )
419419{
420420 tag -> locktag_field1 = PG_WAIT_SAMPLING_MAGIC ;
421- tag -> locktag_field2 = 0 ;
421+ tag -> locktag_field2 = lock ;
422422 tag -> locktag_field3 = 0 ;
423423 tag -> locktag_field4 = 0 ;
424424 tag -> locktag_type = LOCKTAG_USERLOCK ;
@@ -428,7 +428,8 @@ init_lock_tag(LOCKTAG *tag)
428428static void *
429429receive_array (SHMRequest request , Size item_size , Size * count )
430430{
431- LOCKTAG tag ;
431+ LOCKTAG queueTag ;
432+ LOCKTAG collectorTag ;
432433 shm_mq * mq ;
433434 shm_mq_handle * mqh ;
434435 shm_mq_result res ;
@@ -438,8 +439,14 @@ receive_array(SHMRequest request, Size item_size, Size *count)
438439 Pointer result ,
439440 ptr ;
440441
441- init_lock_tag (& tag );
442- LockAcquire (& tag , ExclusiveLock , false, false);
442+ /* Ensure nobody else trying to send request to queue */
443+ init_lock_tag (& queueTag , PGWS_QUEUE_LOCK );
444+ LockAcquire (& queueTag , ExclusiveLock , false, false);
445+
446+ /* Ensure collector has processed previous request */
447+ init_lock_tag (& collectorTag , PGWS_COLLECTOR_LOCK );
448+ LockAcquire (& collectorTag , ExclusiveLock , false, false);
449+ LockRelease (& collectorTag , ExclusiveLock , false);
443450
444451 mq = shm_mq_create (collector_mq , COLLECTOR_QUEUE_SIZE );
445452 collector_hdr -> request = request ;
@@ -472,7 +479,7 @@ receive_array(SHMRequest request, Size item_size, Size *count)
472479
473480 shm_mq_detach (mq );
474481
475- LockRelease (& tag , ExclusiveLock , false);
482+ LockRelease (& queueTag , ExclusiveLock , false);
476483
477484 return result ;
478485}
@@ -568,13 +575,18 @@ Datum
568575pg_wait_sampling_reset_profile (PG_FUNCTION_ARGS )
569576{
570577 LOCKTAG tag ;
578+ LOCKTAG tagCollector ;
571579
572580 check_shmem ();
573581
574- init_lock_tag (& tag );
582+ init_lock_tag (& tag , false );
575583
576584 LockAcquire (& tag , ExclusiveLock , false, false);
577585
586+ init_lock_tag (& tagCollector , true);
587+ LockAcquire (& tagCollector , ExclusiveLock , false, false);
588+ LockRelease (& tagCollector , ExclusiveLock , false);
589+
578590 collector_hdr -> request = PROFILE_RESET ;
579591 SetLatch (collector_hdr -> latch );
580592
0 commit comments