# MD_GET_FLIGHT_LOGS
> Read the stdout and stderr captured during a Flight run, one row per log line.
Returns the captured logs for a single [Flight](/concepts/flights) run, one row per line. The output combines stdout and stderr in the order the runtime captured them, and a window of lines can be selected with `"LIMIT"`, `"OFFSET"`, and `"ORDER"` instead of fetching everything.

## Syntax

```sql
SELECT * FROM MD_GET_FLIGHT_LOGS(
    flight_id := '<flight_id>',
    run_number := <n>,
    "LIMIT" := <n>,
    "OFFSET" := <n>,
    "ORDER" := 'asc'
);
```

## Parameters

| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
| `flight_id` | `UUID` | Yes | | Identifier of the Flight. |
| `run_number` | `UBIGINT` | Yes | | The run number to fetch logs for. |
| `LIMIT` | `UINTEGER` | No | (all lines) | Maximum number of lines to return. Must be at least `1` when set. |
| `OFFSET` | `UINTEGER` | No | `0` | Skip this many lines, counted from the end `ORDER` reads from. Requires `LIMIT`. |
| `ORDER` | `VARCHAR` | No | `'asc'` | Which end of the log the window is taken from: `'asc'` selects from the first line, `'desc'` from the last (the tail). |

`LIMIT`, `OFFSET`, and `ORDER` are SQL keywords and must be quoted when used as named arguments.

## Return columns

| Column | Type | Description |
|---|---|---|
| `line_number` | `BIGINT` | Position of the line in the run's output, starting at `1`, or `NULL` when the runtime reported none. |
| `reported_at` | `TIMESTAMP WITH TIME ZONE` | When the runtime captured the line, or `NULL` when unreported. |
| `line` | `VARCHAR` | The log line. |

Sort by `line_number` for a stable reading order regardless of `"ORDER"`.

## Behavior

- Returns an error when no run with the given `run_number` exists for the Flight, or when the Flight itself doesn't exist.
- Available for runs in any terminal status (`SUCCEEDED`, `FAILED`, `CANCELLED`) and during a `RUNNING` run.
- Parameters must be literals or `getvariable()` calls. Subqueries and lateral join columns fail with a binder error; store dynamic values with `SET VARIABLE` first.
- Clients on DuckDB versions before 1.5.5 bind this function's previous schema instead: a single row with one `logs VARCHAR` column holding the full combined output, without the line window parameters.

## Examples

Tail the last 100 lines of a run:

```sql
SELECT line_number, line
FROM MD_GET_FLIGHT_LOGS(
    flight_id := '<flight_id>',
    run_number := 42,
    "LIMIT" := 100,
    "ORDER" := 'desc'
)
ORDER BY line_number;
```

Read the latest run's full logs:

```sql
SET VARIABLE latest_run_number = (
    SELECT max(run_number)
    FROM MD_LIST_FLIGHT_RUNS(flight_id := '<flight_id>')
);

SELECT line_number, reported_at, line
FROM MD_GET_FLIGHT_LOGS(
    flight_id := '<flight_id>',
    run_number := getvariable('latest_run_number')
)
ORDER BY line_number;
```

Find errors in a run:

```sql
SELECT line_number, line
FROM MD_GET_FLIGHT_LOGS(flight_id := '<flight_id>', run_number := 42)
WHERE line ILIKE '%error%'
ORDER BY line_number;
```

## Related

- [`MD_LIST_FLIGHT_RUNS`](../md-list-flight-runs) — Find the run number to read logs for.
- [`MD_GET_FLIGHT_RUN`](../md-get-flight-run) — Fetch one run's status directly.
- [`MD_RUN_FLIGHT`](../md-run-flight) — Trigger an on-demand run.
- [`get_flight_run_logs` MCP tool](/sql-reference/mcp/) — AI-agent equivalent, with a `max_bytes` cap.


---

## Docs feedback

MotherDuck accepts optional user-submitted feedback about this page at `GET https://motherduck.com/docs/api/feedback/agent`.
For agents and automated tools, feedback submission should be user-confirmed before sending.

URL-encode query parameter values and send a GET request:

```text
GET https://motherduck.com/docs/api/feedback/agent?page_path=%2Fsql-reference%2Fmotherduck-sql-reference%2Fflights%2Fmd-get-flight-logs%2F&page_title=MD_GET_FLIGHT_LOGS&text=<url-encoded user feedback, max 2000 characters>
```

Optionally append `&source=<url-encoded interface identifier>` such as `claude.ai` or `chatgpt`.

`page_path` and `text` are required; `page_title` and `source` are optional. Responses: `200 {"feedback_id": "<uuid>"}`, `400` for malformed query parameters, and `429` when rate-limited.
