Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
3 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
/*-------------------------------------------------------------------------
 *
 * shmem.c
 *    create shared memory and initialize shared memory data structures.
 *
 * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
 * Portions Copyright (c) 1994, Regents of the University of California
 *
 *
 * IDENTIFICATION
 *    src/backend/storage/ipc/shmem.c
 *
 *-------------------------------------------------------------------------
 */
/*
 * POSTGRES processes share one or more regions of shared memory.
 * The shared memory is created by a postmaster and is inherited
 * by each backend via fork() (or, in some ports, via other OS-specific
 * methods).  The routines in this file are used for allocating and
 * binding to shared memory data structures.
 *
 * This module provides facilities to allocate fixed-size structures in shared
 * memory, for things like variables shared between all backend processes.
 * Each such structure has a string name to identify it, specified when it is
 * requested.  shmem_hash.c provides a shared hash table implementation on top
 * of that.
 *
 * Shared memory areas should usually not be allocated after postmaster
 * startup, although we do allow small allocations later for the benefit of
 * extension modules that are loaded after startup.  Despite that allowance,
 * extensions that need shared memory should be added in
 * shared_preload_libraries, because the allowance is quite small and there is
 * no guarantee that any memory is available after startup.
 *
 * Nowadays, there is also another way to allocate shared memory called
 * Dynamic Shared Memory.  See dsm.c for that facility.  One big difference
 * between traditional shared memory handled by shmem.c and dynamic shared
 * memory is that traditional shared memory areas are mapped to the same
 * address in all processes, so you can use normal pointers in shared memory
 * structs.  With Dynamic Shared Memory, you must use offsets or DSA pointers
 * instead.
 *
 * Shared memory managed by shmem.c can never be freed, once allocated.  Each
 * hash table has its own free list, so hash buckets can be reused when an
 * item is deleted.
 *
 * Usage
 * -----
 *
 * To allocate shared memory, you need to register a set of callback functions
 * which handle the lifecycle of the allocation.  In the request_fn callback,
 * call ShmemRequestStruct() with the desired name and size.  When the area is
 * later allocated or attached to, the global variable pointed to by the .ptr
 * option is set to the shared memory location of the allocation.  The init_fn
 * callback can perform additional initialization.
 *
 *  typedef struct MyShmemData {
 *      ...
 *  } MyShmemData;
 *
 *  static MyShmemData *MyShmem;
 *
 *  static void my_shmem_request(void *arg);
 *  static void my_shmem_init(void *arg);
 *
 *  const ShmemCallbacks MyShmemCallbacks = {
 *      .request_fn = my_shmem_request,
 *      .init_fn = my_shmem_init,
 *  };
 *
 *  static void
 *  my_shmem_request(void *arg)
 *  {
 *      ShmemRequestStruct(.name = "My shmem area",
 *                         .size = sizeof(MyShmemData),
 *                         .ptr = (void **) &MyShmem,
 *          );
 *  }
 *
 * In builtin PostgreSQL code, add the callbacks to the list in
 * src/include/storage/subsystemlist.h.  In an add-in module, you can register
 * the callbacks by calling RegisterShmemCallbacks(&MyShmemCallbacks) in the
 * extension's _PG_init() function.
 *
 * Lifecycle
 * ---------
 *
 * Initializing shared memory happens in multiple phases.  In the first phase,
 * during postmaster startup, all the request_fn callbacks are called.  Only
 * after all the request_fn callbacks have been called and all the shmem areas
 * have been requested by the ShmemRequestStruct() calls we know how much
 * shared memory we need in total.  After that, postmaster allocates global
 * shared memory segment, and calls all the init_fn callbacks to initialize
 * all the requested shmem areas.
 *
 * In standard Unix-ish environments, individual backends do not need to
 * re-establish their local pointers into shared memory, because they inherit
 * correct values of those variables via fork() from the postmaster.  However,
 * this does not work in the EXEC_BACKEND case.  In ports using EXEC_BACKEND,
 * backend startup also calls the shmem_request callbacks to re-establish the
 * knowledge about each shared memory area, sets the pointer variables
 * (*options->ptr), and calls the attach_fn callback, if any, for additional
 * per-backend setup.
 *
 * Legacy ShmemInitStruct()/ShmemInitHash() functions
 * --------------------------------------------------
 *
 * ShmemInitStruct()/ShmemInitHash() is another way of registering shmem
 * areas.  It pre-dates the ShmemRequestStruct()/ShmemRequestHash() functions,
 * and should not be used in new code, but as of this writing it is still
 * widely used in extensions.
 *
 * To allocate a shmem area with ShmemInitStruct(), you need to separately
 * register the size needed for the area by calling RequestAddinShmemSpace()
 * from the extension's shmem_request_hook, and allocate the area by calling
 * ShmemInitStruct() from the extension's shmem_startup_hook.  There are no
 * init/attach callbacks.  Instead, the caller of ShmemInitStruct() must check
 * the return status of ShmemInitStruct() and initialize the struct if it was
 * not previously initialized.
 *
 * Calling ShmemAlloc() directly
 * -----------------------------
 *
 * There's a more low-level way of allocating shared memory too: you can call
 * ShmemAlloc() directly.  It's used to implement the higher level mechanisms,
 * and should generally not be called directly.
 */
 
#include "postgres.h"
 
#include <unistd.h>
 
#include "access/slru.h"
#include "common/int.h"
#include "fmgr.h"
#include "funcapi.h"
#include "miscadmin.h"
#include "port/pg_bitutils.h"
#include "port/pg_numa.h"
#include "storage/lwlock.h"
#include "storage/pg_shmem.h"
#include "storage/shmem.h"
#include "storage/shmem_internal.h"
#include "storage/spin.h"
#include "utils/builtins.h"
#include "utils/tuplestore.h"
 
typedef struct ShmemIndexEnt ShmemIndexEnt;
 
/*
 * Registered callbacks.
 *
 * During postmaster startup, we accumulate the callbacks from all subsystems
 * in this list.
 *
 * This is in process private memory, although on Unix-like systems, we expect
 * all the registrations to happen at postmaster startup time and be inherited
 * by all the child processes via fork().
 */
