-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: partition transaction message (#645) TG-146 #waiting
* feat: partition transaction message * feat: create job create constraint for transaction_message table
- Loading branch information
1 parent
9f3081b
commit 3257cac
Showing
8 changed files
with
841 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
migrations/20240103082700_transaction_message_partition.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import { Knex } from 'knex'; | ||
import config from '../config.json' assert { type: 'json' }; | ||
|
||
export async function up(knex: Knex): Promise<void> { | ||
console.log('Migrating transaction_message to use partition'); | ||
await knex.transaction(async (trx) => { | ||
await knex | ||
.raw( | ||
`set statement_timeout to ${config.migrationTransactionMessageToPartition.statementTimeout}` | ||
) | ||
.transacting(trx); | ||
await knex.raw( | ||
`CREATE TABLE transaction_message_partition ( | ||
id SERIAL PRIMARY KEY, | ||
tx_id INTEGER NOT NULL, | ||
index INTEGER NOT NULL, | ||
type VARCHAR(255) NOT NULL, | ||
sender VARCHAR(255) NOT NULL, | ||
content JSONB, | ||
parent_id INTEGER | ||
) PARTITION BY RANGE (id); | ||
CREATE INDEX transaction_message_partition_parent_id_index ON transaction_message_partition(parent_id); | ||
CREATE INDEX transaction_message_partition_sender_index ON transaction_message_partition(sender); | ||
CREATE INDEX transaction_message_partition_tx_id_index ON transaction_message_partition(tx_id); | ||
CREATE INDEX transaction_message_partition_type_index ON transaction_message_partition(type);` | ||
); | ||
await knex.schema.renameTable( | ||
'transaction_message', | ||
'transaction_message_partition_0_100000000' | ||
); | ||
await knex.schema.renameTable( | ||
'transaction_message_partition', | ||
'transaction_message' | ||
); | ||
const oldSeqTransactionMessage = await knex.raw( | ||
`SELECT last_value FROM transaction_message_id_seq;` | ||
); | ||
const oldSeqValue = oldSeqTransactionMessage.rows[0].last_value; | ||
await knex | ||
.raw( | ||
`ALTER SEQUENCE transaction_message_partition_id_seq RESTART WITH ${oldSeqValue};` | ||
) | ||
.transacting(trx); | ||
|
||
// add old table transaction into transaction partitioned | ||
await knex | ||
.raw( | ||
`ALTER TABLE transaction_message ATTACH PARTITION transaction_message_partition_0_100000000 FOR VALUES FROM (0) TO (100000000)` | ||
) | ||
.transacting(trx); | ||
|
||
let startId = config.migrationTransactionMessageToPartition.startId; | ||
let endId = config.migrationTransactionMessageToPartition.endId; | ||
const step = config.migrationTransactionMessageToPartition.step; | ||
for (let i = startId; i < endId; i += step) { | ||
const partitionName = `transaction_message_partition_${i}_${i + step}`; | ||
await knex | ||
.raw( | ||
`CREATE TABLE ${partitionName} (LIKE transaction_message INCLUDING ALL)` | ||
) | ||
.transacting(trx); | ||
await knex | ||
.raw( | ||
`ALTER TABLE transaction_message ATTACH PARTITION ${partitionName} FOR VALUES FROM (${i}) TO (${ | ||
i + step | ||
})` | ||
) | ||
.transacting(trx); | ||
} | ||
}); | ||
} | ||
|
||
export async function down(knex: Knex): Promise<void> { | ||
await knex.transaction(async (trx) => { | ||
await knex | ||
.raw( | ||
`ALTER TABLE transaction_message DETACH PARTITION transaction_message_partition_0_100000000;` | ||
) | ||
.transacting(trx); | ||
await knex.schema.dropTableIfExists('transaction_message_partition'); | ||
await knex.schema.renameTable( | ||
'transaction_message', | ||
'transaction_message_partition' | ||
); | ||
await knex.schema.renameTable( | ||
'transaction_message_partition_0_100000000', | ||
'transaction_message' | ||
); | ||
const oldSeqTransactionMessage = await knex.raw( | ||
`SELECT last_value FROM transaction_message_partition_id_seq;` | ||
); | ||
const oldSeqValue = oldSeqTransactionMessage.rows[0].last_value; | ||
await knex | ||
.raw( | ||
`ALTER SEQUENCE transaction_message_id_seq RESTART WITH ${oldSeqValue};` | ||
) | ||
.transacting(trx); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.