

# Database logs for your app

Database logs are the PostgreSQL log of your app’s backend database. They record errors with their statement. They also record statements of 1 second or longer, lock waits of 1 second or longer, and schema changes. For migrations, they record each migration file that changes the schema, as one statement, and failed statements. They don’t record faster statements, so they aren’t a record of every query. If your app has a backend, database logs are on by default in every environment.

## Failed migrations
<a name="monitoring-database-logs-migrations"></a>

To see why a deployment’s migration failed with `MIGRATION_FAILED`, read its build log. For more information, see [View a deployment’s migrations in its build log](deploy-your-app-build-logs.md#deploy-your-app-build-logs-migrations) and [Fix a failed migration](backend-data.md#backend-data-failed).

## Database log delivery
<a name="monitoring-database-logs-delivery"></a>

Lines go to the Amazon CloudWatch Logs (CloudWatch Logs) log group `/aws/amplifygen3/app-name/environment-name/database-logs`. On this page, ` app-name ` is your app’s name and ` environment-name ` is the environment’s name.

Lines take up to 3 minutes to arrive. Lines have no request ID, so match them to a call by time.

You pay CloudWatch Logs charges to ingest and store database logs. For more information, see [Amazon CloudWatch pricing](https://aws.amazon.com/cloudwatch/pricing/).

Each branch has a limit on the number of lines.

To see the settings or change the sampling rate for the log group, run the following AWS Command Line Interface (AWS CLI) commands:

```bash
aws amplifygen3 logs-config get -e environment-name
aws amplifygen3 logs-config edit --database-log-cwl-sampling-rate rate -e environment-name
```

Replace ` rate ` with the percentage of lines to deliver, from 0 to 100. Set the rate to `0` to stop delivery to the log group, or above `0` to start it.

The following AWS CLI command stops database log delivery to CloudWatch Logs:

```bash
aws amplifygen3 logs-config edit --database-log-cwl-sampling-rate 0 -e environment-name
```

To start delivery again, run the same command with the rate that you want.

### Deliver database logs to Amazon S3
<a name="monitoring-database-logs-s3"></a>

You can also deliver database logs to an Amazon Simple Storage Service (Amazon S3) bucket. Each destination has its own sampling rate, from 0 to 100. To turn on delivery to Amazon S3, pass `--database-log-s3-sampling-rate` with a rate above 0. When you set one sampling rate, the other destination keeps its setting. You pay Amazon S3 charges to store the logs that you deliver to Amazon S3. For more information, see [Amazon S3 pricing](https://aws.amazon.com/s3/pricing/).

To deliver logs to Amazon S3, you need a bucket. In the same command as `--database-log-s3-sampling-rate`, pass one of the following options:
+  `--database-log-s3-bucket` – A bucket in your account, as a bucket name or an ARN, such as `arn:aws:s3:::amzn-s3-demo-bucket`. Its bucket policy must let CloudWatch Logs (`delivery.logs.amazonaws.com`) write to it. The command doesn’t change your bucket policy.
+  `--managed-bucket` – The app’s managed bucket. The app’s environments share one managed bucket for each log type. If none exists yet, the command creates it. A new managed bucket blocks public access and uses server-side encryption with Amazon S3 managed keys (SSE-S3).

The following AWS CLI command delivers every database log line to a bucket that you own:

```bash
aws amplifygen3 logs-config edit --database-log-s3-sampling-rate 100 --database-log-s3-bucket amzn-s3-demo-bucket -e environment-name
```

To use the app’s managed bucket instead, replace `--database-log-s3-bucket amzn-s3-demo-bucket` with `--managed-bucket`. The bucket stays set until you name another one. To move logs to another bucket, pass `--database-log-s3-sampling-rate` again with the new bucket.

To stop delivery to Amazon S3, pass `--database-log-s3-sampling-rate 0`. To see the bucket, run `aws amplifygen3 logs-config get -e environment-name `. For database logs, its output table has a row labeled `CloudWatch` for CloudWatch Logs and a row labeled `S3` for Amazon S3.

## Database log fields
<a name="monitoring-database-logs-fields"></a>

The following table lists the fields of each database log line.


| Field | Description | 
| --- | --- | 
|  `resource_arn`  | The ARN of the environment, followed by the destination, `/CWL` or `/S3`. | 
|  `event_timestamp`  | The time of the line, in milliseconds since the Unix epoch. | 
|  `database_name`  | The name of the database. | 
|  `branch`  | The backend branch whose database wrote the line. | 
|  `error_severity`  | The severity of the line, such as `LOG` or `ERROR`. | 
|  `sql_state_code`  | The SQLSTATE code of the line, such as `23505`. A line without an error has `00000`. For more information about SQLSTATE codes, see [PostgreSQL error codes](https://www.postgresql.org/docs/current/errcodes-appendix.html) on the PostgreSQL website. | 
|  `event_message`  | The text of the line. | 
|  `user_name`  | The database user of the session. It’s `api` for the Data API’s session, `migration` for a deployment’s migration session, and `amplify_admin` for a direct connection. | 
|  `application_name`  | The application name of the session. It’s `api` or `migration` for those two sessions. | 
|  `session_id`  | The ID of the database session. | 
|  `transaction_id`  | The ID of the transaction, or `0` if the transaction has no ID. | 
|  `deployment_id`  | On a migration line, the ID of the deployment that ran the migration. | 

To list the database errors of the last hour, run the following AWS CLI command:

```bash
aws amplifygen3 logs get --log-type database -e environment-name --since 1h --filter '{ $.error_severity = "ERROR" }'
```

The filter returns only `ERROR` lines. Each error’s statement is on its own line with `error_severity` `STATEMENT` and the same `session_id`.

To read one backend branch, add `--branch-arn branch-arn `. Direct connections, such as `aws amplifygen3 db connect`, `aws amplifygen3 db query`, and SQL clients, log with `user_name` `amplify_admin`.

## Database performance
<a name="monitoring-database-logs-performance"></a>

To find when the database became slow or busy, start with the `BackendLatency` and `DatabaseConnections` metrics. For each metric, see [CloudWatch metrics](monitoring-with-cloudwatch.md).

Then read the database logs for that time. A connection that the database refused at its connection limit is a `FATAL` line with the `sql_state_code` `53300`. For the limit, see [Quotas](quotas-chapter.md).

To read your database’s statistics views for one diagnostic, use `aws amplifygen3 db inspect name `. Diagnostics include `table-stats`, `index-stats`, `bloat`, `locks`, `blocking`, `long-running-queries`, and `vacuum-stats`. To run every diagnostic and write one comma-separated values (CSV) file for each to the directory in `--output-dir`, use `aws amplifygen3 db report`.

To use a branch other than the environment’s backend branch, add `--branch-arn branch-arn `.

The following AWS CLI commands inspect table statistics and write a report:

```bash
aws amplifygen3 db inspect table-stats --env environment-name
aws amplifygen3 db report --output-dir db-report --env environment-name
```

For the BCU-hours that your backend used this month, see [Capacity](backend-capacity.md).