我正在尝试编写一个自定义pgbench脚本来对我正在设置的新postgres集群进行基准测试。官方文档使它看起来很简单,但是如果定义的变量缺少尾随空格(或其他一些未记录的字符),则看起来并不总是被解释。这是我正在尝试运行的脚本(testlist_bska.sql):
\setrandom aid 100 50875000
BEGIN;
DROP TABLE IF EXISTS nppsmoke_tmp_:aid CASCADE;
CREATE UNLOGGED TABLE nppsmoke_tmp_:aid (testname text NOT NULL);
CREATE INDEX nppsmoke_tmp_idx_:aid ON nppsmoke_tmp_:aid (testname);
COPY nppsmoke_tmp_:aid (testname) FROM '/tmp/testlist_p3_c.csv' WITH (FORMAT csv, ESCAPE '~');
INSERT INTO nppsmoke_testlist (testname) SELECT a.testname FROM nppsmoke_tmp_:aid a LEFT JOIN nppsmoke_testlist b ON b.testname = a.testname WHERE b.testname IS NULL;
UPDATE nppsmoke_testlist set tstamp=now() WHERE testname IN (SELECT testname FROM nppsmoke_tmp_:aid );
DELETE FROM nppsmoke_testlist WHERE age(now(),tstamp) > INTERVAL '12 months';
DROP TABLE nppsmoke_tmp_:aid CASCADE;
COPY nppsmoke_testlist (id,testname) TO '/tmp/testlist_:aid' (DELIMITER ',');
\shell /bin/cp -f /tmp/scrub_9sBO.sql /tmp/scrub_csv.":aid"
\shell /bin/sed -i "s/,\"87692\",/,\":aid\",/g" /tmp/scrub_csv.":aid"
COPY nppsmoketests (last_update,current_status,testid,suiteid,regressioncl,testcl,os,arch,build_type,branch,gpu,subtest,osversion) FROM '/tmp/scrub_sql.:aid ' with CSV HEADER ESCAPE '~';
\shell /bin/rm -f /tmp/testlist_:aid /tmp/scrub_csv.":aid"
COMMIT;
但是,当我调用此脚本时,它失败并显示以下输出:
$ pgbench -U lfriedman -n -f testlist_bska.sql -t 1 nightly
NOTICE: table "nppsmoke_tmp_39362939" does not exist, skipping
Client 0 aborted in state 13: ERROR: could not open file "/tmp/scrub_csv.39362939 " for reading: No such file or directory
transaction type: Custom query
scaling factor: 1
query mode: simple
number of clients: 1
number of threads: 1
number of transactions per client: 1
number of transactions actually processed: 0/1
tps = 0.000000 (including connections establishing)
tps = 0.000000 (excluding connections establishing)
另一个明显的问题是我无法弄清楚如何让sed命令正确替换87692的:aid变量的当前值。
答案 0 :(得分:1)
我最终破解了一种解决方法,因为如果它们没有包含在空格中,我无法使变量在\ shell命令中可靠地工作。我所做的是创建一个shell脚本,该脚本通过\ shell命令调用,并将参数传递给它:
#!/bin/bash
PATH=${PATH}:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/pgsql-9.3/bin:/root/bin
NUM="$1"
OPT="$2"
if [[ ${OPT} == 'setup' ]] ; then
cp -f /tmp/scrub_9sBO.sql /tmp/scrub_csv.${NUM} && sed -i "s/87692/${NUM}/g" /tmp/scrub_csv.${NUM}
elif [[ ${OPT} == 'del' ]] ; then
rm -f /tmp/testlist_${NUM} /tmp/scrub_csv.${NUM}
fi
以下是pgbench脚本的新版本:
\setrandom aid 100 50875000
BEGIN;
DROP TABLE IF EXISTS nppsmoke_tmp_:aid CASCADE;
CREATE UNLOGGED TABLE nppsmoke_tmp_:aid (testname text NOT NULL);
CREATE INDEX nppsmoke_tmp_idx_:aid ON nppsmoke_tmp_:aid (testname);
COPY nppsmoke_tmp_:aid (testname) FROM '/tmp/testlist_p3_c.csv' WITH (FORMAT csv, ESCAPE '~');
INSERT INTO nppsmoke_testlist (testname) SELECT a.testname FROM nppsmoke_tmp_:aid a LEFT JOIN nppsmoke_testlist b ON b.testname = a.testname WHERE b.testname IS NULL;
UPDATE nppsmoke_testlist set tstamp=now() WHERE testname IN (SELECT testname FROM nppsmoke_tmp_:aid );
DELETE FROM nppsmoke_testlist WHERE age(now(),tstamp) > INTERVAL '12 months';
DROP TABLE nppsmoke_tmp_:aid CASCADE;
COPY nppsmoke_testlist (id,testname) TO '/tmp/testlist_:aid' (DELIMITER ',');
\shell /tmp/bm.sh :aid setup
COPY nppsmoketests (last_update,current_status,testid,suiteid,regressioncl,testcl,os,arch,build_type,branch,gpu,subtest,osversion) FROM '/tmp/scrub_csv.:aid' with CSV HEADER ESCAPE '~';
\shell /tmp/bm.sh :aid del
COMMIT;