annotate NOTES @ 433:5964ce6385a5 pg-transactions

use temporary tables for DeletedFiles, RemainingAncestor and DeletedResources
author Alain Mazy <am@osimis.io>
date Wed, 13 Dec 2023 15:48:56 +0100
parents 8b7c1c423367
children 23c7af6f671a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
431
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
1 Resources:
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
2 *********
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
3 - PG transaction modes explained: https://www.postgresql.org/files/developer/concurrency.pdf
433
5964ce6385a5 use temporary tables for DeletedFiles, RemainingAncestor and DeletedResources
Alain Mazy <am@osimis.io>
parents: 432
diff changeset
4 - Isolation level explained (PG + MySQL): https://amirsoleimani.medium.com/understanding-database-isolation-level-via-examples-mysql-and-postgres-a86b5502d404
5964ce6385a5 use temporary tables for DeletedFiles, RemainingAncestor and DeletedResources
Alain Mazy <am@osimis.io>
parents: 432
diff changeset
5 - Message queuing in PG: https://www.crunchydata.com/blog/message-queuing-using-native-postgresql
5964ce6385a5 use temporary tables for DeletedFiles, RemainingAncestor and DeletedResources
Alain Mazy <am@osimis.io>
parents: 432
diff changeset
6
431
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
7
432
8b7c1c423367 new 'TransactionMode' config + rewrote ResourceDeletedFunc to avoid IF/THEN/ELSE pattern
Alain Mazy <am@osimis.io>
parents: 431
diff changeset
8 Create and delete instances Internals:
8b7c1c423367 new 'TransactionMode' config + rewrote ResourceDeletedFunc to avoid IF/THEN/ELSE pattern
Alain Mazy <am@osimis.io>
parents: 431
diff changeset
9 *************************************
8b7c1c423367 new 'TransactionMode' config + rewrote ResourceDeletedFunc to avoid IF/THEN/ELSE pattern
Alain Mazy <am@osimis.io>
parents: 431
diff changeset
10
8b7c1c423367 new 'TransactionMode' config + rewrote ResourceDeletedFunc to avoid IF/THEN/ELSE pattern
Alain Mazy <am@osimis.io>
parents: 431
diff changeset
11 isNewInstance = CreateInstance(...)
8b7c1c423367 new 'TransactionMode' config + rewrote ResourceDeletedFunc to avoid IF/THEN/ELSE pattern
Alain Mazy <am@osimis.io>
parents: 431
diff changeset
12
8b7c1c423367 new 'TransactionMode' config + rewrote ResourceDeletedFunc to avoid IF/THEN/ELSE pattern
Alain Mazy <am@osimis.io>
parents: 431
diff changeset
13 if (!isNewInstance && overwriteInstances)
8b7c1c423367 new 'TransactionMode' config + rewrote ResourceDeletedFunc to avoid IF/THEN/ELSE pattern
Alain Mazy <am@osimis.io>
parents: 431
diff changeset
14 DeleteResource(instance)
8b7c1c423367 new 'TransactionMode' config + rewrote ResourceDeletedFunc to avoid IF/THEN/ELSE pattern
Alain Mazy <am@osimis.io>
parents: 431
diff changeset
15 -> ClearDeletedFiles(manager);
8b7c1c423367 new 'TransactionMode' config + rewrote ResourceDeletedFunc to avoid IF/THEN/ELSE pattern
Alain Mazy <am@osimis.io>
parents: 431
diff changeset
16 DELETE FROM DeletedFiles ------> this is not a temporary table in postgres but it is supposed to be empty before and after each transaction -> it is empty when taking a snapshot of the DB in READ COMMITTED mode!!!
8b7c1c423367 new 'TransactionMode' config + rewrote ResourceDeletedFunc to avoid IF/THEN/ELSE pattern
Alain Mazy <am@osimis.io>
parents: 431
diff changeset
17 ClearDeletedResources(manager);
8b7c1c423367 new 'TransactionMode' config + rewrote ResourceDeletedFunc to avoid IF/THEN/ELSE pattern
Alain Mazy <am@osimis.io>
parents: 431
diff changeset
18 DELETE FROM DeletedResources ------> this is not a temporary table in postgres but it is supposed to be empty before and after each transaction !!!
8b7c1c423367 new 'TransactionMode' config + rewrote ResourceDeletedFunc to avoid IF/THEN/ELSE pattern
Alain Mazy <am@osimis.io>
parents: 431
diff changeset
19
8b7c1c423367 new 'TransactionMode' config + rewrote ResourceDeletedFunc to avoid IF/THEN/ELSE pattern
Alain Mazy <am@osimis.io>
parents: 431
diff changeset
20 DELETE FROM RemainingAncestor ------> this is not a temporary table in postgres but it is supposed to be empty before and after each transaction !!!
8b7c1c423367 new 'TransactionMode' config + rewrote ResourceDeletedFunc to avoid IF/THEN/ELSE pattern
Alain Mazy <am@osimis.io>
parents: 431
diff changeset
21 DELETE FROM Resources WHERE internalId=..
8b7c1c423367 new 'TransactionMode' config + rewrote ResourceDeletedFunc to avoid IF/THEN/ELSE pattern
Alain Mazy <am@osimis.io>
parents: 431
diff changeset
22 -> cascades delete the MainDicomTags, the Metadata and the AttachedFiles
8b7c1c423367 new 'TransactionMode' config + rewrote ResourceDeletedFunc to avoid IF/THEN/ELSE pattern
Alain Mazy <am@osimis.io>
parents: 431
diff changeset
23 -> this triggers AttachedFileDeletedFunc
8b7c1c423367 new 'TransactionMode' config + rewrote ResourceDeletedFunc to avoid IF/THEN/ELSE pattern
Alain Mazy <am@osimis.io>
parents: 431
diff changeset
24 INSERT INTO DeletedFiles VALUES
8b7c1c423367 new 'TransactionMode' config + rewrote ResourceDeletedFunc to avoid IF/THEN/ELSE pattern
Alain Mazy <am@osimis.io>
parents: 431
diff changeset
25 (old.uuid, old.filetype, old.compressedSize,
8b7c1c423367 new 'TransactionMode' config + rewrote ResourceDeletedFunc to avoid IF/THEN/ELSE pattern
Alain Mazy <am@osimis.io>
parents: 431
diff changeset
26 old.uncompressedSize, old.compressionType,
8b7c1c423367 new 'TransactionMode' config + rewrote ResourceDeletedFunc to avoid IF/THEN/ELSE pattern
Alain Mazy <am@osimis.io>
parents: 431
diff changeset
27 old.uncompressedHash, old.compressedHash);
8b7c1c423367 new 'TransactionMode' config + rewrote ResourceDeletedFunc to avoid IF/THEN/ELSE pattern
Alain Mazy <am@osimis.io>
parents: 431
diff changeset
28 RETURN NULL;
8b7c1c423367 new 'TransactionMode' config + rewrote ResourceDeletedFunc to avoid IF/THEN/ELSE pattern
Alain Mazy <am@osimis.io>
parents: 431
diff changeset
29 -> this triggers a SQL trigger: ResourceDeletedFunc
8b7c1c423367 new 'TransactionMode' config + rewrote ResourceDeletedFunc to avoid IF/THEN/ELSE pattern
Alain Mazy <am@osimis.io>
parents: 431
diff changeset
30 INSERT INTO DeletedResources VALUES (old.resourceType, old.publicId);
8b7c1c423367 new 'TransactionMode' config + rewrote ResourceDeletedFunc to avoid IF/THEN/ELSE pattern
Alain Mazy <am@osimis.io>
parents: 431
diff changeset
31 IF EXISTS (SELECT 1 FROM Resources WHERE parentId = old.parentId) THEN
8b7c1c423367 new 'TransactionMode' config + rewrote ResourceDeletedFunc to avoid IF/THEN/ELSE pattern
Alain Mazy <am@osimis.io>
parents: 431
diff changeset
32 -- Signal that the deleted resource has a remaining parent
8b7c1c423367 new 'TransactionMode' config + rewrote ResourceDeletedFunc to avoid IF/THEN/ELSE pattern
Alain Mazy <am@osimis.io>
parents: 431
diff changeset
33 -- (a parent that must not be deleted but whose LastUpdate must be updated)
8b7c1c423367 new 'TransactionMode' config + rewrote ResourceDeletedFunc to avoid IF/THEN/ELSE pattern
Alain Mazy <am@osimis.io>
parents: 431
diff changeset
34 INSERT INTO RemainingAncestor
8b7c1c423367 new 'TransactionMode' config + rewrote ResourceDeletedFunc to avoid IF/THEN/ELSE pattern
Alain Mazy <am@osimis.io>
parents: 431
diff changeset
35 SELECT resourceType, publicId FROM Resources WHERE internalId = old.parentId;
8b7c1c423367 new 'TransactionMode' config + rewrote ResourceDeletedFunc to avoid IF/THEN/ELSE pattern
Alain Mazy <am@osimis.io>
parents: 431
diff changeset
36 ELSE
8b7c1c423367 new 'TransactionMode' config + rewrote ResourceDeletedFunc to avoid IF/THEN/ELSE pattern
Alain Mazy <am@osimis.io>
parents: 431
diff changeset
37 -- Delete a parent resource when its unique child is deleted
8b7c1c423367 new 'TransactionMode' config + rewrote ResourceDeletedFunc to avoid IF/THEN/ELSE pattern
Alain Mazy <am@osimis.io>
parents: 431
diff changeset
38 DELETE FROM Resources WHERE internalId = old.parentId;
8b7c1c423367 new 'TransactionMode' config + rewrote ResourceDeletedFunc to avoid IF/THEN/ELSE pattern
Alain Mazy <am@osimis.io>
parents: 431
diff changeset
39 END IF;
8b7c1c423367 new 'TransactionMode' config + rewrote ResourceDeletedFunc to avoid IF/THEN/ELSE pattern
Alain Mazy <am@osimis.io>
parents: 431
diff changeset
40
8b7c1c423367 new 'TransactionMode' config + rewrote ResourceDeletedFunc to avoid IF/THEN/ELSE pattern
Alain Mazy <am@osimis.io>
parents: 431
diff changeset
41 SELECT * FROM RemainingAncestor
8b7c1c423367 new 'TransactionMode' config + rewrote ResourceDeletedFunc to avoid IF/THEN/ELSE pattern
Alain Mazy <am@osimis.io>
parents: 431
diff changeset
42 -> SignalRemainingAncestor() // There is at most 1 remaining ancestor
8b7c1c423367 new 'TransactionMode' config + rewrote ResourceDeletedFunc to avoid IF/THEN/ELSE pattern
Alain Mazy <am@osimis.io>
parents: 431
diff changeset
43 -> ServerIndex::TransactionContext::SignalRemainingAncestor()
8b7c1c423367 new 'TransactionMode' config + rewrote ResourceDeletedFunc to avoid IF/THEN/ELSE pattern
Alain Mazy <am@osimis.io>
parents: 431
diff changeset
44 -> stores remainingType and remainingPublicId (this is used in StatelessDatabaseOperations::DeleteResource to build the Rest Response of /delete
8b7c1c423367 new 'TransactionMode' config + rewrote ResourceDeletedFunc to avoid IF/THEN/ELSE pattern
Alain Mazy <am@osimis.io>
parents: 431
diff changeset
45 and to update the LastUpdate of all parent (only when deleted from /delete))
8b7c1c423367 new 'TransactionMode' config + rewrote ResourceDeletedFunc to avoid IF/THEN/ELSE pattern
Alain Mazy <am@osimis.io>
parents: 431
diff changeset
46
8b7c1c423367 new 'TransactionMode' config + rewrote ResourceDeletedFunc to avoid IF/THEN/ELSE pattern
Alain Mazy <am@osimis.io>
parents: 431
diff changeset
47 SignalDeletedFiles(output, manager);
8b7c1c423367 new 'TransactionMode' config + rewrote ResourceDeletedFunc to avoid IF/THEN/ELSE pattern
Alain Mazy <am@osimis.io>
parents: 431
diff changeset
48 SELECT * FROM DeletedFiles
8b7c1c423367 new 'TransactionMode' config + rewrote ResourceDeletedFunc to avoid IF/THEN/ELSE pattern
Alain Mazy <am@osimis.io>
parents: 431
diff changeset
49 -> SignalDeletedAttachment()
8b7c1c423367 new 'TransactionMode' config + rewrote ResourceDeletedFunc to avoid IF/THEN/ELSE pattern
Alain Mazy <am@osimis.io>
parents: 431
diff changeset
50 -> ServerIndex::TransactionContext::SignalAttachmentDeleted()
8b7c1c423367 new 'TransactionMode' config + rewrote ResourceDeletedFunc to avoid IF/THEN/ELSE pattern
Alain Mazy <am@osimis.io>
parents: 431
diff changeset
51 -> pendingFilesToRemove_.push_back(FileToRemove(info)) (files are deleted in CommitFilesToRemove in the ServerIndex::TransactionContext::Commit)
8b7c1c423367 new 'TransactionMode' config + rewrote ResourceDeletedFunc to avoid IF/THEN/ELSE pattern
Alain Mazy <am@osimis.io>
parents: 431
diff changeset
52
8b7c1c423367 new 'TransactionMode' config + rewrote ResourceDeletedFunc to avoid IF/THEN/ELSE pattern
Alain Mazy <am@osimis.io>
parents: 431
diff changeset
53 SignalDeletedResources(output, manager);
8b7c1c423367 new 'TransactionMode' config + rewrote ResourceDeletedFunc to avoid IF/THEN/ELSE pattern
Alain Mazy <am@osimis.io>
parents: 431
diff changeset
54 SELECT resourceType, publicId FROM DeletedResources
8b7c1c423367 new 'TransactionMode' config + rewrote ResourceDeletedFunc to avoid IF/THEN/ELSE pattern
Alain Mazy <am@osimis.io>
parents: 431
diff changeset
55 -> SignalDeletedResource()
8b7c1c423367 new 'TransactionMode' config + rewrote ResourceDeletedFunc to avoid IF/THEN/ELSE pattern
Alain Mazy <am@osimis.io>
parents: 431
diff changeset
56 -> Emit DeletedResource event (lua)
8b7c1c423367 new 'TransactionMode' config + rewrote ResourceDeletedFunc to avoid IF/THEN/ELSE pattern
Alain Mazy <am@osimis.io>
parents: 431
diff changeset
57
8b7c1c423367 new 'TransactionMode' config + rewrote ResourceDeletedFunc to avoid IF/THEN/ELSE pattern
Alain Mazy <am@osimis.io>
parents: 431
diff changeset
58
8b7c1c423367 new 'TransactionMode' config + rewrote ResourceDeletedFunc to avoid IF/THEN/ELSE pattern
Alain Mazy <am@osimis.io>
parents: 431
diff changeset
59 if (!CreateInstance(...))
8b7c1c423367 new 'TransactionMode' config + rewrote ResourceDeletedFunc to avoid IF/THEN/ELSE pattern
Alain Mazy <am@osimis.io>
parents: 431
diff changeset
60 Error: "No new instance while overwriting; this should not happen"
8b7c1c423367 new 'TransactionMode' config + rewrote ResourceDeletedFunc to avoid IF/THEN/ELSE pattern
Alain Mazy <am@osimis.io>
parents: 431
diff changeset
61
8b7c1c423367 new 'TransactionMode' config + rewrote ResourceDeletedFunc to avoid IF/THEN/ELSE pattern
Alain Mazy <am@osimis.io>
parents: 431
diff changeset
62 if isNewInstance -> LogChange
8b7c1c423367 new 'TransactionMode' config + rewrote ResourceDeletedFunc to avoid IF/THEN/ELSE pattern
Alain Mazy <am@osimis.io>
parents: 431
diff changeset
63 if isNewSeries -> LogChange
8b7c1c423367 new 'TransactionMode' config + rewrote ResourceDeletedFunc to avoid IF/THEN/ELSE pattern
Alain Mazy <am@osimis.io>
parents: 431
diff changeset
64 ....
431
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
65
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
66 Sample SQL code that you can execute in DBeaver to test new functions/procedures:
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
67
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
68 CreateInstance
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
69 ************************************************************************
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
70
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
71 CREATE OR replace FUNCTION CreateInstance(
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
72 IN patient TEXT,
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
73 IN study TEXT,
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
74 IN series TEXT,
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
75 IN instance TEXT,
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
76 OUT isNewPatient BIGINT,
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
77 OUT isNewStudy BIGINT,
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
78 OUT isNewSeries BIGINT,
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
79 OUT isNewInstance BIGINT,
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
80 OUT patientKey BIGINT,
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
81 OUT studyKey BIGINT,
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
82 OUT seriesKey BIGINT,
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
83 OUT instanceKey BIGINT)
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
84 AS $$
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
85 begin
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
86 isNewPatient := 1;
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
87 isNewStudy := 1;
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
88 isNewSeries := 1;
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
89 isNewInstance := 1;
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
90
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
91 BEGIN
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
92 INSERT INTO "resources" VALUES (DEFAULT, 0, patient, NULL);
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
93 exception
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
94 when unique_violation then
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
95 isNewPatient := 0;
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
96 end;
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
97 select internalid into patientKey from "resources" where publicId=patient and resourcetype = 0;
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
98
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
99 BEGIN
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
100 INSERT INTO "resources" VALUES (DEFAULT, 1, study, patientKey);
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
101 exception
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
102 when unique_violation then
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
103 isNewStudy := 0;
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
104 end;
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
105 select internalid into studyKey from "resources" where publicId=study and resourcetype = 1;
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
106
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
107 BEGIN
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
108 INSERT INTO "resources" VALUES (DEFAULT, 2, series, studyKey);
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
109 exception
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
110 when unique_violation then
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
111 isNewSeries := 0;
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
112 end;
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
113 select internalid into seriesKey from "resources" where publicId=series and resourcetype = 2;
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
114
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
115 BEGIN
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
116 INSERT INTO "resources" VALUES (DEFAULT, 3, instance, seriesKey);
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
117 exception
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
118 when unique_violation then
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
119 isNewInstance := 0;
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
120 end;
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
121 select internalid into instanceKey from "resources" where publicId=instance and resourcetype = 3;
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
122
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
123 END;
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
124 $$ LANGUAGE plpgsql;
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
125
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
126 DO $$
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
127 DECLARE
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
128 result record;
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
129 begin
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
130 delete from "resources";
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
131
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
132 SELECT * INTO result from CreateInstance('patient1', 'study1', 'series1', 'instance1');
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
133
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
134 RAISE NOTICE 'Value patientInternalId: %, is_new: %', result.patientKey, result.isNewPatient;
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
135 RAISE NOTICE 'Value studyInternalId: %, is_new: %', result.studyKey, result.isNewStudy;
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
136 RAISE NOTICE 'Value seriesInternalId: %, is_new: %', result.seriesKey, result.isNewSeries;
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
137 RAISE NOTICE 'Value instanceInternalId: %, is_new: %', result.instanceKey, result.isNewInstance;
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
138 RAISE NOTICE '--------------';
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
139
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
140 SELECT * INTO result from CreateInstance('patient1', 'study1', 'series1', 'instance2');
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
141
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
142 RAISE NOTICE 'Value patientInternalId: %, is_new: %', result.patientKey, result.isNewPatient;
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
143 RAISE NOTICE 'Value studyInternalId: %, is_new: %', result.studyKey, result.isNewStudy;
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
144 RAISE NOTICE 'Value seriesInternalId: %, is_new: %', result.seriesKey, result.isNewSeries;
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
145 RAISE NOTICE 'Value instanceInternalId: %, is_new: %', result.instanceKey, result.isNewInstance;
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
146 RAISE NOTICE '--------------';
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
147
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
148 SELECT * INTO result from CreateInstance('patient1', 'study1', 'series1', 'instance2');
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
149
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
150 RAISE NOTICE 'Value patientInternalId: %, is_new: %', result.patientKey, result.isNewPatient;
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
151 RAISE NOTICE 'Value studyInternalId: %, is_new: %', result.studyKey, result.isNewStudy;
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
152 RAISE NOTICE 'Value seriesInternalId: %, is_new: %', result.seriesKey, result.isNewSeries;
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
153 RAISE NOTICE 'Value instanceInternalId: %, is_new: %', result.instanceKey, result.isNewInstance;
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
154 RAISE NOTICE '--------------';
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
155
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
156 END $$;
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
157
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
158 -- \set patient_key 'patient_key'
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
159 -- SELECT CreateInstance('patient', 'study', 'series', 'instance', patient_key) ;
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
160
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
161 -- drop function CreateInstance
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
162 -- select * from "resources";
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
163 -- delete from "resources";
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
164 -- INSERT INTO "resources" VALUES (DEFAULT, 0, 'patient', NULL)
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
165
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
166
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
167
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
168 ************************************************************************
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
169
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
170 In debug, no verbose logs
433
5964ce6385a5 use temporary tables for DeletedFiles, RemainingAncestor and DeletedResources
Alain Mazy <am@osimis.io>
parents: 432
diff changeset
171 Orthanc 1.12.1 + PG 5.1 (serializable mode) : test_concurrent_anonymize_same_study with 4 workers and 10x repeat: 43.957 s
5964ce6385a5 use temporary tables for DeletedFiles, RemainingAncestor and DeletedResources
Alain Mazy <am@osimis.io>
parents: 432
diff changeset
172 Orthanc mainline + PG mainline (serializable mode) : test_concurrent_anonymize_same_study with 4 workers and 10x repeat: XX s
5964ce6385a5 use temporary tables for DeletedFiles, RemainingAncestor and DeletedResources
Alain Mazy <am@osimis.io>
parents: 432
diff changeset
173 Orthanc mainline + PG mainline (read-committed mode) : test_concurrent_anonymize_same_study with 4 workers and 10x repeat: 19.861 s
5964ce6385a5 use temporary tables for DeletedFiles, RemainingAncestor and DeletedResources
Alain Mazy <am@osimis.io>
parents: 432
diff changeset
174 Orthanc mainline + PG mainline (read-committed mode) : test_concurrent_anonymize_same_study with 4 workers and 10x repeat: 22.500 s (with temporary tables)
431
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
175
433
5964ce6385a5 use temporary tables for DeletedFiles, RemainingAncestor and DeletedResources
Alain Mazy <am@osimis.io>
parents: 432
diff changeset
176 Orthanc 1.12.1 + PG 5.1 (serializable mode) : test_concurrent_uploads_same_study with 20 workers and 1x repeat: 21.214 s
5964ce6385a5 use temporary tables for DeletedFiles, RemainingAncestor and DeletedResources
Alain Mazy <am@osimis.io>
parents: 432
diff changeset
177 Orthanc mainline + PG mainline (serializable mode) : test_concurrent_uploads_same_study with 20 workers and 1x repeat: 23.010 s
5964ce6385a5 use temporary tables for DeletedFiles, RemainingAncestor and DeletedResources
Alain Mazy <am@osimis.io>
parents: 432
diff changeset
178 Orthanc mainline + PG mainline (read-committed mode) : test_concurrent_uploads_same_study with 20 workers and 1x repeat: 1.71 s (with RemainingAncestor.id)
5964ce6385a5 use temporary tables for DeletedFiles, RemainingAncestor and DeletedResources
Alain Mazy <am@osimis.io>
parents: 432
diff changeset
179 Orthanc mainline + PG mainline (read-committed mode) : test_concurrent_uploads_same_study with 20 workers and 1x repeat: 3.26 s (with temporary tables)
5964ce6385a5 use temporary tables for DeletedFiles, RemainingAncestor and DeletedResources
Alain Mazy <am@osimis.io>
parents: 432
diff changeset
180
5964ce6385a5 use temporary tables for DeletedFiles, RemainingAncestor and DeletedResources
Alain Mazy <am@osimis.io>
parents: 432
diff changeset
181 Orthanc 1.12.1 + PG 5.1 (serializable mode) : test_upload_delete_same_study_from_multiple_threads with 5 workers and 30x repeat: 23.016 s
5964ce6385a5 use temporary tables for DeletedFiles, RemainingAncestor and DeletedResources
Alain Mazy <am@osimis.io>
parents: 432
diff changeset
182 Orthanc mainline + PG mainline (read-committed mode) : test_upload_delete_same_study_from_multiple_threads with 5 workers and 30x repeat: 8.788 s
5964ce6385a5 use temporary tables for DeletedFiles, RemainingAncestor and DeletedResources
Alain Mazy <am@osimis.io>
parents: 432
diff changeset
183 Orthanc mainline + PG mainline (read-committed mode) : test_upload_delete_same_study_from_multiple_threads with 5 workers and 30x repeat: 17.129 s (with temporary tables)
431
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
184
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
185 TODO:
432
8b7c1c423367 new 'TransactionMode' config + rewrote ResourceDeletedFunc to avoid IF/THEN/ELSE pattern
Alain Mazy <am@osimis.io>
parents: 431
diff changeset
186 - assert((statement.Next(), statement.IsDone())) commented out -> create temporary tables ?
433
5964ce6385a5 use temporary tables for DeletedFiles, RemainingAncestor and DeletedResources
Alain Mazy <am@osimis.io>
parents: 432
diff changeset
187 try again temporary tables (with different names ? Have a switch in the code ?)
5964ce6385a5 use temporary tables for DeletedFiles, RemainingAncestor and DeletedResources
Alain Mazy <am@osimis.io>
parents: 432
diff changeset
188 - have a separate "thread" to increment/decrement statistics because everybody is fighting to modify the GlobalIntegers rows
5964ce6385a5 use temporary tables for DeletedFiles, RemainingAncestor and DeletedResources
Alain Mazy <am@osimis.io>
parents: 432
diff changeset
189 - test events generation StableSeries ....
5964ce6385a5 use temporary tables for DeletedFiles, RemainingAncestor and DeletedResources
Alain Mazy <am@osimis.io>
parents: 432
diff changeset
190 - test RemainingAncestor response in integration tests: OK
5964ce6385a5 use temporary tables for DeletedFiles, RemainingAncestor and DeletedResources
Alain Mazy <am@osimis.io>
parents: 432
diff changeset
191 - disable MySQL ODBC plugin
5964ce6385a5 use temporary tables for DeletedFiles, RemainingAncestor and DeletedResources
Alain Mazy <am@osimis.io>
parents: 432
diff changeset
192 - run tests with docker localy + in CI
5964ce6385a5 use temporary tables for DeletedFiles, RemainingAncestor and DeletedResources
Alain Mazy <am@osimis.io>
parents: 432
diff changeset
193 - check https://discourse.orthanc-server.org/t/image-insert-are-too-slow-databse-performance-too-poor-when-using-mysql-mariadb/3820
432
8b7c1c423367 new 'TransactionMode' config + rewrote ResourceDeletedFunc to avoid IF/THEN/ELSE pattern
Alain Mazy <am@osimis.io>
parents: 431
diff changeset
194 - PatientAddedFunc contains an IF
8b7c1c423367 new 'TransactionMode' config + rewrote ResourceDeletedFunc to avoid IF/THEN/ELSE pattern
Alain Mazy <am@osimis.io>
parents: 431
diff changeset
195 - validate upgrade DB from previous Orthanc and from scratch
8b7c1c423367 new 'TransactionMode' config + rewrote ResourceDeletedFunc to avoid IF/THEN/ELSE pattern
Alain Mazy <am@osimis.io>
parents: 431
diff changeset
196 - test with older version of PG
433
5964ce6385a5 use temporary tables for DeletedFiles, RemainingAncestor and DeletedResources
Alain Mazy <am@osimis.io>
parents: 432
diff changeset
197
5964ce6385a5 use temporary tables for DeletedFiles, RemainingAncestor and DeletedResources
Alain Mazy <am@osimis.io>
parents: 432
diff changeset
198 DONE:
432
8b7c1c423367 new 'TransactionMode' config + rewrote ResourceDeletedFunc to avoid IF/THEN/ELSE pattern
Alain Mazy <am@osimis.io>
parents: 431
diff changeset
199 - force the create/update DB transaction to be serializable (not needed: this is handled by POSTGRESQL_LOCK_DATABASE_SETUP)