static List *registered_shmem_callbacks;
 
/*
 * In the shmem request phase, all the shmem areas requested with the
 * ShmemRequest*() functions are accumulated in the 'pending_shmem_requests'
 * list.  The List, the ShmemRequest structs, and the 'options' are all
 * allocated in TopMemoryContext.
 */
typedef struct
{
    ShmemStructOpts *options;
    ShmemRequestKind kind;
 
    /* InitShmemIndexEntry() sets this pointer when the area is allocated */
    ShmemIndexEnt *index_entry;
} ShmemRequest;
 
static List *pending_shmem_requests;    /* List of ShmemRequests */
 
/*
 * Per-process state machine, for sanity checking that we do things in the
 * right order.
 *
 * Postmaster:
 *   INITIAL -> REQUESTING -> INITIALIZING -> DONE
 *
 * Backends in EXEC_BACKEND mode:
 *   INITIAL -> REQUESTING -> ATTACHING -> DONE
 *
 * Late request:
 *   DONE -> REQUESTING -> AFTER_STARTUP_ATTACH_OR_INIT -> DONE
 */
enum shmem_request_state
{
    /* Initial state */
    SRS_INITIAL,
 
    /*
     * When we start calling the shmem_request callbacks, we enter the
     * SRS_REQUESTING phase.  All ShmemRequestStruct calls happen in this
     * state.
     */
    SRS_REQUESTING,
 
    /*
     * Postmaster has finished all shmem requests, and is now initializing the
     * shared memory segment.  init_fn callbacks are called in this state.
     */
    SRS_INITIALIZING,
 
    /*
     * A postmaster child process is starting up.  attach_fn callbacks are
     * called in this state.
     */
    SRS_ATTACHING,
 
    /* An after-startup allocation or attachment is in progress */
    SRS_AFTER_STARTUP_ATTACH_OR_INIT,
 
    /* Normal state after shmem initialization / attachment */
    SRS_DONE,
};
static enum shmem_request_state shmem_request_state = SRS_INITIAL;
 
/*
 * This is the first data structure stored in the shared memory segment, at
 * the offset that PGShmemHeader->content_offset points to.  Allocations by
 * ShmemAlloc() are carved out of the space after this.
 *
 * For the base pointer and the total size of the shmem segment, we rely on
 * the PGShmemHeader.
 */
typedef struct ShmemAllocatorData
{
    Size        free_offset;    /* offset to first free space from ShmemBase */
 
    /* protects 'free_offset' */
    slock_t     shmem_lock;
 
    HASHHDR    *index;          /* location of ShmemIndex */
    size_t      index_size;     /* size of shmem region holding ShmemIndex */
    LWLock      index_lock;     /* protects ShmemIndex */
} ShmemAllocatorData;
 
#define ShmemIndexLock (&ShmemAllocator->index_lock)
 
static void *ShmemAllocRaw(Size size, Size alignment, Size *allocated_size);
 
/* shared memory global variables */
 
static PGShmemHeader *ShmemSegHdr;  /* shared mem segment header */
static void *ShmemBase;         /* start address of shared memory */
static void *ShmemEnd;          /* end+1 address of shared memory */
 
static ShmemAllocatorData *ShmemAllocator;
 
/*
 * ShmemIndex is a global directory of shmem areas, itself also stored in the
 * shared memory.
 */
static HTAB *ShmemIndex;
 
 /* max size of data structure string name */
#define SHMEM_INDEX_KEYSIZE      (48)
 
/*
 * # of additional entries to reserve in the shmem index table, for
 * allocations after postmaster startup.  (This is not a hard limit, the hash
 * table can grow larger than that if there is shared memory available)
 */
#define SHMEM_INDEX_ADDITIONAL_SIZE      (128)
 
/* this is a hash bucket in the shmem index table */
typedef struct ShmemIndexEnt
{
    char        key[SHMEM_INDEX_KEYSIZE];   /* string name */
    void       *location;       /* location in shared mem */
    Size        size;           /* # bytes requested for the structure */
    Size        allocated_size; /* # bytes actually allocated */
    bool        initialized;    /* has the init callback been run? */
} ShmemIndexEnt;
 
/* To get reliable results for NUMA inquiry we need to "touch pages" once */
static bool firstNumaTouch = true;
 
static void CallShmemCallbacksAfterStartup(const ShmemCallbacks *callbacks);
static void ProcessShmemRequestsAfterStartup(const ShmemCallbacks *callbacks);
static void InitShmemIndexEntry(ShmemRequest *request);
static bool AttachShmemIndexEntry(ShmemRequest *request, bool missing_ok);
 
Datum       pg_numa_available(PG_FUNCTION_ARGS);
 
/*
 *  ShmemRequestStruct() --- request a named shared memory area
 *
 * Subsystems call this to register their shared memory needs.  This is
 * usually done early in postmaster startup, before the shared memory segment
 * has been created, so that the size can be included in the estimate for
 * total amount of shared memory needed.  We set aside a small amount of
 * memory for allocations that happen later, for the benefit of non-preloaded
 * extensions, but that should not be relied upon.
 *
 * This does not yet allocate the memory, but merely registers the need for
 * it.  The actual allocation happens later in the postmaster startup
 * sequence.
 *
 * This must be called from a shmem_request callback function, registered with
 * RegisterShmemCallbacks().  This enforces a coding pattern that works the
 * same in normal Unix systems and with EXEC_BACKEND.  On Unix systems, the
 * shmem_request callbacks are called once, early in postmaster startup, and
 * the child processes inherit the struct descriptors and any other
 * per-process state from the postmaster.  In EXEC_BACKEND mode, shmem_request
 * callbacks are *also* called in each backend, at backend startup, to
 * re-establish the struct descriptors.  By calling the same function in both
 * cases, we ensure that all the shmem areas are registered the same way in
 * all processes.
 *
 * 'options' defines the name and size of the area, and any other optional
 * features.  Leave unused options as zeros.  The options are copied to
 * longer-lived memory, so it doesn't need to live after the
 * ShmemRequestStruct() call and can point to a local variable in the calling
 * function.  The 'name' must point to a long-lived string though, only the
 * pointer to it is copied.
 */
