Re: [HACKERS] Distinguish view and table problem

Lists: pgsql-hackers
From: "suzhiyang" <suzhiyang(at)gmail(dot)com>
To: "pgsql-hackers" <pgsql-hackers(at)postgresql(dot)org>
Subject: Distinguish view and table problem
Date: 2009-12-18 15:57:53
Message-ID: 200912182357482650255@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Hi!
I just want to distinguish a view and a table while postgres execute exec_simple_query(). In the systable of pg_class, a view and a table has different relkind ('r' 'v'). But when I print the parsetree and the rewrite parsetree, I found that a view and a table has no character to distinguish because the structure Relation has no attribute called relkind. Maybe I should read systable to confirm that we are select from a view or table? But there's still has problem. How could I get the relkind of a table(view) by its name from pg_class?
Another question is that does postgres save the user's original query_string at anywhere(systable etc.)? If I want to save the sourceText in the systable, I could add a column to pg_class called query_string. How could I insert a line to pg_class or read a line from it?

Thank you very much!

2009-12-18

suzhiyang


From: Filip Rembiałkowski <plk(dot)zuber(at)gmail(dot)com>
To: suzhiyang <suzhiyang(at)gmail(dot)com>
Cc: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Distinguish view and table problem
Date: 2009-12-18 16:47:15
Message-ID: 92869e660912180847j2a237962sc1163e123c1be78@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

2009/12/18 suzhiyang <suzhiyang(at)gmail(dot)com>

> How could I get the relkind of a table(view) by its name from pg_class?
>

pg_class is (quite logically) UNIQUE on (relname, relnamespace)

SELECT c.relkind from pg_class c, pg_namespace n
where c.relnamespace = n.oid
and c.relname = 'thetable'
and n.nspname = 'theschema'

--
Filip Rembiałkowski
JID,mailto:filip(dot)rembialkowski(at)gmail(dot)com
http://filip.rembialkowski.net/


From: Robert Haas <robertmhaas(at)gmail(dot)com>
To: suzhiyang <suzhiyang(at)gmail(dot)com>
Cc: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Distinguish view and table problem
Date: 2009-12-18 17:31:05
Message-ID: 603c8f070912180931qb6a9d49ldefcb54d06e4e9da@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Fri, Dec 18, 2009 at 10:57 AM, suzhiyang <suzhiyang(at)gmail(dot)com> wrote:
> Another question is that does postgres save the user's original query_string
> at anywhere(systable etc.)? If I want to save the sourceText in the
> systable, I could add a column to pg_class called query_string. How could I
> insert a line to pg_class or read a line from it?

pg_class wouldn't make much sense for this. But you might be
interested in pg_stat_activity.

...Robert


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "suzhiyang" <suzhiyang(at)gmail(dot)com>
Cc: "pgsql-hackers" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Distinguish view and table problem
Date: 2009-12-18 17:45:11
Message-ID: 8218.1261158311@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

"suzhiyang" <suzhiyang(at)gmail(dot)com> writes:
> I just want to distinguish a view and a table while postgres execute
> exec_simple_query(). In the systable of pg_class, a view and a table
> has different relkind ('r' 'v'). But when I print the parsetree and
> the rewrite parsetree, I found that a view and a table has no
> character to distinguish because the structure Relation has no
> attribute called relkind.

See rel->rd_rel->relkind ...

regards, tom lane


From: Filip Rembiałkowski <plk(dot)zuber(at)gmail(dot)com>
To: suzhiyang <suzhiyang(at)gmail(dot)com>
Cc: Pgsql Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Distinguish view and table problem
Date: 2009-12-19 12:48:21
Message-ID: 92869e660912190448k563a2328o5243403dc56cd64e@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

W dniu 19 grudnia 2009 03:20 użytkownik suzhiyang <suzhiyang(at)gmail(dot)com>napisał:

> Sorry, I've not describe my problem precisely.
> I mean that I want to get relkind or something from a systable by the
> programm but not by sql.
>
I don't understand how you can get data from table without using SQL. (maybe
I'm just "too sql")

