Script - connecting to other segments
search cancel

Script - connecting to other segments

book

Article ID: 296189

calendar_today

Updated On:

Products

VMware Tanzu Greenplum

Issue/Introduction

Before connecting to a segment or master in utility mode, you must get the configuration of the segment first.

This is not so efficient and convenient if you have many different segments to connect, especially when troubleshooting catalog issues.

The script discussed in this article aims to help simplify these operations. The script discussed in this article aims to help simplify these operations. You can put this script into a file (for example, seg_conn.sh):
#!/bin/sh

#variable definition
seg_no=$1
db_name=$2
file_name=$3
sql_check_conf="
\o /tmp/seg_conn.out;
select content, hostname, port from gp_segment_configuration where role = 'p';
"

#check parameters
[ $# -gt 3 ] && echo "Too many parameters." && exit 1;
([ x"${seg_no}" == x ] || [ x"${db_name}" == x ]) && echo "Usage: $0 <content_number> <dbname> [file_to_run]" && exit 1;

#generate tmp file that contains seg info
nohup psql postgres <<EOF
    ${sql_check_conf}
EOF

#concat CONNECT string
host_name=`cat /tmp/seg_conn.out | sed 's/[ ]*//g' | grep ^${seg_no}'|' | awk -F'|' '{print $2}'`
seg_port=`cat /tmp/seg_conn.out | sed 's/[ ]*//g' | grep ^${seg_no}'|' | awk -F'|' '{print $3}'`

#execute
if [ x"${file_name}" == x ]; then
PGOPTIONS='-c gp_session_role=utility' psql ${db_name} -h ${host_name} -p ${seg_port}
else
PGOPTIONS='-c gp_session_role=utility' psql ${db_name} -h ${host_name} -p ${seg_port} -f ${file_name}
fi


Environment


Cause

Before connecting to a segment or master in utility mode, you must get the configuration of the segment first.

There is no efficient and convenient way to do this if you have many different segments to connect, especially when troubleshooting catalog issues.

Resolution

The format of the command is: ./seg_conn.sh <seg_no> <db_name> [file_to run]

If you want to connect to gpperfmon on segment 10, run the following command:
./seg_conn.sh 10 gpperfmon
If you want to connect to gpperfmon on master, run the following command:
./seg_conn.sh -1 gpperfmon
If you want to connect to gpperfmon on segment 10 and meanwhile run /tmp/test.sql, run the following command:
./seg_conn.sh 10 gpperfmon /tmp/test.sql
You can also confirm if it connects to the right segment by checking the bottom two lines of the debug info from sh -x:
[gpadmin@dw-greenplum-1 ~]$ sh -x /tmp/seg_conn.sh 0 postgres
...
...
++ sed 's/[ ]*//g'
++ awk '-F|' '{print $3}'
++ grep '^0|'
+ seg_port=33000
+ '[' x == x ']'
+ PGOPTIONS='-c gp_session_role=utility'
+ psql postgres -h dw-greenplum-1 -p 33000