void
ShmemRequestStructWithOpts(const ShmemStructOpts *options)
{
    ShmemStructOpts *options_copy;
 
    options_copy = MemoryContextAlloc(TopMemoryContext,
                                      sizeof(ShmemStructOpts));
    memcpy(options_copy, options, sizeof(ShmemStructOpts));
 
    ShmemRequestInternal(options_copy, SHMEM_KIND_STRUCT);
}
 
/*
 * Internal workhorse of ShmemRequestStruct() and ShmemRequestHash().
 *
 * Note: Unlike in the public ShmemRequestStruct() and ShmemRequestHash()
 * functions, 'options' is *not* copied.  It must be allocated in
 * TopMemoryContext by the caller, and will be freed after the init/attach
 * callbacks have been called.  This allows ShmemRequestHash() to pass a
 * pointer to the extended ShmemHashOpts struct instead.
 */
void
ShmemRequestInternal(ShmemStructOpts *options, ShmemRequestKind kind)
{
    MemoryContext oldcontext;
    ShmemRequest *request;
 
    /* Check that we're in the right state */
    if (shmem_request_state != SRS_REQUESTING)
        elog(ERROR, "ShmemRequestStruct can only be called from a shmem_request callback");
 
    /* Check the options */
    if (options->name == NULL)
        elog(ERROR, "shared memory request is missing 'name' option");
 
    if (options->size == SHMEM_ATTACH_UNKNOWN_SIZE)
    {
        if (ShmemIndex == NULL)
            elog(ERROR, "SHMEM_ATTACH_UNKNOWN_SIZE cannot be used during startup");
    }
    else if (options->size <= 0)
        elog(ERROR, "invalid size %zd for shared memory request for \"%s\"",
             options->size, options->name);
 
    if (options->alignment != 0 && pg_nextpower2_size_t(options->alignment) != options->alignment)
        elog(ERROR, "invalid alignment %zu for shared memory request for \"%s\"",
             options->alignment, options->name);
 
    /* Check that it's not already registered in this process */
    foreach_ptr(ShmemRequest, existing, pending_shmem_requests)
    {
        if (strcmp(existing->options->name, options->name) == 0)
            ereport(ERROR,
                    (errmsg("shared memory struct \"%s\" is already registered",
                            options->name)));
    }
 
    /* Request looks valid, remember it */
    oldcontext = MemoryContextSwitchTo(TopMemoryContext);
    request = palloc_object(ShmemRequest);
    request->options = options;
    request->kind = kind;
    request->index_entry = NULL;
    pending_shmem_requests = lappend(pending_shmem_requests, request);
    MemoryContextSwitchTo(oldcontext);
}
 
/*
 *  ShmemGetRequestedSize() --- estimate the total size of all registered shared
 *                              memory structures.
 *
 * This is called at postmaster startup, before the shared memory segment has
 * been created.
 */
size_t
ShmemGetRequestedSize(void)
{
    size_t      size;
 
    /* memory needed for the ShmemIndex */
    size = hash_estimate_size(list_length(pending_shmem_requests) + SHMEM_INDEX_ADDITIONAL_SIZE,
                              sizeof(ShmemIndexEnt));
    size = CACHELINEALIGN(size);
 
    /* memory needed for all the requested areas */
    foreach_ptr(ShmemRequest, request, pending_shmem_requests)
    {
        size_t      alignment = request->options->alignment;
 
        /* pad the start address for alignment like ShmemAllocRaw() does */
        if (alignment < PG_CACHE_LINE_SIZE)
            alignment = PG_CACHE_LINE_SIZE;
        size = TYPEALIGN(alignment, size);
 
        size = add_size(size, request->options->size);
    }
 
    return size;
}
 
/*
 *  ShmemInitRequested() --- allocate and initialize requested shared memory
 *                            structures.
 *
 * This is called once at postmaster startup, after the shared memory segment
 * has been created.
 */
void
ShmemInitRequested(void)
{
    /* should be called only by the postmaster or a standalone backend */
    Assert(!IsUnderPostmaster);
    Assert(shmem_request_state == SRS_INITIALIZING);
 
    /*
     * Initialize the ShmemIndex entries and perform basic initialization of
     * all the requested memory areas.  There are no concurrent processes yet,
     * so no need for locking.
     */
    foreach_ptr(ShmemRequest, request, pending_shmem_requests)
    {
        InitShmemIndexEntry(request);
    }
 
    /*
     * Call the subsystem-specific init callbacks to finish initialization of
     * all the areas.
     */
    foreach_ptr(const ShmemCallbacks, callbacks, registered_shmem_callbacks)
    {
        if (callbacks->init_fn)
            callbacks->init_fn(callbacks->opaque_arg);
    }
 
    /* Now we can mark all the areas as initialized and free the requests */
    foreach_ptr(ShmemRequest, request, pending_shmem_requests)
    {
        request->index_entry->initialized = true;
        pfree(request->options);
    }
    list_free_deep(pending_shmem_requests);
    pending_shmem_requests = NIL;
 
    shmem_request_state = SRS_DONE;
}
 
/*
 * Re-establish process private state related to shmem areas.
 *
 * This is called at backend startup in EXEC_BACKEND mode, in every backend.
 */
#ifdef EXEC_BACKEND
void
ShmemAttachRequested(void)
{
    ListCell   *lc;
 
    /* Must be initializing a (non-standalone) backend */
    Assert(IsUnderPostmaster);
    Assert(ShmemAllocator->index != NULL);
    Assert(shmem_request_state == SRS_REQUESTING);
    shmem_request_state = SRS_ATTACHING;
 
    LWLockAcquire(ShmemIndexLock, LW_SHARED);
 
    /*
     * Attach to all the requested memory areas.
     */
    foreach_ptr(ShmemRequest, request, pending_shmem_requests)
    {
        AttachShmemIndexEntry(request, false);
        pfree(request->options);
    }
    list_free_deep(pending_shmem_requests);
    pending_shmem_requests = NIL;
 
    /* Call attach callbacks */
    foreach(lc, registered_shmem_callbacks)
    {
        const ShmemCallbacks *callbacks = (const ShmemCallbacks *) lfirst(lc);
 
        if (callbacks->attach_fn)
            callbacks->attach_fn(callbacks->opaque_arg);
    }
 
    LWLockRelease(ShmemIndexLock);
 
    shmem_request_state = SRS_DONE;
}
#endif
 
