Skip to content

Commit f0d1127

Browse files
committed
Remove "parent" column from pg_backend_memory_contexts
32d3ed8 added the "path" column to pg_backend_memory_contexts to allow a stable method of obtaining the parent MemoryContext of a given row in the view. Using the "path" column is now the preferred method of obtaining the parent row. Previously, any queries which were self-joining to this view using the "name" and "parent" columns could get incorrect results due to the fact that names are not unique. Here we aim to explicitly break such queries so that they can be corrected and use the "path" column instead. It is possible that there are more innocent users of the parent column that just need an indication of the parent and having to write out a self-joining CTE may be an unnecessary hassle for those cases. Let's remove the column for now and see if anyone comes back with any complaints. This does seem like a good time to attempt to get rid of the column as we still have around 1 year to revert this if someone comes back with a valid complaint. Plus this view is new to v14 and is quite niche, so perhaps not many people will be affected. Author: Melih Mutlu <m.melihmutlu@gmail.com> Discussion: https://postgr.es/m/CAGPVpCT7NOe4fZXRL8XaoxHpSXYTu6GTpULT_3E-HT9hzjoFRA@mail.gmail.com
1 parent 3f44959 commit f0d1127

File tree

7 files changed

+36
-69
lines changed

7 files changed

+36
-69
lines changed

doc/src/sgml/system-views.sgml

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -481,15 +481,6 @@
481481
</para></entry>
482482
</row>
483483

484-
<row>
485-
<entry role="catalog_table_entry"><para role="column_definition">
486-
<structfield>parent</structfield> <type>text</type>
487-
</para>
488-
<para>
489-
Name of the parent of this memory context
490-
</para></entry>
491-
</row>
492-
493484
<row>
494485
<entry role="catalog_table_entry"><para role="column_definition">
495486
<structfield>type</structfield> <type>text</type>

src/backend/utils/adt/mcxtfuncs.c

Lines changed: 21 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -40,28 +40,6 @@ typedef struct MemoryContextId
4040
int context_id;
4141
} MemoryContextId;
4242

43-
/*
44-
* get_memory_context_name_and_ident
45-
* Populate *name and *ident from the name and ident from 'context'.
46-
*/
47-
static void
48-
get_memory_context_name_and_ident(MemoryContext context, const char **const name,
49-
const char **const ident)
50-
{
51-
*name = context->name;
52-
*ident = context->ident;
53-
54-
/*
55-
* To be consistent with logging output, we label dynahash contexts with
56-
* just the hash table name as with MemoryContextStatsPrint().
57-
*/
58-
if (*ident == NULL && strcmp(*name, "dynahash") == 0)
59-
{
60-
*name = *ident;
61-
*ident = NULL;
62-
}
63-
}
64-
6543
/*
6644
* int_list_to_array
6745
* Convert an IntList to an array of INT4OIDs.
@@ -93,7 +71,7 @@ PutMemoryContextsStatsTupleStore(Tuplestorestate *tupstore,
9371
TupleDesc tupdesc, MemoryContext context,
9472
HTAB *context_id_lookup)
9573
{
96-
#define PG_GET_BACKEND_MEMORY_CONTEXTS_COLS 11
74+
#define PG_GET_BACKEND_MEMORY_CONTEXTS_COLS 10
9775

9876
Datum values[PG_GET_BACKEND_MEMORY_CONTEXTS_COLS];
9977
bool nulls[PG_GET_BACKEND_MEMORY_CONTEXTS_COLS];
@@ -128,7 +106,18 @@ PutMemoryContextsStatsTupleStore(Tuplestorestate *tupstore,
128106
memset(values, 0, sizeof(values));
129107
memset(nulls, 0, sizeof(nulls));
130108

131-
get_memory_context_name_and_ident(context, &name, &ident);
109+
name = context->name;
110+
ident = context->ident;
111+
112+
/*
113+
* To be consistent with logging output, we label dynahash contexts with
114+
* just the hash table name as with MemoryContextStatsPrint().
115+
*/
116+
if (ident && strcmp(name, "dynahash") == 0)
117+
{
118+
name = ident;
119+
ident = NULL;
120+
}
132121

