Skip to content

Commit

Permalink
[#558] Support xid8 type (#559)
Browse files Browse the repository at this point in the history
Fixes [#558](#558)

This patch adds support for the `xid8` type introduced in PostgreSQL 13.
  • Loading branch information
maltoe authored and josevalim committed Sep 26, 2021
1 parent 7e0272c commit ad5716f
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 3 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## v0.15.11-dev

* Enhancements
* Support `xid8` type introduced in PostgreSQL 13

## v0.15.10 (2021-07-27)

* Enhancements
Expand Down
8 changes: 6 additions & 2 deletions lib/postgrex/binary_utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,18 @@ defmodule Postgrex.BinaryUtils do
quote do: signed - 16
end

defmacro uint16 do
quote do: unsigned - 16
defmacro uint64 do
quote do: unsigned - 64
end

defmacro uint32 do
quote do: unsigned - 32
end

defmacro uint16 do
quote do: unsigned - 16
end

defmacro int8 do
quote do: signed - 8
end
Expand Down
25 changes: 25 additions & 0 deletions lib/postgrex/extensions/xid8.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
defmodule Postgrex.Extensions.Xid8 do
@moduledoc false
import Postgrex.BinaryUtils, warn: false
use Postgrex.BinaryExtension, send: "xid8send"

@xid8_range 0..18_446_744_073_709_551_615

def encode(_) do
range = Macro.escape(@xid8_range)

quote location: :keep do
int when int in unquote(range) ->
<<8::int32, int::uint64>>

other ->
raise DBConnection.EncodeError, Postgrex.Utils.encode_msg(other, unquote(range))
end
end

def decode(_) do
quote location: :keep do
<<8::int32, int::uint64>> -> int
end
end
end
3 changes: 2 additions & 1 deletion lib/postgrex/utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ defmodule Postgrex.Utils do
Postgrex.Extensions.TSVector,
Postgrex.Extensions.UUID,
Postgrex.Extensions.VoidBinary,
Postgrex.Extensions.VoidText
Postgrex.Extensions.VoidText,
Postgrex.Extensions.Xid8
]

@doc """
Expand Down
19 changes: 19 additions & 0 deletions test/query_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,25 @@ defmodule QueryTest do
catch_error(query("SELECT $1::int8", [-9_223_372_036_854_775_808 - 1]))
end

@tag min_pg_version: "13.0"
test "decode xid8 from pg_current_xact_id", context do
assert [[int]] = query("SELECT pg_current_xact_id()", [])
assert is_integer(int)
end

@tag min_pg_version: "13.0"
test "encode enforces bounds on xid8", context do
# xid8's range is 0 to +18_446_744_073_709_551_615
assert [[0]] = query("SELECT $1::xid", [0])
assert [[18_446_744_073_709_551_615]] = query("SELECT $1::xid8", [18_446_744_073_709_551_615])

assert %DBConnection.EncodeError{} =
catch_error(query("SELECT $1::xid8", [18_446_744_073_709_551_615 + 1]))

assert %DBConnection.EncodeError{} =
catch_error(query("SELECT $1::xid", [0 - 1]))
end

test "encode uuid", context do
uuid = <<0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15>>
assert [[^uuid]] = query("SELECT $1::uuid", [uuid])
Expand Down

0 comments on commit ad5716f

Please sign in to comment.