/*
 * Insert requested shmem area into the shared memory index and initialize it.
 *
 * Note that this only does performs basic initialization depending on
 * ShmemRequestKind, like setting the global pointer variable to the area for
 * SHMEM_KIND_STRUCT or setting up the backend-private HTAB control struct.
 * This does *not* call the subsystem-specific init callbacks.  That's done
 * later after all the shmem areas have been initialized or attached to.
 */
static void
InitShmemIndexEntry(ShmemRequest *request)
{
    const char *name = request->options->name;
    ShmemIndexEnt *index_entry;
    bool        found;
    size_t      allocated_size;
    void       *structPtr;
 
    /* Size must be known at this point. */
    Assert(request->options->size != SHMEM_ATTACH_UNKNOWN_SIZE);
 
    /* look it up in the shmem index */
    index_entry = (ShmemIndexEnt *)
        hash_search(ShmemIndex, name, HASH_ENTER_NULL, &found);
    if (found)
        elog(ERROR, "shared memory struct \"%s\" is already initialized", name);
    if (!index_entry)
    {
        /* tried to add it to the hash table, but there was no space */
        ereport(ERROR,
                (errcode(ERRCODE_OUT_OF_MEMORY),
                 errmsg("could not create ShmemIndex entry for data structure \"%s\"",
                        name)));
    }
 
    /*
     * We inserted the entry to the shared memory index.  Allocate requested
     * amount of shared memory for it, and initialize the index entry.
     */
    structPtr = ShmemAllocRaw(request->options->size,
                              request->options->alignment,
                              &allocated_size);
    if (structPtr == NULL)
    {
        /* out of memory; remove the failed ShmemIndex entry */
        hash_search(ShmemIndex, name, HASH_REMOVE, NULL);
        ereport(ERROR,
                (errcode(ERRCODE_OUT_OF_MEMORY),
                 errmsg("not enough shared memory for data structure"
                        " \"%s\" (%zd bytes requested)",
                        name, request->options->size)));
    }
    index_entry->size = request->options->size;
    index_entry->allocated_size = allocated_size;
    index_entry->location = structPtr;
 
    /*
     * The area is considered fully initialized only after the subsystem's
     * init callback has been called.  For now, perform only basic
     * initialization based on the kind of shmem area it is.
     */
    index_entry->initialized = false;
    switch (request->kind)
    {
        case SHMEM_KIND_STRUCT:
            if (request->options->ptr)
                *(request->options->ptr) = index_entry->location;
            break;
        case SHMEM_KIND_HASH:
            shmem_hash_init(structPtr, request->options);
            break;
        case SHMEM_KIND_SLRU:
            shmem_slru_init(structPtr, request->options);
            break;
    }
 
    /* return the pointer to the entry to the caller */
    request->index_entry = index_entry;
}
 
/*
 * Look up a named shmem area in the shared memory index and attach to it.
 *
 * Note that this only performs the basic attachment actions depending on
 * ShmemRequestKind, like setting the global pointer variable to the area for
 * SHMEM_KIND_STRUCT or setting up the backend-private HTAB control struct.
 * This does *not* call the subsystem-specific attach callbacks.  That's done
 * later after all the shmem areas have been initialized or attached to.
 */
static bool
AttachShmemIndexEntry(ShmemRequest *request, bool missing_ok)
{
    const char *name = request->options->name;
    ShmemIndexEnt *index_entry;
 
    /* Look it up in the shmem index */
    index_entry = (ShmemIndexEnt *)
        hash_search(ShmemIndex, name, HASH_FIND, NULL);
    if (!index_entry)
    {
        if (!missing_ok)
            ereport(ERROR,
                    (errmsg("could not find ShmemIndex entry for data structure \"%s\"",
                            request->options->name)));
        return false;
    }
 
    /*
     * If it was previously allocated but not fully initialized, error out.
     * There is currently no way of retrying or cleaning up an uninitialized
     * entry, it just lingers until the server is shut down.  But this can
     * only happen when allocating areas after postmaster startup, and it's
     * unlikely that you could successfully retry anyway.  The most likely
     * reason for failed initialization is that you are out of shared memory
     * and retrying won't help with that.
     */
    if (!index_entry->initialized)
        ereport(ERROR,
                (errmsg("cannot attach to shared memory struct \"%s\" because it was not fully initialized",
                        request->options->name)));
 
    /* Check that the size in the index matches the request */
    if (index_entry->size != request->options->size &&
        request->options->size != SHMEM_ATTACH_UNKNOWN_SIZE)
    {
        ereport(ERROR,
                (errmsg("shared memory struct \"%s\" was created with"
                        " different size: existing %zu, requested %zd",
                        name, index_entry->size, request->options->size)));
    }
 
    /*
     * Re-establish the caller's pointer variable, or do other actions to
     * attach depending on the kind of shmem area it is.
     */
    switch (request->kind)
    {
        case SHMEM_KIND_STRUCT:
            if (request->options->ptr)
                *(request->options->ptr) = index_entry->location;
            break;
        case SHMEM_KIND_HASH:
            shmem_hash_attach(index_entry->location, request->options);
            break;
        case SHMEM_KIND_SLRU:
            shmem_slru_attach(index_entry->location, request->options);
            break;
    }
 
    request->index_entry = index_entry;
 
    return true;
}
 
/*
 *  InitShmemAllocator() --- set up basic pointers to shared memory.
 *
 * Called at postmaster or stand-alone backend startup, to initialize the
 * allocator's data structure in the shared memory segment.  In EXEC_BACKEND,
 * this is also called at backend startup, to set up pointers to the
 * already-initialized data structure.
 */
