Postgresデータベースのストアドプロシージャ、パーティションテーブル、空間フィールド

Postgresデータベースのストアドプロシージャ、パーティションテーブル、空間フィールド、乱数など。


--查看所有表的行数
SELECT schemaname,relname,n_live_tup 
  FROM pg_stat_user_tables 
  ORDER BY n_live_tup DESC;
--192.168.10.10:5432/postgis_21_sample@postgres/postgres
drop table linetest cascade;
CREATE TABLE linetest
(
    gid integer,
    the_geom geometry
);

--创建分区表
create or replace function creatTB9() returns 
boolean AS
$BODY$
declare 
 jd integer;
 wd integer;
 _create_sql TEXT;
  begin
 FOR jd IN 1..3 LOOP
  for wd in 1..3 loop
  _create_sql:=format('CREATE TABLE line_partition_%s_%s(check(gid>0)) INHERITS(linetest);',jd,wd);
  EXECUTE _create_sql;
  end loop;
 end loop;
 return true;
  end;
$BODY$
LANGUAGE plpgsql;

--创建规则
create or replace function creatRule9() returns 
boolean AS
$BODY$
declare 
 jd integer;
 wd integer;
 _create_sql TEXT;
 _create_rule TEXT;
  begin
 FOR jd IN 1..3 LOOP
  for wd in 1..3 loop
  _create_rule:=format('CREATE OR REPLACE RULE rule_%s_%s AS ON INSERT TO linetest WHERE the_geom && st_makeenvelope(%s,%s,%s,%s) DO INSTEAD INSERT INTO line_partition_%s_%s VALUES(NEW.*);',jd,wd,-180+((jd-1)*12),-90+((wd-1)*6),-180+(jd*12),-90+(wd*6),jd,wd);
  EXECUTE _create_rule;
  end loop;
 end loop;
 return true;
  end;
$BODY$
LANGUAGE plpgsql;

--存储过程,insertTest3600_partitionAll
--参数说明
--njd:60(-180到180按6度等分)
--nwd:60(-90到90按3度等分)
--ncount:27778(一个亿除于60*60)
create or replace function fun_insert9() returns 
boolean AS
$BODY$
declare ii integer;
declare jd integer;
declare wd integer;
declare n integer;
declare x3 double precision;
declare y3 double precision;
declare x2 double precision;
declare y2 double precision;
declare x1 double precision;
declare y1 double precision;
declare _create_sql TEXT;
  begin 
  ii:=0;
	  FOR jd IN 1..3 LOOP
	  	FOR wd IN 1..3 LOOP
	  		FOR m IN 1..100 LOOP
	  			FOR n IN 1..1000 LOOP
				  x1:=6*((jd-1)*2) + n/1000.0 -180;
				  y1:=3*((wd-1)*2)+ m/100.0-90;
				  x2:=x1+0.0001;
				  y2:=y1;
				  ii:= ii+1;
				  _create_sql:=format('insert into line_partition_%s_%s values(%s, ST_GeomFromText(''LINESTRING(%s %s,%s %s)'',4326));',jd,wd,ii,x1,y1,x2,y2);
				  --_create_sql:=format('insert into linetest values(%s, ST_GeomFromText(''LINESTRING(%s %s,%s %s)'',4326));',ii,x1,y1,x2,y2);
				  execute _create_sql;
		  		end loop;
		  	end loop;
		  end loop;
	  end loop;	  
	  return true;
  end;
$BODY$
LANGUAGE plpgsql;


--the_geom geometry(Point,4326)
--创建分区表
select * from creatTB9() as tab;
--创建规则
select * from creatRule9() as tab;
--插入数据
select * from fun_insert9() as tab;

Successfully run. Total query runtime: 1 min 59 secs.
1 rows affected.
--------------------------------------------------------------------------------------------------------------------------
--postgresql 函数之游标、更新数据
--postgresql 函数之游标、更新数据
--postgresql 函数之游标、更新数据

CREATE OR REPLACE FUNCTION fn_update_gid() returns 
boolean AS
$BODY$
	DECLARE
	   unbound_refcursor refcursor;  --游标
	   n_refcursor refcursor;  --游标
		 ctid1 VARCHAR(100);
		 jd integer;
		 wd integer;
		 ii integer;
		 i integer;
		 n integer;
		 gid1 integer;
		 _create_sql TEXT;
	BEGIN
	 ii:=1;	
	FOR jd IN 1..3 LOOP
	  FOR wd IN 1..3 LOOP
	    _create_sql:=format('select ctid from line_partition_%s_%s',jd,wd);
		open unbound_refcursor for execute _create_sql;  --打开游标 并注入要搜索的字段的记录
	    _create_sql := format('select count(*) from line_partition_%s_%s',jd,wd);
	    open n_refcursor for execute _create_sql;
		fetch n_refcursor into n;
	    FOR i IN 1..n LOOP
			fetch unbound_refcursor into ctid1;--persPin, deptCode, deptName, deptId;  --将游标指定的值赋值给变量  
			--if ctid1 is null then
			  --exit;
			--end if;
			_create_sql:=format('update line_partition_%s_%s set gid=%s  where ctid=''%s'';',jd,wd,ii,ctid1);
			--_create_sql:=format('update line_partition_1_1 set gid=%s  where ctid=''%s'';',ii,ctid1);
			execute _create_sql;
    		raise notice '%',_create_sql;  --打印消息
			--_all_sql := format('%s%s',_all_sql,_create_sql);
			ii:=ii+1;
        end loop;  --结束循环
	    close n_refcursor;
	    close unbound_refcursor;  --关闭游标
      end loop;
    end loop;	  
	  
	  return true;
END$BODY$
  LANGUAGE plpgsql;
  
select * from fn_update_gid() as tab;

--postgresql 函数之游标、更新数据
--postgresql 函数之游标、更新数据
--postgresql 函数之游标、更新数据
--------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------

--统计各数据库占用磁盘大小:

 SELECT d.datname AS Name,  pg_catalog.pg_get_userbyid(d.datdba) AS Owner,
    CASE WHEN pg_catalog.has_database_privilege(d.datname, 'CONNECT')
        THEN pg_catalog.pg_size_pretty(pg_catalog.pg_database_size(d.datname))
        ELSE 'No Access'
    END AS SIZE
FROM pg_catalog.pg_database d
    ORDER BY
    CASE WHEN pg_catalog.has_database_privilege(d.datname, 'CONNECT')
        THEN pg_catalog.pg_database_size(d.datname)
        ELSE NULL
    END DESC -- nulls first
    LIMIT 20

--统计数据库中各表占用磁盘大小:
SELECT
    table_schema || '.' || table_name AS table_full_name,
    pg_size_pretty(pg_total_relation_size('"' || table_schema || '"."' || table_name || '"')) AS size
FROM information_schema.tables
ORDER BY
    pg_total_relation_size('"' || table_schema || '"."' || table_name || '"') DESC

 

おすすめ

転載: blog.csdn.net/qq503690160/article/details/113388175