How to get schema of a table if I have the oid (Postgres 9.5.1)? - postgresql

I'm trying to figure out how to get the schema of a table if I have the oid in Postgres 9.5.1. I tried using information_schema but can't figure out which table I use to join with the oid.

You need to join pg_namespace to pg_class
select nsp.nspname as schema_name, tbl.relname as table_name
from pg_namespace nsp
join pg_class tbl on nsp.oid = tbl.relnamespace
where tbl.oid = 42;

Related

Connecting Microstrategy Cube to a Postgres Materialized View

I have created a Materialized View in a Postgres Database. When I am creating my microstrategy cube connection to this Postgres Database, I am unable to see this Materialized View.
Can anyone help me to understand why I am unable to see this?
Thanks
You need to edit the read settings for your Warehouse Catalog.
Location: Schema->Warehouse Catalog Options->Catalog->Read Settings
Query for tables:
SELECT n.nspname as NAME_SPACE,
c.relname as TAB_NAME
FROM pg_catalog.pg_class c
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
WHERE c.relkind in ('v', 'm', 'r', 'p', '')
AND n.nspname <> 'pg_catalog'
AND n.nspname <> 'information_schema'
AND n.nspname !~ '^pg_toast'
ORDER BY 1,2
Column Query:
hanged to LEFT JOIN in on table INFORMATION_SCHEMA.COLUMNS (no issues for me so far)
SELECT DISTINCT
nspname AS NAME_SPACE,
relname AS TAB_NAME,
attname AS COL_NAME,
typname AS DATA_TYPE,
COALESCE(B.CHARACTER_MAXIMUM_LENGTH, attlen) AS DATA_LEN,
NUMERIC_PRECISION AS DATA_PREC,
NUMERIC_SCALE AS DATA_SCALE
FROM
pg_attribute A
JOIN
pg_class C
ON
A.attrelid = C.oid
JOIN
pg_namespace N
ON
C.relnamespace = N.oid
JOIN
pg_type T
ON
A.atttypid = T.oid
LEFT OUTER JOIN
INFORMATION_SCHEMA.COLUMNS B
ON
A.attname = B.COLUMN_NAME
AND C.relname = B.TABLE_NAME
AND N.nspname = B.TABLE_SCHEMA
WHERE
relname IN (#TABLE_LIST#)
AND attnum > 0
ORDER BY
1,
2,
3
Especially the columns query could be improved but until now I haven't looked back at this as it works fine.
Reason for all of this can be found in the following discussion: Materialized views don't show up in information_schema.
Found a workaround for this...
You can create the view as a standard view, connect this to Microstrategy, and then delete the view and created as a Materialized View. This will then pick up the newly created Materialized view as long as you keep the same name.

How to delete all index tables from postgres database?

I have quite a few index tables in my DB. I want to delete them and only index those tables that are very big. How Can i delete them?
I can do
select relname from pg_class where relkind='i'; and drop index
But I think this query will also delete some system tables. How do i do this without affecting thr functioning of the DB?
If you are using pg_class to find all indexes, you need to join that to pg_namespace and filter on the schema where your tables (and indexes) are stored.
It is much easier to pg_indexes instead though:
select schemaname,
indexname,
tablename,
format('drop index %I.%I;', schemaname, indexname) as drop_statement
from pg_indexes
where schemaname not in ('pg_catalog', 'pg_toast');
That will however also show you indexes that are used for primary keys.
If you want to exclude primary key indexes, you can use something like this:
select s.nspname as schemaname,
i.relname as indexname,
t.relname as tablename,
format('drop index %I.%I;', s.nspname, i.relname) as drop_statement
from pg_index idx
join pg_class i on i.oid = idx.indexrelid
join pg_class t on t.oid = idx.indrelid
join pg_namespace s on i.relnamespace = s.oid
where s.nspname not in ('pg_catalog', 'pg_toast')
and not idx.indisprimary;
If you also want to exclude unique indexes, just add and not idx.indisunique to the where condition.
Here a sample that actually drops the indices. Based on #a_horse_with_no_name answer.
DO $$
DECLARE
ind record;
BEGIN
FOR ind IN
SELECT
s.nspname as schemaname,
i.relname as indexname,
t.relname as tablename,
format('drop index %I.%I;', s.nspname, i.relname) as drop_statement
FROM pg_index idx
join pg_class i on i.oid = idx.indexrelid
join pg_class t on t.oid = idx.indrelid
join pg_namespace s on i.relnamespace = s.oid
WHERE s.nspname not in ('pg_catalog', 'pg_toast')
AND NOT idx.indisprimary
LOOP
RAISE NOTICE 'Command: %', ind.drop_statement;
EXECUTE ind.drop_statement;
END LOOP;
COMMIT;
END$$;

Listing all relations with schema and columns in PostgreSQL

I am trying to list all the relations with their corresponding schema and columns in PostgreSQL using System Catalog constructs. But it also ends up showing some index names along with relation names, and shows attributes like cmax, cmin, ctid, etc. and so on which are not the actual attributes that I had create while constructing the table. Here is my query:
SELECT
ns.nspname AS schema_name,
idx.attrelid :: REGCLASS AS table_name,
idx.attname AS column_name
FROM pg_attribute AS idx
JOIN pg_class AS i
ON i.oid = idx.attrelid
JOIN pg_namespace AS NS ON i.relnamespace = NS.OID
WHERE nspname='public';
To filter out system attributes, add attnum > 0. To filter out non-tables, add relkind = 'r'. To it would look like
SELECT
ns.nspname AS schema_name,
idx.attrelid :: REGCLASS AS table_name,
idx.attname AS column_name
FROM pg_attribute AS idx
JOIN pg_class AS i
ON i.oid = idx.attrelid
JOIN pg_namespace AS NS ON i.relnamespace = NS.OID
WHERE nspname='public' AND attnum > 0 AND relkind = 'r';
You could also use the information schema, which is a bit more user-friendly in this aspect:
SELECT table_schema, table_name, column_name
FROM information_schema.columns
WHERE table_schema = 'public';

List columns with indexes in Amazon Redshift

I need to query Redshift metadata to get a list of table columns that includes information whether the column is part of primary key or not.
There is a post already List columns with indexes in PostgreSQL that has an answer for PostgreSQL, however unfortunately, it fails on Redshift with "ERROR: 42809: op ANY/ALL (array) requires array on right side"
I figured out how to do it with the help of this https://bitbucket.org/zzzeek/sqlalchemy/pull-request/6/sqlalchemy-to-support-postgresql-80/diff
SELECT attname column_name, attnotnull,
format_type(atttypid, atttypmod) as column_type, atttypmod,
i.indisprimary as primary_key,
col_description(attrelid, attnum) as description
FROM pg_attribute c
LEFT OUTER JOIN pg_index i
ON c.attrelid = i.indrelid AND i.indisprimary AND
c.attnum = ANY(string_to_array(textin(int2vectorout(i.indkey)), ' '))
where c.attnum > 0 AND NOT c.attisdropped AND c.attrelid = :tableOid
order by attnum
The following worked for me:
SELECT n.nspname as schema_name,
t.relname as table_name,
i.relname as index_name,
c.contype as index_type,
a.attname as column_name,
a.attnum AS column_position
FROM pg_class t
INNER JOIN pg_index AS ix ON t.oid = ix.indrelid
INNER JOIN pg_constraint AS c ON ix.indrelid = c.conrelid
INNER JOIN pg_class AS i ON i.oid = ix.indexrelid
INNER JOIN pg_attribute AS a ON a.attrelid = t.oid
AND a.attnum= ANY(string_to_array(textin(int2vectorout(ix.indkey)),' ')::int[])
INNER JOIN pg_namespace AS n ON n.oid = t.relnamespace;
You can leverage the table DDL view AWS published a few months ago (https://github.com/awslabs/amazon-redshift-utils/blob/master/src/AdminViews/v_generate_tbl_ddl.sql) by picking out the constraint component and parsing out the key columns:
select schemaname,tablename, substring(ddl,charindex('(',ddl)+1, charindex(')',ddl)-1-charindex('(',ddl))
from
(
SELECT
n.nspname AS schemaname
,c.relname AS tablename
,200000000 + CAST(con.oid AS INT) AS seq
,'\t,' + pg_get_constraintdef(con.oid) AS ddl
FROM
pg_constraint AS con
INNER JOIN pg_class AS c
ON c.relnamespace = con.connamespace
AND c.relfilenode = con.conrelid
INNER JOIN pg_namespace AS n
ON n.oid = c.relnamespace
WHERE c.relkind = 'r'
ORDER BY seq
)
Note that this query also gives you foreign key columns. It's easy enough to filter those out by appending the query with
where ddl like '%PRIMARY KEY%'
Use below query:
select * from pg_table_def where tablename = 'mytablename'
This will give you all columns for table along with their data type , encoding and if it has sort key or dist key.

How do you view new sequence ownership information in Postgres after using ALTER SEQUENCE?

I'm using "ALTER SEQUENCE sequence OWNED BY table.column" to change sequence association in Postgres. Is there a way to view this new information using \ds or something similar? \ds still shows the table owner as the sequence owner.
SELECT c.relname,u.usename
FROM pg_class c, pg_user u
WHERE c.relowner = u.usesysid and c.relkind = 'S'
AND relnamespace IN (
SELECT oid
FROM pg_namespace
WHERE nspname NOT LIKE 'pg_%'
AND nspname != 'information_schema'
);
OWNED BY table.column changes the associated column; OWNER TO newowner changes the role which owns the sequence. The owner role is what is displayed in \ds. I don't know a psql command to see the linked column of a sequence. OWNED BY is primary used to cascade a delete of the associated sequence when the referent column is removed.
You can get visibility into the owned by column by constructing a query against the system catalog tables pg_class, pg_depend, and pg_attribute. I'm not sure of all the exact semantics of pg_depend, but this query should show you a sequence's column dependency:
select tab.relname as tabname, attr.attname as column
from pg_class as seq
join pg_depend as dep on (seq.relfilenode = dep.objid)
join pg_class as tab on (dep.refobjid = tab.relfilenode)
join pg_attribute as attr on (attr.attnum = dep.refobjsubid and attr.attrelid = dep.refobjid)
where seq.relname = 'sequence';
Yuri Levinsky's answer modified to include the namespace column:
SELECT n.nspname, c.relname, u.usename
FROM pg_class c
INNER JOIN pg_user u ON c.relowner = u.usesysid
INNER JOIN pg_namespace n ON c.relnamespace = n.oid
WHERE c.relkind = 'S'
AND n.nspname NOT LIKE 'pg_%'
AND n.nspname != 'information_schema'
ORDER BY 1, 2, 3;