void
InitShmemAllocator(PGShmemHeader *seghdr)
{
    Size        offset;
    int64       hash_nelems;
    HASHCTL     info;
    int         hash_flags;
 
#ifndef EXEC_BACKEND
    Assert(!IsUnderPostmaster);
#endif
    Assert(seghdr != NULL);
 
    if (IsUnderPostmaster)
    {
        Assert(shmem_request_state == SRS_INITIAL);
    }
    else
    {
        Assert(shmem_request_state == SRS_REQUESTING);
        shmem_request_state = SRS_INITIALIZING;
    }
 
    /*
     * We assume the pointer and offset are MAXALIGN.  Not a hard requirement,
     * but it's true today and keeps the math below simpler.
     */
    Assert(seghdr == (void *) MAXALIGN(seghdr));
    Assert(seghdr->content_offset == MAXALIGN(seghdr->content_offset));
 
    /*
     * Allocations after this point should go through ShmemAlloc, which
     * expects to allocate everything on cache line boundaries.  Make sure the
     * first allocation begins on a cache line boundary.
     */
    offset = CACHELINEALIGN(seghdr->content_offset + sizeof(ShmemAllocatorData));
    if (offset > seghdr->totalsize)
        ereport(ERROR,
                (errcode(ERRCODE_OUT_OF_MEMORY),
                 errmsg("out of shared memory (%zu bytes requested)",
                        offset)));
 
    /*
     * In postmaster or stand-alone backend, initialize the shared memory
     * allocator so that we can allocate shared memory for ShmemIndex using
     * ShmemAlloc().  In a regular backend just set up the pointers required
     * by ShmemAlloc().
     */
    ShmemAllocator = (ShmemAllocatorData *) ((char *) seghdr + seghdr->content_offset);
    if (!IsUnderPostmaster)
    {
        SpinLockInit(&ShmemAllocator->shmem_lock);
        ShmemAllocator->free_offset = offset;
        LWLockInitialize(&ShmemAllocator->index_lock, LWTRANCHE_SHMEM_INDEX);
    }
 
    ShmemSegHdr = seghdr;
    ShmemBase = seghdr;
    ShmemEnd = (char *) ShmemBase + seghdr->totalsize;
 
    /*
     * Create (or attach to) the shared memory index of shmem areas.
     *
     * This is the same initialization as ShmemInitHash() does, but we cannot
     * use ShmemInitHash() here because it relies on ShmemIndex being already
     * initialized.
     */
    hash_nelems = list_length(pending_shmem_requests) + SHMEM_INDEX_ADDITIONAL_SIZE;
 
    info.keysize = SHMEM_INDEX_KEYSIZE;
    info.entrysize = sizeof(ShmemIndexEnt);
    hash_flags = HASH_ELEM | HASH_STRINGS | HASH_FIXED_SIZE;
 
    if (!IsUnderPostmaster)
    {
        ShmemAllocator->index_size = hash_estimate_size(hash_nelems, info.entrysize);
        ShmemAllocator->index = (HASHHDR *) ShmemAlloc(ShmemAllocator->index_size);
    }
    ShmemIndex = shmem_hash_create(ShmemAllocator->index,
                                   ShmemAllocator->index_size,
                                   IsUnderPostmaster,
                                   "ShmemIndex", hash_nelems,
                                   &info, hash_flags);
    Assert(ShmemIndex != NULL);
 
    /*
     * Add an entry for ShmemIndex itself into ShmemIndex, so that it's
     * visible in the pg_shmem_allocations view
     */
    if (!IsUnderPostmaster)
    {
        bool        found;
        ShmemIndexEnt *result = (ShmemIndexEnt *)
            hash_search(ShmemIndex, "ShmemIndex", HASH_ENTER, &found);
 
        Assert(!found);
        result->size = ShmemAllocator->index_size;
        result->allocated_size = ShmemAllocator->index_size;
        result->location = ShmemAllocator->index;
        result->initialized = true;
    }
}
 
/*
 * Reset state on postmaster crash restart.
 */
void
ResetShmemAllocator(void)
{
    Assert(!IsUnderPostmaster);
    ShmemAllocator = NULL;
    ShmemIndex = NULL;
    shmem_request_state = SRS_INITIAL;
 
    pending_shmem_requests = NIL;
 
    /*
     * Note that we don't clear the registered callbacks.  We will need to
     * call them again as we restart
     */
}
 
/*
 * ShmemAlloc -- allocate max-aligned chunk from shared memory
 *
 * Throws error if request cannot be satisfied.
 *
 * Assumes ShmemSegHdr is initialized.
 */
void *
ShmemAlloc(Size size)
{
    void       *newSpace;
    Size        allocated_size;
 
    newSpace = ShmemAllocRaw(size, 0, &allocated_size);
    if (!newSpace)
        ereport(ERROR,
                (errcode(ERRCODE_OUT_OF_MEMORY),
                 errmsg("out of shared memory (%zu bytes requested)",
                        size)));
    return newSpace;
}
 
/*
 * ShmemAllocNoError -- allocate max-aligned chunk from shared memory
 *
 * As ShmemAlloc, but returns NULL if out of space, rather than erroring.
 */
void *
ShmemAllocNoError(Size size)
{
    Size        allocated_size;
 
    return ShmemAllocRaw(size, 0, &allocated_size);
}
 
/*
 * ShmemAllocRaw -- allocate align chunk and return allocated size
 *
 * Also sets *allocated_size to the number of bytes allocated, which will
 * be equal to the number requested plus any padding we choose to add.
 *
 * Returns NULL in case space can not be allocated.
 */
