freevova

freevova

Postgrex error(citext extension) from Broadway Kafka consumer

Hi there :wave:

I have a Broadway Kafka Consumer, which consumes some data from Kafka:

defmodule KafkaConsumer do
  use Broadway
  alias Broadway.{Message, BatchInfo}
  alias Company

  def start_link(opts) do
    Broadway.start_link(__MODULE__,
      name: __MODULE__,
      producer: [
        module: {
          producer,
          [
            group_id: group_id,
            hosts: hosts,
            topics: ...
          ]
        },
        concurrency: 1
      ],
      processors: [
        default: [
          concurrency: 3
        ]
      ],
      batchers: [
        default: [],
        insert_all: [
          batch_size: 10,
          batch_timeout: 1_000
        ]
      ]
    )
  end
  ...
end

There is a table in my PostgreSQL DB with a unique name field. The type of that field is citext.
My app consumes names from Kafka and tries to insert them into DB with the INSERT ON CONFLICT query, based on the unique name constraint. For that I have insert_all batcher.
Sometimes I can receive next names:

names = ["İsbir Yatak", "Isbir Yatak"]

The only difference between these 2 is the accent on the first letter. But it turns out that these two names refer to the same value for the citext type. To exclude the next exception:

  ** (Postgrex.Error) ERROR 21000 (cardinality_violation) ON CONFLICT DO UPDATE command cannot affect row a second time

I’ve created the following function:

  @spec name_to_citext_name([String.t()]) :: %{String.t() => String.t()}
  def name_to_citext_name(names) do
    sql = """
    SELECT name, data.distinct_name
    FROM UNNEST($1::citext[]) name
    INNER JOIN (
      SELECT DISTINCT distinct_name
      FROM UNNEST($1::citext[]) distinct_name
    ) data on name = data.distinct_name;
    """

    Repo
    |> Ecto.Adapters.SQL.query!(sql, [names])
    |> Map.get(:rows)
    |> Map.new(fn [name, citext_name] -> {name, citext_name} end)
  end

This function prepares unique names, based on citext type, which I pass to the INSERT ON CONFLICT query. It works as expected, but sometimes, in very rare cases, I receive the next error:

    (Postgrex.Error ERROR 42704 (undefined_object) type "citext[]" does not exist

I tried to intercept the exception and log the results of the next queries:

SELECT * FROM pg_extension;
SELECT oid FROM pg_database WHERE datname = (SELECT current_database() AS name);

The results of these queries are the same, as when I connect to the PostgreSQL directly and manually perform queries.
Also, I’ve noticed that sometimes I can get the next error:

(Postgrex.Error ERROR 42P01 (undefined_table) relation "..." does not exist)

from the other consumer. I suppose the issue here is the same.

I will be very grateful if someone can help in any way, or tell me where to dig further

Where Next?

Popular in Questions Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
dokuzbir
Hello, I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these buyer = %{ id: ...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
hpopp
To simplify some tasks at work, I wrote and published this package yesterday. It’s a simple macro that enables Access behaviour on struct...
New
jc00ke
Expanding on this topic: https://forum.elixirforum.net/t/map-typespec-question/19217 Let’s say I have a map with required and optional k...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

Other popular topics Top

sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement