Skip to content

Commit a633666

Browse files
author
Commitfest Bot
committed
[CF 5652] v3 - pg_dsm_registry system view
This branch was automatically generated by a robot using patches from an email thread registered at: https://commitfest.postgresql.org/patch/5652 The branch will be overwritten each time a new patch version is posted to the thread, and also periodically to check for bitrot caused by changes on the master branch. Patch(es): https://www.postgresql.org/message-id/CA+v5N41vf8KhKBTfjjmEjYUaLd9KVtePOm=kfJjF4TtDfytPRQ@mail.gmail.com Author(s): Florents Tselai
2 parents c55df7c + bb4a857 commit a633666

File tree

6 files changed

+110
-0
lines changed

6 files changed

+110
-0
lines changed

src/backend/catalog/system_views.sql

+8
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,14 @@ GRANT SELECT ON pg_shmem_allocations_numa TO pg_read_all_stats;
666666
REVOKE EXECUTE ON FUNCTION pg_get_shmem_allocations_numa() FROM PUBLIC;
667667
GRANT EXECUTE ON FUNCTION pg_get_shmem_allocations_numa() TO pg_read_all_stats;
668668

669+
CREATE VIEW pg_dsm_registry AS
670+
SELECT * FROM pg_get_dsm_registry();
671+
672+
REVOKE ALL ON pg_dsm_registry FROM PUBLIC;
673+
GRANT SELECT ON pg_dsm_registry TO pg_read_all_stats;
674+
REVOKE EXECUTE ON FUNCTION pg_get_dsm_registry() FROM PUBLIC;
675+
GRANT EXECUTE ON FUNCTION pg_get_dsm_registry() TO pg_read_all_stats;
676+
669677
CREATE VIEW pg_backend_memory_contexts AS
670678
SELECT * FROM pg_get_backend_memory_contexts();
671679

src/backend/storage/ipc/dsm_registry.c

+83
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@
3131
#include "storage/lwlock.h"
3232
#include "storage/shmem.h"
3333
#include "utils/memutils.h"
34+
#include "fmgr.h"
35+
#include "funcapi.h"
36+
#include "miscadmin.h"
37+
#include "utils/builtins.h"
3438

