Skip to content

Instantly share code, notes, and snippets.

@matussvrcek
Last active April 30, 2025 16:42
Show Gist options
  • Save matussvrcek/108e0bc30f9d0e24821dc41c597c62c3 to your computer and use it in GitHub Desktop.
Save matussvrcek/108e0bc30f9d0e24821dc41c597c62c3 to your computer and use it in GitHub Desktop.
AWS RDS Performance Insights PostgreSQL get full query

AWS RDS PostgreSQL: Get Full Query Text from Performance Insights

This guide explains how to retrieve full SQL query text (beyond 4096 characters) from Performance Insights on AWS RDS for PostgreSQL using the AWS CLI.

https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/USER_PerfInsights.UsingDashboard.SQLTextSize.html#sql-text-engine-limits


Prerequisites

  • Performance Insights must be enabled on your RDS instance.
  • AWS CLI must be installed and configured (aws configure).
  • You must have permissions for pi:GetResourceMetrics and pi:GetDimensionKeyDetails.

Settings

  • Region: eu-central-1
  • Time range: Last 30 minutes (dynamically calculated using macOS date command)

How to Get the DB_ID (Performance Insights DB Identifier)

You can get the required DB_ID by running:

aws rds describe-db-instances \
  --region eu-central-1 \
  --query "DBInstances[].{DBInstanceIdentifier:DBInstanceIdentifier,Arn:DbiResourceId}" \
  --output table

Or find it in the AWS Console:

  • Go to Amazon RDS → Databases → [your instance] → Configuration.
  • Look for the Performance Insights identifier, e.g. db-ABCDEFG1234567XYZ.

Step 1: Get Query IDs with get-resource-metrics

This command fetches the top queries over the past 30 minutes:

aws pi get-resource-metrics \
  --service-type RDS \
  --identifier DB_ID \
  --metric-queries '[{
    "Metric": "db.load.avg",
    "GroupBy": {
      "Group": "db.sql"
    }
  }]' \
  --start-time $(date -u -v-30M +%Y-%m-%dT%H:%M:%SZ) \
  --end-time $(date -u +%Y-%m-%dT%H:%M:%SZ) \
  --region eu-central-1

Sample Output

"Dimensions": {
  "db.sql.id": "37532328C2F2FD4051A9762DC0095B80003D5FEA",
  "db.sql.statement": "select workorderi0_.id as id1_142_, ..."
}

Copy the value of "db.sql.id" for the next step.


Step 2: Get Full Query Text with get-dimension-key-details

Now use the ID from Step 1 to get the full query text:

aws pi get-dimension-key-details \
  --service-type RDS \
  --identifier DB_ID \
  --group db.sql \
  --group-identifier "37532328C2F2FD4051A9762DC0095B80003D5FEA" \
  --region eu-central-1

Result

You will get the entire original SQL query, without the 4096-character truncation present in the Performance Insights UI.


Notes

  • db.sql.id is not the same as db.sql_tokenized.id — only use the former for retrieving full query text.
  • Increasing PostgreSQL’s track_activity_query_size helps only with pg_stat_activity and does not affect Performance Insights output size.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment