8000 Add flags argument to dsm_create. · sarvex/postgres@12968cf · GitHub
[go: up one dir, main page]

Skip to content
10000

Commit 12968cf

Browse files
committed
Add flags argument to dsm_create.
Right now, there's only one flag, DSM_CREATE_NULL_IF_MAXSEGMENTS, which suppresses the error that would normally be thrown when the maximum number of segments already exists, instead returning NULL. It might be useful to add more flags in the future, such as one to ignore allocation errors, but I haven't done that here.
1 parent 5f286c0 commit 12968cf

File tree

3 files changed

+33
-4
lines changed

3 files changed

+33
-4
lines changed

src/backend/storage/ipc/dsm.c

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -454,9 +454,9 @@ dsm_set_control_handle(dsm_handle h)
454454
* Create a new dynamic shared memory segment.
455455
*/
456456
dsm_segment *
457-
dsm_create(Size size)
457+
dsm_create(Size size, int flags)
458458
{
459-
dsm_segment *seg = dsm_create_descriptor();
459+
dsm_segment *seg;
460460
uint32 i;
461461
uint32 nitems;
462462

@@ -466,6 +466,21 @@ dsm_create(Size size)
466466
if (!dsm_init_done)
467467
dsm_backend_startup();
468468

469+
/*
470+
* If we've been instructed to return NULL when it's not possible to
471+
* register another segment, check whether we seem to be at the limit.
472+
* This allows us to avoid the overhead of creating a new segment only to
473+
* immediately destroy it again. Since we don't take the lock here, the
474+
* value we read might be slightly stale, but the remote possibility of
475+
* an unnecessary failure here shouldn't trouble anyone too much.
476+
*/
477+
if ((flags & DSM_CREATE_NULL_IF_MAXSEGMENTS) != 0
478+
&& dsm_control->nitems >= dsm_control->maxitems)
479+
return NULL;
480+
481+
/* Create a new segment descriptor. */
482+
seg = dsm_create_descriptor();
483+
469484
/* Loop until we find an unused segment identifier. */
470485
for (;;)
471486
{
@@ -496,9 +511,21 @@ dsm_create(Size size)
496511

497512
/* Verify that we can support an additional mapping. */
498513
if (nitems >= dsm_control->maxitems)
514+
{
515+
if ((flags & DSM_CREATE_NULL_IF_MAXSEGMENTS) != 0)
516+
{
517+
dsm_impl_op(DSM_OP_DESTROY, seg->handle, 0, &seg->impl_private,
518+
&seg->mapped_address, &seg->mapped_size, WARNING);
519+
if (seg->resowner != NULL)
520+
ResourceOwnerForgetDSM(seg->resowner, seg);
521+
dlist_delete(&seg->node);
522+
pfree(seg);
523+
return NULL;
524+
}
499525
ereport(ERROR,
500526
(errcode(ERRCODE_INSUFFICIENT_RESOURCES),
501527
errmsg("too many dynamic shared memory segments")));
528+
}
502529

503530
/* Enter the handle into a new array slot. */
504531
dsm_control->item[nitems].handle = seg->handle;

src/include/storage/dsm.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
typedef struct dsm_segment dsm_segment;
1919

20+
#define DSM_CREATE_NULL_IF_MAXSEGMENTS 0x0001
21+
2022
/* Startup and shutdown functions. */
2123
struct PGShmemHeader; /* avoid including pg_shmem.h */
2224
extern void dsm_cleanup_using_control_segment(dsm_handle old_control_handle);
@@ -29,7 +31,7 @@ extern void dsm_set_control_handle(dsm_handle h);
2931
#endif
3032

3133
/* Functions that create, update, or remove mappings. */
32-
extern dsm_segment *dsm_create(Size size);
34+
extern dsm_segment *dsm_create(Size size, int flags);
3335
extern dsm_segment *dsm_attach(dsm_handle h);
3436
extern void *dsm_resize(dsm_segment *seg, Size size);
3537
extern void *dsm_remap(dsm_segment *seg);

src/test/modules/test_shm_mq/setup.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ setup_dynamic_shared_memory(int64 queue_size, int nworkers,
125125
segsize = shm_toc_estimate(&e);
126126

127127
/* Create the shared memory segment and establish a table of contents. */
128-
seg = dsm_create(shm_toc_estimate(&e));
128+
seg = dsm_create(shm_toc_estimate(&e), 0);
129129
toc = shm_toc_create(PG_TEST_SHM_MQ_MAGIC, dsm_segment_address(seg),
130130
segsize);
131131

0 commit comments

Comments
 (0)
0