3539
typedef struct DSMRegistryCtxStruct
3640
{
@@ -198,3 +202,82 @@ GetNamedDSMSegment(const char *name, size_t size,
198202

199203
return ret;
200204
}
205+
206+
void
207+
iterate_dsm_registry(void (*callback)(DSMRegistryEntry *, void *), void *arg);
208+
void
209+
iterate_dsm_registry(void (*callback)(DSMRegistryEntry *, void *), void *arg)
210+
{
211+
DSMRegistryEntry *entry;
212+
dshash_seq_status status;
213+
/* Ensure DSM registry is initialized */
214+
init_dsm_registry();
215+
216+
/* Use non-exclusive access to avoid blocking other backends */
217+
dshash_seq_init(&status, dsm_registry_table, false);
218+
while ((entry = dshash_seq_next(&status)) != NULL)
219+
callback(entry, arg);
220+
dshash_seq_term(&status);
221+
}
222+
223+
/* SQL SRF showing DSM registry allocated memory */
224+
PG_FUNCTION_INFO_V1(pg_get_dsm_registry);
225+
226+
typedef struct
227+
{
228+
Tuplestorestate *tupstore;
229+
TupleDesc tupdesc;
230+
} DSMRegistrySRFContext;
231+
232+
static void
233+
collect_dsm_registry(DSMRegistryEntry *entry, void *arg)
234+
{
235+
DSMRegistrySRFContext *ctx = (DSMRegistrySRFContext *) arg;
236+
Datum values[2];
237+
bool nulls[2] = {false, false};
238+
239+
values[0] = CStringGetTextDatum(entry->name);
240+
values[1] = Int64GetDatum(entry->size);
241+
242+
tuplestore_putvalues(ctx->tupstore, ctx->tupdesc, values, nulls);
243+
}
244+
245+
Datum
246+
pg_get_dsm_registry(PG_FUNCTION_ARGS)
247+
{
248+
ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
249+
MemoryContext per_query_ctx;
250+
MemoryContext oldcontext;
251+
TupleDesc tupdesc;
252+
Tuplestorestate *tupstore;
253+
DSMRegistrySRFContext ctx;
254+
255+
if (rsinfo == NULL || !IsA(rsinfo, ReturnSetInfo))
256+
ereport(ERROR, (errmsg("pg_get_dsm_registry must be used in a SRF context")));
257+
258+
/* Set up tuple descriptor */
259+
tupdesc = CreateTemplateTupleDesc(2);
260+
TupleDescInitEntry(tupdesc, (AttrNumber) 1, "name", TEXTOID, -1, 0);
261+
TupleDescInitEntry(tupdesc, (AttrNumber) 2, "size", INT8OID, -1, 0);
262+
263+
/* Switch to per-query memory context */
264+
per_query_ctx = rsinfo->econtext->ecxt_per_query_memory;
265+
oldcontext = MemoryContextSwitchTo(per_query_ctx);
266+
267+
/* Initialize tuplestore */
268+
tupstore = tuplestore_begin_heap(false, false, work_mem);
269+
270+
ctx.tupstore = tupstore;
271+
ctx.tupdesc = tupdesc;
272+
273+
/* Collect registry data */
274+
iterate_dsm_registry(collect_dsm_registry, &ctx);
275+
276+
/* Switch back and return results */
277+
MemoryContextSwitchTo(oldcontext);
278+
rsinfo->returnMode = SFRM_Materialize;
279+
rsinfo->setResult = tupstore;
280+
rsinfo->setDesc = tupdesc;
281+
282+
return (Datum) 0;
283+
}

src/include/catalog/pg_proc.dat

+6
Original file line numberDiff line numberDiff line change
@@ -8541,6 +8541,12 @@
85418541
proallargtypes => '{text,int8,int8,int8}', proargmodes => '{o,o,o,o}',
85428542
proargnames => '{name,off,size,allocated_size}',
85438543
prosrc => 'pg_get_shmem_allocations' },
8544+
{ oid => '6062', descr => 'dsm registry allocations',
8545+
proname => 'pg_get_dsm_registry', prorows => '50', proretset => 't',
8546+
provolatile => 'v', prorettype => 'record', proargtypes => '',
8547+
proallargtypes => '{text,int8}', proargmodes => '{o,o}',
8548+
proargnames => '{name,size}',
8549+
prosrc => 'pg_get_dsm_registry' },
85448550

85458551
{ oid => '4099', descr => 'Is NUMA support available?',
85468552
proname => 'pg_numa_available', provolatile => 's', prorettype => 'bool',

src/test/modules/test_dsm_registry/expected/test_dsm_registry.out

+7
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,10 @@ SELECT get_val_in_shmem();
1212
1236
1313
(1 row)
1414

15+
-- 20 bytes = int (4 bytes) + LWLock (16bytes)
16+
SELECT * FROM pg_dsm_registry;
17+
name | size
18+
-------------------+------
19+
test_dsm_registry | 20
20+
(1 row)
21+

src/test/modules/test_dsm_registry/sql/test_dsm_registry.sql

+3
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,6 @@ CREATE EXTENSION test_dsm_registry;
22
SELECT set_val_in_shmem(1236);
33
\c
44
SELECT get_val_in_shmem();
5+
6+
-- 20 bytes = int (4 bytes) + LWLock (16bytes)
7+
SELECT * FROM pg_dsm_registry;

src/test/regress/expected/rules.out

+3
Original file line numberDiff line numberDiff line change
@@ -1340,6 +1340,9 @@ pg_cursors| SELECT name,
13401340
is_scrollable,
13411341
creation_time
13421342
FROM pg_cursor() c(name, statement, is_holdable, is_binary, is_scrollable, creation_time);
1343+
pg_dsm_registry| SELECT name,
1344+
size
1345+
FROM pg_get_dsm_registry() pg_get_dsm_registry(name, size);
13431346
pg_file_settings| SELECT sourcefile,
13441347
sourceline,
13451348
seqno,

0 commit comments

Comments
 (0)