Skip to content

Files

Latest commit

Dec 15, 2023
f400df2 · Dec 15, 2023

History

History
220 lines (107 loc) · 5.24 KB

20180714_01.md

File metadata and controls

220 lines (107 loc) · 5.24 KB

PostgreSQL 随机数据生成(tablefunc.normal_rand 指定mean stddev)

作者

digoal

日期

2018-07-14

标签

PostgreSQL , tablefunc , normal_rand , mean , stddev


背景

生成测试数据,PostgreSQL有各种各样的方法,建本文参考文档。

本文提供一种生成指定任意个数、平均值、标准方差的数值。使用tablefunc插件的normal_rand即可。

F.38.1.1. normal_rand
 normal_rand(int numvals, float8 mean, float8 stddev) returns setof float8
normal_rand produces a set of normally distributed random values (Gaussian distribution).

numvals is the number of values to be returned from the function. mean is the mean of the normal distribution of values and stddev is the standard deviation of the normal distribution of values.

For example, this call requests 1000 values with a mean of 5 and a standard deviation of 3:

test=# SELECT * FROM normal_rand(1000, 5, 3);
     normal_rand
----------------------
     1.56556322244898
     9.10040991424657
     5.36957140345079
   -0.369151492880995
    0.283600703686639
       .
       .
       .
     4.82992125404908
     9.71308014517282
     2.49639286969028
(1000 rows)

例子

postgres=# create extension tablefunc;  
  
select normal_rand(10,10,0.1);  

例子

postgres=# select normal_rand(20,10,0);  
 normal_rand   
-------------  
          10  
          10  
          10  
          10  
          10  
          10  
          10  
          10  
          10  
          10  
          10  
          10  
          10  
          10  
          10  
          10  
          10  
          10  
          10  
          10  
(20 rows)  
  
postgres=# select normal_rand(20,10,1);  
   normal_rand      
------------------  
 9.72520225550088  
 10.0119482459389  
 10.5916890547498  
 10.1463735999097  
 8.92547759747822  
 11.6355890779347  
 10.9720784944947  
 10.1733686382872  
 10.5646035351755  
 10.1595366384555  
 10.2430859539833  
 10.1603680100806  
 8.76754663101294  
 8.07820227599032  
 8.37749903746811  
 13.7146194036438  
 10.7485346808754  
 10.0266158727721  
 10.5129017656189  
 11.0932448676642  
(20 rows)  

参考

《PostgreSQL 生成任意基数数独 - 4》

《PostgreSQL 生成任意基数数独 - 3》

《PostgreSQL 生成任意基数数独 - 2》

《PostgreSQL 生成任意基数数独 - 1》

《PostgreSQL 如何快速构建 海量 逼真 测试数据》

《生成泊松、高斯、指数、随机分布数据 - PostgreSQL 9.5 new feature - pgbench improve, gaussian (standard normal) & exponential distribution》

https://www.postgresql.org/docs/devel/static/tablefunc.html

HTAP 测试

您的愿望将传达给PG kernel hacker、数据库厂商等, 帮助提高数据库产品质量和功能, 说不定下一个PG版本就有您提出的功能点. 针对非常好的提议,奖励限量版PG文化衫、纪念品、贴纸、PG热门书籍等,奖品丰富,快来许愿。开不开森.

digoal's wechat