Check return type of a function in postgresql - postgresql

I want to get return type of a function, so query will like this:
select t.typname
from pg_proc p
inner join pg_namespace n on p.pronamespace = n.oid
inner join pg_type t on t.oid = p.prorettype
where n.nspname = current_schema() and p.prokind = 'f' and p.proname = 'my_function_name';
Now, I use typname to compare to get return type of function: refcursor, record, int,...
But I want to ask that I can change below query to (not join pg_type):
select p.prorettype
from pg_proc p
inner join pg_namespace n on p.pronamespace = n.oid
where n.nspname = current_schema() and p.prokind = 'f' and p.proname = 'my_function_name'
So now, to check return type is refcursor, instead of if typname = 'refcursor' then, I will use if prorettype = 1790 then (1790 is oid of data type 'refcursor'). So can I do this? I mean, in all Postgresql version, everytime, oid of 'refecursor' is still 1790?
Thank you.

You can use format_type()
select format_type(p.prorettype, null) as return_type
from pg_proc p
where p.proname = 'my_function_name';
and p.pronamespace = current_schema::regnamespace;
and p.prokind = 'f'

Related

Get information about table partitions

I have a partitioned table out of main table using range.
CREATE TABLE public.partition1 PARTITION OF public.maintable
FOR VALUES FROM ('2017-01-01 00:00:00') TO ('2050-01-01 00:00:00')
How can i get the Values range information using a query to postgres.
I have used a query that at least gives me information for the main and partitioned tables, but i cannot seem to find a way to access the value range
FROM ('2017-01-01 00:00:00') TO ('2050-01-01 00:00:00')
assigned to table partition1
Query used to get partition table information
WITH RECURSIVE partition_info
(relid,
relname,
relsize,
relispartition,
relkind) AS
(
(SELECT oid AS relid,
relname,
pg_relation_size(oid) AS relsize,
relispartition,
relkind
FROM pg_catalog.pg_class
WHERE relname = 'completedorders' AND
relkind = 'p')
UNION ALL
(SELECT
c.oid AS relid,
c.relname AS relname,
pg_relation_size(c.oid) AS relsize,
c.relispartition AS relispartition,
c.relkind AS relkind
FROM partition_info AS p,
pg_catalog.pg_inherits AS i,
pg_catalog.pg_class AS c
WHERE p.relid = i.inhparent AND
c.oid = i.inhrelid AND
c.relispartition = true)
)
SELECT * FROM partition_info;
The following query provides the information about partitions as well. From there on its just string manipulation in order to get further information.
Note: you will have to change the name of the table in the query.
with recursive inh as (
select i.inhrelid, null::text as parent
from pg_catalog.pg_inherits i
join pg_catalog.pg_class cl on i.inhparent = cl.oid
join pg_catalog.pg_namespace nsp on cl.relnamespace = nsp.oid
where nsp.nspname = 'public' ---<< change table schema here
and cl.relname = 'tablename' ---<< change table name here
union all
select i.inhrelid, (i.inhparent::regclass)::text
from inh
join pg_catalog.pg_inherits i on (inh.inhrelid = i.inhparent)
)
select c.relname as partition_name,
n.nspname as partition_schema,
pg_get_expr(c.relpartbound, c.oid, true) as partition_expression,
pg_get_expr(p.partexprs, c.oid, true) as sub_partition,
parent,
case p.partstrat
when 'l' then 'LIST'
when 'r' then 'RANGE'
end as sub_partition_strategy
from inh
join pg_catalog.pg_class c on inh.inhrelid = c.oid
join pg_catalog.pg_namespace n on c.relnamespace = n.oid
left join pg_partitioned_table p on p.partrelid = c.oid
order by n.nspname, c.relname

How to get the table a FK refers to in Postgres sql

