Skip to content

Commit ca94ece

Browse files
author
Commitfest Bot
committed
[CF 6257] v3 - show size of DSAs and dshash tables in pg_dsm_registry_allocations
This branch was automatically generated by a robot using patches from an email thread registered at: https://commitfest.postgresql.org/patch/6257 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/aS4e9z74V-Vn7NEi@nathan Author(s): Nathan Bossart
2 parents 35988b3 + 9fb3e24 commit ca94ece

File tree

6 files changed

+53
-25
lines changed

6 files changed

+53
-25
lines changed

doc/src/sgml/system-views.sgml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1150,8 +1150,8 @@ AND c1.path[c2.level] = c2.path[c2.level];
11501150
<structfield>size</structfield> <type>int8</type>
11511151
</para>
11521152
<para>
1153-
Size of the allocation in bytes. NULL for entries of type
1154-
<literal>area</literal> and <literal>hash</literal>.
1153+
Size of the allocation in bytes. NULL for entries that failed
1154+
initialization.
11551155
</para></entry>
11561156
</row>
11571157
</tbody>

src/backend/storage/ipc/dsm_registry.c

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -463,26 +463,18 @@ pg_get_dsm_registry_allocations(PG_FUNCTION_ARGS)
463463
Datum vals[3];
464464
bool nulls[3] = {0};
465465

466-
/* Do not show partially-initialized entries. */
467-
if (entry->type == DSMR_ENTRY_TYPE_DSM &&
468-
entry->dsm.handle == DSM_HANDLE_INVALID)
469-
continue;
470-
if (entry->type == DSMR_ENTRY_TYPE_DSA &&
471-
entry->dsa.handle == DSA_HANDLE_INVALID)
472-
continue;
473-
if (entry->type == DSMR_ENTRY_TYPE_DSH &&
474-
entry->dsh.dsa_handle == DSA_HANDLE_INVALID)
475-
continue;
476-
477466
vals[0] = CStringGetTextDatum(entry->name);
478467
vals[1] = CStringGetTextDatum(DSMREntryTypeNames[entry->type]);
479468

480-
/*
481-
* Since we can't know the size of DSA/dshash entries without first
482-
* attaching to them, return NULL for those.
483-
*/
469+
/* Be careful to only return the sizes of initialized entries. */
484470
if (entry->type == DSMR_ENTRY_TYPE_DSM)
485471
vals[2] = Int64GetDatum(entry->dsm.size);
472+
else if (entry->type == DSMR_ENTRY_TYPE_DSA &&
473+
entry->dsa.handle != DSA_HANDLE_INVALID)
474+
vals[2] = Int64GetDatum(dsa_get_total_size_from_handle(entry->dsa.handle));
475+
else if (entry->type == DSMR_ENTRY_TYPE_DSH &&
476+
entry->dsh.dsa_handle !=DSA_HANDLE_INVALID)
477+
vals[2] = Int64GetDatum(dsa_get_total_size_from_handle(entry->dsh.dsa_handle));
486478
else
487479
nulls[2] = true;
488480

src/backend/utils/mmgr/dsa.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1050,6 +1050,41 @@ dsa_get_total_size(dsa_area *area)
10501050
return size;
10511051
}
10521052

1053+
/*
1054+
* Same as dsa_get_total_size(), but accepts a DSA handle. The area must have
1055+
* been created with dsa_create (not dsa_create_in_place).
1056+
*/
1057+
size_t
1058+
dsa_get_total_size_from_handle(dsa_handle handle)
1059+
{
1060+
size_t size;
1061+
bool already_attached;
1062+
dsm_segment *segment;
1063+
dsa_area_control *control;
1064+
1065+
already_attached = dsa_is_attached(handle);
1066+
if (already_attached)
1067+
segment = dsm_find_mapping(handle);
1068+
else
1069+
segment = dsm_attach(handle);
1070+
1071+
if (segment == NULL)
1072+
ereport(ERROR,
1073+
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1074+
errmsg("could not attach to dynamic shared area")));
1075+
1076+
control = (dsa_area_control *) dsm_segment_address(segment);
1077+
1078+
LWLockAcquire(&control->lock, LW_SHARED);
1079+
size = control->total_segment_size;
1080+
LWLockRelease(&control->lock);
1081+
1082+
if (!already_attached)
1083+
dsm_detach(segment);
1084+
1085+
return size;
1086+
}
1087+
10531088
/*
10541089
* Aggressively free all spare memory in the hope of returning DSM segments to
10551090
* the operating system.

src/include/utils/dsa.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ extern dsa_pointer dsa_allocate_extended(dsa_area *area, size_t size, int flags)
161161
extern void dsa_free(dsa_area *area, dsa_pointer dp);
162162
extern void *dsa_get_address(dsa_area *area, dsa_pointer dp);
163163
extern size_t dsa_get_total_size(dsa_area *area);
164+
extern size_t dsa_get_total_size_from_handle(dsa_handle handle);
164165
extern void dsa_trim(dsa_area *area);
165166
extern void dsa_dump(dsa_area *area);
166167

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
SELECT name, type, size IS DISTINCT FROM 0 AS size
1+
SELECT name, type, size > 0 AS size_ok
22
FROM pg_dsm_registry_allocations
33
WHERE name like 'test_dsm_registry%' ORDER BY name;
4-
name | type | size
5-
------+------+------
4+
name | type | size_ok
5+
------+------+---------
66
(0 rows)
77

88
CREATE EXTENSION test_dsm_registry;
@@ -32,11 +32,11 @@ SELECT get_val_in_hash('test');
3232
(1 row)
3333

3434
\c
35-
SELECT name, type, size IS DISTINCT FROM 0 AS size
35+
SELECT name, type, size > 0 AS size_ok
3636
FROM pg_dsm_registry_allocations
3737
WHERE name like 'test_dsm_registry%' ORDER BY name;
38-
name | type | size
39-
------------------------+---------+------
38+
name | type | size_ok
39+
------------------------+---------+---------
4040
test_dsm_registry_dsa | area | t
4141
test_dsm_registry_dsm | segment | t
4242
test_dsm_registry_hash | hash | t
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
SELECT name, type, size IS DISTINCT FROM 0 AS size
1+
SELECT name, type, size > 0 AS size_ok
22
FROM pg_dsm_registry_allocations
33
WHERE name like 'test_dsm_registry%' ORDER BY name;
44
CREATE EXTENSION test_dsm_registry;
@@ -8,6 +8,6 @@ SELECT set_val_in_hash('test', '1414');
88
SELECT get_val_in_shmem();
99
SELECT get_val_in_hash('test');
1010
\c
11-
SELECT name, type, size IS DISTINCT FROM 0 AS size
11+
SELECT name, type, size > 0 AS size_ok
1212
FROM pg_dsm_registry_allocations
1313
WHERE name like 'test_dsm_registry%' ORDER BY name;

0 commit comments

Comments
 (0)