How to find out the availability of datatype in Postgres - postgresql

Is there any way to find out particular datatype [say for example chkpass] is available or not through a query ?

PostgreSQL provides a huge amount of meta-data which is easily accessed via SQL. To obtain information about the presence of a (scalar) data type, the information provided by pg_type catalog might be of interest here. Try, for example:
SELECT COUNT(*)
FROM pg_type
WHERE typname = 'chkpass'

run: psql -E
and then, in psql session \dT or \dTS

Related

PostgreSQL - Determine column storage type

I've been reading a lot about PostgreSQL's TOAST, and there's one thing I seem to be missing. They mention in the documentation that, "there are four different strategies for storing TOAST-able columns on disk," those being: PLAIN, EXTENDED, EXTERNAL, and MAIN. They also have a very clear way to define which strategy to use for your column, which can be found here. Essentially, it would be something like this:
ALTER TABLE table_name ALTER COLUMN column_name SET STORAGE EXTERNAL
The one thing I don't see is how to easily retrieve that setting. My question is, is there a simple way (either through commands or pgAdmin) to retrieve the storage strategy being used by a column?
This is stored pg_attribute.attstorage, e.g.:
select att.attname,
case att.attstorage
when 'p' then 'plain'
when 'm' then 'main'
when 'e' then 'external'
when 'x' then 'extended'
end as attstorage
from pg_attribute att
join pg_class tbl on tbl.oid = att.attrelid
join pg_namespace ns on tbl.relnamespace = ns.oid
where tbl.relname = 'table_name'
and ns.nspname = 'public'
and not att.attisdropped;
Note that attstorage is only valid if attlen is > -1
While I like #a_horse_with_no_name's method, after I posted this question, I expanded my search to just general table information and found that if you use psql, you can use the command described here, and the result will be a table listing all of the columns, their types, modifiers, storage types, stats targets, and descriptions.
So, using psql this info can be found with:
\d+ table_name
I just figured I'd post this in case anyone wanted another solution.

How to find database name of \data\base postgres folders?

I have a large folder of 70 GB in my postgres installation under:
D:\Program Files\PostgreSQL\9.5\data\base\130205
Question: how could I find out which database is based on that folder?
I have like 10 databases running on the same server, and most of them having a tablespace on a different drive.
But probably I'm missing a mapping somewhere, maybe a large index or kind of. How can I find out the "causing" database of these amounts of data?
Just run oid2name as PostgreSQL operating system user.
Thanks to the hint of #a_horse, the following statement shows the oid and table names:
SELECT oid,* from pg_database
You can use the following query to find the size of your largest databases (taken from here):
SELECT d.datname AS Name, pg_catalog.pg_get_userbyid(d.datdba) AS Owner,
CASE WHEN pg_catalog.has_database_privilege(d.datname, 'CONNECT')
THEN pg_catalog.pg_size_pretty(pg_catalog.pg_database_size(d.datname))
ELSE 'No Access'
END AS SIZE
FROM pg_catalog.pg_database d
ORDER BY
CASE WHEN pg_catalog.has_database_privilege(d.datname, 'CONNECT')
THEN pg_catalog.pg_database_size(d.datname)
ELSE NULL
END DESC -- nulls first
LIMIT 20
you can use this syntax. it takes the ID and the name of the database from the pg_databse.
$ select pg_database.datname,pg_database.oid from pg_database;

extracting meta info from a table psql using information_schema

How do I extract following meta information from a psql table. column_name, data_type, char_max_length,numeric_precision,constraint_type, constraint_reference, check_clause .In short the information that we have got by this query "\d table_name".
Start psql with the -E flag, and take note of the queries it generates when you issue \d table_name -- you'll need to reproduce many or most of those queries in order to get all the metadata you're asking for.

Metadata from a postgresql-database

