PostgreSQL SELECT primary key as "serial" or "bigserial"
3
votes
1
answer
8397
views
I've meshed together a way to determine what the
data_type
is as in the data_type
you use in the syntax when creating a new table based off of the PostgreSQL wiki page.
If there is something wrong with my query I need to actually know *what* in a given scenario would throw it off on the explicit context of having a query or queries to run on a purely test database/table to modify that database/table so run this query on in order to test for any false-positives.
SELECT pg_attribute.attname,
format_type(pg_attribute.atttypid, pg_attribute.atttypmod),
CASE
WHEN format_type(pg_attribute.atttypid, pg_attribute.atttypmod)='bigint' THEN 'bigserial'
WHEN format_type(pg_attribute.atttypid, pg_attribute.atttypmod)='integer' THEN 'serial'
END AS type
FROM pg_index, pg_class, pg_attribute
WHERE pg_class.oid = 'delete2'::regclass
AND indrelid = pg_class.oid
AND pg_attribute.attrelid = pg_class.oid
AND pg_attribute.attnum = any(pg_index.indkey)
AND indisprimary;
Here is a table with a primary key that does not return the primary key with this query:
CREATE TABLE delete_key_bigserial (
test1 integer,
id bigserial NOT NULL,
col1 text,
col2 text,
test2 integer
);
Asked by John
(769 rep)
Jan 29, 2015, 02:30 PM
Last activity: May 30, 2025, 12:44 AM
Last activity: May 30, 2025, 12:44 AM