static void *
ShmemAllocRaw(Size size, Size alignment, Size *allocated_size)
{
    Size        rawStart;
    Size        newStart;
    Size        newFree;
    void       *newSpace;
 
    /*
     * Ensure all space is adequately aligned.  We used to only MAXALIGN this
     * space but experience has proved that on modern systems that is not good
     * enough.  Many parts of the system are very sensitive to critical data
     * structures getting split across cache line boundaries.  To avoid that,
     * attempt to align the beginning of the allocation to a cache line
     * boundary.  The calling code will still need to be careful about how it
     * uses the allocated space - e.g. by padding each element in an array of
     * structures out to a power-of-two size - but without this, even that
     * won't be sufficient.
     */
    if (alignment < PG_CACHE_LINE_SIZE)
        alignment = PG_CACHE_LINE_SIZE;
 
    Assert(ShmemSegHdr != NULL);
 
    SpinLockAcquire(&ShmemAllocator->shmem_lock);
 
    rawStart = ShmemAllocator->free_offset;
    newStart = TYPEALIGN(alignment, rawStart);
 
    /*
     * newFree = newStart + size, which is the start of the remaining space
     * after the allocation.  If it exceeds the shmem segment size, we don't
     * have enough space available.
     */
    if (!pg_add_size_overflow(newStart, size, &newFree) &&
        newFree <= ShmemSegHdr->totalsize)
    {
        newSpace = (char *) ShmemBase + newStart;
        ShmemAllocator->free_offset = newFree;
    }
    else
        newSpace = NULL;
 
    SpinLockRelease(&ShmemAllocator->shmem_lock);
 
    /* note this assert is okay with newSpace == NULL */
    Assert(newSpace == (void *) TYPEALIGN(alignment, newSpace));
 
    *allocated_size = newFree - rawStart;
    return newSpace;
}
 
/*
 * ShmemAddrIsValid -- test if an address refers to shared memory
 *
 * Returns true if the pointer points within the shared memory segment.
 */
bool
ShmemAddrIsValid(const void *addr)
{
    return (addr >= ShmemBase) && (addr < ShmemEnd);
}
 
/*
 * Register callbacks that define a shared memory area (or multiple areas).
 *
 * The system will call the callbacks at different stages of postmaster or
 * backend startup, to allocate and initialize the area.
 *
 * This is normally called early during postmaster startup, but if the
 * SHMEM_CALLBACKS_ALLOW_AFTER_STARTUP is set, this can also be used after
 * startup, although after startup there's no guarantee that there's enough
 * shared memory available.  When called after startup, this immediately calls
 * the right callbacks depending on whether another backend had already
 * initialized the area.
 *
 * Note: In EXEC_BACKEND mode, this needs to be called in every backend
 * process.  That's needed because we cannot pass down the callback function
 * pointers from the postmaster process, because different processes may have
 * loaded libraries to different addresses.
 */
void
RegisterShmemCallbacks(const ShmemCallbacks *callbacks)
{
    if (shmem_request_state == SRS_DONE)
    {
        /*
         * After-startup initialization or attachment.  Call the appropriate
         * callbacks immediately.
         *
         * This is not allowed from the postmaster, because the postmaster
         * cannot acquire locks.
         */
        if ((callbacks->flags & SHMEM_CALLBACKS_ALLOW_AFTER_STARTUP) == 0)
            elog(ERROR, "cannot request shared memory at this time");
        Assert(IsUnderPostmaster || !IsPostmasterEnvironment);
 
        CallShmemCallbacksAfterStartup(callbacks);
    }
    else if (shmem_request_state == SRS_INITIAL)
    {
        /* Remember the callbacks for later */
        registered_shmem_callbacks = lappend(registered_shmem_callbacks,
                                             (void *) callbacks);
    }
    else
        elog(ERROR, "cannot request shared memory at this time");
}
 
/*
 * Register a shmem area (or multiple areas) after startup.
 */
static void
CallShmemCallbacksAfterStartup(const ShmemCallbacks *callbacks)
{
    Assert(shmem_request_state == SRS_DONE);
    Assert(pending_shmem_requests == NIL);
 
    PG_TRY();
    {
        shmem_request_state = SRS_REQUESTING;
 
        /*
         * Call the request callback first.  The callback makes
         * ShmemRequest*() calls for each shmem area, adding them to
         * pending_shmem_requests.
         */
        if (callbacks->request_fn)
            callbacks->request_fn(callbacks->opaque_arg);
 
        /* Process all the requests */
        shmem_request_state = SRS_AFTER_STARTUP_ATTACH_OR_INIT;
        if (pending_shmem_requests != NIL)
            ProcessShmemRequestsAfterStartup(callbacks);
    }
    PG_FINALLY();
    {
        foreach_ptr(ShmemRequest, request, pending_shmem_requests)
            pfree(request->options);
        list_free_deep(pending_shmem_requests);
        pending_shmem_requests = NIL;
 
        shmem_request_state = SRS_DONE;
    }
    PG_END_TRY();
}
 
static void
ProcessShmemRequestsAfterStartup(const ShmemCallbacks *callbacks)
{
    bool        found_any;
    bool        notfound_any;
 
    /* There should be some requests to process */
    Assert(pending_shmem_requests != NIL);
 
    /* Caller manages the global state variable */
    Assert(shmem_request_state == SRS_AFTER_STARTUP_ATTACH_OR_INIT);
 
    /*
     * Hold ShmemIndexLock while we allocate all the shmem entries and run all
     * the initializers.
     */
    LWLockAcquire(ShmemIndexLock, LW_EXCLUSIVE);
 
    /*
     * Check if the requested shared memory areas have already been
     * initialized.  We assume all the areas requested by the request callback
     * to form a coherent unit such that they're all already initialized or
     * none.  Otherwise it would be ambiguous which callback, init or attach,
     * to callback afterwards.
     */
    found_any = notfound_any = false;
    foreach_ptr(ShmemRequest, request, pending_shmem_requests)
    {
        ShmemIndexEnt *index_entry;
 
        index_entry = (ShmemIndexEnt *)
            hash_search(ShmemIndex, request->options->name, HASH_FIND, NULL);
        if (index_entry)
        {
            /*
             * Check for a half-initialized area.  (See also similar check in
             * AttachShmemIndexEntry())
             */
            if (!index_entry->initialized)
                ereport(ERROR,
                        (errmsg("cannot attach to shared memory struct \"%s\" because it was not fully initialized",
                                request->options->name)));
            found_any = true;
        }
        else
        {
            if (request->options->size == SHMEM_ATTACH_UNKNOWN_SIZE)
                ereport(ERROR,
                        (errmsg("cannot attach to shared memory struct \"%s\" because it does not exist",
                                request->options->name),
                         errdetail("SHMEM_ATTACH_UNKNOWN_SIZE can only be used to attach to an existing shared memory structure.")));
            notfound_any = true;
        }
    }
    if (found_any && notfound_any)
        elog(ERROR, "some of the requested shmem areas have already been initialized");
 
    /*
     * Allocate or attach all the shmem areas requested by the request_fn
     * callback.
     */
    foreach_ptr(ShmemRequest, request, pending_shmem_requests)
    {
        if (found_any)
            AttachShmemIndexEntry(request, false);
        else
            InitShmemIndexEntry(request);
    }
 
    /* Finish by calling the appropriate subsystem-specific callback */
    if (found_any)
    {
        if (callbacks->attach_fn)
            callbacks->attach_fn(callbacks->opaque_arg);
    }
    else
    {
        if (callbacks->init_fn)
            callbacks->init_fn(callbacks->opaque_arg);
    }
 
    foreach_ptr(ShmemRequest, request, pending_shmem_requests)
    {
        request->index_entry->initialized = true;
    }
 
    LWLockRelease(ShmemIndexLock);
}
 
