3 # shell script to create Bacula PostgreSQL tables
6 db_name=${db_name:-@db_name@}
8 $bindir/psql -f - -d ${db_name} $* <<END-OF-DATA
12 filenameid serial not null,
14 primary key (filenameid)
17 ALTER TABLE filename ALTER COLUMN name SET STATISTICS 1000;
18 CREATE UNIQUE INDEX filename_name_idx on filename (name);
22 pathid serial not null,
27 ALTER TABLE path ALTER COLUMN path SET STATISTICS 1000;
28 CREATE UNIQUE INDEX path_name_idx on path (path);
32 fileid bigserial not null,
33 fileindex integer not null default 0,
34 jobid integer not null,
35 pathid integer not null,
36 filenameid integer not null,
37 markid integer not null default 0,
43 CREATE INDEX file_jobid_idx on file (jobid);
44 CREATE INDEX file_fp_idx on file (filenameid, pathid);
47 -- Add this if you have a good number of job
48 -- that run at the same time
49 -- ALTER SEQUENCE file_fileid_seq CACHE 1000;
52 -- Possibly add one or more of the following indexes
53 -- if your Verifies are too slow.
55 -- CREATE INDEX file_pathid_idx on file(pathid);
56 -- CREATE INDEX file_filenameid_idx on file(filenameid);
57 -- CREATE INDEX file_jpfid_idx on file (jobid, pathid, filenameid);
61 jobid serial not null,
64 type char(1) not null,
65 level char(1) not null,
66 clientid integer default 0,
67 jobstatus char(1) not null,
68 schedtime timestamp without time zone,
69 starttime timestamp without time zone,
70 endtime timestamp without time zone,
71 realendtime timestamp without time zone,
72 jobtdate bigint default 0,
73 volsessionid integer default 0,
74 volsessiontime integer default 0,
75 jobfiles integer default 0,
76 jobbytes bigint default 0,
77 readbytes bigint default 0,
78 joberrors integer default 0,
79 jobmissingfiles integer default 0,
80 poolid integer default 0,
81 filesetid integer default 0,
82 purgedfiles smallint default 0,
83 hasbase smallint default 0,
84 priorjobid integer default 0,
88 CREATE INDEX job_name_idx on job (name);
90 -- Create a table like Job for long term statistics
91 CREATE TABLE JobHisto (LIKE Job);
92 CREATE INDEX jobhisto_idx ON jobhisto ( starttime );
95 CREATE TABLE Location (
96 LocationId serial not null,
97 Location text not null,
98 Cost integer default 0,
100 primary key (LocationId)
106 filesetid serial not null,
107 fileset text not null,
109 createtime timestamp without time zone not null,
110 primary key (filesetid)
113 CREATE INDEX fileset_name_idx on fileset (fileset);
115 CREATE TABLE jobmedia
117 jobmediaid serial not null,
118 jobid integer not null,
119 mediaid integer not null,
120 firstindex integer default 0,
121 lastindex integer default 0,
122 startfile integer default 0,
123 endfile integer default 0,
124 startblock bigint default 0,
125 endblock bigint default 0,
126 volindex integer default 0,
127 copy integer default 0,
128 primary key (jobmediaid)
131 CREATE INDEX job_media_job_id_media_id_idx on jobmedia (jobid, mediaid);
135 mediaid serial not null,
136 volumename text not null,
137 slot integer default 0,
138 poolid integer default 0,
139 mediatype text not null,
140 mediatypeid integer default 0,
141 labeltype integer default 0,
142 firstwritten timestamp without time zone,
143 lastwritten timestamp without time zone,
144 labeldate timestamp without time zone,
145 voljobs integer default 0,
146 volfiles integer default 0,
147 volblocks integer default 0,
148 volmounts integer default 0,
149 volbytes bigint default 0,
150 volparts integer default 0,
151 volerrors integer default 0,
152 volwrites integer default 0,
153 volcapacitybytes bigint default 0,
154 volstatus text not null
155 check (volstatus in ('Full','Archive','Append',
156 'Recycle','Purged','Read-Only','Disabled',
157 'Error','Busy','Used','Cleaning','Scratch')),
158 enabled smallint default 1,
159 recycle smallint default 0,
160 ActionOnPurge smallint default 0,
161 volretention bigint default 0,
162 voluseduration bigint default 0,
163 maxvoljobs integer default 0,
164 maxvolfiles integer default 0,
165 maxvolbytes bigint default 0,
166 inchanger smallint default 0,
167 StorageId integer default 0,
168 DeviceId integer default 0,
169 mediaaddressing smallint default 0,
170 volreadtime bigint default 0,
171 volwritetime bigint default 0,
172 endfile integer default 0,
173 endblock bigint default 0,
174 LocationId integer default 0,
175 recyclecount integer default 0,
176 initialwrite timestamp without time zone,
177 scratchpoolid integer default 0,
178 recyclepoolid integer default 0,
180 primary key (mediaid)
183 create unique index media_volumename_id on media (volumename);
186 CREATE TABLE MediaType (
188 MediaType TEXT NOT NULL,
189 ReadOnly INTEGER DEFAULT 0,
190 PRIMARY KEY(MediaTypeId)
193 CREATE TABLE Storage (
196 AutoChanger INTEGER DEFAULT 0,
197 PRIMARY KEY(StorageId)
200 CREATE TABLE Device (
203 MediaTypeId INTEGER NOT NULL,
204 StorageId INTEGER NOT NULL,
205 DevMounts INTEGER NOT NULL DEFAULT 0,
206 DevReadBytes BIGINT NOT NULL DEFAULT 0,
207 DevWriteBytes BIGINT NOT NULL DEFAULT 0,
208 DevReadBytesSinceCleaning BIGINT NOT NULL DEFAULT 0,
209 DevWriteBytesSinceCleaning BIGINT NOT NULL DEFAULT 0,
210 DevReadTime BIGINT NOT NULL DEFAULT 0,
211 DevWriteTime BIGINT NOT NULL DEFAULT 0,
212 DevReadTimeSinceCleaning BIGINT NOT NULL DEFAULT 0,
213 DevWriteTimeSinceCleaning BIGINT NOT NULL DEFAULT 0,
214 CleaningDate timestamp without time zone,
215 CleaningPeriod BIGINT NOT NULL DEFAULT 0,
216 PRIMARY KEY(DeviceId)
222 poolid serial not null,
224 numvols integer default 0,
225 maxvols integer default 0,
226 useonce smallint default 0,
227 usecatalog smallint default 0,
228 acceptanyvolume smallint default 0,
229 volretention bigint default 0,
230 voluseduration bigint default 0,
231 maxvoljobs integer default 0,
232 maxvolfiles integer default 0,
233 maxvolbytes bigint default 0,
234 autoprune smallint default 0,
235 recycle smallint default 0,
236 ActionOnPurge smallint default 0,
238 check (pooltype in ('Backup','Copy','Cloned','Archive','Migration','Scratch')),
239 labeltype integer default 0,
240 labelformat text not null,
241 enabled smallint default 1,
242 scratchpoolid integer default 0,
243 recyclepoolid integer default 0,
244 NextPoolId integer default 0,
245 MigrationHighBytes BIGINT DEFAULT 0,
246 MigrationLowBytes BIGINT DEFAULT 0,
247 MigrationTime BIGINT DEFAULT 0,
251 CREATE INDEX pool_name_idx on pool (name);
255 clientid serial not null,
258 autoprune smallint default 0,
259 fileretention bigint default 0,
260 jobretention bigint default 0,
261 primary key (clientid)
264 create unique index client_name_idx on client (name);
268 LogId serial not null,
269 JobId integer not null,
270 Time timestamp without time zone,
271 LogText text not null,
274 create index log_name_idx on Log (JobId);
276 CREATE TABLE LocationLog (
277 LocLogId SERIAL NOT NULL,
278 Date timestamp without time zone,
279 Comment TEXT NOT NULL,
280 MediaId INTEGER DEFAULT 0,
281 LocationId INTEGER DEFAULT 0,
282 newvolstatus text not null
283 check (newvolstatus in ('Full','Archive','Append',
284 'Recycle','Purged','Read-Only','Disabled',
285 'Error','Busy','Used','Cleaning','Scratch')),
287 PRIMARY KEY(LocLogId)
292 CREATE TABLE counters
294 counter text not null,
295 minvalue integer default 0,
296 maxvalue integer default 0,
297 currentvalue integer default 0,
298 wrapcounter text not null,
299 primary key (counter)
304 CREATE TABLE basefiles
306 baseid serial not null,
307 jobid integer not null,
308 fileid bigint not null,
314 CREATE TABLE unsavedfiles
316 UnsavedId integer not null,
317 jobid integer not null,
318 pathid integer not null,
319 filenameid integer not null,
320 primary key (UnsavedId)
323 CREATE TABLE CDImages
325 MediaId integer not null,
326 LastBurn timestamp without time zone not null,
327 primary key (MediaId)
333 versionid integer not null
336 CREATE TABLE Status (
337 JobStatus CHAR(1) NOT NULL,
339 PRIMARY KEY (JobStatus)
342 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
343 ('C', 'Created, not yet running');
344 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
346 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
348 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
349 ('T', 'Completed successfully');
350 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
351 ('E', 'Terminated with errors');
352 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
353 ('e', 'Non-fatal error');
354 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
355 ('f', 'Fatal error');
356 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
357 ('D', 'Verify found differences');
358 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
359 ('A', 'Canceled by user');
360 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
361 ('F', 'Waiting for Client');
362 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
363 ('S', 'Waiting for Storage daemon');
364 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
365 ('m', 'Waiting for new media');
366 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
367 ('M', 'Waiting for media mount');
368 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
369 ('s', 'Waiting for storage resource');
370 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
371 ('j', 'Waiting for job resource');
372 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
373 ('c', 'Waiting for client resource');
374 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
375 ('d', 'Waiting on maximum jobs');
376 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
377 ('t', 'Waiting on start time');
378 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
379 ('p', 'Waiting on higher priority jobs');
380 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
381 ('a', 'SD despooling attributes');
382 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
383 ('i', 'Doing batch insert file records');
385 INSERT INTO Version (VersionId) VALUES (11);
387 -- Make sure we have appropriate permissions
394 echo "Creation of Bacula PostgreSQL tables succeeded."
396 echo "Creation of Bacula PostgreSQL tables failed."