共計(jì) 1727 個(gè)字符,預(yù)計(jì)需要花費(fèi) 5 分鐘才能閱讀完成。
本篇文章給大家分享的是有關(guān) PostgreSQL 中怎么利用 DBLink 實(shí)現(xiàn)自治事務(wù),丸趣 TV 小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著丸趣 TV 小編一起來看看吧。
安裝 dblink 插件, 驗(yàn)證 dblink 可連接至本地 PG
[local]:5432 pg12@testdb=# create extension dblink;
ERROR: extension dblink already exists
Time: 1.128 ms
[local]:5432 pg12@testdb=# select t1.* from dblink(host=/tmp port=5432 dbname=testdb user=pg12 , select * from t1) as t1(id int,c1 int,c2 int) limit 1;
id | c1 | c2
----+----+----
4 | 4 | 4
(1 row)
Time: 1413.943 ms (00:01.414)
創(chuàng)建數(shù)據(jù)表和存儲(chǔ)過程
[local]:5432 pg12@testdb=# drop table if exists log;
DROP TABLE
Time: 38.531 ms
[local]:5432 pg12@testdb=# CREATE TABLE log(
pg12@testdb(# msg text not null,
pg12@testdb(# sender_ts timestamptz not null,
pg12@testdb(# local_ts timestamptz default now()
pg12@testdb(# );
CREATE TABLE
Time: 8.686 ms
[local]:5432 pg12@testdb=# CREATE OR REPLACE FUNCTION log_dblink(msg text)
pg12@testdb-# RETURNS void
pg12@testdb-# LANGUAGE sql
pg12@testdb-# AS $function$
pg12@testdb$# select dblink( host=/tmp port=5432 user=pg12 dbname=testdb ,
pg12@testdb$# format(insert into log select %L, %L , msg, clock_timestamp()::text))
pg12@testdb$# $function$;
CREATE FUNCTION
Time: 3.657 ms
測(cè)試驗(yàn)證, 啟動(dòng)事務(wù), 調(diào)用函數(shù), 然后 rollback
[local]:5432 pg12@testdb=# begin;
BEGIN
Time: 1.293 ms
[local]:5432 pg12@testdb=#* select log_dblink( log message
log_dblink
------------
(1 row)
Time: 16.394 ms
[local]:5432 pg12@testdb=#* rollback;
ROLLBACK
Time: 0.845 ms
通過 dblink 執(zhí)行的 SQL 主事務(wù)隔離, 已 commit
[local]:5432 pg12@testdb=# select * from log;
msg | sender_ts | local_ts
-------------+-------------------------------+-------------------------------
log message | 2019-08-05 14:23:30.459183+08 | 2019-08-05 14:23:30.468047+08
(1 row)
Time: 0.695 ms
以上就是 PostgreSQL 中怎么利用 DBLink 實(shí)現(xiàn)自治事務(wù),丸趣 TV 小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見到或用到的。希望你能通過這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注丸趣 TV 行業(yè)資訊頻道。
正文完
發(fā)表至: 數(shù)據(jù)庫(kù)
2023-08-03