diff --git a/lbry/blockchain/sync/steps.py b/lbry/blockchain/sync/steps.py index 3bcafbeba..48688f272 100644 --- a/lbry/blockchain/sync/steps.py +++ b/lbry/blockchain/sync/steps.py @@ -150,20 +150,22 @@ def process_inputs_outputs(initial_sync=False, p=None): step = 1 if initial_sync and p.ctx.is_postgres: - p.start(4) + p.start(6) else: p.start(2) - # 1. Update TXIs to have the address of TXO they are spending. - set_input_addresses(p.ctx) - p.step(step) - step += 1 + # 0. Vacuum if initial_sync and p.ctx.is_postgres: - p.ctx.execute(text("ALTER TABLE txi ADD PRIMARY KEY (txo_hash);")) - p.step(step) - step += 1 + with p.ctx.engine.connect() as c: + c.execute(text("COMMIT;")) + c.execute(text("VACUUM FULL ANALYZE txo;")) + p.step(step) + step += 1 + c.execute(text("VACUUM FULL ANALYZE txi;")) + p.step(step) + step += 1 - # 2. Update spent TXOs setting is_spent = True + # 1. Update spent TXOs setting is_spent = True update_spent_outputs(p.ctx) p.step(step) step += 1 @@ -172,6 +174,15 @@ def process_inputs_outputs(initial_sync=False, p=None): p.step(step) step += 1 + # 2. Update TXIs to have the address of TXO they are spending. + set_input_addresses(p.ctx) + p.step(step) + step += 1 + if initial_sync and p.ctx.is_postgres: + p.ctx.execute(text("ALTER TABLE txi ADD PRIMARY KEY (txo_hash);")) + p.step(step) + step += 1 + @sync_step(Event.BLOCK_FILTER, initial_sync=True, ongoing_sync=True) def process_block_filters(p=None): diff --git a/tests/integration/blockchain/test_blockchain.py b/tests/integration/blockchain/test_blockchain.py index 840104c76..7e735a5b0 100644 --- a/tests/integration/blockchain/test_blockchain.py +++ b/tests/integration/blockchain/test_blockchain.py @@ -535,11 +535,13 @@ class TestMultiBlockFileSyncing(BasicBlockchainTestCase): # 3 - db.sync.input self.assertEventsAlmostEqual( self.extract_events('db.sync.input', events), [ - [0, 4], - [1, 4], - [2, 4], - [3, 4], - [4, 4], + [0, 6], + [1, 6], + [2, 6], + [3, 6], + [4, 6], + [5, 6], + [6, 6], ] if self.db_driver == 'postgresql' else [ [0, 2], [1, 2],