I want to get data about all relationships from a PostgreSql-database with query like
SELECT
[creatorUser].[primTableName].[primColumnName] AS primary_column,
[creatorUser].[foreignTableName].[foreignColumnName] AS foreign_column,
FROM
sys.sysidx si,
sysidxcol sic,
sys.SYSFKCOL sfc,
sysusers su,
sys.systable st,
sys.syscolumn sc
WHERE
si.table_id=sic.table_id AND
si.index_id=sic.index_id AND
sfc.primary_column_id=sic.primary_column_id AND
sfc.foreign_table_id=sic.table_id AND
st.table_id=sfc.foreign_table_id AND
su.uid=st.creator AND
sc.column_name=si.index_name
Can anybody help, how to get that data? How to be a correct query
owner1.table1.primCol1 owner1.table1.foreignCol1
owner1.table1.primCol2 owner1.table1.foreignCol2
..
?
PostgreSQL has no sys tables, so it's not clear how you could possibly expect that query to work. Given your comments, I'm guessing that's the query you use on Sybase, and you want to translate it for PostgreSQL.
Well, assuming that the Sybase syntax:
[creatorUser].[primTableName].[primColumnName] AS primary_column,
[creatorUser].[foreignTableName].[foreignColumnName] AS foreign_column,
means "Concatenate the user who created the relationship, the table name, and the column name" I'd probably do something in PostgreSQL like:
SELECT
format('%I.%I.%I', fk.table_schema, fk.table_name, fk.column_name) AS foreign_side,
format('%I.%I.%I', pk.table_schema, pk.table_name, pk.column_name) AS target_side
FROM
information_schema.referential_constraints rc
INNER JOIN information_schema.key_column_usage fk
ON (rc.constraint_catalog = fk.constraint_catalog
AND rc.constraint_schema = fk.constraint_schema
AND rc.constraint_name = fk.constraint_name)
INNER JOIN information_schema.constraint_column_usage pk
ON (rc.unique_constraint_catalog = pk.constraint_catalog
AND rc.unique_constraint_schema = rc.constraint_schema
AND rc.unique_constraint_name = pk.constraint_name);
Except for the format(...) that'll work on Sybase too, with some luck.
Output like:
foreign_side | target_side
---------------------+---------------------------------
public.users.id_cat | public.usercategories.id_numcat
(1 row)
That's schema.table.column . I'm guessing Sybase's "creatorUser" is really "schema" in this case.
You should query appropriate tables under pg_catalog or information_schema schemas. The easiest way to figure out the correct query is to run (... = other command-line options like user, database, ...)
psql -E ...
and then psql displays meta-data queries used in internal commands like \d, \dt, \dv, ... From manual:
-E
--echo-hidden
Echo the actual queries generated by \d and other backslash commands. You can use this to study psql's internal operations. This is equivalent to setting the variable ECHO_HIDDEN from within psql.
information_schema is same as pg_catalog but more portalbe (it is defined in SQL standard). If your app uses postgres only then I would use pg_catalog instead of information_schema

oracle has 'DESCRIBE' to get all the details of the table like wise does t/sql has any thing

oracle has 'DESCRIBE' to get all the details of the table like wise does t/sql has any thing.
SQL Server has sp_help/sp_helptext
MySQL has describe
Sql-Server's sp_help is about as close as you get for something built-in. Remeber to put the tablename in as a parameter...:-)
EXEC sp_help 'mytable'
If you're in ssms, you can r-click your query window and opt to output results to text -- it's a little easier to read the output.
ms-help://MS.SQLCC.v10/MS.SQLSVR.v10.en/s10de_6tsql/html/913cd5d4-39a3-4a4b-a926-75ed32878884.htm
Also -- you can write your own using the system tables (sys.objects, sys.columns, ...) I think 'DESCRIBE' just gives you column name, nullable, and type... so not nearly as much as sp_help provides.
SELECT * FROM sysobjects WHERE parent_obj = (SELECT id FROM sysobjects WHERE name = 'table_name') AND xtype='PK'
will show table details.
SELECT * FROM sys.Tables
will list all tables
There is no describe equivalent.
you might find this searching for MSSQL instead of T/SQL. Most people talking about Transact SQL are talking about stored procedures.