From 580100f15b06b5c7e5ea7e899bfcac97a06167c8 Mon Sep 17 00:00:00 2001 From: Miles Zhang Date: Mon, 15 Jul 2024 17:11:23 +0800 Subject: [PATCH 1/4] Issue 740 (#2047) * feat: partitioned cell_outputs by status Signed-off-by: Miles Zhang * feat: set timeout 0 for long running migration Signed-off-by: Miles Zhang * fix: adjust cell_outputs migration Signed-off-by: Miles Zhang * fix: update status first when update cell_inputs Signed-off-by: Miles Zhang * chore: update structure.sql file Signed-off-by: Miles Zhang * fix: CellOutput query better to add status Signed-off-by: Miles Zhang --------- Signed-off-by: Miles Zhang --- app/models/cell_output.rb | 15 +- .../ckb_sync/new_node_data_processor.rb | 9 +- app/models/referring_cell.rb | 1 - ...rename_cell_outputs_to_cell_outputs_old.rb | 9 + .../20240709131132_create_cell_outputs.rb | 47 + ...import_cell_outputs_old_to_cell_outputs.rb | 18 + ...0240709142013_add_index_to_cell_outputs.rb | 13 + db/structure.sql | 1016 +++++++++++++++-- 8 files changed, 1034 insertions(+), 94 deletions(-) create mode 100644 db/migrate/20240709131020_rename_cell_outputs_to_cell_outputs_old.rb create mode 100644 db/migrate/20240709131132_create_cell_outputs.rb create mode 100644 db/migrate/20240709131713_import_cell_outputs_old_to_cell_outputs.rb create mode 100644 db/migrate/20240709142013_add_index_to_cell_outputs.rb diff --git a/app/models/cell_output.rb b/app/models/cell_output.rb index faded8e64..05bd0aaa8 100644 --- a/app/models/cell_output.rb +++ b/app/models/cell_output.rb @@ -1,6 +1,8 @@ class CellOutput < ApplicationRecord include CellOutputs::ExtraInfo + self.primary_key = :id + SYSTEM_TX_HASH = "0x0000000000000000000000000000000000000000000000000000000000000000".freeze MAXIMUM_DOWNLOADABLE_SIZE = 64000 MIN_SUDT_AMOUNT_BYTESIZE = 16 @@ -298,11 +300,10 @@ def self.update_cell_types_for_cota # # id :bigint not null, primary key # capacity :decimal(64, 2) -# data :binary # ckb_transaction_id :bigint # created_at :datetime not null # updated_at :datetime not null -# status :integer default("live") +# status :integer default("live"), not null # address_id :decimal(30, ) # block_id :decimal(30, ) # tx_hash :binary @@ -322,17 +323,13 @@ def self.update_cell_types_for_cota # # Indexes # -# index_cell_outputs_on_address_id_and_status (address_id,status) +# index_cell_outputs_on_address_id (address_id) # index_cell_outputs_on_block_id (block_id) # index_cell_outputs_on_block_timestamp (block_timestamp) -# index_cell_outputs_on_cell_type (cell_type) -# index_cell_outputs_on_ckb_transaction_id_and_cell_index (ckb_transaction_id,cell_index) UNIQUE # index_cell_outputs_on_consumed_block_timestamp (consumed_block_timestamp) # index_cell_outputs_on_consumed_by_id (consumed_by_id) -# index_cell_outputs_on_data_hash (data_hash) USING hash # index_cell_outputs_on_lock_script_id (lock_script_id) -# index_cell_outputs_on_status (status) -# index_cell_outputs_on_tx_hash_and_cell_index (tx_hash,cell_index) UNIQUE +# index_cell_outputs_on_tx_hash_and_cell_index_and_status (tx_hash,cell_index,status) UNIQUE +# index_cell_outputs_on_tx_id_and_cell_index_and_status (ckb_transaction_id,cell_index,status) UNIQUE # index_cell_outputs_on_type_script_id (type_script_id) -# index_cell_outputs_on_type_script_id_and_id (type_script_id,id) # diff --git a/app/models/ckb_sync/new_node_data_processor.rb b/app/models/ckb_sync/new_node_data_processor.rb index 84751cf2a..953d0c4d7 100644 --- a/app/models/ckb_sync/new_node_data_processor.rb +++ b/app/models/ckb_sync/new_node_data_processor.rb @@ -846,7 +846,7 @@ def build_cells_and_locks!( build_cell_outputs!(node_block, outputs, ckb_txs, local_block, cell_outputs_attributes, output_capacities, tags, udt_address_ids, dao_address_ids, contained_udt_ids, contained_addr_ids, addrs_changes, token_transfer_ckb_tx_ids) if cell_outputs_attributes.present? - id_hashes = CellOutput.upsert_all(cell_outputs_attributes, unique_by: %i[tx_hash cell_index], + id_hashes = CellOutput.upsert_all(cell_outputs_attributes, unique_by: %i[tx_hash cell_index status], returning: %i[id data_hash]) cell_data_attrs = [] @@ -873,8 +873,11 @@ def build_cells_and_locks!( CellInput.upsert_all(cell_inputs_attributes, unique_by: %i[ckb_transaction_id index]) if prev_cell_outputs_attributes.present? + cell_ouput_ids = prev_cell_outputs_attributes.map { |attr| attr[:id] } + CellOutput.live.where(id: cell_ouput_ids).update_all(status: :dead) CellOutput.upsert_all(prev_cell_outputs_attributes, - unique_by: %i[tx_hash cell_index]) + unique_by: %i[tx_hash cell_index status], + record_timestamps: true) end ScriptTransaction.create_from_scripts TypeScript.where(id: type_script_ids) @@ -1221,11 +1224,9 @@ def cell_input_attributes(input, ckb_transaction_id, local_block_id, previous_output: { id: previous_output.id, cell_type: previous_output.cell_type, - created_at: previous_output.created_at, tx_hash: input.previous_output.tx_hash, cell_index: input.previous_output.index, status: "dead", - updated_at: Time.current, consumed_by_id: ckb_transaction_id, consumed_block_timestamp: CkbTransaction.find(ckb_transaction_id).block_timestamp, }, diff --git a/app/models/referring_cell.rb b/app/models/referring_cell.rb index 2deaba7d1..9ae25ebf2 100644 --- a/app/models/referring_cell.rb +++ b/app/models/referring_cell.rb @@ -56,6 +56,5 @@ def self.create_initial_data_for_ckb_transaction(ckb_transaction) # # Indexes # -# index_referring_cells_on_cell_output_id (cell_output_id) UNIQUE # index_referring_cells_on_contract_id_and_cell_output_id (contract_id,cell_output_id) UNIQUE # diff --git a/db/migrate/20240709131020_rename_cell_outputs_to_cell_outputs_old.rb b/db/migrate/20240709131020_rename_cell_outputs_to_cell_outputs_old.rb new file mode 100644 index 000000000..54bcfe74b --- /dev/null +++ b/db/migrate/20240709131020_rename_cell_outputs_to_cell_outputs_old.rb @@ -0,0 +1,9 @@ +class RenameCellOutputsToCellOutputsOld < ActiveRecord::Migration[7.0] + def up + rename_table :cell_outputs, :cell_outputs_old + end + + def down + rename_table :cell_outputs_old, :cell_outputs + end +end diff --git a/db/migrate/20240709131132_create_cell_outputs.rb b/db/migrate/20240709131132_create_cell_outputs.rb new file mode 100644 index 000000000..d19f4e1ad --- /dev/null +++ b/db/migrate/20240709131132_create_cell_outputs.rb @@ -0,0 +1,47 @@ +class CreateCellOutputs < ActiveRecord::Migration[7.0] + def up + execute <<-SQL + CREATE TABLE cell_outputs ( + id bigserial NOT NULL, + capacity numeric(64,2), + ckb_transaction_id bigint, + created_at timestamp without time zone NOT NULL, + updated_at timestamp without time zone NOT NULL, + status smallint DEFAULT 0, + address_id numeric(30,0), + block_id numeric(30,0), + tx_hash bytea, + cell_index integer, + consumed_by_id numeric(30,0), + cell_type integer DEFAULT 0, + data_size integer, + occupied_capacity numeric(30,0), + block_timestamp numeric(30,0), + consumed_block_timestamp numeric(30,0), + type_hash character varying, + udt_amount numeric(40,0), + dao character varying, + lock_script_id bigint, + type_script_id bigint, + data_hash bytea, + primary key (id, status) + ) PARTITION BY LIST (status); + + CREATE TABLE cell_outputs_live PARTITION OF cell_outputs + FOR VALUES IN (0); + + CREATE TABLE cell_outputs_dead PARTITION OF cell_outputs + FOR VALUES IN (1); + + CREATE TABLE cell_outputs_pending PARTITION OF cell_outputs + FOR VALUES IN (2); + + CREATE TABLE cell_outputs_rejected PARTITION OF cell_outputs + FOR VALUES IN (3); + SQL + end + + def down + drop_table :cell_outputs + end +end diff --git a/db/migrate/20240709131713_import_cell_outputs_old_to_cell_outputs.rb b/db/migrate/20240709131713_import_cell_outputs_old_to_cell_outputs.rb new file mode 100644 index 000000000..8ce4ce42c --- /dev/null +++ b/db/migrate/20240709131713_import_cell_outputs_old_to_cell_outputs.rb @@ -0,0 +1,18 @@ +class ImportCellOutputsOldToCellOutputs < ActiveRecord::Migration[7.0] + def up + execute <<~SQL + SET statement_timeout = 0; + + INSERT INTO cell_outputs (id, capacity, ckb_transaction_id, status, address_id, block_id, tx_hash, cell_index, consumed_by_id, cell_type, data_size, occupied_capacity, block_timestamp, consumed_block_timestamp, type_hash, udt_amount, dao, lock_script_id, type_script_id, data_hash, created_at, updated_at) + SELECT id, capacity, ckb_transaction_id, status, address_id, block_id, tx_hash, cell_index, consumed_by_id, cell_type, data_size, occupied_capacity, block_timestamp, consumed_block_timestamp, type_hash, udt_amount, dao, lock_script_id, type_script_id, data_hash, created_at, updated_at FROM cell_outputs_old; + + SELECT setval('cell_outputs_id_seq', (SELECT max(id) FROM cell_outputs)); + SQL + end + + def down + execute <<~SQL + TRUNCATE TABLE cell_outputs; + SQL + end +end diff --git a/db/migrate/20240709142013_add_index_to_cell_outputs.rb b/db/migrate/20240709142013_add_index_to_cell_outputs.rb new file mode 100644 index 000000000..5326630eb --- /dev/null +++ b/db/migrate/20240709142013_add_index_to_cell_outputs.rb @@ -0,0 +1,13 @@ +class AddIndexToCellOutputs < ActiveRecord::Migration[7.0] + def change + add_index :cell_outputs, :address_id + add_index :cell_outputs, :block_id + add_index :cell_outputs, :consumed_by_id + add_index :cell_outputs, :lock_script_id + add_index :cell_outputs, :type_script_id + add_index :cell_outputs, %i[ckb_transaction_id cell_index status], unique: true, name: "index_cell_outputs_on_tx_id_and_cell_index_and_status" + add_index :cell_outputs, %i[tx_hash cell_index status], unique: true, name: "index_cell_outputs_on_tx_hash_and_cell_index_and_status" + add_index :cell_outputs, :block_timestamp + add_index :cell_outputs, :consumed_block_timestamp + end +end diff --git a/db/structure.sql b/db/structure.sql index a2d1b67c7..5488d9c07 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -9,6 +9,13 @@ SET xmloption = content; SET client_min_messages = warning; SET row_security = off; +-- +-- Name: public; Type: SCHEMA; Schema: -; Owner: - +-- + +-- *not* creating schema, since initdb creates it + + -- -- Name: btree_gin; Type: EXTENSION; Schema: -; Owner: - -- @@ -23,6 +30,20 @@ CREATE EXTENSION IF NOT EXISTS btree_gin WITH SCHEMA public; COMMENT ON EXTENSION btree_gin IS 'support for indexing common datatypes in GIN'; +-- +-- Name: pg_buffercache; Type: EXTENSION; Schema: -; Owner: - +-- + +CREATE EXTENSION IF NOT EXISTS pg_buffercache WITH SCHEMA public; + + +-- +-- Name: EXTENSION pg_buffercache; Type: COMMENT; Schema: -; Owner: - +-- + +COMMENT ON EXTENSION pg_buffercache IS 'examine the shared buffer cache'; + + -- -- Name: array_subtract(anyarray, anyarray); Type: FUNCTION; Schema: public; Owner: - -- @@ -174,7 +195,7 @@ begin insert into account_books (ckb_transaction_id, address_id) values (row.id, i) ON CONFLICT DO NOTHING; end loop; - END LOOP; + END LOOP; close c; end $$; @@ -196,21 +217,21 @@ DECLARE if new.contained_address_ids is null then new.contained_address_ids := array[]::int[]; end if; - if old is null + if old is null then to_add := new.contained_address_ids; to_remove := array[]::int[]; else - + to_add := array_subtract(new.contained_address_ids, old.contained_address_ids); - to_remove := array_subtract(old.contained_address_ids, new.contained_address_ids); + to_remove := array_subtract(old.contained_address_ids, new.contained_address_ids); end if; if to_add is not null then FOREACH i IN ARRAY to_add - LOOP + LOOP RAISE NOTICE 'ckb_tx_addr_id(%)', i; - insert into account_books (ckb_transaction_id, address_id) + insert into account_books (ckb_transaction_id, address_id) values (new.id, i); END LOOP; end if; @@ -415,9 +436,9 @@ CREATE TABLE public.addresses ( dao_deposit numeric(30,0) DEFAULT 0.0, interest numeric(30,0) DEFAULT 0.0, block_timestamp bigint, + visible boolean DEFAULT true, live_cells_count bigint DEFAULT 0.0, mined_blocks_count integer DEFAULT 0, - visible boolean DEFAULT true, average_deposit_time bigint, unclaimed_compensation numeric(30,0), is_depositor boolean DEFAULT false, @@ -470,7 +491,6 @@ CREATE TABLE public.blocks ( "timestamp" bigint, transactions_root bytea, proposals_hash bytea, - uncles_count integer, extra_hash bytea, uncle_block_hashes bytea, version integer, @@ -497,6 +517,7 @@ CREATE TABLE public.blocks ( nonce numeric(50,0) DEFAULT 0.0, start_number numeric(30,0) DEFAULT 0.0, length numeric(30,0) DEFAULT 0.0, + uncles_count integer, compact_target numeric(20,0), live_cell_changes integer, block_time bigint, @@ -506,8 +527,8 @@ CREATE TABLE public.blocks ( miner_message character varying, extension jsonb, median_timestamp bigint DEFAULT 0.0, - ckb_node_version character varying, - cycles bigint + cycles bigint, + ckb_node_version character varying ); @@ -1095,11 +1116,10 @@ ALTER SEQUENCE public.cell_inputs_id_seq OWNED BY public.cell_inputs.id; CREATE TABLE public.cell_outputs ( id bigint NOT NULL, capacity numeric(64,2), - data bytea, ckb_transaction_id bigint, created_at timestamp without time zone NOT NULL, updated_at timestamp without time zone NOT NULL, - status smallint DEFAULT 0, + status smallint DEFAULT 0 NOT NULL, address_id numeric(30,0), block_id numeric(30,0), tx_hash bytea, @@ -1116,7 +1136,8 @@ CREATE TABLE public.cell_outputs ( lock_script_id bigint, type_script_id bigint, data_hash bytea -); +) +PARTITION BY LIST (status); -- @@ -1138,6 +1159,176 @@ CREATE SEQUENCE public.cell_outputs_id_seq ALTER SEQUENCE public.cell_outputs_id_seq OWNED BY public.cell_outputs.id; +-- +-- Name: cell_outputs_dead; Type: TABLE; Schema: public; Owner: - +-- + +CREATE TABLE public.cell_outputs_dead ( + id bigint DEFAULT nextval('public.cell_outputs_id_seq'::regclass) NOT NULL, + capacity numeric(64,2), + ckb_transaction_id bigint, + created_at timestamp without time zone NOT NULL, + updated_at timestamp without time zone NOT NULL, + status smallint DEFAULT 0 NOT NULL, + address_id numeric(30,0), + block_id numeric(30,0), + tx_hash bytea, + cell_index integer, + consumed_by_id numeric(30,0), + cell_type integer DEFAULT 0, + data_size integer, + occupied_capacity numeric(30,0), + block_timestamp numeric(30,0), + consumed_block_timestamp numeric(30,0), + type_hash character varying, + udt_amount numeric(40,0), + dao character varying, + lock_script_id bigint, + type_script_id bigint, + data_hash bytea +); + + +-- +-- Name: cell_outputs_live; Type: TABLE; Schema: public; Owner: - +-- + +CREATE TABLE public.cell_outputs_live ( + id bigint DEFAULT nextval('public.cell_outputs_id_seq'::regclass) NOT NULL, + capacity numeric(64,2), + ckb_transaction_id bigint, + created_at timestamp without time zone NOT NULL, + updated_at timestamp without time zone NOT NULL, + status smallint DEFAULT 0 NOT NULL, + address_id numeric(30,0), + block_id numeric(30,0), + tx_hash bytea, + cell_index integer, + consumed_by_id numeric(30,0), + cell_type integer DEFAULT 0, + data_size integer, + occupied_capacity numeric(30,0), + block_timestamp numeric(30,0), + consumed_block_timestamp numeric(30,0), + type_hash character varying, + udt_amount numeric(40,0), + dao character varying, + lock_script_id bigint, + type_script_id bigint, + data_hash bytea +); + + +-- +-- Name: cell_outputs_old; Type: TABLE; Schema: public; Owner: - +-- + +CREATE TABLE public.cell_outputs_old ( + id bigint NOT NULL, + capacity numeric(64,2), + data bytea, + ckb_transaction_id bigint, + created_at timestamp without time zone NOT NULL, + updated_at timestamp without time zone NOT NULL, + status smallint DEFAULT 0, + address_id numeric(30,0), + block_id numeric(30,0), + tx_hash bytea, + cell_index integer, + consumed_by_id numeric(30,0), + cell_type integer DEFAULT 0, + data_size integer, + occupied_capacity numeric(30,0), + block_timestamp numeric(30,0), + consumed_block_timestamp numeric(30,0), + type_hash character varying, + udt_amount numeric(40,0), + dao character varying, + lock_script_id bigint, + type_script_id bigint, + data_hash bytea +); + + +-- +-- Name: cell_outputs_old_id_seq; Type: SEQUENCE; Schema: public; Owner: - +-- + +CREATE SEQUENCE public.cell_outputs_old_id_seq + START WITH 1 + INCREMENT BY 1 + NO MINVALUE + NO MAXVALUE + CACHE 1; + + +-- +-- Name: cell_outputs_old_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: - +-- + +ALTER SEQUENCE public.cell_outputs_old_id_seq OWNED BY public.cell_outputs_old.id; + + +-- +-- Name: cell_outputs_pending; Type: TABLE; Schema: public; Owner: - +-- + +CREATE TABLE public.cell_outputs_pending ( + id bigint DEFAULT nextval('public.cell_outputs_id_seq'::regclass) NOT NULL, + capacity numeric(64,2), + ckb_transaction_id bigint, + created_at timestamp without time zone NOT NULL, + updated_at timestamp without time zone NOT NULL, + status smallint DEFAULT 0 NOT NULL, + address_id numeric(30,0), + block_id numeric(30,0), + tx_hash bytea, + cell_index integer, + consumed_by_id numeric(30,0), + cell_type integer DEFAULT 0, + data_size integer, + occupied_capacity numeric(30,0), + block_timestamp numeric(30,0), + consumed_block_timestamp numeric(30,0), + type_hash character varying, + udt_amount numeric(40,0), + dao character varying, + lock_script_id bigint, + type_script_id bigint, + data_hash bytea +); + + +-- +-- Name: cell_outputs_rejected; Type: TABLE; Schema: public; Owner: - +-- + +CREATE TABLE public.cell_outputs_rejected ( + id bigint DEFAULT nextval('public.cell_outputs_id_seq'::regclass) NOT NULL, + capacity numeric(64,2), + ckb_transaction_id bigint, + created_at timestamp without time zone NOT NULL, + updated_at timestamp without time zone NOT NULL, + status smallint DEFAULT 0 NOT NULL, + address_id numeric(30,0), + block_id numeric(30,0), + tx_hash bytea, + cell_index integer, + consumed_by_id numeric(30,0), + cell_type integer DEFAULT 0, + data_size integer, + occupied_capacity numeric(30,0), + block_timestamp numeric(30,0), + consumed_block_timestamp numeric(30,0), + type_hash character varying, + udt_amount numeric(40,0), + dao character varying, + lock_script_id bigint, + type_script_id bigint, + data_hash bytea +); + + -- -- Name: ckb_transactions; Type: TABLE; Schema: public; Owner: - -- @@ -1335,9 +1526,9 @@ ALTER SEQUENCE public.contracts_id_seq OWNED BY public.contracts.id; CREATE TABLE public.daily_statistics ( id bigint NOT NULL, - transactions_count character varying DEFAULT 0, - addresses_count character varying DEFAULT 0, - total_dao_deposit character varying DEFAULT 0.0, + transactions_count character varying DEFAULT '0'::character varying, + addresses_count character varying DEFAULT '0'::character varying, + total_dao_deposit character varying DEFAULT '0.0'::character varying, block_timestamp numeric(30,0), created_at_unixtimestamp integer, created_at timestamp(6) without time zone NOT NULL, @@ -1356,8 +1547,8 @@ CREATE TABLE public.daily_statistics ( avg_difficulty character varying DEFAULT '0'::character varying, uncle_rate character varying DEFAULT '0'::character varying, total_depositors_count character varying DEFAULT '0'::character varying, - total_tx_fee numeric(30,0), address_balance_distribution jsonb, + total_tx_fee numeric(30,0), occupied_capacity numeric(30,0), daily_dao_deposit numeric(30,0), daily_dao_depositors_count integer, @@ -1557,7 +1748,6 @@ CREATE TABLE public.forked_blocks ( "timestamp" bigint, transactions_root bytea, proposals_hash bytea, - uncles_count integer, extra_hash bytea, uncle_block_hashes bytea, version integer, @@ -1584,6 +1774,7 @@ CREATE TABLE public.forked_blocks ( nonce numeric(50,0) DEFAULT 0.0, start_number numeric(30,0) DEFAULT 0.0, length numeric(30,0) DEFAULT 0.0, + uncles_count integer, compact_target numeric(20,0), live_cell_changes integer, block_time numeric(13,0), @@ -1593,8 +1784,8 @@ CREATE TABLE public.forked_blocks ( miner_message character varying, extension jsonb, median_timestamp numeric DEFAULT 0.0, - ckb_node_version character varying, - cycles bigint + cycles bigint, + ckb_node_version character varying ); @@ -2673,6 +2864,34 @@ CREATE SEQUENCE public.xudt_tags_id_seq ALTER SEQUENCE public.xudt_tags_id_seq OWNED BY public.xudt_tags.id; +-- +-- Name: cell_outputs_dead; Type: TABLE ATTACH; Schema: public; Owner: - +-- + +ALTER TABLE ONLY public.cell_outputs ATTACH PARTITION public.cell_outputs_dead FOR VALUES IN ('1'); + + +-- +-- Name: cell_outputs_live; Type: TABLE ATTACH; Schema: public; Owner: - +-- + +ALTER TABLE ONLY public.cell_outputs ATTACH PARTITION public.cell_outputs_live FOR VALUES IN ('0'); + + +-- +-- Name: cell_outputs_pending; Type: TABLE ATTACH; Schema: public; Owner: - +-- + +ALTER TABLE ONLY public.cell_outputs ATTACH PARTITION public.cell_outputs_pending FOR VALUES IN ('2'); + + +-- +-- Name: cell_outputs_rejected; Type: TABLE ATTACH; Schema: public; Owner: - +-- + +ALTER TABLE ONLY public.cell_outputs ATTACH PARTITION public.cell_outputs_rejected FOR VALUES IN ('3'); + + -- -- Name: ckb_transactions_committed; Type: TABLE ATTACH; Schema: public; Owner: - -- @@ -2841,6 +3060,13 @@ ALTER TABLE ONLY public.cell_inputs ALTER COLUMN id SET DEFAULT nextval('public. ALTER TABLE ONLY public.cell_outputs ALTER COLUMN id SET DEFAULT nextval('public.cell_outputs_id_seq'::regclass); +-- +-- Name: cell_outputs_old id; Type: DEFAULT; Schema: public; Owner: - +-- + +ALTER TABLE ONLY public.cell_outputs_old ALTER COLUMN id SET DEFAULT nextval('public.cell_outputs_old_id_seq'::regclass); + + -- -- Name: ckb_transactions id; Type: DEFAULT; Schema: public; Owner: - -- @@ -3258,7 +3484,47 @@ ALTER TABLE ONLY public.cell_inputs -- ALTER TABLE ONLY public.cell_outputs - ADD CONSTRAINT cell_outputs_pkey PRIMARY KEY (id); + ADD CONSTRAINT cell_outputs_pkey PRIMARY KEY (id, status); + + +-- +-- Name: cell_outputs_dead cell_outputs_dead_pkey; Type: CONSTRAINT; Schema: public; Owner: - +-- + +ALTER TABLE ONLY public.cell_outputs_dead + ADD CONSTRAINT cell_outputs_dead_pkey PRIMARY KEY (id, status); + + +-- +-- Name: cell_outputs_live cell_outputs_live_pkey; Type: CONSTRAINT; Schema: public; Owner: - +-- + +ALTER TABLE ONLY public.cell_outputs_live + ADD CONSTRAINT cell_outputs_live_pkey PRIMARY KEY (id, status); + + +-- +-- Name: cell_outputs_old cell_outputs_old_pkey; Type: CONSTRAINT; Schema: public; Owner: - +-- + +ALTER TABLE ONLY public.cell_outputs_old + ADD CONSTRAINT cell_outputs_old_pkey PRIMARY KEY (id); + + +-- +-- Name: cell_outputs_pending cell_outputs_pending_pkey; Type: CONSTRAINT; Schema: public; Owner: - +-- + +ALTER TABLE ONLY public.cell_outputs_pending + ADD CONSTRAINT cell_outputs_pending_pkey PRIMARY KEY (id, status); + + +-- +-- Name: cell_outputs_rejected cell_outputs_rejected_pkey; Type: CONSTRAINT; Schema: public; Owner: - +-- + +ALTER TABLE ONLY public.cell_outputs_rejected + ADD CONSTRAINT cell_outputs_rejected_pkey PRIMARY KEY (id, status); -- @@ -3689,84 +3955,399 @@ CREATE UNIQUE INDEX cell_deps_tx_cell_idx ON public.cell_dependencies USING btre -- --- Name: idx_ckb_txs_for_blocks; Type: INDEX; Schema: public; Owner: - +-- Name: index_cell_outputs_on_address_id; Type: INDEX; Schema: public; Owner: - -- -CREATE INDEX idx_ckb_txs_for_blocks ON ONLY public.ckb_transactions USING btree (block_id, block_timestamp); +CREATE INDEX index_cell_outputs_on_address_id ON ONLY public.cell_outputs USING btree (address_id); -- --- Name: ckb_transactions_committed_block_id_block_timestamp_idx; Type: INDEX; Schema: public; Owner: - +-- Name: cell_outputs_dead_address_id_idx; Type: INDEX; Schema: public; Owner: - -- -CREATE INDEX ckb_transactions_committed_block_id_block_timestamp_idx ON public.ckb_transactions_committed USING btree (block_id, block_timestamp); +CREATE INDEX cell_outputs_dead_address_id_idx ON public.cell_outputs_dead USING btree (address_id); -- --- Name: idx_ckb_txs_timestamp; Type: INDEX; Schema: public; Owner: - +-- Name: index_cell_outputs_on_block_id; Type: INDEX; Schema: public; Owner: - -- -CREATE INDEX idx_ckb_txs_timestamp ON ONLY public.ckb_transactions USING btree (block_timestamp DESC NULLS LAST, id); +CREATE INDEX index_cell_outputs_on_block_id ON ONLY public.cell_outputs USING btree (block_id); -- --- Name: ckb_transactions_committed_block_timestamp_id_idx; Type: INDEX; Schema: public; Owner: - +-- Name: cell_outputs_dead_block_id_idx; Type: INDEX; Schema: public; Owner: - -- -CREATE INDEX ckb_transactions_committed_block_timestamp_id_idx ON public.ckb_transactions_committed USING btree (block_timestamp DESC NULLS LAST, id); +CREATE INDEX cell_outputs_dead_block_id_idx ON public.cell_outputs_dead USING btree (block_id); -- --- Name: index_ckb_transactions_on_tags; Type: INDEX; Schema: public; Owner: - +-- Name: index_cell_outputs_on_block_timestamp; Type: INDEX; Schema: public; Owner: - -- -CREATE INDEX index_ckb_transactions_on_tags ON ONLY public.ckb_transactions USING gin (tags); +CREATE INDEX index_cell_outputs_on_block_timestamp ON ONLY public.cell_outputs USING btree (block_timestamp); -- --- Name: ckb_transactions_committed_tags_idx; Type: INDEX; Schema: public; Owner: - +-- Name: cell_outputs_dead_block_timestamp_idx; Type: INDEX; Schema: public; Owner: - -- -CREATE INDEX ckb_transactions_committed_tags_idx ON public.ckb_transactions_committed USING gin (tags); +CREATE INDEX cell_outputs_dead_block_timestamp_idx ON public.cell_outputs_dead USING btree (block_timestamp); -- --- Name: index_ckb_transactions_on_tx_hash; Type: INDEX; Schema: public; Owner: - +-- Name: index_cell_outputs_on_tx_id_and_cell_index_and_status; Type: INDEX; Schema: public; Owner: - -- -CREATE INDEX index_ckb_transactions_on_tx_hash ON ONLY public.ckb_transactions USING hash (tx_hash); +CREATE UNIQUE INDEX index_cell_outputs_on_tx_id_and_cell_index_and_status ON ONLY public.cell_outputs USING btree (ckb_transaction_id, cell_index, status); -- --- Name: ckb_transactions_committed_tx_hash_idx; Type: INDEX; Schema: public; Owner: - +-- Name: cell_outputs_dead_ckb_transaction_id_cell_index_status_idx; Type: INDEX; Schema: public; Owner: - -- -CREATE INDEX ckb_transactions_committed_tx_hash_idx ON public.ckb_transactions_committed USING hash (tx_hash); +CREATE UNIQUE INDEX cell_outputs_dead_ckb_transaction_id_cell_index_status_idx ON public.cell_outputs_dead USING btree (ckb_transaction_id, cell_index, status); -- --- Name: ckb_transactions_pending_block_id_block_timestamp_idx; Type: INDEX; Schema: public; Owner: - +-- Name: index_cell_outputs_on_consumed_block_timestamp; Type: INDEX; Schema: public; Owner: - -- -CREATE INDEX ckb_transactions_pending_block_id_block_timestamp_idx ON public.ckb_transactions_pending USING btree (block_id, block_timestamp); +CREATE INDEX index_cell_outputs_on_consumed_block_timestamp ON ONLY public.cell_outputs USING btree (consumed_block_timestamp); -- --- Name: ckb_transactions_pending_block_timestamp_id_idx; Type: INDEX; Schema: public; Owner: - +-- Name: cell_outputs_dead_consumed_block_timestamp_idx; Type: INDEX; Schema: public; Owner: - -- -CREATE INDEX ckb_transactions_pending_block_timestamp_id_idx ON public.ckb_transactions_pending USING btree (block_timestamp DESC NULLS LAST, id); +CREATE INDEX cell_outputs_dead_consumed_block_timestamp_idx ON public.cell_outputs_dead USING btree (consumed_block_timestamp); -- --- Name: ckb_transactions_pending_tags_idx; Type: INDEX; Schema: public; Owner: - +-- Name: index_cell_outputs_on_consumed_by_id; Type: INDEX; Schema: public; Owner: - -- -CREATE INDEX ckb_transactions_pending_tags_idx ON public.ckb_transactions_pending USING gin (tags); +CREATE INDEX index_cell_outputs_on_consumed_by_id ON ONLY public.cell_outputs USING btree (consumed_by_id); -- --- Name: ckb_transactions_pending_tx_hash_idx; Type: INDEX; Schema: public; Owner: - +-- Name: cell_outputs_dead_consumed_by_id_idx; Type: INDEX; Schema: public; Owner: - +-- + +CREATE INDEX cell_outputs_dead_consumed_by_id_idx ON public.cell_outputs_dead USING btree (consumed_by_id); + + +-- +-- Name: index_cell_outputs_on_lock_script_id; Type: INDEX; Schema: public; Owner: - +-- + +CREATE INDEX index_cell_outputs_on_lock_script_id ON ONLY public.cell_outputs USING btree (lock_script_id); + + +-- +-- Name: cell_outputs_dead_lock_script_id_idx; Type: INDEX; Schema: public; Owner: - +-- + +CREATE INDEX cell_outputs_dead_lock_script_id_idx ON public.cell_outputs_dead USING btree (lock_script_id); + + +-- +-- Name: index_cell_outputs_on_tx_hash_and_cell_index_and_status; Type: INDEX; Schema: public; Owner: - +-- + +CREATE UNIQUE INDEX index_cell_outputs_on_tx_hash_and_cell_index_and_status ON ONLY public.cell_outputs USING btree (tx_hash, cell_index, status); + + +-- +-- Name: cell_outputs_dead_tx_hash_cell_index_status_idx; Type: INDEX; Schema: public; Owner: - +-- + +CREATE UNIQUE INDEX cell_outputs_dead_tx_hash_cell_index_status_idx ON public.cell_outputs_dead USING btree (tx_hash, cell_index, status); + + +-- +-- Name: index_cell_outputs_on_type_script_id; Type: INDEX; Schema: public; Owner: - +-- + +CREATE INDEX index_cell_outputs_on_type_script_id ON ONLY public.cell_outputs USING btree (type_script_id); + + +-- +-- Name: cell_outputs_dead_type_script_id_idx; Type: INDEX; Schema: public; Owner: - +-- + +CREATE INDEX cell_outputs_dead_type_script_id_idx ON public.cell_outputs_dead USING btree (type_script_id); + + +-- +-- Name: cell_outputs_live_address_id_idx; Type: INDEX; Schema: public; Owner: - +-- + +CREATE INDEX cell_outputs_live_address_id_idx ON public.cell_outputs_live USING btree (address_id); + + +-- +-- Name: cell_outputs_live_block_id_idx; Type: INDEX; Schema: public; Owner: - +-- + +CREATE INDEX cell_outputs_live_block_id_idx ON public.cell_outputs_live USING btree (block_id); + + +-- +-- Name: cell_outputs_live_block_timestamp_idx; Type: INDEX; Schema: public; Owner: - +-- + +CREATE INDEX cell_outputs_live_block_timestamp_idx ON public.cell_outputs_live USING btree (block_timestamp); + + +-- +-- Name: cell_outputs_live_ckb_transaction_id_cell_index_status_idx; Type: INDEX; Schema: public; Owner: - +-- + +CREATE UNIQUE INDEX cell_outputs_live_ckb_transaction_id_cell_index_status_idx ON public.cell_outputs_live USING btree (ckb_transaction_id, cell_index, status); + + +-- +-- Name: cell_outputs_live_consumed_block_timestamp_idx; Type: INDEX; Schema: public; Owner: - +-- + +CREATE INDEX cell_outputs_live_consumed_block_timestamp_idx ON public.cell_outputs_live USING btree (consumed_block_timestamp); + + +-- +-- Name: cell_outputs_live_consumed_by_id_idx; Type: INDEX; Schema: public; Owner: - +-- + +CREATE INDEX cell_outputs_live_consumed_by_id_idx ON public.cell_outputs_live USING btree (consumed_by_id); + + +-- +-- Name: cell_outputs_live_lock_script_id_idx; Type: INDEX; Schema: public; Owner: - +-- + +CREATE INDEX cell_outputs_live_lock_script_id_idx ON public.cell_outputs_live USING btree (lock_script_id); + + +-- +-- Name: cell_outputs_live_tx_hash_cell_index_status_idx; Type: INDEX; Schema: public; Owner: - +-- + +CREATE UNIQUE INDEX cell_outputs_live_tx_hash_cell_index_status_idx ON public.cell_outputs_live USING btree (tx_hash, cell_index, status); + + +-- +-- Name: cell_outputs_live_type_script_id_idx; Type: INDEX; Schema: public; Owner: - +-- + +CREATE INDEX cell_outputs_live_type_script_id_idx ON public.cell_outputs_live USING btree (type_script_id); + + +-- +-- Name: cell_outputs_pending_address_id_idx; Type: INDEX; Schema: public; Owner: - +-- + +CREATE INDEX cell_outputs_pending_address_id_idx ON public.cell_outputs_pending USING btree (address_id); + + +-- +-- Name: cell_outputs_pending_block_id_idx; Type: INDEX; Schema: public; Owner: - +-- + +CREATE INDEX cell_outputs_pending_block_id_idx ON public.cell_outputs_pending USING btree (block_id); + + +-- +-- Name: cell_outputs_pending_block_timestamp_idx; Type: INDEX; Schema: public; Owner: - +-- + +CREATE INDEX cell_outputs_pending_block_timestamp_idx ON public.cell_outputs_pending USING btree (block_timestamp); + + +-- +-- Name: cell_outputs_pending_ckb_transaction_id_cell_index_status_idx; Type: INDEX; Schema: public; Owner: - +-- + +CREATE UNIQUE INDEX cell_outputs_pending_ckb_transaction_id_cell_index_status_idx ON public.cell_outputs_pending USING btree (ckb_transaction_id, cell_index, status); + + +-- +-- Name: cell_outputs_pending_consumed_block_timestamp_idx; Type: INDEX; Schema: public; Owner: - +-- + +CREATE INDEX cell_outputs_pending_consumed_block_timestamp_idx ON public.cell_outputs_pending USING btree (consumed_block_timestamp); + + +-- +-- Name: cell_outputs_pending_consumed_by_id_idx; Type: INDEX; Schema: public; Owner: - +-- + +CREATE INDEX cell_outputs_pending_consumed_by_id_idx ON public.cell_outputs_pending USING btree (consumed_by_id); + + +-- +-- Name: cell_outputs_pending_lock_script_id_idx; Type: INDEX; Schema: public; Owner: - +-- + +CREATE INDEX cell_outputs_pending_lock_script_id_idx ON public.cell_outputs_pending USING btree (lock_script_id); + + +-- +-- Name: cell_outputs_pending_tx_hash_cell_index_status_idx; Type: INDEX; Schema: public; Owner: - +-- + +CREATE UNIQUE INDEX cell_outputs_pending_tx_hash_cell_index_status_idx ON public.cell_outputs_pending USING btree (tx_hash, cell_index, status); + + +-- +-- Name: cell_outputs_pending_type_script_id_idx; Type: INDEX; Schema: public; Owner: - +-- + +CREATE INDEX cell_outputs_pending_type_script_id_idx ON public.cell_outputs_pending USING btree (type_script_id); + + +-- +-- Name: cell_outputs_rejected_address_id_idx; Type: INDEX; Schema: public; Owner: - +-- + +CREATE INDEX cell_outputs_rejected_address_id_idx ON public.cell_outputs_rejected USING btree (address_id); + + +-- +-- Name: cell_outputs_rejected_block_id_idx; Type: INDEX; Schema: public; Owner: - +-- + +CREATE INDEX cell_outputs_rejected_block_id_idx ON public.cell_outputs_rejected USING btree (block_id); + + +-- +-- Name: cell_outputs_rejected_block_timestamp_idx; Type: INDEX; Schema: public; Owner: - +-- + +CREATE INDEX cell_outputs_rejected_block_timestamp_idx ON public.cell_outputs_rejected USING btree (block_timestamp); + + +-- +-- Name: cell_outputs_rejected_ckb_transaction_id_cell_index_status_idx; Type: INDEX; Schema: public; Owner: - +-- + +CREATE UNIQUE INDEX cell_outputs_rejected_ckb_transaction_id_cell_index_status_idx ON public.cell_outputs_rejected USING btree (ckb_transaction_id, cell_index, status); + + +-- +-- Name: cell_outputs_rejected_consumed_block_timestamp_idx; Type: INDEX; Schema: public; Owner: - +-- + +CREATE INDEX cell_outputs_rejected_consumed_block_timestamp_idx ON public.cell_outputs_rejected USING btree (consumed_block_timestamp); + + +-- +-- Name: cell_outputs_rejected_consumed_by_id_idx; Type: INDEX; Schema: public; Owner: - +-- + +CREATE INDEX cell_outputs_rejected_consumed_by_id_idx ON public.cell_outputs_rejected USING btree (consumed_by_id); + + +-- +-- Name: cell_outputs_rejected_lock_script_id_idx; Type: INDEX; Schema: public; Owner: - +-- + +CREATE INDEX cell_outputs_rejected_lock_script_id_idx ON public.cell_outputs_rejected USING btree (lock_script_id); + + +-- +-- Name: cell_outputs_rejected_tx_hash_cell_index_status_idx; Type: INDEX; Schema: public; Owner: - +-- + +CREATE UNIQUE INDEX cell_outputs_rejected_tx_hash_cell_index_status_idx ON public.cell_outputs_rejected USING btree (tx_hash, cell_index, status); + + +-- +-- Name: cell_outputs_rejected_type_script_id_idx; Type: INDEX; Schema: public; Owner: - +-- + +CREATE INDEX cell_outputs_rejected_type_script_id_idx ON public.cell_outputs_rejected USING btree (type_script_id); + + +-- +-- Name: idx_ckb_txs_for_blocks; Type: INDEX; Schema: public; Owner: - +-- + +CREATE INDEX idx_ckb_txs_for_blocks ON ONLY public.ckb_transactions USING btree (block_id, block_timestamp); + + +-- +-- Name: ckb_transactions_committed_block_id_block_timestamp_idx; Type: INDEX; Schema: public; Owner: - +-- + +CREATE INDEX ckb_transactions_committed_block_id_block_timestamp_idx ON public.ckb_transactions_committed USING btree (block_id, block_timestamp); + + +-- +-- Name: idx_ckb_txs_timestamp; Type: INDEX; Schema: public; Owner: - +-- + +CREATE INDEX idx_ckb_txs_timestamp ON ONLY public.ckb_transactions USING btree (block_timestamp DESC NULLS LAST, id); + + +-- +-- Name: ckb_transactions_committed_block_timestamp_id_idx; Type: INDEX; Schema: public; Owner: - +-- + +CREATE INDEX ckb_transactions_committed_block_timestamp_id_idx ON public.ckb_transactions_committed USING btree (block_timestamp DESC NULLS LAST, id); + + +-- +-- Name: index_ckb_transactions_on_tags; Type: INDEX; Schema: public; Owner: - +-- + +CREATE INDEX index_ckb_transactions_on_tags ON ONLY public.ckb_transactions USING gin (tags); + + +-- +-- Name: ckb_transactions_committed_tags_idx; Type: INDEX; Schema: public; Owner: - +-- + +CREATE INDEX ckb_transactions_committed_tags_idx ON public.ckb_transactions_committed USING gin (tags); + + +-- +-- Name: index_ckb_transactions_on_tx_hash; Type: INDEX; Schema: public; Owner: - +-- + +CREATE INDEX index_ckb_transactions_on_tx_hash ON ONLY public.ckb_transactions USING hash (tx_hash); + + +-- +-- Name: ckb_transactions_committed_tx_hash_idx; Type: INDEX; Schema: public; Owner: - +-- + +CREATE INDEX ckb_transactions_committed_tx_hash_idx ON public.ckb_transactions_committed USING hash (tx_hash); + + +-- +-- Name: ckb_transactions_pending_block_id_block_timestamp_idx; Type: INDEX; Schema: public; Owner: - +-- + +CREATE INDEX ckb_transactions_pending_block_id_block_timestamp_idx ON public.ckb_transactions_pending USING btree (block_id, block_timestamp); + + +-- +-- Name: ckb_transactions_pending_block_timestamp_id_idx; Type: INDEX; Schema: public; Owner: - +-- + +CREATE INDEX ckb_transactions_pending_block_timestamp_id_idx ON public.ckb_transactions_pending USING btree (block_timestamp DESC NULLS LAST, id); + + +-- +-- Name: ckb_transactions_pending_tags_idx; Type: INDEX; Schema: public; Owner: - +-- + +CREATE INDEX ckb_transactions_pending_tags_idx ON public.ckb_transactions_pending USING gin (tags); + + +-- +-- Name: ckb_transactions_pending_tx_hash_idx; Type: INDEX; Schema: public; Owner: - -- CREATE INDEX ckb_transactions_pending_tx_hash_idx ON public.ckb_transactions_pending USING hash (tx_hash); @@ -4123,94 +4704,94 @@ CREATE UNIQUE INDEX index_cell_inputs_on_ckb_transaction_id_and_index ON public. -- --- Name: index_cell_outputs_on_address_id_and_status; Type: INDEX; Schema: public; Owner: - +-- Name: index_cell_outputs_old_on_address_id_and_status; Type: INDEX; Schema: public; Owner: - -- -CREATE INDEX index_cell_outputs_on_address_id_and_status ON public.cell_outputs USING btree (address_id, status); +CREATE INDEX index_cell_outputs_old_on_address_id_and_status ON public.cell_outputs_old USING btree (address_id, status); -- --- Name: index_cell_outputs_on_block_id; Type: INDEX; Schema: public; Owner: - +-- Name: index_cell_outputs_old_on_block_id; Type: INDEX; Schema: public; Owner: - -- -CREATE INDEX index_cell_outputs_on_block_id ON public.cell_outputs USING btree (block_id); +CREATE INDEX index_cell_outputs_old_on_block_id ON public.cell_outputs_old USING btree (block_id); -- --- Name: index_cell_outputs_on_block_timestamp; Type: INDEX; Schema: public; Owner: - +-- Name: index_cell_outputs_old_on_block_timestamp; Type: INDEX; Schema: public; Owner: - -- -CREATE INDEX index_cell_outputs_on_block_timestamp ON public.cell_outputs USING btree (block_timestamp); +CREATE INDEX index_cell_outputs_old_on_block_timestamp ON public.cell_outputs_old USING btree (block_timestamp); -- --- Name: index_cell_outputs_on_cell_type; Type: INDEX; Schema: public; Owner: - +-- Name: index_cell_outputs_old_on_cell_type; Type: INDEX; Schema: public; Owner: - -- -CREATE INDEX index_cell_outputs_on_cell_type ON public.cell_outputs USING btree (cell_type); +CREATE INDEX index_cell_outputs_old_on_cell_type ON public.cell_outputs_old USING btree (cell_type); -- --- Name: index_cell_outputs_on_ckb_transaction_id_and_cell_index; Type: INDEX; Schema: public; Owner: - +-- Name: index_cell_outputs_old_on_ckb_transaction_id_and_cell_index; Type: INDEX; Schema: public; Owner: - -- -CREATE UNIQUE INDEX index_cell_outputs_on_ckb_transaction_id_and_cell_index ON public.cell_outputs USING btree (ckb_transaction_id, cell_index); +CREATE UNIQUE INDEX index_cell_outputs_old_on_ckb_transaction_id_and_cell_index ON public.cell_outputs_old USING btree (ckb_transaction_id, cell_index); -- --- Name: index_cell_outputs_on_consumed_block_timestamp; Type: INDEX; Schema: public; Owner: - +-- Name: index_cell_outputs_old_on_consumed_block_timestamp; Type: INDEX; Schema: public; Owner: - -- -CREATE INDEX index_cell_outputs_on_consumed_block_timestamp ON public.cell_outputs USING btree (consumed_block_timestamp); +CREATE INDEX index_cell_outputs_old_on_consumed_block_timestamp ON public.cell_outputs_old USING btree (consumed_block_timestamp); -- --- Name: index_cell_outputs_on_consumed_by_id; Type: INDEX; Schema: public; Owner: - +-- Name: index_cell_outputs_old_on_consumed_by_id; Type: INDEX; Schema: public; Owner: - -- -CREATE INDEX index_cell_outputs_on_consumed_by_id ON public.cell_outputs USING btree (consumed_by_id); +CREATE INDEX index_cell_outputs_old_on_consumed_by_id ON public.cell_outputs_old USING btree (consumed_by_id); -- --- Name: index_cell_outputs_on_data_hash; Type: INDEX; Schema: public; Owner: - +-- Name: index_cell_outputs_old_on_data_hash; Type: INDEX; Schema: public; Owner: - -- -CREATE INDEX index_cell_outputs_on_data_hash ON public.cell_outputs USING hash (data_hash); +CREATE INDEX index_cell_outputs_old_on_data_hash ON public.cell_outputs_old USING hash (data_hash); -- --- Name: index_cell_outputs_on_lock_script_id; Type: INDEX; Schema: public; Owner: - +-- Name: index_cell_outputs_old_on_lock_script_id; Type: INDEX; Schema: public; Owner: - -- -CREATE INDEX index_cell_outputs_on_lock_script_id ON public.cell_outputs USING btree (lock_script_id); +CREATE INDEX index_cell_outputs_old_on_lock_script_id ON public.cell_outputs_old USING btree (lock_script_id); -- --- Name: index_cell_outputs_on_status; Type: INDEX; Schema: public; Owner: - +-- Name: index_cell_outputs_old_on_status; Type: INDEX; Schema: public; Owner: - -- -CREATE INDEX index_cell_outputs_on_status ON public.cell_outputs USING btree (status); +CREATE INDEX index_cell_outputs_old_on_status ON public.cell_outputs_old USING btree (status); -- --- Name: index_cell_outputs_on_tx_hash_and_cell_index; Type: INDEX; Schema: public; Owner: - +-- Name: index_cell_outputs_old_on_tx_hash_and_cell_index; Type: INDEX; Schema: public; Owner: - -- -CREATE UNIQUE INDEX index_cell_outputs_on_tx_hash_and_cell_index ON public.cell_outputs USING btree (tx_hash, cell_index); +CREATE UNIQUE INDEX index_cell_outputs_old_on_tx_hash_and_cell_index ON public.cell_outputs_old USING btree (tx_hash, cell_index); -- --- Name: index_cell_outputs_on_type_script_id; Type: INDEX; Schema: public; Owner: - +-- Name: index_cell_outputs_old_on_type_script_id; Type: INDEX; Schema: public; Owner: - -- -CREATE INDEX index_cell_outputs_on_type_script_id ON public.cell_outputs USING btree (type_script_id); +CREATE INDEX index_cell_outputs_old_on_type_script_id ON public.cell_outputs_old USING btree (type_script_id); -- --- Name: index_cell_outputs_on_type_script_id_and_id; Type: INDEX; Schema: public; Owner: - +-- Name: index_cell_outputs_old_on_type_script_id_and_id; Type: INDEX; Schema: public; Owner: - -- -CREATE INDEX index_cell_outputs_on_type_script_id_and_id ON public.cell_outputs USING btree (type_script_id, id); +CREATE INDEX index_cell_outputs_old_on_type_script_id_and_id ON public.cell_outputs_old USING btree (type_script_id, id); -- @@ -4409,13 +4990,6 @@ CREATE UNIQUE INDEX index_omiga_inscription_infos_on_udt_hash ON public.omiga_in CREATE UNIQUE INDEX index_portfolios_on_user_id_and_address_id ON public.portfolios USING btree (user_id, address_id); --- --- Name: index_referring_cells_on_cell_output_id; Type: INDEX; Schema: public; Owner: - --- - -CREATE UNIQUE INDEX index_referring_cells_on_cell_output_id ON public.referring_cells USING btree (cell_output_id); - - -- -- Name: index_referring_cells_on_contract_id_and_cell_output_id; Type: INDEX; Schema: public; Owner: - -- @@ -4724,6 +5298,286 @@ CREATE UNIQUE INDEX pk ON public.udt_transactions USING btree (udt_id, ckb_trans CREATE UNIQUE INDEX tx_address_changes_alt_pk ON public.transaction_address_changes USING btree (address_id, ckb_transaction_id); +-- +-- Name: cell_outputs_dead_address_id_idx; Type: INDEX ATTACH; Schema: public; Owner: - +-- + +ALTER INDEX public.index_cell_outputs_on_address_id ATTACH PARTITION public.cell_outputs_dead_address_id_idx; + + +-- +-- Name: cell_outputs_dead_block_id_idx; Type: INDEX ATTACH; Schema: public; Owner: - +-- + +ALTER INDEX public.index_cell_outputs_on_block_id ATTACH PARTITION public.cell_outputs_dead_block_id_idx; + + +-- +-- Name: cell_outputs_dead_block_timestamp_idx; Type: INDEX ATTACH; Schema: public; Owner: - +-- + +ALTER INDEX public.index_cell_outputs_on_block_timestamp ATTACH PARTITION public.cell_outputs_dead_block_timestamp_idx; + + +-- +-- Name: cell_outputs_dead_ckb_transaction_id_cell_index_status_idx; Type: INDEX ATTACH; Schema: public; Owner: - +-- + +ALTER INDEX public.index_cell_outputs_on_tx_id_and_cell_index_and_status ATTACH PARTITION public.cell_outputs_dead_ckb_transaction_id_cell_index_status_idx; + + +-- +-- Name: cell_outputs_dead_consumed_block_timestamp_idx; Type: INDEX ATTACH; Schema: public; Owner: - +-- + +ALTER INDEX public.index_cell_outputs_on_consumed_block_timestamp ATTACH PARTITION public.cell_outputs_dead_consumed_block_timestamp_idx; + + +-- +-- Name: cell_outputs_dead_consumed_by_id_idx; Type: INDEX ATTACH; Schema: public; Owner: - +-- + +ALTER INDEX public.index_cell_outputs_on_consumed_by_id ATTACH PARTITION public.cell_outputs_dead_consumed_by_id_idx; + + +-- +-- Name: cell_outputs_dead_lock_script_id_idx; Type: INDEX ATTACH; Schema: public; Owner: - +-- + +ALTER INDEX public.index_cell_outputs_on_lock_script_id ATTACH PARTITION public.cell_outputs_dead_lock_script_id_idx; + + +-- +-- Name: cell_outputs_dead_pkey; Type: INDEX ATTACH; Schema: public; Owner: - +-- + +ALTER INDEX public.cell_outputs_pkey ATTACH PARTITION public.cell_outputs_dead_pkey; + + +-- +-- Name: cell_outputs_dead_tx_hash_cell_index_status_idx; Type: INDEX ATTACH; Schema: public; Owner: - +-- + +ALTER INDEX public.index_cell_outputs_on_tx_hash_and_cell_index_and_status ATTACH PARTITION public.cell_outputs_dead_tx_hash_cell_index_status_idx; + + +-- +-- Name: cell_outputs_dead_type_script_id_idx; Type: INDEX ATTACH; Schema: public; Owner: - +-- + +ALTER INDEX public.index_cell_outputs_on_type_script_id ATTACH PARTITION public.cell_outputs_dead_type_script_id_idx; + + +-- +-- Name: cell_outputs_live_address_id_idx; Type: INDEX ATTACH; Schema: public; Owner: - +-- + +ALTER INDEX public.index_cell_outputs_on_address_id ATTACH PARTITION public.cell_outputs_live_address_id_idx; + + +-- +-- Name: cell_outputs_live_block_id_idx; Type: INDEX ATTACH; Schema: public; Owner: - +-- + +ALTER INDEX public.index_cell_outputs_on_block_id ATTACH PARTITION public.cell_outputs_live_block_id_idx; + + +-- +-- Name: cell_outputs_live_block_timestamp_idx; Type: INDEX ATTACH; Schema: public; Owner: - +-- + +ALTER INDEX public.index_cell_outputs_on_block_timestamp ATTACH PARTITION public.cell_outputs_live_block_timestamp_idx; + + +-- +-- Name: cell_outputs_live_ckb_transaction_id_cell_index_status_idx; Type: INDEX ATTACH; Schema: public; Owner: - +-- + +ALTER INDEX public.index_cell_outputs_on_tx_id_and_cell_index_and_status ATTACH PARTITION public.cell_outputs_live_ckb_transaction_id_cell_index_status_idx; + + +-- +-- Name: cell_outputs_live_consumed_block_timestamp_idx; Type: INDEX ATTACH; Schema: public; Owner: - +-- + +ALTER INDEX public.index_cell_outputs_on_consumed_block_timestamp ATTACH PARTITION public.cell_outputs_live_consumed_block_timestamp_idx; + + +-- +-- Name: cell_outputs_live_consumed_by_id_idx; Type: INDEX ATTACH; Schema: public; Owner: - +-- + +ALTER INDEX public.index_cell_outputs_on_consumed_by_id ATTACH PARTITION public.cell_outputs_live_consumed_by_id_idx; + + +-- +-- Name: cell_outputs_live_lock_script_id_idx; Type: INDEX ATTACH; Schema: public; Owner: - +-- + +ALTER INDEX public.index_cell_outputs_on_lock_script_id ATTACH PARTITION public.cell_outputs_live_lock_script_id_idx; + + +-- +-- Name: cell_outputs_live_pkey; Type: INDEX ATTACH; Schema: public; Owner: - +-- + +ALTER INDEX public.cell_outputs_pkey ATTACH PARTITION public.cell_outputs_live_pkey; + + +-- +-- Name: cell_outputs_live_tx_hash_cell_index_status_idx; Type: INDEX ATTACH; Schema: public; Owner: - +-- + +ALTER INDEX public.index_cell_outputs_on_tx_hash_and_cell_index_and_status ATTACH PARTITION public.cell_outputs_live_tx_hash_cell_index_status_idx; + + +-- +-- Name: cell_outputs_live_type_script_id_idx; Type: INDEX ATTACH; Schema: public; Owner: - +-- + +ALTER INDEX public.index_cell_outputs_on_type_script_id ATTACH PARTITION public.cell_outputs_live_type_script_id_idx; + + +-- +-- Name: cell_outputs_pending_address_id_idx; Type: INDEX ATTACH; Schema: public; Owner: - +-- + +ALTER INDEX public.index_cell_outputs_on_address_id ATTACH PARTITION public.cell_outputs_pending_address_id_idx; + + +-- +-- Name: cell_outputs_pending_block_id_idx; Type: INDEX ATTACH; Schema: public; Owner: - +-- + +ALTER INDEX public.index_cell_outputs_on_block_id ATTACH PARTITION public.cell_outputs_pending_block_id_idx; + + +-- +-- Name: cell_outputs_pending_block_timestamp_idx; Type: INDEX ATTACH; Schema: public; Owner: - +-- + +ALTER INDEX public.index_cell_outputs_on_block_timestamp ATTACH PARTITION public.cell_outputs_pending_block_timestamp_idx; + + +-- +-- Name: cell_outputs_pending_ckb_transaction_id_cell_index_status_idx; Type: INDEX ATTACH; Schema: public; Owner: - +-- + +ALTER INDEX public.index_cell_outputs_on_tx_id_and_cell_index_and_status ATTACH PARTITION public.cell_outputs_pending_ckb_transaction_id_cell_index_status_idx; + + +-- +-- Name: cell_outputs_pending_consumed_block_timestamp_idx; Type: INDEX ATTACH; Schema: public; Owner: - +-- + +ALTER INDEX public.index_cell_outputs_on_consumed_block_timestamp ATTACH PARTITION public.cell_outputs_pending_consumed_block_timestamp_idx; + + +-- +-- Name: cell_outputs_pending_consumed_by_id_idx; Type: INDEX ATTACH; Schema: public; Owner: - +-- + +ALTER INDEX public.index_cell_outputs_on_consumed_by_id ATTACH PARTITION public.cell_outputs_pending_consumed_by_id_idx; + + +-- +-- Name: cell_outputs_pending_lock_script_id_idx; Type: INDEX ATTACH; Schema: public; Owner: - +-- + +ALTER INDEX public.index_cell_outputs_on_lock_script_id ATTACH PARTITION public.cell_outputs_pending_lock_script_id_idx; + + +-- +-- Name: cell_outputs_pending_pkey; Type: INDEX ATTACH; Schema: public; Owner: - +-- + +ALTER INDEX public.cell_outputs_pkey ATTACH PARTITION public.cell_outputs_pending_pkey; + + +-- +-- Name: cell_outputs_pending_tx_hash_cell_index_status_idx; Type: INDEX ATTACH; Schema: public; Owner: - +-- + +ALTER INDEX public.index_cell_outputs_on_tx_hash_and_cell_index_and_status ATTACH PARTITION public.cell_outputs_pending_tx_hash_cell_index_status_idx; + + +-- +-- Name: cell_outputs_pending_type_script_id_idx; Type: INDEX ATTACH; Schema: public; Owner: - +-- + +ALTER INDEX public.index_cell_outputs_on_type_script_id ATTACH PARTITION public.cell_outputs_pending_type_script_id_idx; + + +-- +-- Name: cell_outputs_rejected_address_id_idx; Type: INDEX ATTACH; Schema: public; Owner: - +-- + +ALTER INDEX public.index_cell_outputs_on_address_id ATTACH PARTITION public.cell_outputs_rejected_address_id_idx; + + +-- +-- Name: cell_outputs_rejected_block_id_idx; Type: INDEX ATTACH; Schema: public; Owner: - +-- + +ALTER INDEX public.index_cell_outputs_on_block_id ATTACH PARTITION public.cell_outputs_rejected_block_id_idx; + + +-- +-- Name: cell_outputs_rejected_block_timestamp_idx; Type: INDEX ATTACH; Schema: public; Owner: - +-- + +ALTER INDEX public.index_cell_outputs_on_block_timestamp ATTACH PARTITION public.cell_outputs_rejected_block_timestamp_idx; + + +-- +-- Name: cell_outputs_rejected_ckb_transaction_id_cell_index_status_idx; Type: INDEX ATTACH; Schema: public; Owner: - +-- + +ALTER INDEX public.index_cell_outputs_on_tx_id_and_cell_index_and_status ATTACH PARTITION public.cell_outputs_rejected_ckb_transaction_id_cell_index_status_idx; + + +-- +-- Name: cell_outputs_rejected_consumed_block_timestamp_idx; Type: INDEX ATTACH; Schema: public; Owner: - +-- + +ALTER INDEX public.index_cell_outputs_on_consumed_block_timestamp ATTACH PARTITION public.cell_outputs_rejected_consumed_block_timestamp_idx; + + +-- +-- Name: cell_outputs_rejected_consumed_by_id_idx; Type: INDEX ATTACH; Schema: public; Owner: - +-- + +ALTER INDEX public.index_cell_outputs_on_consumed_by_id ATTACH PARTITION public.cell_outputs_rejected_consumed_by_id_idx; + + +-- +-- Name: cell_outputs_rejected_lock_script_id_idx; Type: INDEX ATTACH; Schema: public; Owner: - +-- + +ALTER INDEX public.index_cell_outputs_on_lock_script_id ATTACH PARTITION public.cell_outputs_rejected_lock_script_id_idx; + + +-- +-- Name: cell_outputs_rejected_pkey; Type: INDEX ATTACH; Schema: public; Owner: - +-- + +ALTER INDEX public.cell_outputs_pkey ATTACH PARTITION public.cell_outputs_rejected_pkey; + + +-- +-- Name: cell_outputs_rejected_tx_hash_cell_index_status_idx; Type: INDEX ATTACH; Schema: public; Owner: - +-- + +ALTER INDEX public.index_cell_outputs_on_tx_hash_and_cell_index_and_status ATTACH PARTITION public.cell_outputs_rejected_tx_hash_cell_index_status_idx; + + +-- +-- Name: cell_outputs_rejected_type_script_id_idx; Type: INDEX ATTACH; Schema: public; Owner: - +-- + +ALTER INDEX public.index_cell_outputs_on_type_script_id ATTACH PARTITION public.cell_outputs_rejected_type_script_id_idx; + + -- -- Name: ckb_transactions_committed_block_id_block_timestamp_idx; Type: INDEX ATTACH; Schema: public; Owner: - -- @@ -5238,6 +6092,8 @@ INSERT INTO "schema_migrations" (version) VALUES ('20240509074313'), ('20240513055849'), ('20240620083123'), -('20240625032839'); - - +('20240625032839'), +('20240709131020'), +('20240709131132'), +('20240709131713'), +('20240709142013'); From 2d452cd4c04ae6de29fb5a242f5b04225fb51dc5 Mon Sep 17 00:00:00 2001 From: Miles Zhang Date: Mon, 15 Jul 2024 19:47:31 +0800 Subject: [PATCH 2/4] fix: update cell_outputs from pending to live first (#2064) Signed-off-by: Miles Zhang --- app/models/ckb_sync/new_node_data_processor.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/models/ckb_sync/new_node_data_processor.rb b/app/models/ckb_sync/new_node_data_processor.rb index 953d0c4d7..84c6fb640 100644 --- a/app/models/ckb_sync/new_node_data_processor.rb +++ b/app/models/ckb_sync/new_node_data_processor.rb @@ -846,6 +846,8 @@ def build_cells_and_locks!( build_cell_outputs!(node_block, outputs, ckb_txs, local_block, cell_outputs_attributes, output_capacities, tags, udt_address_ids, dao_address_ids, contained_udt_ids, contained_addr_ids, addrs_changes, token_transfer_ckb_tx_ids) if cell_outputs_attributes.present? + tx_ids = cell_outputs_attributes.map { |attr| attr[:ckb_transaction_id] } + CellOutput.pending.where(ckb_transaction_id: tx_ids).update_all(status: :live) id_hashes = CellOutput.upsert_all(cell_outputs_attributes, unique_by: %i[tx_hash cell_index status], returning: %i[id data_hash]) cell_data_attrs = [] From 129506606de7981ffa5a96481263755876d45ff8 Mon Sep 17 00:00:00 2001 From: Miles Zhang Date: Mon, 15 Jul 2024 21:01:27 +0800 Subject: [PATCH 3/4] chore: config database timeout for production (#2035) Signed-off-by: Miles Zhang --- config/database.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/config/database.yml b/config/database.yml index da6cb7916..2405ef716 100644 --- a/config/database.yml +++ b/config/database.yml @@ -21,14 +21,14 @@ default: &default # http://guides.rubyonrails.org/configuring.html#database-pooling host: <%= ENV.fetch("DB_HOST") { "localhost" } %> port: <%= ENV.fetch("DB_PORT") { "5432" } %> - pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 25 } %> + pool: <%= ENV.fetch("DB_POOL") { 5 } %> username: <%= ENV.fetch("DB_USERNAME") { "postgres" } %> password: <%= ENV.fetch("DB_PASSWORD") { "postgres" } %> reaping_frequency: 60 # every 60s check pool timeout: 5000 # over 5s will timeout variables: - statement_timeout: 120000 # query timemout over 120s - idle_in_transaction_session_timeout: 60000 # idle transaction over 60s + statement_timeout: 60000 # query timemout over 60s + idle_in_transaction_session_timeout: 120000 # idle transaction over 120s development: <<: *default @@ -88,5 +88,5 @@ test: # url: <%= ENV['DATABASE_URL'] %> # production: + <<: *default url: <%= ENV['DATABASE_URL'] %> - pool: <%= ENV["DB_POOL"] || ENV['RAILS_MAX_THREADS'] || 5 %> From 68746b01512f37a3475189e1c13f4873b2a38198 Mon Sep 17 00:00:00 2001 From: Miles Zhang Date: Mon, 15 Jul 2024 23:35:08 +0800 Subject: [PATCH 4/4] fix: get cell_output data from cell_datum (#2065) Signed-off-by: Miles Zhang --- app/workers/generate_statistics_data_worker.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/app/workers/generate_statistics_data_worker.rb b/app/workers/generate_statistics_data_worker.rb index d715fc22d..2442793df 100644 --- a/app/workers/generate_statistics_data_worker.rb +++ b/app/workers/generate_statistics_data_worker.rb @@ -15,25 +15,25 @@ def perform(block_id) epoch_stats.update(largest_block_size: block_size, largest_block_number: block.number) end - cell_outputs = block.cell_outputs.select(:id, :created_at, :data, :capacity, :lock_script_id, :type_script_id).to_a + cell_outputs = block.cell_outputs.includes(:cell_datum) cell_outputs_attributes = [] cell_outputs.each do |cell_output| data_size = - if cell_output.data + if cell_output.data != "0x" CKB::Utils.hex_to_bin(cell_output.data).bytesize else 0 end cell_outputs_attributes << { - id: cell_output.id, + tx_hash: cell_output.tx_hash, + cell_index: cell_output.cell_index, + status: cell_output.status, data_size:, occupied_capacity: CkbUtils.calculate_cell_min_capacity(cell_output.node_output, cell_output.data), - created_at: cell_output.created_at, - updated_at: Time.current, } end - CellOutput.upsert_all(cell_outputs_attributes) if cell_outputs_attributes.present? + CellOutput.upsert_all(cell_outputs_attributes, unique_by: %i[tx_hash cell_index status], record_timestamps: true) if cell_outputs_attributes.present? end end