[GENERAL] Crosstab function

2014-05-05 Thread Hengky Liwandouw
Hi Friends, 

 

Could somebody help me with crosstab function ?  I have warehouse table:

 

CREATE TABLE tblwarehouse (

id integer NOT NULL,

warehousename character varying(20)

);

 

COPY tblwarehouse (id, warehousename) FROM stdin;

2 OFFICE

3 STORE2

\.

 

And product table :

 

CREATE TABLE tblproduct (

id serial NOT NULL,

produkid text,

warehouseid integer,

onhand integer

);

 

COPY tblproduct (produkid, warehouseid, onhand) FROM stdin;

279140414 2 10

279140421 3 10

279140414 3 45

\.

 

I need crosstab function to display record from tblproduct like this :

 

   PRODUKID| OFFICE | STORE2 | TOTAL

---+++ --

 279140414 | 10 | 45 |   55

 279140421 |  0 | 10 |   10

 

The crosstab warehouse column name (OFFICE  Store2) is taken from
tblwarehouse so when user add warehouse, crosstab column name will change
automatically. And also each row has total qty.

 

 

 

Thanks in advance !

 



Re: [GENERAL] Monitoring Pg servers with Microsoft SCOM

2014-05-05 Thread Magnus Hagander
On Mon, May 5, 2014 at 3:15 AM, Glen Eustace geust...@godzone.net.nzwrote:


 On 5/05/2014, at 12:26 pm, Adrian Klaver adrian.kla...@aklaver.com
 wrote:

  On 05/04/2014 04:17 PM, Glen Eustace wrote:
  I am in the process of deploying Microsoft System Centre Operations
  Manager and was hoping that somebody had either developed or knew of
  where I could get hold of a management pack for PostgreSQL.
 
  I am not sure whether there is an instrumentation interface into the DB
  so haven't yet looked at rolling our own.
 
  Any comments appreciated.
 
  Searches on this all seem to point back to this post:
 
 
 http://blogs.technet.com/b/kevinholman/archive/2012/03/19/opsmgr-how-to-monitor-non-microsoft-sql-databases-in-scom-an-example-using-postgre-sql.aspx

 Yes, I had seen that.  I was more interested in being able to instrument
 the Pg internals, connection counts, transaction rates, RAM usage etc.  In
 a similar sort of way to MS-SQL.

 Running transactions against an individual DB is still useful but I was
 hoping to do better.


As long as you can run arbitrary SQL, you can get all the information out.
Assuming it can do something with it rather than just run a plain query. If
so, I suggest you take a look at the check_postgres nagios plugin or the
munin plugins for some examples of which SQL to run to get the interesting
metrics back that you want. Some quick googling shows several examples of
how to monitor with custom SQL queries, but I don't know enough (or
anything) about SCOM to recommend any of them in particular.

-- 
 Magnus Hagander
 Me: http://www.hagander.net/
 Work: http://www.redpill-linpro.com/


Re: [GENERAL] Crosstab function

2014-05-05 Thread David G Johnston
Hengky Lie wrote
 The crosstab warehouse column name (OFFICE  Store2) is taken from
 tblwarehouse so when user add warehouse, crosstab column name will change
 automatically. And also each row has total qty.

In what programming language?  The only way to do this is to dynamically
construct the appropriate query, with the correct number of columns,
on-the-fly in the client application and send it as a normal query to the
server.  There is no pure SQL solution.

For the total column you will need a virtual warehouse that holds those
values.  Likely the easiest way to get that will be to UNION ALL the main
real warehouse query and another query the groups by product and sum-counts
that values from the individual warehouses.

IIRC you've already been shown how to write the basic crosstab query; this
really isn't any different but you will need procedural logic and some way
to dynamically build a SQL query string based upon how many warehouses you
have at the time you run the query.

I am assuming you know how to write the basic join query to get the general
form needed for the real warehouse data.

David J.



--
View this message in context: 
http://postgresql.1045698.n5.nabble.com/Crosstab-function-tp5802402p5802456.html
Sent from the PostgreSQL - general mailing list archive at Nabble.com.


-- 
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general


[GENERAL] envelope/postage announcement

2014-05-05 Thread Tim Clarke
Michael Tocci kindly announced these packages recently and they sound
promising but my search-fu is failing me - anyone got a link please?

-- 
Tim Clarke



-- 
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general


Re: [GENERAL] envelope/postage announcement

