Обсуждение: Regarding the correct and best way to fetching a tablename in contrib module

Поиск
Список
Период
Сортировка

Regarding the correct and best way to fetching a tablename in contrib module

От
Gaurav Mishra
Дата:
Hi folks ,

I have developed a new data type in PostgreSQL. The handler associated with this data type is written in C language. This handler encrypts the column data. I need help on figuring out how to get the table name when my handler code is called during CRUD and Alter table operations.

let me elaborate a bit about the things i am gonna do here , So the thing is i am developing a cont-rib module and wants to capture table name inside that cont-rib module .

question:

1. Is there any way to capture a table name during CRUD and alter table  ?
I have seen some of the triggers but not able to make it (i don't thing there is any create table trigger exist ). in case If it is possible tell me a way to achieve it.

2. I though of extracting meta-data using pg_class but not helping it seems because i have to give explicitly a  rel-name(table-name) in where clause 
do you think any other way to achieve it ? please elaborate if any and please let me know.

3. Is there any callback functions available so that i can call those callback functions inside a code and if yes at what level we get tablename attached with colname .

Here is some example which will make you understand a bit about the things i am gonna do .

create table new_table(name varchar , new integer) ; 

insert into new_table values('abcdefghijkl' , 5004);

create table new_table1(name1 varchar , new1 integer) ; 

insert into new_table1 values('mnopqrst' , 5005);

So my extension should extact a tablename ,means i should know tablename with the built-in datatype I have declared .

here what i expect as a output :

create extension table_name-extract;

select extract_tablename();

table-name  datatype-name
new_table  name new
new_table1 name1 new1

extract_tablename : this function should give me a table name with data_type inside contrib module so that i can use in the extension .

Any help would be much appropriated .

Thanks and Regards,
Gaurav Mishra
9986425323 

Re: Regarding the correct and best way to fetching a tablename incontrib module

От
Michael Paquier
Дата:
On Mon, Jun 25, 2018 at 07:06:06PM +0530, Gaurav Mishra wrote:
> here what i expect as a output :
>
> create extension table_name-extract;
>
> select extract_tablename();
>
> table-name  datatype-name
> new_table  name new
> new_table1 name1 new1
>
> extract_tablename : this function should give me a table name with
> data_type inside contrib module so that i can use in the extension .

It is possible to know what is the set of objects registered within an
extension using a lookup to system catalogs.  The internal set of
queries used by psql is full of good patterns, so why not looking at
what psql -E does when using psql shortcuts and build up a query which
does what you are looking for?  If you filter the set of objects by type
(a table), then you can look at pg_attribute and get all the column names
you are looking for.  There is also not special need to write C code for
that.
--
Michael

Вложения