> ## Documentation Index
> Fetch the complete documentation index at: https://docs.squarecloud.app/llms.txt
> Use this file to discover all available pages before exploring further.

# Database Memory, Connections and Slow Queries

> Fix database problems on Square Cloud: RAM that keeps growing, out-of-memory stops, too many connections, slow queries, a full disk and slow starts after a forced stop.

Most database problems show up as memory that seems to leak, connections that run out or queries that get slow. Start from the symptom below; engine details are on the [PostgreSQL](https://docs.squarecloud.app/en/services/databases/postgresql.md), [MySQL](https://docs.squarecloud.app/en/services/databases/mysql.md), [MongoDB](https://docs.squarecloud.app/en/services/databases/mongodb.md) and [Redis](https://docs.squarecloud.app/en/services/databases/redis.md) pages.

## RAM keeps growing

**What it means:** the database's RAM chart climbs after it starts and then stays high, even when traffic drops.

**Why it happens:** the RAM shown in the dashboard is the instance's whole memory, including the operating system's file cache that the engine keeps warm. Each engine also holds memory on purpose:

* **PostgreSQL:** `shared_buffers` (25% of the RAM) plus the file cache.
* **MongoDB:** the WiredTiger cache, and an allocator that keeps freed memory for reuse.
* **MySQL:** the InnoDB buffer pool (128 MB unless you raised it) plus the file cache.
* **Redis:** the data itself, up to 90% of the RAM.

Climbing and then flattening out is normal, not a leak.

**When it is a problem:** if the database runs out of memory, it is stopped and stays stopped until you start it again (there is no automatic restart after an out-of-memory stop). Then:

1. Start it from the dashboard.
2. Find what grew: a query or aggregation over too much data, too many connections (each one costs memory), or on Redis keys without an expiry.
3. Reduce that, or give the database more RAM.

## Too many connections

**What it means:** new connections are refused:

| Engine | Message |
| :- | :- |
| PostgreSQL | `remaining connection slots are reserved for roles with the SUPERUSER attribute` / `sorry, too many clients already` |
| MySQL | `Too many connections` |
| MongoDB | the connection closes right away |
| Redis | `max number of clients reached` |

**Why it happens:** every database accepts up to 256 simultaneous connections (253 for your clients on PostgreSQL), whatever its size. The usual causes are a new client or connection per request, a pool per module instead of per process, several apps or shards each with a large pool, or on MongoDB many `MongoClient` instances (each adds 2 monitoring connections to its pool).

**How to fix:**

1. Create **one pool or client per process** at startup and reuse it everywhere (`pg.Pool`, `mysql2.createPool`, one `MongoClient`, one Redis client).
2. Size the pools so that every process together stays under the limit, with a margin: 4 processes with a pool of 20 use 80.
3. Open the database's **Insights** tab to see which client addresses hold the connections (the third part of each IP address is masked).

## Slow queries

**What it means:** requests to your app get slow, and the database's CPU stays high.

**How to find them:** the **Insights** tab lists the slow queries, refreshed every 60 seconds:

* **PostgreSQL and MySQL:** statements with their mean time, listed once any run took 100 ms or more.
* **MongoDB:** single operations of 100 ms or more, from the profiler.
* **Redis:** the `SLOWLOG`, commands of 10 ms or more.

**How to fix:** run the engine's explain on the slow query and add the index it needs (`EXPLAIN ANALYZE` on PostgreSQL, `EXPLAIN` on MySQL, `explain("executionStats")` on MongoDB). On Redis, replace commands that walk the whole keyspace or a giant key (`KEYS`, `HGETALL` on huge hashes) with `SCAN`, `HSCAN` and `SSCAN`. A query that reads a lot of data also needs RAM: more RAM keeps more of it in cache.

## The database stopped by itself

| Cause | What you see | What happens next |
| :- | :- | :- |
| Out of memory | The database is stopped; the RAM chart reached its limit. | It stays stopped until you start it. See [RAM keeps growing](#ram-keeps-growing). |
| Crash with an error | The database stopped unexpectedly. | It restarts automatically, at most once an hour, and only if it had been running for at least 60 seconds. |
| Storage full (MongoDB) | MongoDB stopped writing and shut down. | It stays stopped. See [the disk is full](#the-disk-is-full). |

## The disk is full

Each database has **10 GB of storage**, fixed. Data, indexes and the engine's own files share it: PostgreSQL's write-ahead log (256 to 512 MB), MongoDB's diagnostic data (up to 200 MB), MySQL's `mysql.slow_log` table when you turn the slow log on, and on Redis the room a save needs next to its data file.

| Engine | What happens when it is full |
| :- | :- |
| PostgreSQL | Writes fail (`could not extend file ... No space left on device`). |
| MySQL | Writes fail (`The table '...' is full`). |
| MongoDB | The server stops and does not come back by itself. |
| Redis | Saves fail, and writes answer `MISCONF` until a save succeeds. |

**How to fix:**

1. Delete or archive data you don't need, and drop unused indexes. Files such as images and uploads belong in [Blob Storage](https://docs.squarecloud.app/en/services/blob.md), not in the database.
2. Return the space: on PostgreSQL a plain `VACUUM` makes it reusable inside the table, while `VACUUM FULL` gives it back to the disk but needs free space the size of the table while it runs. On MySQL, `OPTIMIZE TABLE` rebuilds the table. On MongoDB, `compact` on the collection.
3. On MongoDB, start the database from the dashboard first, then free space right away.

> [!WARNING]
>
> A database close to its 10 GB may also fail to snapshot: an export is limited to about 10 GB, and PostgreSQL and MySQL exports are uncompressed SQL text. Keep a margin.

## Slow to start after a stop

**What it means:** right after starting, PostgreSQL answers `the database system is not yet accepting connections`, or MySQL takes a long time to accept connections.

**Why it happens:** stopping a database gives it 5 seconds to shut down cleanly; a busy database may need longer and is then stopped by force. On the next start it runs its crash recovery (PostgreSQL replays its write-ahead log, MySQL's InnoDB recovers its tables), which takes longer on large databases.

**How to fix:** wait for the recovery to finish; nothing is lost by waiting. Stop the database when its write load is low.

## Related guides

* [Databases overview](https://docs.squarecloud.app/en/services/databases.md): limits, snapshots, Insights and what every database includes.
* [Database connection errors](https://docs.squarecloud.app/en/platform/troubleshooting/database-connection-errors.md): SSL/TLS, authentication and network errors.

## Contact us

If you continue facing **technical difficulties**, our **specialized support team** is available to assist you. [**Contact us**](https://squarecloud.app/en/support) and we'll be happy to help you resolve any issue: support quality is a big part of why developers rate Square Cloud [**4.9/5 across 402 reviews**](https://www.trustpilot.com/review/squarecloud.app) on Google and Trustpilot.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.