LIGHTNING-SQL(7)
๐ NAME ๐
okay so welcome to the LIGHTNING-SQL(7) page, bestie โ this is the manual for the sql RPC command, and it's giving โcomplex queries on list commandsโ energy. ๐
sql โ Command to do complex queries on list commands.
โจ SYNOPSIS โจ
---
# ๐ฆ DESCRIPTION ๐ฆ
Command added in `v23.02`.
The <cyan>`sql` RPC command</cyan> runs the given query across a `sqlite3` database created from various `list` commands.
When tables are accessed, it calls the below commands, so it's no faster than any other local access (though it goes to great length to cache `listnodes` and `listchannels`) which then processes the results. lowkey important, ngl.
It is, however faster for remote access if the result of the query is much smaller than the list commands would be. slay for bandwidth. ๐ธ
> Note that you may need to use `-o` if you use queries which contain `=` (which make `$ lightning-cli(1)` default to keyword style)
- **`query`** (*string*): The standard `sqlite3` query to run. Note that queries like `"SELECT *"` are fragile, as columns will change across releases; see `lightning-listsqlschemas(7)`. fr fr, don't get too attached to `SELECT *`.
---
# ๐ PERMITTED SQLITE3 FUNCTIONS ๐
Writing to the database is not permitted, and limits are placed on various other query parameters. so no sneaky writes, bestie. ๐
Additionally, only the following functions are allowed:
- `abs`
- `avg`
- `coalesce`
- `count`
- `date`
- `datetime`
- `julianday`
- `hex`
- `quote`
- `length`
- `like`
- `lower`
- `upper`
- `min`
- `max`
- `strftime`
- `sum`
- `time`
- `timediff`
- `total`
- `unixepoch`
- `json_object`
- `json_group_array`
---
# ๐ TREATMENT OF TYPES ๐
The following types are supported in schemas, and this shows how they are presented in the database. This matters: a JSON boolean is represented as an integer in the database, so a query will return `0` or `1`, not `true` or `false`. <purple>this is the part where the type lore gets real</purple>.
- **`hex`** โ a hex string.
- JSON: a string
- sqlite3: `BLOB`
- **`hash`/`secret`/`pubkey`/`txid`** โ just like `hex`.
- **`msat`/`integer`/`u64`/`u32`/`u16`/`u8`** โ normal numbers.
- JSON: an unsigned integer
- sqlite3: `INTEGER`
- **`boolean`** โ true or false.
- JSON: literal `true` or `false`
- sqlite3: `INTEGER`
- **`number`** โ a floating point number (used for times in some places).
- JSON: number
- sqlite3: `REAL`
- **`string`** โ text.
- JSON: string
- sqlite3: `TEXT`
- **`short_channel_id`** โ a short-channel-id of form `1x2x3`.
- JSON: string
- sqlite3: `TEXT`
---
# ๐ TABLES ๐
Note that tables which have a `created_index` field use that as the primary key (and `rowid` is an alias to this), otherwise an explicit `rowid` integer primary key is generated, whose value changes on each refresh. This field is used for related tables to refer to specific rows in their parent. (`sqlite3` usually has this as an implicit column, but we make it explicit as the implicit version is not allowed to be used as a foreign key).
The following tables are currently supported:
- `bkpr_accountevents` (see `lightning-bkpr-listaccountevents(7)`)
- `account` (type `string`, sqltype `TEXT`)
- `type` (type `string`, sqltype `TEXT`)
- `tag` (type `string`, sqltype `TEXT`)
- `credit_msat` (type `msat`, sqltype `INTEGER`)
- `debit_msat` (type `msat`, sqltype `INTEGER`)
- `currency` (type `string`, sqltype `TEXT`)
- `timestamp` (type `u32`, sqltype `INTEGER`)
- `currencyrate` (type `number`, sqltype `REAL`)
- `description` (type `string`, sqltype `TEXT`)
- `outpoint` (type `string`, sqltype `TEXT`)
- `blockheight` (type `u32`, sqltype `INTEGER`)
- `origin` (type `string`, sqltype `TEXT`)
- `payment_id` (type `hex`, sqltype `BLOB`)
- `txid` (type `txid`, sqltype `BLOB`)
- `fees_msat` (type `msat`, sqltype `INTEGER`)
- `is_rebalance` (type `boolean`, sqltype `INTEGER`)
- `part_id` (type `u32`, sqltype `INTEGER`)
---
- **`bkpr_income`** โ the income table, with its own whole `lightning-bkpr-listincome(7)` moment ๐ธโจ
- `account` โ type `string`, sqltype `TEXT` ๐
- `tag` โ type `string`, sqltype `TEXT` ๐ท๏ธ
- `credit_msat` โ type `msat`, sqltype `INTEGER` ๐
- `debit_msat` โ type `msat`, sqltype `INTEGER` ๐ธ
- `currency` โ type `string`, sqltype `TEXT` ๐ฑ
- `timestamp` โ type `u32`, sqltype `INTEGER` โฐ
- `description` โ type `string`, sqltype `TEXT` ๐
- `outpoint` โ type `string`, sqltype `TEXT` ๐
- `txid` โ type `txid`, sqltype `BLOB` ๐งพ
- `payment_id` โ type `hex`, sqltype `BLOB` ๐
- **`chainmoves`** โ indexed by `account_id` (see `lightning-listchainmoves(7)`) โ๏ธ
- `created_index` โ type `u64`, sqltype `INTEGER PRIMARY KEY` ๐
- `account_id` โ type `string`, sqltype `TEXT` ๐
- `credit_msat` โ type `msat`, sqltype `INTEGER` ๐
- `debit_msat` โ type `msat`, sqltype `INTEGER` ๐ธ
- `timestamp` โ type `u64`, sqltype `INTEGER` โฐ
- `primary_tag` โ type `string`, sqltype `TEXT` ๐ท๏ธ
- related table `chainmoves_extra_tags` โ the extra-tags side quest ๐
- `row` โ reference to `chainmoves.created_index`, sqltype `INTEGER` ๐
- `arrindex` โ index within array, sqltype `INTEGER` ๐ข
- `extra_tags` โ type `string`, sqltype `TEXT` ๐ท๏ธ
- `peer_id` โ type `pubkey`, sqltype `BLOB` ๐ซถ
- `originating_account` โ type `string`, sqltype `TEXT` ๐
- `spending_txid` โ type `txid`, sqltype `BLOB` ๐งพ
- `utxo` โ type `outpoint`, sqltype `TEXT` ๐
- `payment_hash` โ type `hash`, sqltype `BLOB` #๏ธโฃ
- `output_msat` โ type `msat`, sqltype `INTEGER` ๐
- `output_count` โ type `u32`, sqltype `INTEGER` ๐ข
- `blockheight` โ type `u32`, sqltype `INTEGER` ๐งฑ
- **`channelmoves`** โ indexed by `account_id`, also indexed by `payment_hash` (see `lightning-listchannelmoves(7)`) ๐
- `created_index` โ type `u64`, sqltype `INTEGER PRIMARY KEY` ๐
- `account_id` โ type `string`, sqltype `TEXT` ๐
- `credit_msat` โ type `msat`, sqltype `INTEGER` ๐
- `debit_msat` โ type `msat`, sqltype `INTEGER` ๐ธ
- `timestamp` โ type `u64`, sqltype `INTEGER` โฐ
- `primary_tag` โ type `string`, sqltype `TEXT` ๐ท๏ธ
- `payment_hash` โ type `hash`, sqltype `BLOB` #๏ธโฃ
- `part_id` โ type `u64`, sqltype `INTEGER` ๐งฉ
- `group_id` โ type `u64`, sqltype `INTEGER` ๐ฏ
- `fees_msat` โ type `msat`, sqltype `INTEGER` ๐ธ
- **`channels`** โ indexed by `short_channel_id` (see `lightning-listchannels(7)`) ๐ก
- `source` โ type `pubkey`, sqltype `BLOB` ๐ซถ
- `destination` โ type `pubkey`, sqltype `BLOB` ๐ฏ
- `short_channel_id` โ type `short_channel_id`, sqltype `TEXT` ๐
- `direction` โ type `u32`, sqltype `INTEGER` ๐งญ
- `public` โ type `boolean`, sqltype `INTEGER` ๐
- `amount_msat` โ type `msat`, sqltype `INTEGER` ๐ฐ
- `message_flags` โ type `u8`, sqltype `INTEGER` ๐ฉ
- `channel_flags` โ type `u8`, sqltype `INTEGER` ๐
- `active` โ type `boolean`, sqltype `INTEGER` โ
- `last_update` โ type `u32`, sqltype `INTEGER` ๐ฐ๏ธ
- `base_fee_millisatoshi` โ type `u32`, sqltype `INTEGER` ๐ต
- `fee_per_millionth` โ type `u32`, sqltype `INTEGER` ๐
- `delay` โ type `u32`, sqltype `INTEGER` โณ
- `htlc_minimum_msat` โ type `msat`, sqltype `INTEGER` ๐ฝ
- `htlc_maximum_msat` โ type `msat`, sqltype `INTEGER` ๐ผ
- `features` โ type `hex`, sqltype `BLOB` ๐งฌ
- **`closedchannels`** โ (see `lightning-listclosedchannels(7)`) ๐ช
- `peer_id` โ type `pubkey`, sqltype `BLOB` ๐ซถ
- `channel_id` โ type `hash`, sqltype `BLOB` #๏ธโฃ
- `short_channel_id` โ type `short_channel_id`, sqltype `TEXT` ๐
- `alias_local` โ type `short_channel_id`, sqltype `TEXT`, from JSON object `alias` ๐ท๏ธ
- `alias_remote` โ type `short_channel_id`, sqltype `TEXT`, from JSON object `alias` ๐ท๏ธ
- `opener` โ type `string`, sqltype `TEXT` ๐
- `closer` โ type `string`, sqltype `TEXT` ๐
- `private` โ type `boolean`, sqltype `INTEGER` ๐
- related table `closedchannels_channel_type_bits`, from JSON object `channel_type` โ the bitsy channel-type table ๐งฉ
- `row` โ reference to `closedchannels_channel_type.rowid`, sqltype `INTEGER` ๐
- `arrindex` โ index within array, sqltype `INTEGER` ๐ข
- `bits` โ type `u32`, sqltype `INTEGER` ๐งฎ
- related table `closedchannels_channel_type_names`, from JSON object `channel_type` โ the names version, because of course ๐
- `row` โ reference to `closedchannels_channel_type.rowid`, sqltype `INTEGER` ๐
- `arrindex` โ index within array, sqltype `INTEGER` ๐ข
- `names` โ type `string`, sqltype `TEXT` ๐ท๏ธ
- `total_local_commitments` โ type `u64`, sqltype `INTEGER` ๐งพ
- `total_remote_commitments` โ type `u64`, sqltype `INTEGER` ๐งพ
- `total_htlcs_sent` โ type `u64`, sqltype `INTEGER` ๐ค
- `funding_txid` โ type `txid`, sqltype `BLOB` ๐งพ
- `funding_outnum` โ type `u32`, sqltype `INTEGER` ๐ข
- `funding_psbt` โ type `string`, sqltype `TEXT` ๐
- `funding_withheld` โ type `boolean`, sqltype `INTEGER` ๐
- `leased` โ type `boolean`, sqltype `INTEGER` ๐
- `funding_fee_paid_msat` โ type `msat`, sqltype `INTEGER` ๐ธ
- `funding_fee_rcvd_msat` โ type `msat`, sqltype `INTEGER` ๐ฐ
- `funding_pushed_msat` โ type `msat`, sqltype `INTEGER` ๐ซธ
- `total_msat` โ type `msat`, sqltype `INTEGER` ๐ฐ
- `final_to_us_msat` โ type `msat`, sqltype `INTEGER` ๐ซถ
- `min_to_us_msat` โ type `msat`, sqltype `INTEGER` ๐ฝ
- `max_to_us_msat` โ type `msat`, sqltype `INTEGER` ๐ผ
- `last_commitment_txid` โ type `hash`, sqltype `BLOB` #๏ธโฃ
- `last_commitment_fee_msat` โ type `msat`, sqltype `INTEGER` ๐ธ
- `close_cause` โ type `string`, sqltype `TEXT` ๐
- `last_stable_connection` โ type `u64`, sqltype `INTEGER` ๐ง
- **`forwards`** โ indexed by `in_channel` and `in_htlc_id` (see `lightning-listforwards(7)`) โก๏ธ
- `created_index` โ type `u64`, sqltype `INTEGER PRIMARY KEY` ๐
- `in_channel` โ type `short_channel_id`, sqltype `TEXT` ๐ฅ
- `in_htlc_id` โ type `u64`, sqltype `INTEGER` ๐
- `in_msat` โ type `msat`, sqltype `INTEGER` ๐
- `status` โ type `string`, sqltype `TEXT` ๐
- `received_time` โ type `number`, sqltype `REAL` โฐ
- `out_channel` โ type `short_channel_id`, sqltype `TEXT` ๐ค
- `out_htlc_id` โ type `u64`, sqltype `INTEGER` ๐
- `updated_index` โ type `u64`, sqltype `INTEGER` ๐
- `style` โ type `string`, sqltype `TEXT` ๐
- `fee_msat` โ type `msat`, sqltype `INTEGER` ๐ธ
- `out_msat` โ type `msat`, sqltype `INTEGER` ๐ฐ
- `resolved_time` โ type `number`, sqltype `REAL` โฑ๏ธ
- `failcode` โ type `u32`, sqltype `INTEGER` ๐
- `failreason` โ type `string`, sqltype `TEXT` ๐ญ
- **`htlcs`** โ indexed by `short_channel_id` and `id` (see `lightning-listhtlcs(7)`) ๐งพ
- `short_channel_id` โ type `short_channel_id`, sqltype `TEXT` ๐
- `created_index` โ type `u64`, sqltype `INTEGER PRIMARY KEY` ๐
- `updated_index` โ type `u64`, sqltype `INTEGER` ๐
- `id` โ type `u64`, sqltype `INTEGER` ๐
- `expiry` โ type `u32`, sqltype `INTEGER` โณ
- `amount_msat` โ type `msat`, sqltype `INTEGER` ๐ฐ
- `direction` โ type `string`, sqltype `TEXT` ๐งญ
- `payment_hash` โ type `hash`, sqltype `BLOB` #๏ธโฃ
- `state` โ type `string`, sqltype `TEXT` ๐
- **`invoices`** โ indexed by `payment_hash` (see `lightning-listinvoices(7)`) ๐งพ๐
- `label` โ type `string`, sqltype `TEXT` ๐ท๏ธ
- `description` โ type `string`, sqltype `TEXT` ๐
- `payment_hash` โ type `hash`, sqltype `BLOB` #๏ธโฃ
- `status` โ type `string`, sqltype `TEXT` ๐
- `expires_at` โ type `u64`, sqltype `INTEGER` โณ
- `amount_msat` โ type `msat`, sqltype `INTEGER` ๐ฐ
- `bolt11` โ type `string`, sqltype `TEXT` ๐งพ
- `bolt12` โ type `string`, sqltype `TEXT` ๐งพ
- `local_offer_id` โ type `hash`, sqltype `BLOB` #๏ธโฃ
- `invreq_payer_note` โ type `string`, sqltype `TEXT` ๐
- `created_index` โ type `u64`, sqltype `INTEGER PRIMARY KEY` ๐
- `updated_index` โ type `u64`, sqltype `INTEGER` ๐
- `pay_index` โ type `u64`, sqltype `INTEGER` ๐ณ
- `amount_received_msat` โ type `msat`, sqltype `INTEGER` ๐ฐ
- `paid_at` โ type `u64`, sqltype `INTEGER` โฐ
- `paid_outpoint_txid` โ type `txid`, sqltype `BLOB`, from JSON object `paid_outpoint` ๐งพ
- `paid_outpoint_outnum` โ type `u32`, sqltype `INTEGER`, from JSON object `paid_outpoint` ๐ข
- `payment_preimage` โ type `secret`, sqltype `BLOB` ๐คซ
- **`networkevents`** โ (see `lightning-listnetworkevents(7)`) ๐ก
- `created_index` โ type `u64`, sqltype `INTEGER PRIMARY KEY` ๐
- `timestamp` โ type `u64`, sqltype `INTEGER` โฐ
- `peer_id` โ type `pubkey`, sqltype `BLOB` ๐ซถ
- `type` โ type `string`, sqltype `TEXT` ๐ท๏ธ
- `reason` โ type `string`, sqltype `TEXT` ๐
- `duration_nsec` โ type `u64`, sqltype `INTEGER` โฑ๏ธ
- `connect_attempted` โ type `boolean`, sqltype `INTEGER` ๐
- **`nodes`** โ indexed by `nodeid` (see `lightning-listnodes(7)`) ๐
- `nodeid` โ type `pubkey`, sqltype `BLOB` ๐ซถ
- `last_timestamp` โ type `u32`, sqltype `INTEGER` โฐ
- `alias` โ type `string`, sqltype `TEXT` ๐ท๏ธ
- `color` โ type `hex`, sqltype `BLOB` ๐จ
- `features` โ type `hex`, sqltype `BLOB` ๐งฌ
- related table `nodes_addresses` โ the nodeโs address list ๐
- `row` โ reference to `nodes.rowid`, sqltype `INTEGER` ๐
- `arrindex` โ index within array, sqltype `INTEGER` ๐ข
- `type` โ type `string`, sqltype `TEXT` ๐ท๏ธ
- `port` โ type `u16`, sqltype `INTEGER` ๐
- `address` โ type `string`, sqltype `TEXT` ๐
- `option_will_fund_lease_fee_base_msat` โ type `msat`, sqltype `INTEGER`, from JSON object `option_will_fund` ๐ธ
- `option_will_fund_lease_fee_basis` โ type `u32`, sqltype `INTEGER`, from JSON object `option_will_fund` ๐
- `option_will_fund_funding_weight` โ type `u32`, sqltype `INTEGER`, from JSON object `option_will_fund` โ๏ธ
- `option_will_fund_channel_fee_max_base_msat` โ type `msat`, sqltype `INTEGER`, from JSON object `option_will_fund` ๐ต
- `option_will_fund_channel_fee_max_proportional_thousandths` โ type `u32`, sqltype `INTEGER`, from JSON object `option_will_fund` ๐
- `option_will_fund_compact_lease` โ type `hex`, sqltype `BLOB`, from JSON object `option_will_fund` ๐ฆ
- **`offers`** โ indexed by `offer_id` (see `lightning-listoffers(7)`) ๐
- `offer_id` โ type `hash`, sqltype `BLOB` #๏ธโฃ
- `active` โ type `boolean`, sqltype `INTEGER` โ
- `single_use` โ type `boolean`, sqltype `INTEGER` 1๏ธโฃ
- `force_paths` โ type `boolean`, sqltype `INTEGER` ๐ฃ๏ธ
- `bolt12` โ type `string`, sqltype `TEXT` ๐งพ
- `description` โ type `string`, sqltype `TEXT` ๐
- `used` โ type `boolean`, sqltype `INTEGER` โ๏ธ
- `label` โ type `string`, sqltype `TEXT` ๐ท๏ธ
- **`peerchannels`** โ indexed by `peer_id` (see `lightning-listpeerchannels(7)`) ๐ซถ๐ก
- `peer_id` โ type `pubkey`, sqltype `BLOB` ๐ซถ
- `peer_connected` โ type `boolean`, sqltype `INTEGER` ๐
- `reestablished` โ type `boolean`, sqltype `INTEGER` ๐
- `state` โ type `string`, sqltype `TEXT` ๐
- `scratch_txid` โ type `txid`, sqltype `BLOB` ๐งพ
- related table `peerchannels_channel_type_bits`, from JSON object `channel_type` โ channel-type bits, slay ๐งฉ
- `row` โ reference to `peerchannels_channel_type.rowid`, sqltype `INTEGER` ๐
- `arrindex` โ index within array, sqltype `INTEGER` ๐ข
- `bits` โ type `u32`, sqltype `INTEGER` ๐งฎ
- related table `peerchannels_channel_type_names`, from JSON object `channel_type` โ and the matching names ๐
- `row` โ reference to `peerchannels_channel_type.rowid`, sqltype `INTEGER` ๐
- `arrindex` โ index within array, sqltype `INTEGER` ๐ข
- `names` โ type `string`, sqltype `TEXT` ๐ท๏ธ
- `local_htlc_minimum_msat` โ type `msat`, sqltype `INTEGER`, from JSON object `updates.local` ๐ฝ
- `local_htlc_maximum_msat` โ type `msat`, sqltype `INTEGER`, from JSON object `updates.local` ๐ผ
- **`local_cltv_expiry_delta`** โ type `u32`, sqltype `INTEGER`, pulled straight outta the JSON object `updates.local` โจ
- **`local_fee_base_msat`** โ type `msat`, sqltype `INTEGER`, from JSON object `updates.local`
- **`local_fee_proportional_millionths`** โ type `u32`, sqltype `INTEGER`, from JSON object `updates.local`
- **`remote_htlc_minimum_msat`** โ type `msat`, sqltype `INTEGER`, from JSON object `updates.remote` ๐ซ
- **`remote_htlc_maximum_msat`** โ type `msat`, sqltype `INTEGER`, from JSON object `updates.remote`
- **`remote_cltv_expiry_delta`** โ type `u32`, sqltype `INTEGER`, from JSON object `updates.remote`
- **`remote_fee_base_msat`** โ type `msat`, sqltype `INTEGER`, from JSON object `updates.remote`
- **`remote_fee_proportional_millionths`** โ type `u32`, sqltype `INTEGER`, from JSON object `updates.remote`
- **`ignore_fee_limits`** โ type `boolean`, sqltype `INTEGER`
- **`lost_state`** โ type `boolean`, sqltype `INTEGER`
- **`feerate_perkw`** โ type `u32`, sqltype `INTEGER`, from JSON object `feerate`
- **`feerate_perkb`** โ type `u32`, sqltype `INTEGER`, from JSON object `feerate`
- **`owner`** โ type `string`, sqltype `TEXT`
- **`short_channel_id`** โ type `short_channel_id`, sqltype `TEXT`
- **`direction`** โ type `u32`, sqltype `INTEGER`
- **`channel_id`** โ type `hash`, sqltype `BLOB`
- **`funding_txid`** โ type `txid`, sqltype `BLOB`
- **`funding_outnum`** โ type `u32`, sqltype `INTEGER`
- **`initial_feerate`** โ type `string`, sqltype `TEXT`
- **`last_feerate`** โ type `string`, sqltype `TEXT`
- **`next_feerate`** โ type `string`, sqltype `TEXT`
- **`next_fee_step`** โ type `u32`, sqltype `INTEGER`
- ๐๏ธ related table **`peerchannels_inflight`**
- **`row`** โ reference to `peerchannels.rowid`, sqltype `INTEGER`
- **`arrindex`** โ index within array, sqltype `INTEGER`
- **`funding_txid`** โ type `txid`, sqltype `BLOB`
- **`funding_outnum`** โ type `u32`, sqltype `INTEGER`
- **`feerate`** โ type `string`, sqltype `TEXT`
- **`total_funding_msat`** โ type `msat`, sqltype `INTEGER`
- **`splice_amount`** โ type `integer`, sqltype `INTEGER`
- **`our_funding_msat`** โ type `msat`, sqltype `INTEGER`
- **`scratch_txid`** โ type `txid`, sqltype `BLOB`
- **`close_to`** โ type `hex`, sqltype `BLOB`
- **`private`** โ type `boolean`, sqltype `INTEGER`
- **`opener`** โ type `string`, sqltype `TEXT`
- **`closer`** โ type `string`, sqltype `TEXT`
- ๐๏ธ related table **`peerchannels_features`**
- **`row`** โ reference to `peerchannels.rowid`, sqltype `INTEGER`
- **`arrindex`** โ index within array, sqltype `INTEGER`
- **`features`** โ type `string`, sqltype `TEXT`
- **`funding_pushed_msat`** โ type `msat`, sqltype `INTEGER`, from JSON object `funding` ๐ธ
- **`funding_local_funds_msat`** โ type `msat`, sqltype `INTEGER`, from JSON object `funding`
- **`funding_remote_funds_msat`** โ type `msat`, sqltype `INTEGER`, from JSON object `funding`
- **`funding_fee_paid_msat`** โ type `msat`, sqltype `INTEGER`, from JSON object `funding`
- **`funding_fee_rcvd_msat`** โ type `msat`, sqltype `INTEGER`, from JSON object `funding`
- **`funding_psbt`** โ type `string`, sqltype `TEXT`, from JSON object `funding`
- **`funding_withheld`** โ type `boolean`, sqltype `INTEGER`, from JSON object `funding`
- **`to_us_msat`** โ type `msat`, sqltype `INTEGER`
- **`min_to_us_msat`** โ type `msat`, sqltype `INTEGER`
- **`max_to_us_msat`** โ type `msat`, sqltype `INTEGER`
- **`total_msat`** โ type `msat`, sqltype `INTEGER`
- **`fee_base_msat`** โ type `msat`, sqltype `INTEGER`
- **`fee_proportional_millionths`** โ type `u32`, sqltype `INTEGER`
- **`dust_limit_msat`** โ type `msat`, sqltype `INTEGER`
- **`their_max_htlc_value_in_flight_msat`** โ type `msat`, sqltype `INTEGER`
- **`our_max_htlc_value_in_flight_msat`** โ type `msat`, sqltype `INTEGER`
- **`their_reserve_msat`** โ type `msat`, sqltype `INTEGER`
- **`our_reserve_msat`** โ type `msat`, sqltype `INTEGER`
- **`spendable_msat`** โ type `msat`, sqltype `INTEGER`
- **`receivable_msat`** โ type `msat`, sqltype `INTEGER`
- **`minimum_htlc_in_msat`** โ type `msat`, sqltype `INTEGER`
- **`minimum_htlc_out_msat`** โ type `msat`, sqltype `INTEGER`
- **`maximum_htlc_out_msat`** โ type `msat`, sqltype `INTEGER`
- **`their_to_self_delay`** โ type `u32`, sqltype `INTEGER`
- **`our_to_self_delay`** โ type `u32`, sqltype `INTEGER`
- **`max_accepted_htlcs`** โ type `u32`, sqltype `INTEGER`
- **`alias_local`** โ type `short_channel_id`, sqltype `TEXT`, from JSON object `alias`
- **`alias_remote`** โ type `short_channel_id`, sqltype `TEXT`, from JSON object `alias` โจ
- ๐๏ธ related table **`peerchannels_state_changes`**
- **`row`** โ reference to `peerchannels.rowid`, sqltype `INTEGER`
- **`arrindex`** โ index within array, sqltype `INTEGER`
- **`timestamp`** โ type `string`, sqltype `TEXT`
- **`old_state`** โ type `string`, sqltype `TEXT`
- **`new_state`** โ type `string`, sqltype `TEXT`
- **`cause`** โ type `string`, sqltype `TEXT`
- **`message`** โ type `string`, sqltype `TEXT`
- ๐๏ธ related table **`peerchannels_status`**
- **`row`** โ reference to `peerchannels.rowid`, sqltype `INTEGER`
- **`arrindex`** โ index within array, sqltype `INTEGER`
- **`status`** โ type `string`, sqltype `TEXT`
- **`in_payments_offered`** โ type `u64`, sqltype `INTEGER`
- **`in_offered_msat`** โ type `msat`, sqltype `INTEGER`
- **`in_payments_fulfilled`** โ type `u64`, sqltype `INTEGER`
- **`in_fulfilled_msat`** โ type `msat`, sqltype `INTEGER`
- **`out_payments_offered`** โ type `u64`, sqltype `INTEGER`
- **`out_offered_msat`** โ type `msat`, sqltype `INTEGER`
- **`out_payments_fulfilled`** โ type `u64`, sqltype `INTEGER`
- **`out_fulfilled_msat`** โ type `msat`, sqltype `INTEGER`
- **`last_stable_connection`** โ type `u64`, sqltype `INTEGER`
- ๐๏ธ related table **`peerchannels_htlcs`**
- **`row`** โ reference to `peerchannels.rowid`, sqltype `INTEGER`
- **`arrindex`** โ index within array, sqltype `INTEGER`
- **`direction`** โ type `string`, sqltype `TEXT`
- **`id`** โ type `u64`, sqltype `INTEGER`
- **`amount_msat`** โ type `msat`, sqltype `INTEGER`
- **`expiry`** โ type `u32`, sqltype `INTEGER`
- **`payment_hash`** โ type `hash`, sqltype `BLOB`
- **`local_trimmed`** โ type `boolean`, sqltype `INTEGER`
- **`status`** โ type `string`, sqltype `TEXT`
- **`state`** โ type `string`, sqltype `TEXT`
- **`close_to_addr`** โ type `string`, sqltype `TEXT`
- **`last_tx_fee_msat`** โ type `msat`, sqltype `INTEGER`
And that's a wrap on `peerchannels` ๐ next table bestie:
- ๐ซถ **`peers`** indexed by `id` (see `lightning-listpeers(7)`)
- **`id`** โ type `pubkey`, sqltype `BLOB`
- **`connected`** โ type `boolean`, sqltype `INTEGER`
- **`num_channels`** โ type `u32`, sqltype `INTEGER`
- ๐๏ธ related table **`peers_netaddr`**
- **`row`** โ reference to `peers.rowid`, sqltype `INTEGER`
- **`arrindex`** โ index within array, sqltype `INTEGER`
- **`netaddr`** โ type `string`, sqltype `TEXT`
- **`remote_addr`** โ type `string`, sqltype `TEXT`
- **`features`** โ type `hex`, sqltype `BLOB`
- ๐
**`sendpays`** indexed by `payment_hash` (see `lightning-listsendpays(7)`)
- **`created_index`** โ type `u64`, sqltype `INTEGER PRIMARY KEY` ๐ (yes, the whole `INTEGER PRIMARY KEY` is the sqltype, that's not a typo)
- **`id`** โ type `u64`, sqltype `INTEGER`
- **`groupid`** โ type `u64`, sqltype `INTEGER`
- **`partid`** โ type `u64`, sqltype `INTEGER`
- **`payment_hash`** โ type `hash`, sqltype `BLOB`
- **`updated_index`** โ type `u64`, sqltype `INTEGER`
- **`status`** โ type `string`, sqltype `TEXT`
- **`amount_msat`** โ type `msat`, sqltype `INTEGER`
- **`destination`** โ type `pubkey`, sqltype `BLOB`
- **`created_at`** โ type `u64`, sqltype `INTEGER`
- **`amount_sent_msat`** โ type `msat`, sqltype `INTEGER`
- **`label`** โ type `string`, sqltype `TEXT`
- **`bolt11`** โ type `string`, sqltype `TEXT`
- **`description`** โ type `string`, sqltype `TEXT`
- **`bolt12`** โ type `string`, sqltype `TEXT`
- **`completed_at`** โ type `u64`, sqltype `INTEGER`
- **`payment_preimage`** โ type `secret`, sqltype `BLOB`
- **`erroronion`** โ type `hex`, sqltype `BLOB`
- ๐ฆ **`transactions`** indexed by `hash` (see `lightning-listtransactions(7)`)
- **`hash`** โ type `txid`, sqltype `BLOB`
- **`rawtx`** โ type `hex`, sqltype `BLOB`
- **`blockheight`** โ type `u32`, sqltype `INTEGER`
- **`txindex`** โ type `u32`, sqltype `INTEGER`
- **`locktime`** โ type `u32`, sqltype `INTEGER`
- **`version`** โ type `u32`, sqltype `INTEGER`
- ๐๏ธ related table **`transactions_inputs`**
- **`row`** โ reference to `transactions.rowid`, sqltype `INTEGER`
- **`arrindex`** โ index within array, sqltype `INTEGER`
- **`txid`** โ type `txid`, sqltype `BLOB`
- **`idx`** โ type `u32`, sqltype `INTEGER`, from JSON field `index`
- **`sequence`** โ type `u32`, sqltype `INTEGER`
- ๐๏ธ related table **`transactions_outputs`**
- **`row`** โ reference to `transactions.rowid`, sqltype `INTEGER`
- **`arrindex`** โ index within array, sqltype `INTEGER`
- **`idx`** โ type `u32`, sqltype `INTEGER`, from JSON field `index`
- **`amount_msat`** โ type `msat`, sqltype `INTEGER`
- **`scriptPubKey`** โ type `hex`, sqltype `BLOB`
---
# ๐ RETURN VALUE ๐
When it all goes right, you get an object with `rows` in it, and `rows` is an array. Every entry inside that array is itself an array of values โ and each one of those values is an integer, real number, string or null, depending on the sqlite3 type. Simple as that ๐ซถ
The object may contain `warning_db_failure` if the database fails partway through its operation. On success, an object is returned, containing:
- **`rows`** (array of arrays):
- `(array)`
The following warnings may also be returned:
- **`warning_db_failure`**: A message if the database encounters an error partway through. Like, it got most of the way there and then tripped ๐ญ at least it tells you.
---
# ๐ญ ERRORS ๐ญ
On failure, an error is returned. That's the whole vibe. No extra lore, no secret sauce โ if it breaks, you get an error.
---
# ๐ซถ AUTHOR ๐ซถ
`Rusty Russell` `rusty@rustcorp.com.au` is mainly responsible. Iconic behavior honestly ๐
---
# ๐ SEE ALSO ๐
- `lightning-listtransactions(7)`
- `lightning-listhtlcs(7)`
- `lightning-listinvoices(7)`
- `lightning-listoffers(7)`
- `lightning-listchannels(7)`
- `lightning-listpeers(7)`
- `lightning-listpeerchannels(7)`
- `lightning-listsendpays(7)`
- `lightning-listchainmoves(7)`
- `lightning-listchannelmoves(7)`
- `lightning-bkpr-listaccountevents(7)`
- `lightning-bkpr-listincome(7)`
- `lightning-listnodes(7)`
- `lightning-listforwards(7)`
That's a whole reading list bestie, go click through them ๐
---
# ๐ RESOURCES ๐
Main web site: `https://github.com/ElementsProject/lightning`
# โจ EXAMPLES โจ
Okay bestie, these are the little real-world moments where the `sql` command absolutely eats. ๐
All the commands and JSON are copied exactly, so you can run them and see the magic yourself. โจ
### ๐ Example 1: A simple peers selection query ๐
**Request:**
$ lightning-cli sql -k "query"="SELECT id FROM peers"
{ "id": "example:sql#1", "method": "sql", "params": { "query": "SELECT id FROM peers" } }
**Response:**
{ "rows": [ [ "nodeid020202020202020202020202020202020202020202020202020202020202" ] ] }
---
### ๐ฆ Example 2: A statement containing `=` needs `-o` in shell ๐ฆ
Lowkey important: if your SQL has `=` in it, the shell is gonna be nosy, so use `-o`. ๐ค
**Request:**
$ lightning-cli sql -o "SELECT label, description, status FROM invoices WHERE label='label inv_l12'"
{ "id": "example:sql#2", "method": "sql", "params": [ "SELECT label, description, status FROM invoices WHERE label='label inv_l12'" ] }
**Response:**
{ "rows": [ [ "label inv_l12", "description inv_l12", "unpaid" ] ] }
---
### ๐ธ Example 3: Getting specific `nodeid` values from the `nodes` table ๐ธ
You want certain nodeids? Say less. ๐
**Request:**
$ lightning-cli sql -o "SELECT nodeid FROM nodes WHERE nodeid != x'nodeid030303030303030303030303030303030303030303030303030303030303'"
{ "id": "example:sql#3", "method": "sql", "params": [ "SELECT nodeid FROM nodes WHERE nodeid != x'nodeid030303030303030303030303030303030303030303030303030303030303'" ] }
**Response:**
{ "rows": [ [ "nodeid020202020202020202020202020202020202020202020202020202020202" ], [ "nodeid010101010101010101010101010101010101010101010101010101010101" ], [ "nodeid040404040404040404040404040404040404040404040404040404040404" ] ] }
---
### ๐ง Example 4: Comparing a `BLOB` column โ use `x'hex'` or `X'hex'` ๐ง
If you're comparing a `BLOB` column, you literally need `x'hex'` or `X'hex'`. No cap. ๐
**Request:**
$ lightning-cli sql "SELECT nodeid FROM nodes WHERE nodeid IN (x'nodeid010101010101010101010101010101010101010101010101010101010101', x'nodeid030303030303030303030303030303030303030303030303030303030303')"
{ "id": "example:sql#4", "method": "sql", "params": [ "SELECT nodeid FROM nodes WHERE nodeid IN (x'nodeid010101010101010101010101010101010101010101010101010101010101', x'nodeid030303030303030303030303030303030303030303030303030303030303')" ] }
**Response:**
{ "rows": [ [ "nodeid010101010101010101010101010101010101010101010101010101010101" ], [ "nodeid030303030303030303030303030303030303030303030303030303030303" ] ] }
---
### ๐ Example 5: Related tables are usually referenced by `JOIN` ๐
This is giving โletโs bring the whole friend group together.โ ๐ซถ
**Request:**
$ lightning-cli sql -o 'SELECT peer_id, to_us_msat, total_msat, peerchannels_status.status FROM peerchannels INNER JOIN peerchannels_status ON peerchannels_status.row = peerchannels.rowid'
{ "id": "example:sql#5", "method": "sql", "params": [ "SELECT peer_id, to_us_msat, total_msat, peerchannels_status.status FROM peerchannels INNER JOIN peerchannels_status ON peerchannels_status.row = peerchannels.rowid" ] }
**Response:**
{ "rows": [ [ "nodeid020202020202020202020202020202020202020202020202020202020202", 490493792, 1000000000, "CHANNELD_NORMAL:Channel ready for use." ] ] }
---
### ๐ Example 6: Simple function usage โ `COUNT` says hi ๐
Strings inside arrays need `"`, and `'` to protect them from the shell. Shell-proofing is self-care. โจ
**Request:**
$ lightning-cli sql "SELECT COUNT(*) FROM forwards"
{ "id": "example:sql#6", "method": "sql", "params": [ "SELECT COUNT(*) FROM forwards" ] }
**Response:**
{ "rows": [ [ 9 ] ] }
---
### ๐ Example 7: `SELECT *` from `peerchannels_features` ๐
Main character query tbh. ๐
**Request:**
$ lightning-cli sql "SELECT * from peerchannels_features"
{ "id": "example:sql#7", "method": "sql", "params": [ "SELECT * from peerchannels_features" ] }
**Response:**
{ "rows": [ [ 28, 23, 0, "option_static_remotekey" ], [ 30, 23, 2, "option_anchors" ] ] }
---
Thatโs the whole page, bestie. Cute little footer moment: source/version is `Core Lightning v26.06.9`, and the page tag is `LIGHTNING-SQL(7)`. Slay, bye. ๐
๐
๐ฆ where this page comes from
- nixpkgs unstable: clightning 26.06.9
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms