]> granicus.if.org Git - postgresql/commitdiff
Fix infelicities in describeOneTableDetails' partitioned-table handling.
authorTom Lane <tgl@sss.pgh.pa.us>
Wed, 24 Jul 2019 22:14:26 +0000 (18:14 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Wed, 24 Jul 2019 22:14:46 +0000 (18:14 -0400)
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 1af25ca0c;
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.

src/bin/psql/describe.c

index 43285676857c3a7e27f3f9db64fa4abeafdd424b..eb547103dea3b0569699c5665612cd2bfe64d4d1 100644 (file)
@@ -2083,9 +2083,8 @@ describeOneTableDetails(const char *schemaname,
                                                                          partconstraintdef);
                                printTableAddFooter(&cont, tmpbuf.data);
                        }
-
-                       PQclear(result);
                }
+               PQclear(result);
        }
 
        if (tableinfo.relkind == RELKIND_PARTITIONED_TABLE)
@@ -2098,12 +2097,15 @@ describeOneTableDetails(const char *schemaname,
                                                  "SELECT pg_catalog.pg_get_partkeydef('%s'::pg_catalog.oid);",
                                                  oid);
                result = PSQLexec(buf.data);
-               if (!result || PQntuples(result) != 1)
+               if (!result)
                        goto error_return;
 
-               partkeydef = PQgetvalue(result, 0, 0);
-               printfPQExpBuffer(&tmpbuf, _("Partition key: %s"), partkeydef);
-               printTableAddFooter(&cont, tmpbuf.data);
+               if (PQntuples(result) == 1)
+               {
+                       partkeydef = PQgetvalue(result, 0, 0);
+                       printfPQExpBuffer(&tmpbuf, _("Partition key: %s"), partkeydef);
+                       printTableAddFooter(&cont, tmpbuf.data);
+               }
                PQclear(result);
        }