Re: [patch] Proposal for \crosstabview in psql

From: "Daniel Verite" <daniel(at)manitou-mail(dot)org>
To: "PostgreSQL Hackers" <pgsql-hackers(at)postgresql(dot)org>
Cc: "Jim Nasby" <Jim(dot)Nasby(at)BlueTreble(dot)com>,"Dean Rasheed" <dean(dot)a(dot)rasheed(at)gmail(dot)com>,"Andres Freund" <andres(at)anarazel(dot)de>,"Pavel Stehule" <pavel(dot)stehule(at)gmail(dot)com>,"Alvaro Herrera" <alvherre(at)2ndquadrant(dot)com>,"Teodor Sigaev" <teodor(at)sigaev(dot)ru>
Subject: Re: [patch] Proposal for \crosstabview in psql
Date: 2016-02-18 13:11:05
Message-ID: 3d513263-104b-41e3-b1c7-4ad4bd99c491@mm
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Daniel Verite wrote:

> > > ORDER BY name
> > > \crosstabview cols = (select to_char(d, 'Mon') from
> > > generate_series('2000-01-01'::date, '2000-12-01', '1 month') d)
> >
> > My concern with that is that often you don't know what the columns will
> > be, because you don't know what exact data the query will produce. So to
> > use this syntax you'd have to re-create a huge chunk of the original
> > query. :(
> Also, if that additional query refers to tables, it should be executed
> with the same data visibility as the main query. Doesn't that mean
> that both queries should happen within the same repeatable
> read transaction?
> Another impractical aspect of this approach is that a
> meta-command invocation in psql must fit on a single line, so
> queries containing newlines are not acceptable as argument.
> This problem exists with "\copy (select...) to ..." already.

Thinking more about that, it occurs to me that if the sort must come
from a user-supplied bit of SQL, it would be simpler to just direct the
user to submit it in the main query, in an additional dedicated column.

For instance, to get a specific, separate order on "h",
let the user change this:

SELECT v, h, c FROM v_data ORDER BY v;

into that:

SELECT v, h, row_number() over(order by h) as hn, c
FROM v_data ORDER BY v;

then with a relatively simple modification to the patch,
this invocation:

\crosstabview v h:hn c

would display "h" in the horizontal header ordered by "hn".

ISTM this handles two objections raised upthread:

1. The ORDER BY inside OVER() can be augmented with additional
clauses such as lc_collate, desc, nulls last, etc... contrary to
the controversed "+/-" syntax.

2. a post-sort "backdoor" query is no longer necessary.

The drawback for me is that this change doesn't play out with
my original scenario for the command, which is to give the ability to
scrutinize query results in crosstab mode, playing with variations on
what column is pivoted and how headers for both directions get sorted,
while ideally not changing _at all_ the original query in the query
buffer, but just invoking successive \crosstabview [args] commands
with varying arguments.

Best regards,
Daniel Vérité
PostgreSQL-powered mailer:
Twitter: @DanielVerite

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Christoph Berg 2016-02-18 13:34:38 Relaxing SSL key permission checks
Previous Message Michael Paquier 2016-02-18 13:08:49 Re: a raft of parallelism-related bug fixes