]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/cats/make_postgresql_tables.in
cb8d58dd6d5ec362ea4d8f9fe7be7dc5a22088b4
[bacula/bacula] / bacula / src / cats / make_postgresql_tables.in
1 #!/bin/sh
2 #
3 # shell script to create Bacula PostgreSQL tables
4 #
5 # Important note: 
6 #   You won't get any support for performance issue if you changed the default
7 #   schema.
8 #
9 bindir=@POSTGRESQL_BINDIR@
10 PATH="$bindir:$PATH"
11 db_name=${db_name:-@db_name@}
12
13 psql -f - -d ${db_name} $* <<END-OF-DATA
14
15 CREATE TABLE Filename
16 (
17     FilenameId        serial      not null,
18     Name              text        not null,
19     primary key (FilenameId)
20 );
21
22 ALTER TABLE Filename ALTER COLUMN Name SET STATISTICS 1000;
23 CREATE UNIQUE INDEX filename_name_idx on Filename (Name);
24
25 CREATE TABLE Path
26 (
27     PathId            serial      not null,
28     Path              text        not null,
29     primary key (PathId)
30 );
31
32 ALTER TABLE Path ALTER COLUMN Path SET STATISTICS 1000;
33 CREATE UNIQUE INDEX path_name_idx on Path (Path);
34
35 -- We strongly recommend to avoid the temptation to add new indexes.
36 -- In general, these will cause very significant performance
37 -- problems in other areas.  A better approch is to carefully check
38 -- that all your memory configuation parameters are
39 -- suitable for the size of your installation.  If you backup
40 -- millions of files, you need to adapt the database memory
41 -- configuration parameters concerning sorting, joining and global
42 -- memory.  By default, sort and join parameters are very small
43 -- (sometimes 8Kb), and having sufficient memory specified by those
44 -- parameters is extremely important to run fast.  
45
46 -- In File table
47 -- FileIndex can be 0 for FT_DELETED files
48 -- FileNameId can link to Filename.Name='' for directories
49 CREATE TABLE File
50 (
51     FileId            bigserial   not null,
52     FileIndex         integer     not null  default 0,
53     JobId             integer     not null,
54     PathId            integer     not null,
55     FilenameId        integer     not null,
56     DeltaSeq          smallint    not null  default 0,
57     MarkId            integer     not null  default 0,
58     LStat             text        not null,
59     Md5               text        not null,
60     primary key (FileId)
61 );
62
63 CREATE INDEX file_jpfid_idx on File (JobId, PathId, FilenameId);
64 CREATE INDEX file_jobid_idx on File (JobId);
65
66 --
67 -- Add this if you have a good number of job
68 -- that run at the same time
69 -- ALTER SEQUENCE file_fileid_seq CACHE 1000;
70
71 --
72 -- Possibly add one or more of the following indexes
73 --  if your Verifies are too slow, but they can slow down
74 --  backups.
75 --
76 -- CREATE INDEX file_pathid_idx on file(pathid);
77 -- CREATE INDEX file_filenameid_idx on file(filenameid);
78
79 CREATE TABLE RestoreObject (
80    RestoreObjectId SERIAL NOT NULL,
81    ObjectName TEXT NOT NULL,
82    RestoreObject BYTEA NOT NULL,
83    PluginName TEXT NOT NULL,
84    ObjectLength INTEGER DEFAULT 0,
85    ObjectFullLength INTEGER DEFAULT 0,
86    ObjectIndex INTEGER DEFAULT 0,
87    ObjectType INTEGER DEFAULT 0,
88    FileIndex INTEGER DEFAULT 0,
89    JobId INTEGER,
90    ObjectCompression INTEGER DEFAULT 0,
91    PRIMARY KEY(RestoreObjectId)
92    );
93 CREATE INDEX restore_jobid_idx on RestoreObject(JobId);
94
95
96 CREATE TABLE Job
97 (
98     JobId             serial      not null,
99     Job               text        not null,
100     Name              text        not null,
101     Type              char(1)     not null,
102     Level             char(1)     not null,
103     ClientId          integer     default 0,
104     JobStatus         char(1)     not null,
105     SchedTime         timestamp   without time zone,
106     StartTime         timestamp   without time zone,
107     EndTime           timestamp   without time zone,
108     RealEndTime       timestamp   without time zone,
109     JobTDate          bigint      default 0,
110     VolSessionId      integer     default 0,
111     volSessionTime    integer     default 0,
112     JobFiles          integer     default 0,
113     JobBytes          bigint      default 0,
114     ReadBytes         bigint      default 0,
115     JobErrors         integer     default 0,
116     JobMissingFiles   integer     default 0,
117     PoolId            integer     default 0,
118     FilesetId         integer     default 0,
119     PriorJobid        integer     default 0,
120     PurgedFiles       smallint    default 0,
121     HasBase           smallint    default 0,
122     HasCache          smallint    default 0,
123     Reviewed          smallint    default 0,
124     Comment           text,
125     primary key (jobid)
126 );
127
128 CREATE INDEX job_name_idx on job (name);
129
130 -- Create a table like Job for long term statistics 
131 CREATE TABLE JobHisto (LIKE Job);
132 CREATE INDEX jobhisto_idx ON JobHisto ( StartTime );
133
134
135 CREATE TABLE Location (
136    LocationId         serial      not null,
137    Location           text        not null,
138    Cost               integer     default 0,
139    Enabled            smallint,
140    primary key (LocationId)
141 );
142
143
144 CREATE TABLE fileset
145 (
146     filesetid         serial      not null,
147     fileset           text        not null,
148     md5               text        not null,
149     createtime        timestamp without time zone not null,
150     primary key (filesetid)
151 );
152
153 CREATE INDEX fileset_name_idx on fileset (fileset);
154
155 CREATE TABLE jobmedia
156 (
157     jobmediaid        serial      not null,
158     jobid             integer     not null,
159     mediaid           integer     not null,
160     firstindex        integer     default 0,
161     lastindex         integer     default 0,
162     startfile         integer     default 0,
163     endfile           integer     default 0,
164     startblock        bigint      default 0,
165     endblock          bigint      default 0,
166     volindex          integer     default 0,
167     primary key (jobmediaid)
168 );
169
170 CREATE INDEX job_media_job_id_media_id_idx on jobmedia (jobid, mediaid);
171
172 CREATE TABLE media
173 (
174     mediaid           serial      not null,
175     volumename        text        not null,
176     slot              integer     default 0,
177     poolid            integer     default 0,
178     mediatype         text        not null,
179     mediatypeid       integer     default 0,
180     labeltype         integer     default 0,
181     firstwritten      timestamp   without time zone,
182     lastwritten       timestamp   without time zone,
183     labeldate         timestamp   without time zone,
184     voljobs           integer     default 0,
185     volfiles          integer     default 0,
186     volblocks         integer     default 0,
187     volmounts         integer     default 0,
188     volbytes          bigint      default 0,
189     volparts          integer     default 0,
190     volerrors         integer     default 0,
191     volwrites         integer     default 0,
192     volcapacitybytes  bigint      default 0,
193     volstatus         text        not null
194         check (volstatus in ('Full','Archive','Append',
195               'Recycle','Purged','Read-Only','Disabled',
196               'Error','Busy','Used','Cleaning','Scratch')),
197     enabled           smallint    default 1,
198     recycle           smallint    default 0,
199     ActionOnPurge     smallint    default 0,
200     volretention      bigint      default 0,
201     voluseduration    bigint      default 0,
202     maxvoljobs        integer     default 0,
203     maxvolfiles       integer     default 0,
204     maxvolbytes       bigint      default 0,
205     inchanger         smallint    default 0,
206     StorageId         integer     default 0,
207     DeviceId          integer     default 0,
208     mediaaddressing   smallint    default 0,
209     volreadtime       bigint      default 0,
210     volwritetime      bigint      default 0,
211     endfile           integer     default 0,
212     endblock          bigint      default 0,
213     LocationId        integer     default 0,
214     recyclecount      integer     default 0,
215     initialwrite      timestamp   without time zone,
216     scratchpoolid     integer     default 0,
217     recyclepoolid     integer     default 0,
218     comment           text,
219     primary key (mediaid)
220 );
221
222 create unique index media_volumename_id on media (volumename);
223
224  
225 CREATE TABLE MediaType (
226    MediaTypeId SERIAL,
227    MediaType TEXT NOT NULL,
228    ReadOnly INTEGER DEFAULT 0,
229    PRIMARY KEY(MediaTypeId)
230    );
231
232 CREATE TABLE Storage (
233    StorageId SERIAL,
234    Name TEXT NOT NULL,
235    AutoChanger INTEGER DEFAULT 0,
236    PRIMARY KEY(StorageId)
237    );
238
239 CREATE TABLE Device (
240    DeviceId SERIAL,
241    Name TEXT NOT NULL,
242    MediaTypeId INTEGER NOT NULL,
243    StorageId INTEGER NOT NULL,
244    DevMounts INTEGER NOT NULL DEFAULT 0,
245    DevReadBytes BIGINT NOT NULL DEFAULT 0,
246    DevWriteBytes BIGINT NOT NULL DEFAULT 0,
247    DevReadBytesSinceCleaning BIGINT NOT NULL DEFAULT 0,
248    DevWriteBytesSinceCleaning BIGINT NOT NULL DEFAULT 0,
249    DevReadTime BIGINT NOT NULL DEFAULT 0,
250    DevWriteTime BIGINT NOT NULL DEFAULT 0,
251    DevReadTimeSinceCleaning BIGINT NOT NULL DEFAULT 0,
252    DevWriteTimeSinceCleaning BIGINT NOT NULL DEFAULT 0,
253    CleaningDate timestamp without time zone,
254    CleaningPeriod BIGINT NOT NULL DEFAULT 0,
255    PRIMARY KEY(DeviceId)
256    );
257
258
259 CREATE TABLE pool
260 (
261     poolid            serial      not null,
262     name              text        not null,
263     numvols           integer     default 0,
264     maxvols           integer     default 0,
265     useonce           smallint    default 0,
266     usecatalog        smallint    default 0,
267     acceptanyvolume   smallint    default 0,
268     volretention      bigint      default 0,
269     voluseduration    bigint      default 0,
270     maxvoljobs        integer     default 0,
271     maxvolfiles       integer     default 0,
272     maxvolbytes       bigint      default 0,
273     autoprune         smallint    default 0,
274     recycle           smallint    default 0,
275     ActionOnPurge     smallint    default 0,
276     pooltype          text                          
277       check (pooltype in ('Backup','Copy','Cloned','Archive','Migration','Scratch')),
278     labeltype         integer     default 0,
279     labelformat       text        not null,
280     enabled           smallint    default 1,
281     scratchpoolid     integer     default 0,
282     recyclepoolid     integer     default 0,
283     NextPoolId        integer     default 0,
284     MigrationHighBytes BIGINT     DEFAULT 0,
285     MigrationLowBytes  BIGINT     DEFAULT 0,
286     MigrationTime      BIGINT     DEFAULT 0,
287     primary key (poolid)
288 );
289
290 CREATE INDEX pool_name_idx on pool (name);
291
292 CREATE TABLE client
293 (
294     clientid          serial      not null,
295     name              text        not null,
296     uname             text        not null,
297     autoprune         smallint    default 0,
298     fileretention     bigint      default 0,
299     jobretention      bigint      default 0,
300     primary key (clientid)
301 );
302
303 create unique index client_name_idx on client (name);
304
305 CREATE TABLE Log
306 (
307     LogId             serial      not null,
308     JobId             integer     not null,
309     Time              timestamp   without time zone,
310     LogText           text        not null,
311     primary key (LogId)
312 );
313 create index log_name_idx on Log (JobId);
314
315 CREATE TABLE LocationLog (
316    LocLogId SERIAL NOT NULL,
317    Date timestamp   without time zone,
318    Comment TEXT NOT NULL,
319    MediaId INTEGER DEFAULT 0,
320    LocationId INTEGER DEFAULT 0,
321    newvolstatus text not null
322         check (newvolstatus in ('Full','Archive','Append',
323               'Recycle','Purged','Read-Only','Disabled',
324               'Error','Busy','Used','Cleaning','Scratch')),
325    newenabled smallint,
326    PRIMARY KEY(LocLogId)
327 );
328
329
330
331 CREATE TABLE counters
332 (
333     counter           text        not null,
334     minvalue          integer     default 0,
335     maxvalue          integer     default 0,
336     currentvalue      integer     default 0,
337     wrapcounter       text        not null,
338     primary key (counter)
339 );
340
341
342
343 CREATE TABLE basefiles
344 (
345     baseid            serial                not null,
346     jobid             integer               not null,
347     fileid            bigint                not null,
348     fileindex         integer                       ,
349     basejobid         integer                       ,
350     primary key (baseid)
351 );
352
353 CREATE INDEX basefiles_jobid_idx ON BaseFiles ( JobId );
354
355 CREATE TABLE unsavedfiles
356 (
357     UnsavedId         integer               not null,
358     jobid             integer               not null,
359     pathid            integer               not null,
360     filenameid        integer               not null,
361     primary key (UnsavedId)
362 );
363
364 CREATE TABLE CDImages 
365 (
366    MediaId integer not null,
367    LastBurn timestamp without time zone not null,
368    primary key (MediaId)
369 );
370
371
372 CREATE TABLE PathHierarchy
373 (
374      PathId integer NOT NULL,
375      PPathId integer NOT NULL,
376      CONSTRAINT pathhierarchy_pkey PRIMARY KEY (PathId)
377 );
378
379 CREATE INDEX pathhierarchy_ppathid 
380           ON PathHierarchy (PPathId);
381
382 CREATE TABLE PathVisibility
383 (
384       PathId integer NOT NULL,
385       JobId integer NOT NULL,
386       Size int8 DEFAULT 0,
387       Files int4 DEFAULT 0,
388       CONSTRAINT pathvisibility_pkey PRIMARY KEY (JobId, PathId)
389 );
390 CREATE INDEX pathvisibility_jobid
391              ON PathVisibility (JobId);
392
393 CREATE TABLE version
394 (
395     versionid         integer               not null
396 );
397
398 CREATE TABLE Status (
399    JobStatus CHAR(1) NOT NULL,
400    JobStatusLong TEXT,
401    Severity int,
402    PRIMARY KEY (JobStatus)
403    );
404
405 INSERT INTO Status (JobStatus,JobStatusLong,Severity) VALUES
406    ('C', 'Created, not yet running',15);
407 INSERT INTO Status (JobStatus,JobStatusLong,Severity) VALUES
408    ('R', 'Running',15);
409 INSERT INTO Status (JobStatus,JobStatusLong,Severity) VALUES
410    ('B', 'Blocked',15);
411 INSERT INTO Status (JobStatus,JobStatusLong,Severity) VALUES
412    ('T', 'Completed successfully', 10);
413 INSERT INTO Status (JobStatus,JobStatusLong,Severity) VALUES
414    ('E', 'Terminated with errors', 25);
415 INSERT INTO Status (JobStatus,JobStatusLong,Severity) VALUES
416    ('e', 'Non-fatal error',20);
417 INSERT INTO Status (JobStatus,JobStatusLong,Severity) VALUES
418    ('f', 'Fatal error',100);
419 INSERT INTO Status (JobStatus,JobStatusLong,Severity) VALUES
420    ('D', 'Verify found differences',15);
421 INSERT INTO Status (JobStatus,JobStatusLong,Severity) VALUES
422    ('A', 'Canceled by user',90);
423 INSERT INTO Status (JobStatus,JobStatusLong,Severity) VALUES
424    ('F', 'Waiting for Client',15);
425 INSERT INTO Status (JobStatus,JobStatusLong,Severity) VALUES
426    ('S', 'Waiting for Storage daemon',15);
427 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
428    ('m', 'Waiting for new media');
429 INSERT INTO Status (JobStatus,JobStatusLong,Severity) VALUES
430    ('M', 'Waiting for media mount',15);
431 INSERT INTO Status (JobStatus,JobStatusLong,Severity) VALUES
432    ('s', 'Waiting for storage resource',15);
433 INSERT INTO Status (JobStatus,JobStatusLong,Severity) VALUES
434    ('j', 'Waiting for job resource',15);
435 INSERT INTO Status (JobStatus,JobStatusLong,Severity) VALUES
436    ('c', 'Waiting for client resource',15);
437 INSERT INTO Status (JobStatus,JobStatusLong,Severity) VALUES
438    ('d', 'Waiting on maximum jobs',15);
439 INSERT INTO Status (JobStatus,JobStatusLong,Severity) VALUES
440    ('t', 'Waiting on start time',15);
441 INSERT INTO Status (JobStatus,JobStatusLong,Severity) VALUES
442    ('p', 'Waiting on higher priority jobs',15);
443 INSERT INTO Status (JobStatus,JobStatusLong,Severity) VALUES
444    ('a', 'SD despooling attributes',15);
445 INSERT INTO Status (JobStatus,JobStatusLong,Severity) VALUES
446    ('i', 'Doing batch insert file records',15);
447
448 INSERT INTO Version (VersionId) VALUES (@BDB_VERSION@);
449
450 -- Make sure we have appropriate permissions
451
452
453 END-OF-DATA
454 pstat=$?
455 if test $pstat = 0; 
456 then
457    echo "Creation of Bacula PostgreSQL tables succeeded."
458 else
459    echo "Creation of Bacula PostgreSQL tables failed."
460 fi
461 exit $pstat