Do AI Agents Need a Semantic Layer?Livestream August 26

Skip to main content

QUERY_HISTORY view

Requires permissionBusiness planRoles and access control →
View query historyAdmin ✓Builder ✓Explorer —

Marked preset roles include the permission by default; a custom role qualifies when it inherits a role that includes it.

tip

The Duckling overview page (Settings → Duckling overview) gives a 24-hour summary of activity across every Duckling in the organization and requires the separate View Duckling overview permission. See Duckling overview for details.

You can monitor and cancel running queries in the MotherDuck UI under Settings → Running Queries. See Running Queries for details.

The MD_INFORMATION_SCHEMA.QUERY_HISTORY view provides a consolidated view of all queries run across an organization.

Retention

Query history is retained indefinitely. This retention policy may change in the future. If you depend on long-term query history, we recommend periodically exporting it to your own storage.

Schema​

When you query the MD_INFORMATION_SCHEMA.QUERY_HISTORY view, the query results contain one row for each query that was run in the organization. Note that the information in this view will have some delays. A more realtime view of ongoing and completed queries that have not been captured in QUERY_HISTORY yet is provided through the MD_INFORMATION_SCHEMA.RECENT_QUERIES view.

The MD_INFORMATION_SCHEMA.QUERY_HISTORY view has the following schema:

Column NameData TypeValue
QUERY_IDUUIDA unique ID representing the particular query run. You can retrieve this value on the client with MD_LAST_QUERY_ID().
QUERY_TEXTSTRINGQuery SQL text (up to 100k chars)
START_TIMETIMESTAMPTZStart time of the query
END_TIMETIMESTAMPTZEnd time of the query
EXECUTION_TIMEINTERVALDuration where the query is actively executing
WAIT_TIMEINTERVALDuration where the query is waiting on resources to become available. For example, a query may need to wait because other queries are using all available execution threads. Alternatively, a query may be waiting on data to become available (in case of data upload). Wait time is a useful signal for query optimization or uncovering workloads that could benefit from scaling compute horizontally with more ducklings or vertically with larger ducklings.
TOTAL_ELAPSED_TIMEINTERVALTotal duration of the query
ERROR_MESSAGESTRINGError message, if the query returned an error
ERROR_TYPESTRINGError type, if the query returned an error
USER_AGENTSTRINGUser agent of the client
USER_NAMESTRINGIdentifier for the MotherDuck user in their organization
QUERY_NRUBIGINTID of the query within the transaction that ran the query. Number that just increments for each query that is run within a given transaction
TRANSACTION_NRUBIGINTID of the transaction that contained the query. Number that just increments for each new transaction on a given connection
CONNECTION_IDUUIDUnique ID for the client DuckDB connection where the query was issued
DUCKDB_IDUUIDUnique ID for the client DuckDB instance where the query was issued
DUCKDB_VERSIONSTRINGClient DuckDB version that issued the query
INSTANCE_TYPESTRINGThe size of Duckling that the query was run on (Pulse / Standard / Jumbo / Mega / Giga / ...)
QUERY_TYPESTRINGThe nature of the query (DDL / DML / QUERY / ...)
BYTES_UPLOADEDUBIGINTNumber of bytes uploaded from client to server (relevant for hybrid queries)
BYTES_DOWNLOADEDUBIGINTNumber of bytes downloaded from server to client (relevant for hybrid queries)
BYTES_SPILLED_TO_DISKUBIGINTTotal number of bytes spilled to disk for "larger than in-memory" workloads
DUCKLING_IDSTRINGIdentifies the duckling that ran the query. It is composed of the user name and a qualifier (rw for read-write ducklings, or rs.0, rs.1, ... for the respective read scaling duckling)
SESSION_NAMESTRINGThe session_name that was supplied when connecting to MotherDuck

The fields START_TIME, END_TIME, TOTAL_ELAPSED_TIME, ERROR_MESSAGE, and ERROR_TYPE are captured on the server (that is, when query starts and ends on server). In the future they will be based on client information too (taking better into account the full hybrid context).

Example usage​

from MD_INFORMATION_SCHEMA.QUERY_HISTORY limit 10;