go.temporal.io/server@v1.23.0/common/persistence/sql/sqlplugin/postgresql/execution.go (about)

     1  // The MIT License
     2  //
     3  // Copyright (c) 2020 Temporal Technologies Inc.  All rights reserved.
     4  //
     5  // Copyright (c) 2020 Uber Technologies, Inc.
     6  //
     7  // Permission is hereby granted, free of charge, to any person obtaining a copy
     8  // of this software and associated documentation files (the "Software"), to deal
     9  // in the Software without restriction, including without limitation the rights
    10  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    11  // copies of the Software, and to permit persons to whom the Software is
    12  // furnished to do so, subject to the following conditions:
    13  //
    14  // The above copyright notice and this permission notice shall be included in
    15  // all copies or substantial portions of the Software.
    16  //
    17  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    18  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    19  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    20  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    21  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    22  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    23  // THE SOFTWARE.
    24  
    25  package postgresql
    26  
    27  import (
    28  	"context"
    29  	"database/sql"
    30  
    31  	"go.temporal.io/server/common/persistence/sql/sqlplugin"
    32  )
    33  
    34  const (
    35  	executionsColumns = `shard_id, namespace_id, workflow_id, run_id, next_event_id, last_write_version, data, data_encoding, state, state_encoding, db_record_version`
    36  
    37  	createExecutionQuery = `INSERT INTO executions(` + executionsColumns + `)
    38   VALUES(:shard_id, :namespace_id, :workflow_id, :run_id, :next_event_id, :last_write_version, :data, :data_encoding, :state, :state_encoding, :db_record_version)`
    39  
    40  	updateExecutionQuery = `UPDATE executions SET
    41   db_record_version = :db_record_version, next_event_id = :next_event_id, last_write_version = :last_write_version, data = :data, data_encoding = :data_encoding, state = :state, state_encoding = :state_encoding
    42   WHERE shard_id = :shard_id AND namespace_id = :namespace_id AND workflow_id = :workflow_id AND run_id = :run_id`
    43  
    44  	getExecutionQuery = `SELECT ` + executionsColumns + ` FROM executions
    45   WHERE shard_id = $1 AND namespace_id = $2 AND workflow_id = $3 AND run_id = $4`
    46  
    47  	deleteExecutionQuery = `DELETE FROM executions 
    48   WHERE shard_id = $1 AND namespace_id = $2 AND workflow_id = $3 AND run_id = $4`
    49  
    50  	lockExecutionQueryBase = `SELECT db_record_version, next_event_id FROM executions 
    51   WHERE shard_id = $1 AND namespace_id = $2 AND workflow_id = $3 AND run_id = $4`
    52  
    53  	writeLockExecutionQuery = lockExecutionQueryBase + ` FOR UPDATE`
    54  	readLockExecutionQuery  = lockExecutionQueryBase + ` FOR SHARE`
    55  
    56  	createCurrentExecutionQuery = `INSERT INTO current_executions
    57  (shard_id, namespace_id, workflow_id, run_id, create_request_id, state, status, last_write_version) VALUES
    58  (:shard_id, :namespace_id, :workflow_id, :run_id, :create_request_id, :state, :status, :last_write_version)`
    59  
    60  	deleteCurrentExecutionQuery = "DELETE FROM current_executions WHERE shard_id = $1 AND namespace_id = $2 AND workflow_id = $3 AND run_id = $4"
    61  
    62  	getCurrentExecutionQuery = `SELECT
    63  shard_id, namespace_id, workflow_id, run_id, create_request_id, state, status, last_write_version
    64  FROM current_executions WHERE shard_id = $1 AND namespace_id = $2 AND workflow_id = $3`
    65  
    66  	lockCurrentExecutionJoinExecutionsQuery = `SELECT
    67  ce.shard_id, ce.namespace_id, ce.workflow_id, ce.run_id, ce.create_request_id, ce.state, ce.status, e.last_write_version
    68  FROM current_executions ce
    69  INNER JOIN executions e ON e.shard_id = ce.shard_id AND e.namespace_id = ce.namespace_id AND e.workflow_id = ce.workflow_id AND e.run_id = ce.run_id
    70  WHERE ce.shard_id = $1 AND ce.namespace_id = $2 AND ce.workflow_id = $3 FOR UPDATE`
    71  
    72  	lockCurrentExecutionQuery = getCurrentExecutionQuery + ` FOR UPDATE`
    73  
    74  	updateCurrentExecutionsQuery = `UPDATE current_executions SET
    75  run_id = :run_id,
    76  create_request_id = :create_request_id,
    77  state = :state,
    78  status = :status,
    79  last_write_version = :last_write_version
    80  WHERE
    81  shard_id = :shard_id AND
    82  namespace_id = :namespace_id AND
    83  workflow_id = :workflow_id
    84  `
    85  
    86  	createHistoryImmediateTasksQuery = `INSERT INTO history_immediate_tasks(shard_id, category_id, task_id, data, data_encoding) 
    87   VALUES(:shard_id, :category_id, :task_id, :data, :data_encoding)`
    88  
    89  	getHistoryImmediateTasksQuery = `SELECT task_id, data, data_encoding 
    90   FROM history_immediate_tasks WHERE shard_id = $1 AND category_id = $2 AND task_id >= $3 AND task_id < $4 ORDER BY task_id LIMIT $5`
    91  
    92  	deleteHistoryImmediateTaskQuery       = `DELETE FROM history_immediate_tasks WHERE shard_id = $1 AND category_id = $2 AND task_id = $3`
    93  	rangeDeleteHistoryImmediateTasksQuery = `DELETE FROM history_immediate_tasks WHERE shard_id = $1 AND category_id = $2 AND task_id >= $3 AND task_id < $4`
    94  
    95  	createHistoryScheduledTasksQuery = `INSERT INTO history_scheduled_tasks (shard_id, category_id, visibility_timestamp, task_id, data, data_encoding)
    96    VALUES (:shard_id, :category_id, :visibility_timestamp, :task_id, :data, :data_encoding)`
    97  
    98  	getHistoryScheduledTasksQuery = `SELECT visibility_timestamp, task_id, data, data_encoding FROM history_scheduled_tasks 
    99    WHERE shard_id = $1 
   100    AND category_id = $2 
   101    AND ((visibility_timestamp >= $3 AND task_id >= $4) OR visibility_timestamp > $5) 
   102    AND visibility_timestamp < $6
   103    ORDER BY visibility_timestamp,task_id LIMIT $7`
   104  
   105  	deleteHistoryScheduledTaskQuery       = `DELETE FROM history_scheduled_tasks WHERE shard_id = $1 AND category_id = $2 AND visibility_timestamp = $3 AND task_id = $4`
   106  	rangeDeleteHistoryScheduledTasksQuery = `DELETE FROM history_scheduled_tasks WHERE shard_id = $1 AND category_id = $2 AND visibility_timestamp >= $3 AND visibility_timestamp < $4`
   107  
   108  	createTransferTasksQuery = `INSERT INTO transfer_tasks(shard_id, task_id, data, data_encoding) 
   109   VALUES(:shard_id, :task_id, :data, :data_encoding)`
   110  
   111  	getTransferTasksQuery = `SELECT task_id, data, data_encoding 
   112   FROM transfer_tasks WHERE shard_id = $1 AND task_id >= $2 AND task_id < $3 ORDER BY task_id LIMIT $4`
   113  
   114  	deleteTransferTaskQuery      = `DELETE FROM transfer_tasks WHERE shard_id = $1 AND task_id = $2`
   115  	rangeDeleteTransferTaskQuery = `DELETE FROM transfer_tasks WHERE shard_id = $1 AND task_id >= $2 AND task_id < $3`
   116  
   117  	createTimerTasksQuery = `INSERT INTO timer_tasks (shard_id, visibility_timestamp, task_id, data, data_encoding)
   118    VALUES (:shard_id, :visibility_timestamp, :task_id, :data, :data_encoding)`
   119  
   120  	getTimerTasksQuery = `SELECT visibility_timestamp, task_id, data, data_encoding FROM timer_tasks 
   121    WHERE shard_id = $1 
   122    AND ((visibility_timestamp >= $2 AND task_id >= $3) OR visibility_timestamp > $4) 
   123    AND visibility_timestamp < $5
   124    ORDER BY visibility_timestamp,task_id LIMIT $6`
   125  
   126  	deleteTimerTaskQuery      = `DELETE FROM timer_tasks WHERE shard_id = $1 AND visibility_timestamp = $2 AND task_id = $3`
   127  	rangeDeleteTimerTaskQuery = `DELETE FROM timer_tasks WHERE shard_id = $1 AND visibility_timestamp >= $2 AND visibility_timestamp < $3`
   128  
   129  	createReplicationTasksQuery = `INSERT INTO replication_tasks (shard_id, task_id, data, data_encoding) 
   130    VALUES(:shard_id, :task_id, :data, :data_encoding)`
   131  
   132  	getReplicationTasksQuery = `SELECT task_id, data, data_encoding FROM replication_tasks WHERE 
   133  shard_id = $1 AND task_id >= $2 AND task_id < $3 ORDER BY task_id LIMIT $4`
   134  
   135  	deleteReplicationTaskQuery      = `DELETE FROM replication_tasks WHERE shard_id = $1 AND task_id = $2`
   136  	rangeDeleteReplicationTaskQuery = `DELETE FROM replication_tasks WHERE shard_id = $1 AND task_id >= $2 AND task_id < $3`
   137  
   138  	getReplicationTasksDLQQuery = `SELECT task_id, data, data_encoding FROM replication_tasks_dlq WHERE 
   139  source_cluster_name = $1 AND
   140  shard_id = $2 AND
   141  task_id >= $3 AND
   142  task_id < $4
   143  ORDER BY task_id LIMIT $5`
   144  
   145  	createVisibilityTasksQuery = `INSERT INTO visibility_tasks(shard_id, task_id, data, data_encoding) 
   146   VALUES(:shard_id, :task_id, :data, :data_encoding)`
   147  
   148  	getVisibilityTasksQuery = `SELECT task_id, data, data_encoding 
   149   FROM visibility_tasks WHERE shard_id = $1 AND task_id >= $2 AND task_id < $3 ORDER BY task_id LIMIT $4`
   150  
   151  	deleteVisibilityTaskQuery      = `DELETE FROM visibility_tasks WHERE shard_id = $1 AND task_id = $2`
   152  	rangeDeleteVisibilityTaskQuery = `DELETE FROM visibility_tasks WHERE shard_id = $1 AND task_id >= $2 AND task_id < $3`
   153  
   154  	bufferedEventsColumns     = `shard_id, namespace_id, workflow_id, run_id, data, data_encoding`
   155  	createBufferedEventsQuery = `INSERT INTO buffered_events(` + bufferedEventsColumns + `)
   156  VALUES (:shard_id, :namespace_id, :workflow_id, :run_id, :data, :data_encoding)`
   157  
   158  	deleteBufferedEventsQuery = `DELETE FROM buffered_events WHERE shard_id = $1 AND namespace_id = $2 AND workflow_id = $3 AND run_id = $4`
   159  	getBufferedEventsQuery    = `SELECT data, data_encoding FROM buffered_events WHERE shard_id = $1 AND namespace_id = $2 AND workflow_id = $3 AND run_id = $4`
   160  
   161  	insertReplicationTaskDLQQuery = `
   162  INSERT INTO replication_tasks_dlq 
   163              (source_cluster_name, 
   164               shard_id, 
   165               task_id, 
   166               data, 
   167               data_encoding) 
   168  VALUES     (:source_cluster_name, 
   169              :shard_id, 
   170              :task_id, 
   171              :data, 
   172              :data_encoding)
   173  `
   174  	deleteReplicationTaskFromDLQQuery = `
   175  	DELETE FROM replication_tasks_dlq 
   176  		WHERE source_cluster_name = $1 
   177  		AND shard_id = $2 
   178  		AND task_id = $3`
   179  
   180  	rangeDeleteReplicationTaskFromDLQQuery = `
   181  	DELETE FROM replication_tasks_dlq 
   182  		WHERE source_cluster_name = $1 
   183  		AND shard_id = $2 
   184  		AND task_id >= $3
   185  		AND task_id < $4`
   186  )
   187  
   188  // InsertIntoExecutions inserts a row into executions table
   189  func (pdb *db) InsertIntoExecutions(
   190  	ctx context.Context,
   191  	row *sqlplugin.ExecutionsRow,
   192  ) (sql.Result, error) {
   193  	return pdb.conn.NamedExecContext(ctx,
   194  		createExecutionQuery,
   195  		row,
   196  	)
   197  }
   198  
   199  // UpdateExecutions updates a single row in executions table
   200  func (pdb *db) UpdateExecutions(
   201  	ctx context.Context,
   202  	row *sqlplugin.ExecutionsRow,
   203  ) (sql.Result, error) {
   204  	return pdb.conn.NamedExecContext(ctx,
   205  		updateExecutionQuery,
   206  		row,
   207  	)
   208  }
   209  
   210  // SelectFromExecutions reads a single row from executions table
   211  func (pdb *db) SelectFromExecutions(
   212  	ctx context.Context,
   213  	filter sqlplugin.ExecutionsFilter,
   214  ) (*sqlplugin.ExecutionsRow, error) {
   215  	var row sqlplugin.ExecutionsRow
   216  	err := pdb.conn.GetContext(ctx,
   217  		&row,
   218  		getExecutionQuery,
   219  		filter.ShardID,
   220  		filter.NamespaceID,
   221  		filter.WorkflowID,
   222  		filter.RunID,
   223  	)
   224  	if err != nil {
   225  		return nil, err
   226  	}
   227  	return &row, nil
   228  }
   229  
   230  // DeleteFromExecutions deletes a single row from executions table
   231  func (pdb *db) DeleteFromExecutions(
   232  	ctx context.Context,
   233  	filter sqlplugin.ExecutionsFilter,
   234  ) (sql.Result, error) {
   235  	return pdb.conn.ExecContext(ctx,
   236  		deleteExecutionQuery,
   237  		filter.ShardID,
   238  		filter.NamespaceID,
   239  		filter.WorkflowID,
   240  		filter.RunID,
   241  	)
   242  }
   243  
   244  // ReadLockExecutions acquires a write lock on a single row in executions table
   245  func (pdb *db) ReadLockExecutions(
   246  	ctx context.Context,
   247  	filter sqlplugin.ExecutionsFilter,
   248  ) (int64, int64, error) {
   249  	var executionVersion sqlplugin.ExecutionVersion
   250  	err := pdb.conn.GetContext(ctx,
   251  		&executionVersion,
   252  		readLockExecutionQuery,
   253  		filter.ShardID,
   254  		filter.NamespaceID,
   255  		filter.WorkflowID,
   256  		filter.RunID,
   257  	)
   258  	return executionVersion.DBRecordVersion, executionVersion.NextEventID, err
   259  }
   260  
   261  // WriteLockExecutions acquires a write lock on a single row in executions table
   262  func (pdb *db) WriteLockExecutions(
   263  	ctx context.Context,
   264  	filter sqlplugin.ExecutionsFilter,
   265  ) (int64, int64, error) {
   266  	var executionVersion sqlplugin.ExecutionVersion
   267  	err := pdb.conn.GetContext(ctx,
   268  		&executionVersion,
   269  		writeLockExecutionQuery,
   270  		filter.ShardID,
   271  		filter.NamespaceID,
   272  		filter.WorkflowID,
   273  		filter.RunID,
   274  	)
   275  	return executionVersion.DBRecordVersion, executionVersion.NextEventID, err
   276  }
   277  
   278  // InsertIntoCurrentExecutions inserts a single row into current_executions table
   279  func (pdb *db) InsertIntoCurrentExecutions(
   280  	ctx context.Context,
   281  	row *sqlplugin.CurrentExecutionsRow,
   282  ) (sql.Result, error) {
   283  	return pdb.conn.NamedExecContext(ctx,
   284  		createCurrentExecutionQuery,
   285  		row,
   286  	)
   287  }
   288  
   289  // UpdateCurrentExecutions updates a single row in current_executions table
   290  func (pdb *db) UpdateCurrentExecutions(
   291  	ctx context.Context,
   292  	row *sqlplugin.CurrentExecutionsRow,
   293  ) (sql.Result, error) {
   294  	return pdb.conn.NamedExecContext(ctx,
   295  		updateCurrentExecutionsQuery,
   296  		row,
   297  	)
   298  }
   299  
   300  // SelectFromCurrentExecutions reads one or more rows from current_executions table
   301  func (pdb *db) SelectFromCurrentExecutions(
   302  	ctx context.Context,
   303  	filter sqlplugin.CurrentExecutionsFilter,
   304  ) (*sqlplugin.CurrentExecutionsRow, error) {
   305  	var row sqlplugin.CurrentExecutionsRow
   306  	err := pdb.conn.GetContext(ctx,
   307  		&row,
   308  		getCurrentExecutionQuery,
   309  		filter.ShardID,
   310  		filter.NamespaceID,
   311  		filter.WorkflowID,
   312  	)
   313  	return &row, err
   314  }
   315  
   316  // DeleteFromCurrentExecutions deletes a single row in current_executions table
   317  func (pdb *db) DeleteFromCurrentExecutions(
   318  	ctx context.Context,
   319  	filter sqlplugin.CurrentExecutionsFilter,
   320  ) (sql.Result, error) {
   321  	return pdb.conn.ExecContext(ctx,
   322  		deleteCurrentExecutionQuery,
   323  		filter.ShardID,
   324  		filter.NamespaceID,
   325  		filter.WorkflowID,
   326  		filter.RunID,
   327  	)
   328  }
   329  
   330  // LockCurrentExecutions acquires a write lock on a single row in current_executions table
   331  func (pdb *db) LockCurrentExecutions(
   332  	ctx context.Context,
   333  	filter sqlplugin.CurrentExecutionsFilter,
   334  ) (*sqlplugin.CurrentExecutionsRow, error) {
   335  	var row sqlplugin.CurrentExecutionsRow
   336  	err := pdb.conn.GetContext(ctx,
   337  		&row,
   338  		lockCurrentExecutionQuery,
   339  		filter.ShardID,
   340  		filter.NamespaceID,
   341  		filter.WorkflowID,
   342  	)
   343  	return &row, err
   344  }
   345  
   346  // LockCurrentExecutionsJoinExecutions joins a row in current_executions with executions table and acquires a
   347  // write lock on the result
   348  func (pdb *db) LockCurrentExecutionsJoinExecutions(
   349  	ctx context.Context,
   350  	filter sqlplugin.CurrentExecutionsFilter,
   351  ) ([]sqlplugin.CurrentExecutionsRow, error) {
   352  	var rows []sqlplugin.CurrentExecutionsRow
   353  	err := pdb.conn.SelectContext(ctx,
   354  		&rows,
   355  		lockCurrentExecutionJoinExecutionsQuery,
   356  		filter.ShardID,
   357  		filter.NamespaceID,
   358  		filter.WorkflowID,
   359  	)
   360  	return rows, err
   361  }
   362  
   363  // InsertIntoHistoryImmediateTasks inserts one or more rows into history_immediate_tasks table
   364  func (pdb *db) InsertIntoHistoryImmediateTasks(
   365  	ctx context.Context,
   366  	rows []sqlplugin.HistoryImmediateTasksRow,
   367  ) (sql.Result, error) {
   368  	return pdb.conn.NamedExecContext(ctx,
   369  		createHistoryImmediateTasksQuery,
   370  		rows,
   371  	)
   372  }
   373  
   374  // RangeSelectFromHistoryImmediateTasks reads one or more rows from transfer_tasks table
   375  func (pdb *db) RangeSelectFromHistoryImmediateTasks(
   376  	ctx context.Context,
   377  	filter sqlplugin.HistoryImmediateTasksRangeFilter,
   378  ) ([]sqlplugin.HistoryImmediateTasksRow, error) {
   379  	var rows []sqlplugin.HistoryImmediateTasksRow
   380  	if err := pdb.conn.SelectContext(ctx,
   381  		&rows,
   382  		getHistoryImmediateTasksQuery,
   383  		filter.ShardID,
   384  		filter.CategoryID,
   385  		filter.InclusiveMinTaskID,
   386  		filter.ExclusiveMaxTaskID,
   387  		filter.PageSize,
   388  	); err != nil {
   389  		return nil, err
   390  	}
   391  	return rows, nil
   392  }
   393  
   394  // DeleteFromHistoryImmediateTasks deletes one or more rows from transfer_tasks table
   395  func (pdb *db) DeleteFromHistoryImmediateTasks(
   396  	ctx context.Context,
   397  	filter sqlplugin.HistoryImmediateTasksFilter,
   398  ) (sql.Result, error) {
   399  	return pdb.conn.ExecContext(ctx,
   400  		deleteHistoryImmediateTaskQuery,
   401  		filter.ShardID,
   402  		filter.CategoryID,
   403  		filter.TaskID,
   404  	)
   405  }
   406  
   407  // RangeDeleteFromHistoryImmediateTasks deletes one or more rows from transfer_tasks table
   408  func (pdb *db) RangeDeleteFromHistoryImmediateTasks(
   409  	ctx context.Context,
   410  	filter sqlplugin.HistoryImmediateTasksRangeFilter,
   411  ) (sql.Result, error) {
   412  	return pdb.conn.ExecContext(ctx,
   413  		rangeDeleteHistoryImmediateTasksQuery,
   414  		filter.ShardID,
   415  		filter.CategoryID,
   416  		filter.InclusiveMinTaskID,
   417  		filter.ExclusiveMaxTaskID,
   418  	)
   419  }
   420  
   421  // InsertIntoHistoryScheduledTasks inserts one or more rows into timer_tasks table
   422  func (pdb *db) InsertIntoHistoryScheduledTasks(
   423  	ctx context.Context,
   424  	rows []sqlplugin.HistoryScheduledTasksRow,
   425  ) (sql.Result, error) {
   426  	for i := range rows {
   427  		rows[i].VisibilityTimestamp = pdb.converter.ToPostgreSQLDateTime(rows[i].VisibilityTimestamp)
   428  	}
   429  	return pdb.conn.NamedExecContext(
   430  		ctx,
   431  		createHistoryScheduledTasksQuery,
   432  		rows,
   433  	)
   434  }
   435  
   436  // RangeSelectFromHistoryScheduledTasks reads one or more rows from timer_tasks table
   437  func (pdb *db) RangeSelectFromHistoryScheduledTasks(
   438  	ctx context.Context,
   439  	filter sqlplugin.HistoryScheduledTasksRangeFilter,
   440  ) ([]sqlplugin.HistoryScheduledTasksRow, error) {
   441  	var rows []sqlplugin.HistoryScheduledTasksRow
   442  	filter.InclusiveMinVisibilityTimestamp = pdb.converter.ToPostgreSQLDateTime(filter.InclusiveMinVisibilityTimestamp)
   443  	filter.ExclusiveMaxVisibilityTimestamp = pdb.converter.ToPostgreSQLDateTime(filter.ExclusiveMaxVisibilityTimestamp)
   444  	if err := pdb.conn.SelectContext(ctx,
   445  		&rows,
   446  		getHistoryScheduledTasksQuery,
   447  		filter.ShardID,
   448  		filter.CategoryID,
   449  		filter.InclusiveMinVisibilityTimestamp,
   450  		filter.InclusiveMinTaskID,
   451  		filter.InclusiveMinVisibilityTimestamp,
   452  		filter.ExclusiveMaxVisibilityTimestamp,
   453  		filter.PageSize,
   454  	); err != nil {
   455  		return nil, err
   456  	}
   457  	for i := range rows {
   458  		rows[i].VisibilityTimestamp = pdb.converter.ToPostgreSQLDateTime(rows[i].VisibilityTimestamp)
   459  	}
   460  	return rows, nil
   461  }
   462  
   463  // DeleteFromHistoryScheduledTasks deletes one or more rows from timer_tasks table
   464  func (pdb *db) DeleteFromHistoryScheduledTasks(
   465  	ctx context.Context,
   466  	filter sqlplugin.HistoryScheduledTasksFilter,
   467  ) (sql.Result, error) {
   468  	filter.VisibilityTimestamp = pdb.converter.ToPostgreSQLDateTime(filter.VisibilityTimestamp)
   469  	return pdb.conn.ExecContext(ctx,
   470  		deleteHistoryScheduledTaskQuery,
   471  		filter.ShardID,
   472  		filter.CategoryID,
   473  		filter.VisibilityTimestamp,
   474  		filter.TaskID,
   475  	)
   476  }
   477  
   478  // RangeDeleteFromHistoryScheduledTasks deletes one or more rows from timer_tasks table
   479  func (pdb *db) RangeDeleteFromHistoryScheduledTasks(
   480  	ctx context.Context,
   481  	filter sqlplugin.HistoryScheduledTasksRangeFilter,
   482  ) (sql.Result, error) {
   483  	filter.InclusiveMinVisibilityTimestamp = pdb.converter.ToPostgreSQLDateTime(filter.InclusiveMinVisibilityTimestamp)
   484  	filter.ExclusiveMaxVisibilityTimestamp = pdb.converter.ToPostgreSQLDateTime(filter.ExclusiveMaxVisibilityTimestamp)
   485  	return pdb.conn.ExecContext(ctx,
   486  		rangeDeleteHistoryScheduledTasksQuery,
   487  		filter.ShardID,
   488  		filter.CategoryID,
   489  		filter.InclusiveMinVisibilityTimestamp,
   490  		filter.ExclusiveMaxVisibilityTimestamp,
   491  	)
   492  }
   493  
   494  // InsertIntoTransferTasks inserts one or more rows into transfer_tasks table
   495  func (pdb *db) InsertIntoTransferTasks(
   496  	ctx context.Context,
   497  	rows []sqlplugin.TransferTasksRow,
   498  ) (sql.Result, error) {
   499  	return pdb.conn.NamedExecContext(ctx,
   500  		createTransferTasksQuery,
   501  		rows,
   502  	)
   503  }
   504  
   505  // RangeSelectFromTransferTasks reads one or more rows from transfer_tasks table
   506  func (pdb *db) RangeSelectFromTransferTasks(
   507  	ctx context.Context,
   508  	filter sqlplugin.TransferTasksRangeFilter,
   509  ) ([]sqlplugin.TransferTasksRow, error) {
   510  	var rows []sqlplugin.TransferTasksRow
   511  	err := pdb.conn.SelectContext(ctx,
   512  		&rows,
   513  		getTransferTasksQuery,
   514  		filter.ShardID,
   515  		filter.InclusiveMinTaskID,
   516  		filter.ExclusiveMaxTaskID,
   517  		filter.PageSize,
   518  	)
   519  	if err != nil {
   520  		return nil, err
   521  	}
   522  	return rows, nil
   523  }
   524  
   525  // DeleteFromTransferTasks deletes one or more rows from transfer_tasks table
   526  func (pdb *db) DeleteFromTransferTasks(
   527  	ctx context.Context,
   528  	filter sqlplugin.TransferTasksFilter,
   529  ) (sql.Result, error) {
   530  	return pdb.conn.ExecContext(ctx,
   531  		deleteTransferTaskQuery,
   532  		filter.ShardID,
   533  		filter.TaskID,
   534  	)
   535  }
   536  
   537  // RangeDeleteFromTransferTasks deletes one or more rows from transfer_tasks table
   538  func (pdb *db) RangeDeleteFromTransferTasks(
   539  	ctx context.Context,
   540  	filter sqlplugin.TransferTasksRangeFilter,
   541  ) (sql.Result, error) {
   542  	return pdb.conn.ExecContext(ctx,
   543  		rangeDeleteTransferTaskQuery,
   544  		filter.ShardID,
   545  		filter.InclusiveMinTaskID,
   546  		filter.ExclusiveMaxTaskID,
   547  	)
   548  }
   549  
   550  // InsertIntoTimerTasks inserts one or more rows into timer_tasks table
   551  func (pdb *db) InsertIntoTimerTasks(
   552  	ctx context.Context,
   553  	rows []sqlplugin.TimerTasksRow,
   554  ) (sql.Result, error) {
   555  	for i := range rows {
   556  		rows[i].VisibilityTimestamp = pdb.converter.ToPostgreSQLDateTime(rows[i].VisibilityTimestamp)
   557  	}
   558  	return pdb.conn.NamedExecContext(ctx,
   559  		createTimerTasksQuery,
   560  		rows,
   561  	)
   562  }
   563  
   564  // RangeSelectFromTimerTasks reads one or more rows from timer_tasks table
   565  func (pdb *db) RangeSelectFromTimerTasks(
   566  	ctx context.Context,
   567  	filter sqlplugin.TimerTasksRangeFilter,
   568  ) ([]sqlplugin.TimerTasksRow, error) {
   569  	var rows []sqlplugin.TimerTasksRow
   570  	filter.InclusiveMinVisibilityTimestamp = pdb.converter.ToPostgreSQLDateTime(filter.InclusiveMinVisibilityTimestamp)
   571  	filter.ExclusiveMaxVisibilityTimestamp = pdb.converter.ToPostgreSQLDateTime(filter.ExclusiveMaxVisibilityTimestamp)
   572  	err := pdb.conn.SelectContext(ctx,
   573  		&rows,
   574  		getTimerTasksQuery,
   575  		filter.ShardID,
   576  		filter.InclusiveMinVisibilityTimestamp,
   577  		filter.InclusiveMinTaskID,
   578  		filter.InclusiveMinVisibilityTimestamp,
   579  		filter.ExclusiveMaxVisibilityTimestamp,
   580  		filter.PageSize,
   581  	)
   582  	if err != nil {
   583  		return nil, err
   584  	}
   585  	for i := range rows {
   586  		rows[i].VisibilityTimestamp = pdb.converter.FromPostgreSQLDateTime(rows[i].VisibilityTimestamp)
   587  	}
   588  	return rows, nil
   589  }
   590  
   591  // DeleteFromTimerTasks deletes one or more rows from timer_tasks table
   592  func (pdb *db) DeleteFromTimerTasks(
   593  	ctx context.Context,
   594  	filter sqlplugin.TimerTasksFilter,
   595  ) (sql.Result, error) {
   596  	filter.VisibilityTimestamp = pdb.converter.ToPostgreSQLDateTime(filter.VisibilityTimestamp)
   597  	return pdb.conn.ExecContext(ctx,
   598  		deleteTimerTaskQuery,
   599  		filter.ShardID,
   600  		filter.VisibilityTimestamp,
   601  		filter.TaskID,
   602  	)
   603  }
   604  
   605  // RangeDeleteFromTimerTasks deletes one or more rows from timer_tasks table
   606  func (pdb *db) RangeDeleteFromTimerTasks(
   607  	ctx context.Context,
   608  	filter sqlplugin.TimerTasksRangeFilter,
   609  ) (sql.Result, error) {
   610  	filter.InclusiveMinVisibilityTimestamp = pdb.converter.ToPostgreSQLDateTime(filter.InclusiveMinVisibilityTimestamp)
   611  	filter.ExclusiveMaxVisibilityTimestamp = pdb.converter.ToPostgreSQLDateTime(filter.ExclusiveMaxVisibilityTimestamp)
   612  	return pdb.conn.ExecContext(ctx,
   613  		rangeDeleteTimerTaskQuery,
   614  		filter.ShardID,
   615  		filter.InclusiveMinVisibilityTimestamp,
   616  		filter.ExclusiveMaxVisibilityTimestamp,
   617  	)
   618  }
   619  
   620  // InsertIntoBufferedEvents inserts one or more rows into buffered_events table
   621  func (pdb *db) InsertIntoBufferedEvents(
   622  	ctx context.Context,
   623  	rows []sqlplugin.BufferedEventsRow,
   624  ) (sql.Result, error) {
   625  	return pdb.conn.NamedExecContext(ctx,
   626  		createBufferedEventsQuery,
   627  		rows,
   628  	)
   629  }
   630  
   631  // SelectFromBufferedEvents reads one or more rows from buffered_events table
   632  func (pdb *db) SelectFromBufferedEvents(
   633  	ctx context.Context,
   634  	filter sqlplugin.BufferedEventsFilter,
   635  ) ([]sqlplugin.BufferedEventsRow, error) {
   636  	var rows []sqlplugin.BufferedEventsRow
   637  	if err := pdb.conn.SelectContext(ctx,
   638  		&rows,
   639  		getBufferedEventsQuery,
   640  		filter.ShardID,
   641  		filter.NamespaceID,
   642  		filter.WorkflowID,
   643  		filter.RunID,
   644  	); err != nil {
   645  		return nil, err
   646  	}
   647  	for i := 0; i < len(rows); i++ {
   648  		rows[i].NamespaceID = filter.NamespaceID
   649  		rows[i].WorkflowID = filter.WorkflowID
   650  		rows[i].RunID = filter.RunID
   651  		rows[i].ShardID = filter.ShardID
   652  	}
   653  	return rows, nil
   654  }
   655  
   656  // DeleteFromBufferedEvents deletes one or more rows from buffered_events table
   657  func (pdb *db) DeleteFromBufferedEvents(
   658  	ctx context.Context,
   659  	filter sqlplugin.BufferedEventsFilter,
   660  ) (sql.Result, error) {
   661  	return pdb.conn.ExecContext(ctx,
   662  		deleteBufferedEventsQuery,
   663  		filter.ShardID,
   664  		filter.NamespaceID,
   665  		filter.WorkflowID,
   666  		filter.RunID,
   667  	)
   668  }
   669  
   670  // InsertIntoReplicationTasks inserts one or more rows into replication_tasks table
   671  func (pdb *db) InsertIntoReplicationTasks(
   672  	ctx context.Context,
   673  	rows []sqlplugin.ReplicationTasksRow,
   674  ) (sql.Result, error) {
   675  	return pdb.conn.NamedExecContext(ctx,
   676  		createReplicationTasksQuery,
   677  		rows,
   678  	)
   679  }
   680  
   681  // RangeSelectFromReplicationTasks reads one or more rows from replication_tasks table
   682  func (pdb *db) RangeSelectFromReplicationTasks(
   683  	ctx context.Context,
   684  	filter sqlplugin.ReplicationTasksRangeFilter,
   685  ) ([]sqlplugin.ReplicationTasksRow, error) {
   686  	var rows []sqlplugin.ReplicationTasksRow
   687  	err := pdb.conn.SelectContext(ctx,
   688  		&rows,
   689  		getReplicationTasksQuery,
   690  		filter.ShardID,
   691  		filter.InclusiveMinTaskID,
   692  		filter.ExclusiveMaxTaskID,
   693  		filter.PageSize,
   694  	)
   695  	return rows, err
   696  }
   697  
   698  // DeleteFromReplicationTasks deletes one rows from replication_tasks table
   699  func (pdb *db) DeleteFromReplicationTasks(
   700  	ctx context.Context,
   701  	filter sqlplugin.ReplicationTasksFilter,
   702  ) (sql.Result, error) {
   703  	return pdb.conn.ExecContext(ctx,
   704  		deleteReplicationTaskQuery,
   705  		filter.ShardID,
   706  		filter.TaskID,
   707  	)
   708  }
   709  
   710  // RangeDeleteFromReplicationTasks deletes multi rows from replication_tasks table
   711  func (pdb *db) RangeDeleteFromReplicationTasks(
   712  	ctx context.Context,
   713  	filter sqlplugin.ReplicationTasksRangeFilter,
   714  ) (sql.Result, error) {
   715  	return pdb.conn.ExecContext(ctx,
   716  		rangeDeleteReplicationTaskQuery,
   717  		filter.ShardID,
   718  		filter.InclusiveMinTaskID,
   719  		filter.ExclusiveMaxTaskID,
   720  	)
   721  }
   722  
   723  // InsertIntoReplicationDLQTasks inserts one or more rows into replication_tasks_dlq table
   724  func (pdb *db) InsertIntoReplicationDLQTasks(
   725  	ctx context.Context,
   726  	rows []sqlplugin.ReplicationDLQTasksRow,
   727  ) (sql.Result, error) {
   728  	return pdb.conn.NamedExecContext(ctx,
   729  		insertReplicationTaskDLQQuery,
   730  		rows,
   731  	)
   732  }
   733  
   734  // RangeSelectFromReplicationDLQTasks reads one or more rows from replication_tasks_dlq table
   735  func (pdb *db) RangeSelectFromReplicationDLQTasks(
   736  	ctx context.Context,
   737  	filter sqlplugin.ReplicationDLQTasksRangeFilter,
   738  ) ([]sqlplugin.ReplicationDLQTasksRow, error) {
   739  	var rows []sqlplugin.ReplicationDLQTasksRow
   740  	err := pdb.conn.SelectContext(ctx,
   741  		&rows, getReplicationTasksDLQQuery,
   742  		filter.SourceClusterName,
   743  		filter.ShardID,
   744  		filter.InclusiveMinTaskID,
   745  		filter.ExclusiveMaxTaskID,
   746  		filter.PageSize,
   747  	)
   748  	return rows, err
   749  }
   750  
   751  // DeleteFromReplicationDLQTasks deletes one row from replication_tasks_dlq table
   752  func (pdb *db) DeleteFromReplicationDLQTasks(
   753  	ctx context.Context,
   754  	filter sqlplugin.ReplicationDLQTasksFilter,
   755  ) (sql.Result, error) {
   756  
   757  	return pdb.conn.ExecContext(ctx,
   758  		deleteReplicationTaskFromDLQQuery,
   759  		filter.SourceClusterName,
   760  		filter.ShardID,
   761  		filter.TaskID,
   762  	)
   763  }
   764  
   765  // RangeDeleteFromReplicationDLQTasks deletes one or more rows from replication_tasks_dlq table
   766  func (pdb *db) RangeDeleteFromReplicationDLQTasks(
   767  	ctx context.Context,
   768  	filter sqlplugin.ReplicationDLQTasksRangeFilter,
   769  ) (sql.Result, error) {
   770  
   771  	return pdb.conn.ExecContext(ctx,
   772  		rangeDeleteReplicationTaskFromDLQQuery,
   773  		filter.SourceClusterName,
   774  		filter.ShardID,
   775  		filter.InclusiveMinTaskID,
   776  		filter.ExclusiveMaxTaskID,
   777  	)
   778  }
   779  
   780  // InsertIntoVisibilityTasks inserts one or more rows into visibility_tasks table
   781  func (pdb *db) InsertIntoVisibilityTasks(
   782  	ctx context.Context,
   783  	rows []sqlplugin.VisibilityTasksRow,
   784  ) (sql.Result, error) {
   785  	return pdb.conn.NamedExecContext(ctx,
   786  		createVisibilityTasksQuery,
   787  		rows,
   788  	)
   789  }
   790  
   791  // RangeSelectFromVisibilityTasks reads one or more rows from visibility_tasks table
   792  func (pdb *db) RangeSelectFromVisibilityTasks(
   793  	ctx context.Context,
   794  	filter sqlplugin.VisibilityTasksRangeFilter,
   795  ) ([]sqlplugin.VisibilityTasksRow, error) {
   796  	var rows []sqlplugin.VisibilityTasksRow
   797  	err := pdb.conn.SelectContext(ctx,
   798  		&rows,
   799  		getVisibilityTasksQuery,
   800  		filter.ShardID,
   801  		filter.InclusiveMinTaskID,
   802  		filter.ExclusiveMaxTaskID,
   803  		filter.PageSize,
   804  	)
   805  	if err != nil {
   806  		return nil, err
   807  	}
   808  	return rows, nil
   809  }
   810  
   811  // DeleteFromVisibilityTasks deletes one or more rows from visibility_tasks table
   812  func (pdb *db) DeleteFromVisibilityTasks(
   813  	ctx context.Context,
   814  	filter sqlplugin.VisibilityTasksFilter,
   815  ) (sql.Result, error) {
   816  	return pdb.conn.ExecContext(ctx,
   817  		deleteVisibilityTaskQuery,
   818  		filter.ShardID,
   819  		filter.TaskID,
   820  	)
   821  }
   822  
   823  // RangeDeleteFromVisibilityTasks deletes one or more rows from visibility_tasks table
   824  func (pdb *db) RangeDeleteFromVisibilityTasks(
   825  	ctx context.Context,
   826  	filter sqlplugin.VisibilityTasksRangeFilter,
   827  ) (sql.Result, error) {
   828  	return pdb.conn.ExecContext(ctx,
   829  		rangeDeleteVisibilityTaskQuery,
   830  		filter.ShardID,
   831  		filter.InclusiveMinTaskID,
   832  		filter.ExclusiveMaxTaskID,
   833  	)
   834  }