PostgreSQL模拟Oracle dba_objects
PostgreSQL模拟Oracle dba_objects查询出schema下所有的用户自定义对象
创建测试数据
psql -U postgres
create user test password 'test';
create database testdb;
\c testdb
CREATE SCHEMA AUTHORIZATION test;
创建对象
export PGPASSWORD=test
psql -U test -d testdb
-- 创建域
create domain phone_type as text constraint phone_check check (VALUE ~ '^\d{11}$');
-- 创建数据类型
CREATE TYPE human_sex AS ENUM ('male', 'female');
CREATE TYPE test_type AS (f1 int, f2 text);
-- 创建序列
create sequence employees_s;
create sequence employees_s1;
-- 创建测试表
create table test_t1(id int);
create table test_t2(id int);
create table test_t3(id int);
create table test_t4(id int);
create table employees
( employee_id int8 primary key
, first_name varchar(20)
, last_name varchar(25)
, sex human_sex
, email varchar(25)
, phone_number phone_type
, salary numeric(8,2)
, last_update_date timestamp
, constraint emp_salary_min check (salary > 0)
, constraint emp_email_uk unique (email)
) ;
create table tbp(id int,date timestamp(6),col2 text) partition by range(date);
create table tbp_2020 partition of tbp for values from ('2020-01-01') to ('2021-01-01');
create table tbp_2021 partition of tbp for values from ('2021-01-01') to ('2022-01-01');
create table tbp_2022 partition of tbp for values from ('2022-01-01') to ('2023-01-01');
create table tbp_2023 partition of tbp for values from ('2023-01-01') to ('2024-01-01');
create table tbp_default partition of tbp default;
-- 创建主分区表
CREATE TABLE sales (
id int,
region VARCHAR(50),
sales_date DATE,
amount NUMERIC
) PARTITION BY RANGE (sales_date);
-- 创建一级分区
CREATE TABLE sales_2023 PARTITION OF sales FOR VALUES FROM ('2023-01-01') TO ('2024-01-01') PARTITION BY LIST (region);
CREATE TABLE sales_2024 PARTITION OF sales FOR VALUES FROM ('2024-01-01') TO ('2025-01-01') PARTITION BY LIST (region);
-- 创建二级分区
CREATE TABLE sales_2023_north PARTITION OF sales_2023 FOR VALUES IN ('North');
CREATE TABLE sales_2023_south PARTITION OF sales_2023 FOR VALUES IN ('South');
CREATE TABLE sales_2023_east PARTITION OF sales_2023 FOR VALUES IN ('East');
CREATE TABLE sales_2023_west PARTITION OF sales_2023 FOR VALUES IN ('West');
CREATE TABLE sales_2024_north PARTITION OF sales_2024 FOR VALUES IN ('North');
CREATE TABLE sales_2024_south PARTITION OF sales_2024 FOR VALUES IN ('South');
CREATE TABLE sales_2024_east PARTITION OF sales_2024 FOR VALUES IN ('East');
CREATE TABLE sales_2024_west PARTITION OF sales_2024 FOR VALUES IN ('West');
-- 插入数据
insert into employees values(nextval('employees_s'),'King','Johnn','male','johnn@163.com','15145264084',10000,now());
-- 以下两条应该报错
insert into employees values(nextval('employees_s'),'Job','Lucy','female','lucy@163.com','151452640841',10000,now());
insert into employees values(nextval('employees_s'),'Job','Lucy','females','lucy@163.com','15145264084',10000,now());
-- 创建索引
create index idx_name on employees(first_name,last_name);
create index idx_id on tbp(id);
create index idx_id2 on sales(id);
-- 创建约束
ALTER TABLE employees ADD CONSTRAINT check_salary CHECK (salary>0);
视图
-- 普通视图
create view emp as select * from employees;
-- 物化视图
create materialized view emp_v as select * from employees;
refresh materialized view emp_v;
创建触发器
-- 普通触发器
CREATE OR REPLACE FUNCTION update_time_column()
RETURNS TRIGGER AS $$
BEGIN
NEW.last_update_date := CURRENT_TIMESTAMP;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER update_time_trigger BEFORE UPDATE ON employees FOR EACH ROW EXECUTE FUNCTION update_time_column();
-- 事件触发器
CREATE OR REPLACE FUNCTION DISABLE_DROP_TABLE()
RETURNS event_trigger AS $$
BEGIN
if tg_tag = 'DROP TABLE' THEN
RAISE EXCEPTION 'Command % is disabled.', tg_tag;
END if;
END;
$$ LANGUAGE plpgsql;
CREATE EVENT TRIGGER DISABLE_DROP_TABLE on ddl_command_start EXECUTE FUNCTION DISABLE_DROP_TABLE();
函数和存储过程
-- 函数
CREATE FUNCTION inc(val integer) RETURNS integer AS $$
BEGIN
RETURN val + 1;
END;
$$ LANGUAGE PLPGSQL;
CREATE FUNCTION inc(val1 integer,val2 integer) RETURNS integer AS $$
BEGIN
RETURN val1 + val2;
END;
$$ LANGUAGE PLPGSQL;
-- 存储过程
CREATE OR REPLACE PROCEDURE test_sum(a NUMERIC,b NUMERIC,C NUMERIC)
AS $$
DECLARE
val int;
BEGIN
val := a+b+c;
RAISE NOTICE 'Total is : % !',val;
END;
$$ language plpgsql;
对象信息查询
表(分区表和普通表)、索引(分区索引和普通索引)、视图、物化视图、序列
select relname as object_name,case when relkind='r' and relispartition='f' and relhassubclass='f' then '普通表'
when relkind='p' and relispartition='f' and relhassubclass='t' then '分区表主表'
when relkind='p' and relispartition='t' and relhassubclass='t' then '分区表一级分区'
when relkind='r' and relispartition='t' and relhassubclass='f' then '分区表子表'
when relkind='i' and relispartition='f' and relhassubclass='f' then '普通索引'
when relkind='I' and relispartition='f' and relhassubclass='t' then '分区表主表索引'
when relkind='i' and relispartition='t' and relhassubclass='f' then '分区表子表索引'
when relkind='I' and relispartition='t' and relhassubclass='t' then '分区表一级分区索引'
when relkind='S' then '序列'
when relkind='c' then '组合类型'
when relkind='m' then '物化视图'
when relkind='v' then '普通视图'
end as object_type
from pg_class
where relnamespace::REGNAMESPACE::text='test'
order by 2 nulls last;
数据类型
select typname as object_name,'数据类型' as object_type from pg_type t
left join pg_class c ON c.reltype = t.oid
where t.typnamespace::REGNAMESPACE::text='test'
AND c.relkind IS NULL
and t.typname !~ '^_';
约束
select conname as object_name,'约束' as object_type from pg_constraint where connamespace::REGNAMESPACE::text='test';
select t2.nspname as schema,t3.relname as table,t1.conname,
case when t1.contype='c' then '检查约束'
when t1.contype='p' then '主键约束'
when t1.contype='u' then '唯一约束' end as contype,
t4.relname as index
from pg_constraint t1 join pg_namespace t2 on t1.connamespace = t2.oid
join pg_class t3 on t1.conrelid = t3.oid
left join pg_class t4 on t1.conindid = t4.oid
where t2.nspname = 'test';
触发器
-- 普通触发器
select -- t3.nspname as schema,
concat(t2.relname,'.',tgname) as object_name,
'普通触发器' as object_type
from pg_trigger t1
join pg_class t2 on t1.tgrelid=t2.oid
join pg_namespace t3 on t2.relnamespace=t3.oid
where t3.nspname = 'test';
-- select concat(event_object_table,'.',trigger_name) as object_name,'trigger' as object_type from information_schema.triggers where trigger_schema='test';
-- 事件触发器
select evtname as object_name,'事件触发器' as object_type from pg_event_trigger;
存储过程/函数
select
translate(concat(proname,(select array(select format_type(val,null) from unnest(proargtypes) as val))::text),'{}','()') as object_name,
case when prokind='f' then '函数' when prokind='p' then '存储过程' end as object_type
from pg_proc
where pronamespace::REGNAMESPACE::text='test';
合并查询总数量
with schema_info as (
select unnest(ARRAY['test']) as schema_name -- 模式名
)
select object_type,count(*) from (
-- 表(分区表和普通表)、索引(分区索引和普通索引)、视图、物化视图、序列
select relname as object_name,
case when relkind='r' and relispartition='f' and relhassubclass='f' then '普通表'
when relkind='p' and relispartition='f' and relhassubclass='t' then '分区表主表'
-- when relkind='p' and relispartition='t' and relhassubclass='t' then '分区表一级分区'
-- when relkind='r' and relispartition='t' and relhassubclass='f' then '分区表子表'
when relkind='i' and relispartition='f' and relhassubclass='f' then '普通索引'
-- when relkind='I' and relispartition='f' and relhassubclass='t' then '分区表主表索引'
-- when relkind='i' and relispartition='t' and relhassubclass='f' then '分区表子表索引'
-- when relkind='I' and relispartition='t' and relhassubclass='t' then '分区表一级分区索引'
when relkind='S' then '序列'
when relkind='c' then '组合类型'
when relkind='m' then '物化视图'
when relkind='v' then '普通视图'
end as object_type
from pg_class join schema_info on (pg_class.relnamespace::REGNAMESPACE::text=schema_info.schema_name)
union all
-- 数据类型
select typname as object_name,'数据类型' as object_type from pg_type t
left join pg_class c ON c.reltype = t.oid
join schema_info on (t.typnamespace::REGNAMESPACE::text=schema_info.schema_name)
AND c.relkind IS NULL
and t.typname !~ '^_'
union all
-- 约束
select conname as object_name,'约束' as object_type from pg_constraint where connamespace::REGNAMESPACE::text=(select * from schema_info)
union all
-- 触发器
select -- t3.nspname as schema,
concat(t2.relname,'.',tgname) as object_name,
'普通触发器' as object_type
from pg_trigger t1
join pg_class t2 on t1.tgrelid=t2.oid
join pg_namespace t3 on t2.relnamespace=t3.oid
join schema_info on (t3.nspname=schema_info.schema_name)
union all
select evtname as object_name,'事件触发器' as object_type from pg_event_trigger
union all
-- 存储过程/函数
select
translate(concat(proname,(select array(select format_type(val,null) from unnest(proargtypes) as val))::text),'{}','()') as object_name,
case when prokind='f' then '函数' when prokind='p' then '存储过程' end as object_type
from pg_proc
join schema_info on (pg_proc.pronamespace::REGNAMESPACE::text=schema_info.schema_name)) t
where t.object_type is not null
group by 1
order by 2 desc;
PostgreSQL模拟Oracle dba_objects的更多相关文章
- [转帖]Oracle dba_objects和all_objects 最大的区别
Oracle dba_objects和all_objects 最大的区别 原创 Oracle 作者:maohaiqing0304 时间:2015-08-14 15:07:18 9281 0 链 ...
- 如何在postgresql中模拟oracle的dual表,来测试数据库最基本的连接功能?
还好,网上弄到的,,没有dual的数据库,可以试图用select函数不带from数据表的方式来实现返回值. 一段测试代码: try: conn = psycopg2.connect(database= ...
- PostgreSQL 与 Oracle 访问分区表执行计划差异
熟悉Oracle 的DBA都知道,Oracle 访问分区表时,对于没有提供分区条件的,也就是在无法使用分区剪枝情况下,优化器会根据全局的统计信息制定执行计划,该执行计划针对所有分区适用.在分析利弊之前 ...
- (转)PostgreSQL 兼容Oracle - orafce
转自:http://blog.163.com/digoal@126/blog/static/1638770402015112144250486/ PostgreSQL是和Oracle最接近的企业数据库 ...
- ORACLE DBA_OBJECTS视图中OBJECT_TYPE为LOB的对象查看
在ORACLE数据库中,DBA_OBJECTS视图中OBJECT_TYPE为LOB的对象是什么东西呢?其实OBJECT_TYPE为LOB就是大对象(LOB),它指那些用来存储大量数据的数据库字段.下面 ...
- postgresql和oracle数据库对比
SQL执行计划干预 从使用postgresql来看,想要改变执行计划只能通过対表进行分析,不能通过添加hint的方式来改变执行计划: oracle不仅可以通过对表进行收集统计来改变执行计划,而且很重要 ...
- postgresql与Oracle:空字符串与null
空字符串:两个单引号,中间无空格等任何内容 在postgresql中,空字符串与null是不同的:而oracle中,空字符串与null等同.测试如下: postgresql中: postgres=# ...
- PostgreSQL与Oracle对应的函数
一.对应的函数 1.sysdate oracle pgsql sysdate current_date. current_timestamp nvl coalesce trunc date_trun ...
- mysql 模拟oracle中的序列
因业务需要,把oracle 数据据转成mysql,同时oracle中程序本来一直在用 序列, mysql中没有,所以需要在mysql中新建一个表进行模拟, CREATE TABLE `sequence ...
- Windows的ODBC配置指南: MySQL, PostgreSQL, DB2, Oracle
MySQL- 官网: https://dev.mysql.com/downloads/connector/odbc/- 安装: * msi格式, 直接安装即可 * zip格式, 解压缩, 命令行(管理 ...
随机推荐
- C#基础 - Cancellation
目录 前言 1,概览 1.1 Cancellation是合作性的 1.2 CancellationToken及其典型用法 1.3 CancellationToken的响应 1.4 一个容易搞错的点 2 ...
- Docker 导出容器 / 镜像
有时由于镜像大小.网络限制等原因,我们不能将本地制作的容器 / 镜像上传到公共容器注册表.此时我们可以选择将镜像以本地文件的形式导出. 导入 / 导出容器 docker export "CO ...
- 编译器实现之旅——第十六章 代码装载、链接器、全局变量与main函数
在上一章的旅程中,我们已经实现了函数调用的代码生成器分派函数,但在上一章的末尾,我们留下了三个问题: 我们需要为全局变量压栈 main函数需要在程序启动时被自动调用 我们需要实现一个链接器,以将所有的 ...
- (Ljava/lang/String;)Ljava/util/List;
背景:原正常代码,更改类名后,重新运行 报错:(Ljava/lang/String;)Ljava/util/List; 解决:mvn clean 后 compile,再运行,正常
- Word在不同电脑排版异常
Word在不同电脑排版异常 问题描述 今天又有同学向我抱怨用 word 写的论文明明在自己的电脑格式调的好好的,怎么在导师那格式又乱了,害的挨批. 笔者也遇到过该问题,正好趁这次机会简单整理一下. 注 ...
- bacnet mstp设备数据 转IEC61850项目案例
目录 1 案例说明 1 2 VFBOX网关工作原理 1 3 使用YABE软件读取BACNET MSTP设备信息 2 4 配置网关采集BACNET MSTP数据 4 5 用IEC61850协议转发数据 ...
- DRBD - Distributed Replication Block Device
Ref https://computingforgeeks.com/install-and-configure-drbd-on-centos-rhel https://www.veritas.com/ ...
- ASP.NET Core – CORS (Cross-Origin Resource Sharing)
参考 Docs – Enable Cross-Origin Requests (CORS) in ASP.NET Core 介绍 CORS (Cross-Origin Resource Sharing ...
- JavaScript – Fetch
前言 上一篇 JavaScript – XMLHttpRequest 有提到 XMLHttpRequest 正在被 Fetch 取代,这篇就继续介绍 Fetch 吧. 参考 阮一峰 – Fetch A ...
- DOM – MutationObserver
介绍 它和 IntersectionObserver, ResizeObserver 差不多, 都是观察 element 变化的. 它可以观察元素的 attribute 增加, 移除, 修改, app ...