1 -- SQL to create the initial tables for the MediaWiki database.
2 -- This is read and executed by the install script; you should
3 -- not have to run it by itself unless doing a manual install.
4 -- This is the PostgreSQL version.
5 -- For information about each table, please see the notes in maintenance/tables.sql
6 -- Please make sure all dollar-quoting uses $mw$ at the start of the line
7 -- We can't use SERIAL everywhere: the sequence names are hard-coded into the PHP
8 -- TODO: Change CHAR to BOOL
11 SET client_min_messages
= 'ERROR';
13 CREATE SEQUENCE user_user_id_seq
MINVALUE 0 START WITH 0;
14 CREATE TABLE mwuser ( -- replace reserved word 'user'
15 user_id
INTEGER NOT NULL PRIMARY KEY DEFAULT nextval('user_user_id_seq'),
16 user_name
TEXT NOT NULL UNIQUE,
19 user_newpassword
TEXT,
20 user_newpass_time TIMESTAMPTZ
,
23 user_email_token
CHAR(32),
24 user_email_token_expires TIMESTAMPTZ
,
25 user_email_authenticated TIMESTAMPTZ
,
27 user_touched TIMESTAMPTZ
,
28 user_registration TIMESTAMPTZ
,
29 user_editcount
INTEGER
31 CREATE INDEX user_email_token_idx
ON mwuser (user_email_token
);
33 -- Create a dummy user to satisfy fk contraints especially with revisions
35 VALUES (DEFAULT,'Anonymous','',NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,now(),now());
37 CREATE TABLE user_groups (
38 ug_user
INTEGER NULL REFERENCES mwuser(user_id
) ON DELETE CASCADE,
39 ug_group
TEXT NOT NULL
41 CREATE UNIQUE INDEX user_groups_unique
ON user_groups (ug_user
, ug_group
);
43 CREATE TABLE user_newtalk (
44 user_id
INTEGER NOT NULL REFERENCES mwuser(user_id
) ON DELETE CASCADE,
47 CREATE INDEX user_newtalk_id_idx
ON user_newtalk (user_id
);
48 CREATE INDEX user_newtalk_ip_idx
ON user_newtalk (user_ip
);
51 CREATE SEQUENCE page_page_id_seq
;
53 page_id
INTEGER NOT NULL PRIMARY KEY DEFAULT nextval('page_page_id_seq'),
54 page_namespace
SMALLINT NOT NULL,
55 page_title
TEXT NOT NULL,
56 page_restrictions
TEXT,
57 page_counter
BIGINT NOT NULL DEFAULT 0,
58 page_is_redirect
CHAR NOT NULL DEFAULT 0,
59 page_is_new
CHAR NOT NULL DEFAULT 0,
60 page_random
NUMERIC(15,14) NOT NULL DEFAULT RANDOM(),
61 page_touched TIMESTAMPTZ
,
62 page_latest
INTEGER NOT NULL, -- FK?
63 page_len
INTEGER NOT NULL
65 CREATE UNIQUE INDEX page_unique_name
ON page (page_namespace
, page_title
);
66 CREATE INDEX page_main_title
ON page (page_title
) WHERE page_namespace
= 0;
67 CREATE INDEX page_talk_title
ON page (page_title
) WHERE page_namespace
= 1;
68 CREATE INDEX page_user_title
ON page (page_title
) WHERE page_namespace
= 2;
69 CREATE INDEX page_utalk_title
ON page (page_title
) WHERE page_namespace
= 3;
70 CREATE INDEX page_project_title
ON page (page_title
) WHERE page_namespace
= 4;
71 CREATE INDEX page_random_idx
ON page (page_random
);
72 CREATE INDEX page_len_idx
ON page (page_len
);
74 CREATE FUNCTION page_deleted() RETURNS TRIGGER LANGUAGE plpgsql
AS
77 DELETE FROM recentchanges
WHERE rc_namespace
= OLD.page_namespace
AND rc_title
= OLD.page_title
;
82 CREATE TRIGGER page_deleted
AFTER DELETE ON page
83 FOR EACH ROW EXECUTE PROCEDURE page_deleted();
85 CREATE SEQUENCE rev_rev_id_val
;
86 CREATE TABLE revision (
87 rev_id
INTEGER NOT NULL UNIQUE DEFAULT nextval('rev_rev_id_val'),
88 rev_page
INTEGER NULL REFERENCES page (page_id
) ON DELETE CASCADE,
89 rev_text_id
INTEGER NULL, -- FK
91 rev_user
INTEGER NOT NULL REFERENCES mwuser(user_id
) ON DELETE RESTRICT,
92 rev_user_text
TEXT NOT NULL,
93 rev_timestamp TIMESTAMPTZ
NOT NULL,
94 rev_minor_edit
CHAR NOT NULL DEFAULT '0',
95 rev_deleted
CHAR NOT NULL DEFAULT '0'
97 CREATE UNIQUE INDEX revision_unique
ON revision (rev_page
, rev_id
);
98 CREATE INDEX rev_text_id_idx
ON revision (rev_text_id
);
99 CREATE INDEX rev_timestamp_idx
ON revision (rev_timestamp
);
100 CREATE INDEX rev_user_idx
ON revision (rev_user
);
101 CREATE INDEX rev_user_text_idx
ON revision (rev_user_text
);
104 CREATE SEQUENCE text_old_id_val
;
105 CREATE TABLE pagecontent ( -- replaces reserved word 'text'
106 old_id
INTEGER NOT NULL PRIMARY KEY DEFAULT nextval('text_old_id_val'),
112 CREATE SEQUENCE pr_id_val
;
113 CREATE TABLE page_restrictions (
114 pr_id
INTEGER NOT NULL PRIMARY KEY DEFAULT nextval('pr_id_val'),
115 pr_page
INTEGER NULL REFERENCES page (page_id
) ON DELETE CASCADE,
116 pr_type
TEXT NOT NULL,
117 pr_level
TEXT NOT NULL,
118 pr_cascade
SMALLINT NOT NULL,
119 pr_user
INTEGER NULL,
120 pr_expiry TIMESTAMPTZ
NULL
122 CREATE UNIQUE INDEX pr_pagetype
ON page_restrictions (pr_page
,pr_type
);
125 CREATE TABLE archive2 (
126 ar_namespace
SMALLINT NOT NULL,
127 ar_title
TEXT NOT NULL,
130 ar_user
INTEGER NULL REFERENCES mwuser(user_id
) ON DELETE SET NULL,
131 ar_user_text
TEXT NOT NULL,
132 ar_timestamp TIMESTAMPTZ
NOT NULL,
133 ar_minor_edit
CHAR NOT NULL DEFAULT '0',
138 CREATE INDEX archive_name_title_timestamp
ON archive2 (ar_namespace
,ar_title
,ar_timestamp
);
140 -- This is the easiest way to work around the char(15) timestamp hack without modifying PHP code
141 CREATE VIEW archive
AS
143 ar_namespace
, ar_title
, ar_text
, ar_comment
, ar_user
, ar_user_text
,
144 ar_minor_edit
, ar_flags
, ar_rev_id
, ar_text_id
,
145 TO_CHAR(ar_timestamp
, 'YYYYMMDDHH24MISS') AS ar_timestamp
148 CREATE RULE archive_insert
AS ON INSERT TO archive
149 DO INSTEAD INSERT INTO archive2
VALUES (
150 NEW.ar_namespace
, NEW.ar_title
, NEW.ar_text
, NEW.ar_comment
, NEW.ar_user
, NEW.ar_user_text
,
151 TO_TIMESTAMP(NEW.ar_timestamp
, 'YYYYMMDDHH24MISS'),
152 NEW.ar_minor_edit
, NEW.ar_flags
, NEW.ar_rev_id
, NEW.ar_text_id
155 CREATE RULE archive_delete
AS ON DELETE TO archive
156 DO INSTEAD DELETE FROM archive2 a2
WHERE
157 a2.ar_title
= OLD.ar_title
AND
158 a2.ar_namespace
= OLD.ar_namespace
AND
159 a2.ar_rev_id
= OLD.ar_rev_id
;
162 CREATE TABLE redirect (
163 rd_from
INTEGER NOT NULL REFERENCES page(page_id
) ON DELETE CASCADE,
164 rd_namespace
SMALLINT NOT NULL,
165 rd_title
TEXT NOT NULL
167 CREATE INDEX redirect_ns_title
ON redirect (rd_namespace
,rd_title
,rd_from
);
170 CREATE TABLE pagelinks (
171 pl_from
INTEGER NOT NULL REFERENCES page(page_id
) ON DELETE CASCADE,
172 pl_namespace
SMALLINT NOT NULL,
173 pl_title
TEXT NOT NULL
175 CREATE UNIQUE INDEX pagelink_unique
ON pagelinks (pl_from
,pl_namespace
,pl_title
);
177 CREATE TABLE templatelinks (
178 tl_from
INTEGER NOT NULL REFERENCES page(page_id
) ON DELETE CASCADE,
179 tl_namespace
TEXT NOT NULL,
180 tl_title
TEXT NOT NULL
182 CREATE UNIQUE INDEX templatelinks_unique
ON templatelinks (tl_namespace
,tl_title
,tl_from
);
184 CREATE TABLE imagelinks (
185 il_from
INTEGER NOT NULL REFERENCES page(page_id
) ON DELETE CASCADE,
188 CREATE UNIQUE INDEX il_from
ON imagelinks (il_to
,il_from
);
190 CREATE TABLE categorylinks (
191 cl_from
INTEGER NOT NULL REFERENCES page(page_id
) ON DELETE CASCADE,
194 cl_timestamp TIMESTAMPTZ
NOT NULL
196 CREATE UNIQUE INDEX cl_from
ON categorylinks (cl_from
, cl_to
);
197 CREATE INDEX cl_sortkey
ON categorylinks (cl_to
, cl_sortkey
);
199 CREATE TABLE externallinks (
200 el_from
INTEGER NOT NULL REFERENCES page(page_id
) ON DELETE CASCADE,
202 el_index
TEXT NOT NULL
204 CREATE INDEX externallinks_from_to
ON externallinks (el_from
,el_to
);
205 CREATE INDEX externallinks_index
ON externallinks (el_index
);
207 CREATE TABLE langlinks (
208 ll_from
INTEGER NOT NULL REFERENCES page (page_id
) ON DELETE CASCADE,
212 CREATE UNIQUE INDEX langlinks_unique
ON langlinks (ll_from
,ll_lang
);
213 CREATE INDEX langlinks_lang_title
ON langlinks (ll_lang
,ll_title
);
216 CREATE TABLE site_stats (
217 ss_row_id
INTEGER NOT NULL UNIQUE,
218 ss_total_views
INTEGER DEFAULT 0,
219 ss_total_edits
INTEGER DEFAULT 0,
220 ss_good_articles
INTEGER DEFAULT 0,
221 ss_total_pages
INTEGER DEFAULT -1,
222 ss_users
INTEGER DEFAULT -1,
223 ss_admins
INTEGER DEFAULT -1,
224 ss_images
INTEGER DEFAULT 0
227 CREATE TABLE hitcounter (
228 hc_id
BIGINT NOT NULL
232 CREATE SEQUENCE ipblocks_ipb_id_val
;
233 CREATE TABLE ipblocks (
234 ipb_id
INTEGER NOT NULL PRIMARY KEY DEFAULT nextval('ipblocks_ipb_id_val'),
235 ipb_address
TEXT NULL,
236 ipb_user
INTEGER NULL REFERENCES mwuser(user_id
) ON DELETE SET NULL,
237 ipb_by
INTEGER NOT NULL REFERENCES mwuser(user_id
) ON DELETE CASCADE,
238 ipb_reason
TEXT NOT NULL,
239 ipb_timestamp TIMESTAMPTZ
NOT NULL,
240 ipb_auto
CHAR NOT NULL DEFAULT '0',
241 ipb_anon_only
CHAR NOT NULL DEFAULT '0',
242 ipb_create_account
CHAR NOT NULL DEFAULT '1',
243 ipb_enable_autoblock
CHAR NOT NULL DEFAULT '1',
244 ipb_expiry TIMESTAMPTZ
NOT NULL,
245 ipb_range_start
TEXT,
248 CREATE INDEX ipb_address
ON ipblocks (ipb_address
);
249 CREATE INDEX ipb_user
ON ipblocks (ipb_user
);
250 CREATE INDEX ipb_range
ON ipblocks (ipb_range_start
,ipb_range_end
);
254 img_name
TEXT NOT NULL PRIMARY KEY,
255 img_size
INTEGER NOT NULL,
256 img_width
INTEGER NOT NULL,
257 img_height
INTEGER NOT NULL,
261 img_major_mime
TEXT DEFAULT 'unknown',
262 img_minor_mime
TEXT DEFAULT 'unknown',
263 img_description
TEXT NOT NULL,
264 img_user
INTEGER NULL REFERENCES mwuser(user_id
) ON DELETE SET NULL,
265 img_user_text
TEXT NOT NULL,
266 img_timestamp TIMESTAMPTZ
268 CREATE INDEX img_size_idx
ON image (img_size
);
269 CREATE INDEX img_timestamp_idx
ON image (img_timestamp
);
271 CREATE TABLE oldimage (
272 oi_name
TEXT NOT NULL REFERENCES image(img_name
),
273 oi_archive_name
TEXT NOT NULL,
274 oi_size
INTEGER NOT NULL,
275 oi_width
INTEGER NOT NULL,
276 oi_height
INTEGER NOT NULL,
277 oi_bits
SMALLINT NOT NULL,
279 oi_user
INTEGER NULL REFERENCES mwuser(user_id
) ON DELETE SET NULL,
280 oi_user_text
TEXT NOT NULL,
281 oi_timestamp TIMESTAMPTZ
NOT NULL
283 CREATE INDEX oi_name
ON oldimage (oi_name
);
286 CREATE TABLE filearchive (
287 fa_id SERIAL
NOT NULL PRIMARY KEY,
288 fa_name
TEXT NOT NULL,
289 fa_archive_name
TEXT,
290 fa_storage_group
VARCHAR(16),
291 fa_storage_key
CHAR(64),
292 fa_deleted_user
INTEGER NULL REFERENCES mwuser(user_id
) ON DELETE SET NULL,
293 fa_deleted_timestamp TIMESTAMPTZ
NOT NULL,
294 fa_deleted_reason
TEXT,
295 fa_size
SMALLINT NOT NULL,
296 fa_width
SMALLINT NOT NULL,
297 fa_height
SMALLINT NOT NULL,
301 fa_major_mime
TEXT DEFAULT 'unknown',
302 fa_minor_mime
TEXT DEFAULT 'unknown',
303 fa_description
TEXT NOT NULL,
304 fa_user
INTEGER NULL REFERENCES mwuser(user_id
) ON DELETE SET NULL,
305 fa_user_text
TEXT NOT NULL,
306 fa_timestamp TIMESTAMPTZ
308 CREATE INDEX fa_name_time
ON filearchive (fa_name
, fa_timestamp
);
309 CREATE INDEX fa_dupe
ON filearchive (fa_storage_group
, fa_storage_key
);
310 CREATE INDEX fa_notime
ON filearchive (fa_deleted_timestamp
);
311 CREATE INDEX fa_nouser
ON filearchive (fa_deleted_user
);
314 CREATE SEQUENCE rc_rc_id_seq
;
315 CREATE TABLE recentchanges (
316 rc_id
INTEGER NOT NULL PRIMARY KEY DEFAULT nextval('rc_rc_id_seq'),
317 rc_timestamp TIMESTAMPTZ
NOT NULL,
318 rc_cur_time TIMESTAMPTZ
NOT NULL,
319 rc_user
INTEGER NULL REFERENCES mwuser(user_id
) ON DELETE SET NULL,
320 rc_user_text
TEXT NOT NULL,
321 rc_namespace
SMALLINT NOT NULL,
322 rc_title
TEXT NOT NULL,
324 rc_minor
CHAR NOT NULL DEFAULT '0',
325 rc_bot
CHAR NOT NULL DEFAULT '0',
326 rc_new
CHAR NOT NULL DEFAULT '0',
327 rc_cur_id
INTEGER NULL REFERENCES page(page_id
) ON DELETE SET NULL,
328 rc_this_oldid
INTEGER NOT NULL,
329 rc_last_oldid
INTEGER NOT NULL,
330 rc_type
CHAR NOT NULL DEFAULT '0',
331 rc_moved_to_ns
SMALLINT,
332 rc_moved_to_title
TEXT,
333 rc_patrolled
CHAR NOT NULL DEFAULT '0',
338 CREATE INDEX rc_timestamp
ON recentchanges (rc_timestamp
);
339 CREATE INDEX rc_namespace_title
ON recentchanges (rc_namespace
, rc_title
);
340 CREATE INDEX rc_cur_id
ON recentchanges (rc_cur_id
);
341 CREATE INDEX new_name_timestamp
ON recentchanges (rc_new
, rc_namespace
, rc_timestamp
);
342 CREATE INDEX rc_ip
ON recentchanges (rc_ip
);
345 CREATE TABLE watchlist (
346 wl_user
INTEGER NOT NULL REFERENCES mwuser(user_id
) ON DELETE CASCADE,
347 wl_namespace
SMALLINT NOT NULL DEFAULT 0,
348 wl_title
TEXT NOT NULL,
349 wl_notificationtimestamp TIMESTAMPTZ
351 CREATE UNIQUE INDEX wl_user_namespace_title
ON watchlist (wl_namespace
, wl_title
, wl_user
);
355 math_inputhash
TEXT NOT NULL UNIQUE,
356 math_outputhash
TEXT NOT NULL,
357 math_html_conservativeness
SMALLINT NOT NULL,
363 CREATE TABLE interwiki (
364 iw_prefix
TEXT NOT NULL UNIQUE,
365 iw_url
TEXT NOT NULL,
366 iw_local
CHAR NOT NULL,
367 iw_trans
CHAR NOT NULL DEFAULT '0'
371 CREATE TABLE querycache (
372 qc_type
TEXT NOT NULL,
373 qc_value
SMALLINT NOT NULL,
374 qc_namespace
SMALLINT NOT NULL,
375 qc_title
TEXT NOT NULL
377 CREATE INDEX querycache_type_value
ON querycache (qc_type
, qc_value
);
379 CREATE TABLE querycache_info (
380 qci_type
TEXT UNIQUE,
381 qci_timestamp TIMESTAMPTZ
NULL
384 CREATE TABLE querycachetwo (
385 qcc_type
TEXT NOT NULL,
386 qcc_value
SMALLINT NOT NULL DEFAULT 0,
387 qcc_namespace
INTEGER NOT NULL DEFAULT 0,
388 qcc_title
TEXT NOT NULL DEFAULT '',
389 qcc_namespacetwo
INTEGER NOT NULL DEFAULT 0,
390 qcc_titletwo
TEXT NOT NULL DEFAULT ''
392 CREATE INDEX querycachetwo_type_value
ON querycachetwo (qcc_type
, qcc_value
);
393 CREATE INDEX querycachetwo_title
ON querycachetwo (qcc_type
,qcc_namespace
,qcc_title
);
394 CREATE INDEX querycachetwo_titletwo
ON querycachetwo (qcc_type
,qcc_namespacetwo
,qcc_titletwo
);
397 CREATE TABLE objectcache (
398 keyname
CHAR(255) UNIQUE,
399 value BYTEA
NOT NULL DEFAULT '',
400 exptime TIMESTAMPTZ
NOT NULL
402 CREATE INDEX objectcacache_exptime
ON objectcache (exptime
);
404 CREATE TABLE transcache (
405 tc_url
TEXT NOT NULL UNIQUE,
406 tc_contents
TEXT NOT NULL,
407 tc_time TIMESTAMPTZ
NOT NULL
411 CREATE TABLE logging (
412 log_type
TEXT NOT NULL,
413 log_action
TEXT NOT NULL,
414 log_timestamp TIMESTAMPTZ
NOT NULL,
415 log_user
INTEGER REFERENCES mwuser(user_id
) ON DELETE SET NULL,
416 log_namespace
SMALLINT NOT NULL,
417 log_title
TEXT NOT NULL,
421 CREATE INDEX logging_type_name
ON logging (log_type
, log_timestamp
);
422 CREATE INDEX logging_user_time
ON logging (log_timestamp
, log_user
);
423 CREATE INDEX logging_page_time
ON logging (log_namespace
, log_title
, log_timestamp
);
426 CREATE TABLE trackbacks (
427 tb_id SERIAL
NOT NULL PRIMARY KEY,
428 tb_page
INTEGER REFERENCES page(page_id
) ON DELETE CASCADE,
429 tb_title
TEXT NOT NULL,
430 tb_url
TEXT NOT NULL,
434 CREATE INDEX trackback_page
ON trackbacks (tb_page
);
437 CREATE SEQUENCE job_job_id_seq
;
439 job_id
INTEGER NOT NULL PRIMARY KEY DEFAULT nextval('job_job_id_seq'),
440 job_cmd
TEXT NOT NULL,
441 job_namespace
SMALLINT NOT NULL,
442 job_title
TEXT NOT NULL,
443 job_params
TEXT NOT NULL
445 CREATE INDEX job_cmd_namespace_title
ON job (job_cmd
, job_namespace
, job_title
);
447 -- Tsearch2 2 stuff. Will fail if we don't have proper access to the tsearch2 tables
449 ALTER TABLE page
ADD titlevector tsvector
;
450 CREATE FUNCTION ts2_page_title() RETURNS TRIGGER LANGUAGE plpgsql
AS
453 IF TG_OP
= 'INSERT' THEN
454 NEW.titlevector
= to_tsvector('default',NEW.page_title
);
455 ELSIF
NEW.page_title
!= OLD.page_title
THEN
456 NEW.titlevector
:= to_tsvector('default',NEW.page_title
);
462 CREATE TRIGGER ts2_page_title
BEFORE INSERT OR UPDATE ON page
463 FOR EACH ROW EXECUTE PROCEDURE ts2_page_title();
466 ALTER TABLE pagecontent
ADD textvector tsvector
;
467 CREATE FUNCTION ts2_page_text() RETURNS TRIGGER LANGUAGE plpgsql
AS
470 IF TG_OP
= 'INSERT' THEN
471 NEW.textvector
= to_tsvector('default',NEW.old_text
);
472 ELSIF
NEW.old_text
!= OLD.old_text
THEN
473 NEW.textvector
:= to_tsvector('default',NEW.old_text
);
479 CREATE TRIGGER ts2_page_text
BEFORE INSERT OR UPDATE ON pagecontent
480 FOR EACH ROW EXECUTE PROCEDURE ts2_page_text();
482 -- These are added by the setup script due to version compatibility issues
483 -- If using 8.1, switch from "gin" to "gist"
484 -- CREATE INDEX ts2_page_title ON page USING gin(titlevector);
485 -- CREATE INDEX ts2_page_text ON pagecontent USING gin(textvector);
487 CREATE FUNCTION add_interwiki (TEXT,INT,CHAR) RETURNS INT LANGUAGE SQL AS
489 INSERT INTO interwiki (iw_prefix
, iw_url
, iw_local
) VALUES ($
1,$
2,$
3);
493 -- This table is not used unless profiling is turned on
494 CREATE TABLE profiling (
495 pf_count
INTEGER NOT NULL DEFAULT 0,
496 pf_time
NUMERIC(18,10) NOT NULL DEFAULT 0,
497 pf_name
TEXT NOT NULL,
500 CREATE UNIQUE INDEX pf_name_server
ON profiling (pf_name
, pf_server
);
503 CREATE TABLE mediawiki_version (
505 mw_version
TEXT NOT NULL,
508 pg_version
TEXT NULL,
513 ts2_schema
TEXT NULL,
516 sql_version
TEXT NULL,
518 cdate TIMESTAMPTZ
NOT NULL DEFAULT now()
521 INSERT INTO mediawiki_version (type,mw_version
,sql_version
,sql_date
)
522 VALUES ('Creation','??','$LastChangedRevision$','$LastChangedDate$');