Skip to content

Commit 3e2c22c

Browse files
ildusAlexander Korotkov
authored andcommitted
Fix possible segfault in pg_stat_wait_get_history
1 parent 62b5052 commit 3e2c22c

File tree

4 files changed

+17
-9
lines changed

4 files changed

+17
-9
lines changed

contrib/pg_stat_wait/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ include $(top_builddir)/src/Makefile.global
1919
include $(top_srcdir)/contrib/contrib-global.mk
2020
endif
2121

22-
check: regresscheck
22+
check: isolationcheck
2323

2424
submake-regress:
2525
$(MAKE) -C $(top_builddir)/src/test/regress all

contrib/pg_stat_wait/collector.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
#include "pg_stat_wait.h"
2222

23-
CollectorShmqHeader *hdr;
23+
CollectorShmqHeader *hdr = NULL;
2424

2525
static void *pgsw;
2626
shm_toc *toc;
@@ -52,13 +52,18 @@ CollectorShmemSize(void)
5252
return size;
5353
}
5454

55-
void
56-
AllocateCollectorMem(void)
55+
CollectorShmqHeader *
56+
GetCollectorMem(bool init)
5757
{
5858
bool found;
5959
Size segsize = CollectorShmemSize();
6060

6161
pgsw = ShmemInitStruct("pg_stat_wait", segsize, &found);
62+
if (!init && !found)
63+
{
64+
ereport(ERROR, (errcode(ERRCODE_INTERNAL_ERROR),
65+
errmsg("A collector memory wasn't initialized yet")));
66+
}
6267

6368
if (!found)
6469
{
@@ -76,6 +81,7 @@ AllocateCollectorMem(void)
7681
toc = shm_toc_attach(PG_STAT_WAIT_MAGIC, pgsw);
7782
hdr = shm_toc_lookup(toc, 0);
7883
}
84+
return hdr;
7985
}
8086

8187
void

contrib/pg_stat_wait/pg_stat_wait.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ PG_MODULE_MAGIC;
1818
void _PG_init(void);
1919
void _PG_fini(void);
2020

21-
extern CollectorShmqHeader *hdr;
2221
extern shm_toc *toc;
2322

2423
static shmem_startup_hook_type prev_shmem_startup_hook = NULL;
@@ -35,7 +34,7 @@ pgsw_shmem_startup(void)
3534
if (prev_shmem_startup_hook)
3635
prev_shmem_startup_hook();
3736

38-
AllocateCollectorMem();
37+
GetCollectorMem(true);
3938
}
4039
}
4140

@@ -582,13 +581,16 @@ PG_FUNCTION_INFO_V1(pg_stat_wait_get_history);
582581
Datum
583582
pg_stat_wait_get_history(PG_FUNCTION_ARGS)
584583
{
585-
History *observations;
586-
FuncCallContext *funcctx;
584+
History *observations;
585+
FuncCallContext *funcctx;
586+
CollectorShmqHeader *hdr;
587587

588588
if (!WaitsHistoryOn)
589589
ereport(ERROR, (errcode(ERRCODE_CONFIG_FILE_ERROR),
590590
errmsg("Waits history turned off")));
591591

592+
hdr = GetCollectorMem(false);
593+
592594
if (SRF_IS_FIRSTCALL())
593595
{
594596
shm_mq *mq;

contrib/pg_stat_wait/pg_stat_wait.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ extern PGDLLIMPORT bool historySkipLatch;
7575

7676

7777
Size CollectorShmemSize(void);
78-
void AllocateCollectorMem(void);
78+
CollectorShmqHeader *GetCollectorMem(bool init);
7979
void RegisterWaitsCollector(void);
8080
void AllocHistory(History *, int);
8181
int GetCurrentWaitsState(PGPROC *, HistoryItem *, int);

0 commit comments

Comments
 (0)