Discussion:
how to pass tablename to a function
A L.
2014-09-04 23:42:45 UTC
Permalink
Hi, All


I like to pass the tablename to function but not using execute clause, here is my script


CREATE OR REPLACE FUNCTION functions.pgsql_event_unpack_batch(IN
_tbl text, IN jobid bigint, IN jobtime timestamp with time zone, IN
startid bigint, IN stopid bigint)

RETURNS TABLE(events bigint, errors bigint[]) AS

$BODY$

BEGIN


RETURN QUERY

WITH

unpacking (raw_id, time, userid, eventtype, pagename, userhost, application, status, error)

AS (

select

id as raw_id,

(up).time,

(up).userid,

coalesce((up).eventtype, ''),

coalesce((up).pagename, ''),

(up).userhostaddress as userhost,

coalesce((up).application, ''),

(up).status,

(up).error

from(

select id,

functions.python_events_unpack(event_object) up

from _tbl

where id between startid and stopid

) a

where (up).userid is not NULL

)



I want to pass the _tbl to the select query integrated in the unpacking(), how can I make it?


thanks


Alec
Adrian Klaver
2014-09-04 23:57:09 UTC
Permalink
Post by A L.
Hi, All
I like to pass the tablename to function but not using execute clause, here is my script
CREATE OR REPLACE FUNCTION functions.pgsql_event_unpack_batch(IN _tbl
text, IN jobid bigint, IN jobtime timestamp with time zone, IN startid
bigint, IN stopid bigint)
RETURNS TABLE(events bigint, errors bigint[]) AS
$BODY$
BEGIN
RETURN QUERY
WITH
unpacking (raw_id, time, userid, eventtype, pagename, userhost,
application, status, error)
AS (
select
id as raw_id,
(up).time,
(up).userid,
coalesce((up).eventtype, ''),
coalesce((up).pagename, ''),
(up).userhostaddress as userhost,
coalesce((up).application, ''),
(up).status,
(up).error
from(
select id,
functions.python_events_unpack(event_object) up
from _tbl
where id between startid and stopid
) a
where (up).userid is not NULL
)
I want to pass the _tbl to the select query integrated in the
unpacking(), how can I make it?
Assuming you are using plpgsql, see here:

http://www.postgresql.org/docs/9.3/interactive/plpgsql-statements.html#PLPGSQL-STATEMENTS-EXECUTING-DYN

or use a language like plpythonu.
Post by A L.
thanks
Alec
--
Adrian Klaver
***@aklaver.com
--
Sent via pgsql-general mailing list (pgsql-***@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general
David G Johnston
2014-09-05 00:23:28 UTC
Permalink
Post by A L.
I like to pass the tablename to function but not using execute clause, here is my script
[...]
I want to pass the _tbl to the select query integrated in the unpacking(),
how can I make it?
There is no way to perform a query with an unknown, at design time,
identifier without using EXECUTE.

The recommended way to do this is to use "format()" and dollar-quoting -
v9.1+ required:

v_qry := format(

$qry$
WITH [...]
SELECT id, func(...) FROM %I WHERE id [...]
$qry$

, _tbl

);

RETURN QUERY EXECUTE v_qry;

David J.




--
View this message in context: http://postgresql.1045698.n5.nabble.com/how-to-pass-tablename-to-a-function-tp5817864p5817871.html
Sent from the PostgreSQL - general mailing list archive at Nabble.com.
--
Sent via pgsql-general mailing list (pgsql-***@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general
alecinvan
2014-09-04 22:28:22 UTC
Permalink
Hi, All

I like to pass the tablename to function but not using execute clause, here
is my script

CREATE OR REPLACE FUNCTION functions.pgsql_event_unpack_batch(IN _tbl text,
IN jobid bigint, IN jobtime timestamp with time zone, IN startid bigint, IN
stopid bigint)
RETURNS TABLE(events bigint, errors bigint[]) AS
$BODY$
BEGIN

RETURN QUERY
WITH
unpacking (raw_id, time, userid, eventtype, pagename, userhost,
application, status, error)
AS (
select
id as raw_id,
(up).time,
(up).userid,
coalesce((up).eventtype, ''),
coalesce((up).pagename, ''),
(up).userhostaddress as userhost,
coalesce((up).application, ''),
(up).status,
(up).error
from(
select id,
functions.python_events_unpack(event_object) up
from _tbl
where id between startid and stopid
) a
where (up).userid is not NULL
)


I want to pass the _tbl to the select query integrated in the unpacking(),
how can I make it?

thanks

Alec






--
View this message in context: http://postgresql.1045698.n5.nabble.com/how-to-pass-tablename-to-a-function-tp5817861.html
Sent from the PostgreSQL - general mailing list archive at Nabble.com.
--
Sent via pgsql-general mailing list (pgsql-***@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general
Igor Neyman
2014-09-05 14:50:47 UTC
Permalink
-----Original Message-----
From: pgsql-general-***@postgresql.org [mailto:pgsql-general-***@postgresql.org] On Behalf Of alecinvan
Sent: Thursday, September 04, 2014 6:28 PM
To: pgsql-***@postgresql.org
Subject: [GENERAL] how to pass tablename to a function

Hi, All

I like to pass the tablename to function but not using execute clause, here is my script

CREATE OR REPLACE FUNCTION functions.pgsql_event_unpack_batch(IN _tbl text, IN jobid bigint, IN jobtime timestamp with time zone, IN startid bigint, IN stopid bigint)
RETURNS TABLE(events bigint, errors bigint[]) AS $BODY$ BEGIN

RETURN QUERY
WITH
unpacking (raw_id, time, userid, eventtype, pagename, userhost, application, status, error)
AS (
select
id as raw_id,
(up).time,
(up).userid,
coalesce((up).eventtype, ''),
coalesce((up).pagename, ''),
(up).userhostaddress as userhost,
coalesce((up).application, ''),
(up).status,
(up).error
from(
select id,
functions.python_events_unpack(event_object) up
from _tbl
where id between startid and stopid
) a
where (up).userid is not NULL
)


I want to pass the _tbl to the select query integrated in the unpacking(), how can I make it?

thanks

Alec


You can't.
You have to use dynamic sql (EXECUTE).

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