Skip to content

Commit c864a2a

Browse files
committed
Add DTM implementation based on timestamps
1 parent b03fba9 commit c864a2a

File tree

17 files changed

+3040
-0
lines changed

17 files changed

+3040
-0
lines changed

contrib/pg_tsdtm/Makefile

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
MODULE_big = pg_dtm
2+
OBJS = pg_dtm.o
3+
4+
EXTENSION = pg_dtm
5+
DATA = pg_dtm--1.0.sql
6+
7+
ifdef USE_PGXS
8+
PG_CONFIG = pg_config
9+
PGXS := $(shell $(PG_CONFIG) --pgxs)
10+
include $(PGXS)
11+
else
12+
subdir = contrib/pg_dtm
13+
top_builddir = ../..
14+
include $(top_builddir)/src/Makefile.global
15+
include $(top_srcdir)/contrib/contrib-global.mk
16+
endif

contrib/pg_tsdtm/README

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
===
2+
dtm
3+
===
4+
5+
Distributed transaction management tools for PostgreSQL.
6+
7+
--------------------
8+
Communication scheme
9+
--------------------
10+
11+
.- Backend -.
12+
/ \
13+
/ \
14+
DTM ---- Backend ---- Coordinator
15+
\ /
16+
\ /
17+
`- Backend -´
18+
19+
-----------------------
20+
Coordinator-Backend API
21+
-----------------------
22+
23+
This API includes a set of postgres procedures that
24+
the coordinator can call with "select" statement.
25+
26+
extend_transaction (n integer) -> (higcid gcid)
27+
join_transaction (higcid gcid) -> ()
28+
FIXME: add procedures that would start and finish 2pc
29+
30+
----------
31+
libdtm api
32+
----------
33+
34+
typedef unsigned long long cid_t;
35+
36+
// Connects to the specified DTM.
37+
DTMConn DtmConnect(char *host, int port);
38+
39+
// Disconnects from the DTM. Do not use the 'dtm' pointer after this call, or
40+
// bad things will happen.
41+
void DtmDisconnect(DTMConn dtm);
42+
43+
// Asks DTM for the first 'gcid' with unknown status. This 'gcid' is used as a
44+
// kind of snapshot. Returns the 'gcid' on success, or INVALID_GCID otherwise.
45+
cid_t DtmGlobalGetNextCid(DTMConn dtm);
46+
47+
// Prepares a commit. Returns the 'gcid' on success, or INVALID_GCID otherwise.
48+
cid_t DtmGlobalPrepare(DTMConn dtm);
49+
50+
// Finishes a given commit with 'committed' status. Returns 'true' on success,
51+
// 'false' otherwise.
52+
bool DtmGlobalCommit(DTMConn dtm, cid_t gcid);
53+
54+
// Finishes a given commit with 'aborted' status.
55+
void DtmGlobalRollback(DTMConn dtm, cid_t gcid);
56+
57+
// Gets the status of the commit identified by 'gcid'. Returns the status on
58+
// success, or -1 otherwise.
59+
int DtmGlobalGetTransStatus(DTMConn dtm, cid_t gcid);
60+
61+
--------------------
62+
Backend-DTM Protocol
63+
--------------------
64+
65+
DTM <--> Backend:
66+
67+
<- 'p'<hex16 self> - "prepare"
68+
-> '+'<hex16 gcid> - "commit prepared"
69+
-> '-' - "something went wrong"
70+
71+
<- 'c'<hex16 gcid> - "commit"
72+
-> '+' - "commit saved"
73+
-> '-' - "something went wrong"
74+
75+
<- 'a'<hex16 gcid> - "abort"
76+
-> '+' - "abort saved"
77+
-> '-' - "something went wrong"
78+
79+
<- 'h' - "horizon"
80+
-> '+'<hex16 gcid> - "here is a gcid you can use as a snapshot"
81+
-> '-' - "something went wrong"
82+
83+
<- 's'<hex16 gcid> - "status"
84+
-> '+''c|a|?' - "here is the transaction status"
85+
(c)ommitted, (a)borted or (?)unknown
86+
-> '-' - "something went wrong"
87+
88+
Backend disconnection is considered as an abort of all incomplete commits
89+
prepared by that backend.

contrib/pg_tsdtm/expected/test_dtm.out

Whitespace-only changes.

contrib/pg_tsdtm/pg_dtm--1.0.sql

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
-- complain if script is sourced in psql, rather than via CREATE EXTENSION
2+
\echo Use "CREATE EXTENSION pg_dtm" to load this file. \quit
3+
4+
CREATE FUNCTION dtm_register_node(id integer) RETURNS void
5+
AS 'MODULE_PATHNAME','dtm_register_node'
6+
LANGUAGE C;
7+
8+
CREATE FUNCTION dtm_extend(gtid cstring default null) RETURNS bigint
9+
AS 'MODULE_PATHNAME','dtm_extend'
10+
LANGUAGE C;
11+
12+
CREATE FUNCTION dtm_access(snapshot bigint, gtid cstring default null) RETURNS bigint
13+
AS 'MODULE_PATHNAME','dtm_access'
14+
LANGUAGE C;
15+
16+
CREATE FUNCTION dtm_begin_prepare(gtid cstring, coordinator integer) RETURNS void
17+
AS 'MODULE_PATHNAME','dtm_begin_prepare'
18+
LANGUAGE C;
19+
20+
CREATE FUNCTION dtm_prepare(gtid cstring, csn bigint) RETURNS bigint
21+
AS 'MODULE_PATHNAME','dtm_prepare'
22+
LANGUAGE C;
23+
24+
CREATE FUNCTION dtm_end_prepare(gtid cstring, csn bigint) RETURNS void
25+
AS 'MODULE_PATHNAME','dtm_end_prepare'
26+
LANGUAGE C;

0 commit comments

Comments
 (0)