Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle primary key conflicts better (upserts or ignore) #135

Open
proddata opened this issue Feb 26, 2024 · 2 comments
Open

Handle primary key conflicts better (upserts or ignore) #135

proddata opened this issue Feb 26, 2024 · 2 comments
Labels
enhancement go Pull requests that update Go code

Comments

@proddata
Copy link
Member

Originated from community question: https://community.cratedb.com/t/prometheus-failed-to-write-data-into-cratedb-after-encountering-error-a-document-with-the-same-primary-key-exists-already/1724


We probably could add the option to use upserts or make it the even default approach.

Since the table definition is using primary keys we should change the
insert statement

const crateWriteStatement = `INSERT INTO metrics ("labels", "labels_hash", "timestamp", "value", "valueRaw") VALUES ($1, $2, $3, $4, $5)`

to either ignore conflicts:

INSERT INTO metrics ("labels", "labels_hash", "timestamp", "value", "valueRaw") VALUES ($1, $2, $3, $4, $5)
ON CONFLICT ("timestamp", "labels_hash", "day__generated")
DO NOTHING;

or handle upserts:

INSERT INTO metrics ("labels", "labels_hash", "timestamp", "value", "valueRaw") VALUES ($1, $2, $3, $4, $5)
ON CONFLICT ("timestamp", "labels_hash", "day__generated")
DO UPDATE SET
   "value" = excluded."value",
   "valueRaw" = excluded."valueRaw";

Potentially this could be configurable (to do either or).

@surister surister added enhancement go Pull requests that update Go code labels Feb 27, 2024
@surister
Copy link

Maybe adding an environment variable ON_CONFLICT with values ignore and upsert and make upsert default?

docker run -it --rm --publish=9268:9268 -e ON_CONFLICT=ignore ghcr.io/crate/cratedb-prometheus-adapter

@amotl
Copy link
Member

amotl commented Feb 27, 2024

@proddata: Thanks for reporting this, and for your suggestions. I agree with your proposal.

@surister: I also try to approach improvements to this program each time like this again, but then, reality strikes: The program currently doesn't do much with environment variables, opposed to what one would expect for a cloud-native program. The configuration is mostly handled on behalf of configuration file settings.

Because it is possible to configure multiple CrateDB endpoints there, i.e. the program implements a flavor of multi-tenancy on that dimension, it is mostly not applicable to use settings that would apply globally.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement go Pull requests that update Go code
Projects
None yet
Development

No branches or pull requests

3 participants