For a given table, I am trying to get all the columns, to include their name, type, whether the column is a primary key, or a foreign key, and if it is a FK, what table it points to. I wrote the query below, but it seems to be give me the what the columns are referenced by, instead of the other way around:
select c.column_name, c.udt_name, constraint_type, kcu.table_name as references from information_schema.columns c
left outer join information_schema.constraint_column_usage u on c.column_name=u.column_name
left outer join information_schema.table_constraints t on u.constraint_name=t.constraint_name
left outer join information_schema.key_column_usage AS kcu on t.constraint_name = kcu.constraint_name
where c.table_name=#name
I am not too worried about edge cases, I am just trying to inverse the references column. Thanks for your time.
Here is a query to get all the foreign keys from a given table along with what they point to:
SELECT c.conname,
t1.relname AS from_table,
a1.attname AS from_column,
t2.relname AS to_table,
a2.attname AS to_column
FROM pg_catalog.pg_constraint c,
pg_catalog.pg_class t1,
pg_catalog.pg_class t2,
pg_catalog.pg_attribute a1,
pg_catalog.pg_attribute a2,
pg_catalog.pg_namespace n1,
pg_catalog.pg_namespace n2
WHERE c.conrelid = t1.oid
AND c.confrelid = t2.oid
AND c.contype = 'f'
AND a1.attrelid = t1.oid
AND a1.attnum = ANY(c.conkey)
AND a2.attrelid = t2.oid
AND a2.attnum = ANY(c.confkey)
AND t1.relkind = 'r'
AND t2.relkind = 'r'
AND n1.oid = t1.relnamespace
AND n2.oid = t2.relnamespace
AND n1.nspname NOT IN ('pg_catalog', 'pg_toast')
AND n2.nspname NOT IN ('pg_catalog', 'pg_toast')
AND pg_catalog.pg_table_is_visible(t1.oid)
AND pg_catalog.pg_table_is_visible(t2.oid)
AND t1.relname = #name;
I see you are using information_schema instead of pg_catalog, so the easiest way might be to turn my query into a CTE or subquery, then join to it based on your table and column name.
EDIT: It sounds like you just need some help joining my query to what you already have. Actually your query has a lot of errors in how it does the joins, so I've rewritten it to include the correct foreign key results:
SELECT c.column_name,
c.udt_name,
t.constraint_type,
t.constraint_name,
x.to_table
FROM information_schema.columns c
LEFT OUTER JOIN (
SELECT t.constraint_type,
t.constraint_catalog,
t.constraint_schema,
t.constraint_name,
t.table_catalog,
t.table_schema,
t.table_name,
u.column_name
FROM information_schema.constraint_column_usage u
LEFT OUTER JOIN information_schema.table_constraints t
ON t.table_catalog = u.table_catalog
AND t.table_schema = u.table_schema
AND t.table_name = u.table_name
AND t.constraint_catalog = u.constraint_catalog
AND t.constraint_schema = u.constraint_schema
AND t.constraint_name = u.constraint_name
WHERE t.constraint_type IS DISTINCT FROM 'FOREIGN KEY'
) t
ON c.table_catalog = t.table_catalog
AND c.table_schema = t.table_schema
AND c.table_name = t.table_name
AND c.column_name = t.column_name
LEFT OUTER JOIN (
SELECT c.conname,
t1.relname AS from_table,
a1.attname AS from_column,
t2.relname AS to_table,
a2.attname AS to_column
FROM pg_catalog.pg_constraint c,
pg_catalog.pg_class t1,
pg_catalog.pg_class t2,
pg_catalog.pg_attribute a1,
pg_catalog.pg_attribute a2,
pg_catalog.pg_namespace n1,
pg_catalog.pg_namespace n2
WHERE c.conrelid = t1.oid
AND c.confrelid = t2.oid
AND c.contype = 'f'
AND a1.attrelid = t1.oid
AND a1.attnum = ANY(c.conkey)
AND a2.attrelid = t2.oid
AND a2.attnum = ANY(c.confkey)
AND t1.relkind = 'r'
AND t2.relkind = 'r'
AND n1.oid = t1.relnamespace
AND n2.oid = t2.relnamespace
AND n1.nspname NOT IN ('pg_catalog', 'pg_toast')
AND n2.nspname NOT IN ('pg_catalog', 'pg_toast')
AND pg_catalog.pg_table_is_visible(t1.oid)
AND pg_catalog.pg_table_is_visible(t2.oid)
) x
ON x.from_table = c.table_name
AND x.from_column = c.column_name
WHERE c.table_name = #cards
;
This version also prevents lots of duplicate rows if you have NOT NULL or CHECK constraints.
I am not sure if you know how to programming with other languages(such as java, C#) besides writing Sql queries
If you know any of them, you can use the function which are int the JDBC/ODBC drivers to get the things you want
Here is an example of using jdbc
you open a connection to the database
from the connection you get, and you can get the meta data, here is the code
conn=dataSource.getConnection();
DatabaseMetaData dbmeta = conn.getMetaData();
After you get the meta data, you can get all the columns, primary keys, foreign keys and even indexed. See the sample codes below
ResultSet rsKey = dbmeta.getPrimaryKeys(null, schemaName, tableName);
ResultSet rs = dbmeta.getColumns(null, schemaName, tableName, "%");
ResultSet rsForeign=meta.getExportedKeys(null, schemaName, tableName);

Using default sql tables to find columns present in a table

I have a postgresql database set up with clients data split by schema.
Within sql I would like to identify for the table products which columns containing the string "id" exist
I'm trying to use the pg_... tables to identify these columns, but the below query appears to be bringing back results from across schemas despite the restriction on table_schema
SELECT *
FROM pg_class c
INNER JOIN pg_attribute a ON a.attrelid = c.oid
INNER JOIN pg_type t ON a.atttypid = t.oid
INNER JOIN information_schema.tables sch ON c.relname = sch.table_name
WHERE c.relname = 'products'
AND a.attnum > 0
AND a.attname LIKE '%id%'
AND table_schema = 'schema001'
I guess the schemas could be set up incorrectly or the where clause is incorrect - any help would be appreciated
Use pg_namespace and drop the information_schema:
SELECT nspname, relname, attname
FROM pg_class c
INNER JOIN pg_namespace n ON n.oid = c.relnamespace
INNER JOIN pg_attribute a ON a.attrelid = c.oid
INNER JOIN pg_type t ON a.atttypid = t.oid
WHERE c.relname = 'products'
AND a.attnum > 0
AND a.attname LIKE '%id%'
AND n.nspname = 'schema001';

Postgres: variable saying from which schema I select

I have created a SQL SELECT to get all enums and their values in a schema:
SELECT
t.typname, array_agg(e.enumlabel)
FROM pg_type t
JOIN pg_enum e ON t.oid = e.enumtypid
JOIN pg_namespace n ON t.typnamespace = n.oid
WHERE t.typtype = 'e' AND n.nspname = 'public'
GROUP BY typname
I have put the select into a view so I dont have to write it everytime I want to call it. The only thing that bothers me is that if I rename the schema or use it in another schema I have to rewrite the name of the schema manually, check line 6 of the code:
WHERE t.typtype = 'e' AND n.nspname = 'public'
Public is hardtyped there. When selecting in postgres, is there a "global" variable saying from which schema you select? I was not able to find any.
Thanks
PS: I use postgres 8.4
The current schema can be retrieved using the function current_schema()
http://www.postgresql.org/docs/current/static/functions-info.html
Alternatively to wiring the schema or looking it up with current_schema(), you could make your view group by schema as well and then select on the view.
create or replace view enum_vw as
SELECT
n.nspname, t.typname, array_agg(e.enumlabel)
FROM pg_type t
JOIN pg_enum e ON t.oid = e.enumtypid
JOIN pg_namespace n ON t.typnamespace = n.oid
WHERE t.typtype = 'e'
GROUP BY n.nspname, t.typname;
select * from enum_vw where nspname = 'public';

Is it possible to discover the column types from a Postgres function?

I'm working on a utility that is using templates to generate a data access layer against a Postgres database. As part of this I'm trying to dynamically discover the return types of the stored procedures. This is easy enough in simple cases where a single standard type is returned, but I'm struggling when it comes to it returning a user defined type.
I'd appreciate if someone could provide the necessary SQL to return this data.
Thanks
Mark
I appreciate the answers that I have so far, which effectively boil to to the following SQL
SELECT p.proname, t.typname, p,proretset
FROM pg_catalog.pg_proc p
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace
INNER JOIN pg_type t ON p.prorettype = t.oid
WHERE n.nspname = 'public'
--and proname = 'foo'
ORDER BY proname;
This will return the name of the return types. However I still need to decompose the type into the properties that make it up when it returns a user defined type.
In the case that a function returns a record I don't think there is any way to discover its return structure other than calling the function and examining its return values.
psql meta commands are an easy shortcut to finding information schema stuff.
try test=# \d? to find introspection info.
then psql -E -c '\df' will show the sql behind the show function command:
d$ psql -E -c '\df+'
********* QUERY **********
SELECT n.nspname as "Schema",
p.proname as "Name",
pg_catalog.pg_get_function_result(p.oid) as "Result data type",
pg_catalog.pg_get_function_arguments(p.oid) as "Argument data types",
CASE
WHEN p.proisagg THEN 'agg'
WHEN p.proiswindow THEN 'window'
WHEN p.prorettype = 'pg_catalog.trigger'::pg_catalog.regtype THEN 'trigger'
ELSE 'normal'
END as "Type",
CASE
WHEN p.provolatile = 'i' THEN 'immutable'
WHEN p.provolatile = 's' THEN 'stable'
WHEN p.provolatile = 'v' THEN 'volatile'
END as "Volatility",
pg_catalog.pg_get_userbyid(p.proowner) as "Owner",
l.lanname as "Language",
p.prosrc as "Source code",
pg_catalog.obj_description(p.oid, 'pg_proc') as "Description"
FROM pg_catalog.pg_proc p
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace
LEFT JOIN pg_catalog.pg_language l ON l.oid = p.prolang
WHERE pg_catalog.pg_function_is_visible(p.oid)
AND n.nspname <> 'pg_catalog'
AND n.nspname <> 'information_schema'
ORDER BY 1, 2, 4;
**************************
In your case the would be what you want:
pg_catalog.pg_get_function_result(p.oid) as "Result data type",
This query will list the stored procedures with the types.
SELECT proname, proargnames as arguments,
oidvectortypes(proargtypes) as arguments_type,
t.typname as return_type,prosrc as source
FROM pg_catalog.pg_namespace n
JOIN pg_catalog.pg_proc p ON pronamespace = n.oid
JOIN pg_type t ON p.prorettype = t.oid
WHERE nspname = 'public'
You can always filter by proname.
Just to get started:
SELECT
*
FROM
pg_proc
JOIN pg_type ON pg_type.oid = ANY(proallargtypes)
WHERE
proname = 'foo';
Are you looking for this?
SELECT proname,
pg_get_function_result(oid)
FROM pg_proc
WHERE proname = 'foo';
if the function returns a record then the type is not known until runtime, demonstrated with:
create or replace function func() returns record language plpgsql immutable as $$
declare
r record;
q record;
begin
select 10, 'hello' into r;
select 11, 'hello', 'helloagain' into q;
if random()>0.5 then
return r;
else
return q;
end if;
end;$$;
in other words, you can't know the type until after you call the function. Once you have called the function, you could dynamically determine information about the record by passing it into a C-language function as referenced here
Thanks for the help guys, I'm think JackPDouglas is correct and that since functions that return record sets can be polymorphic that there's no way to find out the return type definition.
However here's the SQL I was looking for to get the definition of a function that returns a composite type:
SELECT t.typname, attname, a.typname
from pg_type t
JOIN pg_class on (reltype = t.oid)
JOIN pg_attribute on (attrelid = pg_class.oid)
JOIN pg_type a on (atttypid = a.oid)
WHERE t.typname = (
SELECT t.typname
FROM pg_catalog.pg_proc p
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace
INNER JOIN pg_type t ON p.prorettype = t.oid
WHERE n.nspname = 'public'
and proname = 'foo'
ORDER BY proname
);