Skip to content

Commit 0748a57

Browse files
Making sudo requirement optional
1 parent a091d5c commit 0748a57

File tree

2 files changed

+61
-43
lines changed

2 files changed

+61
-43
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,20 @@ When running as `nohup`, previous error can be received if `ssh` session is clos
113113
Defaults:root !requiretty
114114
```
115115

116+
Alternatively the script can be used without requiring `sudo` by changing PSQL Commands...
117+
118+
```bash
119+
# PSQL COMMAND
120+
psql="psql"
121+
pg_dump="pg_dump"
122+
```
123+
124+
... and invoking it via `su`.
125+
126+
```bash
127+
$ su postgres -c './pg_partitioner.sh -db alfresco count-nodes'
128+
```
129+
116130
Todo list
117131
----------------------
118132

pg_partitioner.sh

Lines changed: 47 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ set -o pipefail
66
set -o nounset
77
# set -o xtrace
88

9+
# PSQL COMMAND
10+
psql="sudo -u postgres psql"
11+
pg_dump="sudo -u postgres pg_dump"
12+
913
# THE DEFAULTS INITIALIZATION
1014
_arg_database_name=
1115
_arg_nodes_per_partition=
@@ -18,7 +22,7 @@ MAX_BLOCK_INSERT=1000000
1822

1923
### FUNCTIONS
2024
function existsDatabase {
21-
if sudo -u postgres psql -lqt | cut -d \| -f 1 | grep -qw $_arg_database_name; then
25+
if $psql -lqt | cut -d \| -f 1 | grep -qw $_arg_database_name; then
2226
echo "Database $_arg_database_name exists in local PostgreSQL"
2327
else
2428
echo "ERROR: database $_arg_database_name does NOT exist in local PostgreSQL!"
@@ -32,37 +36,37 @@ function initNumberOfNodes {
3236
}
3337

3438
function numberOfNodes {
35-
NONODES=`sudo -u postgres psql -d $_arg_database_name -t -c "select max(node_id) from alf_node_properties"`
39+
NONODES=`$psql -d $_arg_database_name -t -c "select max(node_id) from alf_node_properties"`
3640
echo "$NONODES";
3741
}
3842

3943
function dumpDB {
4044
echo "Dumping DB..."
4145
mkdir -p $_arg_dump_directory
42-
sudo -u postgres pg_dump $_arg_database_name > $_arg_dump_directory/$_arg_database_name.dump
46+
$pg_dump $_arg_database_name > $_arg_dump_directory/$_arg_database_name.dump
4347
echo "Dumping DB done!"
4448
}
4549

4650
function restoreDB {
4751
echo "Restoring DB..."
48-
sudo -u postgres psql -t -c "drop database $_arg_database_name;"
49-
sudo -u postgres psql -t -c "create database $_arg_database_name with encoding 'utf8';"
50-
sudo -u postgres psql $_arg_database_name < $_arg_restore_file
52+
$psql -t -c "drop database $_arg_database_name;"
53+
$psql -t -c "create database $_arg_database_name with encoding 'utf8';"
54+
$psql $_arg_database_name < $_arg_restore_file
5155
echo "Restoring DB done!"
5256
}
5357

5458
function createMasterTable {
5559

5660
echo "Creating Master Table ..."
5761

58-
sudo -u postgres psql -d $_arg_database_name -t -c "CREATE TABLE alf_node_properties_intermediate (LIKE alf_node_properties INCLUDING ALL);"
59-
sudo -u postgres psql -d $_arg_database_name -t -c "CREATE FUNCTION alf_node_properties_insert_trigger()
62+
$psql -d $_arg_database_name -t -c "CREATE TABLE alf_node_properties_intermediate (LIKE alf_node_properties INCLUDING ALL);"
63+
$psql -d $_arg_database_name -t -c "CREATE FUNCTION alf_node_properties_insert_trigger()
6064
RETURNS trigger AS \$\$
6165
BEGIN
6266
RAISE EXCEPTION 'Create partitions first.';
6367
END;
6468
\$\$ LANGUAGE plpgsql;"
65-
sudo -u postgres psql -d $_arg_database_name -t -c "CREATE TRIGGER alf_node_properties_insert_trigger
69+
$psql -d $_arg_database_name -t -c "CREATE TRIGGER alf_node_properties_insert_trigger
6670
BEFORE INSERT ON alf_node_properties_intermediate
6771
FOR EACH ROW EXECUTE PROCEDURE alf_node_properties_insert_trigger();"
6872

@@ -84,18 +88,18 @@ function createPartitions {
8488
MIN_LEVEL=$((($i - 1) * $_arg_nodes_per_partition))
8589
MAX_LEVEL=$(($MIN_LEVEL + $_arg_nodes_per_partition))
8690

87-
sudo -u postgres psql -d $_arg_database_name -t -c "CREATE TABLE alf_node_properties_$PART_NAME
91+
$psql -d $_arg_database_name -t -c "CREATE TABLE alf_node_properties_$PART_NAME
8892
(CHECK (node_id > $MIN_LEVEL AND node_id <= $MAX_LEVEL))
8993
INHERITS (alf_node_properties_intermediate);"
90-
sudo -u postgres psql -d $_arg_database_name -t -c "ALTER TABLE alf_node_properties_$PART_NAME ADD PRIMARY KEY (node_id, qname_id, list_index, locale_id);"
91-
sudo -u postgres psql -d $_arg_database_name -t -c "CREATE INDEX fk_alf_nprop_n_$PART_NAME ON alf_node_properties_$PART_NAME (node_id);"
92-
sudo -u postgres psql -d $_arg_database_name -t -c "CREATE INDEX fk_alf_nprop_qn_$PART_NAME ON alf_node_properties_$PART_NAME (qname_id);"
93-
sudo -u postgres psql -d $_arg_database_name -t -c "CREATE INDEX fk_alf_nprop_loc_$PART_NAME ON alf_node_properties_$PART_NAME (locale_id);"
94-
sudo -u postgres psql -d $_arg_database_name -t -c "CREATE INDEX idx_alf_nprop_s_$PART_NAME ON alf_node_properties_$PART_NAME (qname_id, string_value, node_id);"
95-
sudo -u postgres psql -d $_arg_database_name -t -c "CREATE INDEX idx_alf_nprop_l_$PART_NAME ON alf_node_properties_$PART_NAME (qname_id, long_value, node_id);"
96-
sudo -u postgres psql -d $_arg_database_name -t -c "CREATE INDEX idx_alf_nprop_b_$PART_NAME ON alf_node_properties_$PART_NAME (qname_id, boolean_value, node_id);"
97-
sudo -u postgres psql -d $_arg_database_name -t -c "CREATE INDEX idx_alf_nprop_f_$PART_NAME ON alf_node_properties_$PART_NAME (qname_id, float_value, node_id);"
98-
sudo -u postgres psql -d $_arg_database_name -t -c "CREATE INDEX idx_alf_nprop_d_$PART_NAME ON alf_node_properties_$PART_NAME (qname_id, double_value, node_id);"
94+
$psql -d $_arg_database_name -t -c "ALTER TABLE alf_node_properties_$PART_NAME ADD PRIMARY KEY (node_id, qname_id, list_index, locale_id);"
95+
$psql -d $_arg_database_name -t -c "CREATE INDEX fk_alf_nprop_n_$PART_NAME ON alf_node_properties_$PART_NAME (node_id);"
96+
$psql -d $_arg_database_name -t -c "CREATE INDEX fk_alf_nprop_qn_$PART_NAME ON alf_node_properties_$PART_NAME (qname_id);"
97+
$psql -d $_arg_database_name -t -c "CREATE INDEX fk_alf_nprop_loc_$PART_NAME ON alf_node_properties_$PART_NAME (locale_id);"
98+
$psql -d $_arg_database_name -t -c "CREATE INDEX idx_alf_nprop_s_$PART_NAME ON alf_node_properties_$PART_NAME (qname_id, string_value, node_id);"
99+
$psql -d $_arg_database_name -t -c "CREATE INDEX idx_alf_nprop_l_$PART_NAME ON alf_node_properties_$PART_NAME (qname_id, long_value, node_id);"
100+
$psql -d $_arg_database_name -t -c "CREATE INDEX idx_alf_nprop_b_$PART_NAME ON alf_node_properties_$PART_NAME (qname_id, boolean_value, node_id);"
101+
$psql -d $_arg_database_name -t -c "CREATE INDEX idx_alf_nprop_f_$PART_NAME ON alf_node_properties_$PART_NAME (qname_id, float_value, node_id);"
102+
$psql -d $_arg_database_name -t -c "CREATE INDEX idx_alf_nprop_d_$PART_NAME ON alf_node_properties_$PART_NAME (qname_id, double_value, node_id);"
99103

100104
echo "Partition alf_node_properties_$PART_NAME created"
101105

@@ -125,28 +129,28 @@ function addPartition {
125129
PART_NAME=$PARTITIONS
126130

127131
# Check if partition exists
128-
OUTPUT=$(sudo -u postgres psql -d $_arg_database_name -t -c "SELECT EXISTS (
132+
OUTPUT=$($psql -d $_arg_database_name -t -c "SELECT EXISTS (
129133
SELECT 1
130134
FROM information_schema.tables
131135
WHERE table_name = 'alf_node_properties_$PART_NAME'
132136
);")
133137

134138
if [[ "$OUTPUT" == " f" ]]; then
135139

136-
sudo -u postgres psql -d $_arg_database_name -t -c "CREATE TABLE alf_node_properties_$PART_NAME
140+
$psql -d $_arg_database_name -t -c "CREATE TABLE alf_node_properties_$PART_NAME
137141
(CHECK (node_id > $MIN_LEVEL AND node_id <= $MAX_LEVEL))
138142
INHERITS (alf_node_properties);"
139-
sudo -u postgres psql -d $_arg_database_name -t -c "ALTER TABLE alf_node_properties_$PART_NAME ADD PRIMARY KEY (node_id, qname_id, list_index, locale_id);"
140-
sudo -u postgres psql -d $_arg_database_name -t -c "CREATE INDEX fk_alf_nprop_n_$PART_NAME ON alf_node_properties_$PART_NAME (node_id);"
141-
sudo -u postgres psql -d $_arg_database_name -t -c "CREATE INDEX fk_alf_nprop_qn_$PART_NAME ON alf_node_properties_$PART_NAME (qname_id);"
142-
sudo -u postgres psql -d $_arg_database_name -t -c "CREATE INDEX fk_alf_nprop_loc_$PART_NAME ON alf_node_properties_$PART_NAME (locale_id);"
143-
sudo -u postgres psql -d $_arg_database_name -t -c "CREATE INDEX idx_alf_nprop_s_$PART_NAME ON alf_node_properties_$PART_NAME (qname_id, string_value, node_id);"
144-
sudo -u postgres psql -d $_arg_database_name -t -c "CREATE INDEX idx_alf_nprop_l_$PART_NAME ON alf_node_properties_$PART_NAME (qname_id, long_value, node_id);"
145-
sudo -u postgres psql -d $_arg_database_name -t -c "CREATE INDEX idx_alf_nprop_b_$PART_NAME ON alf_node_properties_$PART_NAME (qname_id, boolean_value, node_id);"
146-
sudo -u postgres psql -d $_arg_database_name -t -c "CREATE INDEX idx_alf_nprop_f_$PART_NAME ON alf_node_properties_$PART_NAME (qname_id, float_value, node_id);"
147-
sudo -u postgres psql -d $_arg_database_name -t -c "CREATE INDEX idx_alf_nprop_d_$PART_NAME ON alf_node_properties_$PART_NAME (qname_id, double_value, node_id);"
148-
149-
sudo -u postgres psql -d $_arg_database_name -t -c "GRANT ALL PRIVILEGES ON TABLE alf_node_properties_$PART_NAME TO alfresco"
143+
$psql -d $_arg_database_name -t -c "ALTER TABLE alf_node_properties_$PART_NAME ADD PRIMARY KEY (node_id, qname_id, list_index, locale_id);"
144+
$psql -d $_arg_database_name -t -c "CREATE INDEX fk_alf_nprop_n_$PART_NAME ON alf_node_properties_$PART_NAME (node_id);"
145+
$psql -d $_arg_database_name -t -c "CREATE INDEX fk_alf_nprop_qn_$PART_NAME ON alf_node_properties_$PART_NAME (qname_id);"
146+
$psql -d $_arg_database_name -t -c "CREATE INDEX fk_alf_nprop_loc_$PART_NAME ON alf_node_properties_$PART_NAME (locale_id);"
147+
$psql -d $_arg_database_name -t -c "CREATE INDEX idx_alf_nprop_s_$PART_NAME ON alf_node_properties_$PART_NAME (qname_id, string_value, node_id);"
148+
$psql -d $_arg_database_name -t -c "CREATE INDEX idx_alf_nprop_l_$PART_NAME ON alf_node_properties_$PART_NAME (qname_id, long_value, node_id);"
149+
$psql -d $_arg_database_name -t -c "CREATE INDEX idx_alf_nprop_b_$PART_NAME ON alf_node_properties_$PART_NAME (qname_id, boolean_value, node_id);"
150+
$psql -d $_arg_database_name -t -c "CREATE INDEX idx_alf_nprop_f_$PART_NAME ON alf_node_properties_$PART_NAME (qname_id, float_value, node_id);"
151+
$psql -d $_arg_database_name -t -c "CREATE INDEX idx_alf_nprop_d_$PART_NAME ON alf_node_properties_$PART_NAME (qname_id, double_value, node_id);"
152+
153+
$psql -d $_arg_database_name -t -c "GRANT ALL PRIVILEGES ON TABLE alf_node_properties_$PART_NAME TO alfresco"
150154

151155
echo "Partition alf_node_properties_$PART_NAME created"
152156

@@ -189,7 +193,7 @@ function triggerInsertRows {
189193
fi
190194
done
191195

192-
sudo -u postgres psql -d $_arg_database_name -t -c "CREATE OR REPLACE FUNCTION alf_node_properties_insert_trigger()
196+
$psql -d $_arg_database_name -t -c "CREATE OR REPLACE FUNCTION alf_node_properties_insert_trigger()
193197
RETURNS trigger AS
194198
\$\$
195199
BEGIN
@@ -208,7 +212,7 @@ function triggerInsertRows {
208212

209213
function insertInto {
210214

211-
sudo -u postgres psql -d $_arg_database_name -t -c "INSERT INTO alf_node_properties_intermediate (
215+
$psql -d $_arg_database_name -t -c "INSERT INTO alf_node_properties_intermediate (
212216
node_id,
213217
actual_type_n,
214218
persisted_type_n,
@@ -285,9 +289,9 @@ function analyze {
285289
for i in `seq 1 $PARTITIONS`;
286290
do
287291
PART_NAME=$i
288-
sudo -u postgres psql -d $_arg_database_name -t -c "ANALYZE VERBOSE alf_node_properties_$PART_NAME"
292+
$psql -d $_arg_database_name -t -c "ANALYZE VERBOSE alf_node_properties_$PART_NAME"
289293
done
290-
sudo -u postgres psql -d $_arg_database_name -t -c "ANALYZE VERBOSE alf_node_properties_intermediate"
294+
$psql -d $_arg_database_name -t -c "ANALYZE VERBOSE alf_node_properties_intermediate"
291295

292296
echo "Analyzing tables done!"
293297
}
@@ -298,14 +302,14 @@ function swap {
298302

299303
echo "Swapping tables..."
300304

301-
sudo -u postgres psql -d $_arg_database_name -t -c "ALTER TABLE alf_node_properties RENAME TO alf_node_properties_retired"
302-
sudo -u postgres psql -d $_arg_database_name -t -c "ALTER TABLE alf_node_properties_intermediate RENAME TO alf_node_properties"
303-
sudo -u postgres psql -d $_arg_database_name -t -c "DROP TABLE alf_node_properties_retired"
304-
sudo -u postgres psql -d $_arg_database_name -t -c "GRANT ALL PRIVILEGES ON TABLE alf_node_properties TO alfresco"
305+
$psql -d $_arg_database_name -t -c "ALTER TABLE alf_node_properties RENAME TO alf_node_properties_retired"
306+
$psql -d $_arg_database_name -t -c "ALTER TABLE alf_node_properties_intermediate RENAME TO alf_node_properties"
307+
$psql -d $_arg_database_name -t -c "DROP TABLE alf_node_properties_retired"
308+
$psql -d $_arg_database_name -t -c "GRANT ALL PRIVILEGES ON TABLE alf_node_properties TO alfresco"
305309
for i in `seq 1 $PARTITIONS`;
306310
do
307311
PART_NAME=$i
308-
sudo -u postgres psql -d $_arg_database_name -t -c "GRANT ALL PRIVILEGES ON TABLE alf_node_properties_$PART_NAME TO alfresco"
312+
$psql -d $_arg_database_name -t -c "GRANT ALL PRIVILEGES ON TABLE alf_node_properties_$PART_NAME TO alfresco"
309313
done
310314

311315
echo "Swapping tables done!"
@@ -321,9 +325,9 @@ function vacuum {
321325
for i in `seq 1 $PARTITIONS`;
322326
do
323327
PART_NAME=$i
324-
sudo -u postgres psql -d $_arg_database_name -t -c "VACUUM VERBOSE alf_node_properties_$PART_NAME"
328+
$psql -d $_arg_database_name -t -c "VACUUM VERBOSE alf_node_properties_$PART_NAME"
325329
done
326-
sudo -u postgres psql -d $_arg_database_name -t -c "VACUUM VERBOSE alf_node_properties_intermediate"
330+
$psql -d $_arg_database_name -t -c "VACUUM VERBOSE alf_node_properties_intermediate"
327331

328332
echo "VACUUM FULL done!"
329333
}

0 commit comments

Comments
 (0)