Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

PostgreSQL DBA (99)-Develop (generate random string)

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

In order to verify the related functions of TOAST, through lpad, rpad and other methods, because the repetition rate is too high to see the effect, it is necessary to generate random strings to fill the data.

A sample function for generating random strings

CREATE OR REPLACE FUNCTION sf_generate_randomstring (length int4) RETURNS textAS $$declare chars text []: ='{0force1, RETURNS textAS $$declare chars text []: ='{0force1, RETURNS textAS $$declare chars text []: ='{0Power1, 2, 4, 5, 5, 8, 9, A, B, C, D, 5, 8, 5, 5, 4, 5, 5, 4, 5, 8, 9, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 7, 7, 5, 7, 7, 8, 7, 7, 8, 7, 7, 7, 7, 8, Wre xrem y re z}' Ret text: =''; I integer: = 0; lenofarray int: = array_length (chars, 1); begin if length < 0 then raise exception 'invalid input parameter, please re-enter!' ; end if; for i in 1..length loop ret: = ret | | chars [1+random () * (lenofarray-1)]; end loop; return ret;end;$$ LANGUAGE 'plpgsql'

This function randomly generates a string composed of a-zA-Z0-9 characters whose length is determined by the input parameter length. Of course, you can also add other characters, such as Chinese characters, to the array chars.

The effect is as follows:

[local]: 5432 pg12@testdb=# select sf_generate_randomstring Sf_generate_randomstring -hmski6FmSCcRPcfmSkC3aaX7Ay3QlmTz0497pitHNjLcU8KdKVtO7ysmoqRnwM2Dc9VuPnOxGfUgTX6lIdplRciYETic4GrdRqD1 (1 row) Time: 2.022 ms [local]: 5432 pg12@testdb=# select length (sf_generate_randomstring (40000)) Length-40000 (1 row) Time: 174.529 ms [local]: 5432 pg12@testdb=#

references

How to quickly build a large amount of realistic Test data by PostgreSQL

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report