104
104
PG_INDEXES_QUERY = """
105
105
SELECT indexname AS NAME,
106
106
indexdef AS definition,
107
+ schemaname,
107
108
tablename
108
109
FROM pg_indexes
109
- WHERE { table_names_like};
110
+ WHERE schemaname='{schema_name}' AND ({ table_names_like}) ;
110
111
"""
111
112
112
113
PG_CHECK_FOR_FOREIGN_KEY = """
144
145
SELECT relname,
145
146
pg_get_partkeydef(oid) AS partition_key
146
147
FROM pg_class
147
- WHERE relname in ({table_names });
148
+ WHERE oid in ({table_ids });
148
149
"""
149
150
150
151
NUM_PARTITIONS_QUERY = """
@@ -295,7 +296,7 @@ def report_postgres_metadata(self):
295
296
tables_buffer = []
296
297
297
298
for tables in table_chunks :
298
- table_info = self ._query_table_information (cursor , tables )
299
+ table_info = self ._query_table_information (cursor , schema [ 'name' ], tables )
299
300
300
301
tables_buffer = [* tables_buffer , * table_info ]
301
302
for t in table_info :
@@ -455,7 +456,7 @@ def _query_tables_for_schema(
455
456
return table_payloads
456
457
457
458
def _query_table_information (
458
- self , cursor : psycopg2 .extensions .cursor , table_info : List [Dict [str , Union [str , bool ]]]
459
+ self , cursor : psycopg2 .extensions .cursor , schema_name : str , table_info : List [Dict [str , Union [str , bool ]]]
459
460
) -> List [Dict [str , Union [str , Dict ]]]:
460
461
"""
461
462
Collect table information . Returns a dictionary
@@ -481,11 +482,10 @@ def _query_table_information(
481
482
tables = {t .get ("name" ): {** t , "num_partitions" : 0 } for t in table_info }
482
483
table_name_lookup = {t .get ("id" ): t .get ("name" ) for t in table_info }
483
484
table_ids = "," .join (["'{}'" .format (t .get ("id" )) for t in table_info ])
484
- table_names = "," .join (["'{}'" .format (t .get ("name" )) for t in table_info ])
485
485
table_names_like = " OR " .join (["tablename LIKE '{}%'" .format (t .get ("name" )) for t in table_info ])
486
486
487
487
# Get indexes
488
- cursor .execute (PG_INDEXES_QUERY .format (table_names_like = table_names_like ))
488
+ cursor .execute (PG_INDEXES_QUERY .format (schema_name = schema_name , table_names_like = table_names_like ))
489
489
rows = cursor .fetchall ()
490
490
for row in rows :
491
491
# Partition indexes in some versions of Postgres have appended digits for each partition
@@ -497,7 +497,7 @@ def _query_table_information(
497
497
498
498
# Get partitions
499
499
if VersionUtils .transform_version (str (self ._check .version ))["version.major" ] != "9" :
500
- cursor .execute (PARTITION_KEY_QUERY .format (table_names = table_names ))
500
+ cursor .execute (PARTITION_KEY_QUERY .format (table_ids = table_ids ))
501
501
rows = cursor .fetchall ()
502
502
for row in rows :
503
503
tables .get (row .get ("relname" ))["partition_key" ] = row .get ("partition_key" )
0 commit comments