From 3db903fe4091b70e6d0e1c2344f802b663fcec57 Mon Sep 17 00:00:00 2001 From: Stefan Benten Date: Tue, 26 Mar 2019 11:34:30 +0100 Subject: [PATCH] Move from Unique to Index on SatelliteDB (#1563) * Move from Unique to Index * Remove Index * Make some more Indexes Unique and adjust migration * Fix Migration Statements * Fix Typo * Fix Migration of older Table * Exchange DROP statement * Remove "if not exists" * Revert Change in old Migration --- satellite/satellitedb/dbx/satellitedb.dbx | 35 +++-- satellite/satellitedb/dbx/satellitedb.dbx.go | 130 +++++++++++------- .../dbx/satellitedb.dbx.postgres.sql | 25 ++-- .../dbx/satellitedb.dbx.sqlite3.sql | 25 ++-- satellite/satellitedb/migrate.go | 33 +++-- .../satellitedb/testdata/postgres.v9.sql | 25 ++-- 6 files changed, 159 insertions(+), 114 deletions(-) diff --git a/satellite/satellitedb/dbx/satellitedb.dbx b/satellite/satellitedb/dbx/satellitedb.dbx index dee784563..8827a0dca 100644 --- a/satellite/satellitedb/dbx/satellitedb.dbx +++ b/satellite/satellitedb/dbx/satellitedb.dbx @@ -335,7 +335,11 @@ read all ( model bucket_usage ( key id - unique rollup_end_time bucket_id + index ( + name bucket_id_rollup + fields bucket_id rollup_end_time + unique + ) field id blob field bucket_id blob //--TODO: add foreign key constraint--// @@ -381,7 +385,11 @@ read limitoffset ( model serial_number ( key id - unique serial_number + index ( + name serial_number + fields serial_number + unique + ) field id serial field serial_number blob @@ -395,8 +403,7 @@ model serial_number ( ) model used_serial ( - key serial_number_id - unique serial_number_id storage_node_id + key serial_number_id storage_node_id field serial_number_id serial_number.id cascade field storage_node_id blob @@ -422,8 +429,11 @@ create used_serial () // --- bucket roll up tables --- // model bucket_bandwidth_rollup ( - key bucket_id - unique bucket_id interval_start interval_seconds action + key bucket_id interval_start action + index ( + name bucket_id_interval_start_interval_seconds + fields bucket_id interval_start interval_seconds + ) field bucket_id blob field interval_start utimestamp @@ -436,8 +446,7 @@ model bucket_bandwidth_rollup ( ) model bucket_storage_rollup ( - key bucket_id - unique bucket_id interval_start interval_seconds + key bucket_id interval_start field bucket_id blob field interval_start utimestamp @@ -450,8 +459,11 @@ model bucket_storage_rollup ( // --- storage node roll up tables --- // model storagenode_bandwidth_rollup ( - key storagenode_id - unique storagenode_id interval_start interval_seconds action + key storagenode_id interval_start action + index ( + name storagenode_id_interval_start_interval_seconds + fields storagenode_id interval_start interval_seconds + ) field storagenode_id blob field interval_start utimestamp @@ -463,8 +475,7 @@ model storagenode_bandwidth_rollup ( ) model storagenode_storage_rollup ( - key storagenode_id - unique storagenode_id interval_start interval_seconds + key storagenode_id interval_start field storagenode_id blob field interval_start utimestamp diff --git a/satellite/satellitedb/dbx/satellitedb.dbx.go b/satellite/satellitedb/dbx/satellitedb.dbx.go index 1e61d2364..1a1284f1c 100644 --- a/satellite/satellitedb/dbx/satellitedb.dbx.go +++ b/satellite/satellitedb/dbx/satellitedb.dbx.go @@ -306,8 +306,7 @@ CREATE TABLE bucket_bandwidth_rollups ( inline bigint NOT NULL, allocated bigint NOT NULL, settled bigint NOT NULL, - PRIMARY KEY ( bucket_id ), - UNIQUE ( bucket_id, interval_start, interval_seconds, action ) + PRIMARY KEY ( bucket_id, interval_start, action ) ); CREATE TABLE bucket_storage_rollups ( bucket_id bytea NOT NULL, @@ -315,8 +314,7 @@ CREATE TABLE bucket_storage_rollups ( interval_seconds integer NOT NULL, inline bigint NOT NULL, remote bigint NOT NULL, - PRIMARY KEY ( bucket_id ), - UNIQUE ( bucket_id, interval_start, interval_seconds ) + PRIMARY KEY ( bucket_id, interval_start ) ); CREATE TABLE bucket_usages ( id bytea NOT NULL, @@ -331,8 +329,7 @@ CREATE TABLE bucket_usages ( repair_egress bigint NOT NULL, get_egress bigint NOT NULL, audit_egress bigint NOT NULL, - PRIMARY KEY ( id ), - UNIQUE ( rollup_end_time, bucket_id ) + PRIMARY KEY ( id ) ); CREATE TABLE bwagreements ( serialnum text NOT NULL, @@ -416,8 +413,7 @@ CREATE TABLE serial_numbers ( serial_number bytea NOT NULL, bucket_id bytea NOT NULL, expires_at timestamp NOT NULL, - PRIMARY KEY ( id ), - UNIQUE ( serial_number ) + PRIMARY KEY ( id ) ); CREATE TABLE storagenode_bandwidth_rollups ( storagenode_id bytea NOT NULL, @@ -426,16 +422,14 @@ CREATE TABLE storagenode_bandwidth_rollups ( action integer NOT NULL, allocated bigint NOT NULL, settled bigint NOT NULL, - PRIMARY KEY ( storagenode_id ), - UNIQUE ( storagenode_id, interval_start, interval_seconds, action ) + PRIMARY KEY ( storagenode_id, interval_start, action ) ); CREATE TABLE storagenode_storage_rollups ( storagenode_id bytea NOT NULL, interval_start timestamp NOT NULL, interval_seconds integer NOT NULL, total bigint NOT NULL, - PRIMARY KEY ( storagenode_id ), - UNIQUE ( storagenode_id, interval_start, interval_seconds ) + PRIMARY KEY ( storagenode_id, interval_start ) ); CREATE TABLE users ( id bytea NOT NULL, @@ -466,10 +460,13 @@ CREATE TABLE project_members ( CREATE TABLE used_serials ( serial_number_id integer NOT NULL REFERENCES serial_numbers( id ) ON DELETE CASCADE, storage_node_id bytea NOT NULL, - PRIMARY KEY ( serial_number_id ), - UNIQUE ( serial_number_id, storage_node_id ) + PRIMARY KEY ( serial_number_id, storage_node_id ) ); -CREATE INDEX serial_numbers_expires_at_index ON serial_numbers ( expires_at );` +CREATE INDEX bucket_id_interval_start_interval_seconds ON bucket_bandwidth_rollups ( bucket_id, interval_start, interval_seconds ); +CREATE UNIQUE INDEX bucket_id_rollup ON bucket_usages ( bucket_id, rollup_end_time ); +CREATE UNIQUE INDEX serial_number ON serial_numbers ( serial_number ); +CREATE INDEX serial_numbers_expires_at_index ON serial_numbers ( expires_at ); +CREATE INDEX storagenode_id_interval_start_interval_seconds ON storagenode_bandwidth_rollups ( storagenode_id, interval_start, interval_seconds );` } func (obj *postgresDB) wrapTx(tx *sql.Tx) txMethods { @@ -567,8 +564,7 @@ CREATE TABLE bucket_bandwidth_rollups ( inline INTEGER NOT NULL, allocated INTEGER NOT NULL, settled INTEGER NOT NULL, - PRIMARY KEY ( bucket_id ), - UNIQUE ( bucket_id, interval_start, interval_seconds, action ) + PRIMARY KEY ( bucket_id, interval_start, action ) ); CREATE TABLE bucket_storage_rollups ( bucket_id BLOB NOT NULL, @@ -576,8 +572,7 @@ CREATE TABLE bucket_storage_rollups ( interval_seconds INTEGER NOT NULL, inline INTEGER NOT NULL, remote INTEGER NOT NULL, - PRIMARY KEY ( bucket_id ), - UNIQUE ( bucket_id, interval_start, interval_seconds ) + PRIMARY KEY ( bucket_id, interval_start ) ); CREATE TABLE bucket_usages ( id BLOB NOT NULL, @@ -592,8 +587,7 @@ CREATE TABLE bucket_usages ( repair_egress INTEGER NOT NULL, get_egress INTEGER NOT NULL, audit_egress INTEGER NOT NULL, - PRIMARY KEY ( id ), - UNIQUE ( rollup_end_time, bucket_id ) + PRIMARY KEY ( id ) ); CREATE TABLE bwagreements ( serialnum TEXT NOT NULL, @@ -677,8 +671,7 @@ CREATE TABLE serial_numbers ( serial_number BLOB NOT NULL, bucket_id BLOB NOT NULL, expires_at TIMESTAMP NOT NULL, - PRIMARY KEY ( id ), - UNIQUE ( serial_number ) + PRIMARY KEY ( id ) ); CREATE TABLE storagenode_bandwidth_rollups ( storagenode_id BLOB NOT NULL, @@ -687,16 +680,14 @@ CREATE TABLE storagenode_bandwidth_rollups ( action INTEGER NOT NULL, allocated INTEGER NOT NULL, settled INTEGER NOT NULL, - PRIMARY KEY ( storagenode_id ), - UNIQUE ( storagenode_id, interval_start, interval_seconds, action ) + PRIMARY KEY ( storagenode_id, interval_start, action ) ); CREATE TABLE storagenode_storage_rollups ( storagenode_id BLOB NOT NULL, interval_start TIMESTAMP NOT NULL, interval_seconds INTEGER NOT NULL, total INTEGER NOT NULL, - PRIMARY KEY ( storagenode_id ), - UNIQUE ( storagenode_id, interval_start, interval_seconds ) + PRIMARY KEY ( storagenode_id, interval_start ) ); CREATE TABLE users ( id BLOB NOT NULL, @@ -727,10 +718,13 @@ CREATE TABLE project_members ( CREATE TABLE used_serials ( serial_number_id INTEGER NOT NULL REFERENCES serial_numbers( id ) ON DELETE CASCADE, storage_node_id BLOB NOT NULL, - PRIMARY KEY ( serial_number_id ), - UNIQUE ( serial_number_id, storage_node_id ) + PRIMARY KEY ( serial_number_id, storage_node_id ) ); -CREATE INDEX serial_numbers_expires_at_index ON serial_numbers ( expires_at );` +CREATE INDEX bucket_id_interval_start_interval_seconds ON bucket_bandwidth_rollups ( bucket_id, interval_start, interval_seconds ); +CREATE UNIQUE INDEX bucket_id_rollup ON bucket_usages ( bucket_id, rollup_end_time ); +CREATE UNIQUE INDEX serial_number ON serial_numbers ( serial_number ); +CREATE INDEX serial_numbers_expires_at_index ON serial_numbers ( expires_at ); +CREATE INDEX storagenode_id_interval_start_interval_seconds ON storagenode_bandwidth_rollups ( storagenode_id, interval_start, interval_seconds );` } func (obj *sqlite3DB) wrapTx(tx *sql.Tx) txMethods { @@ -4089,15 +4083,16 @@ func (obj *postgresImpl) Create_UsedSerial(ctx context.Context, used_serial_serial_number_id UsedSerial_SerialNumberId_Field, used_serial_storage_node_id UsedSerial_StorageNodeId_Field) ( used_serial *UsedSerial, err error) { + __serial_number_id_val := used_serial_serial_number_id.value() __storage_node_id_val := used_serial_storage_node_id.value() - var __embed_stmt = __sqlbundle_Literal("INSERT INTO used_serials ( storage_node_id ) VALUES ( ? ) RETURNING used_serials.serial_number_id, used_serials.storage_node_id") + var __embed_stmt = __sqlbundle_Literal("INSERT INTO used_serials ( serial_number_id, storage_node_id ) VALUES ( ?, ? ) RETURNING used_serials.serial_number_id, used_serials.storage_node_id") var __stmt = __sqlbundle_Render(obj.dialect, __embed_stmt) - obj.logStmt(__stmt, __storage_node_id_val) + obj.logStmt(__stmt, __serial_number_id_val, __storage_node_id_val) used_serial = &UsedSerial{} - err = obj.driver.QueryRow(__stmt, __storage_node_id_val).Scan(&used_serial.SerialNumberId, &used_serial.StorageNodeId) + err = obj.driver.QueryRow(__stmt, __serial_number_id_val, __storage_node_id_val).Scan(&used_serial.SerialNumberId, &used_serial.StorageNodeId) if err != nil { return nil, obj.makeErr(err) } @@ -4973,7 +4968,7 @@ func (obj *postgresImpl) Find_SerialNumber_By_SerialNumber(ctx context.Context, serial_number_serial_number SerialNumber_SerialNumber_Field) ( serial_number *SerialNumber, err error) { - var __embed_stmt = __sqlbundle_Literal("SELECT serial_numbers.id, serial_numbers.serial_number, serial_numbers.bucket_id, serial_numbers.expires_at FROM serial_numbers WHERE serial_numbers.serial_number = ?") + var __embed_stmt = __sqlbundle_Literal("SELECT serial_numbers.id, serial_numbers.serial_number, serial_numbers.bucket_id, serial_numbers.expires_at FROM serial_numbers WHERE serial_numbers.serial_number = ? LIMIT 2") var __values []interface{} __values = append(__values, serial_number_serial_number.value()) @@ -4981,14 +4976,33 @@ func (obj *postgresImpl) Find_SerialNumber_By_SerialNumber(ctx context.Context, var __stmt = __sqlbundle_Render(obj.dialect, __embed_stmt) obj.logStmt(__stmt, __values...) - serial_number = &SerialNumber{} - err = obj.driver.QueryRow(__stmt, __values...).Scan(&serial_number.Id, &serial_number.SerialNumber, &serial_number.BucketId, &serial_number.ExpiresAt) - if err == sql.ErrNoRows { - return nil, nil - } + __rows, err := obj.driver.Query(__stmt, __values...) if err != nil { return nil, obj.makeErr(err) } + defer __rows.Close() + + if !__rows.Next() { + if err := __rows.Err(); err != nil { + return nil, obj.makeErr(err) + } + return nil, nil + } + + serial_number = &SerialNumber{} + err = __rows.Scan(&serial_number.Id, &serial_number.SerialNumber, &serial_number.BucketId, &serial_number.ExpiresAt) + if err != nil { + return nil, obj.makeErr(err) + } + + if __rows.Next() { + return nil, tooManyRows("SerialNumber_By_SerialNumber") + } + + if err := __rows.Err(); err != nil { + return nil, obj.makeErr(err) + } + return serial_number, nil } @@ -6546,14 +6560,15 @@ func (obj *sqlite3Impl) Create_UsedSerial(ctx context.Context, used_serial_serial_number_id UsedSerial_SerialNumberId_Field, used_serial_storage_node_id UsedSerial_StorageNodeId_Field) ( used_serial *UsedSerial, err error) { + __serial_number_id_val := used_serial_serial_number_id.value() __storage_node_id_val := used_serial_storage_node_id.value() - var __embed_stmt = __sqlbundle_Literal("INSERT INTO used_serials ( storage_node_id ) VALUES ( ? )") + var __embed_stmt = __sqlbundle_Literal("INSERT INTO used_serials ( serial_number_id, storage_node_id ) VALUES ( ?, ? )") var __stmt = __sqlbundle_Render(obj.dialect, __embed_stmt) - obj.logStmt(__stmt, __storage_node_id_val) + obj.logStmt(__stmt, __serial_number_id_val, __storage_node_id_val) - __res, err := obj.driver.Exec(__stmt, __storage_node_id_val) + __res, err := obj.driver.Exec(__stmt, __serial_number_id_val, __storage_node_id_val) if err != nil { return nil, obj.makeErr(err) } @@ -7439,7 +7454,7 @@ func (obj *sqlite3Impl) Find_SerialNumber_By_SerialNumber(ctx context.Context, serial_number_serial_number SerialNumber_SerialNumber_Field) ( serial_number *SerialNumber, err error) { - var __embed_stmt = __sqlbundle_Literal("SELECT serial_numbers.id, serial_numbers.serial_number, serial_numbers.bucket_id, serial_numbers.expires_at FROM serial_numbers WHERE serial_numbers.serial_number = ?") + var __embed_stmt = __sqlbundle_Literal("SELECT serial_numbers.id, serial_numbers.serial_number, serial_numbers.bucket_id, serial_numbers.expires_at FROM serial_numbers WHERE serial_numbers.serial_number = ? LIMIT 2") var __values []interface{} __values = append(__values, serial_number_serial_number.value()) @@ -7447,14 +7462,33 @@ func (obj *sqlite3Impl) Find_SerialNumber_By_SerialNumber(ctx context.Context, var __stmt = __sqlbundle_Render(obj.dialect, __embed_stmt) obj.logStmt(__stmt, __values...) - serial_number = &SerialNumber{} - err = obj.driver.QueryRow(__stmt, __values...).Scan(&serial_number.Id, &serial_number.SerialNumber, &serial_number.BucketId, &serial_number.ExpiresAt) - if err == sql.ErrNoRows { - return nil, nil - } + __rows, err := obj.driver.Query(__stmt, __values...) if err != nil { return nil, obj.makeErr(err) } + defer __rows.Close() + + if !__rows.Next() { + if err := __rows.Err(); err != nil { + return nil, obj.makeErr(err) + } + return nil, nil + } + + serial_number = &SerialNumber{} + err = __rows.Scan(&serial_number.Id, &serial_number.SerialNumber, &serial_number.BucketId, &serial_number.ExpiresAt) + if err != nil { + return nil, obj.makeErr(err) + } + + if __rows.Next() { + return nil, tooManyRows("SerialNumber_By_SerialNumber") + } + + if err := __rows.Err(); err != nil { + return nil, obj.makeErr(err) + } + return serial_number, nil } diff --git a/satellite/satellitedb/dbx/satellitedb.dbx.postgres.sql b/satellite/satellitedb/dbx/satellitedb.dbx.postgres.sql index 2c6749149..6e13292bf 100644 --- a/satellite/satellitedb/dbx/satellitedb.dbx.postgres.sql +++ b/satellite/satellitedb/dbx/satellitedb.dbx.postgres.sql @@ -34,8 +34,7 @@ CREATE TABLE bucket_bandwidth_rollups ( inline bigint NOT NULL, allocated bigint NOT NULL, settled bigint NOT NULL, - PRIMARY KEY ( bucket_id ), - UNIQUE ( bucket_id, interval_start, interval_seconds, action ) + PRIMARY KEY ( bucket_id, interval_start, action ) ); CREATE TABLE bucket_storage_rollups ( bucket_id bytea NOT NULL, @@ -43,8 +42,7 @@ CREATE TABLE bucket_storage_rollups ( interval_seconds integer NOT NULL, inline bigint NOT NULL, remote bigint NOT NULL, - PRIMARY KEY ( bucket_id ), - UNIQUE ( bucket_id, interval_start, interval_seconds ) + PRIMARY KEY ( bucket_id, interval_start ) ); CREATE TABLE bucket_usages ( id bytea NOT NULL, @@ -59,8 +57,7 @@ CREATE TABLE bucket_usages ( repair_egress bigint NOT NULL, get_egress bigint NOT NULL, audit_egress bigint NOT NULL, - PRIMARY KEY ( id ), - UNIQUE ( rollup_end_time, bucket_id ) + PRIMARY KEY ( id ) ); CREATE TABLE bwagreements ( serialnum text NOT NULL, @@ -144,8 +141,7 @@ CREATE TABLE serial_numbers ( serial_number bytea NOT NULL, bucket_id bytea NOT NULL, expires_at timestamp NOT NULL, - PRIMARY KEY ( id ), - UNIQUE ( serial_number ) + PRIMARY KEY ( id ) ); CREATE TABLE storagenode_bandwidth_rollups ( storagenode_id bytea NOT NULL, @@ -154,16 +150,14 @@ CREATE TABLE storagenode_bandwidth_rollups ( action integer NOT NULL, allocated bigint NOT NULL, settled bigint NOT NULL, - PRIMARY KEY ( storagenode_id ), - UNIQUE ( storagenode_id, interval_start, interval_seconds, action ) + PRIMARY KEY ( storagenode_id, interval_start, action ) ); CREATE TABLE storagenode_storage_rollups ( storagenode_id bytea NOT NULL, interval_start timestamp NOT NULL, interval_seconds integer NOT NULL, total bigint NOT NULL, - PRIMARY KEY ( storagenode_id ), - UNIQUE ( storagenode_id, interval_start, interval_seconds ) + PRIMARY KEY ( storagenode_id, interval_start ) ); CREATE TABLE users ( id bytea NOT NULL, @@ -194,7 +188,10 @@ CREATE TABLE project_members ( CREATE TABLE used_serials ( serial_number_id integer NOT NULL REFERENCES serial_numbers( id ) ON DELETE CASCADE, storage_node_id bytea NOT NULL, - PRIMARY KEY ( serial_number_id ), - UNIQUE ( serial_number_id, storage_node_id ) + PRIMARY KEY ( serial_number_id, storage_node_id ) ); +CREATE INDEX bucket_id_interval_start_interval_seconds ON bucket_bandwidth_rollups ( bucket_id, interval_start, interval_seconds ); +CREATE UNIQUE INDEX bucket_id_rollup ON bucket_usages ( bucket_id, rollup_end_time ); +CREATE UNIQUE INDEX serial_number ON serial_numbers ( serial_number ); CREATE INDEX serial_numbers_expires_at_index ON serial_numbers ( expires_at ); +CREATE INDEX storagenode_id_interval_start_interval_seconds ON storagenode_bandwidth_rollups ( storagenode_id, interval_start, interval_seconds ); diff --git a/satellite/satellitedb/dbx/satellitedb.dbx.sqlite3.sql b/satellite/satellitedb/dbx/satellitedb.dbx.sqlite3.sql index ef16731f4..7dda317a1 100644 --- a/satellite/satellitedb/dbx/satellitedb.dbx.sqlite3.sql +++ b/satellite/satellitedb/dbx/satellitedb.dbx.sqlite3.sql @@ -34,8 +34,7 @@ CREATE TABLE bucket_bandwidth_rollups ( inline INTEGER NOT NULL, allocated INTEGER NOT NULL, settled INTEGER NOT NULL, - PRIMARY KEY ( bucket_id ), - UNIQUE ( bucket_id, interval_start, interval_seconds, action ) + PRIMARY KEY ( bucket_id, interval_start, action ) ); CREATE TABLE bucket_storage_rollups ( bucket_id BLOB NOT NULL, @@ -43,8 +42,7 @@ CREATE TABLE bucket_storage_rollups ( interval_seconds INTEGER NOT NULL, inline INTEGER NOT NULL, remote INTEGER NOT NULL, - PRIMARY KEY ( bucket_id ), - UNIQUE ( bucket_id, interval_start, interval_seconds ) + PRIMARY KEY ( bucket_id, interval_start ) ); CREATE TABLE bucket_usages ( id BLOB NOT NULL, @@ -59,8 +57,7 @@ CREATE TABLE bucket_usages ( repair_egress INTEGER NOT NULL, get_egress INTEGER NOT NULL, audit_egress INTEGER NOT NULL, - PRIMARY KEY ( id ), - UNIQUE ( rollup_end_time, bucket_id ) + PRIMARY KEY ( id ) ); CREATE TABLE bwagreements ( serialnum TEXT NOT NULL, @@ -144,8 +141,7 @@ CREATE TABLE serial_numbers ( serial_number BLOB NOT NULL, bucket_id BLOB NOT NULL, expires_at TIMESTAMP NOT NULL, - PRIMARY KEY ( id ), - UNIQUE ( serial_number ) + PRIMARY KEY ( id ) ); CREATE TABLE storagenode_bandwidth_rollups ( storagenode_id BLOB NOT NULL, @@ -154,16 +150,14 @@ CREATE TABLE storagenode_bandwidth_rollups ( action INTEGER NOT NULL, allocated INTEGER NOT NULL, settled INTEGER NOT NULL, - PRIMARY KEY ( storagenode_id ), - UNIQUE ( storagenode_id, interval_start, interval_seconds, action ) + PRIMARY KEY ( storagenode_id, interval_start, action ) ); CREATE TABLE storagenode_storage_rollups ( storagenode_id BLOB NOT NULL, interval_start TIMESTAMP NOT NULL, interval_seconds INTEGER NOT NULL, total INTEGER NOT NULL, - PRIMARY KEY ( storagenode_id ), - UNIQUE ( storagenode_id, interval_start, interval_seconds ) + PRIMARY KEY ( storagenode_id, interval_start ) ); CREATE TABLE users ( id BLOB NOT NULL, @@ -194,7 +188,10 @@ CREATE TABLE project_members ( CREATE TABLE used_serials ( serial_number_id INTEGER NOT NULL REFERENCES serial_numbers( id ) ON DELETE CASCADE, storage_node_id BLOB NOT NULL, - PRIMARY KEY ( serial_number_id ), - UNIQUE ( serial_number_id, storage_node_id ) + PRIMARY KEY ( serial_number_id, storage_node_id ) ); +CREATE INDEX bucket_id_interval_start_interval_seconds ON bucket_bandwidth_rollups ( bucket_id, interval_start, interval_seconds ); +CREATE UNIQUE INDEX bucket_id_rollup ON bucket_usages ( bucket_id, rollup_end_time ); +CREATE UNIQUE INDEX serial_number ON serial_numbers ( serial_number ); CREATE INDEX serial_numbers_expires_at_index ON serial_numbers ( expires_at ); +CREATE INDEX storagenode_id_interval_start_interval_seconds ON storagenode_bandwidth_rollups ( storagenode_id, interval_start, interval_seconds ); diff --git a/satellite/satellitedb/migrate.go b/satellite/satellitedb/migrate.go index cb8c52480..2eaf3761d 100644 --- a/satellite/satellitedb/migrate.go +++ b/satellite/satellitedb/migrate.go @@ -371,15 +371,14 @@ func (db *DB) PostgresMigration() *migrate.Migration { serial_number bytea NOT NULL, bucket_id bytea NOT NULL, expires_at timestamp NOT NULL, - PRIMARY KEY ( id ), - UNIQUE ( serial_number ) + PRIMARY KEY ( id ) )`, `CREATE INDEX serial_numbers_expires_at_index ON serial_numbers ( expires_at )`, + `CREATE UNIQUE INDEX serial_number_index ON serial_numbers ( serial_number )`, `CREATE TABLE used_serials ( serial_number_id integer NOT NULL REFERENCES serial_numbers( id ) ON DELETE CASCADE, storage_node_id bytea NOT NULL, - PRIMARY KEY ( serial_number_id ), - UNIQUE ( serial_number_id, storage_node_id ) + PRIMARY KEY ( serial_number_id, storage_node_id ) )`, `CREATE TABLE storagenode_bandwidth_rollups ( storagenode_id bytea NOT NULL, @@ -388,16 +387,19 @@ func (db *DB) PostgresMigration() *migrate.Migration { action integer NOT NULL, allocated bigint NOT NULL, settled bigint NOT NULL, - PRIMARY KEY ( storagenode_id ), - UNIQUE ( storagenode_id, interval_start, interval_seconds, action ) + PRIMARY KEY ( storagenode_id, interval_start, action ) + )`, + `CREATE INDEX storagenode_id_interval_start_interval_seconds_index ON storagenode_bandwidth_rollups ( + storagenode_id, + interval_start, + interval_seconds )`, `CREATE TABLE storagenode_storage_rollups ( storagenode_id bytea NOT NULL, interval_start timestamp NOT NULL, interval_seconds integer NOT NULL, total bigint NOT NULL, - PRIMARY KEY ( storagenode_id ), - UNIQUE ( storagenode_id, interval_start, interval_seconds ) + PRIMARY KEY ( storagenode_id, interval_start ) )`, `CREATE TABLE bucket_bandwidth_rollups ( bucket_id bytea NOT NULL, @@ -407,8 +409,12 @@ func (db *DB) PostgresMigration() *migrate.Migration { inline bigint NOT NULL, allocated bigint NOT NULL, settled bigint NOT NULL, - PRIMARY KEY ( bucket_id ), - UNIQUE ( bucket_id, interval_start, interval_seconds, action ) + PRIMARY KEY ( bucket_id, interval_start, action ) + )`, + `CREATE INDEX bucket_id_interval_start_interval_seconds_index ON bucket_bandwidth_rollups ( + bucket_id, + interval_start, + interval_seconds )`, `CREATE TABLE bucket_storage_rollups ( bucket_id bytea NOT NULL, @@ -416,9 +422,12 @@ func (db *DB) PostgresMigration() *migrate.Migration { interval_seconds integer NOT NULL, inline bigint NOT NULL, remote bigint NOT NULL, - PRIMARY KEY ( bucket_id ), - UNIQUE ( bucket_id, interval_start, interval_seconds ) + PRIMARY KEY ( bucket_id, interval_start ) )`, + `ALTER TABLE bucket_usages DROP CONSTRAINT bucket_usages_rollup_end_time_bucket_id_key`, + `CREATE UNIQUE INDEX bucket_id_rollup_end_time_index ON bucket_usages ( + bucket_id, + rollup_end_time )`, }, }, }, diff --git a/satellite/satellitedb/testdata/postgres.v9.sql b/satellite/satellitedb/testdata/postgres.v9.sql index 0d210b0bc..66bc560ea 100644 --- a/satellite/satellitedb/testdata/postgres.v9.sql +++ b/satellite/satellitedb/testdata/postgres.v9.sql @@ -33,8 +33,7 @@ CREATE TABLE bucket_bandwidth_rollups ( inline bigint NOT NULL, allocated bigint NOT NULL, settled bigint NOT NULL, - PRIMARY KEY ( bucket_id ), - UNIQUE ( bucket_id, interval_start, interval_seconds, action ) + PRIMARY KEY ( bucket_id, interval_start, action ) ); CREATE TABLE bucket_storage_rollups ( bucket_id bytea NOT NULL, @@ -42,8 +41,7 @@ CREATE TABLE bucket_storage_rollups ( interval_seconds integer NOT NULL, inline bigint NOT NULL, remote bigint NOT NULL, - PRIMARY KEY ( bucket_id ), - UNIQUE ( bucket_id, interval_start, interval_seconds ) + PRIMARY KEY ( bucket_id, interval_start ) ); CREATE TABLE bucket_usages ( id bytea NOT NULL, @@ -58,8 +56,7 @@ CREATE TABLE bucket_usages ( repair_egress bigint NOT NULL, get_egress bigint NOT NULL, audit_egress bigint NOT NULL, - PRIMARY KEY ( id ), - UNIQUE ( rollup_end_time, bucket_id ) + PRIMARY KEY ( id ) ); CREATE TABLE bwagreements ( serialnum text NOT NULL, @@ -143,8 +140,7 @@ CREATE TABLE serial_numbers ( serial_number bytea NOT NULL, bucket_id bytea NOT NULL, expires_at timestamp NOT NULL, - PRIMARY KEY ( id ), - UNIQUE ( serial_number ) + PRIMARY KEY ( id ) ); CREATE TABLE storagenode_bandwidth_rollups ( storagenode_id bytea NOT NULL, @@ -153,16 +149,14 @@ CREATE TABLE storagenode_bandwidth_rollups ( action integer NOT NULL, allocated bigint NOT NULL, settled bigint NOT NULL, - PRIMARY KEY ( storagenode_id ), - UNIQUE ( storagenode_id, interval_start, interval_seconds, action ) + PRIMARY KEY ( storagenode_id, interval_start, action ) ); CREATE TABLE storagenode_storage_rollups ( storagenode_id bytea NOT NULL, interval_start timestamp NOT NULL, interval_seconds integer NOT NULL, total bigint NOT NULL, - PRIMARY KEY ( storagenode_id ), - UNIQUE ( storagenode_id, interval_start, interval_seconds ) + PRIMARY KEY ( storagenode_id, interval_start ) ); CREATE TABLE users ( id bytea NOT NULL, @@ -193,10 +187,13 @@ CREATE TABLE project_members ( CREATE TABLE used_serials ( serial_number_id integer NOT NULL REFERENCES serial_numbers( id ) ON DELETE CASCADE, storage_node_id bytea NOT NULL, - PRIMARY KEY ( serial_number_id ), - UNIQUE ( serial_number_id, storage_node_id ) + PRIMARY KEY ( serial_number_id, storage_node_id ) ); +CREATE INDEX bucket_id_interval_start_interval_seconds ON bucket_bandwidth_rollups ( bucket_id, interval_start, interval_seconds ); +CREATE UNIQUE INDEX bucket_id_rollup ON bucket_usages ( bucket_id, rollup_end_time ); +CREATE UNIQUE INDEX serial_number ON serial_numbers ( serial_number ); CREATE INDEX serial_numbers_expires_at_index ON serial_numbers ( expires_at ); +CREATE INDEX storagenode_id_interval_start_interval_seconds ON storagenode_bandwidth_rollups ( storagenode_id, interval_start, interval_seconds ); ---