A query which accomplishes what is in the title would solve my problem.
My problem:
I want to extract column names and data types for a table. Here is my query for this:
SELECT
column_name,
data_type
FROM
information_schema.columns
WHERE
table_name = '<table_name>'
ORDER BY ordinal_position;
This works great, given that the table only occurs once in the whole database. If it occurs in multiple schemas, this query returns all the columns and data types for all tables of that name.
I know that I want only to deal with the first occurrence of this table in the search_path
, i.e. the table whose schema appears first in the search_path
. How do I extract that information, so I can then set a condition on the table_schema
column of information_schema.columns
in my query?
2
Answers
Thanks to @Bergi, who commented with the query to get the correct schema name.
Here is the complete query which solves the problem I outlined in the question, using @Bergi's query as a subquery:
You can get the schemas from the
search_path
parameter as an array using thecurrent_schemas()
session info function. You could then join this against theinformation_schema.tables
, add your name condition, order by the array index (fromUNNEST(current_schemas(true)) WITH ORDINALITY
), andLIMIT
the result to the first row.An easier way to resolve a table name using the search path is to just use it, namely by converting the string to the respective table object identifier type. This can be done using a type cast
'<table_name>'::regclass
or a function callto_regclass('<table_name>')
– the latter doesn’t throw an error when the table doesn’t exist. The result uniquely identifies a specific table in one of thesearch_path
schemas, though getting the schema name from the oid is a bit cumbersome. You could directly query your table columns from thepg_attribute
system table instead ofinformation_schema.columns
, though that requires you to be aware of things likeattisdropped
and there’s no direct equivalent todata_type
.Instead, you could get the schema name from the
pg_class
system table:or from the
pg_identify_object
function: