diff --git a/lbry/blockchain/sync/claims.py b/lbry/blockchain/sync/claims.py index 7b07951ea..982e48e93 100644 --- a/lbry/blockchain/sync/claims.py +++ b/lbry/blockchain/sync/claims.py @@ -165,6 +165,8 @@ def claims_insert( @event_emitter("blockchain.sync.claims.indexes", "steps") def claims_constraints_and_indexes(p: ProgressContext): + if p.ctx.is_postgres and pg_has_claim_primary_key(p): + return p.start(2 + len(pg_add_claim_and_tag_constraints_and_indexes)) if p.ctx.is_postgres: p.ctx.execute_notx(text("VACUUM ANALYZE claim;")) @@ -309,3 +311,11 @@ def update_claim_filters(resolve_censor_channel_hashes, search_censor_channel_ha ['claim_hash', 'censor_type', 'censoring_channel_hash'], select_reposts(search_censor_channel_hashes, Censor.SEARCH) )) + + +def pg_has_claim_primary_key(p: ProgressContext): + claim_constraints = p.ctx.execute( + "select * from information_schema.table_constraints as tc " + "where tc.table_name='claim' and constraint_type='PRIMARY KEY'" + ).fetchall() + return len(claim_constraints) > 0 diff --git a/lbry/blockchain/sync/synchronizer.py b/lbry/blockchain/sync/synchronizer.py index 2d386909b..fbda6b3a7 100644 --- a/lbry/blockchain/sync/synchronizer.py +++ b/lbry/blockchain/sync/synchronizer.py @@ -259,7 +259,7 @@ class BlockchainSync(Sync): async def sync_claims(self, blocks) -> bool: delete_claims = takeovers = claims_with_changed_supports = claims_with_changed_reposts = 0 - initial_sync = not await self.db.has_filters() + initial_sync = not await self.db.has_claims() with Progress(self.db.message_queue, CLAIMS_INIT_EVENT) as p: if initial_sync: total, batches = await self.distribute_unspent_txos(CLAIM_TYPE_CODES)