pgsql识别两个同结构表的不同

平常的业务中,往往需要检查记录业务信息的表在不同时期发生变化的记录,从而针对更改的记录做出更好的决策。之前有用游标实现过两张同结构表的遍历对比,但针对上十万上百万的数据记录的表时,运行起来需要很长时间,并且会造成运行内存短时间占满的情况,现在采用except进行两表操作。代码如下:

create or replace function get_tables_differences
(in schema_n_1 text,in schema_n_2 text,in table_n_1 text,in table_n_2 text)
returns void
language plpgsql
as $$
begin
drop table if exists temp_table_1;
drop table if exists temp_table_2;
drop table if exists result_table_get_tables_differences;
execute format('create table temp_table_1 as select * from %I.%I limit(0)',schema_n_1,table_n_1);
execute format('create table temp_table_2 as select * from %I.%I limit(0)',schema_n_2,table_n_2);
alter table temp_table_1 add column if not exists from_where text default 'first_table';
alter table temp_table_2 add column if not exists from_where text default 'second_table';
execute format('insert into temp_table_1 (select *
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要备份pgsql数据库的结构数据,可以使用pg_dump命令。具体步骤如下: 1. 打开命令提示符界面,切换到数据库安装的bin目录下。 2. 输入以下命令来备份整个数据库,包括结构和数据: ``` pg_dump -h localhost -p 5432 -U postgres -d 数据库名 > 备份文件路径 ``` 其中,localhost是数据库主机名,5432是数据库端口号,postgres是数据库用户名,数据库名是要备份的数据库的名称,备份文件路径是备份文件的保存路径和文件名。[1] 例如,要备份名为data的数据库,并将备份文件保存在D盘的data.bak文件中,可以使用以下命令: ``` pg_dump -h localhost -p 5432 -U postgres -d data > D:\data.bak ``` 如果只想备份指定的(包括数据),可以使用以下命令: ``` pg_dump -h 主机名 -p 端口号 -d 数据库名 -t 名1 -t 名2 -U 用户名 > 备份文件路径 ``` 其中,主机名是数据库主机名,端口号是数据库端口号,数据库名是要备份的数据库的名称,名1和名2是要备份的的名称,用户名是数据库用户名,备份文件路径是备份文件的保存路径和文件名。[3] 例如,要备份名为data的数据库中的table1和table2,并将备份文件保存在/data/文件夹下的filename.sql文件中,可以使用以下命令: ``` pg_dump -h localhost -p 5432 -d data -t table1 -t table2 -U postgres > /data/filename.sql ``` 如果需要了解更多关于pgsql数据的函数和SQL操作,可以参考该链接:https://www.runoob.com/postgresql/postgresql-tutorial.html。[2]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值