mirror of
https://github.com/jackc/pgx.git
synced 2025-05-31 11:42:24 +00:00
Add binary encoding for timestamptz
This commit is contained in:
parent
faed7f2879
commit
9e321af35c
@ -15,6 +15,7 @@ var int8TextVsBinaryTestDataLoaded bool
|
|||||||
var float4TextVsBinaryTestDataLoaded bool
|
var float4TextVsBinaryTestDataLoaded bool
|
||||||
var float8TextVsBinaryTestDataLoaded bool
|
var float8TextVsBinaryTestDataLoaded bool
|
||||||
var boolTextVsBinaryTestDataLoaded bool
|
var boolTextVsBinaryTestDataLoaded bool
|
||||||
|
var timestampTzTextVsBinaryTestDataLoaded bool
|
||||||
|
|
||||||
func createNarrowTestData(b *testing.B, conn *pgx.Connection) {
|
func createNarrowTestData(b *testing.B, conn *pgx.Connection) {
|
||||||
if narrowTestDataLoaded {
|
if narrowTestDataLoaded {
|
||||||
@ -544,3 +545,60 @@ func BenchmarkBoolBinary(b *testing.B) {
|
|||||||
mustSelectRows(b, conn, "selectBool")
|
mustSelectRows(b, conn, "selectBool")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func createTimestampTzTextVsBinaryTestData(b *testing.B, conn *pgx.Connection) {
|
||||||
|
if timestampTzTextVsBinaryTestDataLoaded {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
mustExecute(b, conn, `
|
||||||
|
drop table if exists t;
|
||||||
|
|
||||||
|
create temporary table t(
|
||||||
|
a timestamptz not null,
|
||||||
|
b timestamptz not null,
|
||||||
|
c timestamptz not null,
|
||||||
|
d timestamptz not null,
|
||||||
|
e timestamptz not null
|
||||||
|
);
|
||||||
|
|
||||||
|
insert into t(a, b, c, d, e)
|
||||||
|
select
|
||||||
|
now() - '10 years'::interval * random(),
|
||||||
|
now() - '10 years'::interval * random(),
|
||||||
|
now() - '10 years'::interval * random(),
|
||||||
|
now() - '10 years'::interval * random(),
|
||||||
|
now() - '10 years'::interval * random()
|
||||||
|
from generate_series(1, 10);
|
||||||
|
`)
|
||||||
|
|
||||||
|
timestampTzTextVsBinaryTestDataLoaded = true
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkTimestampTzText(b *testing.B) {
|
||||||
|
conn := getSharedConnection()
|
||||||
|
createTimestampTzTextVsBinaryTestData(b, conn)
|
||||||
|
|
||||||
|
encoders := removeBinaryEncoders()
|
||||||
|
defer func() { restoreBinaryEncoders(encoders) }()
|
||||||
|
|
||||||
|
mustPrepare(b, conn, "selectTimestampTz", "select * from t")
|
||||||
|
defer func() { conn.Deallocate("selectTimestampTz") }()
|
||||||
|
|
||||||
|
b.ResetTimer()
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
mustSelectRows(b, conn, "selectTimestampTz")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkTimestampTzBinary(b *testing.B) {
|
||||||
|
conn := getSharedConnection()
|
||||||
|
createTimestampTzTextVsBinaryTestData(b, conn)
|
||||||
|
mustPrepare(b, conn, "selectTimestampTz", "select * from t")
|
||||||
|
defer func() { conn.Deallocate("selectTimestampTz") }()
|
||||||
|
|
||||||
|
b.ResetTimer()
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
mustSelectRows(b, conn, "selectTimestampTz")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -97,6 +97,7 @@ func init() {
|
|||||||
// timestamptz
|
// timestamptz
|
||||||
ValueTranscoders[Oid(1184)] = &ValueTranscoder{
|
ValueTranscoders[Oid(1184)] = &ValueTranscoder{
|
||||||
DecodeText: decodeTimestampTzFromText,
|
DecodeText: decodeTimestampTzFromText,
|
||||||
|
DecodeBinary: decodeTimestampTzFromBinary,
|
||||||
EncodeTo: encodeTimestampTz}
|
EncodeTo: encodeTimestampTz}
|
||||||
|
|
||||||
// use text transcoder for anything we don't understand
|
// use text transcoder for anything we don't understand
|
||||||
@ -299,6 +300,20 @@ func decodeTimestampTzFromText(mr *MessageReader, size int32) interface{} {
|
|||||||
return t
|
return t
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func decodeTimestampTzFromBinary(mr *MessageReader, size int32) interface{} {
|
||||||
|
if size != 8 {
|
||||||
|
panic("Received an invalid size for an int8")
|
||||||
|
}
|
||||||
|
microsecFromUnixEpochToY2K := int64(946684800 * 1000000)
|
||||||
|
microsecSinceY2K := mr.ReadInt64()
|
||||||
|
microsecSinceUnixEpoch := microsecFromUnixEpochToY2K + microsecSinceY2K
|
||||||
|
return time.Unix(microsecSinceUnixEpoch/1000000, (microsecSinceUnixEpoch%1000000)*1000)
|
||||||
|
|
||||||
|
// 2000-01-01 00:00:00 in 946684800
|
||||||
|
// 946684800 * 1000000
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
func encodeTimestampTz(w *MessageWriter, value interface{}) {
|
func encodeTimestampTz(w *MessageWriter, value interface{}) {
|
||||||
t := value.(time.Time)
|
t := value.(time.Time)
|
||||||
s := t.Format("2006-01-02 15:04:05.999999 -0700")
|
s := t.Format("2006-01-02 15:04:05.999999 -0700")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user