MCPcopy Create free account
hub / github.com/CrunchyData/postgres-operator / Exec

Method Exec

internal/postgres/exec.go:23–42  ·  view source on GitHub ↗

Exec uses "psql" to execute sql. The sql statement(s) are passed via stdin and may contain psql variables that are assigned from the variables map. - https://www.postgresql.org/docs/current/app-psql.html#APP-PSQL-VARIABLES

(
	ctx context.Context, sql io.Reader, variables map[string]string,
)

Source from the content-addressed store, hash-verified

21// and may contain psql variables that are assigned from the variables map.
22// - https://www.postgresql.org/docs/current/app-psql.html#APP-PSQL-VARIABLES
23func (exec Executor) Exec(
24 ctx context.Context, sql io.Reader, variables map[string]string,
25) (string, string, error) {
26 // Convert variables into `psql` arguments.
27 args := make([]string, 0, len(variables))
28 for k, v := range variables {
29 args = append(args, "--set="+k+"="+v)
30 }
31
32 // The map iteration above is nondeterministic. Sort the arguments so that
33 // calls to exec are deterministic.
34 // - https://golang.org/ref/spec#For_range
35 sort.Strings(args)
36
37 // Execute `psql` without reading config files nor prompting for a password.
38 var stdout, stderr bytes.Buffer
39 err := exec(ctx, sql, &stdout, &stderr,
40 append([]string{"psql", "-Xw", "--file=-"}, args...)...)
41 return stdout.String(), stderr.String(), err
42}
43
44// ExecInAllDatabases uses "bash" and "psql" to execute sql in every database
45// that allows connections, including templates. The sql command(s) may contain

Callers 5

TestExecutorExecFunction · 0.45
WriteUsersInPostgreSQLFunction · 0.45

Calls 1

StringMethod · 0.45

Tested by 1

TestExecutorExecFunction · 0.36