|
40 | 40 |
|
41 | 41 | #include "postgres.h" |
42 | 42 |
|
| 43 | +#include "funcapi.h" |
43 | 44 | #include "lib/dshash.h" |
44 | 45 | #include "storage/dsm_registry.h" |
45 | 46 | #include "storage/lwlock.h" |
46 | 47 | #include "storage/shmem.h" |
| 48 | +#include "utils/builtins.h" |
47 | 49 | #include "utils/memutils.h" |
48 | 50 |
|
49 | 51 | #define DSMR_NAME_LEN 128 |
@@ -88,6 +90,13 @@ typedef enum DSMREntryType |
88 | 90 | DSMR_ENTRY_TYPE_DSH, |
89 | 91 | } DSMREntryType; |
90 | 92 |
|
| 93 | +static const char *const DSMREntryTypeNames[] = |
| 94 | +{ |
| 95 | + [DSMR_ENTRY_TYPE_DSM] = "segment", |
| 96 | + [DSMR_ENTRY_TYPE_DSA] = "area", |
| 97 | + [DSMR_ENTRY_TYPE_DSH] = "hash", |
| 98 | +}; |
| 99 | + |
91 | 100 | typedef struct DSMRegistryEntry |
92 | 101 | { |
93 | 102 | char name[DSMR_NAME_LEN]; |
@@ -435,3 +444,43 @@ GetNamedDSHash(const char *name, const dshash_parameters *params, bool *found) |
435 | 444 |
|
436 | 445 | return ret; |
437 | 446 | } |
| 447 | + |
| 448 | +Datum |
| 449 | +pg_get_dsm_registry_allocations(PG_FUNCTION_ARGS) |
| 450 | +{ |
| 451 | + ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo; |
| 452 | + DSMRegistryEntry *entry; |
| 453 | + MemoryContext oldcontext; |
| 454 | + dshash_seq_status status; |
| 455 | + |
| 456 | + InitMaterializedSRF(fcinfo, MAT_SRF_USE_EXPECTED_DESC); |
| 457 | + |
| 458 | + /* Be sure any local memory allocated by DSM/DSA routines is persistent. */ |
| 459 | + oldcontext = MemoryContextSwitchTo(TopMemoryContext); |
| 460 | + init_dsm_registry(); |
| 461 | + MemoryContextSwitchTo(oldcontext); |
| 462 | + |
| 463 | + dshash_seq_init(&status, dsm_registry_table, false); |
| 464 | + while ((entry = dshash_seq_next(&status)) != NULL) |
| 465 | + { |
| 466 | + Datum vals[3]; |
| 467 | + bool nulls[3] = {0}; |
| 468 | + |
| 469 | + vals[0] = CStringGetTextDatum(entry->name); |
| 470 | + vals[1] = CStringGetTextDatum(DSMREntryTypeNames[entry->type]); |
| 471 | + |
| 472 | + /* |
| 473 | + * Since we can't know the size of DSA/dshash entries without first |
| 474 | + * attaching to them, return NULL for those. |
| 475 | + */ |
| 476 | + if (entry->type == DSMR_ENTRY_TYPE_DSM) |
| 477 | + vals[2] = Int64GetDatum(entry->data.dsm.size); |
| 478 | + else |
| 479 | + nulls[2] = true; |
| 480 | + |
| 481 | + tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc, vals, nulls); |
| 482 | + } |
| 483 | + dshash_seq_term(&status); |
| 484 | + |
| 485 | + return (Datum) 0; |
| 486 | +} |
0 commit comments