annotate NOTES @ 437:d979f25e60cf pg-transactions

Re-organized DB creation/upgrade into standalone files
author Alain Mazy <am@osimis.io>
date Mon, 18 Dec 2023 18:50:01 +0100
parents f16faa1fdc46
children 2a48f8fcec6e
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
436
f16faa1fdc46 InsertOrUpdateMetadata function + UpdateAndGetStatistics
Alain Mazy <am@osimis.io>
parents: 435
diff changeset
172 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
173 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
174
433
5964ce6385a5 use temporary tables for DeletedFiles, RemainingAncestor and DeletedResources
Alain Mazy <am@osimis.io>
parents: 432
diff changeset
175 Orthanc 1.12.1 + PG 5.1 (serializable mode) : test_concurrent_uploads_same_study with 20 workers and 1x repeat: 21.214 s
436
f16faa1fdc46 InsertOrUpdateMetadata function + UpdateAndGetStatistics
Alain Mazy <am@osimis.io>
parents: 435
diff changeset
176 Orthanc mainline + PG mainline (read-committed mode) : test_concurrent_uploads_same_study with 20 workers and 1x repeat: 6.57 s (with temporary tables)
433
5964ce6385a5 use temporary tables for DeletedFiles, RemainingAncestor and DeletedResources
Alain Mazy <am@osimis.io>
parents: 432
diff changeset
177
5964ce6385a5 use temporary tables for DeletedFiles, RemainingAncestor and DeletedResources
Alain Mazy <am@osimis.io>
parents: 432
diff changeset
178 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
436
f16faa1fdc46 InsertOrUpdateMetadata function + UpdateAndGetStatistics
Alain Mazy <am@osimis.io>
parents: 435
diff changeset
179 Orthanc mainline + PG mainline (read-committed mode) : test_upload_delete_same_study_from_multiple_threads with 5 workers and 30x repeat: 7.129 s
f16faa1fdc46 InsertOrUpdateMetadata function + UpdateAndGetStatistics
Alain Mazy <am@osimis.io>
parents: 435
diff changeset
180
f16faa1fdc46 InsertOrUpdateMetadata function + UpdateAndGetStatistics
Alain Mazy <am@osimis.io>
parents: 435
diff changeset
181 With Docker with 10 connections SQL:
f16faa1fdc46 InsertOrUpdateMetadata function + UpdateAndGetStatistics
Alain Mazy <am@osimis.io>
parents: 435
diff changeset
182 osimis/orthanc:23.11.0: TIMING test_concurrent_anonymize_same_study with 4 workers and 10x repeat: 25.047 s
f16faa1fdc46 InsertOrUpdateMetadata function + UpdateAndGetStatistics
Alain Mazy <am@osimis.io>
parents: 435
diff changeset
183 osimis/orthanc:current: TIMING test_concurrent_anonymize_same_study with 4 workers and 10x repeat: 8.649 s
f16faa1fdc46 InsertOrUpdateMetadata function + UpdateAndGetStatistics
Alain Mazy <am@osimis.io>
parents: 435
diff changeset
184
f16faa1fdc46 InsertOrUpdateMetadata function + UpdateAndGetStatistics
Alain Mazy <am@osimis.io>
parents: 435
diff changeset
185 osimis/orthanc:23.11.0: TIMING test_concurrent_anonymize_same_study deletion took: 11.807 s
f16faa1fdc46 InsertOrUpdateMetadata function + UpdateAndGetStatistics
Alain Mazy <am@osimis.io>
parents: 435
diff changeset
186 osimis/orthanc:current: TIMING test_concurrent_anonymize_same_study deletion took: 10.620 s
f16faa1fdc46 InsertOrUpdateMetadata function + UpdateAndGetStatistics
Alain Mazy <am@osimis.io>
parents: 435
diff changeset
187
f16faa1fdc46 InsertOrUpdateMetadata function + UpdateAndGetStatistics
Alain Mazy <am@osimis.io>
parents: 435
diff changeset
188 osimis/orthanc:23.11.0: TIMING test_concurrent_uploads_same_study with 20 workers and 1x repeat: 10.736 s
f16faa1fdc46 InsertOrUpdateMetadata function + UpdateAndGetStatistics
Alain Mazy <am@osimis.io>
parents: 435
diff changeset
189 osimis/orthanc:current: TIMING test_concurrent_uploads_same_study with 20 workers and 1x repeat: 3.465 s
f16faa1fdc46 InsertOrUpdateMetadata function + UpdateAndGetStatistics
Alain Mazy <am@osimis.io>
parents: 435
diff changeset
190
f16faa1fdc46 InsertOrUpdateMetadata function + UpdateAndGetStatistics
Alain Mazy <am@osimis.io>
parents: 435
diff changeset
191 osimis/orthanc:23.11.0: TIMING test_upload_delete_same_study_from_multiple_threads with 5 workers and 30x repeat: 11.966 s
f16faa1fdc46 InsertOrUpdateMetadata function + UpdateAndGetStatistics
Alain Mazy <am@osimis.io>
parents: 435
diff changeset
192 osimis/orthanc:current: TIMING test_upload_delete_same_study_from_multiple_threads with 5 workers and 30x repeat: 5.092 s
f16faa1fdc46 InsertOrUpdateMetadata function + UpdateAndGetStatistics
Alain Mazy <am@osimis.io>
parents: 435
diff changeset
193
431
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
194
7c1fe5d6c12c PG: IncrementGlobalProperty
Alain Mazy <am@osimis.io>
parents:
diff changeset
195 TODO:
437
d979f25e60cf Re-organized DB creation/upgrade into standalone files
Alain Mazy <am@osimis.io>
parents: 436
diff changeset
196 - reenable PatientRecyclingOrder
d979f25e60cf Re-organized DB creation/upgrade into standalone files
Alain Mazy <am@osimis.io>
parents: 436
diff changeset
197 - have a separate "thread" to UpdateStatistics ?
433
5964ce6385a5 use temporary tables for DeletedFiles, RemainingAncestor and DeletedResources
Alain Mazy <am@osimis.io>
parents: 432
diff changeset
198 - test events generation StableSeries ....
5964ce6385a5 use temporary tables for DeletedFiles, RemainingAncestor and DeletedResources
Alain Mazy <am@osimis.io>
parents: 432
diff changeset
199 - 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
200 - validate upgrade DB from previous Orthanc and from scratch
437
d979f25e60cf Re-organized DB creation/upgrade into standalone files
Alain Mazy <am@osimis.io>
parents: 436
diff changeset
201 - check minimal version of PG (9.5 - 9.6 ? for create index if not exists)
d979f25e60cf Re-organized DB creation/upgrade into standalone files
Alain Mazy <am@osimis.io>
parents: 436
diff changeset
202 - implement a downgrade script ?
432
8b7c1c423367 new 'TransactionMode' config + rewrote ResourceDeletedFunc to avoid IF/THEN/ELSE pattern
Alain Mazy <am@osimis.io>
parents: 431
diff changeset
203 - test with older version of PG
436
f16faa1fdc46 InsertOrUpdateMetadata function + UpdateAndGetStatistics
Alain Mazy <am@osimis.io>
parents: 435
diff changeset
204 - In Docker images, re-enable MySQL & ODBC plugins + tests
433
5964ce6385a5 use temporary tables for DeletedFiles, RemainingAncestor and DeletedResources
Alain Mazy <am@osimis.io>
parents: 432
diff changeset
205
5964ce6385a5 use temporary tables for DeletedFiles, RemainingAncestor and DeletedResources
Alain Mazy <am@osimis.io>
parents: 432
diff changeset
206 DONE:
432
8b7c1c423367 new 'TransactionMode' config + rewrote ResourceDeletedFunc to avoid IF/THEN/ELSE pattern
Alain Mazy <am@osimis.io>
parents: 431
diff changeset
207 - 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
208 - PatientAddedFunc contains an IF (check if other IF/THEN/ELSE pattern remains)