> That is, if I execute such sql by exec_simple_query("select xxx from
> pg_xxx"), how could I get the result of it in the programm?
>

Are you programming in C? If so, use the API provided by PostgreSQL,
http://www.postgresql.org/docs/current/static/libpq.html

PS. suzhiyang, please use "Reply All" when talking on this list.


From: Jaime Casanova <jcasanov(at)systemguards(dot)com(dot)ec>
To: Filip Rembiałkowski <plk(dot)zuber(at)gmail(dot)com>
Cc: suzhiyang <suzhiyang(at)gmail(dot)com>, Pgsql Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Distinguish view and table problem
Date: 2009-12-19 15:19:34
Message-ID: 3073cc9b0912190719vdab9a2cu26f8febfe96b6121@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

2009/12/19 Filip Rembiałkowski <plk(dot)zuber(at)gmail(dot)com>:
>
> W dniu 19 grudnia 2009 03:20 użytkownik suzhiyang <suzhiyang(at)gmail(dot)com>
> napisał:
>>
>> Sorry, I've not describe my problem precisely.
>> I mean that  I want to get relkind or something from a systable by the
>> programm but not by sql.
>
> I don't understand how you can get data from table without using SQL. (maybe
> I'm just "too sql")
>

i think he is hacking postgres's source code to make the TODO: "allow
recompilation of views" (he send an email about that in another
thread)...

i think this is somewhat necesary to read:
http://wiki.postgresql.org/wiki/Developer_FAQ#Technical_Questions
and of course look at other files that acces that kind of info, for
example look at AlterTableNamespace() funtion in
src/backend/commands/tablecmds.c to find out for a complete example to
identify tables and views

--
Atentamente,
Jaime Casanova
Soporte y capacitación de PostgreSQL
Asesoría y desarrollo de sistemas
Guayaquil - Ecuador
Cel. +59387171157


From: Filip Rembiałkowski <plk(dot)zuber(at)gmail(dot)com>
To: Jaime Casanova <jcasanov(at)systemguards(dot)com(dot)ec>
Cc: suzhiyang <suzhiyang(at)gmail(dot)com>, Pgsql Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Distinguish view and table problem
Date: 2009-12-20 00:33:30
Message-ID: 92869e660912191633v6e6219e3k3a948905b73286fe@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

W dniu 19 grudnia 2009 16:19 użytkownik Jaime Casanova <
jcasanov(at)systemguards(dot)com(dot)ec> napisał:

>
> i think he is hacking postgres's source code to make the TODO: "allow
> recompilation of views" (he send an email about that in another
> thread)...
>

oh. I didn't realise, that such seemingly simple question can relate to such
hard task.

even Oracle and other big players do not have an ideal solution for this...

good luck suzhiyang!

--
Filip Rembiałkowski
JID,mailto:filip(dot)rembialkowski(at)gmail(dot)com
http://filip.rembialkowski.net/


From: suzhiyang <suzhiyang(at)gmail(dot)com>
To: Filip_Rembiałkowski <plk(dot)zuber(at)gmail(dot)com>, Jaime Casanova <jcasanov(at)systemguards(dot)com(dot)ec>
Cc: Pgsql Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: [HACKERS] Distinguish view and table problem
Date: 2009-12-20 01:17:48
Message-ID: 200912200917466091109@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

.........
This task is just a homework for me, but the TA may not deep into this problem and give me such difficult task. That simple idea was very ugly by all appearances. I'm a freshman to postgres, sorry for that bad idea. Now I've give up this problem, complaint to TA and try to solve another easier one to complete my work.
These days I've learned a lot from your discussion and source code.:-)
Thank you for your help!

2009-12-20

suzhiyang

发件人: Filip_Rembiałkowski
发送时间: 2009-12-20 08:33:31
收件人: Jaime Casanova
抄送: suzhiyang; Pgsql Hackers
主题: Re: [HACKERS] Distinguish view and table problem

W dniu 19 grudnia 2009 16:19 użytkownik Jaime Casanova <jcasanov(at)systemguards(dot)com(dot)ec> napisał:

i think he is hacking postgres's source code to make the TODO: "allow
recompilation of views" (he send an email about that in another
thread)...

oh. I didn't realise, that such seemingly simple question can relate to such hard task.

even Oracle and other big players do not have an ideal solution for this...

good luck suzhiyang!

--
Filip Rembiałkowski
JID,mailto:filip(dot)rembialkowski(at)gmail(dot)com
http://filip.rembialkowski.net/