pgtype icon indicating copy to clipboard operation
pgtype copied to clipboard

adding multiple postgres interval formats

Open huehnerhose opened this issue 5 years ago • 3 comments

Since it's a postgres server side configuration which interval primarily is used (postgres, sql, postgres verbose, iso8601) the pg.Interval has to handle multiple "input" formats.

I needed iso8601 support (fast). That PR is more a POC to determine which format the server uses and implementing a POC-style iso8601 parsing, which only supports secods as resolution.

ToDo here would be:

  • [ ] verify format detection
  • [ ] build a testable format switch (I had to reconfigure and restart postgres)
  • [ ] implement postgres verbose and sql standard parsing
  • [ ] properly implement iso8601 parsing

huehnerhose avatar Aug 04 '20 11:08 huehnerhose

A few thoughts: If you use the binary format you don't have to worry about interval format. But if you need to work with the text format it would be nice to avoid detecting the format each time. I've considered adding adding the server parameters to ConnInfo. IIRC the server reports IntervalStyle on connection and whenever it is changed. That could make things simpler.

jackc avatar Aug 04 '20 14:08 jackc

Thanks for the input, but: What do you mean by using the binary format? For background, I stumbled in this whole topic because of an scanning error with a differently configured server, using an older version von pgx/pgtype (3.something)

If there is a better way omitting the textformat altogether, I am all in!

Detecting the text format by not using regex would be great, too ;)

huehnerhose avatar Aug 05 '20 10:08 huehnerhose

PostgreSQL has two transfer formats for almost all data types: a text format and a binary format. The text format is what you see in psql and what is affected by the various set options (e.g. IntervalStyle). For the interval type the format is microseconds as an int64, days as a int32, and months as a int32. This 16 byte binary format is much simpler and faster to parse than the text format -- and is unaffected by IntervalStyle.

pgx uses the binary format by default, so unless the text format is chosen for some reason it should "just work" without any changes.

jackc avatar Aug 05 '20 22:08 jackc