Skip to content

Commit fb10bbd

Browse files
authored
Do not check for enabled triggers / FKs for fallback workflow of PG if session_replication_role session variable is used (#2322)
Adding GRANT SQL for granting the SET on session_replication_role to source-db-user to utilize it in Fallback case for import data to source Added a check in GetEnabledTriggersAndFks(), to not fetch the triggers/Fks in case this session parameter is used.
1 parent 411843a commit fb10bbd

File tree

4 files changed

+36
-4
lines changed

4 files changed

+36
-4
lines changed

guardrails-scripts/yb-voyager-pg-grant-migration-permissions.sql

+9
Original file line numberDiff line numberDiff line change
@@ -228,5 +228,14 @@ GRANT pg_read_all_stats to :voyager_user;
228228
WHERE
229229
schema_name = ANY(string_to_array(:'schema_list', ','))
230230
\gexec
231+
232+
DO $$
233+
BEGIN
234+
IF (substring((SELECT setting FROM pg_catalog.pg_settings WHERE name = 'server_version'), '^[0-9]+')::int >= 15) THEN
235+
RAISE NOTICE 'Granting set on PARAMETER session_replication_role TO %;', current_setting('myvars.voyager_user');
236+
EXECUTE format('GRANT SET ON PARAMETER session_replication_role TO %I;', current_setting('myvars.voyager_user'));
237+
END IF;
238+
END $$;
239+
231240
\endif
232241
\endif

yb-voyager/src/tgtdb/postgres.go

+9-2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333
_ "github.com/jackc/pgx/v5/stdlib"
3434
"github.com/samber/lo"
3535
log "github.com/sirupsen/logrus"
36+
"golang.org/x/exp/slices"
3637

3738
"github.com/yugabyte/yb-voyager/yb-voyager/src/callhome"
3839
"github.com/yugabyte/yb-voyager/yb-voyager/src/constants"
@@ -115,6 +116,9 @@ func (pg *TargetPostgreSQL) Init() error {
115116
if err != nil {
116117
return err
117118
}
119+
if len(pg.tconf.SessionVars) == 0 {
120+
pg.tconf.SessionVars = getPGSessionInitScript(pg.tconf)
121+
}
118122
schemas := strings.Split(pg.tconf.Schema, ",")
119123
schemaList := strings.Join(schemas, "','") // a','b','c
120124
checkSchemaExistsQuery := fmt.Sprintf(
@@ -241,12 +245,11 @@ func (pg *TargetPostgreSQL) InitConnPool() error {
241245
pg.tconf.Parallelism = fetchDefaultParallelJobs(tconfs, PG_DEFAULT_PARALLELISM_FACTOR)
242246
log.Infof("Using %d parallel jobs by default. Use --parallel-jobs to specify a custom value", pg.tconf.Parallelism)
243247
}
244-
245248
params := &ConnectionParams{
246249
NumConnections: pg.tconf.Parallelism,
247250
NumMaxConnections: pg.tconf.Parallelism,
248251
ConnUriList: targetUriList,
249-
SessionInitScript: getYBSessionInitScript(pg.tconf),
252+
SessionInitScript: pg.tconf.SessionVars,
250253
// works fine as we check the support of any session variable before using it in the script.
251254
// So upsert and disable transaction will never be used for PG
252255
}
@@ -1054,6 +1057,10 @@ func (pg *TargetPostgreSQL) getSchemaList() []string {
10541057
}
10551058

10561059
func (pg *TargetPostgreSQL) GetEnabledTriggersAndFks() (enabledTriggers []string, enabledFks []string, err error) {
1060+
if slices.Contains(pg.tconf.SessionVars, SET_SESSION_REPLICATE_ROLE_TO_REPLICA) {
1061+
//Not check for any triggers / FKs in case this session parameter is used
1062+
return nil, nil, nil
1063+
}
10571064
querySchemaArray := pg.getSchemaList()
10581065
querySchemaList := strings.Join(querySchemaArray, ",")
10591066

yb-voyager/src/tgtdb/tconf.go

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ type TargetConf struct {
5757
Parallelism int `json:"parallelism"`
5858
EnableYBAdaptiveParallelism utils.BoolStr `json:"enable_adaptive_parallelism"`
5959
MaxParallelism int `json:"max_parallelism"` // in case adaptive parallelism is enabled.
60+
SessionVars []string `json:"session_vars"`
6061
}
6162

6263
func (t *TargetConf) Clone() *TargetConf {

yb-voyager/src/tgtdb/yugabytedb.go

+17-2
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,10 @@ func (yb *TargetYugabyteDB) Init() error {
119119
return err
120120
}
121121

122+
if len(yb.tconf.SessionVars) == 0 {
123+
yb.tconf.SessionVars = getYBSessionInitScript(yb.tconf)
124+
}
125+
122126
checkSchemaExistsQuery := fmt.Sprintf(
123127
"SELECT count(nspname) FROM pg_catalog.pg_namespace WHERE nspname = '%s';",
124128
yb.tconf.Schema)
@@ -241,7 +245,7 @@ func (yb *TargetYugabyteDB) InitConnPool() error {
241245
NumConnections: yb.tconf.Parallelism,
242246
NumMaxConnections: yb.tconf.MaxParallelism,
243247
ConnUriList: targetUriList,
244-
SessionInitScript: getYBSessionInitScript(yb.tconf),
248+
SessionInitScript: yb.tconf.SessionVars,
245249
}
246250
yb.connPool = NewConnectionPool(params)
247251
redactedParams := &ConnectionParams{}
@@ -996,6 +1000,17 @@ const (
9961000
ERROR_MSG_PERMISSION_DENIED = "permission denied"
9971001
)
9981002

1003+
func getPGSessionInitScript(tconf *TargetConf) []string {
1004+
var sessionVars []string
1005+
if checkSessionVariableSupport(tconf, SET_CLIENT_ENCODING_TO_UTF8) {
1006+
sessionVars = append(sessionVars, SET_CLIENT_ENCODING_TO_UTF8)
1007+
}
1008+
if checkSessionVariableSupport(tconf, SET_SESSION_REPLICATE_ROLE_TO_REPLICA) {
1009+
sessionVars = append(sessionVars, SET_SESSION_REPLICATE_ROLE_TO_REPLICA)
1010+
}
1011+
return sessionVars
1012+
}
1013+
9991014
func getYBSessionInitScript(tconf *TargetConf) []string {
10001015
var sessionVars []string
10011016
if checkSessionVariableSupport(tconf, SET_CLIENT_ENCODING_TO_UTF8) {
@@ -1067,7 +1082,7 @@ func checkSessionVariableSupport(tconf *TargetConf, sqlStmt string) bool {
10671082
if !utils.AskPrompt("Are you sure you want to proceed?") {
10681083
utils.ErrExit("Aborting import.")
10691084
}
1070-
return true
1085+
return false // support is not there even if the target user doesn't have privileges to set this parameter.
10711086
}
10721087
utils.ErrExit("error while executing sqlStatement: %q: %v", sqlStmt, err)
10731088
} else {

0 commit comments

Comments
 (0)