Skip to content

Commit ad5716f

Browse files
maltoejosevalim
authored andcommitted
[#558] Support xid8 type (#559)
Fixes [#558](#558) This patch adds support for the `xid8` type introduced in PostgreSQL 13.
1 parent 7e0272c commit ad5716f

File tree

5 files changed

+57
-3
lines changed

5 files changed

+57
-3
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## v0.15.11-dev
4+
5+
* Enhancements
6+
* Support `xid8` type introduced in PostgreSQL 13
7+
38
## v0.15.10 (2021-07-27)
49

510
* Enhancements

lib/postgrex/binary_utils.ex

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,18 @@ defmodule Postgrex.BinaryUtils do
1313
quote do: signed - 16
1414
end
1515

16-
defmacro uint16 do
17-
quote do: unsigned - 16
16+
defmacro uint64 do
17+
quote do: unsigned - 64
1818
end
1919

2020
defmacro uint32 do
2121
quote do: unsigned - 32
2222
end
2323

24+
defmacro uint16 do
25+
quote do: unsigned - 16
26+
end
27+
2428
defmacro int8 do
2529
quote do: signed - 8
2630
end

lib/postgrex/extensions/xid8.ex

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
defmodule Postgrex.Extensions.Xid8 do
2+
@moduledoc false
3+
import Postgrex.BinaryUtils, warn: false
4+
use Postgrex.BinaryExtension, send: "xid8send"
5+
6+
@xid8_range 0..18_446_744_073_709_551_615
7+
8+
def encode(_) do
9+
range = Macro.escape(@xid8_range)
10+
11+
quote location: :keep do
12+
int when int in unquote(range) ->
13+
<<8::int32, int::uint64>>
14+
15+
other ->
16+
raise DBConnection.EncodeError, Postgrex.Utils.encode_msg(other, unquote(range))
17+
end
18+
end
19+
20+
def decode(_) do
21+
quote location: :keep do
22+
<<8::int32, int::uint64>> -> int
23+
end
24+
end
25+
end

lib/postgrex/utils.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ defmodule Postgrex.Utils do
3838
Postgrex.Extensions.TSVector,
3939
Postgrex.Extensions.UUID,
4040
Postgrex.Extensions.VoidBinary,
41-
Postgrex.Extensions.VoidText
41+
Postgrex.Extensions.VoidText,
42+
Postgrex.Extensions.Xid8
4243
]
4344

4445
@doc """

test/query_test.exs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,25 @@ defmodule QueryTest do
734734
catch_error(query("SELECT $1::int8", [-9_223_372_036_854_775_808 - 1]))
735735
end
736736

737+
@tag min_pg_version: "13.0"
738+
test "decode xid8 from pg_current_xact_id", context do
739+
assert [[int]] = query("SELECT pg_current_xact_id()", [])
740+
assert is_integer(int)
741+
end
742+
743+
@tag min_pg_version: "13.0"
744+
test "encode enforces bounds on xid8", context do
745+
# xid8's range is 0 to +18_446_744_073_709_551_615
746+
assert [[0]] = query("SELECT $1::xid", [0])
747+
assert [[18_446_744_073_709_551_615]] = query("SELECT $1::xid8", [18_446_744_073_709_551_615])
748+
749+
assert %DBConnection.EncodeError{} =
750+
catch_error(query("SELECT $1::xid8", [18_446_744_073_709_551_615 + 1]))
751+
752+
assert %DBConnection.EncodeError{} =
753+
catch_error(query("SELECT $1::xid", [0 - 1]))
754+
end
755+
737756
test "encode uuid", context do
738757
uuid = <<0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15>>
739758
assert [[^uuid]] = query("SELECT $1::uuid", [uuid])

0 commit comments

Comments
 (0)