Skip to content
This repository was archived by the owner on Apr 2, 2024. It is now read-only.

Optimize insert path in the DB by avoiding ON CONFLICT #1090

Merged
merged 1 commit into from
Feb 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ We use the following categories for changes:
### Fixed
- Fix broken `promscale_packager` telemetry field for docker envs [#1077]
- Fix compression of old chunks thus reducing storage requirements [#1081]
- Improved INSERT performance by avoidng ON CONFLICT [#1090]

## [0.8.0] - 2022-01-18

Expand Down
4 changes: 2 additions & 2 deletions pkg/migrations/migration_files_generated.go

Large diffs are not rendered by default.

13 changes: 12 additions & 1 deletion pkg/migrations/sql/idempotent/base.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3067,13 +3067,24 @@ $$
DECLARE
num_rows BIGINT;
BEGIN
--turns out there is a horrible CPU perf penalty on the DB for ON CONFLICT DO NOTHING.
--yet in our data, conflicts are rare. So we first try inserting without ON CONFLICT
--and fall back if there is a unique constraint violation.
EXECUTE FORMAT(
'INSERT INTO prom_data.%1$I (time, value, series_id)
SELECT * FROM unnest($1, $2, $3) a(t,v,s) ORDER BY s,t ON CONFLICT DO NOTHING',
SELECT * FROM unnest($1, $2, $3) a(t,v,s) ORDER BY s,t',
metric_table
) USING time_array, value_array, series_id_array;
GET DIAGNOSTICS num_rows = ROW_COUNT;
RETURN num_rows;
EXCEPTION WHEN unique_violation THEN
EXECUTE FORMAT(
'INSERT INTO prom_data.%1$I (time, value, series_id)
SELECT * FROM unnest($1, $2, $3) a(t,v,s) ORDER BY s,t ON CONFLICT DO NOTHING',
metric_table
) USING time_array, value_array, series_id_array;
GET DIAGNOSTICS num_rows = ROW_COUNT;
RETURN num_rows;
END;
$$
LANGUAGE PLPGSQL;
Expand Down