Skip to content

Commit

Permalink
outputs/postgresql: change config param use_uint8 to uint64_type
Browse files Browse the repository at this point in the history
In the future, there might be other options for handling uint8 types (e.g. using doubles). This change makes the config forward-compatible for such options.
  • Loading branch information
phemmer committed Jun 2, 2022
1 parent ffaa5ce commit dd0e5bb
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 9 deletions.
26 changes: 26 additions & 0 deletions plugins/outputs/postgresql/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# This Dockerfile can be used to build an image including the pguint extension.
#
# docker build -t postgres:pguint .
# docker run -d -t postgres -p 5432:5432 -e POSTGRES_HOST_AUTH_METHOD=trust postgres:pguint
# docker logs -f postgres 2>&1 | grep -q 'listening on IPv4 address "0.0.0.0", port 5432'
# go test -v

# Tag from https://hub.docker.com/_/postgres?tab=tags
ARG POSTGRES_TAG=latest

ARG PGUINT_REPO
ARG PGUINT_RELEASE

FROM postgres:${POSTGRES_TAG}

RUN apt-get update && apt-get install -y build-essential curl postgresql-server-dev-${PG_MAJOR}=${PG_VERSION}

ENV PGUINT_REPO=${PGUINT_REPO:-phemmer/pguint}
ENV PGUINT_REF=${PGUINT_REF:-fix-getmsgint64}
RUN mkdir /pguint && cd /pguint && \
curl -L https://github.com/${PGUINT_REPO}/tarball/${PGUINT_REF} | tar -zx --strip-components=1 && \
make && make install && \
echo 'CREATE EXTENSION uint;' > /docker-entrypoint-initdb.d/uint.sql && \
echo '\\c template1' >> /docker-entrypoint-initdb.d/uint.sql && \
echo 'CREATE EXTENSION uint;' >> /docker-entrypoint-initdb.d/uint.sql

8 changes: 6 additions & 2 deletions plugins/outputs/postgresql/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,12 @@ The plugin manages the schema, automatically updating missing columns.
# '''ALTER TABLE {{.table}} ADD COLUMN IF NOT EXISTS {{.columns|join ", ADD COLUMN IF NOT EXISTS "}}''',
# ]

## Controls whether to use the uint8 data type provided by the pguint extension.
# use_uint8 = false
## The postgres data type to use for storing unsigned 64-bit integer values as Postgres does not have a native
## unsigned 64-bit integer type.
## The value can be one of:
## numeric - Uses the PostgreSQL "numeric" data type.
## uint8 - Requires pguint extension (https://github.com/petere/pguint)
# uint64_type = "numeric"

## When using pool_max_conns>1, and a temporary error occurs, the query is retried with an incremental backoff. This
## controls the maximum backoff duration.
Expand Down
2 changes: 1 addition & 1 deletion plugins/outputs/postgresql/datatypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const (
// DerivePgDatatype returns the appropriate PostgreSQL data type
// that could hold the value.
func (p *Postgresql) derivePgDatatype(value interface{}) string {
if p.UseUint8 {
if p.Uint64Type == PgUint8 {
if _, ok := value.(uint64); ok {
return PgUint8
}
Expand Down
19 changes: 15 additions & 4 deletions plugins/outputs/postgresql/postgresql.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,12 @@ var sampleConfig = `
# '''ALTER TABLE {{.table}} ADD COLUMN IF NOT EXISTS {{.columns|join ", ADD COLUMN IF NOT EXISTS "}}''',
# ]
## Controls whether to use the uint8 data type provided by the pguint extension.
# use_uint8 = false
## The postgres data type to use for storing unsigned 64-bit integer values as Postgres does not have a native
## unsigned 64-bit integer type.
## The value can be one of:
## numeric - Uses the PostgreSQL "numeric" data type.
## uint8 - Requires pguint extension (https://github.com/petere/pguint)
# uint64_type = "numeric"
## When using pool_max_conns>1, and a temporary error occurs, the query is retried with an incremental backoff. This
## controls the maximum backoff duration.
Expand All @@ -117,7 +121,7 @@ type Postgresql struct {
AddColumnTemplates []*sqltemplate.Template `toml:"add_column_templates"`
TagTableCreateTemplates []*sqltemplate.Template `toml:"tag_table_create_templates"`
TagTableAddColumnTemplates []*sqltemplate.Template `toml:"tag_table_add_column_templates"`
UseUint8 bool `toml:"use_uint8"`
Uint64Type string `toml:"uint64_type"`
RetryMaxBackoff config.Duration `toml:"retry_max_backoff"`
TagCacheSize int `toml:"tag_cache_size"`
LogLevel string `toml:"log_level"`
Expand Down Expand Up @@ -223,8 +227,15 @@ func (p *Postgresql) Init() error {
}
}

if p.UseUint8 {
switch p.Uint64Type {
case "":
p.Uint64Type = PgNumeric
fallthrough
case PgNumeric:
case PgUint8:
p.dbConfig.AfterConnect = p.registerUint8
default:
return fmt.Errorf("invalid uint64_type")
}

return nil
Expand Down
2 changes: 1 addition & 1 deletion plugins/outputs/postgresql/postgresql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -717,7 +717,7 @@ func TestWrite_utf8(t *testing.T) {

func TestWrite_UnsignedIntegers(t *testing.T) {
p := newPostgresqlTest(t)
p.UseUint8 = true
p.Uint64Type = PgUint8
_ = p.Init()
if err := p.Connect(); err != nil {
if strings.Contains(err.Error(), "retreiving OID for uint8 data type") {
Expand Down
2 changes: 1 addition & 1 deletion plugins/outputs/postgresql/table_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ func TestTableManager_MatchSource(t *testing.T) {

func TestTableManager_MatchSource_UnsignedIntegers(t *testing.T) {
p := newPostgresqlTest(t)
p.UseUint8 = true
p.Uint64Type = PgUint8
_ = p.Init()
if err := p.Connect(); err != nil {
if strings.Contains(err.Error(), "retreiving OID for uint8 data type") {
Expand Down

0 comments on commit dd0e5bb

Please sign in to comment.