Debian Codegraph @

src/backend/storage/ipc/shmem.c

raw
1 /*-------------------------------------------------------------------------
2 *
3 * shmem.c
4 * create shared memory and initialize shared memory data structures.
5 *
6 * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
8 *
9 *
10 * IDENTIFICATION
11 * src/backend/storage/ipc/shmem.c
12 *
13 *-------------------------------------------------------------------------
14 */
15 /*
16 * POSTGRES processes share one or more regions of shared memory.
17 * The shared memory is created by a postmaster and is inherited
18 * by each backend via fork() (or, in some ports, via other OS-specific
19 * methods). The routines in this file are used for allocating and
20 * binding to shared memory data structures.
21 *
22 * NOTES:
23 * (a) There are three kinds of shared memory data structures
24 * available to POSTGRES: fixed-size structures, queues and hash
25 * tables. Fixed-size structures contain things like global variables
26 * for a module and should never be allocated after the shared memory
27 * initialization phase. Hash tables have a fixed maximum size, but
28 * their actual size can vary dynamically. When entries are added
29 * to the table, more space is allocated. Queues link data structures
30 * that have been allocated either within fixed-size structures or as hash
31 * buckets. Each shared data structure has a string name to identify
32 * it (assigned in the module that declares it).
33 *
34 * (b) During initialization, each module looks for its
35 * shared data structures in a hash table called the "Shmem Index".
36 * If the data structure is not present, the caller can allocate
37 * a new one and initialize it. If the data structure is present,
38 * the caller "attaches" to the structure by initializing a pointer
39 * in the local address space.
40 * The shmem index has two purposes: first, it gives us
41 * a simple model of how the world looks when a backend process
42 * initializes. If something is present in the shmem index,
43 * it is initialized. If it is not, it is uninitialized. Second,
44 * the shmem index allows us to allocate shared memory on demand
45 * instead of trying to preallocate structures and hard-wire the
46 * sizes and locations in header files. If you are using a lot
47 * of shared memory in a lot of different places (and changing
48 * things during development), this is important.
49 *
50 * (c) In standard Unix-ish environments, individual backends do not
51 * need to re-establish their local pointers into shared memory, because
52 * they inherit correct values of those variables via fork() from the
53 * postmaster. However, this does not work in the EXEC_BACKEND case.
54 * In ports using EXEC_BACKEND, new backends have to set up their local
55 * pointers using the method described in (b) above.
56 *
57 * (d) memory allocation model: shared memory can never be
58 * freed, once allocated. Each hash table has its own free list,
59 * so hash buckets can be reused when an item is deleted. However,
60 * if one hash table grows very large and then shrinks, its space
61 * cannot be redistributed to other tables. We could build a simple
62 * hash bucket garbage collector if need be. Right now, it seems
63 * unnecessary.
64 */
65
66 #include "postgres.h"
67
68 #include "fmgr.h"
69 #include "funcapi.h"
70 #include "miscadmin.h"
71 #include "port/pg_numa.h"
72 #include "storage/lwlock.h"
73 #include "storage/pg_shmem.h"
74 #include "storage/shmem.h"
75 #include "storage/spin.h"
76 #include "utils/builtins.h"
77
78 static void *ShmemAllocRaw(Size size, Size *allocated_size);
79
80 /* shared memory global variables */
81
82 static PGShmemHeader *ShmemSegHdr; /* shared mem segment header */
83
84 static void *ShmemBase; /* start address of shared memory */
85
86 static void *ShmemEnd; /* end+1 address of shared memory */
87
88 slock_t *ShmemLock; /* spinlock for shared memory and LWLock
89 * allocation */
90
91 static HTAB *ShmemIndex = NULL; /* primary index hashtable for shmem */
92
93 /* To get reliable results for NUMA inquiry we need to "touch pages" once */
94 static bool firstNumaTouch = true;
95
96 Datum pg_numa_available(PG_FUNCTION_ARGS);
97
98 /*
99 * InitShmemAccess() --- set up basic pointers to shared memory.
100 */
101 void
102 InitShmemAccess(PGShmemHeader *seghdr)
103 {
104 ShmemSegHdr = seghdr;
105 ShmemBase = seghdr;
106 ShmemEnd = (char *) ShmemBase + seghdr->totalsize;
107 }
108
109 /*
110 * InitShmemAllocation() --- set up shared-memory space allocation.
111 *
112 * This should be called only in the postmaster or a standalone backend.
113 */
114 void
115 InitShmemAllocation(void)
116 {
117 PGShmemHeader *shmhdr = ShmemSegHdr;
118 char *aligned;
119
120 Assert(shmhdr != NULL);
121
122 /*
123 * Initialize the spinlock used by ShmemAlloc. We must use
124 * ShmemAllocUnlocked, since obviously ShmemAlloc can't be called yet.
125 */
126 ShmemLock = (slock_t *) ShmemAllocUnlocked(sizeof(slock_t));
127
128 SpinLockInit(ShmemLock);
129
130 /*
131 * Allocations after this point should go through ShmemAlloc, which
132 * expects to allocate everything on cache line boundaries. Make sure the
133 * first allocation begins on a cache line boundary.
134 */
135 aligned = (char *)
136 (CACHELINEALIGN((((char *) shmhdr) + shmhdr->freeoffset)));
137 shmhdr->freeoffset = aligned - (char *) shmhdr;
138
139 /* ShmemIndex can't be set up yet (need LWLocks first) */
140 shmhdr->index = NULL;
141 ShmemIndex = (HTAB *) NULL;
142 }
143
144 /*
145 * ShmemAlloc -- allocate max-aligned chunk from shared memory
146 *
147 * Throws error if request cannot be satisfied.
148 *
149 * Assumes ShmemLock and ShmemSegHdr are initialized.
150 */
151 void *
152 ShmemAlloc(Size size)
153 {
154 void *newSpace;
155 Size allocated_size;
156
157 newSpace = ShmemAllocRaw(size, &allocated_size);
158 if (!newSpace)
159 ereport(ERROR,
160 (errcode(ERRCODE_OUT_OF_MEMORY),
161 errmsg("out of shared memory (%zu bytes requested)",
162 size)));
163 return newSpace;
164 }
165
166 /*
167 * ShmemAllocNoError -- allocate max-aligned chunk from shared memory
168 *
169 * As ShmemAlloc, but returns NULL if out of space, rather than erroring.
170 */
171 void *
172 ShmemAllocNoError(Size size)
173 {
174 Size allocated_size;
175
176 return ShmemAllocRaw(size, &allocated_size);
177 }
178
179 /*
180 * ShmemAllocRaw -- allocate align chunk and return allocated size
181 *
182 * Also sets *allocated_size to the number of bytes allocated, which will
183 * be equal to the number requested plus any padding we choose to add.
184 */
185 static void *
186 ShmemAllocRaw(Size size, Size *allocated_size)
187 {
188 Size newStart;
189 Size newFree;
190 void *newSpace;
191
192 /*
193 * Ensure all space is adequately aligned. We used to only MAXALIGN this
194 * space but experience has proved that on modern systems that is not good
195 * enough. Many parts of the system are very sensitive to critical data
196 * structures getting split across cache line boundaries. To avoid that,
197 * attempt to align the beginning of the allocation to a cache line
198 * boundary. The calling code will still need to be careful about how it
199 * uses the allocated space - e.g. by padding each element in an array of
200 * structures out to a power-of-two size - but without this, even that
201 * won't be sufficient.
202 */
203 size = CACHELINEALIGN(size);
204 *allocated_size = size;
205
206 Assert(ShmemSegHdr != NULL);
207
208 SpinLockAcquire(ShmemLock);
209
210 newStart = ShmemSegHdr->freeoffset;
211
212 newFree = newStart + size;
213 if (newFree <= ShmemSegHdr->totalsize)
214 {
215 newSpace = (char *) ShmemBase + newStart;
216 ShmemSegHdr->freeoffset = newFree;
217 }
218 else
219 newSpace = NULL;
220
221 SpinLockRelease(ShmemLock);
222
223 /* note this assert is okay with newSpace == NULL */
224 Assert(newSpace == (void *) CACHELINEALIGN(newSpace));
225
226 return newSpace;
227 }
228
229 /*
230 * ShmemAllocUnlocked -- allocate max-aligned chunk from shared memory
231 *
232 * Allocate space without locking ShmemLock. This should be used for,
233 * and only for, allocations that must happen before ShmemLock is ready.
234 *
235 * We consider maxalign, rather than cachealign, sufficient here.
236 */
237 void *
238 ShmemAllocUnlocked(Size size)
239 {
240 Size newStart;
241 Size newFree;
242 void *newSpace;
243
244 /*
245 * Ensure allocated space is adequately aligned.
246 */
247 size = MAXALIGN(size);
248
249 Assert(ShmemSegHdr != NULL);
250
251 newStart = ShmemSegHdr->freeoffset;
252
253 newFree = newStart + size;
254 if (newFree > ShmemSegHdr->totalsize)
255 ereport(ERROR,
256 (errcode(ERRCODE_OUT_OF_MEMORY),
257 errmsg("out of shared memory (%zu bytes requested)",
258 size)));
259 ShmemSegHdr->freeoffset = newFree;
260
261 newSpace = (char *) ShmemBase + newStart;
262
263 Assert(newSpace == (void *) MAXALIGN(newSpace));
264
265 return newSpace;
266 }
267
268 /*
269 * ShmemAddrIsValid -- test if an address refers to shared memory
270 *
271 * Returns true if the pointer points within the shared memory segment.
272 */
273 bool
274 ShmemAddrIsValid(const void *addr)
275 {
276 return (addr >= ShmemBase) && (addr < ShmemEnd);
277 }
278
279 /*
280 * InitShmemIndex() --- set up or attach to shmem index table.
281 */
282 void
283 InitShmemIndex(void)
284 {
285 HASHCTL info;
286
287 /*
288 * Create the shared memory shmem index.
289 *
290 * Since ShmemInitHash calls ShmemInitStruct, which expects the ShmemIndex
291 * hashtable to exist already, we have a bit of a circularity problem in
292 * initializing the ShmemIndex itself. The special "ShmemIndex" hash
293 * table name will tell ShmemInitStruct to fake it.
294 */
295 info.keysize = SHMEM_INDEX_KEYSIZE;
296 info.entrysize = sizeof(ShmemIndexEnt);
297
298 ShmemIndex = ShmemInitHash("ShmemIndex",
299 SHMEM_INDEX_SIZE, SHMEM_INDEX_SIZE,
300 &info,
301 HASH_ELEM | HASH_STRINGS);
302 }
303
304 /*
305 * ShmemInitHash -- Create and initialize, or attach to, a
306 * shared memory hash table.
307 *
308 * We assume caller is doing some kind of synchronization
309 * so that two processes don't try to create/initialize the same
310 * table at once. (In practice, all creations are done in the postmaster
311 * process; child processes should always be attaching to existing tables.)
312 *
313 * max_size is the estimated maximum number of hashtable entries. This is
314 * not a hard limit, but the access efficiency will degrade if it is
315 * exceeded substantially (since it's used to compute directory size and
316 * the hash table buckets will get overfull).
317 *
318 * init_size is the number of hashtable entries to preallocate. For a table
319 * whose maximum size is certain, this should be equal to max_size; that
320 * ensures that no run-time out-of-shared-memory failures can occur.
321 *
322 * *infoP and hash_flags must specify at least the entry sizes and key
323 * comparison semantics (see hash_create()). Flag bits and values specific
324 * to shared-memory hash tables are added here, except that callers may
325 * choose to specify HASH_PARTITION and/or HASH_FIXED_SIZE.
326 *
327 * Note: before Postgres 9.0, this function returned NULL for some failure
328 * cases. Now, it always throws error instead, so callers need not check
329 * for NULL.
330 */
331 HTAB *
332 ShmemInitHash(const char *name, /* table string name for shmem index */
333 long init_size, /* initial table size */
334 long max_size, /* max size of the table */
335 HASHCTL *infoP, /* info about key and bucket size */
336 int hash_flags) /* info about infoP */
337 {
338 bool found;
339 void *location;
340
341 /*
342 * Hash tables allocated in shared memory have a fixed directory; it can't
343 * grow or other backends wouldn't be able to find it. So, make sure we
344 * make it big enough to start with.
345 *
346 * The shared memory allocator must be specified too.
347 */
348 infoP->dsize = infoP->max_dsize = hash_select_dirsize(max_size);
349 infoP->alloc = ShmemAllocNoError;
350 hash_flags |= HASH_SHARED_MEM | HASH_ALLOC | HASH_DIRSIZE;
351
352 /* look it up in the shmem index */
353 location = ShmemInitStruct(name,
354 hash_get_shared_size(infoP, hash_flags),
355 &found);
356
357 /*
358 * if it already exists, attach to it rather than allocate and initialize
359 * new space
360 */
361 if (found)
362 hash_flags |= HASH_ATTACH;
363
364 /* Pass location of hashtable header to hash_create */
365 infoP->hctl = (HASHHDR *) location;
366
367 return hash_create(name, init_size, infoP, hash_flags);
368 }
369
370 /*
371 * ShmemInitStruct -- Create/attach to a structure in shared memory.
372 *
373 * This is called during initialization to find or allocate
374 * a data structure in shared memory. If no other process
375 * has created the structure, this routine allocates space
376 * for it. If it exists already, a pointer to the existing
377 * structure is returned.
378 *
379 * Returns: pointer to the object. *foundPtr is set true if the object was
380 * already in the shmem index (hence, already initialized).
381 *
382 * Note: before Postgres 9.0, this function returned NULL for some failure
383 * cases. Now, it always throws error instead, so callers need not check
384 * for NULL.
385 */
386 void *
387 ShmemInitStruct(const char *name, Size size, bool *foundPtr)
388 {
389 ShmemIndexEnt *result;
390 void *structPtr;
391
392 LWLockAcquire(ShmemIndexLock, LW_EXCLUSIVE);
393
394 if (!ShmemIndex)
395 {
396 PGShmemHeader *shmemseghdr = ShmemSegHdr;
397
398 /* Must be trying to create/attach to ShmemIndex itself */
399 Assert(strcmp(name, "ShmemIndex") == 0);
400
401 if (IsUnderPostmaster)
402 {
403 /* Must be initializing a (non-standalone) backend */
404 Assert(shmemseghdr->index != NULL);
405 structPtr = shmemseghdr->index;
406 *foundPtr = true;
407 }
408 else
409 {
410 /*
411 * If the shmem index doesn't exist, we are bootstrapping: we must
412 * be trying to init the shmem index itself.
413 *
414 * Notice that the ShmemIndexLock is released before the shmem
415 * index has been initialized. This should be OK because no other
416 * process can be accessing shared memory yet.
417 */
418 Assert(shmemseghdr->index == NULL);
419 structPtr = ShmemAlloc(size);
420 shmemseghdr->index = structPtr;
421 *foundPtr = false;
422 }
423 LWLockRelease(ShmemIndexLock);
424 return structPtr;
425 }
426
427 /* look it up in the shmem index */
428 result = (ShmemIndexEnt *)
429 hash_search(ShmemIndex, name, HASH_ENTER_NULL, foundPtr);
430
431 if (!result)
432 {
433 LWLockRelease(ShmemIndexLock);
434 ereport(ERROR,
435 (errcode(ERRCODE_OUT_OF_MEMORY),
436 errmsg("could not create ShmemIndex entry for data structure \"%s\"",
437 name)));
438 }
439
440 if (*foundPtr)
441 {
442 /*
443 * Structure is in the shmem index so someone else has allocated it
444 * already. The size better be the same as the size we are trying to
445 * initialize to, or there is a name conflict (or worse).
446 */
447 if (result->size != size)
448 {
449 LWLockRelease(ShmemIndexLock);
450 ereport(ERROR,
451 (errmsg("ShmemIndex entry size is wrong for data structure"
452 " \"%s\": expected %zu, actual %zu",
453 name, size, result->size)));
454 }
455 structPtr = result->location;
456 }
457 else
458 {
459 Size allocated_size;
460
461 /* It isn't in the table yet. allocate and initialize it */
462 structPtr = ShmemAllocRaw(size, &allocated_size);
463 if (structPtr == NULL)
464 {
465 /* out of memory; remove the failed ShmemIndex entry */
466 hash_search(ShmemIndex, name, HASH_REMOVE, NULL);
467 LWLockRelease(ShmemIndexLock);
468 ereport(ERROR,
469 (errcode(ERRCODE_OUT_OF_MEMORY),
470 errmsg("not enough shared memory for data structure"
471 " \"%s\" (%zu bytes requested)",
472 name, size)));
473 }
474 result->size = size;
475 result->allocated_size = allocated_size;
476 result->location = structPtr;
477 }
478
479 LWLockRelease(ShmemIndexLock);
480
481 Assert(ShmemAddrIsValid(structPtr));
482
483 Assert(structPtr == (void *) CACHELINEALIGN(structPtr));
484
485 return structPtr;
486 }
487
488
489 /* SQL SRF showing allocated shared memory */
490 Datum
491 pg_get_shmem_allocations(PG_FUNCTION_ARGS)
492 {
493 #define PG_GET_SHMEM_SIZES_COLS 4
494 ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
495 HASH_SEQ_STATUS hstat;
496 ShmemIndexEnt *ent;
497 Size named_allocated = 0;
498 Datum values[PG_GET_SHMEM_SIZES_COLS];
499 bool nulls[PG_GET_SHMEM_SIZES_COLS];
500
501 InitMaterializedSRF(fcinfo, 0);
502
503 LWLockAcquire(ShmemIndexLock, LW_SHARED);
504
505 hash_seq_init(&hstat, ShmemIndex);
506
507 /* output all allocated entries */
508 memset(nulls, 0, sizeof(nulls));
509 while ((ent = (ShmemIndexEnt *) hash_seq_search(&hstat)) != NULL)
510 {
511 values[0] = CStringGetTextDatum(ent->key);
512 values[1] = Int64GetDatum((char *) ent->location - (char *) ShmemSegHdr);
513 values[2] = Int64GetDatum(ent->size);
514 values[3] = Int64GetDatum(ent->allocated_size);
515 named_allocated += ent->allocated_size;
516
517 tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc,
518 values, nulls);
519 }
520
521 /* output shared memory allocated but not counted via the shmem index */
522 values[0] = CStringGetTextDatum("<anonymous>");
523 nulls[1] = true;
524 values[2] = Int64GetDatum(ShmemSegHdr->freeoffset - named_allocated);
525 values[3] = values[2];
526 tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc, values, nulls);
527
528 /* output as-of-yet unused shared memory */
529 nulls[0] = true;
530 values[1] = Int64GetDatum(ShmemSegHdr->freeoffset);
531 nulls[1] = false;
532 values[2] = Int64GetDatum(ShmemSegHdr->totalsize - ShmemSegHdr->freeoffset);
533 values[3] = values[2];
534 tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc, values, nulls);
535
536 LWLockRelease(ShmemIndexLock);
537
538 return (Datum) 0;
539 }
540
541 /*
542 * SQL SRF showing NUMA memory nodes for allocated shared memory
543 *
544 * Compared to pg_get_shmem_allocations(), this function does not return
545 * information about shared anonymous allocations and unused shared memory.
546 */
547 Datum
548 pg_get_shmem_allocations_numa(PG_FUNCTION_ARGS)
549 {
550 #define PG_GET_SHMEM_NUMA_SIZES_COLS 3
551 ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
552 HASH_SEQ_STATUS hstat;
553 ShmemIndexEnt *ent;
554 Datum values[PG_GET_SHMEM_NUMA_SIZES_COLS];
555 bool nulls[PG_GET_SHMEM_NUMA_SIZES_COLS];
556 Size os_page_size;
557 void **page_ptrs;
558 int *pages_status;
559 uint64 shm_total_page_count,
560 shm_ent_page_count,
561 max_nodes;
562 Size *nodes;
563
564 if (pg_numa_init() == -1)
565 elog(ERROR, "libnuma initialization failed or NUMA is not supported on this platform");
566
567 InitMaterializedSRF(fcinfo, 0);
568
569 max_nodes = pg_numa_get_max_node();
570 nodes = palloc(sizeof(Size) * (max_nodes + 2));
571
572 /*
573 * Shared memory allocations can vary in size and may not align with OS
574 * memory page boundaries, while NUMA queries work on pages.
575 *
576 * To correctly map each allocation to NUMA nodes, we need to: 1.
577 * Determine the OS memory page size. 2. Align each allocation's start/end
578 * addresses to page boundaries. 3. Query NUMA node information for all
579 * pages spanning the allocation.
580 */
581 os_page_size = pg_get_shmem_pagesize();
582
583 /*
584 * Allocate memory for page pointers and status based on total shared
585 * memory size. This simplified approach allocates enough space for all
586 * pages in shared memory rather than calculating the exact requirements
587 * for each segment.
588 *
589 * Add 1, because we don't know how exactly the segments align to OS
590 * pages, so the allocation might use one more memory page. In practice
591 * this is not very likely, and moreover we have more entries, each of
592 * them using only fraction of the total pages.
593 */
594 shm_total_page_count = (ShmemSegHdr->totalsize / os_page_size) + 1;
595 page_ptrs = palloc0(sizeof(void *) * shm_total_page_count);
596 pages_status = palloc(sizeof(int) * shm_total_page_count);
597
598 if (firstNumaTouch)
599 elog(DEBUG1, "NUMA: page-faulting shared memory segments for proper NUMA readouts");
600
601 LWLockAcquire(ShmemIndexLock, LW_SHARED);
602
603 hash_seq_init(&hstat, ShmemIndex);
604
605 /* output all allocated entries */
606 while ((ent = (ShmemIndexEnt *) hash_seq_search(&hstat)) != NULL)
607 {
608 int i;
609 char *startptr,
610 *endptr;
611 Size total_len;
612
613 /*
614 * Calculate the range of OS pages used by this segment. The segment
615 * may start / end half-way through a page, we want to count these
616 * pages too. So we align the start/end pointers down/up, and then
617 * calculate the number of pages from that.
618 */
619 startptr = (char *) TYPEALIGN_DOWN(os_page_size, ent->location);
620 endptr = (char *) TYPEALIGN(os_page_size,
621 (char *) ent->location + ent->allocated_size);
622 total_len = (endptr - startptr);
623
624 shm_ent_page_count = total_len / os_page_size;
625
626 /*
627 * If we ever get 0xff (-1) back from kernel inquiry, then we probably
628 * have a bug in mapping buffers to OS pages.
629 */
630 memset(pages_status, 0xff, sizeof(int) * shm_ent_page_count);
631
632 /*
633 * Setup page_ptrs[] with pointers to all OS pages for this segment,
634 * and get the NUMA status using pg_numa_query_pages.
635 *
636 * In order to get reliable results we also need to touch memory
637 * pages, so that inquiry about NUMA memory node doesn't return -2
638 * (ENOENT, which indicates unmapped/unallocated pages).
639 */
640 for (i = 0; i < shm_ent_page_count; i++)
641 {
642 page_ptrs[i] = startptr + (i * os_page_size);
643
644 if (firstNumaTouch)
645 pg_numa_touch_mem_if_required(page_ptrs[i]);
646
647 CHECK_FOR_INTERRUPTS();
648 }
649
650 if (pg_numa_query_pages(0, shm_ent_page_count, page_ptrs, pages_status) == -1)
651 elog(ERROR, "failed NUMA pages inquiry status: %m");
652
653 /* Count number of NUMA nodes used for this shared memory entry */
654 memset(nodes, 0, sizeof(Size) * (max_nodes + 2));
655
656 for (i = 0; i < shm_ent_page_count; i++)
657 {
658 int s = pages_status[i];
659
660 /* Ensure we are adding only valid index to the array */
661 if (s >= 0 && s <= max_nodes)
662 {
663 /* valid NUMA node */
664 nodes[s]++;
665 continue;
666 }
667 else if (s == -2)
668 {
669 /* -2 means ENOENT (e.g. page was moved to swap) */
670 nodes[max_nodes + 1]++;
671 continue;
672 }
673
674 elog(ERROR, "invalid NUMA node id outside of allowed range "
675 "[0, " UINT64_FORMAT "]: %d", max_nodes, s);
676 }
677
678 /* no NULLs for regular nodes */
679 memset(nulls, 0, sizeof(nulls));
680
681 /*
682 * Add one entry for each NUMA node, including those without allocated
683 * memory for this segment.
684 */
685 for (i = 0; i <= max_nodes; i++)
686 {
687 values[0] = CStringGetTextDatum(ent->key);
688 values[1] = i;
689 values[2] = Int64GetDatum(nodes[i] * os_page_size);
690
691 tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc,
692 values, nulls);
693 }
694
695 /* The last entry is used for pages without a NUMA node. */
696 nulls[1] = true;
697 values[0] = CStringGetTextDatum(ent->key);
698 values[2] = Int64GetDatum(nodes[max_nodes + 1] * os_page_size);
699
700 tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc,
701 values, nulls);
702 }
703
704 LWLockRelease(ShmemIndexLock);
705 firstNumaTouch = false;
706
707 return (Datum) 0;
708 }
709
710 /*
711 * Determine the memory page size used for the shared memory segment.
712 *
713 * If the shared segment was allocated using huge pages, returns the size of
714 * a huge page. Otherwise returns the size of regular memory page.
715 *
716 * This should be used only after the server is started.
717 */
718 Size
719 pg_get_shmem_pagesize(void)
720 {
721 Size os_page_size;
722 #ifdef WIN32
723 SYSTEM_INFO sysinfo;
724
725 GetSystemInfo(&sysinfo);
726 os_page_size = sysinfo.dwPageSize;
727 #else
728 os_page_size = sysconf(_SC_PAGESIZE);
729 #endif
730
731 Assert(IsUnderPostmaster);
732 Assert(huge_pages_status != HUGE_PAGES_UNKNOWN);
733
734 if (huge_pages_status == HUGE_PAGES_ON)
735 GetHugePageSize(&os_page_size, NULL);
736
737 return os_page_size;
738 }
739
740 Datum
741 pg_numa_available(PG_FUNCTION_ARGS)
742 {
743 PG_RETURN_BOOL(pg_numa_init() != -1);
744 }