@@ -6,6 +6,10 @@ set -o pipefail
6
6
set -o nounset
7
7
# set -o xtrace
8
8
9
+ # PSQL COMMAND
10
+ psql=" sudo -u postgres psql"
11
+ pg_dump=" sudo -u postgres pg_dump"
12
+
9
13
# THE DEFAULTS INITIALIZATION
10
14
_arg_database_name=
11
15
_arg_nodes_per_partition=
@@ -18,7 +22,7 @@ MAX_BLOCK_INSERT=1000000
18
22
19
23
# ## FUNCTIONS
20
24
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
22
26
echo " Database $_arg_database_name exists in local PostgreSQL"
23
27
else
24
28
echo " ERROR: database $_arg_database_name does NOT exist in local PostgreSQL!"
@@ -32,37 +36,37 @@ function initNumberOfNodes {
32
36
}
33
37
34
38
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" `
36
40
echo " $NONODES " ;
37
41
}
38
42
39
43
function dumpDB {
40
44
echo " Dumping DB..."
41
45
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
43
47
echo " Dumping DB done!"
44
48
}
45
49
46
50
function restoreDB {
47
51
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
51
55
echo " Restoring DB done!"
52
56
}
53
57
54
58
function createMasterTable {
55
59
56
60
echo " Creating Master Table ..."
57
61
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()
60
64
RETURNS trigger AS \$\$
61
65
BEGIN
62
66
RAISE EXCEPTION 'Create partitions first.';
63
67
END;
64
68
\$\$ 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
66
70
BEFORE INSERT ON alf_node_properties_intermediate
67
71
FOR EACH ROW EXECUTE PROCEDURE alf_node_properties_insert_trigger();"
68
72
@@ -84,18 +88,18 @@ function createPartitions {
84
88
MIN_LEVEL=$(( ($i - 1 ) * $_arg_nodes_per_partition ))
85
89
MAX_LEVEL=$(( $MIN_LEVEL + $_arg_nodes_per_partition ))
86
90
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
88
92
(CHECK (node_id > $MIN_LEVEL AND node_id <= $MAX_LEVEL ))
89
93
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);"
99
103
100
104
echo " Partition alf_node_properties_$PART_NAME created"
101
105
@@ -125,28 +129,28 @@ function addPartition {
125
129
PART_NAME=$PARTITIONS
126
130
127
131
# 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 (
129
133
SELECT 1
130
134
FROM information_schema.tables
131
135
WHERE table_name = 'alf_node_properties_$PART_NAME '
132
136
);" )
133
137
134
138
if [[ " $OUTPUT " == " f" ]]; then
135
139
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
137
141
(CHECK (node_id > $MIN_LEVEL AND node_id <= $MAX_LEVEL ))
138
142
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"
150
154
151
155
echo " Partition alf_node_properties_$PART_NAME created"
152
156
@@ -189,7 +193,7 @@ function triggerInsertRows {
189
193
fi
190
194
done
191
195
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()
193
197
RETURNS trigger AS
194
198
\$\$
195
199
BEGIN
@@ -208,7 +212,7 @@ function triggerInsertRows {
208
212
209
213
function insertInto {
210
214
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 (
212
216
node_id,
213
217
actual_type_n,
214
218
persisted_type_n,
@@ -285,9 +289,9 @@ function analyze {
285
289
for i in ` seq 1 $PARTITIONS ` ;
286
290
do
287
291
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 "
289
293
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"
291
295
292
296
echo " Analyzing tables done!"
293
297
}
@@ -298,14 +302,14 @@ function swap {
298
302
299
303
echo " Swapping tables..."
300
304
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"
305
309
for i in ` seq 1 $PARTITIONS ` ;
306
310
do
307
311
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"
309
313
done
310
314
311
315
echo " Swapping tables done!"
@@ -321,9 +325,9 @@ function vacuum {
321
325
for i in ` seq 1 $PARTITIONS ` ;
322
326
do
323
327
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 "
325
329
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"
327
331
328
332
echo " VACUUM FULL done!"
329
333
}
0 commit comments