annotate NOTES @ 478:0064a54745e1

todo
author Alain Mazy <am@osimis.io>
date Wed, 06 Mar 2024 12:27:48 +0100
parents 0a8b34e3a337
children bf4b9c7cf338
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
444
2ca939d02d39 cleanup
Alain Mazy <am@osimis.io>
parents: 443
diff changeset
71 CREATE OR REPLACE FUNCTION CreateInstance(
2ca939d02d39 cleanup
Alain Mazy <am@osimis.io>
parents: 443
diff changeset
72 IN patient_public_id TEXT,
2ca939d02d39 cleanup
Alain Mazy <am@osimis.io>
parents: 443
diff changeset
73 IN study_public_id TEXT,
2ca939d02d39 cleanup
Alain Mazy <am@osimis.io>
parents: 443
diff changeset
74 IN series_public_id TEXT,
2ca939d02d39 cleanup
Alain Mazy <am@osimis.io>
parents: 443
diff changeset
75 IN instance_public_id TEXT,
2ca939d02d39 cleanup
Alain Mazy <am@osimis.io>
parents: 443
diff changeset
76 OUT is_new_patient BIGINT,
2ca939d02d39 cleanup
Alain Mazy <am@osimis.io>
parents: 443
diff changeset
77 OUT is_new_study BIGINT,
2ca939d02d39 cleanup
Alain Mazy <am@osimis.io>
parents: 443
diff changeset
78 OUT is_new_series BIGINT,
2ca939d02d39 cleanup
Alain Mazy <am@osimis.io>
parents: 443
diff changeset
79 OUT is_new_instance BIGINT,
2ca939d02d39 cleanup
Alain Mazy <am@osimis.io>
parents: 443
diff changeset
80 OUT patient_internal_id BIGINT,
2ca939d02d39 cleanup
Alain Mazy <am@osimis.io>
parents: 443
diff changeset
81 OUT study_internal_id BIGINT,
2ca939d02d39 cleanup
Alain Mazy <am@osimis.io>
parents: 443
diff changeset
82 OUT series_internal_id BIGINT,
2ca939d02d39 cleanup
Alain Mazy <am@osimis.io>
parents: 443
diff changeset
83 OUT instance_internal_id BIGINT) AS $body$
2ca939d02d39 cleanup
Alain Mazy <am@osimis.io>
parents: 443
diff changeset
84
2ca939d02d39 cleanup
Alain Mazy <am@osimis.io>
parents: 443
diff changeset
85 BEGIN
2ca939d02d39 cleanup
Alain Mazy <am@osimis.io>
parents: 443
diff changeset
86 is_new_patient := 1;
2ca939d02d39 cleanup
Alain Mazy <am@osimis.io>
parents: 443
diff changeset
87 is_new_study := 1;
2ca939d02d39 cleanup
Alain Mazy <am@osimis.io>
parents: 443
diff changeset
88 is_new_series := 1;
2ca939d02d39 cleanup
Alain Mazy <am@osimis.io>
parents: 443
diff changeset
89 is_new_instance := 1;
2ca939d02d39 cleanup
Alain Mazy <am@osimis.io>
parents: 443
diff changeset
90
2ca939d02d39 cleanup
Alain Mazy <am@osimis.io>
parents: 443
diff changeset
91
2ca939d02d39 cleanup
Alain Mazy <am@osimis.io>
parents: 443
diff changeset
92 BEGIN
2ca939d02d39 cleanup
Alain Mazy <am@osimis.io>
parents: 443
diff changeset
93 INSERT INTO "resources" VALUES (DEFAULT, 0, patient_public_id, NULL);
2ca939d02d39 cleanup
Alain Mazy <am@osimis.io>
parents: 443
diff changeset
94 EXCEPTION
2ca939d02d39 cleanup
Alain Mazy <am@osimis.io>
parents: 443
diff changeset
95 WHEN unique_violation THEN
2ca939d02d39 cleanup
Alain Mazy <am@osimis.io>
parents: 443
diff changeset
96 is_new_patient := 0;
2ca939d02d39 cleanup
Alain Mazy <am@osimis.io>
parents: 443
diff changeset
97 END;
2ca939d02d39 cleanup
Alain Mazy <am@osimis.io>
parents: 443
diff changeset
98 SELECT internalid INTO patient_internal_id FROM "resources" WHERE publicId = patient_public_id AND resourcetype = 0 FOR UPDATE; -- also locks the resource and its parent to prevent from deletion while we complete this transaction
431
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
99
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
100 BEGIN
444
2ca939d02d39 cleanup
Alain Mazy <am@osimis.io>
parents: 443
diff changeset
101 INSERT INTO "resources" VALUES (DEFAULT, 1, study_public_id, patient_internal_id);
2ca939d02d39 cleanup
Alain Mazy <am@osimis.io>
parents: 443
diff changeset
102 EXCEPTION
2ca939d02d39 cleanup
Alain Mazy <am@osimis.io>
parents: 443
diff changeset
103 WHEN unique_violation THEN
2ca939d02d39 cleanup
Alain Mazy <am@osimis.io>
parents: 443
diff changeset
104 is_new_study := 0;
2ca939d02d39 cleanup
Alain Mazy <am@osimis.io>
parents: 443
diff changeset
105 END;
2ca939d02d39 cleanup
Alain Mazy <am@osimis.io>
parents: 443
diff changeset
106 SELECT internalid INTO study_internal_id FROM "resources" WHERE publicId = study_public_id AND resourcetype = 1 FOR UPDATE; -- also locks the resource and its parent to prevent from deletion while we complete this transaction
431
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
107
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
108 BEGIN
444
2ca939d02d39 cleanup
Alain Mazy <am@osimis.io>
parents: 443
diff changeset
109 INSERT INTO "resources" VALUES (DEFAULT, 2, series_public_id, study_internal_id);
2ca939d02d39 cleanup
Alain Mazy <am@osimis.io>
parents: 443
diff changeset
110 EXCEPTION
2ca939d02d39 cleanup
Alain Mazy <am@osimis.io>
parents: 443
diff changeset
111 WHEN unique_violation THEN
2ca939d02d39 cleanup
Alain Mazy <am@osimis.io>
parents: 443
diff changeset
112 is_new_series := 0;
2ca939d02d39 cleanup
Alain Mazy <am@osimis.io>
parents: 443
diff changeset
113 END;
2ca939d02d39 cleanup
Alain Mazy <am@osimis.io>
parents: 443
diff changeset
114 SELECT internalid INTO series_internal_id FROM "resources" WHERE publicId = series_public_id AND resourcetype = 2 FOR UPDATE; -- also locks the resource and its parent to prevent from deletion while we complete this transaction
431
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
115
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
116 BEGIN
444
2ca939d02d39 cleanup
Alain Mazy <am@osimis.io>
parents: 443
diff changeset
117 INSERT INTO "resources" VALUES (DEFAULT, 3, instance_public_id, series_internal_id);
2ca939d02d39 cleanup
Alain Mazy <am@osimis.io>
parents: 443
diff changeset
118 EXCEPTION
2ca939d02d39 cleanup
Alain Mazy <am@osimis.io>
parents: 443
diff changeset
119 WHEN unique_violation THEN
2ca939d02d39 cleanup
Alain Mazy <am@osimis.io>
parents: 443
diff changeset
120 is_new_instance := 0;
2ca939d02d39 cleanup
Alain Mazy <am@osimis.io>
parents: 443
diff changeset
121 END;
2ca939d02d39 cleanup
Alain Mazy <am@osimis.io>
parents: 443
diff changeset
122 SELECT internalid INTO instance_internal_id FROM "resources" WHERE publicId = instance_public_id AND resourcetype = 3 FOR UPDATE; -- also locks the resource and its parent to prevent from deletion while we complete this transaction
2ca939d02d39 cleanup
Alain Mazy <am@osimis.io>
parents: 443
diff changeset
123
2ca939d02d39 cleanup
Alain Mazy <am@osimis.io>
parents: 443
diff changeset
124 IF is_new_instance > 0 THEN
2ca939d02d39 cleanup
Alain Mazy <am@osimis.io>
parents: 443
diff changeset
125 -- Move the patient to the end of the recycling order.
2ca939d02d39 cleanup
Alain Mazy <am@osimis.io>
parents: 443
diff changeset
126 PERFORM PatientAddedOrUpdated(patient_internal_id, 1);
2ca939d02d39 cleanup
Alain Mazy <am@osimis.io>
parents: 443
diff changeset
127 END IF;
431
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
128 END;
444
2ca939d02d39 cleanup
Alain Mazy <am@osimis.io>
parents: 443
diff changeset
129
2ca939d02d39 cleanup
Alain Mazy <am@osimis.io>
parents: 443
diff changeset
130 $body$ LANGUAGE plpgsql;
2ca939d02d39 cleanup
Alain Mazy <am@osimis.io>
parents: 443
diff changeset
131
431
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
132
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
133 DO $$
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
134 DECLARE
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
135 result record;
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
136 begin
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
137 delete from "resources";
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
138
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
139 SELECT * INTO result from CreateInstance('patient1', 'study1', 'series1', 'instance1');
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
140
444
2ca939d02d39 cleanup
Alain Mazy <am@osimis.io>
parents: 443
diff changeset
141 RAISE NOTICE 'Value patientInternalId: %, is_new: %', result.patient_internal_id, result.is_new_patient;
2ca939d02d39 cleanup
Alain Mazy <am@osimis.io>
parents: 443
diff changeset
142 RAISE NOTICE 'Value studyInternalId: %, is_new: %', result.study_internal_id, result.is_new_study;
2ca939d02d39 cleanup
Alain Mazy <am@osimis.io>
parents: 443
diff changeset
143 RAISE NOTICE 'Value seriesInternalId: %, is_new: %', result.series_internal_id, result.is_new_series;
2ca939d02d39 cleanup
Alain Mazy <am@osimis.io>
parents: 443
diff changeset
144 RAISE NOTICE 'Value instanceInternalId: %, is_new: %', result.instance_internal_id, result.is_new_instance;
431
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
145 RAISE NOTICE '--------------';
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
146
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
147 SELECT * INTO result from CreateInstance('patient1', 'study1', 'series1', 'instance2');
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
148
444
2ca939d02d39 cleanup
Alain Mazy <am@osimis.io>
parents: 443
diff changeset
149 RAISE NOTICE 'Value patientInternalId: %, is_new: %', result.patient_internal_id, result.is_new_patient;
2ca939d02d39 cleanup
Alain Mazy <am@osimis.io>
parents: 443
diff changeset
150 RAISE NOTICE 'Value studyInternalId: %, is_new: %', result.study_internal_id, result.is_new_study;
2ca939d02d39 cleanup
Alain Mazy <am@osimis.io>
parents: 443
diff changeset
151 RAISE NOTICE 'Value seriesInternalId: %, is_new: %', result.series_internal_id, result.is_new_series;
2ca939d02d39 cleanup
Alain Mazy <am@osimis.io>
parents: 443
diff changeset
152 RAISE NOTICE 'Value instanceInternalId: %, is_new: %', result.instance_internal_id, result.is_new_instance;
431
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
153 RAISE NOTICE '--------------';
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
154
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
155 SELECT * INTO result from CreateInstance('patient1', 'study1', 'series1', 'instance2');
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
156
444
2ca939d02d39 cleanup
Alain Mazy <am@osimis.io>
parents: 443
diff changeset
157 RAISE NOTICE 'Value patientInternalId: %, is_new: %', result.patient_internal_id, result.is_new_patient;
2ca939d02d39 cleanup
Alain Mazy <am@osimis.io>
parents: 443
diff changeset
158 RAISE NOTICE 'Value studyInternalId: %, is_new: %', result.study_internal_id, result.is_new_study;
2ca939d02d39 cleanup
Alain Mazy <am@osimis.io>
parents: 443
diff changeset
159 RAISE NOTICE 'Value seriesInternalId: %, is_new: %', result.series_internal_id, result.is_new_series;
2ca939d02d39 cleanup
Alain Mazy <am@osimis.io>
parents: 443
diff changeset
160 RAISE NOTICE 'Value instanceInternalId: %, is_new: %', result.instance_internal_id, result.is_new_instance;
431
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
161 RAISE NOTICE '--------------';
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
162
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
163 END $$;
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
164
444
2ca939d02d39 cleanup
Alain Mazy <am@osimis.io>
parents: 443
diff changeset
165
431
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
166 -- \set patient_key 'patient_key'
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
167 -- SELECT CreateInstance('patient', 'study', 'series', 'instance', patient_key) ;
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
168
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
169 -- drop function CreateInstance
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
170 -- select * from "resources";
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
171 -- delete from "resources";
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
172 -- INSERT INTO "resources" VALUES (DEFAULT, 0, 'patient', NULL)
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
173
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
174
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
175
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
176 ************************************************************************
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
177
443
2a48f8fcec6e cleanup
Alain Mazy <am@osimis.io>
parents: 437
diff changeset
178 In debug, no verbose logs, 10 connections
433
5964ce6385a5 use temporary tables for DeletedFiles, RemainingAncestor and DeletedResources
Alain Mazy <am@osimis.io>
parents: 432
diff changeset
179 Orthanc 1.12.1 + PG 5.1 (serializable mode) : test_concurrent_anonymize_same_study with 4 workers and 10x repeat: 43.957 s
436
f16faa1fdc46 InsertOrUpdateMetadata function + UpdateAndGetStatistics
Alain Mazy <am@osimis.io>
parents: 435
diff changeset
180 Orthanc mainline + PG mainline (read-committed mode) : test_concurrent_anonymize_same_study with 4 workers and 10x repeat: 15.744 s
f16faa1fdc46 InsertOrUpdateMetadata function + UpdateAndGetStatistics
Alain Mazy <am@osimis.io>
parents: 435
diff changeset
181 test_concurrent_anonymize_same_study deletion took: 18.8 s
435
326f8304daa1 new creating temporary tables inside functions
Alain Mazy <am@osimis.io>
parents: 434
diff changeset
182
433
5964ce6385a5 use temporary tables for DeletedFiles, RemainingAncestor and DeletedResources
Alain Mazy <am@osimis.io>
parents: 432
diff changeset
183 Orthanc 1.12.1 + PG 5.1 (serializable mode) : test_concurrent_uploads_same_study with 20 workers and 1x repeat: 21.214 s
446
Alain Mazy <am@osimis.io>
parents: 444
diff changeset
184 Orthanc mainline + PG mainline (read-committed mode) : test_concurrent_uploads_same_study with 20 workers and 1x repeat: 9.514 s
433
5964ce6385a5 use temporary tables for DeletedFiles, RemainingAncestor and DeletedResources
Alain Mazy <am@osimis.io>
parents: 432
diff changeset
185
443
2a48f8fcec6e cleanup
Alain Mazy <am@osimis.io>
parents: 437
diff changeset
186 Orthanc 1.12.1 + PG 5.1 (serializable mode) : test_upload_delete_same_study_from_multiple_threads with 5 workers and 3x repeat (10x): 23.016 s
2a48f8fcec6e cleanup
Alain Mazy <am@osimis.io>
parents: 437
diff changeset
187 Orthanc mainline + PG mainline (read-committed mode) : test_upload_delete_same_study_from_multiple_threads with 5 workers and 3x repeat (10x): 7.129 s
436
f16faa1fdc46 InsertOrUpdateMetadata function + UpdateAndGetStatistics
Alain Mazy <am@osimis.io>
parents: 435
diff changeset
188
444
2ca939d02d39 cleanup
Alain Mazy <am@osimis.io>
parents: 443
diff changeset
189 Orthanc mainline + PG mainline (read-committed mode) : test_upload_multiple_studies_from_multiple_threads with 10 workers and 25 files and repeat 3x: 6.454 s
2ca939d02d39 cleanup
Alain Mazy <am@osimis.io>
parents: 443
diff changeset
190
436
f16faa1fdc46 InsertOrUpdateMetadata function + UpdateAndGetStatistics
Alain Mazy <am@osimis.io>
parents: 435
diff changeset
191 With Docker with 10 connections SQL:
446
Alain Mazy <am@osimis.io>
parents: 444
diff changeset
192 osimis/orthanc:24.1.2 : test_concurrent_anonymize_same_study with 4 workers and 10x repeat: 18.956 s FAIL !!!
Alain Mazy <am@osimis.io>
parents: 444
diff changeset
193 test_concurrent_anonymize_same_study deletion took: NA
Alain Mazy <am@osimis.io>
parents: 444
diff changeset
194 osimis/orthanc:current: test_concurrent_anonymize_same_study with 4 workers and 10x repeat: 6.867 s
Alain Mazy <am@osimis.io>
parents: 444
diff changeset
195 test_concurrent_anonymize_same_study deletion took: 9.095 s
436
f16faa1fdc46 InsertOrUpdateMetadata function + UpdateAndGetStatistics
Alain Mazy <am@osimis.io>
parents: 435
diff changeset
196
446
Alain Mazy <am@osimis.io>
parents: 444
diff changeset
197 osimis/orthanc:24.1.2 : test_concurrent_uploads_same_study with 20 workers and 1x repeat: 9.822 s
Alain Mazy <am@osimis.io>
parents: 444
diff changeset
198 osimis/orthanc:current: test_concurrent_uploads_same_study with 20 workers and 1x repeat: 16.027 s up to 38s ! (slower but the test is not representative of a real life scenario !!!!!)
436
f16faa1fdc46 InsertOrUpdateMetadata function + UpdateAndGetStatistics
Alain Mazy <am@osimis.io>
parents: 435
diff changeset
199
446
Alain Mazy <am@osimis.io>
parents: 444
diff changeset
200 osimis/orthanc:24.1.2 : test_upload_delete_same_study_from_multiple_threads with 5 workers and 3x repeat (10x): 12.966 s
Alain Mazy <am@osimis.io>
parents: 444
diff changeset
201 osimis/orthanc:current: test_upload_delete_same_study_from_multiple_threads with 5 workers and 3x repeat (10x): 4.196 s
Alain Mazy <am@osimis.io>
parents: 444
diff changeset
202
Alain Mazy <am@osimis.io>
parents: 444
diff changeset
203 osimis/orthanc:24.1.2 : test_upload_multiple_studies_from_multiple_threads with 10 workers and 25 files and repeat 3x: 8.957 s
Alain Mazy <am@osimis.io>
parents: 444
diff changeset
204 osimis/orthanc:current: test_upload_multiple_studies_from_multiple_threads with 10 workers and 25 files and repeat 3x: 2.671 s
436
f16faa1fdc46 InsertOrUpdateMetadata function + UpdateAndGetStatistics
Alain Mazy <am@osimis.io>
parents: 435
diff changeset
205
443
2a48f8fcec6e cleanup
Alain Mazy <am@osimis.io>
parents: 437
diff changeset
206 Testing the connecions (note: Orthanc and PG server running on the same server)
2a48f8fcec6e cleanup
Alain Mazy <am@osimis.io>
parents: 437
diff changeset
207 10 connections : test_concurrent_anonymize_same_study with 4 workers and 10x repeat: 15.744 s
2a48f8fcec6e cleanup
Alain Mazy <am@osimis.io>
parents: 437
diff changeset
208 1 connection : test_concurrent_anonymize_same_study with 4 workers and 10x repeat: 21.341 s
2a48f8fcec6e cleanup
Alain Mazy <am@osimis.io>
parents: 437
diff changeset
209 10 connections : test_concurrent_uploads_same_study with 20 workers and 1x repeat: 6.57 s
2a48f8fcec6e cleanup
Alain Mazy <am@osimis.io>
parents: 437
diff changeset
210 1 connection : test_concurrent_uploads_same_study with 20 workers and 1x repeat: 10.223 s
2a48f8fcec6e cleanup
Alain Mazy <am@osimis.io>
parents: 437
diff changeset
211 10 connections : test_upload_delete_same_study_from_multiple_threads with 5 workers and 3x repeat (10x): 7.129 s
2a48f8fcec6e cleanup
Alain Mazy <am@osimis.io>
parents: 437
diff changeset
212 1 connection : test_upload_delete_same_study_from_multiple_threads with 5 workers and 3x repeat (10x): 11.172 s
2a48f8fcec6e cleanup
Alain Mazy <am@osimis.io>
parents: 437
diff changeset
213
431
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
214
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
215 TODO:
437
d979f25e60cf Re-organized DB creation/upgrade into standalone files
Alain Mazy <am@osimis.io>
parents: 436
diff changeset
216 - have a separate "thread" to UpdateStatistics ?
453
Alain Mazy <am@osimis.io>
parents: 446
diff changeset
217
433
5964ce6385a5 use temporary tables for DeletedFiles, RemainingAncestor and DeletedResources
Alain Mazy <am@osimis.io>
parents: 432
diff changeset
218 - check https://discourse.orthanc-server.org/t/image-insert-are-too-slow-databse-performance-too-poor-when-using-mysql-mariadb/3820
443
2a48f8fcec6e cleanup
Alain Mazy <am@osimis.io>
parents: 437
diff changeset
219
433
5964ce6385a5 use temporary tables for DeletedFiles, RemainingAncestor and DeletedResources
Alain Mazy <am@osimis.io>
parents: 432
diff changeset
220 DONE:
453
Alain Mazy <am@osimis.io>
parents: 446
diff changeset
221 - implement a downgrade script ? And test it in PotgresUpgrades integ tests
Alain Mazy <am@osimis.io>
parents: 446
diff changeset
222 - test the transfer plugin
Alain Mazy <am@osimis.io>
parents: 446
diff changeset
223 - perf tests: upload generated data (different studies)
Alain Mazy <am@osimis.io>
parents: 446
diff changeset
224 - In Docker images, re-enable MySQL & ODBC plugins + tests
443
2a48f8fcec6e cleanup
Alain Mazy <am@osimis.io>
parents: 437
diff changeset
225 - reenable PatientRecyclingOrder
432
8b7c1c423367 new 'TransactionMode' config + rewrote ResourceDeletedFunc to avoid IF/THEN/ELSE pattern
Alain Mazy <am@osimis.io>
parents: 431
diff changeset
226 - force the create/update DB transaction to be serializable (not needed: this is handled by POSTGRESQL_LOCK_DATABASE_SETUP)
437
d979f25e60cf Re-organized DB creation/upgrade into standalone files
Alain Mazy <am@osimis.io>
parents: 436
diff changeset
227 - PatientAddedFunc contains an IF (check if other IF/THEN/ELSE pattern remains)
443
2a48f8fcec6e cleanup
Alain Mazy <am@osimis.io>
parents: 437
diff changeset
228 - validate upgrade DB from previous Orthanc and from scratch
2a48f8fcec6e cleanup
Alain Mazy <am@osimis.io>
parents: 437
diff changeset
229 - check minimal version of PG (9.5 - 9.6 ? for create index if not exists): seems to work with 9.5 cfr PotgresUpgrades integ tests
2a48f8fcec6e cleanup
Alain Mazy <am@osimis.io>
parents: 437
diff changeset
230 - test events generation StableSeries .... (count the NewSeries, NewInstances event and make sure they match the numb)