func partitionedDml(projectId, instanceId, databaseId string) error {
	ctx := context.Background()
	db, err := sql.Open("spanner", fmt.Sprintf("projects/%s/instances/%s/databases/%s", projectId, instanceId, databaseId))
	if err != nil {
		return err
	}
	defer db.Close()

	// First insert a couple of test records that we will delete using Partitioned DML.
	conn, err := db.Conn(ctx)
	if err != nil {
		return fmt.Errorf("failed to get a connection: %v", err)
	}
	if err := conn.Raw(func(driverConn interface{}) error {
		_, err := driverConn.(spannerdriver.SpannerConn).Apply(ctx, []*spanner.Mutation{
			spanner.InsertOrUpdateMap("Singers", map[string]interface{}{"SingerId": 1, "Name": "Singer 1"}),
			spanner.InsertOrUpdateMap("Singers", map[string]interface{}{"SingerId": 2, "Name": "Singer 2"}),
			spanner.InsertOrUpdateMap("Singers", map[string]interface{}{"SingerId": 3, "Name": "Singer 3"}),
			spanner.InsertOrUpdateMap("Singers", map[string]interface{}{"SingerId": 4, "Name": "Singer 4"}),
		})
		return err
	}); err != nil {
		return fmt.Errorf("failed to insert test records: %v", err)
	}

	// Now delete all records in the Singers table using Partitioned DML.
	if _, err := conn.ExecContext(ctx, "SET AUTOCOMMIT_DML_MODE='PARTITIONED_NON_ATOMIC'"); err != nil {
		return fmt.Errorf("failed to change DML mode to Partitioned_Non_Atomic: %v", err)
	}
	res, err := conn.ExecContext(ctx, "DELETE FROM Singers WHERE TRUE")
	if err != nil {
		return fmt.Errorf("failed to execute DELETE statement: %v", err)
	}
	affected, err := res.RowsAffected()
	if err != nil {
		return fmt.Errorf("failed to get affected rows: %v", err)
	}

	// Partitioned DML returns the minimum number of records that were affected.
	fmt.Printf("Deleted %v records using Partitioned DML\n", affected)

	// Closing the connection will return it to the connection pool. The DML mode will automatically be reset to the
	// default TRANSACTIONAL mode when the connection is returned to the pool, so we do not need to change it back
	// manually.
	_ = conn.Close()

	return nil
}
// Example for executing a Partitioned DML transaction on a Google Cloud Spanner database.
// See https://cloud.google.com/spanner/docs/dml-partitioned for more information on Partitioned DML.
//
// Execute the sample with the command `go run main.go` from this directory.
Example for executing a Partitioned DML transaction on a Google Cloud Spanner database.

Execute the sample with the command `go run main.go` from this directory.
// Example for executing a Partitioned DML transaction on a Google Cloud Spanner database . // See https : // cloud . google . com / spanner / docs / dml - partitioned for more information on Partitioned DML . // // Execute the sample with the command ` go run main . go ` from this directory .
func partitionedDml(projectId, instanceId, databaseId string) error {
	ctx := context.Background()
	db, err := sql.Open("spanner", fmt.Sprintf("projects/%s/instances/%s/databases/%s", projectId, instanceId, databaseId))
	if err != nil {
		return err
	}
	defer db.Close()
	conn, err := db.Conn(ctx)
	if err != nil {
		return fmt.Errorf("failed to get a connection: %v", err)
	}
	if err := conn.Raw(func(driverConn interface{}) error {
		_, err := driverConn.(spannerdriver.SpannerConn).Apply(ctx, []*spanner.Mutation{
			spanner.InsertOrUpdateMap("Singers", map[string]interface{}{"SingerId": 1, "Name": "Singer 1"}),
			spanner.InsertOrUpdateMap("Singers", map[string]interface{}{"SingerId": 2, "Name": "Singer 2"}),
			spanner.InsertOrUpdateMap("Singers", map[string]interface{}{"SingerId": 3, "Name": "Singer 3"}),
			spanner.InsertOrUpdateMap("Singers", map[string]interface{}{"SingerId": 4, "Name": "Singer 4"}),
		})
		return err
	}); err != nil {
		return fmt.Errorf("failed to insert test records: %v", err)
	}
	if _, err := conn.ExecContext(ctx, "SET AUTOCOMMIT_DML_MODE='PARTITIONED_NON_ATOMIC'"); err != nil {
		return fmt.Errorf("failed to change DML mode to Partitioned_Non_Atomic: %v", err)
	}
	res, err := conn.ExecContext(ctx, "DELETE FROM Singers WHERE TRUE")
	if err != nil {
		return fmt.Errorf("failed to execute DELETE statement: %v", err)
	}
	affected, err := res.RowsAffected()
	if err != nil {
		return fmt.Errorf("failed to get affected rows: %v", err)
	}
	fmt.Printf("Deleted %v records using Partitioned DML\n", affected)
	_ = conn.Close()
	return nil
}
func partitionedDml ( projectId , instanceId , databaseId string ) error { ctx := context . Background ( ) db , err := sql . Open ( " " , fmt . Sprintf ( " " , projectId , instanceId , databaseId ) ) if err != nil { return err } defer db . Close ( ) conn , err := db . Conn ( ctx ) if err != nil { return fmt . Errorf ( " " , err ) } if err := conn . Raw ( func ( driverConn interface { } ) error { _ , err := driverConn . ( spannerdriver . SpannerConn ) . Apply ( ctx , [ ] * spanner . Mutation { spanner . InsertOrUpdateMap ( " " , map [ string ] interface { } { " " : 1 , " " : " " } ) , spanner . InsertOrUpdateMap ( " " , map [ string ] interface { } { " " : 2 , " " : " " } ) , spanner . InsertOrUpdateMap ( " " , map [ string ] interface { } { " " : 3 , " " : " " } ) , spanner . InsertOrUpdateMap ( " " , map [ string ] interface { } { " " : 4 , " " : " " } ) , } ) return err } ) ; err != nil { return fmt . Errorf ( " " , err ) } if _ , err := conn . ExecContext ( ctx , " " ) ; err != nil { return fmt . Errorf ( " " , err ) } res , err := conn . ExecContext ( ctx , " " ) if err != nil { return fmt . Errorf ( " " , err ) } affected , err := res . RowsAffected ( ) if err != nil { return fmt . Errorf ( " " , err ) } fmt . Printf ( " \n " , affected ) _ = conn . Close ( ) return nil }
Example for executing a Partitioned DML transaction on a Google Cloud Spanner database.
Example for executing a Partitioned DML transaction on a Google Cloud Spanner database .
// First insert a couple of test records that we will delete using Partitioned DML. // Now delete all records in the Singers table using Partitioned DML. // Partitioned DML returns the minimum number of records that were affected. // Closing the connection will return it to the connection pool. The DML mode will automatically be reset to the // default TRANSACTIONAL mode when the connection is returned to the pool, so we do not need to change it back // manually.