CINXE.COM
sql - chromium/src - Git at Google
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><title>sql - chromium/src - Git at Google</title><link rel="stylesheet" type="text/css" href="/+static/base.css"><link rel="stylesheet" type="text/css" href="/+static/doc.css"><link rel="stylesheet" type="text/css" href="/+static/prettify/prettify.css"><!-- default customHeadTagPart --></head><body class="Site"><header class="Site-header"><div class="Header"><a class="Header-image" href="/"><img src="//www.gstatic.com/images/branding/lockups/2x/lockup_git_color_108x24dp.png" width="108" height="24" alt="Google Git"></a><div class="Header-menu"> <a class="Header-menuItem" href="https://accounts.google.com/AccountChooser?faa=1&service=gerritcodereview&continue=https://chromium.googlesource.com/login/chromium/src/%2B/refs/tags/133.0.6850.2/sql">Sign in</a> </div></div></header><div class="Site-content"><div class="Container "><div class="Breadcrumbs"><a class="Breadcrumbs-crumb" href="/?format=HTML">chromium</a> / <a class="Breadcrumbs-crumb" href="/chromium/">chromium</a> / <a class="Breadcrumbs-crumb" href="/chromium/src/">src</a> / <a class="Breadcrumbs-crumb" href="/chromium/src/+/refs/tags/133.0.6850.2">refs/tags/133.0.6850.2</a> / <a class="Breadcrumbs-crumb" href="/chromium/src/+/refs/tags/133.0.6850.2/">.</a> / <span class="Breadcrumbs-crumb">sql</span></div><div class="TreeDetail"><div class="u-sha1 u-monospace TreeDetail-sha1">tree: a4016364087c63bd6e8d46363172b239542cf02d [<a href="/chromium/src/+log/refs/tags/133.0.6850.2/sql">path history</a>] <span>[<a href="/chromium/src/+archive/refs/tags/133.0.6850.2/sql.tar.gz">tgz</a>]</span></div><ol class="FileList"><li class="FileList-item FileList-item--gitTree" title="Tree - fuzzers/"><a class="FileList-itemLink" href="/chromium/src/+/refs/tags/133.0.6850.2/sql/fuzzers/">fuzzers/</a></li><li class="FileList-item FileList-item--gitTree" title="Tree - test/"><a class="FileList-itemLink" href="/chromium/src/+/refs/tags/133.0.6850.2/sql/test/">test/</a></li><li class="FileList-item FileList-item--regularFile" title="Regular file - .clang-tidy"><a class="FileList-itemLink" href="/chromium/src/+/refs/tags/133.0.6850.2/sql/.clang-tidy">.clang-tidy</a></li><li class="FileList-item FileList-item--regularFile" title="Regular file - .clangd"><a class="FileList-itemLink" href="/chromium/src/+/refs/tags/133.0.6850.2/sql/.clangd">.clangd</a></li><li class="FileList-item FileList-item--regularFile" title="Regular file - BUILD.gn"><a class="FileList-itemLink" href="/chromium/src/+/refs/tags/133.0.6850.2/sql/BUILD.gn">BUILD.gn</a></li><li class="FileList-item FileList-item--regularFile" title="Regular file - database.cc"><a class="FileList-itemLink" href="/chromium/src/+/refs/tags/133.0.6850.2/sql/database.cc">database.cc</a></li><li class="FileList-item FileList-item--regularFile" title="Regular file - database.h"><a class="FileList-itemLink" href="/chromium/src/+/refs/tags/133.0.6850.2/sql/database.h">database.h</a></li><li class="FileList-item FileList-item--regularFile" title="Regular file - database_memory_dump_provider.cc"><a class="FileList-itemLink" href="/chromium/src/+/refs/tags/133.0.6850.2/sql/database_memory_dump_provider.cc">database_memory_dump_provider.cc</a></li><li class="FileList-item FileList-item--regularFile" title="Regular file - database_memory_dump_provider.h"><a class="FileList-itemLink" href="/chromium/src/+/refs/tags/133.0.6850.2/sql/database_memory_dump_provider.h">database_memory_dump_provider.h</a></li><li class="FileList-item FileList-item--regularFile" title="Regular file - database_options_unittest.cc"><a class="FileList-itemLink" href="/chromium/src/+/refs/tags/133.0.6850.2/sql/database_options_unittest.cc">database_options_unittest.cc</a></li><li class="FileList-item FileList-item--regularFile" title="Regular file - database_unittest.cc"><a class="FileList-itemLink" href="/chromium/src/+/refs/tags/133.0.6850.2/sql/database_unittest.cc">database_unittest.cc</a></li><li class="FileList-item FileList-item--regularFile" title="Regular file - DEPS"><a class="FileList-itemLink" href="/chromium/src/+/refs/tags/133.0.6850.2/sql/DEPS">DEPS</a></li><li class="FileList-item FileList-item--regularFile" title="Regular file - DIR_METADATA"><a class="FileList-itemLink" href="/chromium/src/+/refs/tags/133.0.6850.2/sql/DIR_METADATA">DIR_METADATA</a></li><li class="FileList-item FileList-item--regularFile" title="Regular file - error_delegate_util.cc"><a class="FileList-itemLink" href="/chromium/src/+/refs/tags/133.0.6850.2/sql/error_delegate_util.cc">error_delegate_util.cc</a></li><li class="FileList-item FileList-item--regularFile" title="Regular file - error_delegate_util.h"><a class="FileList-itemLink" href="/chromium/src/+/refs/tags/133.0.6850.2/sql/error_delegate_util.h">error_delegate_util.h</a></li><li class="FileList-item FileList-item--regularFile" title="Regular file - init_status.h"><a class="FileList-itemLink" href="/chromium/src/+/refs/tags/133.0.6850.2/sql/init_status.h">init_status.h</a></li><li class="FileList-item FileList-item--regularFile" title="Regular file - initialization.cc"><a class="FileList-itemLink" href="/chromium/src/+/refs/tags/133.0.6850.2/sql/initialization.cc">initialization.cc</a></li><li class="FileList-item FileList-item--regularFile" title="Regular file - initialization.h"><a class="FileList-itemLink" href="/chromium/src/+/refs/tags/133.0.6850.2/sql/initialization.h">initialization.h</a></li><li class="FileList-item FileList-item--regularFile" title="Regular file - internal_api_token.h"><a class="FileList-itemLink" href="/chromium/src/+/refs/tags/133.0.6850.2/sql/internal_api_token.h">internal_api_token.h</a></li><li class="FileList-item FileList-item--regularFile" title="Regular file - meta_table.cc"><a class="FileList-itemLink" href="/chromium/src/+/refs/tags/133.0.6850.2/sql/meta_table.cc">meta_table.cc</a></li><li class="FileList-item FileList-item--regularFile" title="Regular file - meta_table.h"><a class="FileList-itemLink" href="/chromium/src/+/refs/tags/133.0.6850.2/sql/meta_table.h">meta_table.h</a></li><li class="FileList-item FileList-item--regularFile" title="Regular file - meta_table_unittest.cc"><a class="FileList-itemLink" href="/chromium/src/+/refs/tags/133.0.6850.2/sql/meta_table_unittest.cc">meta_table_unittest.cc</a></li><li class="FileList-item FileList-item--regularFile" title="Regular file - OWNERS"><a class="FileList-itemLink" href="/chromium/src/+/refs/tags/133.0.6850.2/sql/OWNERS">OWNERS</a></li><li class="FileList-item FileList-item--regularFile" title="Regular file - README.md"><a class="FileList-itemLink" href="/chromium/src/+/refs/tags/133.0.6850.2/sql/README.md">README.md</a></li><li class="FileList-item FileList-item--regularFile" title="Regular file - recovery.cc"><a class="FileList-itemLink" href="/chromium/src/+/refs/tags/133.0.6850.2/sql/recovery.cc">recovery.cc</a></li><li class="FileList-item FileList-item--regularFile" title="Regular file - recovery.h"><a class="FileList-itemLink" href="/chromium/src/+/refs/tags/133.0.6850.2/sql/recovery.h">recovery.h</a></li><li class="FileList-item FileList-item--regularFile" title="Regular file - recovery_unittest.cc"><a class="FileList-itemLink" href="/chromium/src/+/refs/tags/133.0.6850.2/sql/recovery_unittest.cc">recovery_unittest.cc</a></li><li class="FileList-item FileList-item--regularFile" title="Regular file - sandboxed_vfs.cc"><a class="FileList-itemLink" href="/chromium/src/+/refs/tags/133.0.6850.2/sql/sandboxed_vfs.cc">sandboxed_vfs.cc</a></li><li class="FileList-item FileList-item--regularFile" title="Regular file - sandboxed_vfs.h"><a class="FileList-itemLink" href="/chromium/src/+/refs/tags/133.0.6850.2/sql/sandboxed_vfs.h">sandboxed_vfs.h</a></li><li class="FileList-item FileList-item--regularFile" title="Regular file - sandboxed_vfs_file.cc"><a class="FileList-itemLink" href="/chromium/src/+/refs/tags/133.0.6850.2/sql/sandboxed_vfs_file.cc">sandboxed_vfs_file.cc</a></li><li class="FileList-item FileList-item--regularFile" title="Regular file - sandboxed_vfs_file.h"><a class="FileList-itemLink" href="/chromium/src/+/refs/tags/133.0.6850.2/sql/sandboxed_vfs_file.h">sandboxed_vfs_file.h</a></li><li class="FileList-item FileList-item--regularFile" title="Regular file - sql_features.cc"><a class="FileList-itemLink" href="/chromium/src/+/refs/tags/133.0.6850.2/sql/sql_features.cc">sql_features.cc</a></li><li class="FileList-item FileList-item--regularFile" title="Regular file - sql_features.h"><a class="FileList-itemLink" href="/chromium/src/+/refs/tags/133.0.6850.2/sql/sql_features.h">sql_features.h</a></li><li class="FileList-item FileList-item--regularFile" title="Regular file - sql_memory_dump_provider.cc"><a class="FileList-itemLink" href="/chromium/src/+/refs/tags/133.0.6850.2/sql/sql_memory_dump_provider.cc">sql_memory_dump_provider.cc</a></li><li class="FileList-item FileList-item--regularFile" title="Regular file - sql_memory_dump_provider.h"><a class="FileList-itemLink" href="/chromium/src/+/refs/tags/133.0.6850.2/sql/sql_memory_dump_provider.h">sql_memory_dump_provider.h</a></li><li class="FileList-item FileList-item--regularFile" title="Regular file - sql_memory_dump_provider_unittest.cc"><a class="FileList-itemLink" href="/chromium/src/+/refs/tags/133.0.6850.2/sql/sql_memory_dump_provider_unittest.cc">sql_memory_dump_provider_unittest.cc</a></li><li class="FileList-item FileList-item--regularFile" title="Regular file - sqlite_features_unittest.cc"><a class="FileList-itemLink" href="/chromium/src/+/refs/tags/133.0.6850.2/sql/sqlite_features_unittest.cc">sqlite_features_unittest.cc</a></li><li class="FileList-item FileList-item--regularFile" title="Regular file - sqlite_result_code.cc"><a class="FileList-itemLink" href="/chromium/src/+/refs/tags/133.0.6850.2/sql/sqlite_result_code.cc">sqlite_result_code.cc</a></li><li class="FileList-item FileList-item--regularFile" title="Regular file - sqlite_result_code.h"><a class="FileList-itemLink" href="/chromium/src/+/refs/tags/133.0.6850.2/sql/sqlite_result_code.h">sqlite_result_code.h</a></li><li class="FileList-item FileList-item--regularFile" title="Regular file - sqlite_result_code_unittest.cc"><a class="FileList-itemLink" href="/chromium/src/+/refs/tags/133.0.6850.2/sql/sqlite_result_code_unittest.cc">sqlite_result_code_unittest.cc</a></li><li class="FileList-item FileList-item--regularFile" title="Regular file - sqlite_result_code_values.cc"><a class="FileList-itemLink" href="/chromium/src/+/refs/tags/133.0.6850.2/sql/sqlite_result_code_values.cc">sqlite_result_code_values.cc</a></li><li class="FileList-item FileList-item--regularFile" title="Regular file - sqlite_result_code_values.h"><a class="FileList-itemLink" href="/chromium/src/+/refs/tags/133.0.6850.2/sql/sqlite_result_code_values.h">sqlite_result_code_values.h</a></li><li class="FileList-item FileList-item--regularFile" title="Regular file - statement.cc"><a class="FileList-itemLink" href="/chromium/src/+/refs/tags/133.0.6850.2/sql/statement.cc">statement.cc</a></li><li class="FileList-item FileList-item--regularFile" title="Regular file - statement.h"><a class="FileList-itemLink" href="/chromium/src/+/refs/tags/133.0.6850.2/sql/statement.h">statement.h</a></li><li class="FileList-item FileList-item--regularFile" title="Regular file - statement_id.cc"><a class="FileList-itemLink" href="/chromium/src/+/refs/tags/133.0.6850.2/sql/statement_id.cc">statement_id.cc</a></li><li class="FileList-item FileList-item--regularFile" title="Regular file - statement_id.h"><a class="FileList-itemLink" href="/chromium/src/+/refs/tags/133.0.6850.2/sql/statement_id.h">statement_id.h</a></li><li class="FileList-item FileList-item--regularFile" title="Regular file - statement_id_unittest.cc"><a class="FileList-itemLink" href="/chromium/src/+/refs/tags/133.0.6850.2/sql/statement_id_unittest.cc">statement_id_unittest.cc</a></li><li class="FileList-item FileList-item--regularFile" title="Regular file - statement_unittest.cc"><a class="FileList-itemLink" href="/chromium/src/+/refs/tags/133.0.6850.2/sql/statement_unittest.cc">statement_unittest.cc</a></li><li class="FileList-item FileList-item--regularFile" title="Regular file - transaction.cc"><a class="FileList-itemLink" href="/chromium/src/+/refs/tags/133.0.6850.2/sql/transaction.cc">transaction.cc</a></li><li class="FileList-item FileList-item--regularFile" title="Regular file - transaction.h"><a class="FileList-itemLink" href="/chromium/src/+/refs/tags/133.0.6850.2/sql/transaction.h">transaction.h</a></li><li class="FileList-item FileList-item--regularFile" title="Regular file - transaction_unittest.cc"><a class="FileList-itemLink" href="/chromium/src/+/refs/tags/133.0.6850.2/sql/transaction_unittest.cc">transaction_unittest.cc</a></li><li class="FileList-item FileList-item--regularFile" title="Regular file - vfs_wrapper.cc"><a class="FileList-itemLink" href="/chromium/src/+/refs/tags/133.0.6850.2/sql/vfs_wrapper.cc">vfs_wrapper.cc</a></li><li class="FileList-item FileList-item--regularFile" title="Regular file - vfs_wrapper.h"><a class="FileList-itemLink" href="/chromium/src/+/refs/tags/133.0.6850.2/sql/vfs_wrapper.h">vfs_wrapper.h</a></li><li class="FileList-item FileList-item--regularFile" title="Regular file - vfs_wrapper_fuchsia.cc"><a class="FileList-itemLink" href="/chromium/src/+/refs/tags/133.0.6850.2/sql/vfs_wrapper_fuchsia.cc">vfs_wrapper_fuchsia.cc</a></li><li class="FileList-item FileList-item--regularFile" title="Regular file - vfs_wrapper_fuchsia.h"><a class="FileList-itemLink" href="/chromium/src/+/refs/tags/133.0.6850.2/sql/vfs_wrapper_fuchsia.h">vfs_wrapper_fuchsia.h</a></li></ol><div class="InlineReadme"><div class="InlineReadme-path">sql/README.md</div><div class="doc"><h1><a class="h" name="SQLite-abstraction-layer" href="#SQLite-abstraction-layer"><span></span></a><a class="h" name="sqlite-abstraction-layer" href="#sqlite-abstraction-layer"><span></span></a>SQLite abstraction layer</h1><div class="toc" role="navigation"><h2>Contents</h2><div class="toc-aux"><ul><li><a href="#SQLite-for-system-designers">SQLite for system designers</a></li><li><a href="#SQLite-for-database-designers">SQLite for database designers</a></li><ul><li><a href="#storage-model">Data storage model</a></li><li><a href="#query-model">Statement execution model</a></li></ul><li><a href="#General-advice">General advice</a></li><ul><li><a href="#Quickly-iterating-on-SQL-statements">Quickly iterating on SQL statements</a></li><li><a href="#SQL-style">SQL style</a></li><li><a href="#Schema-style">Schema style</a></li><li><a href="#Discouraged-features">Discouraged features</a></li><li><a href="#Disabled-features">Disabled features</a></li></ul></ul></div></div><h2><a class="h" name="SQLite-for-system-designers" href="#SQLite-for-system-designers"><span></span></a><a class="h" name="sqlite-for-system-designers" href="#sqlite-for-system-designers"><span></span></a>SQLite for system designers</h2><p><a href="https://www.sqlite.org/">SQLite</a> is a <a href="https://en.wikipedia.org/wiki/Relational_database#RDBMS">relational database management system (RDBMS)</a> that <a href="https://www.sqlite.org/lang.html">supports most of SQL</a>.</p><p>SQLite is architected as a library that can be embedded in another application, such as Chrome. SQLite runs in the application's process, and shares its memory and other resources. This is similar to embedded databases like <a href="https://github.com/google/leveldb">LevelDB</a> and <a href="https://en.wikipedia.org/wiki/Berkeley_DB">BerkeleyDB</a>. By contrast, most popular RDMBSes, like <a href="https://www.postgresql.org/">PostgreSQL</a> and <a href="https://www.mysql.com/">MySQL</a>, are structured as standalone server processes that accept queries from client processes.</p><p>TODO: Explain the process model and locking</p><p>TODO: Explain Chrome decisions -- exclusive locking, full per-feature isolation (separate databases and page caches)</p><h2><a class="h" name="SQLite-for-database-designers" href="#SQLite-for-database-designers"><span></span></a><a class="h" name="sqlite-for-database-designers" href="#sqlite-for-database-designers"><span></span></a>SQLite for database designers</h2><p>The section summarizes aspects of SQLite that are relevant to schema and query design, and may be surprising to readers with prior experience in other popular SQL database systems, such as <a href="https://www.postgresql.org/">PostgreSQL</a> and <a href="https://www.mysql.com/">MySQL</a>.</p><h3><a class="h" name="storage-model" href="#storage-model"><span></span></a>Data storage model</h3><p>The main bottleneck in SQLite database performance is usually disk I/O. So, designing schemas that perform well requires understanding how SQLite stores data on disk.</p><p>At a very high level, a SQLite database is a forest of <a href="https://en.wikipedia.org/wiki/B-tree">B-trees</a>, some of which are <a href="https://en.wikipedia.org/wiki/B%2B_tree">B+-trees</a>. The database file is an array of fixed-size pages, where each page stores a B-tree node. The page size can only be set when a database file is created, and impacts both SQL statement execution speed, and memory consumption.</p><p>The data in each table (usually called <em>rows</em>, <em>records</em>, or <em>tuples</em>) is stored in a separate B-tree. The data in each index (called <em>entries</em>, <em>records</em> or <em>tuples</em>) is also stored in a separate B-tree. So, each B-tree is associated with exactly one table. The <a href="#indexing-model"><em>Indexing</em> section</a> goes into further details.</p><p>Each B-tree node stores multiple tuples of values. The values and their encodings are described in the <a href="#data-types"><em>Value types</em> section</a>.</p><p>Tying everything together: The performance of a SQL statement is roughly the number of database pages touched (read / written) by the statement. These pages are nodes belonging to the B-trees associated with the tables mentioned in the statement. The number of pages touched when accessing a B-tree depends on the B-tree‘s depth. Each B-tree’s depth depends on its record count (number of records stored in it), and on its node width (how many records fit in a node).</p><h4><a class="h" name="data-types" href="#data-types"><span></span></a>Value types</h4><p>SQLite stores values using <a href="https://www.sqlite.org/datatype3.html">5 major types</a>, which are summarized below.</p><ol><li><p>NULL is a special type for the <code class="code">NULL</code> value.</p></li><li><p>INTEGER represents big-endian twos-complement integers. Boolean values (<code class="code">TRUE</code> and <code class="code">FALSE</code>) are represented as the integer values 1 and 0.</p></li><li><p>REAL represents IEEE 754-2008 64-bit floating point numbers.</p></li><li><p>TEXT represents strings (sequences of characters) encoded using a <a href="https://www.sqlite.org/c3ref/c_any.html">supported SQLite encoding</a>. These values are <a href="https://www.sqlite.org/datatype3.html#sort_order">sorted</a> according to <a href="https://www.sqlite.org/datatype3.html#collation">a collating sequence</a> or <a href="https://www.sqlite.org/c3ref/create_collation.html">a collating function</a>.</p></li><li><p>BLOB represents sequences of bytes that are opaque to SQLite. These values are sorted using the bitwise binary comparison offered by <a href="https://en.cppreference.com/w/cpp/string/byte/memcmp">memcmp</a>.</p></li></ol><p>SQLite stores index keys and row values (records / tuples) using <a href="https://sqlite.org/fileformat2.html#record_format">a tightly packed format</a> that makes heavy use of <a href="https://sqlite.org/fileformat2.html#varint">varints</a> and variable-length fields. The column types have almost no influence on the encoding of values. This has the following consequences.</p><ul><li>All SQL integer types, such as <code class="code">TINYINT</code> and <code class="code">BIGINT</code>, are treated as aliases for <code class="code">INTEGER</code>.</li><li>All SQL non-integer numeric types, such as <code class="code">DECIMAL</code>, <code class="code">FLOAT</code>, and <code class="code">DOUBLE PRECISION</code> are treated as aliases for <code class="code">REAL</code>.</li><li>Numeric precision and scale specifiers, such as <code class="code">DECIMAL(5,2)</code> are ignored.</li><li>All string types, such as <code class="code">CHAR</code>, <code class="code">CHARACTER VARYING</code>, <code class="code">VARCHAR</code>, and <code class="code">CLOB</code>, are treated as aliases for <code class="code">TEXT</code>.</li><li>Maximum string length specifiers, such as <code class="code">CHAR(255)</code> are ignored.</li></ul><p>SQLite uses clever heuristics, called <a href="https://www.sqlite.org/datatype3.html#type_affinity">type affinity</a>, to map SQL column types such as <code class="code">VARCHAR</code> to the major types above.</p><p>Chrome database schemas should avoid type affinity, and should not include any information ignored by SQLite.</p><h4><a class="h" name="indexing-model" href="#indexing-model"><span></span></a>Indexing</h4><p>SQLite <a href="https://www.sqlite.org/fileformat2.html#pages">uses B-trees</a> to store both table and index data.</p><p>The exclusive use of B-trees reduces the amount of schema design decisions. Notable examples:</p><ul><li><p>There is no equivalent to <a href="https://www.postgresql.org/docs/13/indexes-types.html">PostgreSQL's index types</a>. In particular, since there are no hashed indexes, the design does not need to consider whether the index only needs to support equality queries, as opposed to greater/smaller than comparisons.</p></li><li><p>There is no equivalent to <a href="https://www.postgresql.org/docs/13/tableam.html">PostgreSQL's table access methods</a>. Each table is clustered by a primary key index, which is implicitly stored in the table's B-tree.</p></li></ul><p>By default, table rows (records / tuples) are stored in a B-tree keyed by <a href="https://sqlite.org/lang_createtable.html#rowid">rowid</a>, an automatically assigned 64-bit integer key. Effectively, these tables are clustered by rowid, which acts as an implicit primary key. Opting out of this SQLite-specific default requires appending <a href="https://sqlite.org/withoutrowid.html"><code class="code">WITHOUT ROWID</code></a> to the <code class="code">CREATE TABLE</code> instruction.</p><p>SQLite's <a href="https://sqlite.org/fileformat2.html#b_tree_pages">B-tree page format</a> has optimized special cases for tables clustered by rowid. This makes rowid the most efficient <a href="https://en.wikipedia.org/wiki/Surrogate_key">surrogate key</a> implementation in SQLite. To make this optimization easier to use, any column that is a primary key and has an <code class="code">INTEGER</code> type is considered an alias for rowid.</p><p>Each SQLite index <a href="https://sqlite.org/fileformat2.html#representation_of_sql_indices">is stored in a B-tree</a>. Each index entry is stored as a B-tree node whose key is made up of the record‘s index key column values, followed by the record’s primary key column values.</p><p><code class="code">WITHOUT ROWID</code> table indexes can include primary key columns without additional storage costs. This is because indexes for <code class="code">WITHOUT ROWID</code> tables enjoy <a href="https://sqlite.org/fileformat2.html#representation_of_sql_indices">a space optimization</a> where columns in both the primary key and the index key are not stored twice in B-tree nodes. Note that data in such tables cannot be recovered by <code class="code">sql::Recovery</code>.</p><h3><a class="h" name="query-model" href="#query-model"><span></span></a>Statement execution model</h3><p>At <a href="https://www.sqlite.org/arch.html">a very high level</a>, SQLite compiles SQL statements (often called <em>queries</em>) into bytecode executed by a virtual machine called the VDBE, or <a href="https://www.sqlite.org/opcode.html">the bytecode engine</a>. A compiled statement can be executed multiple times, amortizing the costs of query parsing and planning. Chrome's SQLite abstraction layer makes it easy to use compiled queries.</p><p>Assuming effective use of cached statements, the performance of a SQL statement comes down to the <em>query plan</em> that SQLite generates for the statement. The query plan is the sequence of B-tree accesses used to execute the statement, which determines the number of B-tree pages touched.</p><p>The rest of this section summarizes the following SQLite documentation pages.</p><ol><li><a href="https://www.sqlite.org/queryplanner.html">query planner overview</a></li><li><a href="https://www.sqlite.org/optoverview.html">query optimizer overview</a></li><li><a href="https://www.sqlite.org/eqp.html"><code class="code">EXPLAIN QUERY PLAN</code> output description</a></li></ol><p>At a high level, a SQLite query plan is a sequence of <strong>nested</strong> loops, where each loop iterates over the data in a B-tree. Each loop can use the current record of the outer loops.</p><p>TODO: Complete this section. Cover joins, sorting, etc.</p><h4><a class="h" name="Getting-SQLite_s-query-plans" href="#Getting-SQLite_s-query-plans"><span></span></a><a class="h" name="getting-sqlite_s-query-plans" href="#getting-sqlite_s-query-plans"><span></span></a>Getting SQLite's query plans</h4><p>Ideally, the SQL schemas and statements used by Chrome features would be simple enough that the query plans would be obvious to the reader.</p><p>When this isn't the case, the fastest way to get the query plan is to load the schema in <a href="https://sqlite.org/cli.html">the SQLite shell</a>, and use <a href="https://www.sqlite.org/eqp.html"><code class="code">EXPLAIN QUERY PLAN</code></a>.</p><p>The following command builds a SQLite shell that uses Chrome's build of SQLite, and supports the <code class="code">EXPLAIN QUERY PLAN</code> command.</p><pre class="code"><span class="pln">autoninja </span><span class="pun">-</span><span class="pln">C out</span><span class="pun">/</span><span class="typ">Default</span><span class="pln"> sqlite_dev_shell </span></pre><p>Inside the SQLite shell, the <code class="code">.eqp on</code> directive automatically shows the results of <code class="code">EXPLAIN QUERY PLAN</code> for every SQL statement executed in the shell.</p><h4><a class="h" name="query-step-types" href="#query-step-types"><span></span></a>Query steps</h4><p>Query steps are the building blocks of SQLite query plans. Each query step is essentially a loop that iterates over the records in a B-tree. These loops differ in terms of how many B-tree pages they touch, and how many records they produce. This sub-section lists the types of steps implemented by SQLite.</p><h5><a class="h" name="Scans" href="#Scans"><span></span></a><a class="h" name="scans" href="#scans"><span></span></a>Scans</h5><p>Scans visit an entire (table or index) B-tree. For this reason, scans are almost never acceptable in Chrome. Most of our features don't have limits on the amount of stored data, so scans can result in an unbounded amount of I/O.</p><p>A <em>table scan</em> visits the entire table's B-tree.</p><p>A <em>covering index scan</em> visits an entire index B-tree, but doesn't access the associated table B-tree.</p><p>SQLite doesn't have any special optimization for <code class="code">COUNT(*)</code> queries. In other words, SQLite does not track subtree sizes in its B-tree nodes.</p><p>Reviewers sometimes emphasize performance issues by calling the scans <em>full</em> table scans and <em>full</em> index scans, where “full” references the fact that the number of B-tree pages accessed is proportional to the entire data set stored on disk.</p><p>TODO: Complete this section. Add examples in a way that doesn't make the section overly long.</p><h5><a class="h" name="Searches" href="#Searches"><span></span></a><a class="h" name="searches" href="#searches"><span></span></a>Searches</h5><p>Searches access a subset of a (table or index) B-tree nodes. Searches limit the amount of nodes they need to access based on query restrictions, such as terms in the <code class="code">WHERE</code> clause. Seeing a <code class="code">SEARCH</code> in a query plan is not a guarantee of performance. Searches can vary wildly in the amount of B-tree pages they need to access.</p><p>One of the fastest possible searches is a <em>table search</em> that performs exactly one B-tree lookup, and produces at most one record.</p><p>The other fastest possible search is a <em>covering index search</em> that also performs one lookup, and produces at most one record.</p><p>TODO: Complete this section. Add examples in a way that doesn't make the section overly long.</p><h2><a class="h" name="General-advice" href="#General-advice"><span></span></a><a class="h" name="general-advice" href="#general-advice"><span></span></a>General advice</h2><p>The following pieces of advice usually come up in code reviews.</p><h3><a class="h" name="Quickly-iterating-on-SQL-statements" href="#Quickly-iterating-on-SQL-statements"><span></span></a><a class="h" name="quickly-iterating-on-sql-statements" href="#quickly-iterating-on-sql-statements"><span></span></a>Quickly iterating on SQL statements</h3><p><a href="https://sqlite.org/cli.html">The SQLite shell</a> offers quick feedback for converging on valid SQL statement syntax, and avoiding SQLite features that are disabled in Chrome. In addition, the <a href="https://www.sqlite.org/lang_explain.html"><code class="code">EXPLAIN</code></a> and <a href="https://www.sqlite.org/eqp.html"><code class="code">EXPLAIN QUERY PLAN</code></a> statements show the results of SQLite's query planner and optimizer, which are very helpful for reasoning about the performance of complex queries. The SQLite shell directive <code class="code">.eqp on</code> automatically issues <code class="code">EXPLAIN QUERY PLAN</code> for all future commands.</p><p>The following commands set up SQLite shells using Chrome's build of SQLite.</p><pre class="code"><span class="pln">autoninja </span><span class="pun">-</span><span class="pln">C out</span><span class="pun">/</span><span class="typ">Default</span><span class="pln"> sqlite_shell sqlite_dev_shell </span></pre><ul><li><code class="code">sqlite_shell</code> runs the SQLite build that we ship in Chrome. It offers the ground truth on whether a SQL statement can be used in Chrome code or not.</li><li><code class="code">sqlite_dev_shell</code> enables the <code class="code">EXPLAIN</code> and <code class="code">EXPLAIN QUERY PLAN</code> statements, as well as a few features used by <a href="https://perfetto.dev/">Perfetto</a>'s analysis tools.</li></ul><h3><a class="h" name="SQL-style" href="#SQL-style"><span></span></a><a class="h" name="sql-style" href="#sql-style"><span></span></a>SQL style</h3><p>SQLite queries are usually embedded as string literals in C++ code. The advice here has the following goals.</p><ol><li><p>Easy to read queries. The best defense against subtle bugs is making the queries very easy to read, so that any bugs become obvious at code review time. SQL string literals don't benefit from our code analysis infrastructure, so the only lines of defense against bugs are testing and code review.</p></li><li><p>Simplify crash debugging. We will always have a low volume of non-actionable crash reports, because Chrome runs on billions of devices, some of which have faulty RAM or processors.</p></li><li><p>No unnecessary performance overheads. The C++ optimizer doesn't understand SQL query literals, so the queries end up as written in the Chrome binary. Extra characters cost binary size, as well as CPU time (which turns into battery usage) during query parsing.</p></li><li><p>Match the embedding language (C++) style guide. This reduces the mental context switch overhead for folks who write and/or review C++ code that contains SQL.</p></li></ol><p>Format statements like so.</p><pre class="code"><span class="pln"> </span><span class="kwd">static</span><span class="pln"> </span><span class="kwd">constexpr</span><span class="pln"> </span><span class="kwd">char</span><span class="pln"> kOriginInfoSql</span><span class="pun">[]</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> </span><span class="com">// clang-format off</span><span class="pln"> </span><span class="str">"CREATE TABLE origin_infos("</span><span class="pln"> </span><span class="str">"origin TEXT NOT NULL,"</span><span class="pln"> </span><span class="str">"last_modified INTEGER NOT NULL,"</span><span class="pln"> </span><span class="str">"secure INTEGER NOT NULL)"</span><span class="pun">;</span><span class="pln"> </span><span class="com">// clang-format on</span><span class="pln"> </span><span class="kwd">static</span><span class="pln"> </span><span class="kwd">constexpr</span><span class="pln"> </span><span class="kwd">char</span><span class="pln"> kInsertSql</span><span class="pun">[]</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> </span><span class="com">// clang-format off</span><span class="pln"> </span><span class="str">"INSERT INTO infos(origin,last_modified,secure) "</span><span class="pln"> </span><span class="str">"VALUES(?,?,?)"</span><span class="pun">;</span><span class="pln"> </span><span class="com">// clang-format on</span><span class="pln"> </span><span class="kwd">static</span><span class="pln"> </span><span class="kwd">constexpr</span><span class="pln"> </span><span class="kwd">char</span><span class="pln"> kSelectSql</span><span class="pun">[]</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> </span><span class="com">// clang-format off</span><span class="pln"> </span><span class="str">"SELECT origin,last_modified,secure FROM origins "</span><span class="pln"> </span><span class="str">"WHERE last_modified>? "</span><span class="pln"> </span><span class="str">"ORDER BY last_modified"</span><span class="pun">;</span><span class="pln"> </span><span class="com">// clang-format on</span><span class="pln"> </span></pre><ul><li><p><a href="https://sqlite.org/lang_keywords.html">SQLite keywords</a> should use ALL CAPS. This makes SQL query literals easier to distinguish and search for.</p></li><li><p>Identifiers, such as table and row names, should use snake_case.</p></li><li><p>Identifiers, keywords, and parameter placeholders (<code class="code">?</code>) should be separated by exactly one character. Separators may be spaces (<code class="code"> </code>), commas (<code class="code">,</code>), or parentheses (<code class="code">(</code>, <code class="code">)</code>).</p></li><li><p>Statement-ending semicolons (<code class="code">;</code>) are omitted.</p></li><li><p>SQL statements are stored in variables typed <code class="code">static constexpr char[]</code>, or in string literals passed directly to methods.</p></li><li><p><a href="https://sqlite.org/lang_insert.html"><code class="code">INSERT</code> statements</a> should list all the table columns by name, in the same order as the corresponding <code class="code">CREATE TABLE</code> statements.</p></li><li><p><a href="https://sqlite.org/lang_select.html"><code class="code">SELECT</code> statements</a> should list the desired table columns by name, in the same order as the corresponding <code class="code">CREATE TABLE</code> statements. <code class="code">SELECT *</code> is strongly discouraged, at least until we have schema checks on database opens.</p></li><li><p><a href="https://sqlite.org/lang_select.html"><code class="code">SELECT</code> statements</a> that retrieve more than one row should include an <a href="https://sqlite.org/lang_select.html#the_order_by_clause"><code class="code">ORDER BY</code> clause</a> to clarify the implicit ordering.</p><ul><li>SELECTs whose outer loop is a table search or table scan implicitly order results by <a href="https://sqlite.org/lang_createtable.html#rowid">rowid</a> or, in the case of <a href="https://sqlite.org/withoutrowid.html"><code class="code">WITHOUT ROWID</code></a> tables, by the table's primary key.</li><li>SELECTs whose outer loop is an index scan or index search order results according to that index.</li></ul></li><li><p><a href="https://sqlite.org/lang_createindex.html"><code class="code">CREATE INDEX</code> statements</a> should immediately follow the <a href="https://sqlite.org/lang_createtable.html"><code class="code">CREATE TABLE</code> statement</a> for the indexed table.</p></li><li><p>Explicit <code class="code">CREATE UNIQUE INDEX</code> statements should be preferred to <a href="https://sqlite.org/lang_createtable.html#unique_constraints"><code class="code">UNIQUE</code> constraints on <code class="code">CREATE TABLE</code></a>.</p></li><li><p>Values must either be embedded in the SQL statement string literal, or bound using <a href="https://www.sqlite.org/lang_expr.html#varparam">parameters</a>.</p></li><li><p>Parameter placeholders should always use the <code class="code">?</code> syntax. Alternative syntaxes, such as <code class="code">?NNN</code> or <code class="code">:AAAA</code>, have few benefits in a codebase where the <code class="code">Bind</code> statements are right next to the queries, and are less known to readers.</p></li><li><p>SQL statements should be embedded in C++ as string literals. The <code class="code">char[]</code> type makes it possible for us to compute query length at compile time in the future. The <code class="code">static</code> and <code class="code">constexpr</code> qualifiers both ensure optimal code generation.</p></li><li><p>Do not execute multiple SQL statements (e.g., by calling <code class="code">Step()</code> or <code class="code">Run()</code> on <code class="code">sql::Statement</code>) on the same C++ line. It‘s difficult to get more than line numbers from crash reports’ stack traces.</p></li></ul><h3><a class="h" name="Schema-style" href="#Schema-style"><span></span></a><a class="h" name="schema-style" href="#schema-style"><span></span></a>Schema style</h3><p>Identifiers (table / index / column names and aliases) must not be <a href="https://sqlite.org/lang_keywords.html">current SQLite keywords</a>. Identifiers may not start with the <code class="code">sqlite_</code> prefix, to avoid conflicting with the name of a <a href="https://www.sqlite.org/fileformat2.html#storage_of_the_sql_database_schema">SQLite internal schema object</a>.</p><p>Column types should only be one of the the SQLite storage types (<code class="code">INTEGER</code>, <code class="code">REAL</code>, <code class="code">TEXT</code>, <code class="code">BLOB</code>), so readers can avoid reasoning about SQLite's type affinity.</p><p>Columns that will store boolean values should have the <code class="code">INTEGER</code> type.</p><p>Columns that will store <code class="code">base::Time</code> values should have the <code class="code">INTEGER</code> type. Values should be serialized using <code class="code">sql::Statement::BindTime()</code> and deserialized using <code class="code">sql::Statement::ColumnTime()</code>.</p><p>Column types should not include information ignored by SQLite, such as numeric precision or scale specifiers, or string length specifiers.</p><p>Columns should have <a href="https://sqlite.org/lang_createtable.html#not_null_constraints"><code class="code">NOT NULL</code> constraints</a> whenever possible. This saves maintainers from having to reason about the less intuitive cases of <a href="https://sqlite.org/nulls.html"><code class="code">NULL</code> handling</a>.</p><p><code class="code">NOT NULL</code> constraints must be explicitly stated in column definitions that include <code class="code">PRIMARY KEY</code> specifiers. For historical reasons, SQLite <a href="https://sqlite.org/lang_createtable.html#the_primary_key">allows NULL primary keys</a> in most cases. When a table‘s primary key is composed of multiple columns, each column’s definition should have a <code class="code">NOT NULL</code> constraint.</p><p>Columns should avoid <code class="code">DEFAULT</code> values. Columns that have <code class="code">NOT NULL</code> constraints and lack a <code class="code">DEFAULT</code> value are easier to review and maintain, as SQLite takes over the burden of checking that <code class="code">INSERT</code> statements aren't missing these columns.</p><p>Surrogate primary keys should use the column type <code class="code">INTEGER PRIMARY KEY</code>, to take advantage of SQLite's rowid optimizations. <a href="https://www.sqlite.org/autoinc.html"><code class="code">AUTOINCREMENT</code></a> should only be used where primary key reuse would be unacceptable.</p><h3><a class="h" name="Discouraged-features" href="#Discouraged-features"><span></span></a><a class="h" name="discouraged-features" href="#discouraged-features"><span></span></a>Discouraged features</h3><p>SQLite exposes a vast array of functionality via SQL statements. The following features are not a good match for SQL statements used by Chrome feature code.</p><h4><a class="h" name="no-pragmas" href="#no-pragmas"><span></span></a>PRAGMA statements</h4><p><a href="https://www.sqlite.org/pragma.html"><code class="code">PRAGMA</code> statements</a> should never be used directly. Chrome's SQLite abstraction layer should be modified to support the desired effects instead.</p><p>Direct <code class="code">PRAGMA</code> use limits our ability to customize and secure our SQLite build. <code class="code">PRAGMA</code> statements may turn on code paths with less testing / fuzzing coverage. Furthermore, some <code class="code">PRAGMA</code> statements invalidate previously compiled queries, reducing the efficiency of Chrome's compiled query cache.</p><h4><a class="h" name="no-foreign-keys" href="#no-foreign-keys"><span></span></a>Foreign key constraints</h4><p><a href="https://sqlite.org/foreignkeys.html">SQL foreign key constraints</a> should not be used. All data validation should be performed using explicit <code class="code">SELECT</code> statements (generally wrapped as helper methods) inside transactions. Cascading deletions should be performed using explicit <code class="code">DELETE</code> statements inside transactions.</p><p>Chrome features cannot rely on foreign key enforcement, due to the possibility of data corruption. Furthermore, foreign key constraints make it more difficult to reason about system behavior (Chrome feature code + SQLite) when the database gets corrupted. Foreign key constraints also make it more difficult to reason about query performance.</p><p>As a result, foreign key constraints are not enforced on SQLite databases opened with Chrome's <code class="code">sql::Database</code> infrastructure.</p><p>After <a href="https://www.w3.org/TR/webdatabase/">WebSQL</a> is removed from Chrome, we plan to disable SQLite's foreign key support using <a href="https://sqlite.org/compile.html#omit_foreign_key">SQLITE_OMIT_FOREIGN_KEY</a>.</p><h4><a class="h" name="no-checks" href="#no-checks"><span></span></a>CHECK constraints</h4><p><a href="https://sqlite.org/lang_createtable.html#check_constraints">SQL CHECK constraints</a> should not be used, for the same reasons as foreign key constraints. The equivalent checks should be performed in C++, typically using <code class="code">DCHECK</code>.</p><p>After <a href="https://www.w3.org/TR/webdatabase/">WebSQL</a> is removed from Chrome, we plan to disable SQLite's CHECK constraint support using <a href="https://sqlite.org/compile.html#omit_check">SQLITE_OMIT_CHECK</a>.</p><h4><a class="h" name="no-triggers" href="#no-triggers"><span></span></a>Triggers</h4><p><a href="https://sqlite.org/lang_createtrigger.html">SQL triggers</a> should not be used.</p><p>Triggers significantly increase the difficulty of reviewing and maintaining Chrome features that use them.</p><p>Triggers are not executed on SQLite databases opened with Chrome's <code class="code">sql::Database</code> infrastructure. This is intended to steer feature developers away from the discouraged feature.</p><p>After <a href="https://www.w3.org/TR/webdatabase/">WebSQL</a> is removed from Chrome, we plan to disable SQLite's trigger support using <a href="https://sqlite.org/compile.html#omit_trigger">SQLITE_OMIT_TRIGGER</a>.</p><h4><a class="h" name="no-ctes" href="#no-ctes"><span></span></a>Common Table Expressions</h4><p><a href="https://sqlite.org/lang_with.html">SQL Common Table Expressions (CTEs)</a> should not be used. Chrome's SQL schemas and queries should be simple enough that the factoring afforded by <a href="https://sqlite.org/lang_with.html#ordinary_common_table_expressions">ordinary CTEs</a> is not necessary. <a href="https://sqlite.org/lang_with.html#recursive_common_table_expressions">Recursive CTEs</a> should be implemented in C++.</p><p>Common Table Expressions do not open up any query optimizations that would not be available otherwise, and make it more difficult to review / analyze queries.</p><h4><a class="h" name="no-views" href="#no-views"><span></span></a>Views</h4><p>SQL views, managed by the <a href="https://www.sqlite.org/lang_createview.html"><code class="code">CREATE VIEW</code> statement</a> and the <a href="https://www.sqlite.org/lang_dropview.html"><code class="code">DROP VIEW</code> statement</a>, should not be used. Chrome's SQL schemas and queries should be simple enough that the factoring afforded by views is not necessary.</p><p>Views are syntactic sugar, and do not open up any new SQL capabilities. SQL statements on views are more difficult to understand and maintain, because of the extra layer of indirection.</p><p>Access to views is disabled by default for SQLite databases opened with Chrome's <code class="code">sql::Database</code> infrastructure. This is intended to steer feature developers away from the discouraged feature.</p><p>After <a href="https://www.w3.org/TR/webdatabase/">WebSQL</a> is removed from Chrome, we plan to disable SQLite's VIEW support using <a href="https://www.sqlite.org/compile.html#omit_view">SQLITE_OMIT_VIEW</a>.</p><h4><a class="h" name="no-double-quoted-strings" href="#no-double-quoted-strings"><span></span></a>Double-quoted string literals</h4><p>String literals should always be single-quoted. That being said, string literals should be rare in Chrome code, because any user input must be injected using statement parameters and the <code class="code">Statement::Bind*()</code> methods.</p><p>Double-quoted string literals are non-standard SQL syntax. The SQLite authors <a href="https://www.sqlite.org/quirks.html#double_quoted_string_literals_are_accepted">currently consider this be a misfeature</a>.</p><p>SQLite support for double-quoted string literals is disabled for databases opened with Chrome's <code class="code">sql::Database</code> infrastructure. This is intended to steer feature developers away from this discouraged feature.</p><p>After <a href="https://www.w3.org/TR/webdatabase/">WebSQL</a> is removed from Chrome, we plan to disable SQLite's support for double-quoted string literals using <a href="https://www.sqlite.org/compile.html#dqs">SQLITE_DQS=0</a>.</p><h4><a class="h" name="no-compound-queries" href="#no-compound-queries"><span></span></a>Compound SELECT statements</h4><p><a href="https://www.sqlite.org/lang_select.html#compound_select_statements">Compound SELECT statements</a> should not be used. Such statements should be broken down into <a href="https://www.sqlite.org/lang_select.html#simple_select_processing">simple SELECT statements</a>, and the operators <code class="code">UNION</code>, <code class="code">UNION ALL</code>, <code class="code">INTERSECT</code> and <code class="code">EXCEPT</code> should be implemented in C++.</p><p>A single compound SELECT statement is more difficult to review and properly unit-test than the equivalent collection of simple SELECT statements. Furthermore, the compound SELECT statement operators can be implemented more efficiently in C++ than in SQLite's bytecode interpreter (VDBE).</p><p>After <a href="https://www.w3.org/TR/webdatabase/">WebSQL</a> is removed from Chrome, we plan to disable SQLite's compound SELECT support using <a href="https://www.sqlite.org/compile.html#omit_compound_select">SQLITE_OMIT_COMPOUND_SELECT</a>.</p><h4><a class="h" name="no-builtin-functions" href="#no-builtin-functions"><span></span></a>Built-in functions</h4><p>SQLite's <a href="https://sqlite.org/lang_corefunc.html">built-in functions</a> should be only be used in SQL statements where they unlock significant performance improvements. Chrome features should store data in a format that leaves the most room for query optimizations, and perform any necessary transformations after reading / before writing the data.</p><ul><li><a href="https://sqlite.org/lang_aggfunc.html">Aggregation functions</a> are best replaced with C++ code that iterates over rows and computes the desired results.</li><li><a href="https://sqlite.org/lang_datefunc.html">Date and time functions</a> are best replaced by <code class="code">base::Time</code> functionality.</li><li>String-processing functions, such as <a href="https://sqlite.org/printf.html"><code class="code">printf()</code></a> and <code class="code">trim()</code> are best replaced by C++ code that uses the helpers in <code class="code">//base/strings/</code>.</li><li>Wrappers for <a href="https://sqlite.org/c3ref/funclist.html">SQLite's C API</a>, such as <code class="code">changes()</code>, <code class="code">last_insert_rowid()</code>, and <code class="code">total_changes()</code>, are best replaced by functionality in <code class="code">sql::Database</code> and <code class="code">sql::Statement</code>.</li><li>SQLite-specific functions, such as <code class="code">sqlite_source_id()</code> and <code class="code">sqlite_version()</code> should not be necessary in Chrome code, and may suggest a problem in the feature's design.</li></ul><p><a href="https://sqlite.org/lang_mathfunc.html">Math functions</a> and <a href="https://sqlite.org/windowfunctions.html#biwinfunc">Window functions</a> are disabled in Chrome's SQLite build.</p><h4><a class="h" name="ATTACH-DATABASE-statements" href="#ATTACH-DATABASE-statements"><span></span></a><a class="h" name="attach-database-statements" href="#attach-database-statements"><span></span></a>ATTACH DATABASE statements</h4><p><a href="https://www.sqlite.org/lang_attach.html"><code class="code">ATTACH DATABASE</code> statements</a> should be used thoughtfully. Each Chrome feature should store its data in a single database. Chrome code should not assume that transactions across multiple databases are atomic.</p><h3><a class="h" name="Disabled-features" href="#Disabled-features"><span></span></a><a class="h" name="disabled-features" href="#disabled-features"><span></span></a>Disabled features</h3><p>We aim to disable SQLite features that should not be used in Chrome, subject to the constraint of keeping WebSQL's feature set stable. We currently disable all new SQLite features, to avoid expanding the attack surface exposed to WebSQL. This stance may change once WebSQL is removed from Chrome.</p><p>The following SQLite features have been disabled in Chrome.</p><h4><a class="h" name="JSON" href="#JSON"><span></span></a><a class="h" name="json" href="#json"><span></span></a>JSON</h4><p>Chrome features should prefer <a href="https://developers.google.com/protocol-buffers">procotol buffers</a> to JSON for on-disk (persistent) serialization of extensible structured data.</p><p>Chrome features should store the values used by indexes directly in their own columns, instead of relying on <a href="https://www.sqlite.org/json1.html">SQLite's JSON support</a>.</p><h4><a class="h" name="UPSERT" href="#UPSERT"><span></span></a><a class="h" name="upsert" href="#upsert"><span></span></a>UPSERT</h4><p><a href="https://www.sqlite.org/lang_UPSERT.html">SQLite's UPSERT implementation</a> has been disabled in order to avoid increasing WebSQL's attack surface. UPSERT is disabled using the <code class="code">SQLITE_OMIT_UPSERT</code> macro, which is not currently included in <a href="https://www.sqlite.org/compile.html">the SQLite compile-time option list</a>, but exists in the source code.</p><p>We currently think that the new UPSERT functionality is not essential to implementing Chrome features efficiently. An example where UPSERT is necessary for the success of a Chrome feature would likely get UPSERT enabled.</p><h4><a class="h" name="Window-functions" href="#Window-functions"><span></span></a><a class="h" name="window-functions" href="#window-functions"><span></span></a>Window functions</h4><p><a href="https://sqlite.org/windowfunctions.html#biwinfunc">Window functions</a> have been disabled primarily because they cause a significant binary size increase, which leads to a corresponding large increase in the attack surface exposed to WebSQL.</p><p>Window functions increase the difficulty of reviewing and maintaining the Chrome features that use them, because window functions add complexity to the mental model of query performance.</p><p>We currently think that this maintenance overhead of window functions exceeds any convenience and performance benefits (compared to simpler queries coordinated in C++).</p><h4><a class="h" name="no-virtual-tables" href="#no-virtual-tables"><span></span></a>Virtual tables</h4><p><a href="https://www.sqlite.org/vtab.html"><code class="code">CREATE VIRTUAL TABLE</code> statements</a> are disabled. The desired functionality should be implemented in C++, and access storage using standard SQL statements.</p><p>Virtual tables are <a href="https://www.sqlite.org/vtab.html">SQLite's module system</a>. SQL statements on virtual tables are essentially running arbitrary code, which makes them very difficult to reason about and maintain. Furthermore, the virtual table implementations don't receive the same level of fuzzing coverage as the SQLite core.</p><p>Chrome's SQLite build has virtual table functionality reduced to the minimum needed to support an internal feature. <a href="https://www.sqlite.org/loadext.html">SQLite's run-time loading mechanism</a> is disabled, and most <a href="https://www.sqlite.org/vtablist.html">built-in virtual tables</a> are disabled as well.</p><p>Ideally we would disable SQLite‘s virtual table support using <a href="https://sqlite.org/compile.html#omit_virtualtable">SQLITE_OMIT_VIRTUALTABLE</a> now that <a href="https://www.w3.org/TR/webdatabase/">WebSQL</a> has been removed from Chrome, but virtual table support is required to use SQLite’s <a href="https://www.sqlite.org/recovery.html">built-in corruption recovery module</a>. The <a href="https://www.sqlite.org/dbpage.html">SQLITE_DBPAGE virtual table</a> is also enabled only for corruption recovery and should not be used in Chrome.</p></div></div></div></div> <!-- Container --></div> <!-- Site-content --><footer class="Site-footer"><div class="Footer"><span class="Footer-poweredBy">Powered by <a href="https://gerrit.googlesource.com/gitiles/">Gitiles</a>| <a href="https://policies.google.com/privacy">Privacy</a>| <a href="https://policies.google.com/terms">Terms</a></span><span class="Footer-formats"><a class="u-monospace Footer-formatsItem" href="?format=TEXT">txt</a> <a class="u-monospace Footer-formatsItem" href="?format=JSON">json</a></span></div></footer></body></html>