Optimize high memory usage in instances

MySQL  |  PostgreSQL  |  SQL Server

This document covers how to identify high memory usage for Cloud SQL instances and provides recommendations on how to solve memory-related issues.

To learn how to configure memory usage for a Cloud SQL instance, seeBest practices for managing memory usage.

Identify high memory usage

The following sections discuss high memory usage scenarios.

Use Metrics Explorer to identify the memory usage

You can review memory usage of the instance with thedatabase/memory/components.usage metric inMetrics Explorer.

Note: If you have less than 10% memory indatabase/memory/components.cache anddatabase/memory/components.free combined, the risk of an OOM event is high. To monitor the memory usage and to prevent OOM events, we recommend that you set up analerting policy with a metric threshold condition of 90% indatabase/memory/components.usage.

Use Query insights to analyse explain plan for queries that are consuming high resources

Query insights helps you detect, diagnose, and prevent query performance problems for Cloud SQL databases.Query insights gives you a list of long running queries along with theirexplain plan (PostgreSQL documentation).Review the explain plan and identify the part of the query that has a high memory usage scan method.Regardless of the query run time, query insights gives you the explain plan for all queries. Identify the complex queries that are taking more time so that you know which queries are blocking the memory for longer durations.

Common PostgreSQL scan methods that use high memory include the following:

  • Bitmap heap scan
  • Quick sort
  • Hash join or Hash

High memory usage and relevant logs for Gemini-enabled instances

Preview —Gemini in Databases

This feature is subject to the "Pre-GA Offerings Terms" in the General Service Terms section of theService Specific Terms. You can process personal data for this feature as outlined in theCloud Data Processing Addendum, subject to the obligations and restrictions described in the agreement under which you access Google Cloud. Pre-GA features are available "as is" and might have limited support. For more information, see thelaunch stage descriptions.

If you haveGemini enabled, instead of an out-of-memory (OOM) failure which leads to database downtime, a connection executing a query with high memory usage is terminated thereby preventing database downtime. To identify the defaulting query, you can check the database logs for the following entries:

  (...timestamp….) db=postgres, user=customer FATAL: terminating connection due to administrator command

The following Cloud SQL for PostgreSQL database log is displayed which captures the high memory usage query that was terminated to prevent OOM. The query is a normalized version of the original query:

  db=postgres,user=customer LOG:  postgres process with PID 1734 for the query "SELECT COUNT(product) AS item_count FROM test_table WHERE product_type = $1 AND product LIKE $2 AND c6_2 IN ($3,$4,$5,$6,$7)" has been cancelled.

Notifications are also displayed on theCloud SQL Instances page for the following events:

  • Memory utilization of the instance over the last 24 hours.
  • List of normalized queries that have been canceled within the past 24 hours.
  • A link to Google documentation about optimizing memory usage.

High Memory Usage - Recommendations

The following recommendations address the common memory-related problems.If the instance continues to use a high amount of memory, chances are high that it eventually gets anout of memory issue.If the memory demands of either PostgreSQL or another process cause the system to run out of memory, you see anOut of Memory kernel message in PostgreSQL logs and the PostgreSQL instance is eventually stopped.For example:

Out of Memory: Killed process 12345 (postgres)

The most common instance where you see an OOM issue is with a higher value ofwork_mem with a high number of active connections.Therefore, if you are getting frequent OOMs or to avoid OOMs in your Cloud SQL for PostgreSQL instance, you should consider following these recommendations:

What's next

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-07-14 UTC.