postgrep创建存储过程例子

 

1.创建数据库
psql

create database db_hxl encoding = 'utf8';
#\c db_hxl

2.创建表
create table tb_test01
(
id bigserial primary key not null,
name varchar(32),
createtime timestamp default current_timestamp,
modifytime  timestamp default current_timestamp
);

 

3.创建存储过程

复制代码
CREATE OR REPLACE FUNCTION "public"."sp_insert_data"()
  RETURNS "pg_catalog"."int4" AS $BODY$  
declare  
    total integer;
        i integer := 1;
BEGIN  
 
  while i <= 1000 loop
   insert into tb_test01(name) values('name'||i);
     update tb_test01
       set name='更新' ,modifytime=now()
      where id=ceil(random()*(10000-1)+1);
     i=i+1;
  end loop;

   RETURN 1;  
END;  
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100
复制代码

 

4.创建crontab 调度
[postgres@localhost scripts]$ crontab -l
##pg生成测试数据
*/30 * * * * /home/postgres/scripts/insert_data.sh 1>/dev/null 2>&1

[postgres@localhost scripts]$ more insert_data.sh
#!/bin/bash
. /etc/profile
. ~/.bash_profile
echo a>>/tmp/a.txt
psql -U postgres -h localhost -d db_hxl << EOF
select sp_insert_data();
EOF

 

posted @   slnngk  阅读(584)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示