Skip to content

Instantly share code, notes, and snippets.

@adrianlzt
Created February 9, 2023 08:11
Show Gist options
  • Save adrianlzt/916a0c641c62b5b5ca700a59b4730dab to your computer and use it in GitHub Desktop.
Save adrianlzt/916a0c641c62b5b5ca700a59b4730dab to your computer and use it in GitHub Desktop.
Zabbix: PostgreSQL query to migrate the old item prototype macros ($1) to its discovery macro
WITH macro AS (
SELECT
itemid,
(regexp_matches(key_, '(\{#[^\}]*\})'))[1] AS m
FROM
items)
UPDATE
items
SET
name = regexp_replace(name, '(\$1)', macro.m)
FROM
macro
WHERE
macro.itemid = items.itemid;
WITH macro AS (
SELECT
itemid,
(regexp_matches(key_, '\{#[^\}]*\},(\{#[^\}]*\})'))[1] AS m
FROM
items)
UPDATE
items
SET
name = regexp_replace(name, '(\$2)', macro.m)
FROM
macro
WHERE
macro.itemid = items.itemid;
WITH macro AS (
SELECT
itemid,
(regexp_matches(key_, '\{#[^\}]*\},\{#[^\}]*\},(\{#[^\}]*\})'))[1] AS m
FROM
items)
UPDATE
items
SET
name = regexp_replace(name, '(\$3)', macro.m)
FROM
macro
WHERE
macro.itemid = items.itemid;
WITH macro AS (
SELECT
itemid,
(regexp_matches(key_, '\{#[^\}]*\},\{#[^\}]*\},\{#[^\}]*\},(\{#[^\}]*\})'))[1] AS m
FROM
items)
UPDATE
items
SET
name = regexp_replace(name, '(\$4)', macro.m)
FROM
macro
WHERE
macro.itemid = items.itemid;
WITH macro AS (
SELECT
itemid,
(regexp_matches(key_, '\{#[^\}]*\},\{#[^\}]*\},\{#[^\}]*\},\{#[^\}]*\},(\{#[^\}]*\})'))[1] AS m
FROM
items)
UPDATE
items
SET
name = regexp_replace(name, '(\$5)', macro.m)
FROM
macro
WHERE
macro.itemid = items.itemid;
@adrianlzt
Copy link
Author

@adrianlzt
Copy link
Author

This just changes $1, $2, $3, $4 and $5, as I did not have positional macros for $6, etc.

To check if there are positional macros for $6:

select name from items where name like '%$6%' limit 1;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment