Skip to content

Instantly share code, notes, and snippets.

@crmaxx
Last active August 29, 2015 14:23
Show Gist options
  • Save crmaxx/cff2c76666c5304339cc to your computer and use it in GitHub Desktop.
Save crmaxx/cff2c76666c5304339cc to your computer and use it in GitHub Desktop.
BEGIN;
LOCK TABLE netscanner_host_statistics IN SHARE ROW EXCLUSIVE MODE;
WITH upsert AS (UPDATE netscanner_host_statistics SET count_of_hosts = count_of_hosts + 1, updated_at = now() WHERE (netscanner_host_statistics.name = 'ya.ru' AND netscanner_host_statistics.workspace_id = 1) RETURNING *) INSERT INTO netscanner_host_statistics (name, workspace_id, count_of_hosts, created_at, updated_at) SELECT 'ya.ru', 1, now(), now() WHERE NOT EXISTS (SELECT * FROM upsert);
INSERT INTO netscanner_hosts_host_statistics (host_id, host_statistic_id) VALUES (398, ?)
COMMIT;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment