github.com/greenplum-db/gpbackup@v0.0.0-20240517212602-89daab1885b3/backup/snapshot.go (about)

     1  package backup
     2  
     3  /*
     4   * This file contains functions related to use of synchronized database snapshots.
     5   */
     6  
     7  import (
     8  	"fmt"
     9  
    10  	"github.com/greenplum-db/gp-common-go-libs/dbconn"
    11  	"github.com/greenplum-db/gp-common-go-libs/gplog"
    12  )
    13  
    14  const (
    15  	SNAPSHOT_GPDB_MIN_VERSION = "6.21.0"
    16  )
    17  
    18   // Export synchronized snapshot using connection 0 and return snapshotId value as string
    19   func GetSynchronizedSnapshot(connectionPool *dbconn.DBConn) (string, error) {
    20  	 var snapshotId string
    21  	 err := connectionPool.Get(&snapshotId, "SELECT pg_catalog.pg_export_snapshot()", 0)
    22  	 if err != nil {
    23  		 return "", err
    24  	 }
    25  	 gplog.Debug("Exported synchronized snapshot %s", snapshotId)
    26  	 return snapshotId, nil
    27   }
    28  
    29  // Set synchronized snapshot for connNum to snapshotId
    30  func SetSynchronizedSnapshot(connectionPool *dbconn.DBConn, connNum int,  snapshotId string) error {
    31  	if connectionPool.Tx[connNum] == nil {
    32  		err := connectionPool.Begin(connNum) // Begins transaction in repeatable read
    33  		if err != nil {
    34  			return err
    35  		}
    36  	}
    37  	_, err := connectionPool.Exec(fmt.Sprintf("SET TRANSACTION SNAPSHOT '%s'", snapshotId), connNum)
    38  	if err != nil {
    39  		return err
    40  	}
    41  	gplog.Debug("Worker %d: Setting synchronized snapshot to %s", connNum, snapshotId)
    42  	return nil
    43  }