133122
if (name)
134123
values[0] = CStringGetTextDatum(name);
@@ -154,18 +143,6 @@ PutMemoryContextsStatsTupleStore(Tuplestorestate *tupstore,
154143
else
155144
nulls[1] = true;
156145

157-
if (context->parent)
158-
{
159-
const char *parent_name,
160-
*parent_ident;
161-
162-
get_memory_context_name_and_ident(context->parent, &parent_name,
163-
&parent_ident);
164-
values[2] = CStringGetTextDatum(parent_name);
165-
}
166-
else
167-
nulls[2] = true;
168-
169146
switch (context->type)
170147
{
171148
case T_AllocSetContext:
@@ -185,14 +162,14 @@ PutMemoryContextsStatsTupleStore(Tuplestorestate *tupstore,
185162
break;
186163
}
187164

188-
values[3] = CStringGetTextDatum(type);
189-
values[4] = Int32GetDatum(list_length(path)); /* level */
190-
values[5] = int_list_to_array(path);
191-
values[6] = Int64GetDatum(stat.totalspace);
192-
values[7] = Int64GetDatum(stat.nblocks);
193-
values[8] = Int64GetDatum(stat.freespace);
194-
values[9] = Int64GetDatum(stat.freechunks);
195-
values[10] = Int64GetDatum(stat.totalspace - stat.freespace);
165+
values[2] = CStringGetTextDatum(type);
166+
values[3] = Int32GetDatum(list_length(path)); /* level */
167+
values[4] = int_list_to_array(path);
168+
values[5] = Int64GetDatum(stat.totalspace);
169+
values[6] = Int64GetDatum(stat.nblocks);
170+
values[7] = Int64GetDatum(stat.freespace);
171+
values[8] = Int64GetDatum(stat.freechunks);
172+
values[9] = Int64GetDatum(stat.totalspace - stat.freespace);
196173

197174
tuplestore_putvalues(tupstore, tupdesc, values, nulls);
198175
list_free(path);

src/include/catalog/catversion.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,6 @@
5757
*/
5858

5959
/* yyyymmddN */
60-
#define CATALOG_VERSION_NO 202408021
60+
#define CATALOG_VERSION_NO 202408121
6161

6262
#endif

src/include/catalog/pg_proc.dat

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8329,9 +8329,9 @@
83298329
proname => 'pg_get_backend_memory_contexts', prorows => '100',
83308330
proretset => 't', provolatile => 'v', proparallel => 'r',
83318331
prorettype => 'record', proargtypes => '',
8332-
proallargtypes => '{text,text,text,text,int4,_int4,int8,int8,int8,int8,int8}',
8333-
proargmodes => '{o,o,o,o,o,o,o,o,o,o,o}',
8334-
proargnames => '{name, ident, parent, type, level, path, total_bytes, total_nblocks, free_bytes, free_chunks, used_bytes}',
8332+
proallargtypes => '{text,text,text,int4,_int4,int8,int8,int8,int8,int8}',
8333+
proargmodes => '{o,o,o,o,o,o,o,o,o,o}',
8334+
proargnames => '{name, ident, type, level, path, total_bytes, total_nblocks, free_bytes, free_chunks, used_bytes}',
83358335
prosrc => 'pg_get_backend_memory_contexts' },
83368336

83378337
# logging memory contexts of the specified backend

src/test/regress/expected/rules.out

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1305,7 +1305,6 @@ pg_available_extensions| SELECT e.name,
13051305
LEFT JOIN pg_extension x ON ((e.name = x.extname)));
13061306
pg_backend_memory_contexts| SELECT name,
13071307
ident,
1308-
parent,
13091308
type,
13101309
level,
13111310
path,
@@ -1314,7 +1313,7 @@ pg_backend_memory_contexts| SELECT name,
13141313
free_bytes,
13151314
free_chunks,
13161315
used_bytes
1317-
FROM pg_get_backend_memory_contexts() pg_get_backend_memory_contexts(name, ident, parent, type, level, path, total_bytes, total_nblocks, free_bytes, free_chunks, used_bytes);
1316+
FROM pg_get_backend_memory_contexts() pg_get_backend_memory_contexts(name, ident, type, level, path, total_bytes, total_nblocks, free_bytes, free_chunks, used_bytes);
13181317
pg_config| SELECT name,
13191318
setting
13201319
FROM pg_config() pg_config(name, setting);

src/test/regress/expected/sysviews.out

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ select count(*) >= 0 as ok from pg_available_extensions;
2121

2222
-- The entire output of pg_backend_memory_contexts is not stable,
2323
-- we test only the existence and basic condition of TopMemoryContext.
24-
select type, name, ident, parent, level, total_bytes >= free_bytes
24+
select type, name, ident, level, total_bytes >= free_bytes
2525
from pg_backend_memory_contexts where level = 1;
26-
type | name | ident | parent | level | ?column?
27-
----------+------------------+-------+--------+-------+----------
28-
AllocSet | TopMemoryContext | | | 1 | t
26+
type | name | ident | level | ?column?
27+
----------+------------------+-------+-------+----------
28+
AllocSet | TopMemoryContext | | 1 | t
2929
(1 row)
3030

3131
-- We can exercise some MemoryContext type stats functions. Most of the
@@ -43,11 +43,11 @@ fetch 1 from cur;
4343
bbbbbbbbbb | 2
4444
(1 row)
4545

46-
select type, name, parent, total_bytes > 0, total_nblocks, free_bytes > 0, free_chunks
46+
select type, name, total_bytes > 0, total_nblocks, free_bytes > 0, free_chunks
4747
from pg_backend_memory_contexts where name = 'Caller tuples';
48-
type | name | parent | ?column? | total_nblocks | ?column? | free_chunks
49-
------+---------------+----------------+----------+---------------+----------+-------------
50-
Bump | Caller tuples | TupleSort sort | t | 2 | t | 0
48+
type | name | ?column? | total_nblocks | ?column? | free_chunks
49+
------+---------------+----------+---------------+----------+-------------
50+
Bump | Caller tuples | t | 2 | t | 0
5151
(1 row)
5252

5353
rollback;

src/test/regress/sql/sysviews.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ select count(*) >= 0 as ok from pg_available_extensions;
1414

1515
-- The entire output of pg_backend_memory_contexts is not stable,
1616
-- we test only the existence and basic condition of TopMemoryContext.
17-
select type, name, ident, parent, level, total_bytes >= free_bytes
17+
select type, name, ident, level, total_bytes >= free_bytes
1818
from pg_backend_memory_contexts where level = 1;
1919

2020
-- We can exercise some MemoryContext type stats functions. Most of the
@@ -28,7 +28,7 @@ declare cur cursor for select left(a,10), b
2828
from (values(repeat('a', 512 * 1024),1),(repeat('b', 512),2)) v(a,b)
2929
order by v.a desc;
3030
fetch 1 from cur;
31-
select type, name, parent, total_bytes > 0, total_nblocks, free_bytes > 0, free_chunks
31+
select type, name, total_bytes > 0, total_nblocks, free_bytes > 0, free_chunks
3232
from pg_backend_memory_contexts where name = 'Caller tuples';
3333
rollback;
3434

0 commit comments

Comments
 (0)