Re: BUG #16743: psql doesn't show whole expression in stored column - Mailing list pgsql-bugs

From Tom Lane
Subject Re: BUG #16743: psql doesn't show whole expression in stored column
Date
Msg-id 464990.1606320939@sss.pgh.pa.us
Whole thread Raw
In response to Re: BUG #16743: psql doesn't show whole expression in stored column  (Tom Lane <tgl@sss.pgh.pa.us>)
Responses Re: BUG #16743: psql doesn't show whole expression in stored column
List pgsql-bugs
I wrote:
> Peter Eisentraut <peter.eisentraut@enterprisedb.com> writes:
>> I think we should get rid of the truncating.  Otherwise, there is no way 
>> to actually get the full information, is there?  (Other than pg_dump or 
>> manual catalog queries.)

> That'd be okay with me.  It's always seemed a little odd that we do that
> for attrdefs but not anything else.

Here's a proposed patch for that.

After looking a bit closer, I saw that the memory leak I was worried
about before is not real, because the code passes mustfree = true to
printTableAddCell.  However, I still don't like it one bit, because you
need some undocumented and fragile assumptions about the relationship
between attidentity and attgenerated to conclude that we won't instead
have a false free() attempt on something that mustn't be free'd.  So I
think we should adjust the code to track mustfree explicitly, as done
below.

Should we back-patch this?  I think that the truncation behavior became
significantly more of a problem with the addition of the GENERATED
feature; before that it was clearer what was going on.  So I'm mildly
inclined to back-patch to v12 where that came in.

            regards, tom lane

diff --git a/src/bin/psql/describe.c b/src/bin/psql/describe.c
index 07d640021c..14150d05a9 100644
--- a/src/bin/psql/describe.c
+++ b/src/bin/psql/describe.c
@@ -1842,7 +1842,7 @@ describeOneTableDetails(const char *schemaname,
     {
         /* use "pretty" mode for expression to avoid excessive parentheses */
         appendPQExpBufferStr(&buf,
-                             ",\n  (SELECT substring(pg_catalog.pg_get_expr(d.adbin, d.adrelid, true) for 128)"
+                             ",\n  (SELECT pg_catalog.pg_get_expr(d.adbin, d.adrelid, true)"
                              "\n   FROM pg_catalog.pg_attrdef d"
                              "\n   WHERE d.adrelid = a.attrelid AND d.adnum = a.attnum AND a.atthasdef)"
                              ",\n  a.attnotnull");
@@ -2045,7 +2045,8 @@ describeOneTableDetails(const char *schemaname,
         {
             char       *identity;
             char       *generated;
-            char       *default_str = "";
+            char       *default_str;
+            bool        mustfree = false;

             printTableAddCell(&cont, PQgetvalue(res, i, attcoll_col), false, false);

@@ -2061,12 +2062,15 @@ describeOneTableDetails(const char *schemaname,
             else if (identity[0] == ATTRIBUTE_IDENTITY_BY_DEFAULT)
                 default_str = "generated by default as identity";
             else if (generated[0] == ATTRIBUTE_GENERATED_STORED)
-                default_str = psprintf("generated always as (%s) stored", PQgetvalue(res, i, attrdef_col));
+            {
+                default_str = psprintf("generated always as (%s) stored",
+                                       PQgetvalue(res, i, attrdef_col));
+                mustfree = true;
+            }
             else
-                /* (note: above we cut off the 'default' string at 128) */
                 default_str = PQgetvalue(res, i, attrdef_col);

-            printTableAddCell(&cont, default_str, false, generated[0] ? true : false);
+            printTableAddCell(&cont, default_str, false, mustfree);
         }

         /* Info for index columns */

pgsql-bugs by date:

Previous
From: Tom Lane
Date:
Subject: Re: BUG #16743: psql doesn't show whole expression in stored column
Next
From: Bruce Momjian
Date:
Subject: Re: BUG #16743: psql doesn't show whole expression in stored column