Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 4e784f3

Browse files
committed
Fix infelicities in describeOneTableDetails' partitioned-table handling.
describeOneTableDetails issued a partition-constraint-fetching query for every table, even ones it knows perfectly well are not partitions. To add insult to injury, it then proceeded to leak the empty PGresult if the table wasn't a partition. Doing that a lot of times might amount to a meaningful leak, so this seems like a back-patchable bug. Fix that, and also fix a related PGresult leak in the partition-parent case (though that leak would occur only if we got no row, which is unexpected). Minor code beautification too, to make this code look more like the pre-existing code around it. Back-patch the whole change into v12. However, the fact that we already know whether the table is a partition dates only to commit 1af25ca; back-patching the relevant changes from that is probably more churn than is justified in released branches. Hence, in v11 and v10, just do the minimum to fix the PGresult leaks. Noted while messing around with adjacent code for yesterday's \d improvements.
1 parent 6655a72 commit 4e784f3

File tree

1 file changed

+22
-21
lines changed

1 file changed

+22
-21
lines changed

src/bin/psql/describe.c

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2102,44 +2102,43 @@ describeOneTableDetails(const char *schemaname,
21022102
}
21032103

21042104
/* Make footers */
2105-
if (pset.sversion >= 100000)
2105+
2106+
if (tableinfo.ispartition)
21062107
{
2107-
/* Get the partition information */
2108+
/* Footer information for a partition child table */
21082109
PGresult *result;
2109-
char *parent_name;
2110-
char *partdef;
2111-
char *partconstraintdef = NULL;
21122110

21132111
printfPQExpBuffer(&buf,
21142112
"SELECT inhparent::pg_catalog.regclass,\n"
2115-
" pg_catalog.pg_get_expr(c.relpartbound, inhrelid)");
2113+
" pg_catalog.pg_get_expr(c.relpartbound, c.oid)");
21162114
/* If verbose, also request the partition constraint definition */
21172115
if (verbose)
21182116
appendPQExpBufferStr(&buf,
2119-
",\n pg_catalog.pg_get_partition_constraintdef(inhrelid)");
2117+
",\n pg_catalog.pg_get_partition_constraintdef(c.oid)");
21202118
appendPQExpBuffer(&buf,
21212119
"\nFROM pg_catalog.pg_class c"
21222120
" JOIN pg_catalog.pg_inherits i"
21232121
" ON c.oid = inhrelid"
2124-
"\nWHERE c.oid = '%s' AND c.relispartition;", oid);
2122+
"\nWHERE c.oid = '%s';", oid);
21252123
result = PSQLexec(buf.data);
21262124
if (!result)
21272125
goto error_return;
21282126

21292127
if (PQntuples(result) > 0)
21302128
{
2131-
parent_name = PQgetvalue(result, 0, 0);
2132-
partdef = PQgetvalue(result, 0, 1);
2133-
2134-
if (PQnfields(result) == 3 && !PQgetisnull(result, 0, 2))
2135-
partconstraintdef = PQgetvalue(result, 0, 2);
2129+
char *parent_name = PQgetvalue(result, 0, 0);
2130+
char *partdef = PQgetvalue(result, 0, 1);
21362131

21372132
printfPQExpBuffer(&tmpbuf, _("Partition of: %s %s"), parent_name,
21382133
partdef);
21392134
printTableAddFooter(&cont, tmpbuf.data);
21402135

21412136
if (verbose)
21422137
{
2138+
char *partconstraintdef = NULL;
2139+
2140+
if (!PQgetisnull(result, 0, 2))
2141+
partconstraintdef = PQgetvalue(result, 0, 2);
21432142
/* If there isn't any constraint, show that explicitly */
21442143
if (partconstraintdef == NULL || partconstraintdef[0] == '\0')
21452144
printfPQExpBuffer(&tmpbuf, _("No partition constraint"));
@@ -2148,27 +2147,29 @@ describeOneTableDetails(const char *schemaname,
21482147
partconstraintdef);
21492148
printTableAddFooter(&cont, tmpbuf.data);
21502149
}
2151-
2152-
PQclear(result);
21532150
}
2151+
PQclear(result);
21542152
}
21552153

21562154
if (tableinfo.relkind == RELKIND_PARTITIONED_TABLE)
21572155
{
2158-
/* Get the partition key information */
2156+
/* Footer information for a partitioned table (partitioning parent) */
21592157
PGresult *result;
2160-
char *partkeydef;
21612158

21622159
printfPQExpBuffer(&buf,
21632160
"SELECT pg_catalog.pg_get_partkeydef('%s'::pg_catalog.oid);",
21642161
oid);
21652162
result = PSQLexec(buf.data);
2166-
if (!result || PQntuples(result) != 1)
2163+
if (!result)
21672164
goto error_return;
21682165

2169-
partkeydef = PQgetvalue(result, 0, 0);
2170-
printfPQExpBuffer(&tmpbuf, _("Partition key: %s"), partkeydef);
2171-
printTableAddFooter(&cont, tmpbuf.data);
2166+
if (PQntuples(result) == 1)
2167+
{
2168+
char *partkeydef = PQgetvalue(result, 0, 0);
2169+
2170+
printfPQExpBuffer(&tmpbuf, _("Partition key: %s"), partkeydef);
2171+
printTableAddFooter(&cont, tmpbuf.data);
2172+
}
21722173
PQclear(result);
21732174
}
21742175

0 commit comments

Comments
 (0)