Skip to content

Can't use non-latin symbols with pq.CopyIn for jsonb type #1023

@wuzzapcom

Description

@wuzzapcom

Description

I'm implementing bulk insert using pq.CopyIn feature. One of column is jsonb type and it contain non-latin symbols, for example:

{ "text": {
  "title": "название"
  }
}

This json can be inserted with regular INSERT.
When I've tried to wrap the code with pq.CopyIn, I got the following error:

invalid input syntax for type json

When I insert same json with latin-only strings, all works properly.

code example

func bulk() {
	tx, err := db.Begin()

	stmt, err := tx.Prepare(pq.CopyIn(
		"example", "payload_jsonb",
	))

	stmt.Exec(Payload{Title: "название"})

        stmt.Exec()
}

what I want to see

Inserted successfully

what I get

invalid input syntax for type json

Same code with Payload{Title: "title"} inserts successfully.

Research

I've discovered that this issue happens because input in pq.CopyIn mode is encoded as text using appendEncodedText.
So cyrillic symbols are encoded like this:

{
    "text": {
        "title":"\321\202\320\260\320\271\321\202\320\273"
     }
}

Apparently PostgreSQL does not accept such json and returns the error.
This is the reason why no issue appears on same json with latin value of the field title.

On the other hand I've checked how regular INSERT handle this case. It uses encode. So json is just not encoded with encodeBytea.

Are there any workarounds for this issue?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions