go - Bulk update Azure Flex Server PostgreSQL parameters with Golang SDK - Stack Overflow

I'm working with Azure Flex Server for PostgreSQ using the Go SDK.I'm looking for a way to s

I'm working with Azure Flex Server for PostgreSQ using the Go SDK.

I'm looking for a way to set multiple PostgreSQL server parameters in bulk or through a single operation, rather than setting them one by one.

Is there a way within the SDK that allows for this? Cannot seem to find a way to do this, and I am now changing them one by one.


What I have tried so far:

// Process updates sequentially
for _, knob := range proposedConfig.KnobsOverrides {
        knobConfig, err := parameters.FindRecommendedKnob(proposedConfig.Config, knob)
        fmtValue, _ := knobConfig.GetSettingValue()

        updatedConfig := armpostgresqlflexibleservers.Configuration{
            Properties: &armpostgresqlflexibleservers.ConfigurationProperties{
                Value:  &fmtValue,
                Source: to.Ptr("user-override"),
            },
        }

        adapter.Logger().Infof("Applying parameter update: %s", knob)

        poller, err := adapter.configurationsClient.BeginPut(
            ctx,
            adapter.azureConfig.ResourceGroupName,
            adapter.azureConfig.ServerName,
            knob,
            updatedConfig,
            nil,
        )

        if err != nil {
            return fmt.Errorf("failed to update configuration %s: %v", knob, err)
        }

        // Wait for completion with timeout
        updateCtx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)

        _, err = poller.PollUntilDone(updateCtx, nil)
        cancel()


        if err != nil {
            return fmt.Errorf("failed to complete configuration update for %s: %v", knob, err)
        }

    }

I'm working with Azure Flex Server for PostgreSQ using the Go SDK.

I'm looking for a way to set multiple PostgreSQL server parameters in bulk or through a single operation, rather than setting them one by one.

Is there a way within the SDK that allows for this? Cannot seem to find a way to do this, and I am now changing them one by one.


What I have tried so far:

// Process updates sequentially
for _, knob := range proposedConfig.KnobsOverrides {
        knobConfig, err := parameters.FindRecommendedKnob(proposedConfig.Config, knob)
        fmtValue, _ := knobConfig.GetSettingValue()

        updatedConfig := armpostgresqlflexibleservers.Configuration{
            Properties: &armpostgresqlflexibleservers.ConfigurationProperties{
                Value:  &fmtValue,
                Source: to.Ptr("user-override"),
            },
        }

        adapter.Logger().Infof("Applying parameter update: %s", knob)

        poller, err := adapter.configurationsClient.BeginPut(
            ctx,
            adapter.azureConfig.ResourceGroupName,
            adapter.azureConfig.ServerName,
            knob,
            updatedConfig,
            nil,
        )

        if err != nil {
            return fmt.Errorf("failed to update configuration %s: %v", knob, err)
        }

        // Wait for completion with timeout
        updateCtx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)

        _, err = poller.PollUntilDone(updateCtx, nil)
        cancel()


        if err != nil {
            return fmt.Errorf("failed to complete configuration update for %s: %v", knob, err)
        }

    }

Share Improve this question edited Mar 27 at 14:20 konsalex asked Mar 27 at 9:12 konsalexkonsalex 4787 silver badges20 bronze badges 2
  • Could you please share what you tried? – Balaji Commented Mar 27 at 9:34
  • Updated the post with what I have tried @Balaji – konsalex Commented Mar 27 at 14:21
Add a comment  | 

1 Answer 1

Reset to default -1

Steps to set up and execute a Go script that updates multiple PostgreSQL parameters in bulk (using Azure Cloud Shell to run Go programs):-

  1. Create a new project folder:
mkdir -p ~/go/src/azure-postgres-config
cd ~/go/src/azure-postgres-config
  1. Initialize GO module:
go mod init azure-postgres-config
  1. Install Azure SDK Packages:
go get github/Azure/azure-sdk-for-go/sdk/azidentity
go get github/Azure/azure-sdk-for-go/sdk/azcore/to
go get github/Azure/azure-sdk-for-go/sdk/resourcemanager/postgresql/armpostgresqlflexibleservers
  1. Create a new GO file:
nano main.go
  1. Write the following GO script: (Update subscriptionID, resourceGroup and serverName)
package main

import (
    "context"
    "fmt"
    "log"

    "github/Azure/azure-sdk-for-go/sdk/azidentity"
    "github/Azure/azure-sdk-for-go/sdk/azcore/to"
    "github/Azure/azure-sdk-for-go/sdk/resourcemanager/postgresql/armpostgresqlflexibleservers"
)

func main() {
    subscriptionID := "your-subscription-id" // **Replace this**
    resourceGroup := "your-resource-group"    // **Replace this**
    serverName := "your-postgres-server-name" // **Replace this**

    cred, err := azidentity.NewDefaultAzureCredential(nil)
    if err != nil {
        log.Fatalf("Failed to get Azure credential: %v", err)
    }

    client, err := armpostgresqlflexibleservers.NewConfigurationsClient(subscriptionID, cred, nil)
    if err != nil {
        log.Fatalf("Failed to create PostgreSQL configurations client: %v", err)
    }

    parameters := map[string]string{
        "log_min_duration_statement": "5000",
        "work_mem":                   "16384",
    }

    for param, value := range parameters {
        fmt.Printf("Updating parameter: %s to %s\n", param, value)

        config := armpostgresqlflexibleservers.Configuration{
            Properties: &armpostgresqlflexibleservers.ConfigurationProperties{
                Value:  to.Ptr(value),
                Source: to.Ptr("user-override"),
            },
        }

        poller, err := client.BeginPut(context.Background(), resourceGroup, serverName, param, config, nil)
        if err != nil {
            log.Fatalf("Failed to update %s: %v", param, err)
        }

        _, err = poller.PollUntilDone(context.Background(), nil)
        if err != nil {
            log.Fatalf("Failed to complete update for %s: %v", param, err)
        }

        fmt.Printf("Successfully updated %s\n", param)
    }
}

  1. Save and Exit the file

  2. Remove unused dependencies and ensure all modules are correctly installed:

go mod tidy
  1. Run the Script:
go run main.go

Go script is now able to update multiple PostgreSQL parameters in bulk using the Azure SDK.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744101407a4558550.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信