*** DRAFT ***
The SQLITE_STMT Virtual Table

1. Overview

The SQLITE_STMT extension implements an eponymous-only virtual table that provides information about all prepared statements associated with the database connection.

The SQLITE_STMT extension is included in the amalgamation though it is disabled by default. Use the SQLITE_ENABLE_STMTVTAB compile-time option to enable the SQLITE_STMT extension. The SQLITE_STMT extension can also be loaded at run-time by compiling the extension into a shared library or DLL using the source code at https://sqlite.org/src/file/ext/misc/stmt.c and following the instructions for how to compile loadable extensions.

The SQLITE_STMT extension is enabled in default builds of the command-line shell.

2. Usage

The SQLITE_STMT virtual table is a read-only table that can be directly queried to access information about all prepared statements on the current database connection. For example:

SELECT * FROM sqlite_stmt;

A statement such as the above can be run immediately prior to invoking sqlite3_close() to confirm that all prepared statements have been finalized and to help identify and track down prepared statements that have "leaked" and missed finalization.

The SQLITE_STMT virtual table can also be used to access performance information about prepared statements, to aid in optimization an application. For example, to find out how much memory is being used by prepared statements that have never been used, one could run:

SELECT sum(mem) FROM sqlite_stmt WHERE run=0;

2.1. Columns

The columns are provided by the SQLITE_STMT virtual table are summarized by the hypothetical CREATE TABLE statement show here:

CREATE TABLE sqlite_stmt(
  sql    TEXT,    -- Original SQL text
  ncol   INT,     -- Number of output columns
  ro     BOOLEAN, -- True for "read only" statements
  busy   BOOLEAN, -- True if the statement is current running
  nscan  INT,     -- Number of full-scan steps
  nsort  INT,     -- Number of sort operations
  naidx  INT,     -- Number of automatic index inserts
  nstep  INT,     -- Number of byte-code engine steps
  reprep INT,     -- Number of reprepare operations
  run    INT,     -- Number of times this statement has been run
  mem    INT      -- Heap memory used by this statement
);

Future releases may add new output columns and may change the order of legacy columns. Further detail about the meaning of each column in the SQLITE_STMT virtual table is provided below:

This page last modified on 2022-01-08 05:02:57 UTC

*** DRAFT ***