Reputation: 2158
I have two AWS PostgreSQL databases running PostgreSQL 12. The following query is instant on both database A and database B:
select DISTINCT table_schema, table_name, column_name,constraint_type
from information_schema.key_column_usage
JOIN information_schema.table_constraints USING (table_schema,table_name,constraint_schema,constraint_name);
And returns something like this. I've just included one schema for an example in the below output. (Around 40,000 records on database A and around 60,000 records on database B)
table_schema | table_name | column_name | constraint_type
-------------------------+-------------------------------------------+------------------------------------------+-----------------
_remote | table_example_1 | test_field_id | FOREIGN KEY
_remote | table_example_1 | id | PRIMARY KEY
_remote | table_example_1 | tenant_id | FOREIGN KEY
_remote | table_example_2 | id | PRIMARY KEY
_remote | table_example_3 | endpoint_id | FOREIGN KEY
_remote | table_example_3 | id | PRIMARY KEY
However, as soon as I add a where clause to the end of the select, database A (which has fewer records overall) hangs, while database B returns instantly.
select DISTINCT table_schema, table_name, column_name,constraint_type
from information_schema.key_column_usage
JOIN information_schema.table_constraints USING (table_schema,table_name,constraint_schema,constraint_name)
where constraint_type in('PRIMARY KEY','FOREIGN_KEY') ;
What could be causing this? My first guess is the actual AWS infrastructure itself, but I'm curious if anyone has any ideas from a SQL standpoint.
The WHERE clause works instantly when I try to filter by other columns, so it seems to be specifically constraint_type
.
Upvotes: 1
Views: 406
Reputation: 2158
Answering my own question. The issue was with the optimiser statistics.
This occurred due to a missing step on my part when upgrading Postgresql version on AWS. Although AWS do most of the work for you during a major version upgrade, one of the steps you must do manually is as follows (from AWS docs):
Run the ANALYZE operation to refresh the pg_statistic table. You should do this for every database on all your PostgreSQL DB instances. Optimizer statistics aren't transferred during a major version upgrade, so you need to regenerate all statistics to avoid performance issues. Run the command without any parameters to generate statistics for all regular tables in the current database, as follows:
ANALYZE VERBOSE
After I ran the ANALYZE VERBOSE
command, statistics were regenerated and my performance issues were resolved.
Full documentation can be found here: https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/USER_UpgradeDBInstance.PostgreSQL.html
Upvotes: 1