# Query metadata

Bolt protocol specifies additional data that can be sent along with the
requested results. Such data is called metadata and can be divided into two
groups:
 - query statistics
 - notifications

Both of these metadata can be accessed through `summary` map that is being sent
along with the results of the query. Query statistics will be under `stats` key,
and notifications under `notifications` key.

## Query statistics

Query statistics are sent whenever a user executes a query that affect data in
any way. In other words, Memgraph tracks the quantity of these changes
throughout the query execution and reports it back to the user.

The structure of statistics is a map of string keys and integer values. Data
that is being tracked:

 - `nodes-created`
 - `nodes-deleted`
 - `relationships-created`
 - `relationships-deleted`
 - `labels-added`
 - `labels-removed`
 - `properties-set`

This data will refer only to the changes done by the query, thus changes made in
triggers will not affect these values.

> **Warning**
>
> Differences compared to triggers
>
> It is possible that after executing a query some of these counters are not
> zero, however the regarding triggers are not triggered. The reason for that is
> triggers are only  triggered when there is a difference between the starting
> and ending state, while the counters are also counting the not permanent
> changes.
>
> For example if the query creates and deletes nodes like
> `CREATE (n) DELETE n;`, then it leaves Memgraph in the same state as before.
> The value will be 1 for both `nodes-created` and `nodes-deleted`, but the
> triggers will not be triggered since there is no difference between in
> states before and after the query is executed.

## Notifications

Notifications will be sent whether we want to confirm the results of query or
want to notify the user about possible wrong usage. Every notification is
represented as a dictionary with these possible values:

Key|Value Type
:-:|:-:
severity|String|/
code|String|/
title|String|/
description|String|/

In order to enable users to handle these notifications however they see fit, we
will introduce possible values for severity and code notifications attributes.
Title and description values will depend on query and the query values and
should be used only as messages.

### Severity

 - `INFO`
 - `WARNING`

### Code

- `CreateConstraint`
- `CreateIndex`
- `CreateStream`
- `CheckStream`
- `CreateTrigger`
- `DropConstraint`
- `DropReplica`
- `DropIndex`
- `DropStream`
- `DropTrigger`
- `ConstraintAlreadyExists`
- `IndexAlreadyExists`
- `LoadCSVTip`
- `IndexDoesNotExist`
- `ConstraintDoesNotExist`
- `RegisterReplica`
- `ReplicaPortWarning`
- `SetReplica`
- `SyncReplicationFailure`
- `StartStream`
- `StartAllStreams`
- `StopStream`
- `StopAllStreams`

### Commit-time notifications

Most notifications are produced while the query is being executed, but a
notification can also be produced by the commit itself. The
`SyncReplicationFailure` notification is reported when a transaction committed
on the MAIN instance but couldn't be replicated to every SYNC replica.

Because it is produced by the commit, it is delivered:

- in the summary of the last query of an implicit (autocommit) transaction, and
- in the summary of the explicit `COMMIT` query, when you manage transactions
  yourself.

```
{
  "severity": "WARNING",
  "code": "SyncReplicationFailure",
  "title": "Failed to replicate to SYNC replica 'instance_1': replica is not reachable or not in sync with the main. Replica will be recovered automatically. Transaction is still committed on the main instance and other alive replicas. Check the status of the replicas using 'SHOW REPLICAS' query.",
  "description": ""
}
```

> **Info**
>
> A `SyncReplicationFailure` notification means the write **succeeded**. When a
> transaction is actually rolled back everywhere, which happens when a
> STRICT_SYNC replica can't confirm it, Memgraph raises a `ReplicationException`
> instead. For details, see [replication
> errors](https://memgraph.com/docs/help-center/errors/replication#error-4).
