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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?