2014-05-05 Thread Alvaro Herrera
Tim Clarke wrote:
 Michael Tocci kindly announced these packages recently and they sound
 promising but my search-fu is failing me - anyone got a link please?

http://www.3comets.com/

I posted a reply to his announce but now I see it didn't make it through
the list because I used the owner address, and that's disallowed by
antispam rules.  Doh.  If he were to post an errata to his announcement
I could approve it.

-- 
Álvaro Herrerahttp://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training  Services


-- 
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general


Re: [GENERAL] envelope/postage announcement

2014-05-05 Thread Tim Clarke
On 05/05/14 17:21, Alvaro Herrera wrote:
 Tim Clarke wrote:
 Michael Tocci kindly announced these packages recently and they sound
 promising but my search-fu is failing me - anyone got a link please?
 http://www.3comets.com/

 I posted a reply to his announce but now I see it didn't make it through
 the list because I used the owner address, and that's disallowed by
 antispam rules.  Doh.  If he were to post an errata to his announcement
 I could approve it.


tvm alvaro

-- 
Tim Clarke



-- 
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general


Re: [GENERAL] C++, Postgres , libpqxx huge query

2014-05-05 Thread Alexandros Efentakis
Thanks John for your answer. The problem is how do I declare the cursor and
fetch through C++ and libpq++? And if I fetch 1000 rows at a time, then how
do I handle that when I reach the end and there are less than 1000 rows
available? Or I need to fetch one row at a time like this
http://stackoverflow.com/questions/16128142/how-to-use-pqxxstateless-cursor-class-from-libpqxx
?

A.


2014-05-04 12:11 GMT+03:00 John R Pierce pie...@hogranch.com:

 On 5/4/2014 1:57 AM, alexandros_e wrote:

 I have to execute an SQL query to Postgres by the following code. The
 query
 returns a huge number of rows (40M or more) and has 4 integer fields:
 When I
 use a workstation with 32Gb everything works but on a 16Gb workstation the
 query is very slow (due to swapping I guess). Is there any way to tell the
 C++ to load rows at batches, without waiting the entire dataset? With
 Java I
 never had these issues before, due to the probably better JDBC driver.


 see DECLARE and FETCH.

 http://www.postgresql.org/docs/current/static/sql-declare.html
 http://www.postgresql.org/docs/current/static/sql-fetch.html





 --
 john r pierce  37N 122W
 somewhere on the middle of the left coast




Re: [GENERAL] C++, Postgres , libpqxx huge query

2014-05-05 Thread Alexandros Efentakis
To answer my own question, I adapted
http://stackoverflow.com/questions/16128142/how-to-use-pqxxstateless-cursor-class-from-libpqxx

try {
work W(*Conn);
pqxx::stateless_cursorpqxx::cursor_base::read_only,
pqxx::cursor_base::owned
cursor(W, sql[sqlLoad], mycursor, false);
/* Assume you know total number of records returned */
for (size_t idx = 0; idx  countRecords; idx += 10) {
/* Fetch 100,000 records at a time */
result r = cursor.retrieve(idx, idx + 10);
for (int rownum = 0; rownum  r.size(); ++rownum) {
const result::tuple row = r[rownum];
vid1 = row[0].asint();
vid2 = row[1].asint();
vid3 = row[2].asint();
.
}
}
} catch (const std::exception e) {
std::cerr  e.what()  std::endl;
}


2014-05-04 17:34 GMT+03:00 Tom Lane t...@sss.pgh.pa.us:

 Andres Freund and...@2ndquadrant.com writes:
  On 2014-05-04 01:57:43 -0700, alexandros_e wrote:
  I have to execute an SQL query to Postgres by the following code. The
 query
  returns a huge number of rows (40M or more) and has 4 integer fields:
 When I
  use a workstation with 32Gb everything works but on a 16Gb workstation
 the
  query is very slow (due to swapping I guess). Is there any way to tell
 the
  C++ to load rows at batches, without waiting the entire dataset? With
 Java I
  never had these issues before, due to the probably better JDBC driver.

  Try looking into either using a serverside cursor or COPY.

 Another possibility is libpq's recently-introduced row-at-a-time mode:

 http://www.postgresql.org/docs/9.3/static/libpq-single-row-mode.html

 though I'm not sure how effectively that's supported by libpqxx.

 regards, tom lane



Re: [GENERAL] Server continuously enters to recovery mode.

2014-05-05 Thread DrakoRod
Hi everybody Thank you for your help! 

I upgrade the version of 9.0.15 to 9.0.17. But after that constantly show
this error, in all sessions:

*WARNING:  terminating connection because of crash of another server
process  
DETAIL:  The postmaster has commanded this server process to roll back
the  
current transaction and exit, because another server process exited  
abnormally and possibly corrupted shared memory.  
HINT:  In a moment you should be able to reconnect to the database and  
repeat your command.  *

The problem was in the database template0, this had a problem with the
index, I saw in the log when server started, this error:


*PANIC: could not open critical system index 2662  
*


So, I tried REINDEX but this crash. But I saw when I tried connect to the
database template0, showed this error:

*PANIC: could not open critical system index 2662  *

So decided drop database.

   * DROP DATABASE template0;  * 


But I saw this error:

*ERROR:  cannot drop a template database  *


Then run this query and after that DROP DATABASE:


*UPDATE pg_database SET datistemplate='false' WHERE datname='template0';
* 

After that I delete the database template0 and resolved it!! 

Thanks for all!! :) 




--
View this message in context: 
http://postgresql.1045698.n5.nabble.com/Server-continuously-enters-to-recovery-mode-tp5802321p5802394.html
Sent from the PostgreSQL - general mailing list archive at Nabble.com.


-- 
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general


[GENERAL] SELECT with column specified by subquery

2014-05-05 Thread webcoyote
I desire to create a SELECT statement where one of the column names comes
from another table.  Something like:

  SELECT id, (SELECT type FROM favorite_food_type WHERE user = 'ralph')
  FROM foods;

If Ralph's favorite food type is fruit, ultimately I'd like the query to
become:

  SELECT id, fruit FROM foods;

Instead I get:

  SELECT id, 'fruit' FROM foods;

Where 'fruit' is a string and not treated as a column name.  Is there any
way to do this?

Thank you!



--
View this message in context: 
http://postgresql.1045698.n5.nabble.com/SELECT-with-column-specified-by-subquery-tp5802533.html
Sent from the PostgreSQL - general mailing list archive at Nabble.com.


-- 
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general


Re: [GENERAL] SELECT with column specified by subquery

2014-05-05 Thread David G Johnston
webcoyote wrote
 I desire to create a SELECT statement where one of the column names comes
 from another table.  Something like:
 
   SELECT id, (SELECT type FROM favorite_food_type WHERE user = 'ralph')
   FROM foods;
 
 If Ralph's favorite food type is fruit, ultimately I'd like the query to
 become:
 
   SELECT id, fruit FROM foods;
 
 Instead I get:
 
   SELECT id, 'fruit' FROM foods;
 
 Where 'fruit' is a string and not treated as a column name.  Is there any
 way to do this?
 
 Thank you!

Not using pure SQL.  You can run the favorite food query, save the result to
a variable, then build a dynamic SQL query and inject the value of the
variable in place of where you would place the column name alias. Pick
whatever language/client you wish.

I do not understand how you get:

SELECT id, 'fruit' FROM foods;

since the result of a select is a table, not another select...

David J.






--
View this message in context: 
http://postgresql.1045698.n5.nabble.com/SELECT-with-column-specified-by-subquery-tp5802533p5802536.html
Sent from the PostgreSQL - general mailing list archive at Nabble.com.


-- 
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general


Re: [GENERAL] Monitoring Pg servers with Microsoft SCOM

2014-05-05 Thread Glen Eustace

On 6/05/2014, at 1:30 am, Magnus Hagander mag...@hagander.net wrote:

 As long as you can run arbitrary SQL, you can get all the information out. 
 Assuming it can do something with it rather than just run a plain query. If 
 so, I suggest you take a look at the check_postgres nagios plugin or the 
 munin plugins for some examples of which SQL to run to get the interesting 
 metrics back that you want. Some quick googling shows several examples of how 
 to monitor with custom SQL queries, but I don't know enough (or anything) 
 about SCOM to recommend any of them in particular.
 

Thanks, that is the conclusion I had come to as well.  I have written a 
management pack for collection other Linux data so may have a go at a 
PostgreSQL one if I get some time.



Re: [GENERAL] Monitoring Pg servers with Microsoft SCOM

2014-05-05 Thread John R Pierce

On 5/5/2014 12:43 PM, Glen Eustace wrote:


Thanks, that is the conclusion I had come to as well.  I have written 
a management pack for collection other Linux data so may have a go at 
a PostgreSQL one if I get some time.


I would just use check_postgres (perl) from your agent script...



--
john r pierce  37N 122W
somewhere on the middle of the left coast



