I want to find all objects (tables, views, ... etc) that have a dependency on a specific table.
What is a query I could write in postgres to accomplish this.
You'd need to query the catalog for that. Probably pg_depend:
http://www.postgresql.org/docs/current/static/catalog-pg-depend.html
Incase you ever need it, don't miss the convenience type converter that lets you turn table oids and text into relnames like so:
select 'pg_statistics'::regclass; -- 'pg_statistics'
select 2619::regclass; -- 'pg_statistics' too, on my install
# select refclassid::regclass from pg_depend where classid = 'pg_class'::regclass group by refclassid;
refclassid
--------------
pg_namespace
pg_type
pg_class
Related
I'm trying to learn SQL queries, and I've encountered things like this:
SELECT name FROM customer_list t
WHERE ...
What does the t mean? Is it like the name of an instance of that table?
Thanks!
The t is an alias that you can refer to instead of the table name in your query. If you have only one table you're querying from it isn't very useful, but it comes in hand when you have multiple tables (e.g., in join clauses and you want to avoid writing the entire long table name whenever you refer to it. E.g.:
SELECT t.name
FROM some_long_table_name t
JOIN some_other_long_table_name s ON t.id = s.id
I am using a query to fetch the number of rows deleted for a given queryid:
select stl_delete.query,
listagg(distinct svv_table_info.table,',')
from stl_delete
join svv_table_info on svv_table_info.table_id=stl_delete.tbl
where stl_delete.query=1090750
group by stl_delete.query
The result seems correct.
When I run:
select event,solution from stl_alert_event_log where query = pg_last_query_id();
event solution
================================== ======================================================
Nested Loop Join in the query plan Review the join predicates to avoid Cartesian products
Firstly, why is there nested loop?
How do I fix the nested loop join here? Going through the internet, solution is the join predicate which is present in the query.
Even if I remove the listaggr and group by, I still see the issue:
select stl_delete.query,
svv_table_info.table
from stl_delete
join svv_table_info on svv_table_info.table_id=stl_delete.tbl
where stl_delete.query=1090750
The system view svv_table_info is complex and gather a lot of information about tables most of which you are not using. The loop join is inside this view and is needed to produce the in-depth table report.
Your query just needs the name of the table based on tableid. There is a system table that holds this information and will run quicker and not produce a loop join. pg_class has the tableid in a column called oid and the table name in relname. (FYI if you select * from pg_class oid won't show up, you need to specify it by name)
Or you can just live with the alert. This loop join isn't very big in Redshift terms.
I have a Firebird query which I should rewrite into PostgreSQL code.
SELECT TRIM(RL.RDB$RELATION_NAME), TRIM(FR.RDB$FIELD_NAME), FS.RDB$FIELD_TYPE
FROM RDB$RELATIONS RL
LEFT OUTER JOIN RDB$RELATION_FIELDS FR ON FR.RDB$RELATION_NAME = RL.RDB$RELATION_NAME
LEFT OUTER JOIN RDB$FIELDS FS ON FS.RDB$FIELD_NAME = FR.RDB$FIELD_SOURCE
WHERE (RL.RDB$VIEW_BLR IS NULL)
ORDER BY RL.RDB$RELATION_NAME, FR.RDB$FIELD_NAME
I understand SQL, but have no idea, how to work with this system tables like RDB$RELATIONS etc. It would be really great if someone helped me with this, but even some links with this tables explanation will be OK.
This piece of query is in C++ code, and when I'm trying to do this :
pqxx::connection conn(serverAddress.str());
pqxx::work trans(conn);
pqxx::result res(trans.exec(/*there is this SQL query*/));//and there is a mistake
it writes that:
RDB$RELATIONS doesn't exist.
Postgres has another way of storing information about system content. This is called System Catalogs.
In Firebird your query basically returns a row for every column of a table in every schema with an additional Integer column that maps to a field datatype.
In Postgres using system tables in pg_catalog schema something similar can be achieved using this query:
SELECT
TRIM(c.relname) AS table_name, TRIM(a.attname) AS column_name, a.atttypid AS field_type
FROM pg_class c
LEFT JOIN pg_attribute a ON
c.oid = a.attrelid
AND a.attnum > 0 -- only ordinary columns, without system ones
WHERE c.relkind = 'r' -- only tables
ORDER BY 1,2
Above query does return system catalogs as well. If you'd like to exclude them you need to add another JOIN to pg_namespace and a where clause with pg_namespace.nspname <> 'pg_catalog', because this is the schema where system catalogs are stored.
If you'd also like to see datatype names instead of their representative numbers add a JOIN to pg_type.
Information schema consists of collection of views. In most cases you don't need the entire SQL query that stands behind the view, so using system tables will give you better performance. You can inspect views definition though, just to get you started on the tables and conditions used to form an output.
I think you are looking for the information_schema.
The tables are listed here: https://www.postgresql.org/docs/current/static/information-schema.html
So for example you can use:
select * from information_schema.tables;
select * from information_schema.columns;
I got a huge PostgreSQL database with lots of tables. I want learn all empty tables without counting each tables for performance reasons (Some of the tables have several millions rows).
This query will give you an approximate result, but does not include counting table rows.
SELECT relname FROM pg_class JOIN pg_namespace ON (pg_class.relnamespace = pg_namespace.oid) WHERE relpages = 0 AND pg_namespace.nspname = 'public';
This will work best after a VACUUM ANALYZE.
as per http://wiki.postgresql.org/wiki/Slow_Counting , one solution is to first find the tables with small 'reltuples' via
select relname from pg_class where reltuples < X
and then test for emptiness only those.
so u want to see table structure, right? try pg admin
u can open table and see all structure eg datatype, index, function and etc
Just a quick and simple question: in PostgreSQL, how do you list the names of all stored functions/stored procedures using a table using just a SELECT statement, if possible? If a simple SELECT is insufficient, I can make do with a stored function.
My question, I think, is somewhat similar to this other question, but this other question is for SQL Server 2005:
List of Stored Procedure from Table
(optional) For that matter, how do you also list the triggers and constraints that use the same table in the same manner?
SELECT p.proname
FROM pg_catalog.pg_namespace n
JOIN pg_catalog.pg_proc p
ON p.pronamespace = n.oid
WHERE n.nspname = 'public';
SELECT proname, prosrc
FROM pg_catalog.pg_namespace n
JOIN pg_catalog.pg_proc p
ON pronamespace = n.oid
WHERE nspname = 'public';
If you are using psql, try \df
From the man page:
Tip
To look up functions taking arguments or returning values of a specific type, use your pager's search capability to scroll through the \df output.
Running \set ECHO_HIDDEN will reveal what \df is running behind the scenes.
Same as #quassnoi and #davidwhthomas, except I added the argument names in there:
SELECT proname, proargnames, prosrc
FROM pg_catalog.pg_namespace n
JOIN pg_catalog.pg_proc p
ON pronamespace = n.oid
WHERE nspname = 'public';
If the purpose behind listing the functions is to clean them up or iterate a new function with a changing params list, you will frequently need to drop functions:
DROP FUNCTION <name>(<args>);
By adding proargnames, I am able to construct the applicable function name for the drop.
Additionally, it's nice to see a more complete picture when evaluating the functions.
You can use the standard information_schema schema to get metadata about your database (it's in the SQL standard, so it should work the same way in different database systems). In this case you want information_schema.routines.
Excluding the system stuff:
select proname from pg_proc where proowner <> 1;
Have a look at my recipe. It reads functions and triggers. It is based on informations from: Extracting META information from PostgreSQL (INFORMATION_SCHEMA)
Please change the schema_name and table_name in the below query:
SELECT n.nspname AS schema_name
, p.proname AS function_name
, pg_get_function_arguments(p.oid) AS args
, pg_get_functiondef(p.oid) AS func_def
FROM pg_proc p
JOIN pg_namespace n ON n.oid = p.pronamespace
AND n.nspname = 'schema_name'
AND p.prosrc like '%table_name%'
Since the table name is case sensitive, so need to define the exact table name.
For retrieving the argument types of the functions, which are required when referencing the function in ALTER -- using oldevectortypes worked well for me.
See How can I get a list of all functions stored in the database of a particular schema in PostgreSQL?