Passing a table as parameter

Lists: pgsql-general
From: Jon Smark <jon(dot)smark(at)yahoo(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Passing a table as parameter
Date: 2011-03-21 17:43:56
Message-ID: 346859.41485.qm@web112809.mail.gq1.yahoo.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

Hi,

Is there any way for a SQL or PL/pgSQL function to receive a table
as parameter? As an illustration, consider the dummy example below.
Note that functions get_from_data1 and get_from_data2 follow essentially
the same pattern; it would be nice to define instead a single polymorphic
function parameterised on the id and table. Is this possible?

Thanks in advance!
Jon

CREATE TABLE data1 (id int4, content text);
CREATE TABLE data2 (id int8, content text);

CREATE FUNCTION get_from_data1 (int4)
RETURNS SETOF text
LANGUAGE sql STABLE AS
$$
SELECT content FROM data1 WHERE id = $1;
$$;

CREATE FUNCTION get_from_data2 (int8)
RETURNS SETOF text
LANGUAGE sql STABLE AS
$$
SELECT content FROM data2 WHERE id = $1;
$$;


From: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
To: Jon Smark <jon(dot)smark(at)yahoo(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Passing a table as parameter
Date: 2011-03-21 19:01:13
Message-ID: AANLkTim2WNWHC9YoR4nsTE8434TX6W7F_HVEgtw=EGeb@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

Hello

2011/3/21 Jon Smark <jon(dot)smark(at)yahoo(dot)com>:
> Hi,
>
> Is there any way for a SQL or PL/pgSQL function to receive a table
> as parameter?  As an illustration, consider the dummy example below.
> Note that functions get_from_data1 and get_from_data2 follow essentially
> the same pattern; it would be nice to define instead a single polymorphic
> function parameterised on the id and table.  Is this possible?
>
> Thanks in advance!
> Jon
>
>
> CREATE TABLE data1 (id int4, content text);
> CREATE TABLE data2 (id int8, content text);
>
>
> CREATE FUNCTION get_from_data1 (int4)
> RETURNS SETOF text
> LANGUAGE sql STABLE AS
> $$
>        SELECT content FROM data1 WHERE id = $1;
> $$;
>
>
> CREATE FUNCTION get_from_data2 (int8)
> RETURNS SETOF text
> LANGUAGE sql STABLE AS
> $$
>        SELECT content FROM data2 WHERE id = $1;
> $$;
>
>

you can pass a table name as parameter only:

CREATE FUNCTION foo(tablename text)
RETURNS SETOF text AS $$
BEGIN
RETURN QUERY EXECUTE 'SELECT content FROM ' || quote_ident(tablename);
END;
$$ LANGUAGE plpgsql;

Regards

Pavel Stehule

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


From: Vibhor Kumar <vibhor(dot)kumar(at)enterprisedb(dot)com>
To: Jon Smark <jon(dot)smark(at)yahoo(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Passing a table as parameter
Date: 2011-03-21 19:35:21
Message-ID: 07C7753E-AAA7-4F96-BEC0-91FB738F261F@enterprisedb.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general


On Mar 21, 2011, at 11:13 PM, Jon Smark wrote:

>
> Is there any way for a SQL or PL/pgSQL function to receive a table
> as parameter? As an illustration, consider the dummy example below.
> Note that functions get_from_data1 and get_from_data2 follow essentially
> the same pattern; it would be nice to define instead a single polymorphic
> function parameterised on the id and table. Is this possible?

Try Something like given below:
CREATE OR REPLACE FUNCTION tab_pass(text) returns setof numeric
as
$$
DECLARE
rec record;
BEGIN
for rec in execute 'select empno from '||$1||' where id = 1'
LOOP
return next rec.empno;
END LOOP;
return next rec.empno;
END;
$$ language plpgsql;

Thanks & Regards,
Vibhor Kumar
EnterpriseDB Corporation
The Enterprise PostgreSQL Company
vibhor(dot)kumar(at)enterprisedb(dot)com
Blog:http://vibhork.blogspot.com


From: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
To: Vibhor Kumar <vibhor(dot)kumar(at)enterprisedb(dot)com>
Cc: Jon Smark <jon(dot)smark(at)yahoo(dot)com>, pgsql-general(at)postgresql(dot)org
Subject: Re: Passing a table as parameter
Date: 2011-03-21 20:02:43
Message-ID: AANLkTi=rJ2ozyh0yv8A__2LJ=DSRet+gMudE=3ZZdrjJ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

2011/3/21 Vibhor Kumar <vibhor(dot)kumar(at)enterprisedb(dot)com>:
>
> On Mar 21, 2011, at 11:13 PM, Jon Smark wrote:
>
>>
>> Is there any way for a SQL or PL/pgSQL function to receive a table
>> as parameter?  As an illustration, consider the dummy example below.
>> Note that functions get_from_data1 and get_from_data2 follow essentially
>> the same pattern; it would be nice to define instead a single polymorphic
>> function parameterised on the id and table.  Is this possible?
>
>
> Try Something like given below:
> CREATE OR REPLACE FUNCTION tab_pass(text) returns setof numeric
> as
> $$
>   DECLARE
>     rec record;
>   BEGIN
>      for rec in execute 'select empno from '||$1||' where id = 1'
>      LOOP
>          return next rec.empno;
>      END LOOP;
>      return next rec.empno;
>   END;
> $$ language plpgsql;

it can work too, but there is sql injection risk.

Do newer 'SELECT ... FROM ' || tabname || ' ...

Regards

Pavel Stehule

>
>
> Thanks & Regards,
> Vibhor Kumar
> EnterpriseDB Corporation
> The Enterprise PostgreSQL Company
> vibhor(dot)kumar(at)enterprisedb(dot)com
> Blog:http://vibhork.blogspot.com
>
>
> --
> Sent via pgsql-general mailing list (pgsql-general(at)postgresql(dot)org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-general
>


From: Vibhor Kumar <vibhor(dot)kumar(at)enterprisedb(dot)com>
To: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
Cc: Jon Smark <jon(dot)smark(at)yahoo(dot)com>, pgsql-general(at)postgresql(dot)org
Subject: Re: Passing a table as parameter
Date: 2011-03-21 20:18:55
Message-ID: D049A134-963B-4482-8DC8-3D366C319E1C@enterprisedb.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general


On Mar 22, 2011, at 1:32 AM, Pavel Stehule wrote:

> it can work too, but there is sql injection risk.
>
> Do newer 'SELECT ... FROM ' || tabname || ' ...
>
> Regards
>
> Pavel Stehule

Yes true. Same with the following too:
CREATE FUNCTION foo(tablename text)
RETURNS SETOF text AS $$
BEGIN
RETURN QUERY EXECUTE 'SELECT content FROM ' || quote_ident(tablename);
END;
$$ LANGUAGE plpgsql;

To prevent from sql injection user can try with SQL Protect:
http://www.enterprisedb.com/docs/en/9.0/sqlprotect/Table%20of%20Contents.htm

Thanks & Regards,
Vibhor Kumar
EnterpriseDB Corporation
The Enterprise PostgreSQL Company
vibhor(dot)kumar(at)enterprisedb(dot)com
Blog:http://vibhork.blogspot.com


From: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
To: Vibhor Kumar <vibhor(dot)kumar(at)enterprisedb(dot)com>
Cc: Jon Smark <jon(dot)smark(at)yahoo(dot)com>, pgsql-general(at)postgresql(dot)org
Subject: Re: Passing a table as parameter
Date: 2011-03-21 20:22:28
Message-ID: AANLkTimm=VuXU785MGnLO3p=Kv6-oOHhWqgynYmvjW0V@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

2011/3/21 Vibhor Kumar <vibhor(dot)kumar(at)enterprisedb(dot)com>:
>
> On Mar 22, 2011, at 1:32 AM, Pavel Stehule wrote:
>
>> it can work too, but there is sql injection risk.
>>
>> Do newer 'SELECT ... FROM ' || tabname || ' ...
>>
>> Regards
>>
>> Pavel Stehule
>
> Yes true. Same with the following too:
> CREATE FUNCTION foo(tablename text)
> RETURNS SETOF text AS $$
> BEGIN
> RETURN QUERY EXECUTE 'SELECT content FROM ' || quote_ident(tablename);
> END;
> $$ LANGUAGE plpgsql;
>
> To prevent from sql injection user can try with SQL Protect:
> http://www.enterprisedb.com/docs/en/9.0/sqlprotect/Table%20of%20Contents.htm
>

simply thinks as using USAGE clause or functions quote_ident,
quote_literal are faster and absolutly secure :). Software like SQL
Protect is good for old unsecured applications but better do
development well.

Regards

Pavel Stehule

> Thanks & Regards,
> Vibhor Kumar
> EnterpriseDB Corporation
> The Enterprise PostgreSQL Company
> vibhor(dot)kumar(at)enterprisedb(dot)com
> Blog:http://vibhork.blogspot.com
>
>


From: Vibhor Kumar <vibhor(dot)kumar(at)enterprisedb(dot)com>
To: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
Cc: Jon Smark <jon(dot)smark(at)yahoo(dot)com>, pgsql-general(at)postgresql(dot)org
Subject: Re: Passing a table as parameter
Date: 2011-03-21 20:47:26
Message-ID: 337C835F-A70D-4FE3-AFEC-41D31BE3DCA6@enterprisedb.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general


On Mar 22, 2011, at 1:52 AM, Pavel Stehule wrote:

> simply thinks as using USAGE clause or functions quote_ident,
> quote_literal are faster and absolutly secure :). Software like SQL

I don't think usage of quote_ident in current requirement of user, would prevent sql injection.
Running sql multiple times, someone can guess the tabename which can give data:
ERROR: relation "am" does not exist
LINE 1: SELECT content FROM am ^QUERY: SELECT content FROM amCONTEXT: PL/pgSQL function "foo" line 2 at RETURN QUERY

SQL Protect will make above message something like given below:
ERROR: SQLPROTECT: Illegal Query: relations

Which stops user guessing relation.

Thanks & Regards,
Vibhor Kumar
EnterpriseDB Corporation
The Enterprise PostgreSQL Company
vibhor(dot)kumar(at)enterprisedb(dot)com
Blog:http://vibhork.blogspot.com


From: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
To: Vibhor Kumar <vibhor(dot)kumar(at)enterprisedb(dot)com>
Cc: Jon Smark <jon(dot)smark(at)yahoo(dot)com>, pgsql-general(at)postgresql(dot)org
Subject: Re: Passing a table as parameter
Date: 2011-03-21 21:09:33
Message-ID: AANLkTikWYfafLK8_bgR1T2DO5vyta955ouogKNv_PFLP@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

2011/3/21 Vibhor Kumar <vibhor(dot)kumar(at)enterprisedb(dot)com>:
>
> On Mar 22, 2011, at 1:52 AM, Pavel Stehule wrote:
>
>> simply thinks as using USAGE clause or functions quote_ident,
>> quote_literal are faster and absolutly secure :). Software like SQL
>
> I don't think usage of quote_ident in current requirement of user, would prevent sql injection.
> Running sql multiple times, someone can guess the tabename which can give data:
> ERROR:  relation "am" does not exist
> LINE 1: SELECT content FROM am ^QUERY:  SELECT content FROM amCONTEXT:  PL/pgSQL function "foo" line 2 at RETURN QUERY
>
> SQL Protect will make above message something like given below:
> ERROR:  SQLPROTECT: Illegal Query: relations
>

it is different view on security. When you have not a security gap,
then is irelevant if somebody has unlimited number of trials. SQL
Protect is "security by obscurity" - a logout can be a good sign for
blind injection.

well usage of quote_ident and quote_literal is a perfect protection
against sql injection. Wrong query doesn't mean a problem. Problem is
when attacker can change a semantic of SQL query.

Pavel

> Which stops user guessing relation.
>
> Thanks & Regards,
> Vibhor Kumar
> EnterpriseDB Corporation
> The Enterprise PostgreSQL Company
> vibhor(dot)kumar(at)enterprisedb(dot)com
> Blog:http://vibhork.blogspot.com
>
>