Skip to content

Commit cdbf9bd

Browse files
maksm90akorotkov
authored andcommitted
Minor fix
1 parent 2daa422 commit cdbf9bd

File tree

5 files changed

+49
-18
lines changed

5 files changed

+49
-18
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# contrib/pg_wait_sampling/Makefile
22

33
MODULE_big = pg_wait_sampling
4-
OBJS = pg_wait_sampling.o collector.o
4+
OBJS = pg_wait_sampling.o collector.o compat.o
55

66
EXTENSION = pg_wait_sampling
77
EXTVERSION = 1.1

collector.c

+28-7
Original file line numberDiff line numberDiff line change
@@ -219,15 +219,25 @@ send_history(History *observations, shm_mq_handle *mqh)
219219

220220
mq_result = shm_mq_send(mqh, sizeof(count), &count, false);
221221
if (mq_result == SHM_MQ_DETACHED)
222+
{
223+
ereport(WARNING,
224+
(errmsg("pg_wait_sampling collector: "
225+
"receiver of message queue have been detached")));
222226
return;
227+
}
223228
for (i = 0; i < count; i++)
224229
{
225230
mq_result = shm_mq_send(mqh,
226231
sizeof(HistoryItem),
227232
&observations->items[i],
228233
false);
229234
if (mq_result == SHM_MQ_DETACHED)
235+
{
236+
ereport(WARNING,
237+
(errmsg("pg_wait_sampling collector: "
238+
"receiver of message queue have been detached")));
230239
return;
240+
}
231241
}
232242
}
233243

@@ -240,12 +250,27 @@ send_profile(HTAB *profile_hash, shm_mq_handle *mqh)
240250
HASH_SEQ_STATUS scan_status;
241251
ProfileItem *item;
242252
Size count = hash_get_num_entries(profile_hash);
253+
shm_mq_result mq_result;
243254

244-
shm_mq_send(mqh, sizeof(count), &count, false);
255+
mq_result = shm_mq_send(mqh, sizeof(count), &count, false);
256+
if (mq_result == SHM_MQ_DETACHED)
257+
{
258+
ereport(WARNING,
259+
(errmsg("pg_wait_sampling collector: "
260+
"receiver of message queue have been detached")));
261+
return;
262+
}
245263
hash_seq_init(&scan_status, profile_hash);
246264
while ((item = (ProfileItem *) hash_seq_search(&scan_status)) != NULL)
247265
{
248-
shm_mq_send(mqh, sizeof(ProfileItem), item, false);
266+
mq_result = shm_mq_send(mqh, sizeof(ProfileItem), item, false);
267+
if (mq_result == SHM_MQ_DETACHED)
268+
{
269+
ereport(WARNING,
270+
(errmsg("pg_wait_sampling collector: "
271+
"receiver of message queue have been detached")));
272+
return;
273+
}
249274
}
250275
}
251276

@@ -428,11 +453,7 @@ collector_main(Datum main_arg)
428453
send_profile(profile_hash, mqh);
429454
}
430455
}
431-
#if PG_VERSION_NUM >= 100000
432-
shm_mq_detach(mqh);
433-
#else
434-
shm_mq_detach(collector_mq);
435-
#endif
456+
shm_mq_detach_compat(mqh, collector_mq);
436457
}
437458
else if (request == PROFILE_RESET)
438459
{

compat.c

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include "postgres.h"
2+
3+
#include "pg_wait_sampling.h"
4+
5+
inline void
6+
shm_mq_detach_compat(shm_mq_handle *mqh, shm_mq *mq)
7+
{
8+
#if PG_VERSION_NUM >= 100000
9+
shm_mq_detach(mqh);
10+
#else
11+
shm_mq_detach(mq);
12+
#endif
13+
}

pg_wait_sampling.c

+5-10
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,10 @@ receive_array(SHMRequest request, Size item_size, Size *count)
530530

531531
res = shm_mq_receive(mqh, &len, &data, false);
532532
if (res != SHM_MQ_SUCCESS || len != sizeof(*count))
533+
{
534+
shm_mq_detach_compat(mqh, mq);
533535
elog(ERROR, "Error reading mq.");
536+
}
534537
memcpy(count, data, sizeof(*count));
535538

536539
result = palloc(item_size * (*count));
@@ -541,22 +544,14 @@ receive_array(SHMRequest request, Size item_size, Size *count)
541544
res = shm_mq_receive(mqh, &len, &data, false);
542545
if (res != SHM_MQ_SUCCESS || len != item_size)
543546
{
544-
#if PG_VERSION_NUM >= 100000
545-
shm_mq_detach(mqh);
546-
#else
547-
shm_mq_detach(mq);
548-
#endif
547+
shm_mq_detach_compat(mqh, mq);
549548
elog(ERROR, "Error reading mq.");
550549
}
551550
memcpy(ptr, data, item_size);
552551
ptr += item_size;
553552
}
554553

555-
#if PG_VERSION_NUM >= 100000
556-
shm_mq_detach(mqh);
557-
#else
558-
shm_mq_detach(mq);
559-
#endif
554+
shm_mq_detach_compat(mqh, mq);
560555

561556
LockRelease(&queueTag, ExclusiveLock, false);
562557

pg_wait_sampling.h

+2
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,6 @@ extern void register_wait_collector(void);
8383
extern void alloc_history(History *, int);
8484
extern void collector_main(Datum main_arg);
8585

86+
extern void shm_mq_detach_compat(shm_mq_handle *mqh, shm_mq *mq);
87+
8688
#endif

0 commit comments

Comments
 (0)