Check Foreign Key
WITH ForeignKeyDetails AS (
SELECT DISTINCT
tc.table_schema,
tc.table_name,
kcu.column_name,
ccu.table_schema AS foreign_table_schema,
ccu.table_name AS foreign_table_name,
ccu.column_name AS foreign_column_name
FROM
information_schema.table_constraints AS tc
JOIN information_schema.key_column_usage AS kcu
ON tc.constraint_name = kcu.constraint_name
AND tc.table_schema = kcu.table_schema
JOIN information_schema.constraint_column_usage AS ccu
ON ccu.constraint_name = tc.constraint_name
AND ccu.table_schema = tc.table_schema
WHERE
tc.constraint_type = 'FOREIGN KEY'
AND tc.table_name = 'issue_headers'
)
SELECT
table_schema,
table_name,
string_agg(DISTINCT column_name, ', ') AS column_names,
string_agg(DISTINCT foreign_table_schema || '.' || foreign_table_name || '.' || foreign_column_name, ', ') AS foreign_keys
FROM
ForeignKeyDetails
GROUP BY
table_schema,
table_name;
Check table column
SELECT
table_schema,
table_name,
column_name,
data_type,
character_maximum_length,
is_nullable,
column_default
FROM
information_schema.columns
WHERE
table_schema NOT IN ('information_schema', 'pg_catalog')
ORDER BY
table_schema,
table_name,
ordinal_position;
Update on conflict
query = f"UPDATE asset_headers SET id = '{asset_id_v3_1}' WHERE org_id = '{org_id}' AND id = '{asset_id_v3}' AND NOT EXISTS ( SELECT 1 FROM asset_headers WHERE id = '{asset_id_v3_1}' );\n"
Check DB Size
SELECT pg_size_pretty( pg_total_relation_size('tablename') );