LAST_KAFKA_MESSAGE_ID()

LAST_KAFKA_MESSAGE_ID() returns the Kafka offset of the last message a completed Kafka external-table scan returned in this session, or NULL before any scan. Feeding it back as the next __mo_read_start_id chains reads exactly once.

Description

LAST_KAFKA_MESSAGE_ID() is a session builtin that reads back the progress of the most recent completed Kafka external-table scan. The value is plain session state and does not survive a proxy connection migration, where the builtin then returns NULL until the next scan.

Syntax

SELECT LAST_KAFKA_MESSAGE_ID();

Arguments

LAST_KAFKA_MESSAGE_ID() takes no arguments. It reads the session state recorded by the most recent completed Kafka external-table scan.

Usage Notes

  • Returns NULL before any completed Kafka scan in the session.

  • With autocommit=false, using the value as the next __mo_read_start_id reads the messages after the last returned offset, giving exactly-once consumption.

  • Progress publishes only when the whole statement succeeds; an aborted or failed statement never advances the value.

Examples

The following example shows the NULL-before-scan value without a Kafka broker:

DROP DATABASE IF EXISTS kafka_id_demo;
CREATE DATABASE kafka_id_demo;
USE kafka_id_demo;

SELECT last_kafka_message_id();

DROP DATABASE kafka_id_demo;

Chaining reads requires a reachable broker, so it is shown as a syntax template rather than a paste-and-run script:

SELECT * FROM kt WHERE __mo_read_start_id = 1000 AND __mo_read_size = 100000;
SELECT last_kafka_message_id();   -- e.g. 4711
SELECT * FROM kt WHERE __mo_read_start_id = 4711;  -- continues after

See Also