/*
 * Call all shmem request callbacks.
 */
void
ShmemCallRequestCallbacks(void)
{
    ListCell   *lc;
 
    Assert(shmem_request_state == SRS_INITIAL);
    shmem_request_state = SRS_REQUESTING;
 
    foreach(lc, registered_shmem_callbacks)
    {
        const ShmemCallbacks *callbacks = (const ShmemCallbacks *) lfirst(lc);
 
        if (callbacks->request_fn)
            callbacks->request_fn(callbacks->opaque_arg);
    }
}
 
/*
 * ShmemInitStruct -- Create/attach to a structure in shared memory.
 *
 *      This is called during initialization to find or allocate
 *      a data structure in shared memory.  If no other process
 *      has created the structure, this routine allocates space
 *      for it.  If it exists already, a pointer to the existing
 *      structure is returned.
 *
 *  Returns: pointer to the object.  *foundPtr is set true if the object was
 *      already in the shmem index (hence, already initialized).
 *
 * Note: This is a legacy interface, kept for backwards compatibility with
 * extensions.  Use ShmemRequestStruct() in new code!
 */
void *
ShmemInitStruct(const char *name, Size size, bool *foundPtr)
{
    void       *ptr = NULL;
    ShmemStructOpts options = {
        .name = name,
        .size = size,
        .ptr = &ptr,
    };
    ShmemRequest request = {&options, SHMEM_KIND_STRUCT};
 
    Assert(shmem_request_state == SRS_DONE ||
           shmem_request_state == SRS_INITIALIZING ||
           shmem_request_state == SRS_REQUESTING);
 
    LWLockAcquire(ShmemIndexLock, LW_EXCLUSIVE);
 
    /* Look up the existing entry if any */
    *foundPtr = AttachShmemIndexEntry(&request, true);
 
    /* Initialize it if not found */
    if (!*foundPtr)
    {
        InitShmemIndexEntry(&request);
        /* no additional initialization needed */
        request.index_entry->initialized = true;
    }
 
    LWLockRelease(ShmemIndexLock);
 
    Assert(ptr != NULL);
    return ptr;
}
 
/* SQL SRF showing allocated shared memory */
Datum
pg_get_shmem_allocations(PG_FUNCTION_ARGS)
{
#define PG_GET_SHMEM_SIZES_COLS 4
    ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
    HASH_SEQ_STATUS hstat;
    ShmemIndexEnt *ent;
    Size        named_allocated = 0;
    Datum       values[PG_GET_SHMEM_SIZES_COLS];
    bool        nulls[PG_GET_SHMEM_SIZES_COLS];
 
    InitMaterializedSRF(fcinfo, 0);
 
    LWLockAcquire(ShmemIndexLock, LW_SHARED);
 
    hash_seq_init(&hstat, ShmemIndex);
 
    /* output all allocated entries */
    memset(nulls, 0, sizeof(nulls));
    while ((ent = (ShmemIndexEnt *) hash_seq_search(&hstat)) != NULL)
    {
        values[0] = CStringGetTextDatum(ent->key);
        values[1] = Int64GetDatum((char *) ent->location - (char *) ShmemSegHdr);
        values[2] = Int64GetDatum(ent->size);
        values[3] = Int64GetDatum(ent->allocated_size);
        named_allocated += ent->allocated_size;
 
        tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc,
                             values, nulls);
    }
 
    /* output shared memory allocated but not counted via the shmem index */
    values[0] = CStringGetTextDatum("<anonymous>");
    nulls[1] = true;
    values[2] = Int64GetDatum(ShmemAllocator->free_offset - named_allocated);
    values[3] = values[2];
    tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc, values, nulls);
 
    /* output as-of-yet unused shared memory */
    nulls[0] = true;
    values[1] = Int64GetDatum(ShmemAllocator->free_offset);
    nulls[1] = false;
    values[2] = Int64GetDatum(ShmemSegHdr->totalsize - ShmemAllocator->free_offset);
    values[3] = values[2];
    tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc, values, nulls);
 
    LWLockRelease(ShmemIndexLock);
 
    return (Datum) 0;
}
 
/*
 * SQL SRF showing NUMA memory nodes for allocated shared memory
 *
 * Compared to pg_get_shmem_allocations(), this function does not return
 * information about shared anonymous allocations and unused shared memory.
 */
