Thread: query output in psql PROMPT

query output in psql PROMPT

From
strk
Date:
A simple patch allow query output in psql PROMPT strings:

Index: prompt.c
===================================================================
RCS file: /projects/cvsroot/pgsql/src/bin/psql/prompt.c,v
retrieving revision 1.38
diff -U2 -r1.38 prompt.c
--- prompt.c    1 Jan 2005 05:43:08 -0000    1.38
+++ prompt.c    14 Jan 2005 14:39:24 -0000
@@ -61,4 +61,7 @@
  * %[ ... %]       - tell readline that the contained text is invisible
  *
+ * %.query.    - the value of the first field in the first row returned
+ *          by the specified query.
+ *
  * If the application-wide prompts become NULL somehow, the returned string
  * will be empty (not NULL!).
@@ -272,4 +275,29 @@
                     }

+                    /* execute db proc */
+                case '.':
+                    {
+                        char *cmd = pg_strdup(p + 1);
+                        PGresult *res;
+                        int cmdend;
+
+                        cmdend = strcspn(cmd, ".");
+                        cmd[cmdend] = '\0';
+                        if (cmd)
+                        {
+                            res = PSQLexec(cmd, false);
+                        }
+                        if (res && PQntuples(res) )
+                        {
+                            memcpy(buf, PQgetvalue(res, 0, 0), MAX_PROMPT_SIZE-1);
+                            PQclear(res);
+                        }
+                        if (strlen(buf) > 0 && buf[strlen(buf) - 1] == '\n')
+                            buf[strlen(buf) - 1] = '\0';
+                        free(cmd);
+                        p += cmdend + 1;
+                        break;
+                    }
+
                     /* interpolate variable */
                 case ':':

--

For standing up against patentability of software,

  Thank You, Poland!

Read the intervention:    http://kwiki.ffii.org/ConsPolon041221En
Send your thanks:         thankyoupoland.info
Read/do more:          http://www.noepatents.org/

Re: query output in psql PROMPT

From
Tom Lane
Date:
strk <strk@keybit.net> writes:
> A simple patch allow query output in psql PROMPT strings:

Why is this a good idea?  Having a query implicitly executed during
every prompt will have a ton of bad side effects, for instance
prematurely freezing the query snapshot in SERIALIZABLE transactions.

The syntax you propose is downright bizarre --- what if I needed a dot
in the query text?

> +                        cmdend = strcspn(cmd, ".");
> +                        cmd[cmdend] = '\0';
> +                        if (cmd)
> +                        {
> +                            res = PSQLexec(cmd, false);
> +                        }

What's the if for?  cmd can't be NULL (you already dumped core if it
is).

> +                        if (res && PQntuples(res) )
> +                        {
> +                            memcpy(buf, PQgetvalue(res, 0, 0), MAX_PROMPT_SIZE-1);
> +                            PQclear(res);
> +                        }

Leaks memory on query failure.

> +                        if (strlen(buf) > 0 && buf[strlen(buf) - 1] == '\n')
> +                            buf[strlen(buf) - 1] = '\0';

Uses undefined contents of buf on query failure.

> +                        p += cmdend + 1;

Falls off the end of the prompt if there was no terminating dot, causing
subsequent iterations of the loop to continue reading undefined memory.

            regards, tom lane

Re: query output in psql PROMPT

From
strk
Date:
On Fri, Jan 14, 2005 at 12:06:32PM -0500, Tom Lane wrote:
> strk <strk@keybit.net> writes:
> > A simple patch allow query output in psql PROMPT strings:
>
> Why is this a good idea?  Having a query implicitly executed during
> every prompt will have a ton of bad side effects, for instance
> prematurely freezing the query snapshot in SERIALIZABLE transactions.

The only purpose for having made the patch is having current_schema()
printed on the prompt (for the first question).

I know the code is very dirty, is there a chance to current_schema()
as a variable if that won't change transaction status ?

--strk;

>
> The syntax you propose is downright bizarre --- what if I needed a dot
> in the query text?
>
> > +                        cmdend = strcspn(cmd, ".");
> > +                        cmd[cmdend] = '\0';
> > +                        if (cmd)
> > +                        {
> > +                            res = PSQLexec(cmd, false);
> > +                        }
>
> What's the if for?  cmd can't be NULL (you already dumped core if it
> is).
>
> > +                        if (res && PQntuples(res) )
> > +                        {
> > +                            memcpy(buf, PQgetvalue(res, 0, 0), MAX_PROMPT_SIZE-1);
> > +                            PQclear(res);
> > +                        }
>
> Leaks memory on query failure.
>
> > +                        if (strlen(buf) > 0 && buf[strlen(buf) - 1] == '\n')
> > +                            buf[strlen(buf) - 1] = '\0';
>
> Uses undefined contents of buf on query failure.
>
> > +                        p += cmdend + 1;
>
> Falls off the end of the prompt if there was no terminating dot, causing
> subsequent iterations of the loop to continue reading undefined memory.
>
>             regards, tom lane

--

For standing up against patentability of software,

  Thank You, Poland!

Read the intervention:    http://kwiki.ffii.org/ConsPolon041221En
Send your thanks:         thankyoupoland.info
Read/do more:          http://www.noepatents.org/

Re: query output in psql PROMPT

From
strk
Date:
Mm... let alone my patch but the 'Uses undefined contents of buf'
is not true (buf is initialized to contain all 0es:

    111:memset(buf, 0, MAX_PROMPT_SIZE + 1);

And the 'Falls off the end of prompt string' is common to
all non-singlechar handlings (execute command, interpolate variable).

.. the remaining part can be cleaned up but I dunno about transaction
state problems ..

--strk;


On Fri, Jan 14, 2005 at 12:06:32PM -0500, Tom Lane wrote:
> strk <strk@keybit.net> writes:
> > A simple patch allow query output in psql PROMPT strings:
>
> Why is this a good idea?  Having a query implicitly executed during
> every prompt will have a ton of bad side effects, for instance
> prematurely freezing the query snapshot in SERIALIZABLE transactions.
>
> The syntax you propose is downright bizarre --- what if I needed a dot
> in the query text?
>
> > +                        cmdend = strcspn(cmd, ".");
> > +                        cmd[cmdend] = '\0';
> > +                        if (cmd)
> > +                        {
> > +                            res = PSQLexec(cmd, false);
> > +                        }
>
> What's the if for?  cmd can't be NULL (you already dumped core if it
> is).
>
> > +                        if (res && PQntuples(res) )
> > +                        {
> > +                            memcpy(buf, PQgetvalue(res, 0, 0), MAX_PROMPT_SIZE-1);
> > +                            PQclear(res);
> > +                        }
>
> Leaks memory on query failure.
>
> > +                        if (strlen(buf) > 0 && buf[strlen(buf) - 1] == '\n')
> > +                            buf[strlen(buf) - 1] = '\0';
>
> Uses undefined contents of buf on query failure.
>
> > +                        p += cmdend + 1;
>
> Falls off the end of the prompt if there was no terminating dot, causing
> subsequent iterations of the loop to continue reading undefined memory.
>
>             regards, tom lane

--

For standing up against patentability of software,

  Thank You, Poland!

Read the intervention:    http://kwiki.ffii.org/ConsPolon041221En
Send your thanks:         thankyoupoland.info
Read/do more:          http://www.noepatents.org/