LIGHTNING-SQL(7)

๐Ÿ“ฆ
version 26.06.9 ยท 2 versions
, in nixpkgs unstable

๐Ÿ’… 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