I want to get the list of sequence names in Postgres.
In Oracle, I can use:
select sequence_name
from user_sequences
But in Postgres when I use that statement it always throws the error: not found user_sequences.
How can I do this in Postgres?
You can use:
select sequence_schema, sequence_name
from information_schema.sequences;
That will return a list of sequences accessible to the current user, not the ones owned by him.
If you want to list sequences owned by the current user you need to join pg_class, pg_namespace and pg_user:
select n.nspname as sequence_schema,
c.relname as sequence_name,
u.usename as owner
from pg_class c
join pg_namespace n on n.oid = c.relnamespace
join pg_user u on u.usesysid = c.relowner
where c.relkind = 'S'
and u.usename = current_user;
In Postgres a user can own objects (e.g. sequences) in multiple schemas, not just "his own", so you also need to check in which schema the sequence is created.
More details in the manual:
https://www.postgresql.org/docs/current/static/infoschema-sequences.html
https://www.postgresql.org/docs/current/static/catalog-pg-class.html
SELECT c.relname FROM pg_class c WHERE c.relkind = 'S';
Or trying running psql as psql -U username -E followed by \ds. This will show you the query that was been used to generate the result as well.
Related
I have created a sequence (let's call it my_seq) in a schema (let's call it my_schema) of my PostgreSQL (version 13) database. I am sure the sequence exists because I can find it in the result set of the query
select n.nspname as sequence_schema,
c.relname as sequence_name
from pg_class c
join pg_namespace n on n.oid = c.relnamespace
where c.relkind = 'S'
and n.nspname = 'my_schema'
However, if I run the following query
select sequence_name
from information_schema.sequences
where sequence_schema = 'my_schema'
my_seq isn't in the result set.
I have run both queries with the same user I created the sequence with.
Can anybody help me find an explanation for this?
The missing sequences are likely the ones used in an Identity column.
You can fetch all sequences using select * from pg_sequences;
To answer the why of the question: information_schema.sequences is a view, you can see its definition by running \d+ information_schema.sequences. There, we can see that it filters out objects being an internal dependency (AND NOT (EXISTS ... AND pg_depend.deptype = 'i'), which is the case of the sequences backing an Identity column.
Am trying to load Postgres attributes from its catalog tables.
I have created a Postgres table and few set of indexes(unique index, clustered index).
With the below query I could the Name of the index, type of the index as well as its comments.
SELECT c.relname as indexname, i.indisunique as isUniqueIndex, i.indisclustered as isClustered, pg_catalog.obj_description(c.oid, 'pg_class') as COMMENT
FROM pg_catalog.pg_class c
JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
JOIN pg_catalog.pg_index i ON i.indexrelid = c.oid
JOIN pg_catalog.pg_class t ON i.indrelid = t.oid
WHERE c.relkind = 'i' and n.nspname = 'schema1' AND t.relname='table_with_index'
Is there a way to retrieve the fillfactor value of the index?
I'm using Postgresql 8.4 and I see the syntax for creating Index with fill factor, so hope there could be a way to get the value from the catalog tables.
A decent method is to run psql with the "-E" option to echo the catalog queries, then extract what you need from that.
Eg. psql -d your_db -E then issue \d+ <your index name>
An alternative query if you are looking for the index defintion is to use the pg_get_indexdef() function.
Eg.
select pg_get_indexdef('aaa_idx'::regclass::oid);
pg_get_indexdef
------------------------------------------------------------------
CREATE INDEX aaa_idx ON aaa USING btree (x) WITH (fillfactor=60)
I went ahead and extracted a catalog query from 9.2 which may or may not work in 8.4. It has been a while since I worked with the 8.4 catalogs and I can't recall if the tables referenced here have changed.
SELECT c.relchecks,
c.relkind,
c.relhasindex,
c.relhasrules,
c.relhastriggers,
c.relhasoids,
pg_catalog.array_to_string(c.reloptions || array(SELECT 'toast.' || x FROM pg_catalog.unnest(tc.reloptions) x), ', ') as storage_options,
c.reltablespace,
CASE
WHEN c.reloftype = 0 THEN ''
ELSE c.reloftype::pg_catalog.regtype::pg_catalog.text
END,
c.relpersistence
FROM pg_catalog.pg_class c
LEFT JOIN pg_catalog.pg_class tc ON (c.reltoastrelid = tc.oid)
LEFT JOIN pg_namespace nsp ON nsp.oid = c.relnamespace
WHERE nsp.nspname = 'schema1'
AND c.relname = 'YOUR INDEX'
When I use the \d command in psql, I get a list of tables and sequences of all users.
Is there a command which will display only the tables which a specific user is the owner?
Not a psql command:
select c.relname, relkind
from
pg_class c
inner join
pg_roles r on r.oid = c.relowner
where
r.rolname = 'that_owner'
and
c.relkind = 'r'
order by c.relname
If you want tables and sequences:
and
c.relkind in ('r', 'S')
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';
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;