Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I do the same, but I have one thing I miss - an easy way to send the output out for graphs.

Tweaking and playing with gnuplot is a loss of time - if on a copy/paste excel and others can understand the data from the label and plot using reasonable defaults without many hints, certainly if columns are identified as datetime, labels etc. there could be a tool to use such hints and make a decent graph (to me, decent means giving a global understanding - sure you can tweak it to look good if you are preparing a report, but a lot of time is spent graphing thinks to figure things out and many graphs go to the trash in the process)

My dream is to do my select queries in psql and direct the output to that tool, never leaving psql - so it could be for example something that would be triggered on a new table creation matching a specific name like xx_, then it would simply require prefixing "select" by "create table xx_abc as ".

The best way I've found is to save the output to a CSV and pass it to other tools, but there are never quite user friendly and usually can't pick reasonable defaults.

There is an OSX psql frontend I tried after it was recommended here on HN (http://inductionapp.com/) but it was not that helpful in day to day operations.

Yet it seemed to be on the same problem - see this picture https://s3.amazonaws.com/induction/induction-visualize.png



Here is an example of feeding query output into gnuplot without leaving psql:

  # psql -U postgres
  psql (8.4.15)
  Type "help" for help.

  postgres=# \t
  Showing only tuples.
  postgres=# \a
  Output format is unaligned.
  postgres=# \f ' '
  Field separator is " ".
  postgres=# select * from example;
  1 1
  2 2
  3 3
  4 4
  postgres=# \o | /usr/bin/gnuplot
  postgres=# select 'set title "My Graph"; set terminal dumb 78 24; set key off; set ylabel "Time"; set xlabel "Servers";' || 'plot ''-'' with lines;' ; select * from example;
  postgres=# \o

                                    My Graph
  Time
      4 ++----------+----------+-----------+----------+-----------+---------**
        +           +          +           +          +            +     **** +
      |                                                           ****     |
  3.5 ++                                                      ****        ++
      |                                                   ****             |
      |                                               ****                 |
    3 ++                                           ***                    ++
      |                                        ****                        |
      |                                    ****                            |
  2.5 ++                               ****                               ++
      |                            ****                                    |
      |                        ****                                        |
    2 ++                    ***                                           ++
      |                 ****                                               |
      |             ****                                                   |
  1.5 ++        ****                                                      ++
      |     ****                                                           |
      + ****      +          +           +          +           +          +
    1 **----------+----------+-----------+----------+-----------+---------++
      1          1.5         2          2.5         3          3.5         4
                                     Servers

  postgres=#


This is incredible! I only wish it were a little easier to do on the fly.


Yeah, gnuplot is a very powerful tool.

I imagine you could put it all into a user-defined postgresql function, so all you have to say is:

   graph(select * from example);


You should be able to map it to a macro using \set.

    # \set foo '(select now())'
    # :foo;
    ?column?
    --------
    2013-02-14 04:18:23+01

    # select count(*) from :foo as foo;
    count
    -----
    
Afaik macros just expand inline, do you can embed stuff like \o.


Aye. This is getting over my head, so I inquired on the pgsql-general list (which is amazingly helpful).

Ian Barwick writes how to put all the prep stuff into a psql script, then all you have to do is define your query and invoke the script:

-- start quote --

What you could do is create a small psql script along these lines:

  barwick@localhost:~$ cat tmp/plot.psql
  \set QUIET yes
  \t\a\f ' '
  \unset QUIET
  \o | /usr/bin/gnuplot
  select 'set title "My Graph"; set terminal dumb 78 24; set key off; set ylabel "Time"; set xlabel "Servers";' || 'plot ''-'' with lines;' ;
  :plot_query;
  \set QUIET yes
  \t\a\f
  \unset QUIET
  \o

  barwick@localhost:~$ psql -U postgres testdb
  psql (9.2.3)
  Type "help" for help.

  testdb=#   \set plot_query 'SELECT * FROM plot'
  testdb=# \i tmp/plot.psql


                                    My Graph

      4 ++---------+-----------+----------+----------+-----------+---------**
      +          +           +          +          +           +     **** +
      |                                                          ****     |
  3.5 ++                                                     ****        ++
      |                                                  ****             |
      |                                              ****                 |
    3 ++                                         ****                    ++
      |                                      ****                         |
  2.5 ++                                *****                            ++
      |                             ****                                  |
      |                         ****                                      |
    2 ++                    ****                                         ++
      |                 ****                                              |
      |             ****                                                  |
  1.5 ++        ****                                                     ++
      |     ****                                                          |
      + ****     +           +          +          +           +          +
    1 **---------+-----------+----------+----------+-----------+---------++
      1         1.5          2         2.5         3          3.5         4
                                     Servers

  testdb=#
-- end quote --

And

Sergey Konoplev explains how to do it with a server-side function - you need gnuplot installed on the db server -

-- start quote --

  plpython/plperl/etc plus this way of calling

  select just_for_fun_graph('select ... from ...', 'My Graph', 78, 24, ...)

  will do the trick.
-- end quote --


You could configure the PAGER environment variable and "\pset pager always" so that all your query results are passed through an external script. The external script would have to decide whether to just spit it out on stdout to display as usual, or to send the query output to a graphing program. This is a pretty ugly solution but I'd be interested to know if anyone is doing anything like this.


I found that the best way to do this is to use the RPostgreSQL/R*SQL driver and plot the graph through R.

Heres a little more on it:

http://theexceptioncatcher.com/blog/2012/11/really-cool-thin...



We have graphs in the plan for CartoDB development, and it is built on postgresql, so hopefully making it easier for people to start getting graphs out of their databses.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: