server: fix indexes for MSSQL event triggers

PR-URL: https://github.com/hasura/graphql-engine-mono/pull/4766
GitOrigin-RevId: 1c1bf173e53fd0b6692131632023817dc843b302
This commit is contained in:
Naveen Naidu 2022-07-01 12:20:13 +05:30 committed by hasura-bot
parent 0c6ddc3666
commit d33025ff5b
5 changed files with 60 additions and 10 deletions

View File

@ -20,6 +20,7 @@ where
import Control.Monad.Trans.Control (MonadBaseControl) import Control.Monad.Trans.Control (MonadBaseControl)
import Data.Environment qualified as Env import Data.Environment qualified as Env
import Data.FileEmbed (makeRelativeToProject) import Data.FileEmbed (makeRelativeToProject)
import Data.Text.Lazy qualified as LT
import Database.MSSQL.Transaction import Database.MSSQL.Transaction
import Database.MSSQL.Transaction qualified as Tx import Database.MSSQL.Transaction qualified as Tx
import Database.ODBC.SQLServer import Database.ODBC.SQLServer
@ -158,12 +159,13 @@ migrateSourceCatalogFrom prevVersion
neededMigrations = neededMigrations =
dropWhile ((/= prevVersion) . fst) sourceMigrations dropWhile ((/= prevVersion) . fst) sourceMigrations
sourceMigrations :: [(CatalogVersion, TxE QErr ())] sourceMigrations :: [(CatalogVersion, TxE QErr [Text])]
sourceMigrations = sourceMigrations =
$( let migrationFromFile from = $( let migrationFromFile from =
let to = succ from let to = succ from
path = "src-rsr/mssql_source_migrations/" <> show from <> "_to_" <> show to <> ".sql" path = "src-rsr/mssql/mssql_source_migrations/" <> show from <> "_to_" <> show to <> ".sql"
in [|multiRowQueryE defaultTxErrorHandler $(makeRelativeToProject path >>= ST.stextFile)|] in do
[|(multiRowQueryE HGE.defaultMSSQLTxErrorHandler $ rawUnescapedText . LT.toStrict $ $(makeRelativeToProject path >>= ST.stextFile))|]
migrationsFromFile = map $ \from -> migrationsFromFile = map $ \from ->
[|($(TH.lift $ from), $(migrationFromFile from))|] [|($(TH.lift $ from), $(migrationFromFile from))|]

View File

@ -19,7 +19,7 @@ import Hasura.Prelude
import Hasura.Server.Migrate.Version import Hasura.Server.Migrate.Version
latestSourceCatalogVersion :: CatalogVersion latestSourceCatalogVersion :: CatalogVersion
latestSourceCatalogVersion = CatalogVersion 1 latestSourceCatalogVersion = CatalogVersion 2
latestSourceCatalogVersionText :: Text latestSourceCatalogVersionText :: Text
latestSourceCatalogVersionText = tshow latestSourceCatalogVersion latestSourceCatalogVersionText = tshow latestSourceCatalogVersion
@ -28,10 +28,23 @@ previousSourceCatalogVersions :: [CatalogVersion]
previousSourceCatalogVersions = [CatalogVersion 1 .. pred latestSourceCatalogVersion] previousSourceCatalogVersions = [CatalogVersion 1 .. pred latestSourceCatalogVersion]
setSourceCatalogVersion :: MonadMSSQLTx m => CatalogVersion -> m () setSourceCatalogVersion :: MonadMSSQLTx m => CatalogVersion -> m ()
setSourceCatalogVersion (CatalogVersion version) = setSourceCatalogVersion (CatalogVersion version) = liftMSSQLTx $ unitQueryE HGE.defaultMSSQLTxErrorHandler setSourceCatalogVersionQuery
liftMSSQLTx $ unitQueryE HGE.defaultMSSQLTxErrorHandler setSourceCatalogVersionQuery
where where
setSourceCatalogVersionQuery = [ODBC.sql| INSERT INTO hdb_catalog.hdb_source_catalog_version(version, upgraded_on) VALUES ($version, SYSDATETIMEOFFSET()) |] setSourceCatalogVersionQuery =
[ODBC.sql|
BEGIN TRANSACTION
IF EXISTS (select 1 from hdb_catalog.hdb_source_catalog_version WITH (UPDLOCK,SERIALIZABLE))
BEGIN
UPDATE hdb_catalog.hdb_source_catalog_version
SET version = $version, upgraded_on = SYSDATETIMEOFFSET()
END
ELSE
BEGIN
INSERT INTO hdb_catalog.hdb_source_catalog_version (version, upgraded_on)
values ($version, SYSDATETIMEOFFSET())
END
COMMIT TRANSACTION
|]
setSourceCatalogVersion CatalogVersion08 = setSourceCatalogVersion CatalogVersion08 =
throw500 "Cannot set the source catalog version to an unstable version." throw500 "Cannot set the source catalog version to an unstable version."

View File

@ -65,6 +65,9 @@ CREATE TABLE hdb_catalog.event_invocation_logs
FOREIGN KEY (event_id) REFERENCES hdb_catalog.event_log (id) FOREIGN KEY (event_id) REFERENCES hdb_catalog.event_log (id)
); );
/* This index improves the performance of deletes by event_id, so that if somebody
tries to delete an event from the hdb_catalog.event_log along with the invocation log
it will be faster with an index compared to without an index. */
CREATE INDEX ON hdb_catalog.event_invocation_logs (event_id); CREATE INDEX ON hdb_catalog.event_invocation_logs (event_id);
CREATE OR REPLACE FUNCTION CREATE OR REPLACE FUNCTION

View File

@ -8,7 +8,10 @@ CREATE TABLE hdb_catalog.event_log
id UNIQUEIDENTIFIER DEFAULT newid() PRIMARY KEY, id UNIQUEIDENTIFIER DEFAULT newid() PRIMARY KEY,
schema_name NVARCHAR(MAX) NOT NULL, schema_name NVARCHAR(MAX) NOT NULL,
table_name NVARCHAR(MAX) NOT NULL, table_name NVARCHAR(MAX) NOT NULL,
trigger_name NVARCHAR(MAX) NOT NULL, /* The maximum key length for a nonclustered index is 1700 bytes.
Hence marking the 'n' for NVARCHAR as 850
*/
trigger_name NVARCHAR(850) NOT NULL,
payload NVARCHAR(MAX) NOT NULL, payload NVARCHAR(MAX) NOT NULL,
delivered BIT NOT NULL DEFAULT 0, delivered BIT NOT NULL DEFAULT 0,
error BIT NOT NULL DEFAULT 0, error BIT NOT NULL DEFAULT 0,
@ -19,15 +22,18 @@ CREATE TABLE hdb_catalog.event_log
archived BIT NOT NULL DEFAULT 0 archived BIT NOT NULL DEFAULT 0
); );
/* This powers `archiveEvents` */
CREATE INDEX event_log_archive_events ON hdb_catalog.event_log (trigger_name);
/* This index powers `fetchEvents` */ /* This index powers `fetchEvents` */
CREATE INDEX event_log_fetch_events CREATE INDEX event_log_fetch_events
ON hdb_catalog.event_log (created_at) ON hdb_catalog.event_log (locked asc, next_retry_at asc, created_at)
WHERE delivered = 0 WHERE delivered = 0
AND error = 0 AND error = 0
AND archived = 0; AND archived = 0;
CREATE TABLE hdb_catalog.event_invocation_logs ( CREATE TABLE hdb_catalog.event_invocation_logs (
id UNIQUEIDENTIFIER NOT NULL DEFAULT newid(), id UNIQUEIDENTIFIER NOT NULL DEFAULT newid() PRIMARY KEY,
event_id UNIQUEIDENTIFIER NOT NULL, event_id UNIQUEIDENTIFIER NOT NULL,
status INTEGER, status INTEGER,
request NVARCHAR(MAX), request NVARCHAR(MAX),
@ -36,3 +42,8 @@ CREATE TABLE hdb_catalog.event_invocation_logs (
FOREIGN KEY (event_id) REFERENCES hdb_catalog.event_log(id) FOREIGN KEY (event_id) REFERENCES hdb_catalog.event_log(id)
); );
/* This index improves the performance of deletes by event_id, so that if somebody
tries to delete an event from the hdb_catalog.event_log along with the invocation log
it will be faster with an index compared to without an index. */
CREATE INDEX fetch_event_invocation_logs ON hdb_catalog.event_invocation_logs (event_id);

View File

@ -0,0 +1,21 @@
ALTER TABLE hdb_catalog.event_log
ALTER COLUMN trigger_name NVARCHAR(850);
/* This powers `archiveEvents` */
CREATE INDEX event_log_archive_events ON hdb_catalog.event_log (trigger_name);
DROP INDEX event_log_fetch_events ON hdb_catalog.event_log
/* This index powers `fetchEvents` */
CREATE INDEX event_log_fetch_events
ON hdb_catalog.event_log (locked asc, next_retry_at asc, created_at)
WHERE delivered = 0
AND error = 0
AND archived = 0;
ALTER TABLE hdb_catalog.event_invocation_logs ADD PRIMARY KEY (id);
/* This index improves the performance of deletes by event_id, so that if somebody
tries to delete an event from the hdb_catalog.event_log along with the invocation log
it will be faster with an index compared to without an index. */
CREATE INDEX fetch_event_invocation_logs ON hdb_catalog.event_invocation_logs (event_id);