Datum
pg_get_shmem_allocations_numa(PG_FUNCTION_ARGS)
{
#define PG_GET_SHMEM_NUMA_SIZES_COLS 3
    ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
    HASH_SEQ_STATUS hstat;
    ShmemIndexEnt *ent;
    Datum       values[PG_GET_SHMEM_NUMA_SIZES_COLS];
    bool        nulls[PG_GET_SHMEM_NUMA_SIZES_COLS];
    Size        os_page_size;
    void      **page_ptrs;
    int        *pages_status;
    uint64      shm_total_page_count,
                shm_ent_page_count,
                max_nodes;
    Size       *nodes;
 
    if (pg_numa_init() == -1)
        elog(ERROR, "libnuma initialization failed or NUMA is not supported on this platform");
 
    InitMaterializedSRF(fcinfo, 0);
 
    max_nodes = pg_numa_get_max_node();
    nodes = palloc_array(Size, max_nodes + 2);
 
    /*
     * Shared memory allocations can vary in size and may not align with OS
     * memory page boundaries, while NUMA queries work on pages.
     *
     * To correctly map each allocation to NUMA nodes, we need to: 1.
     * Determine the OS memory page size. 2. Align each allocation's start/end
     * addresses to page boundaries. 3. Query NUMA node information for all
     * pages spanning the allocation.
     */
    os_page_size = pg_get_shmem_pagesize();
 
    /*
     * Allocate memory for page pointers and status based on total shared
     * memory size. This simplified approach allocates enough space for all
     * pages in shared memory rather than calculating the exact requirements
     * for each segment.
     *
     * Add 1, because we don't know how exactly the segments align to OS
     * pages, so the allocation might use one more memory page. In practice
     * this is not very likely, and moreover we have more entries, each of
     * them using only fraction of the total pages.
     */
    shm_total_page_count = (ShmemSegHdr->totalsize / os_page_size) + 1;
    page_ptrs = palloc0_array(void *, shm_total_page_count);
    pages_status = palloc_array(int, shm_total_page_count);
 
    if (firstNumaTouch)
        elog(DEBUG1, "NUMA: page-faulting shared memory segments for proper NUMA readouts");
 
    LWLockAcquire(ShmemIndexLock, LW_SHARED);
 
    hash_seq_init(&hstat, ShmemIndex);
 
    /* output all allocated entries */
    while ((ent = (ShmemIndexEnt *) hash_seq_search(&hstat)) != NULL)
    {
        int         i;
        char       *startptr,
                   *endptr;
        Size        total_len;
 
        /*
         * Calculate the range of OS pages used by this segment. The segment
         * may start / end half-way through a page, we want to count these
         * pages too. So we align the start/end pointers down/up, and then
         * calculate the number of pages from that.
         */
        startptr = (char *) TYPEALIGN_DOWN(os_page_size, ent->location);
        endptr = (char *) TYPEALIGN(os_page_size,
                                    (char *) ent->location + ent->allocated_size);
        total_len = (endptr - startptr);
 
        shm_ent_page_count = total_len / os_page_size;
 
        /*
         * If we ever get 0xff (-1) back from kernel inquiry, then we probably
         * have a bug in mapping buffers to OS pages.
         */
        memset(pages_status, 0xff, sizeof(int) * shm_ent_page_count);
 
        /*
         * Setup page_ptrs[] with pointers to all OS pages for this segment,
         * and get the NUMA status using pg_numa_query_pages.
         *
         * In order to get reliable results we also need to touch memory
         * pages, so that inquiry about NUMA memory node doesn't return -2
         * (ENOENT, which indicates unmapped/unallocated pages).
         */
        for (i = 0; i < shm_ent_page_count; i++)
        {
            page_ptrs[i] = startptr + (i * os_page_size);
 
            if (firstNumaTouch)
                pg_numa_touch_mem_if_required(page_ptrs[i]);
 
            CHECK_FOR_INTERRUPTS();
        }
 
        if (pg_numa_query_pages(0, shm_ent_page_count, page_ptrs, pages_status) == -1)
            elog(ERROR, "failed NUMA pages inquiry status: %m");
 
        /* Count number of NUMA nodes used for this shared memory entry */
        memset(nodes, 0, sizeof(Size) * (max_nodes + 2));
 
        for (i = 0; i < shm_ent_page_count; i++)
        {
            int         s = pages_status[i];
 
            /* Ensure we are adding only valid index to the array */
            if (s >= 0 && s <= max_nodes)
            {
                /* valid NUMA node */
                nodes[s]++;
                continue;
            }
            else if (s == -2)
            {
                /* -2 means ENOENT (e.g. page was moved to swap) */
                nodes[max_nodes + 1]++;
                continue;
            }
 
            elog(ERROR, "invalid NUMA node id outside of allowed range "
                 "[0, " UINT64_FORMAT "]: %d", max_nodes, s);
        }
 
        /* no NULLs for regular nodes */
        memset(nulls, 0, sizeof(nulls));
 
        /*
         * Add one entry for each NUMA node, including those without allocated
         * memory for this segment.
         */
        for (i = 0; i <= max_nodes; i++)
        {
            values[0] = CStringGetTextDatum(ent->key);
            values[1] = Int32GetDatum(i);
            values[2] = Int64GetDatum(nodes[i] * os_page_size);
 
            tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc,
                                 values, nulls);
        }
 
        /* The last entry is used for pages without a NUMA node. */
        nulls[1] = true;
        values[0] = CStringGetTextDatum(ent->key);
        values[2] = Int64GetDatum(nodes[max_nodes + 1] * os_page_size);
 
        tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc,
                             values, nulls);
    }
 
    LWLockRelease(ShmemIndexLock);
    firstNumaTouch = false;
 
    return (Datum) 0;
}
 
/*
 * Determine the memory page size used for the shared memory segment.
 *
 * If the shared segment was allocated using huge pages, returns the size of
 * a huge page. Otherwise returns the size of regular memory page.
 *
 * This should be used only after shared memory has been initialized.
 */
Size
pg_get_shmem_pagesize(void)
{
    Size        os_page_size;
#ifdef WIN32
    SYSTEM_INFO sysinfo;
 
    GetSystemInfo(&sysinfo);
    os_page_size = sysinfo.dwPageSize;
#else
    os_page_size = sysconf(_SC_PAGESIZE);
#endif
 
    Assert(huge_pages_status != HUGE_PAGES_UNKNOWN);
 
    if (huge_pages_status == HUGE_PAGES_ON)
        GetHugePageSize(&os_page_size, NULL);
 
    return os_page_size;
}
 
Datum
pg_numa_available(PG_FUNCTION_ARGS)
{
    PG_RETURN_BOOL(pg_numa_init() != -1);
}