Repository navigation
feat(query): SHOW/TERMINATE SESSIONS #1814
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: release/3.14
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -92,7 +92,7 @@ SHOW TRANSACTIONS; | |||||
| ``` | ||||||
|
|
||||||
| Each row in the result represents one transaction (or an in-progress snapshot | ||||||
| creation or garbage collection) and contains seven columns: | ||||||
| creation or garbage collection) and contains eight columns: | ||||||
|
|
||||||
| | Column | Type | Description | | ||||||
| |---|---|---| | ||||||
|
|
@@ -103,6 +103,7 @@ creation or garbage collection) and contains seven columns: | |||||
| | `metadata` | `Map` | Metadata supplied by the client when the transaction was opened. For in-progress snapshots and garbage collection it contains progress details (see below). | | ||||||
| | `start_time` | `ZonedDateTime` | UTC time at which the transaction started. | | ||||||
| | `elapsed_ms` | `Integer` | How long the transaction has been running, in milliseconds. | | ||||||
| | `database` | `String` | The database the transaction is running on. | | ||||||
|
|
||||||
| ```copy=false | ||||||
| memgraph> SHOW TRANSACTIONS; | ||||||
|
|
@@ -337,8 +338,8 @@ following the same rules as the id list form: | |||||
| The wildcard must be the only argument. Mixing it with ids, such as | ||||||
| `TERMINATE TRANSACTIONS "*", "9223372036854794885"`, raises an error. | ||||||
|
|
||||||
| A parameterized id is treated exactly like a literal one, so running | ||||||
| `TERMINATE TRANSACTIONS $id` with `$id = "*"` also terminates everything. | ||||||
| Transaction ids, including `"*"`, must be string literals. Query parameters | ||||||
| such as `$id` aren't supported and cause a syntax error. | ||||||
|
|
||||||
| <Callout type="info"> | ||||||
| System transactions (for example an in-flight `CREATE DATABASE` or `GRANT`) are | ||||||
|
|
@@ -439,6 +440,134 @@ Client received exception: Transactions was asked to abort either because it was | |||||
| specified or another user asked to abort it. | ||||||
| ``` | ||||||
|
|
||||||
| ## Manage sessions | ||||||
|
katarinasupe marked this conversation as resolved.
|
||||||
|
|
||||||
| A session is one client connection to Memgraph over Bolt. You can list the | ||||||
| open sessions and close any of them from another connection, for example an | ||||||
| idle (often pooled) connection that still has a database selected and makes | ||||||
| [`DROP DATABASE`](/database-management/multi-tenancy#drop-database-with-force) | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| fail with `Cannot delete <name>, it is currently being used.` Available since | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| Memgraph 3.14. | ||||||
|
|
||||||
| Session queries run on data instances (MAIN and REPLICA), and each instance | ||||||
| lists and closes only the sessions connected to it. On a high availability | ||||||
| coordinator they fail with `Coordinator can run only coordinator queries!`. | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| They run as implicit transactions only; running them inside an explicit | ||||||
| transaction raises an error. | ||||||
|
Comment on lines
+455
to
+456
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the old days, I would've changed this to "...only, so running them..." or even better "..., so don't run them inside explicit transactions (such as: ...) " What are explicit transactions? bo - might be good to have an example, explanation? |
||||||
|
|
||||||
| ### Show sessions | ||||||
|
|
||||||
| To list the open sessions, run: | ||||||
|
|
||||||
| ```cypher | ||||||
| SHOW SESSIONS; | ||||||
| ``` | ||||||
|
|
||||||
| Each row is one session that is currently logged in. A connection that hasn't | ||||||
| logged in yet, or that has logged off, isn't listed. After it logs on again it | ||||||
| reappears with the same `session_id`: | ||||||
|
|
||||||
| | Column | Type | Description | | ||||||
| |---|---|---| | ||||||
| | `session_id` | `String` | Unique id of the session. Use this value with `TERMINATE SESSIONS`. | | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "'TERMINATE SESSIONS' query to terminate it"? |
||||||
| | `username` | `String` | The user the session logged in as, or `""` if authentication is disabled. With impersonation, this is still the login user. | | ||||||
| | `database` | `String` | The database the session is using, or `""` if it isn't using one. | | ||||||
| | `login_timestamp` | `String` | UTC time of the session's most recent login, formatted as `YYYY-MM-DD HH:MM:SS.ffffff` (no time zone suffix). | | ||||||
|
|
||||||
| You see your own sessions, and the sessions on every database where you have | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| the `TRANSACTION_MANAGEMENT` privilege. A session that isn't using a | ||||||
| database counts as using the default database, `memgraph`. When | ||||||
| authentication is disabled, every connection counts as the same user, so you | ||||||
| see all sessions. With [user | ||||||
| impersonation](/database-management/authentication-and-authorization/impersonate-user), | ||||||
| a connection counts as the impersonated user, and its queries are checked | ||||||
| against that user's privileges. | ||||||
|
|
||||||
| In the following example, `admin` has `TRANSACTION_MANAGEMENT` on every | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| database, and `alice` has two idle connections open, one on `analytics` and | ||||||
| one on `memgraph`. The session running the query is listed too: | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
|
||||||
| ```copy=false | ||||||
| memgraph> SHOW SESSIONS; | ||||||
| +----------------------------------------+----------+-------------+------------------------------+ | ||||||
| | session_id | username | database | login_timestamp | | ||||||
| +----------------------------------------+----------+-------------+------------------------------+ | ||||||
| | "eba078f8-2c56-4e22-bf47-4545ac9d4b1a" | "admin" | "memgraph" | "2026-10-07 10:30:33.022574" | | ||||||
| | "e9e40294-db31-4122-9875-75affefc1130" | "alice" | "memgraph" | "2026-10-07 10:30:32.391885" | | ||||||
| | "f9892d9b-ca91-4e16-b3df-1ead198cc846" | "alice" | "analytics" | "2026-10-07 10:30:32.228485" | | ||||||
| +----------------------------------------+----------+-------------+------------------------------+ | ||||||
| 3 rows in set (round trip in 0.001 sec) | ||||||
| ``` | ||||||
|
|
||||||
| ### Terminate sessions | ||||||
|
|
||||||
| To close one or more sessions, run the following query from another | ||||||
| connection: | ||||||
|
|
||||||
| ```plaintext | ||||||
| TERMINATE SESSIONS 'SESSION_ID' [, 'SESSION_ID' ...]; | ||||||
| ``` | ||||||
|
katarinasupe marked this conversation as resolved.
|
||||||
|
|
||||||
| `SESSION_ID` is a `session_id` from `SHOW SESSIONS`, written as a string | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| literal. Query parameters such as `$id` aren't supported and cause a syntax | ||||||
| error. Any other literal value raises the error `Session id must be a string.` | ||||||
|
|
||||||
| An idle session is closed immediately. For a session that is running a query, | ||||||
| Memgraph aborts its transaction and closes the connection as soon as that | ||||||
| query returns. Most queries stop right away, but one that doesn't check for | ||||||
| termination (for example, a long-running function call) runs until it | ||||||
| finishes, and its transaction is then not committed. If the session has an | ||||||
| open explicit transaction (it ran `BEGIN` but hasn't committed), that | ||||||
| transaction is rolled back: its uncommitted changes are discarded and any | ||||||
| writes it holds are released, so conflicting transactions can proceed. A | ||||||
| transaction that is already committing isn't interrupted; it completes. | ||||||
|
Comment on lines
+515
to
+523
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is very hard for me to grasp all the information it's trying to convey. I would need a bullet point list. This makes it easier to understand what IF IF ELSE structure is happening here When I'm reading this block of text I'm unsure where one scenario ends and another one ends The termination of the session depends on the session type:
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I also feel like this paragraph, and the two after it require some kind of subtitle or intro... so this is describing the termination behavior based on session type below is Client behavior upon session termination and i guess i would first explain how to terminate all kinds of sessions, mine or other ppls, then explain behaviour? |
||||||
|
|
||||||
| The client of a terminated session loses its connection. A query it is | ||||||
| running fails with the transient error `Transaction was asked to abort by | ||||||
| another user.` An idle client gets a connection error on its next request. | ||||||
| Because the error is transient, driver retry helpers (for example, the Python | ||||||
| driver's `execute_write`) retry the transaction on a new connection, and the | ||||||
| retry can commit, so terminating a session doesn't stop an application that | ||||||
| uses them from running the same work again. | ||||||
|
|
||||||
|
katarinasupe marked this conversation as resolved.
|
||||||
| You can terminate your own other sessions without any privilege. To terminate | ||||||
| another user's session, you need the `TRANSACTION_MANAGEMENT` privilege on | ||||||
| the database that session is using (the default database, `memgraph`, if it | ||||||
| isn't using one). The session that runs `TERMINATE SESSIONS` can't terminate | ||||||
| itself. | ||||||
|
katarinasupe marked this conversation as resolved.
|
||||||
|
|
||||||
| <Callout type="info"> | ||||||
| These privilege checks apply only in Memgraph Enterprise. In Memgraph | ||||||
| Community, every user has all privileges, so any user can see and terminate | ||||||
| any other user's session. | ||||||
| </Callout> | ||||||
|
|
||||||
| The result has two columns, `session_id` and `killed`, with one row per id in | ||||||
| the query, in the same order. A row reports `killed: true` if the session was | ||||||
| found and you were allowed to close it. This means the close was requested: a | ||||||
| session that is busy with a query can still appear in `SHOW SESSIONS` until | ||||||
| that query returns. An id that matches no session, an id you're not allowed to | ||||||
| terminate, the id of the session running the query, and every repeat of an id | ||||||
| already in the list all report `killed: false`. | ||||||
|
|
||||||
| For example, to close alice's `analytics` session (the second id doesn't | ||||||
| match any session): | ||||||
|
|
||||||
| ```cypher | ||||||
| TERMINATE SESSIONS 'f9892d9b-ca91-4e16-b3df-1ead198cc846', '00000000-0000-0000-0000-000000000000'; | ||||||
| ``` | ||||||
|
|
||||||
| ```copy=false | ||||||
| memgraph> TERMINATE SESSIONS 'f9892d9b-ca91-4e16-b3df-1ead198cc846', '00000000-0000-0000-0000-000000000000'; | ||||||
| +----------------------------------------+--------+ | ||||||
| | session_id | killed | | ||||||
| +----------------------------------------+--------+ | ||||||
| | "f9892d9b-ca91-4e16-b3df-1ead198cc846" | true | | ||||||
| | "00000000-0000-0000-0000-000000000000" | false | | ||||||
| +----------------------------------------+--------+ | ||||||
| 2 rows in set (round trip in 0.001 sec) | ||||||
| ``` | ||||||
|
|
||||||
| ## Isolation levels | ||||||
|
|
||||||
| In database systems, isolation determines how transaction integrity is visible | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In general, for me to be able to understand what this sentence was trying to convey, I had to go to /database-management/authentication-and-authorization/role-based-access-control to understand what does "With 'TRANSACTION_MANAGEMENT'" even mean.
This is maybe a comment for a wider revamp, but here it helped to even mention this is a privilege of some kind.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe the suggestion I'm trying to make is to add a descriptive noun when mentioning a part of the code
'this()' function
'THIS' clause
'THIS' privilege
'--this' flag