Repository navigation
feat(sinks): Add on-conflict config support to postgres sink #26628
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
base: master
Are you sure you want to change the base?
Changes from 1 commit
c3316fc
9c21ae9
fd4e550
6f4f4a1
13babd6
bc6916f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| Add an `on_conflict` option to the `postgres` sink. Setting this option to `do_nothing` appends `ON CONFLICT DO NOTHING` to the insert, so rows that violate a unique constraint are skipped instead of failing the whole batch. Skipped rows are reported as intentionally discarded events. The default, `error`, keeps the existing behavior. | ||
|
|
||
| authors: vaughnw128 |
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -11,10 +11,12 @@ use vector_lib::{ | |||||||||
| EstimatedJsonEncodedSizeOf, | ||||||||||
| codecs::JsonSerializerConfig, | ||||||||||
| event::{Event, EventFinalizers, EventStatus, Finalizable}, | ||||||||||
| internal_event::{ComponentEventsDropped, INTENTIONAL}, | ||||||||||
| request_metadata::{GroupedCountByteSize, MetaDescriptive, RequestMetadata}, | ||||||||||
| stream::DriverResponse, | ||||||||||
| }; | ||||||||||
|
|
||||||||||
| use super::config::OnConflict; | ||||||||||
| use crate::{ | ||||||||||
| internal_events::EndpointBytesSent, | ||||||||||
| sinks::prelude::{RequestMetadataBuilder, RetryLogic}, | ||||||||||
|
|
@@ -50,14 +52,21 @@ pub struct PostgresService { | |||||||||
| connection_pool: Pool<Postgres>, | ||||||||||
| table: String, | ||||||||||
| endpoint: String, | ||||||||||
| on_conflict: OnConflict, | ||||||||||
| } | ||||||||||
|
|
||||||||||
| impl PostgresService { | ||||||||||
| pub const fn new(connection_pool: Pool<Postgres>, table: String, endpoint: String) -> Self { | ||||||||||
| pub const fn new( | ||||||||||
| connection_pool: Pool<Postgres>, | ||||||||||
| table: String, | ||||||||||
| endpoint: String, | ||||||||||
| on_conflict: OnConflict, | ||||||||||
| ) -> Self { | ||||||||||
| Self { | ||||||||||
| connection_pool, | ||||||||||
| table, | ||||||||||
| endpoint, | ||||||||||
| on_conflict, | ||||||||||
| } | ||||||||||
| } | ||||||||||
| } | ||||||||||
|
|
@@ -143,6 +152,7 @@ impl Service<PostgresRequest> for PostgresService { | |||||||||
| let future = async move { | ||||||||||
| let table = service.table; | ||||||||||
| let metadata = request.metadata; | ||||||||||
| let event_count = request.events.len(); | ||||||||||
| let json_serializer = JsonSerializerConfig::default().build(); | ||||||||||
| let serialized_values = request | ||||||||||
| .events | ||||||||||
|
|
@@ -151,14 +161,29 @@ impl Service<PostgresRequest> for PostgresService { | |||||||||
| .collect::<Result<Vec<_>, _>>() | ||||||||||
| .context(VectorCommonSnafu)?; | ||||||||||
|
|
||||||||||
| sqlx::query(&format!( | ||||||||||
| "INSERT INTO {table} SELECT * FROM jsonb_populate_recordset(NULL::{table}, $1)" | ||||||||||
| let conflict_clause = match service.on_conflict { | ||||||||||
| OnConflict::Error => "", | ||||||||||
| OnConflict::DoNothing => " ON CONFLICT DO NOTHING", | ||||||||||
| }; | ||||||||||
|
|
||||||||||
| let result = sqlx::query(&format!( | ||||||||||
| "INSERT INTO {table} SELECT * FROM jsonb_populate_recordset(NULL::{table}, $1){conflict_clause}" | ||||||||||
| )) | ||||||||||
| .bind(Json(serialized_values)) | ||||||||||
| .execute(&service.connection_pool) | ||||||||||
| .await | ||||||||||
| .context(PostgresSnafu)?; | ||||||||||
|
|
||||||||||
| if service.on_conflict == OnConflict::DoNothing { | ||||||||||
| let skipped = event_count.saturating_sub(result.rows_affected() as usize); | ||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Whenever AGENTS.md reference: AGENTS.md:L210-L210 Useful? React with 👍 / 👎.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Confirmed this behavior with my debugging tests
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Useful comment, but I think it doesn't make much of an impact given the reporting provided by other similar sinks. A similar behavior can be seen in the New Relic sink: vector/src/sinks/new_relic/model.rs Lines 115 to 118 in 5606b68
|
||||||||||
| if skipped > 0 { | ||||||||||
| emit!(ComponentEventsDropped::<INTENTIONAL> { | ||||||||||
| count: skipped, | ||||||||||
| reason: "Row conflicts with an existing row.", | ||||||||||
| }); | ||||||||||
| } | ||||||||||
| } | ||||||||||
|
|
||||||||||
| emit!(EndpointBytesSent { | ||||||||||
| byte_size: metadata.request_encoded_size(), | ||||||||||
| protocol: POSTGRES_PROTOCOL, | ||||||||||
|
|
||||||||||
Uh oh!
There was an error while loading. Please reload this page.