Ik heb de volgende functie om mijn host tabel te updaten en wat op te ruimen (ivm preformance).
Nu gaat dat bij een heleboel hosts en ranges prima, echter bij sommige krijg ik de volgende foutmelding (Regelnummering is vanaf AS):
Maar volgens mij KAN dat niet, behalve als hij op het momen van het opleggen van die PK nog niet klaar is met het deleten...
Iemand een idee? En als mijn theorie klopt, hoe los ik dat dan op?
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
| CREATE FUNCTION update_host(varchar,cidr)
RETURNS text
AS 'DECLARE
in_new_hostname ALIAS FOR $1;
in_new_ip ALIAS FOR $2;
Max_lastseen timestamp without time zone;
Sum_total integer;
new_hostid integer;
-- This function is to be used to update a hostname, it wil create a new hostID and copy all data to that host ID
-- The will reslut in some less detail information about the hosts that flushed, but wil increase the preformance
-- If the loss of detail is more important then the preformance, normal updates on the hosts table should be used
BEGIN
--lock tables
LOCK hosts, blocks, sum_blocks, dupes IN ACCESS EXCLUSIVE MODE;
--get Max_lastseen and Sum_total to create a new hostid
SELECT INTO Max_lastseen max(lastseen)
FROM hosts
WHERE hostip <<= in_new_ip;
SELECT INTO Sum_total sum(total)
FROM hosts
WHERE hostip <<= in_new_ip;
INSERT INTO hosts (hostip,hostname,lastseen,total)
VALUES (in_new_ip,in_new_hostname,Max_lastseen,Sum_total);
--get the new hostid
SELECT INTO new_hostid last_value
FROM hosts_hostid_seq;
--fix the blocks table
UPDATE blocks set hostid=new_hostid
where hostid in
(select hostid from hosts where hostip <<= in_new_ip and hostid <> new_hostid);
--and the dupes table
UPDATE dupes set hostid=new_hostid
where hostid in
(select hostid from hosts where hostip <<= in_new_ip and hostid <> new_hostid);
--to fix sum_blocks the PK has to be droped, then the update can take place
--After that an agragation is done and the result is put in a temp table
--the old record will then be delete from the sum_blcoks table and the data from the temp table is copied
--At last, the temp table is removed and the old hosts are removed
ALTER TABLE sum_blocks DROP CONSTRAINT sum_blocks_pkey;
UPDATE sum_blocks set hostid=new_hostid
where hostid in
(select hostid from hosts where hostip <<= in_new_ip and hostid <> new_hostid);
CREATE TEMP TABLE s_blocks as
Select blockdate, blockhour, hostid, email, osid, cpuid, version, core, sum(amount) as amount
from sum_blocks
where hostid = new_hostid
group by blockdate, blockhour, hostid, email, osid, cpuid, version, core;
DELETE from sum_blocks where hostid=new_hostid;
ALTER TABLE sum_blocks ADD PRIMARY KEY (blockdate, blockhour, hostid, email, osid, cpuid, "version", core);
INSERT INTO sum_blocks (blockdate, blockhour, hostid, email, osid, cpuid, version, core, amount)
SELECT blockdate, blockhour, hostid, email, osid, cpuid, version, core, amount from s_blocks;
DROP TABLE s_blocks;
DELETE from hosts
where hostip <<= in_new_ip and hostid <> new_hostid;
RETURN (''OK'');
END;'
LANGUAGE 'plpgsql'; |
Nu gaat dat bij een heleboel hosts en ranges prima, echter bij sommige krijg ik de volgende foutmelding (Regelnummering is vanaf AS):
code:
1
2
3
| ERROR: Cannot create unique index. Table contains non-unique values WARNING: Error occurred while executing PL/pgSQL function update_host WARNING: line 50 at SQL statement |
Maar volgens mij KAN dat niet, behalve als hij op het momen van het opleggen van die PK nog niet klaar is met het deleten...
Iemand een idee? En als mijn theorie klopt, hoe los ik dat dan op?
Cupra Born