--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general


Re: [GENERAL] Crosstab function

2014-05-05 Thread Hengky Liwandouw
Hi David,

Are you sure that there is no pure sql solution for this ? 

I think (with my very limited postgres knowledge), function can solve this.

So far i can use command:

select *
from crosstab
(
  'select produkid, warehouseid, onhand
   from tblproduct order by 1',
  'select distinct warehouseid from tblproduct order by 1'
)
as ct (produkid text, office int, store2 int);

and I have this result :

   PRODUKID| OFFICE | STORE2 |
---+++
 279140414 | 10 | 45 |  
 279140421 || 10 |

The problem is the column header is static. If I have new warehouse, I
should manually add it in the column header.

IF I use command : select 'Produk ID text, ' || array_to_string(array(select
warehousename from tblwarehouse), ' int, ') || ' int';

I can get : Produk ID text, OFFICE int, STORE2 int

Which is the column header I need but I really have no idea how to use this
as column header.

Anyway, If i can't do this in postgres, I will try to build sql string in
the client application (Windev) and send the fixed sql to the server

Thanks




-Original Message-
From: pgsql-general-ow...@postgresql.org
[mailto:pgsql-general-ow...@postgresql.org] On Behalf Of David G Johnston
Sent: Monday, May 05, 2014 10:25 PM
To: pgsql-general@postgresql.org
Subject: Re: [GENERAL] Crosstab function

Hengky Lie wrote
 The crosstab warehouse column name (OFFICE  Store2) is taken from 
 tblwarehouse so when user add warehouse, crosstab column name will 
 change automatically. And also each row has total qty.

In what programming language?  The only way to do this is to dynamically
construct the appropriate query, with the correct number of columns,
on-the-fly in the client application and send it as a normal query to the
server.  There is no pure SQL solution. 

For the total column you will need a virtual warehouse that holds those
values.  Likely the easiest way to get that will be to UNION ALL the main
real warehouse query and another query the groups by product and sum-counts
that values from the individual warehouses.

IIRC you've already been shown how to write the basic crosstab query; this
really isn't any different but you will need procedural logic and some way
to dynamically build a SQL query string based upon how many warehouses you
have at the time you run the query.

I am assuming you know how to write the basic join query to get the general
form needed for the real warehouse data.

David J.





-- 
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general


Re: [GENERAL] Crosstab function

2014-05-05 Thread Paul Jungwirth
 Are you sure that there is no pure sql solution for this ?

There is no pure SQL solution because a SQL query always gives a fixed
number of columns. You could compose the SQL in your client app and
vary the columns by the current warehouses. Or you could say GROUP BY
produkit, tblwarehouse.id and rearrange the result client-side. I'd
say those are the practical approaches, but if they aren't possible,
you may also be able to use Postgres's array feature, so that your
result columns are:

produkid
warehouse_totals[]
total

Someone asked a similar question about a year ago, and I wrote up how
to solve it with arrays and a recursive CTE here:

http://www.illuminatedcomputing.com/posts/2013/03/fun-postgres-puzzle/

Good luck,
Paul


On Mon, May 5, 2014 at 7:37 PM, Hengky Liwandouw
hengkyliwand...@gmail.com wrote:
 Hi David,

 Are you sure that there is no pure sql solution for this ?

 I think (with my very limited postgres knowledge), function can solve this.

 So far i can use command:

 select *
 from crosstab
 (
   'select produkid, warehouseid, onhand
from tblproduct order by 1',
   'select distinct warehouseid from tblproduct order by 1'
 )
 as ct (produkid text, office int, store2 int);

 and I have this result :

