Prompted by @shahanafarooqui's playing with examples and finding common errors. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
18 KiB
lightning-sql -- Command to do complex queries on list commands
SYNOPSIS
sql query
DESCRIPTION
The sql RPC command runs the given query across a sqlite3 database created from various list commands.
When tables are accessed, it calls the above 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.
It is, however faster for remote access if the result of the query is much smaller than the list commands would be.
Note that queries like "SELECT *" are fragile, as columns will change across releases; see lightning-listsqlschemas(7).
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.
-
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
PERMITTED SQLITE3 FUNCTIONS
Writing to the database is not permitted, and limits are placed on various other query parameters.
Additionally, only the following functions are allowed:
- abs
- avg
- coalesce
- count
- hex
- quote
- length
- like
- lower
- upper
- min
- max
- sum
- total
TABLES
The following tables are currently supported:
-
bkpr_accountevents
(see lightning-bkpr-listaccountevents(7))account
(typestring
, sqltypeTEXT
)type
(typestring
, sqltypeTEXT
)tag
(typestring
, sqltypeTEXT
)credit_msat
(typemsat
, sqltypeINTEGER
)debit_msat
(typemsat
, sqltypeINTEGER
)currency
(typestring
, sqltypeTEXT
)timestamp
(typeu32
, sqltypeINTEGER
)outpoint
(typestring
, sqltypeTEXT
)blockheight
(typeu32
, sqltypeINTEGER
)origin
(typestring
, sqltypeTEXT
)payment_id
(typehex
, sqltypeBLOB
)txid
(typetxid
, sqltypeBLOB
)description
(typestring
, sqltypeTEXT
)fees_msat
(typemsat
, sqltypeINTEGER
)is_rebalance
(typeboolean
, sqltypeINTEGER
)part_id
(typeu32
, sqltypeINTEGER
)
-
bkpr_income
(see lightning-bkpr-listincome(7))account
(typestring
, sqltypeTEXT
)tag
(typestring
, sqltypeTEXT
)credit_msat
(typemsat
, sqltypeINTEGER
)debit_msat
(typemsat
, sqltypeINTEGER
)currency
(typestring
, sqltypeTEXT
)timestamp
(typeu32
, sqltypeINTEGER
)description
(typestring
, sqltypeTEXT
)outpoint
(typestring
, sqltypeTEXT
)txid
(typetxid
, sqltypeBLOB
)payment_id
(typehex
, sqltypeBLOB
)
-
channels
indexed byshort_channel_id
(see lightning-listchannels(7))source
(typepubkey
, sqltypeBLOB
)destination
(typepubkey
, sqltypeBLOB
)short_channel_id
(typeshort_channel_id
, sqltypeTEXT
)direction
(typeu32
, sqltypeINTEGER
)public
(typeboolean
, sqltypeINTEGER
)amount_msat
(typemsat
, sqltypeINTEGER
)message_flags
(typeu8
, sqltypeINTEGER
)channel_flags
(typeu8
, sqltypeINTEGER
)active
(typeboolean
, sqltypeINTEGER
)last_update
(typeu32
, sqltypeINTEGER
)base_fee_millisatoshi
(typeu32
, sqltypeINTEGER
)fee_per_millionth
(typeu32
, sqltypeINTEGER
)delay
(typeu32
, sqltypeINTEGER
)htlc_minimum_msat
(typemsat
, sqltypeINTEGER
)htlc_maximum_msat
(typemsat
, sqltypeINTEGER
)features
(typehex
, sqltypeBLOB
)
-
forwards
indexed byin_channel and in_htlc_id
(see lightning-listforwards(7))in_channel
(typeshort_channel_id
, sqltypeTEXT
)in_htlc_id
(typeu64
, sqltypeINTEGER
)in_msat
(typemsat
, sqltypeINTEGER
)status
(typestring
, sqltypeTEXT
)received_time
(typenumber
, sqltypeREAL
)out_channel
(typeshort_channel_id
, sqltypeTEXT
)out_htlc_id
(typeu64
, sqltypeINTEGER
)style
(typestring
, sqltypeTEXT
)fee_msat
(typemsat
, sqltypeINTEGER
)out_msat
(typemsat
, sqltypeINTEGER
)resolved_time
(typenumber
, sqltypeREAL
)failcode
(typeu32
, sqltypeINTEGER
)failreason
(typestring
, sqltypeTEXT
)
-
htlcs
indexed byshort_channel_id and id
(see lightning-listhtlcs(7))short_channel_id
(typeshort_channel_id
, sqltypeTEXT
)id
(typeu64
, sqltypeINTEGER
)expiry
(typeu32
, sqltypeINTEGER
)amount_msat
(typemsat
, sqltypeINTEGER
)direction
(typestring
, sqltypeTEXT
)payment_hash
(typehash
, sqltypeBLOB
)state
(typestring
, sqltypeTEXT
)
-
invoices
indexed bypayment_hash
(see lightning-listinvoices(7))label
(typestring
, sqltypeTEXT
)description
(typestring
, sqltypeTEXT
)payment_hash
(typehash
, sqltypeBLOB
)status
(typestring
, sqltypeTEXT
)expires_at
(typeu64
, sqltypeINTEGER
)amount_msat
(typemsat
, sqltypeINTEGER
)bolt11
(typestring
, sqltypeTEXT
)bolt12
(typestring
, sqltypeTEXT
)local_offer_id
(typehash
, sqltypeBLOB
)invreq_payer_note
(typestring
, sqltypeTEXT
)pay_index
(typeu64
, sqltypeINTEGER
)amount_received_msat
(typemsat
, sqltypeINTEGER
)paid_at
(typeu64
, sqltypeINTEGER
)payment_preimage
(typesecret
, sqltypeBLOB
)
-
nodes
indexed bynodeid
(see lightning-listnodes(7))nodeid
(typepubkey
, sqltypeBLOB
)last_timestamp
(typeu32
, sqltypeINTEGER
)alias
(typestring
, sqltypeTEXT
)color
(typehex
, sqltypeBLOB
)features
(typehex
, sqltypeBLOB
)- related table
nodes_addresses
row
(reference tonodes.rowid
, sqltypeINTEGER
)arrindex
(index within array, sqltypeINTEGER
)type
(typestring
, sqltypeTEXT
)port
(typeu16
, sqltypeINTEGER
)address
(typestring
, sqltypeTEXT
)
option_will_fund_lease_fee_base_msat
(typemsat
, sqltypeINTEGER
, from JSON objectoption_will_fund
)option_will_fund_lease_fee_basis
(typeu32
, sqltypeINTEGER
, from JSON objectoption_will_fund
)option_will_fund_funding_weight
(typeu32
, sqltypeINTEGER
, from JSON objectoption_will_fund
)option_will_fund_channel_fee_max_base_msat
(typemsat
, sqltypeINTEGER
, from JSON objectoption_will_fund
)option_will_fund_channel_fee_max_proportional_thousandths
(typeu32
, sqltypeINTEGER
, from JSON objectoption_will_fund
)option_will_fund_compact_lease
(typehex
, sqltypeBLOB
, from JSON objectoption_will_fund
)
-
offers
indexed byoffer_id
(see lightning-listoffers(7))offer_id
(typehash
, sqltypeBLOB
)active
(typeboolean
, sqltypeINTEGER
)single_use
(typeboolean
, sqltypeINTEGER
)bolt12
(typestring
, sqltypeTEXT
)used
(typeboolean
, sqltypeINTEGER
)label
(typestring
, sqltypeTEXT
)
-
peerchannels
indexed bypeer_id
(see lightning-listpeerchannels(7))peer_id
(typepubkey
, sqltypeBLOB
)peer_connected
(typeboolean
, sqltypeINTEGER
)state
(typestring
, sqltypeTEXT
)scratch_txid
(typetxid
, sqltypeBLOB
)feerate_perkw
(typeu32
, sqltypeINTEGER
, from JSON objectfeerate
)feerate_perkb
(typeu32
, sqltypeINTEGER
, from JSON objectfeerate
)owner
(typestring
, sqltypeTEXT
)short_channel_id
(typeshort_channel_id
, sqltypeTEXT
)channel_id
(typehash
, sqltypeBLOB
)funding_txid
(typetxid
, sqltypeBLOB
)funding_outnum
(typeu32
, sqltypeINTEGER
)initial_feerate
(typestring
, sqltypeTEXT
)last_feerate
(typestring
, sqltypeTEXT
)next_feerate
(typestring
, sqltypeTEXT
)next_fee_step
(typeu32
, sqltypeINTEGER
)- related table
peerchannels_inflight
row
(reference topeerchannels.rowid
, sqltypeINTEGER
)arrindex
(index within array, sqltypeINTEGER
)funding_txid
(typetxid
, sqltypeBLOB
)funding_outnum
(typeu32
, sqltypeINTEGER
)feerate
(typestring
, sqltypeTEXT
)total_funding_msat
(typemsat
, sqltypeINTEGER
)our_funding_msat
(typemsat
, sqltypeINTEGER
)scratch_txid
(typetxid
, sqltypeBLOB
)
close_to
(typehex
, sqltypeBLOB
)private
(typeboolean
, sqltypeINTEGER
)opener
(typestring
, sqltypeTEXT
)closer
(typestring
, sqltypeTEXT
)- related table
peerchannels_features
row
(reference topeerchannels.rowid
, sqltypeINTEGER
)arrindex
(index within array, sqltypeINTEGER
)features
(typestring
, sqltypeTEXT
)
funding_pushed_msat
(typemsat
, sqltypeINTEGER
, from JSON objectfunding
)funding_local_funds_msat
(typemsat
, sqltypeINTEGER
, from JSON objectfunding
)funding_remote_funds_msat
(typemsat
, sqltypeINTEGER
, from JSON objectfunding
)funding_fee_paid_msat
(typemsat
, sqltypeINTEGER
, from JSON objectfunding
)funding_fee_rcvd_msat
(typemsat
, sqltypeINTEGER
, from JSON objectfunding
)to_us_msat
(typemsat
, sqltypeINTEGER
)min_to_us_msat
(typemsat
, sqltypeINTEGER
)max_to_us_msat
(typemsat
, sqltypeINTEGER
)total_msat
(typemsat
, sqltypeINTEGER
)fee_base_msat
(typemsat
, sqltypeINTEGER
)fee_proportional_millionths
(typeu32
, sqltypeINTEGER
)dust_limit_msat
(typemsat
, sqltypeINTEGER
)max_total_htlc_in_msat
(typemsat
, sqltypeINTEGER
)their_reserve_msat
(typemsat
, sqltypeINTEGER
)our_reserve_msat
(typemsat
, sqltypeINTEGER
)spendable_msat
(typemsat
, sqltypeINTEGER
)receivable_msat
(typemsat
, sqltypeINTEGER
)minimum_htlc_in_msat
(typemsat
, sqltypeINTEGER
)minimum_htlc_out_msat
(typemsat
, sqltypeINTEGER
)maximum_htlc_out_msat
(typemsat
, sqltypeINTEGER
)their_to_self_delay
(typeu32
, sqltypeINTEGER
)our_to_self_delay
(typeu32
, sqltypeINTEGER
)max_accepted_htlcs
(typeu32
, sqltypeINTEGER
)alias_local
(typeshort_channel_id
, sqltypeTEXT
, from JSON objectalias
)alias_remote
(typeshort_channel_id
, sqltypeTEXT
, from JSON objectalias
)- related table
peerchannels_state_changes
row
(reference topeerchannels.rowid
, sqltypeINTEGER
)arrindex
(index within array, sqltypeINTEGER
)timestamp
(typestring
, sqltypeTEXT
)old_state
(typestring
, sqltypeTEXT
)new_state
(typestring
, sqltypeTEXT
)cause
(typestring
, sqltypeTEXT
)message
(typestring
, sqltypeTEXT
)
- related table
peerchannels_status
row
(reference topeerchannels.rowid
, sqltypeINTEGER
)arrindex
(index within array, sqltypeINTEGER
)status
(typestring
, sqltypeTEXT
)
in_payments_offered
(typeu64
, sqltypeINTEGER
)in_offered_msat
(typemsat
, sqltypeINTEGER
)in_payments_fulfilled
(typeu64
, sqltypeINTEGER
)in_fulfilled_msat
(typemsat
, sqltypeINTEGER
)out_payments_offered
(typeu64
, sqltypeINTEGER
)out_offered_msat
(typemsat
, sqltypeINTEGER
)out_payments_fulfilled
(typeu64
, sqltypeINTEGER
)out_fulfilled_msat
(typemsat
, sqltypeINTEGER
)- related table
peerchannels_htlcs
row
(reference topeerchannels.rowid
, sqltypeINTEGER
)arrindex
(index within array, sqltypeINTEGER
)direction
(typestring
, sqltypeTEXT
)id
(typeu64
, sqltypeINTEGER
)amount_msat
(typemsat
, sqltypeINTEGER
)expiry
(typeu32
, sqltypeINTEGER
)payment_hash
(typehash
, sqltypeBLOB
)local_trimmed
(typeboolean
, sqltypeINTEGER
)status
(typestring
, sqltypeTEXT
)state
(typestring
, sqltypeTEXT
)
close_to_addr
(typestring
, sqltypeTEXT
)last_tx_fee_msat
(typemsat
, sqltypeINTEGER
)direction
(typeu32
, sqltypeINTEGER
)
-
peers
indexed byid
(see lightning-listpeers(7))id
(typepubkey
, sqltypeBLOB
)connected
(typeboolean
, sqltypeINTEGER
)- related table
peers_netaddr
row
(reference topeers.rowid
, sqltypeINTEGER
)arrindex
(index within array, sqltypeINTEGER
)netaddr
(typestring
, sqltypeTEXT
)
remote_addr
(typestring
, sqltypeTEXT
)features
(typehex
, sqltypeBLOB
)
-
sendpays
indexed bypayment_hash
(see lightning-listsendpays(7))id
(typeu64
, sqltypeINTEGER
)groupid
(typeu64
, sqltypeINTEGER
)partid
(typeu64
, sqltypeINTEGER
)payment_hash
(typehash
, sqltypeBLOB
)status
(typestring
, sqltypeTEXT
)amount_msat
(typemsat
, sqltypeINTEGER
)destination
(typepubkey
, sqltypeBLOB
)created_at
(typeu64
, sqltypeINTEGER
)amount_sent_msat
(typemsat
, sqltypeINTEGER
)label
(typestring
, sqltypeTEXT
)bolt11
(typestring
, sqltypeTEXT
)description
(typestring
, sqltypeTEXT
)bolt12
(typestring
, sqltypeTEXT
)payment_preimage
(typesecret
, sqltypeBLOB
)erroronion
(typehex
, sqltypeBLOB
)
-
transactions
indexed byhash
(see lightning-listtransactions(7))hash
(typetxid
, sqltypeBLOB
)rawtx
(typehex
, sqltypeBLOB
)blockheight
(typeu32
, sqltypeINTEGER
)txindex
(typeu32
, sqltypeINTEGER
)locktime
(typeu32
, sqltypeINTEGER
)version
(typeu32
, sqltypeINTEGER
)- related table
transactions_inputs
row
(reference totransactions.rowid
, sqltypeINTEGER
)arrindex
(index within array, sqltypeINTEGER
)txid
(typetxid
, sqltypeBLOB
)idx
(typeu32
, sqltypeINTEGER
, from JSON fieldindex
)sequence
(typeu32
, sqltypeINTEGER
)type
(typestring
, sqltypeTEXT
)channel
(typeshort_channel_id
, sqltypeTEXT
)
- related table
transactions_outputs
row
(reference totransactions.rowid
, sqltypeINTEGER
)arrindex
(index within array, sqltypeINTEGER
)idx
(typeu32
, sqltypeINTEGER
, from JSON fieldindex
)amount_msat
(typemsat
, sqltypeINTEGER
)scriptPubKey
(typehex
, sqltypeBLOB
)type
(typestring
, sqltypeTEXT
)channel
(typeshort_channel_id
, sqltypeTEXT
)
RETURN VALUE
On success, an object containing rows is returned. It is an array. Each array entry contains an array of values, each an integer, real number, string or null, depending on the sqlite3 type.
The object may contain warning_db_failure if the database fails partway through its operation.
On failure, an error is returned.
EXAMPLES
Here are some example using lightning-cli. Note that you may need to
use -o
if you use queries which contain =
(which make
lightning-cli(1) default to keyword style):
A simple peer selection query:
$ lightning-cli sql "SELECT id FROM peers"
{
"rows": [
[
"02ba9965e3db660385bd1dd2c09dd032e0f2179a94fc5db8917b60adf0b363da00"
]
]
}
A statement containing using =
needs -o
:
$ lightning-cli sql -o "SELECT node_id,last_timestamp FROM nodes WHERE last_timestamp>=1669578892"
{
"rows": [
[
"02ba9965e3db660385bd1dd2c09dd032e0f2179a94fc5db8917b60adf0b363da00",
1669601603
]
]
}
If you want to compare a BLOB column, x'hex'
or X'hex'
are needed:
$ lightning-cli sql -o "SELECT nodeid FROM nodes WHERE nodeid != x'03c9d25b6c0ce4bde5ad97d7ab83f00ae8bd3800a98ccbee36f3c3205315147de1';"
{
"rows": [
[
"0214739d625944f8fdc0da9d2ef44dbd7af58443685e494117b51410c5c3ff973a"
],
[
"02ba9965e3db660385bd1dd2c09dd032e0f2179a94fc5db8917b60adf0b363da00"
]
]
}
$ lightning-cli sql -o "SELECT nodeid FROM nodes WHERE nodeid IN (x'03c9d25b6c0ce4bde5ad97d7ab83f00ae8bd3800a98ccbee36f3c3205315147de1', x'02ba9965e3db660385bd1dd2c09dd032e0f2179a94fc5db8917b60adf0b363da00')"
{
"rows": [
[
"02ba9965e3db660385bd1dd2c09dd032e0f2179a94fc5db8917b60adf0b363da00"
],
[
"03c9d25b6c0ce4bde5ad97d7ab83f00ae8bd3800a98ccbee36f3c3205315147de1"
]
]
}
Related tables are usually referenced by JOIN:
$ lightning-cli sql -o "SELECT nodeid, alias, nodes_addresses.type, nodes_addresses.port, nodes_addresses.address FROM nodes INNER JOIN nodes_addresses ON nodes_addresses.row = nodes.rowid"
{
"rows": [
[
"02ba9965e3db660385bd1dd2c09dd032e0f2179a94fc5db8917b60adf0b363da00",
"YELLOWWATCH-22.11rc2-31-gcd7593b",
"dns",
7272,
"localhost"
],
[
"0214739d625944f8fdc0da9d2ef44dbd7af58443685e494117b51410c5c3ff973a",
"HOPPINGSQUIRREL-1rc2-31-gcd7593b",
"dns",
7171,
"localhost"
]
]
}
Simple function usage, in this case COUNT. Strings inside arrays need ", and ' to protect them from the shell:
$ lightning-cli sql 'SELECT COUNT(*) FROM nodes"
{
"rows": [
[
3
]
]
}
AUTHOR
Rusty Russell <rusty@rustcorp.com.au> is mainly responsible.
SEE ALSO
lightning-listtransactions(7), lightning-listchannels(7), lightning-listpeers(7), lightning-listnodes(7), lightning-listforwards(7).
RESOURCES
Main web site: https://github.com/ElementsProject/lightning comment: # ( SHA256STAMP:dbb9286cf31dc82b33143d5274b1c4eecc75c5ba1dfc18bdf21b4baab585bd45)