postgresql測試常用方法
阿新 • • 發佈:2018-12-25
參考資料
針對 Postgres生成資料方法 <1>生成序列====》 SELECT * FROM generate_series(1,5); <2>生成date====》 SELECT date(generate_series(now(), now() + '1 week', '1 day')); <3>生成integer 隨機數=》 SELECT (random()*(2*10^9))::integer; <4>生成numeric 隨機數=》select (random()*100)::numeric(4,2); <5>生成字串==》 select substr('abcdefghijklmnopqrstuvwxyz',1,(random()*26)::integer); <6>生成重複串==> select repeat('1',(random()*40)::integer); 舉例: SELECT generate_series(1,10) as key,(random()*100.)::numeric(4,2),repeat('1',(random()*25)::integer) ORDER BY random(); key | numeric | repeat -----+---------+-------------------------- 8 | 26.04 | 111 10 | 83.44 | 1 9 | 46.72 | 3 | 57.84 | 1111111111111 4 | 29.61 | 1111111111111111111 5 | 11.32 | 1111111111111 7 | 69.69 | 2 | 42.23 | 11111111111111111 6 | 12.32 | 111111111111111111111111 1 | 84.92 | 111111 二、 如果您想知道執行該sql的時間,請在執行上述命令前設定: postgres=# \timing on Timing is on. (1)測試參考SQL,可以把生成的隨機值改的大一些; #生成新表===> select i,'text:'||i as text into test from generate_series(1,10) as i; #在新表中插入測試資料===> insert into test(i,text) select i,'text:'||i from generate_series(1,10) as i; (2)查看錶test佔用的儲存空間 若檢視其中的index的空間或整個relation的空間,請參考: http://www.postgresql.org/docs/9.1/static/functions-admin.html 或:http://www.postgresql.org/docs/9.1/static/functions-admin.html
--把時間戳轉成epoch值 postgres=# select extract(epoch from now()); date_part ------------------ 1447898857.74524 (1 row) --把epoch 值轉換回時間戳 postgres=# SELECT TIMESTAMP WITH TIME ZONE 'epoch' + 1447898857.74524 * INTERVAL '1 second'; ?column? ------------------------------ 2015-11-19 10:07:37.74524+08 postgres=# SELECT TIMESTAMP WITH TIME ZONE 'epoch' + 1447898857.74524 * INTERVAL '1 second'; ?column? ------------------------------ 2015-11-19 10:07:37.74524+08 --檢視當前的時間戳 postgres=# select clock_timestamp(),current_timestamp,localtimestamp; clock_timestamp | now | timestamp -------------------------------+-------------------------------+---------------------------- 2016-02-02 17:54:15.547194+08 | 2016-02-02 17:54:15.546956+08 | 2016-02-02 17:54:15.546956 --時間加減 postgres=# select date '2016-02-02 10:00:00'+ interval '10 minutes'; ?column? --------------------- 2016-02-02 00:10:00 --直接用sql生成隨機日期時間 select '2015-5-1'::date + trunc(random()*100)::integer +' 00:22:22'::time + (trunc(random()*3600*24)||' second')::interval; --不同時區之間的轉換 postgres=# select '2016-02-03 09:07:30.816885+08' at time zone 'pst'; timezone ---------------------------- 2016-02-02 17:07:30.816885 (1 row) postgres=# select '2016-02-03 09:07:30.816885+08' at time zone 'cct'; timezone ---------------------------- 2016-02-03 09:07:30.816885 (1 row) postgres=# SELECT TIMESTAMP WITH TIME ZONE '2001-02-16 20:38:40-05' AT TIME ZONE 'cct'; timezone --------------------- 2001-02-17 09:38:40 --檢視系統支援的時區 select * from pg_timezone_names ; --時區設定引數 timezone = 'PRC' --修改時區的方法 1. 全域性引數 postgresql.conf timezone='UTC' 2. 資料庫級配置 alter database dbname set timezone='UTC'; pipeline=# select * from pg_db_role_setting ; setdatabase | setrole | setconfig -------------+---------+-------------------------------------- 14930 | 0 | {TimeZone=UTC} 3. 使用者級配置 alter role rolname set timezone='UTC'; 或者 alter role all set timezone='UTC'; pipeline=# select * from pg_db_role_setting ; setdatabase | setrole | setconfig -------------+---------+-------------------------------------- 14930 | 0 | {TimeZone=UTC} 0 | 0 | {TimeZone=UTC} --建立隨機日期時間函式 CREATE OR REPLACE FUNCTION rand_date_time(start_date date, end_date date) RETURNS TIMESTAMP AS $BODY$ DECLARE interval_days integer; random_seconds integer; random_dates integer; random_date date; random_time time; BEGIN interval_days := end_date - start_date; random_dates:= trunc(random()*interval_days); random_date := start_date + random_dates; random_seconds:= trunc(random()*3600*24); random_time:=' 00:00:00'::time+(random_seconds || ' second')::INTERVAL; RETURN random_date +random_time; END; $BODY$ LANGUAGE plpgsql; --生成指定時間內的隨機時間 SELECT rand_date_time('2000-01-01', '2013-12-31'); --休眠1.5秒後執行,單位秒 SELECT clock_timestamp(),pg_sleep(1.5),clock_timestamp(); --休眠5分鐘,單位interval SELECT clock_timestamp(),pg_sleep_for('5 minutes'),clock_timestamp(); --到指定時間執行,注意這些休眠時間不是完全精確的 SELECT clock_timestamp(),pg_sleep_until('today 10:00'),clock_timestamp();