Skip to content

Commit

Permalink
temp index on addresses.tx_vin_vout_row_id for stake invalidation
Browse files Browse the repository at this point in the history
  • Loading branch information
chappjc committed Apr 25, 2021
1 parent 43e994a commit 25cfdda
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 3 deletions.
2 changes: 1 addition & 1 deletion db/dcrpg/indexing.go
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ func (pgb *ChainDB) IndexAll(barLoad chan *dbtypes.ProgressBarLoad) error {
// IndexAddressTable to create them all.
{Msg: "addresses table on tx hash", IndexFunc: IndexAddressTableOnTxHash},
{Msg: "addresses table on block time", IndexFunc: IndexBlockTimeOnTableAddress},
{Msg: "addresses table on address", IndexFunc: IndexAddressTableOnAddress},
{Msg: "addresses table on address", IndexFunc: IndexAddressTableOnAddress}, // TODO: remove or redefine this or IndexAddressTableOnVoutID since that includes address too
{Msg: "addresses table on vout DB ID", IndexFunc: IndexAddressTableOnVoutID},
//{Msg: "addresses table on matching tx hash", IndexFunc: IndexAddressTableOnMatchingTxHash},

Expand Down
2 changes: 2 additions & 0 deletions db/dcrpg/internal/addrstmts.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ const (
` ON addresses(matching_tx_hash);`
DeindexAddressTableOnMatchingTxHash = `DROP INDEX IF EXISTS ` + IndexOfAddressTableOnMatchingTx + ` CASCADE;`

// TODO: figure out why this index exists since it's covered by the unique
// on tx_vin_vout_row_id too.
IndexAddressTableOnAddress = `CREATE INDEX IF NOT EXISTS ` + IndexOfAddressTableOnAddress +
` ON addresses(address);`
DeindexAddressTableOnAddress = `DROP INDEX IF EXISTS ` + IndexOfAddressTableOnAddress + ` CASCADE;`
Expand Down
2 changes: 1 addition & 1 deletion db/dcrpg/internal/indexes.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ var IndexDescriptions = map[string]string{
IndexOfVinsTableOnPrevOut: "vins on previous outpoint",
IndexOfVoutsTableOnTxHashInd: "vouts on transaction hash and index",
IndexOfVoutsTableOnSpendTxID: "vouts on spend_tx_row_id",
IndexOfAddressTableOnAddress: "addresses table on address",
IndexOfAddressTableOnAddress: "addresses table on address", // TODO: remove if it is redundant with IndexOfAddressTableOnVoutID
IndexOfAddressTableOnVoutID: "addresses table on vout row id, address, and is_funding",
IndexOfAddressTableOnBlockTime: "addresses table on block time",
IndexOfAddressTableOnTx: "addresses table on transaction hash",
Expand Down
4 changes: 4 additions & 0 deletions db/dcrpg/pgblockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -3659,6 +3659,10 @@ func (pgb *ChainDB) UpdateLastBlock(msgBlock *wire.MsgBlock, isMainchain bool) e
}

// Update addresses table for last block's regular transactions.
// So slow without indexes:
// Update on addresses (cost=0.00..1012201.53 rows=1 width=181)
// -> Seq Scan on addresses (cost=0.00..1012201.53 rows=1 width=181)
// Filter: ((NOT is_funding) AND (tx_vin_vout_row_id = 13241234))
err = UpdateLastAddressesValid(pgb.db, lastBlockHash.String(), lastIsValid)
if err != nil {
return fmt.Errorf("UpdateLastAddressesValid: %v", err)
Expand Down
20 changes: 19 additions & 1 deletion db/dcrpg/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,16 @@ func (pgb *ChainDB) SyncChainDB(ctx context.Context, client rpcutils.MasterBlock
return lastBlock, err
}

// Create the temporary index on addresses(tx_vin_vout_row_id) that
// prevents the stake disapproval updates to a block to cause massive
// slowdown during initial sync without an index on tx_vin_vout_row_id.
log.Infof("Creating temporary index on addresses(tx_vin_vout_row_id).")
_, err = pgb.db.Exec(`CREATE INDEX IF NOT EXISTS idx_addresses_vinvout_id_tmp ` +
`ON addresses(tx_vin_vout_row_id)`)
if err != nil {
return lastBlock, err
}

// Disable duplicate checks on insert queries since the unique indexes
// that enforce the constraints will not exist.
pgb.EnableDuplicateCheckOnInsert(false)
Expand Down Expand Up @@ -377,6 +387,13 @@ func (pgb *ChainDB) SyncChainDB(ctx context.Context, client rpcutils.MasterBlock
// Index and analyze tables.
var analyzed bool
if reindexing {
// drop the temporary index on addresses(tx_vin_vout_row_id).
log.Infof("Dropping temporary index on addresses(tx_vin_vout_row_id).")
_, err = pgb.db.Exec(`DROP INDEX IF EXISTS idx_addresses_vinvout_id_tmp;`)
if err != nil {
return nodeHeight, err
}

// To build indexes, there must NOT be duplicate rows in terms of the
// constraints defined by the unique indexes. Duplicate transactions,
// vins, and vouts can end up in the tables when identical transactions
Expand All @@ -388,7 +405,8 @@ func (pgb *ChainDB) SyncChainDB(ctx context.Context, client rpcutils.MasterBlock
return 0, err
}

// Create all indexes except addresses and tickets indexes.
// Create all indexes except those on addresses.matching_tx_hash,
// vouts.spend_tx_row_id, and all tickets indexes.
if err = pgb.IndexAll(barLoad); err != nil {
return nodeHeight, fmt.Errorf("IndexAll failed: %v", err)
}
Expand Down

0 comments on commit 25cfdda

Please sign in to comment.