PRODUKID| OFFICE | STORE2 |
 ---+++
  279140414 | 10 | 45 |
  279140421 || 10 |

 The problem is the column header is static. If I have new warehouse, I
 should manually add it in the column header.

 IF I use command : select 'Produk ID text, ' || array_to_string(array(select
 warehousename from tblwarehouse), ' int, ') || ' int';

 I can get : Produk ID text, OFFICE int, STORE2 int

 Which is the column header I need but I really have no idea how to use this
 as column header.

 Anyway, If i can't do this in postgres, I will try to build sql string in
 the client application (Windev) and send the fixed sql to the server

 Thanks




 -Original Message-
 From: pgsql-general-ow...@postgresql.org
 [mailto:pgsql-general-ow...@postgresql.org] On Behalf Of David G Johnston
 Sent: Monday, May 05, 2014 10:25 PM
 To: pgsql-general@postgresql.org
 Subject: Re: [GENERAL] Crosstab function

 Hengky Lie wrote
 The crosstab warehouse column name (OFFICE  Store2) is taken from
 tblwarehouse so when user add warehouse, crosstab column name will
 change automatically. And also each row has total qty.

 In what programming language?  The only way to do this is to dynamically
 construct the appropriate query, with the correct number of columns,
 on-the-fly in the client application and send it as a normal query to the
 server.  There is no pure SQL solution.

 For the total column you will need a virtual warehouse that holds those
 values.  Likely the easiest way to get that will be to UNION ALL the main
 real warehouse query and another query the groups by product and sum-counts
 that values from the individual warehouses.

 IIRC you've already been shown how to write the basic crosstab query; this
 really isn't any different but you will need procedural logic and some way
 to dynamically build a SQL query string based upon how many warehouses you
 have at the time you run the query.

 I am assuming you know how to write the basic join query to get the general
 form needed for the real warehouse data.

 David J.





 --
 Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
 To make changes to your subscription:
 http://www.postgresql.org/mailpref/pgsql-general



-- 
_
Pulchritudo splendor veritatis.


-- 
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general


Re: [GENERAL] Crosstab function

2014-05-05 Thread Dann Corbit
Why not use the crosstab stuff in contrib?
http://www.postgresql.org/docs/9.3/static/tablefunc.html

Has it been removed or something?

-Original Message-
From: pgsql-general-ow...@postgresql.org 
[mailto:pgsql-general-ow...@postgresql.org] On Behalf Of Paul Jungwirth
Sent: Monday, May 5, 2014 7:49 PM
To: Hengky Liwandouw
Cc: pgsql-general General
Subject: Re: [GENERAL] Crosstab function

 Are you sure that there is no pure sql solution for this ?

There is no pure SQL solution because a SQL query always gives a fixed number 
of columns. You could compose the SQL in your client app and vary the columns 
by the current warehouses. Or you could say GROUP BY produkit, tblwarehouse.id 
and rearrange the result client-side. I'd say those are the practical 
approaches, but if they aren't possible, you may also be able to use Postgres's 
array feature, so that your result columns are:

produkid
warehouse_totals[]
total

Someone asked a similar question about a year ago, and I wrote up how to solve 
it with arrays and a recursive CTE here:

http://www.illuminatedcomputing.com/posts/2013/03/fun-postgres-puzzle/

Good luck,
Paul


On Mon, May 5, 2014 at 7:37 PM, Hengky Liwandouw hengkyliwand...@gmail.com 
wrote:
 Hi David,

 Are you sure that there is no pure sql solution for this ?

 I think (with my very limited postgres knowledge), function can solve this.

 So far i can use command:

 select *
 from crosstab
 (
   'select produkid, warehouseid, onhand
from tblproduct order by 1',
   'select distinct warehouseid from tblproduct order by 1'
 )
 as ct (produkid text, office int, store2 int);

 and I have this result :

PRODUKID| OFFICE | STORE2 |
 ---+++
  279140414 | 10 | 45 |
  279140421 || 10 |

 The problem is the column header is static. If I have new warehouse, I 
 should manually add it in the column header.

 IF I use command : select 'Produk ID text, ' || 
 array_to_string(array(select warehousename from tblwarehouse), ' int, 
 ') || ' int';

 I can get : Produk ID text, OFFICE int, STORE2 int

 Which is the column header I need but I really have no idea how to use 
 this as column header.

 Anyway, If i can't do this in postgres, I will try to build sql string 
 in the client application (Windev) and send the fixed sql to the 
 server

 Thanks




 -Original Message-
 From: pgsql-general-ow...@postgresql.org
 [mailto:pgsql-general-ow...@postgresql.org] On Behalf Of David G 
 Johnston
 Sent: Monday, May 05, 2014 10:25 PM
 To: pgsql-general@postgresql.org
 Subject: Re: [GENERAL] Crosstab function

 Hengky Lie wrote
 The crosstab warehouse column name (OFFICE  Store2) is taken from 
 tblwarehouse so when user add warehouse, crosstab column name will 
 change automatically. And also each row has total qty.

 In what programming language?  The only way to do this is to 
 dynamically construct the appropriate query, with the correct number 
 of columns, on-the-fly in the client application and send it as a 
 normal query to the server.  There is no pure SQL solution.

 For the total column you will need a virtual warehouse that holds 
 those values.  Likely the easiest way to get that will be to UNION ALL 
 the main real warehouse query and another query the groups by product 
 and sum-counts that values from the individual warehouses.

 IIRC you've already been shown how to write the basic crosstab query; 
 this really isn't any different but you will need procedural logic and 
 some way to dynamically build a SQL query string based upon how many 
 warehouses you have at the time you run the query.

 I am assuming you know how to write the basic join query to get the 
 general form needed for the real warehouse data.

 David J.





 --
 Sent via pgsql-general mailing list (pgsql-general@postgresql.org) To 
 make changes to your subscription:
 http://www.postgresql.org/mailpref/pgsql-general



--
_
Pulchritudo splendor veritatis.


-- 
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

-- 
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general


Re: [GENERAL] Crosstab function

2014-05-05 Thread David G Johnston
Hengky Lie wrote
 Hi David,
 
 Are you sure that there is no pure sql solution for this ? 
 
 I think (with my very limited postgres knowledge), function can solve
 this.
 
 Which is the column header I need but I really have no idea how to use
 this
 as column header.
 
 Anyway, If i can't do this in postgres, I will try to build sql string in
 the client application (Windev) and send the fixed sql to the server

Positive.

You could build the necessary string in a pl/pgsql language function but you
would still have trouble getting the data out of the function the way you
want; unless you output a single text column no matter the original data (
basically output a cvs version of the crosstab result).

There is no dynamic execution in SQL so even though you can get a string
that looks like what you want you cannot do anything with it.  Only data is
allowed to be dynamic; the engine has to know the names and types of all
schema objects before it can start so there is no way a query can retrieve
these things from its own data. It's the whole cart-horse thing...

The solution is to build the query in the client and send it.  Make sure you
look at the various quote_ functions in order to minimize the risk of SQL
injection attacks.  These are especially useful for pl/pgsql functions but
you might be able to use them in your first query so that you can avoid
coding all the quoting and escaping rules into your application.  At minimum
double-quote all your identifiers and make sure there are no unescaped
embedded double-quotes.  If the only variables are from data in tables
putting constraints on those tables would probably be useful as well - you
limit valid identifiers but minimized risk of bad data causing an issue.

David J.








--
View this message in context: 
http://postgresql.1045698.n5.nabble.com/Crosstab-function-tp5802402p5802601.html
Sent from the PostgreSQL - general mailing list archive at Nabble.com.


-- 
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general


Re: [GENERAL] Crosstab function

2014-05-05 Thread Hengky Liwandouw
Very Clear instruction !

Thank you very much David. I will do it in my client app and follow your
guidance.


-Original Message-
From: pgsql-general-ow...@postgresql.org
[mailto:pgsql-general-ow...@postgresql.org] On Behalf Of David G Johnston
Sent: Tuesday, May 06, 2014 11:01 AM
To: pgsql-general@postgresql.org
Subject: Re: [GENERAL] Crosstab function

Hengky Lie wrote
 Hi David,
 
 Are you sure that there is no pure sql solution for this ? 
 
 I think (with my very limited postgres knowledge), function can solve
 this.
 
 Which is the column header I need but I really have no idea how to use
 this
 as column header.
 
 Anyway, If i can't do this in postgres, I will try to build sql string in
 the client application (Windev) and send the fixed sql to the server

Positive.

You could build the necessary string in a pl/pgsql language function but you
would still have trouble getting the data out of the function the way you
want; unless you output a single text column no matter the original data (
basically output a cvs version of the crosstab result).

There is no dynamic execution in SQL so even though you can get a string
that looks like what you want you cannot do anything with it.  Only data is
allowed to be dynamic; the engine has to know the names and types of all
schema objects before it can start so there is no way a query can retrieve
these things from its own data. It's the whole cart-horse thing...

The solution is to build the query in the client and send it.  Make sure you
look at the various quote_ functions in order to minimize the risk of SQL
injection attacks.  These are especially useful for pl/pgsql functions but
you might be able to use them in your first query so that you can avoid
coding all the quoting and escaping rules into your application.  At minimum
double-quote all your identifiers and make sure there are no unescaped
embedded double-quotes.  If the only variables are from data in tables
putting constraints on those tables would probably be useful as well - you
limit valid identifiers but minimized risk of bad data causing an issue.

David J.








--
View this message in context:
http://postgresql.1045698.n5.nabble.com/Crosstab-function-tp5802402p5802601.
html
Sent from the PostgreSQL - general mailing list archive at Nabble.com.


-- 
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general



-- 
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general