agora inbox for pgsql-zh-general@postgresql.org  
help / color / mirror / Atom feed
随机字符串函数的C语言实现
5+ messages / 3 participants
[nested] [flat]

* 随机字符串函数的C语言实现
@ 2017-09-05 08:28 Quan Zongliang <zongliang.quan@postgresdata.com>
  2017-09-05 09:01 `  Re: [pgsql-zh-general] 随机字符串函数的C语言实现 Jaimin Pan <jaimin.pan@gmail.com>
  0 siblings, 1 reply; 5+ messages in thread

From: Quan Zongliang @ 2017-09-05 08:28 UTC (permalink / raw)
  To: pgsql-zh-general

各位下午好
邮件列表沉寂太久,激活一下大家的情绪。

分享一个C语言实现的随机字符串函数:
PG_FUNCTION_INFO_V1(random_string);
Datum
random_string(PG_FUNCTION_ARGS)
{
	int32 length = PG_GETARG_INT32(0);
	int32 i;
	const char chars[] = 
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
	char *result;

	if (length < 0)
		PG_RETURN_NULL();

	if (length == 0)
		PG_RETURN_TEXT_P(cstring_to_text(""));

	result = palloc(length + 1);
	if (result == NULL)
		ereport(ERROR, (errcode(ERRCODE_OUT_OF_MEMORY),
					errmsg("out of memory")));

	for (i=0; i<length; i++)
	{
		result[i] = chars[random()%(sizeof(chars)-1)];
	}
	result[i] = '\0';

	PG_RETURN_TEXT_P(cstring_to_text(result));
}

原文见:
https://my.oschina.net/quanzl/blog/1529438

欢迎关注我司公众号:postgresdata-news

-- 
权宗亮
神州飞象(北京)数据科技有限公司
我们的力量源自最先进的开源数据库PostgreSQL
zongliang.quan@postgresdata.com



-- 
Sent via pgsql-zh-general mailing list (pgsql-zh-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-zh-general



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

*  Re: [pgsql-zh-general] 随机字符串函数的C语言实现
  2017-09-05 08:28 随机字符串函数的C语言实现 Quan Zongliang <zongliang.quan@postgresdata.com>
@ 2017-09-05 09:01 ` Jaimin Pan <jaimin.pan@gmail.com>
  2017-09-05 09:11   ` Re:  Re: [pgsql-zh-general] 随机字符串函数的C语言实现 winston cheung <winston_cheung@163.com>
  2017-09-05 10:26   ` Re: [pgsql-zh-general] 随机字符串函数的C语言实现 Quan Zongliang <zongliang.quan@postgresdata.com>
  0 siblings, 2 replies; 5+ messages in thread

From: Jaimin Pan @ 2017-09-05 09:01 UTC (permalink / raw)
  To: Quan Zongliang <zongliang.quan@postgresdata.com>; +Cc: pgsql-zh-general

试一试
md5(random()::text);


select pg_backend_pid(),s,'string '|| md5(random()::text) from (select
generate_series(2000,8000,1) s) as g;
Time: 239.733 ms

2017-09-05 16:28 GMT+08:00 Quan Zongliang <zongliang.quan@postgresdata.com>:

> 各位下午好
> 邮件列表沉寂太久,激活一下大家的情绪。
>
> 分享一个C语言实现的随机字符串函数:
> PG_FUNCTION_INFO_V1(random_string);
> Datum
> random_string(PG_FUNCTION_ARGS)
> {
>         int32 length = PG_GETARG_INT32(0);
>         int32 i;
>         const char chars[] = "0123456789ABCDEFGHIJKLMNOPQRS
> TUVWXYZabcdefghijklmnopqrstuvwxyz";
>         char *result;
>
>         if (length < 0)
>                 PG_RETURN_NULL();
>
>         if (length == 0)
>                 PG_RETURN_TEXT_P(cstring_to_text(""));
>
>         result = palloc(length + 1);
>         if (result == NULL)
>                 ereport(ERROR, (errcode(ERRCODE_OUT_OF_MEMORY),
>                                         errmsg("out of memory")));
>
>         for (i=0; i<length; i++)
>         {
>                 result[i] = chars[random()%(sizeof(chars)-1)];
>         }
>         result[i] = '\0';
>
>         PG_RETURN_TEXT_P(cstring_to_text(result));
> }
>
> 原文见:
> https://my.oschina.net/quanzl/blog/1529438
>
> 欢迎关注我司公众号:postgresdata-news
>
> --
> 权宗亮
> 神州飞象(北京)数据科技有限公司
> 我们的力量源自最先进的开源数据库PostgreSQL
> zongliang.quan@postgresdata.com
>
>
>
> --
> Sent via pgsql-zh-general mailing list (pgsql-zh-general@postgresql.org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-zh-general
>

^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re:  Re: [pgsql-zh-general] 随机字符串函数的C语言实现
  2017-09-05 08:28 随机字符串函数的C语言实现 Quan Zongliang <zongliang.quan@postgresdata.com>
  2017-09-05 09:01 `  Re: [pgsql-zh-general] 随机字符串函数的C语言实现 Jaimin Pan <jaimin.pan@gmail.com>
@ 2017-09-05 09:11   ` winston cheung <winston_cheung@163.com>
  2017-09-06 01:48     ` Re:  Re: [pgsql-zh-general] 随机字符串函数的C语言实现 Quan Zongliang <zongliang.quan@postgresdata.com>
  1 sibling, 1 reply; 5+ messages in thread

From: winston cheung @ 2017-09-05 09:11 UTC (permalink / raw)
  To: pgsql-zh-general

权叔有性能测试么?列一下数据啊,哈哈,学习一下。


On 09/05/2017 05:01 PM, Jaimin Pan wrote:
> 试一试
> md5(random()::text);
>
>
> select pg_backend_pid(),s,'string '|| md5(random()::text) from (select 
> generate_series(2000,8000,1) s) as g;
> Time: 239.733 ms
>
> 2017-09-05 16:28 GMT+08:00 Quan Zongliang 
> <zongliang.quan@postgresdata.com 
> <mailto:zongliang.quan@postgresdata.com>>:
>
>     各位下午好
>     邮件列表沉寂太久,激活一下大家的情绪。
>
>     分享一个C语言实现的随机字符串函数:
>     PG_FUNCTION_INFO_V1(random_string);
>     Datum
>     random_string(PG_FUNCTION_ARGS)
>     {
>             int32 length = PG_GETARG_INT32(0);
>             int32 i;
>             const char chars[] =
>     "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
>             char *result;
>
>             if (length < 0)
>                     PG_RETURN_NULL();
>
>             if (length == 0)
>                     PG_RETURN_TEXT_P(cstring_to_text(""));
>
>             result = palloc(length + 1);
>             if (result == NULL)
>                     ereport(ERROR, (errcode(ERRCODE_OUT_OF_MEMORY),
>                                             errmsg("out of memory")));
>
>             for (i=0; i<length; i++)
>             {
>                     result[i] = chars[random()%(sizeof(chars)-1)];
>             }
>             result[i] = '\0';
>
>             PG_RETURN_TEXT_P(cstring_to_text(result));
>     }
>
>     原文见:
>     https://my.oschina.net/quanzl/blog/1529438
>     <https://my.oschina.net/quanzl/blog/1529438;
>
>     欢迎关注我司公众号:postgresdata-news
>
>     -- 
>     权宗亮
>     神州飞象(北京)数据科技有限公司
>     我们的力量源自最先进的开源数据库PostgreSQL
>     zongliang.quan@postgresdata.com
>     <mailto:zongliang.quan@postgresdata.com>
>
>
>
>     -- 
>     Sent via pgsql-zh-general mailing list
>     (pgsql-zh-general@postgresql.org
>     <mailto:pgsql-zh-general@postgresql.org>)
>     To make changes to your subscription:
>     http://www.postgresql.org/mailpref/pgsql-zh-general
>     <http://www.postgresql.org/mailpref/pgsql-zh-general;
>
>

^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re:  Re: [pgsql-zh-general] 随机字符串函数的C语言实现
  2017-09-05 08:28 随机字符串函数的C语言实现 Quan Zongliang <zongliang.quan@postgresdata.com>
  2017-09-05 09:01 `  Re: [pgsql-zh-general] 随机字符串函数的C语言实现 Jaimin Pan <jaimin.pan@gmail.com>
  2017-09-05 09:11   ` Re:  Re: [pgsql-zh-general] 随机字符串函数的C语言实现 winston cheung <winston_cheung@163.com>
@ 2017-09-06 01:48     ` Quan Zongliang <zongliang.quan@postgresdata.com>
  0 siblings, 0 replies; 5+ messages in thread

From: Quan Zongliang @ 2017-09-06 01:48 UTC (permalink / raw)
  To: pgsql-zh-general


这篇博文有简单测试:
https://my.oschina.net/quanzl/blog/1529438
天壤之别的性能差距

On 2017/9/5 17:11, winston cheung wrote:
> 权叔有性能测试么?列一下数据啊,哈哈,学习一下。
> 
> 
> On 09/05/2017 05:01 PM, Jaimin Pan wrote:
>> 试一试
>> md5(random()::text);
>>
>>
>> select pg_backend_pid(),s,'string '|| md5(random()::text) from (select 
>> generate_series(2000,8000,1) s) as g;
>> Time: 239.733 ms
>>
>> 2017-09-05 16:28 GMT+08:00 Quan Zongliang 
>> <zongliang.quan@postgresdata.com 
>> <mailto:zongliang.quan@postgresdata.com>>:
>>
>>     各位下午好
>>     邮件列表沉寂太久,激活一下大家的情绪。
>>
>>     分享一个C语言实现的随机字符串函数:
>>     PG_FUNCTION_INFO_V1(random_string);
>>     Datum
>>     random_string(PG_FUNCTION_ARGS)
>>     {
>>             int32 length = PG_GETARG_INT32(0);
>>             int32 i;
>>             const char chars[] =
>>     "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
>>             char *result;
>>
>>             if (length < 0)
>>                     PG_RETURN_NULL();
>>
>>             if (length == 0)
>>                     PG_RETURN_TEXT_P(cstring_to_text(""));
>>
>>             result = palloc(length + 1);
>>             if (result == NULL)
>>                     ereport(ERROR, (errcode(ERRCODE_OUT_OF_MEMORY),
>>                                             errmsg("out of memory")));
>>
>>             for (i=0; i<length; i++)
>>             {
>>                     result[i] = chars[random()%(sizeof(chars)-1)];
>>             }
>>             result[i] = '\0';
>>
>>             PG_RETURN_TEXT_P(cstring_to_text(result));
>>     }
>>
>>     原文见:
>>     https://my.oschina.net/quanzl/blog/1529438
>>     <https://my.oschina.net/quanzl/blog/1529438;
>>
>>     欢迎关注我司公众号:postgresdata-news
>>
>>     -- 
>>     权宗亮
>>     神州飞象(北京)数据科技有限公司
>>     我们的力量源自最先进的开源数据库PostgreSQL
>>     zongliang.quan@postgresdata.com
>>     <mailto:zongliang.quan@postgresdata.com>
>>
>>
>>
>>     -- 
>>     Sent via pgsql-zh-general mailing list
>>     (pgsql-zh-general@postgresql.org
>>     <mailto:pgsql-zh-general@postgresql.org>)
>>     To make changes to your subscription:
>>     http://www.postgresql.org/mailpref/pgsql-zh-general
>>     <http://www.postgresql.org/mailpref/pgsql-zh-general;
>>
>>
> 



-- 
Sent via pgsql-zh-general mailing list (pgsql-zh-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-zh-general



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: [pgsql-zh-general] 随机字符串函数的C语言实现
  2017-09-05 08:28 随机字符串函数的C语言实现 Quan Zongliang <zongliang.quan@postgresdata.com>
  2017-09-05 09:01 `  Re: [pgsql-zh-general] 随机字符串函数的C语言实现 Jaimin Pan <jaimin.pan@gmail.com>
@ 2017-09-05 10:26   ` Quan Zongliang <zongliang.quan@postgresdata.com>
  1 sibling, 0 replies; 5+ messages in thread

From: Quan Zongliang @ 2017-09-05 10:26 UTC (permalink / raw)
  To: Jaimin Pan <jaimin.pan@gmail.com>; +Cc: pgsql-zh-general

这样不能指定长度,返回值本身又不够长,今天测试是在看TOAST的变化。

On 2017/9/5 17:01, Jaimin Pan wrote:
> 试一试
> md5(random()::text);
> 
> 
> select pg_backend_pid(),s,'string '|| md5(random()::text) from (select 
> generate_series(2000,8000,1) s) as g;
> Time: 239.733 ms
> 
> 2017-09-05 16:28 GMT+08:00 Quan Zongliang 
> <zongliang.quan@postgresdata.com <mailto:zongliang.quan@postgresdata.com>>:
> 
>     各位下午好
>     邮件列表沉寂太久,激活一下大家的情绪。
> 
>     分享一个C语言实现的随机字符串函数:
>     PG_FUNCTION_INFO_V1(random_string);
>     Datum
>     random_string(PG_FUNCTION_ARGS)
>     {
>              int32 length = PG_GETARG_INT32(0);
>              int32 i;
>              const char chars[] =
>     "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
>              char *result;
> 
>              if (length < 0)
>                      PG_RETURN_NULL();
> 
>              if (length == 0)
>                      PG_RETURN_TEXT_P(cstring_to_text(""));
> 
>              result = palloc(length + 1);
>              if (result == NULL)
>                      ereport(ERROR, (errcode(ERRCODE_OUT_OF_MEMORY),
>                                              errmsg("out of memory")));
> 
>              for (i=0; i<length; i++)
>              {
>                      result[i] = chars[random()%(sizeof(chars)-1)];
>              }
>              result[i] = '\0';
> 
>              PG_RETURN_TEXT_P(cstring_to_text(result));
>     }
> 
>     原文见:
>     https://my.oschina.net/quanzl/blog/1529438
>     <https://my.oschina.net/quanzl/blog/1529438;
> 
>     欢迎关注我司公众号:postgresdata-news
> 
>     -- 
>     权宗亮
>     神州飞象(北京)数据科技有限公司
>     我们的力量源自最先进的开源数据库PostgreSQL
>     zongliang.quan@postgresdata.com <mailto:zongliang.quan@postgresdata.com>
> 
> 
> 
>     -- 
>     Sent via pgsql-zh-general mailing list
>     (pgsql-zh-general@postgresql.org
>     <mailto:pgsql-zh-general@postgresql.org>)
>     To make changes to your subscription:
>     http://www.postgresql.org/mailpref/pgsql-zh-general
>     <http://www.postgresql.org/mailpref/pgsql-zh-general;
> 
> 



-- 
Sent via pgsql-zh-general mailing list (pgsql-zh-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-zh-general



^ permalink  raw  reply  [nested|flat] 5+ messages in thread


end of thread, other threads:[~2017-09-06 01:48 UTC | newest]

Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2017-09-05 08:28 随机字符串函数的C语言实现 Quan Zongliang <zongliang.quan@postgresdata.com>
2017-09-05 09:01 ` Jaimin Pan <jaimin.pan@gmail.com>
2017-09-05 09:11   ` winston cheung <winston_cheung@163.com>
2017-09-06 01:48     ` Quan Zongliang <zongliang.quan@postgresdata.com>
2017-09-05 10:26   ` Quan Zongliang <zongliang.quan@postgresdata.com>

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox