Re: [HACKERS] External Tabular Data Via SQL

2004-10-04 Thread Josh Berkus
David,

 Please find enclosed an example of what I hope to make into a
 generalized way of accessing external tabular data via SQL.

This is very cool, and I look forward to playing with it.

However,it seems to me that pgFoundry is the place for it and not /contrib.   
We're really trying not to add new projects to contrib unless they're likely 
to get merged with the man code in a couple of versions.  Further, it's got a 
major external dependency.

Besides, other perl hackers are more likely to play with it if it's somewhere 
more accessable.

-- 
Josh Berkus
Aglio Database Solutions
San Francisco

---(end of broadcast)---
TIP 1: subscribe and unsubscribe commands go to [EMAIL PROTECTED]


Re: [HACKERS] External Tabular Data Via SQL

2004-10-04 Thread Andrew Dunstan

Josh Berkus wrote:
David,
 

Please find enclosed an example of what I hope to make into a
generalized way of accessing external tabular data via SQL.
   

This is very cool, and I look forward to playing with it.
However,it seems to me that pgFoundry is the place for it and not /contrib.   
We're really trying not to add new projects to contrib unless they're likely 
to get merged with the man code in a couple of versions.  Further, it's got a 
major external dependency.

Besides, other perl hackers are more likely to play with it if it's somewhere 
more accessable.
 

Of course, if we had a nice code snippets feature working on pgfoundry 
that might be an ideal place for it . (That's a *big* *hint* BTW)

cheers
andrew
---(end of broadcast)---
TIP 9: the planner will ignore your desire to choose an index scan if your
 joining column's datatypes do not match


[HACKERS] External Tabular Data Via SQL

2004-10-03 Thread David Fetter
Kind people,

Please find enclosed an example of what I hope to make into a
generalized way of accessing external tabular data via SQL.

It is written in PL/PerlU for portability reasons, although it could
probably be re-written in C at the cost of some large amount of
effort.  It depends on having Proc::ProcessTable installed.

init_ps() makes a process_table_type using Proc::ProcessTable.
get_ps() takes no arguments and returns a setof process_table_type,
and can be joined to, for example, pg_stat_activity.

Many thanks and kudos to Andrew Dunstan for helping me figure out the
niggly details.

This should go into a new section of contrib/ like sql_utils or some
such.

Big TIA for any hints, tips or pointers, especially to things I've
missed here. :)

Cheers,
D
-- 
David Fetter [EMAIL PROTECTED] http://fetter.org/
phone: +1 510 893 6100   mobile: +1 415 235 3778

Remember to vote!
DROP TYPE process_table_type CASCADE;
CREATE OR REPLACE FUNCTION init_ps ()
RETURNS VOID
LANGUAGE plperlu
AS $$
use Proc::ProcessTable;
my $t = new Proc::ProcessTable;
my $sql = CREATE TYPE process_table_type AS (\n;
foreach my $field (@{[$t-fields]}) {
next unless $field =~ /\S/;
if ($field =~ /id$/) {
$sql .= qq(  ps_$field INTEGER,\n);
} else {
$sql .= qq(  ps_$field TEXT,\n);
}
}
$sql =~ s/,\n$/\n);/m;
my $rv = spi_exec_query($sql);
return;
$$;

COMMENT ON FUNCTION init_ps() IS $$
Copyright (c) 2004, David Fetter
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

* Redistributions of source code must retain the above copyright
  notice, this list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above
  copyright notice, this list of conditions and the following
  disclaimer in the documentation and/or other materials provided
  with the distribution.

* Neither the name of the the PostgreSQL Project nor the names of its
  contributors may be used to endorse or promote products derived
  from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
AS IS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
$$;

SELECT init_ps();

CREATE OR REPLACE FUNCTION get_ps ()
RETURNS SETOF process_table_type
LANGUAGE plperlu AS
$$
use Proc::ProcessTable;
my $sql = SQL;
SELECT a.attname
FROM
pg_catalog.pg_attribute a
JOIN
pg_catalog.pg_class c
ON (c.oid = a.attrelid)
WHERE c.relname = 'process_table_type'
AND a.attnum  0
AND NOT a.attisdropped
ORDER BY a.attnum
SQL
my $rv = spi_exec_query($sql);
my @fields;
foreach my $i ( @{$rv-{rows}} ) {
push @fields, $i-{attname};
}
my $rows = [];
my $proctab = new Proc::ProcessTable;
foreach my $proc ( @{$proctab-table} ) {
my $row = {};
foreach my $field (@fields) { 
$row-{$field} = $proc-{ substr($field,3) };
}
push @$rows, $row;
}
return $rows;
$$
;

COMMENT ON FUNCTION get_ps() IS $$
Copyright (c) 2004, David Fetter
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

* Redistributions of source code must retain the above copyright
  notice, this list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above
  copyright notice, this list of conditions and the following
  disclaimer in the documentation and/or other materials provided
  with the distribution.

* Neither the name of the the PostgreSQL Project nor the names of its
  contributors may be used to endorse or promote products derived
  from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
AS IS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES