@@ -131,24 +131,28 @@ async def get_table_index(
131131 query = """
132132 WITH t AS (
133133 SELECT
134- schemaname,
135- tablename,
136- format('%I.%I', schemaname, tablename ) as id,
137- format('%I.%I', schemaname, tablename)::regclass as t_oid,
138- obj_description(format('%I.%I', schemaname, tablename)::regclass , 'pg_class') as description,
134+ nspname as schemaname,
135+ relname as tablename,
136+ format('%I.%I', nspname, relname ) as id,
137+ c.oid as t_oid,
138+ obj_description(c.oid , 'pg_class') as description,
139139 (
140140 SELECT
141141 attname
142142 FROM
143+ pg_attribute a
144+ LEFT JOIN
143145 pg_index i
144- JOIN pg_attribute a ON
146+ ON (
145147 a.attrelid = i.indrelid
146148 AND a.attnum = ANY(i.indkey)
149+ )
147150 WHERE
148- i.indrelid = format('%I.%I', schemaname, tablename)::regclass
149- AND
150- (i.indisunique OR i.indisprimary)
151- ORDER BY i.indisprimary
151+ a.attrelid = c.oid
152+ ORDER BY
153+ i.indisprimary DESC NULLS LAST,
154+ i.indisunique DESC NULLS LAST,
155+ attname ~* E'id$' DESC NULLS LAST
152156 LIMIT 1
153157 ) as pk,
154158 (
@@ -164,7 +168,7 @@ async def get_table_index(
164168 pg_attribute
165169 WHERE
166170 attnum>0
167- AND attrelid=format('%I.%I', schemaname, tablename)::regclass
171+ AND attrelid=c.oid
168172 ) as columns,
169173 (
170174 SELECT
@@ -210,16 +214,18 @@ async def get_table_index(
210214 FROM geography_columns
211215 ) as geo
212216 WHERE
213- f_table_schema = schemaname
214- AND f_table_name = tablename
217+ f_table_schema = n.nspname
218+ AND f_table_name = c.relname
215219 ) as geometry_columns
216220 FROM
217- pg_tables
221+ pg_class c
222+ JOIN pg_namespace n ON (c.relnamespace=n.oid)
218223 WHERE
219- schemaname NOT IN ('pg_catalog', 'information_schema')
220- AND tablename NOT IN ('spatial_ref_sys','geometry_columns')
221- AND (:schemas::text[] IS NULL OR schemaname = ANY (:schemas))
222- AND (:tables::text[] IS NULL OR tablename = ANY (:tables))
224+ relkind in ('r','v', 'm', 'f', 'p')
225+ AND n.nspname NOT IN ('pg_catalog', 'information_schema')
226+ AND c.relname NOT IN ('spatial_ref_sys','geometry_columns')
227+ AND (:schemas::text[] IS NULL OR n.nspname = ANY (:schemas))
228+ AND (:tables::text[] IS NULL OR c.relname = ANY (:tables))
223229
224230 )
225231 SELECT
0 commit comments