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 -- TODO: Change CHAR/SMALLINT to BOOL (still used in a non-bool fashion in PHP code)
10 SET client_min_messages = 'ERROR';
12 DROP SEQUENCE IF EXISTS user_user_id_seq CASCADE;
13 DROP SEQUENCE IF EXISTS page_page_id_seq CASCADE;
14 DROP SEQUENCE IF EXISTS revision_rev_id_seq CASCADE;
15 DROP SEQUENCE IF EXISTS text_old_id_seq CASCADE;
16 DROP SEQUENCE IF EXISTS page_restrictions_pr_id_seq CASCADE;
17 DROP SEQUENCE IF EXISTS ipblocks_ipb_id_seq CASCADE;
18 DROP SEQUENCE IF EXISTS filearchive_fa_id_seq CASCADE;
19 DROP SEQUENCE IF EXISTS uploadstash_us_id_seq CASCADE;
20 DROP SEQUENCE IF EXISTS recentchanges_rc_id_seq CASCADE;
21 DROP SEQUENCE IF EXISTS watchlist_wl_id_seq CASCADE;
22 DROP SEQUENCE IF EXISTS logging_log_id_seq CASCADE;
23 DROP SEQUENCE IF EXISTS job_job_id_seq CASCADE;
24 DROP SEQUENCE IF EXISTS category_cat_id_seq CASCADE;
25 DROP SEQUENCE IF EXISTS archive_ar_id_seq CASCADE;
26 DROP SEQUENCE IF EXISTS externallinks_el_id_seq CASCADE;
27 DROP SEQUENCE IF EXISTS sites_site_id_seq CASCADE;
28 DROP SEQUENCE IF EXISTS change_tag_ct_id_seq CASCADE;
29 DROP SEQUENCE IF EXISTS tag_summary_ts_id_seq CASCADE;
30 DROP FUNCTION IF EXISTS page_deleted() CASCADE;
31 DROP FUNCTION IF EXISTS ts2_page_title() CASCADE;
32 DROP FUNCTION IF EXISTS ts2_page_text() CASCADE;
33 DROP FUNCTION IF EXISTS add_interwiki(TEXT,INT,SMALLINT) CASCADE;
34 DROP TYPE IF EXISTS media_type CASCADE;
36 CREATE SEQUENCE user_user_id_seq MINVALUE 0 START WITH 0;
37 CREATE TABLE mwuser ( -- replace reserved word 'user'
38 user_id INTEGER NOT NULL PRIMARY KEY DEFAULT nextval('user_user_id_seq'),
39 user_name TEXT NOT NULL UNIQUE,
42 user_newpassword TEXT,
43 user_newpass_time TIMESTAMPTZ,
46 user_email_token TEXT,
47 user_email_token_expires TIMESTAMPTZ,
48 user_email_authenticated TIMESTAMPTZ,
49 user_touched TIMESTAMPTZ,
50 user_registration TIMESTAMPTZ,
51 user_editcount INTEGER,
52 user_password_expires TIMESTAMPTZ NULL
54 CREATE INDEX user_email_token_idx ON mwuser (user_email_token);
56 -- Create a dummy user to satisfy fk contraints especially with revisions
58 VALUES (DEFAULT,'Anonymous','',NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,now(),now());
60 CREATE TABLE user_groups (
61 ug_user INTEGER NULL REFERENCES mwuser(user_id) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
62 ug_group TEXT NOT NULL
64 CREATE UNIQUE INDEX user_groups_unique ON user_groups (ug_user, ug_group);
66 CREATE TABLE user_former_groups (
67 ufg_user INTEGER NULL REFERENCES mwuser(user_id) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
68 ufg_group TEXT NOT NULL
70 CREATE UNIQUE INDEX ufg_user_group ON user_former_groups (ufg_user, ufg_group);
72 CREATE TABLE user_newtalk (
73 user_id INTEGER NOT NULL REFERENCES mwuser(user_id) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
75 user_last_timestamp TIMESTAMPTZ
77 CREATE INDEX user_newtalk_id_idx ON user_newtalk (user_id);
78 CREATE INDEX user_newtalk_ip_idx ON user_newtalk (user_ip);
80 CREATE TABLE bot_passwords (
81 bp_user INTEGER NOT NULL,
82 bp_app_id TEXT NOT NULL,
83 bp_password TEXT NOT NULL,
84 bp_token TEXT NOT NULL,
85 bp_restrictions TEXT NOT NULL,
86 bp_grants TEXT NOT NULL,
87 PRIMARY KEY ( bp_user, bp_app_id )
90 CREATE SEQUENCE page_page_id_seq;
92 page_id INTEGER NOT NULL PRIMARY KEY DEFAULT nextval('page_page_id_seq'),
93 page_namespace SMALLINT NOT NULL,
94 page_title TEXT NOT NULL,
95 page_restrictions TEXT,
96 page_is_redirect SMALLINT NOT NULL DEFAULT 0,
97 page_is_new SMALLINT NOT NULL DEFAULT 0,
98 page_random NUMERIC(15,14) NOT NULL DEFAULT RANDOM(),
99 page_touched TIMESTAMPTZ,
100 page_links_updated TIMESTAMPTZ NULL,
101 page_latest INTEGER NOT NULL, -- FK?
102 page_len INTEGER NOT NULL,
103 page_content_model TEXT,
104 page_lang TEXT DEFAULT NULL
106 CREATE UNIQUE INDEX page_unique_name ON page (page_namespace, page_title);
107 CREATE INDEX page_main_title ON page (page_title text_pattern_ops) WHERE page_namespace = 0;
108 CREATE INDEX page_talk_title ON page (page_title text_pattern_ops) WHERE page_namespace = 1;
109 CREATE INDEX page_user_title ON page (page_title text_pattern_ops) WHERE page_namespace = 2;
110 CREATE INDEX page_utalk_title ON page (page_title text_pattern_ops) WHERE page_namespace = 3;
111 CREATE INDEX page_project_title ON page (page_title text_pattern_ops) WHERE page_namespace = 4;
112 CREATE INDEX page_mediawiki_title ON page (page_title text_pattern_ops) WHERE page_namespace = 8;
113 CREATE INDEX page_random_idx ON page (page_random);
114 CREATE INDEX page_len_idx ON page (page_len);
116 CREATE FUNCTION page_deleted() RETURNS TRIGGER LANGUAGE plpgsql AS
119 DELETE FROM recentchanges WHERE rc_namespace = OLD.page_namespace AND rc_title = OLD.page_title;
124 CREATE TRIGGER page_deleted AFTER DELETE ON page
125 FOR EACH ROW EXECUTE PROCEDURE page_deleted();
127 CREATE SEQUENCE revision_rev_id_seq;
128 CREATE TABLE revision (
129 rev_id INTEGER NOT NULL UNIQUE DEFAULT nextval('revision_rev_id_seq'),
130 rev_page INTEGER NULL REFERENCES page (page_id) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
131 rev_text_id INTEGER NULL, -- FK
133 rev_user INTEGER NOT NULL REFERENCES mwuser(user_id) ON DELETE RESTRICT DEFERRABLE INITIALLY DEFERRED,
134 rev_user_text TEXT NOT NULL,
135 rev_timestamp TIMESTAMPTZ NOT NULL,
136 rev_minor_edit SMALLINT NOT NULL DEFAULT 0,
137 rev_deleted SMALLINT NOT NULL DEFAULT 0,
138 rev_len INTEGER NULL,
139 rev_parent_id INTEGER NULL,
140 rev_sha1 TEXT NOT NULL DEFAULT '',
141 rev_content_model TEXT,
142 rev_content_format TEXT
144 CREATE UNIQUE INDEX revision_unique ON revision (rev_page, rev_id);
145 CREATE INDEX rev_text_id_idx ON revision (rev_text_id);
146 CREATE INDEX rev_timestamp_idx ON revision (rev_timestamp);
147 CREATE INDEX rev_user_idx ON revision (rev_user);
148 CREATE INDEX rev_user_text_idx ON revision (rev_user_text);
151 CREATE SEQUENCE text_old_id_seq;
152 CREATE TABLE pagecontent ( -- replaces reserved word 'text'
153 old_id INTEGER NOT NULL PRIMARY KEY DEFAULT nextval('text_old_id_seq'),
159 CREATE SEQUENCE page_restrictions_pr_id_seq;
160 CREATE TABLE page_restrictions (
161 pr_id INTEGER NOT NULL UNIQUE DEFAULT nextval('page_restrictions_pr_id_seq'),
162 pr_page INTEGER NULL REFERENCES page (page_id) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
163 pr_type TEXT NOT NULL,
164 pr_level TEXT NOT NULL,
165 pr_cascade SMALLINT NOT NULL,
166 pr_user INTEGER NULL,
167 pr_expiry TIMESTAMPTZ NULL
169 ALTER TABLE page_restrictions ADD CONSTRAINT page_restrictions_pk PRIMARY KEY (pr_page,pr_type);
171 CREATE TABLE page_props (
172 pp_page INTEGER NOT NULL REFERENCES page (page_id) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
173 pp_propname TEXT NOT NULL,
174 pp_value TEXT NOT NULL,
177 ALTER TABLE page_props ADD CONSTRAINT page_props_pk PRIMARY KEY (pp_page,pp_propname);
178 CREATE INDEX page_props_propname ON page_props (pp_propname);
179 CREATE UNIQUE INDEX pp_propname_page ON page_props (pp_propname,pp_page);
180 CREATE INDEX pp_propname_sortkey_page ON page_props (pp_propname, pp_sortkey, pp_page) WHERE (pp_sortkey IS NOT NULL);
182 CREATE SEQUENCE archive_ar_id_seq;
183 CREATE TABLE archive (
184 ar_id INTEGER NOT NULL PRIMARY KEY DEFAULT nextval('archive_ar_id_seq'),
185 ar_namespace SMALLINT NOT NULL,
186 ar_title TEXT NOT NULL,
187 ar_text TEXT, -- technically should be bytea, but not used anymore
188 ar_page_id INTEGER NULL,
189 ar_parent_id INTEGER NULL,
190 ar_sha1 TEXT NOT NULL DEFAULT '',
192 ar_user INTEGER NULL REFERENCES mwuser(user_id) ON DELETE SET NULL DEFERRABLE INITIALLY DEFERRED,
193 ar_user_text TEXT NOT NULL,
194 ar_timestamp TIMESTAMPTZ NOT NULL,
195 ar_minor_edit SMALLINT NOT NULL DEFAULT 0,
199 ar_deleted SMALLINT NOT NULL DEFAULT 0,
201 ar_content_model TEXT,
202 ar_content_format TEXT
204 CREATE INDEX archive_name_title_timestamp ON archive (ar_namespace,ar_title,ar_timestamp);
205 CREATE INDEX archive_user_text ON archive (ar_user_text);
208 CREATE TABLE redirect (
209 rd_from INTEGER NOT NULL REFERENCES page(page_id) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
210 rd_namespace SMALLINT NOT NULL,
211 rd_title TEXT NOT NULL,
212 rd_interwiki TEXT NULL,
213 rd_fragment TEXT NULL
215 CREATE INDEX redirect_ns_title ON redirect (rd_namespace,rd_title,rd_from);
218 CREATE TABLE pagelinks (
219 pl_from INTEGER NOT NULL REFERENCES page(page_id) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
220 pl_from_namespace INTEGER NOT NULL DEFAULT 0,
221 pl_namespace SMALLINT NOT NULL,
222 pl_title TEXT NOT NULL
224 CREATE UNIQUE INDEX pagelink_unique ON pagelinks (pl_from,pl_namespace,pl_title);
225 CREATE INDEX pagelinks_title ON pagelinks (pl_title);
227 CREATE TABLE templatelinks (
228 tl_from INTEGER NOT NULL REFERENCES page(page_id) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
229 tl_from_namespace INTEGER NOT NULL DEFAULT 0,
230 tl_namespace SMALLINT NOT NULL,
231 tl_title TEXT NOT NULL
233 CREATE UNIQUE INDEX templatelinks_unique ON templatelinks (tl_namespace,tl_title,tl_from);
234 CREATE INDEX templatelinks_from ON templatelinks (tl_from);
236 CREATE TABLE imagelinks (
237 il_from INTEGER NOT NULL REFERENCES page(page_id) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
238 il_from_namespace INTEGER NOT NULL DEFAULT 0,
241 CREATE UNIQUE INDEX il_from ON imagelinks (il_to,il_from);
243 CREATE TABLE categorylinks (
244 cl_from INTEGER NOT NULL REFERENCES page(page_id) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
246 cl_sortkey TEXT NULL,
247 cl_timestamp TIMESTAMPTZ NOT NULL,
248 cl_sortkey_prefix TEXT NOT NULL DEFAULT '',
249 cl_collation TEXT NOT NULL DEFAULT 0,
250 cl_type TEXT NOT NULL DEFAULT 'page'
252 CREATE UNIQUE INDEX cl_from ON categorylinks (cl_from, cl_to);
253 CREATE INDEX cl_sortkey ON categorylinks (cl_to, cl_sortkey, cl_from);
255 CREATE SEQUENCE externallinks_el_id_seq;
256 CREATE TABLE externallinks (
257 el_id INTEGER NOT NULL PRIMARY KEY DEFAULT nextval('externallinks_el_id_seq'),
258 el_from INTEGER NOT NULL REFERENCES page(page_id) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
260 el_index TEXT NOT NULL,
261 el_index_60 BYTEA NOT NULL DEFAULT ''
263 CREATE INDEX externallinks_from_to ON externallinks (el_from,el_to);
264 CREATE INDEX externallinks_index ON externallinks (el_index);
265 CREATE INDEX el_index_60 ON externallinks (el_index_60, el_id);
266 CREATE INDEX el_from_index_60 ON externallinks (el_from, el_index_60, el_id);
268 CREATE TABLE langlinks (
269 ll_from INTEGER NOT NULL REFERENCES page (page_id) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
273 CREATE UNIQUE INDEX langlinks_unique ON langlinks (ll_from,ll_lang);
274 CREATE INDEX langlinks_lang_title ON langlinks (ll_lang,ll_title);
277 CREATE TABLE site_stats (
278 ss_row_id INTEGER NOT NULL UNIQUE,
279 ss_total_edits INTEGER DEFAULT 0,
280 ss_good_articles INTEGER DEFAULT 0,
281 ss_total_pages INTEGER DEFAULT -1,
282 ss_users INTEGER DEFAULT -1,
283 ss_active_users INTEGER DEFAULT -1,
284 ss_admins INTEGER DEFAULT -1,
285 ss_images INTEGER DEFAULT 0
289 CREATE SEQUENCE ipblocks_ipb_id_seq;
290 CREATE TABLE ipblocks (
291 ipb_id INTEGER NOT NULL PRIMARY KEY DEFAULT nextval('ipblocks_ipb_id_seq'),
292 ipb_address TEXT NULL,
293 ipb_user INTEGER NULL REFERENCES mwuser(user_id) ON DELETE SET NULL DEFERRABLE INITIALLY DEFERRED,
294 ipb_by INTEGER NOT NULL REFERENCES mwuser(user_id) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
295 ipb_by_text TEXT NOT NULL DEFAULT '',
296 ipb_reason TEXT NOT NULL,
297 ipb_timestamp TIMESTAMPTZ NOT NULL,
298 ipb_auto SMALLINT NOT NULL DEFAULT 0,
299 ipb_anon_only SMALLINT NOT NULL DEFAULT 0,
300 ipb_create_account SMALLINT NOT NULL DEFAULT 1,
301 ipb_enable_autoblock SMALLINT NOT NULL DEFAULT 1,
302 ipb_expiry TIMESTAMPTZ NOT NULL,
303 ipb_range_start TEXT,
305 ipb_deleted SMALLINT NOT NULL DEFAULT 0,
306 ipb_block_email SMALLINT NOT NULL DEFAULT 0,
307 ipb_allow_usertalk SMALLINT NOT NULL DEFAULT 0,
308 ipb_parent_block_id INTEGER NULL REFERENCES ipblocks(ipb_id) ON DELETE SET NULL DEFERRABLE INITIALLY DEFERRED
311 CREATE UNIQUE INDEX ipb_address_unique ON ipblocks (ipb_address,ipb_user,ipb_auto,ipb_anon_only);
312 CREATE INDEX ipb_user ON ipblocks (ipb_user);
313 CREATE INDEX ipb_range ON ipblocks (ipb_range_start,ipb_range_end);
314 CREATE INDEX ipb_parent_block_id ON ipblocks (ipb_parent_block_id);
318 img_name TEXT NOT NULL PRIMARY KEY,
319 img_size INTEGER NOT NULL,
320 img_width INTEGER NOT NULL,
321 img_height INTEGER NOT NULL,
322 img_metadata BYTEA NOT NULL DEFAULT '',
325 img_major_mime TEXT DEFAULT 'unknown',
326 img_minor_mime TEXT DEFAULT 'unknown',
327 img_description TEXT NOT NULL,
328 img_user INTEGER NULL REFERENCES mwuser(user_id) ON DELETE SET NULL DEFERRABLE INITIALLY DEFERRED,
329 img_user_text TEXT NOT NULL,
330 img_timestamp TIMESTAMPTZ,
331 img_sha1 TEXT NOT NULL DEFAULT ''
333 CREATE INDEX img_size_idx ON image (img_size);
334 CREATE INDEX img_timestamp_idx ON image (img_timestamp);
335 CREATE INDEX img_sha1 ON image (img_sha1);
337 CREATE TABLE oldimage (
338 oi_name TEXT NOT NULL,
339 oi_archive_name TEXT NOT NULL,
340 oi_size INTEGER NOT NULL,
341 oi_width INTEGER NOT NULL,
342 oi_height INTEGER NOT NULL,
343 oi_bits SMALLINT NULL,
345 oi_user INTEGER NULL REFERENCES mwuser(user_id) ON DELETE SET NULL DEFERRABLE INITIALLY DEFERRED,
346 oi_user_text TEXT NOT NULL,
347 oi_timestamp TIMESTAMPTZ NULL,
348 oi_metadata BYTEA NOT NULL DEFAULT '',
349 oi_media_type TEXT NULL,
350 oi_major_mime TEXT NULL DEFAULT 'unknown',
351 oi_minor_mime TEXT NULL DEFAULT 'unknown',
352 oi_deleted SMALLINT NOT NULL DEFAULT 0,
353 oi_sha1 TEXT NOT NULL DEFAULT ''
355 ALTER TABLE oldimage ADD CONSTRAINT oldimage_oi_name_fkey_cascaded FOREIGN KEY (oi_name) REFERENCES image(img_name) ON DELETE CASCADE ON UPDATE CASCADE DEFERRABLE INITIALLY DEFERRED;
356 CREATE INDEX oi_name_timestamp ON oldimage (oi_name,oi_timestamp);
357 CREATE INDEX oi_name_archive_name ON oldimage (oi_name,oi_archive_name);
358 CREATE INDEX oi_sha1 ON oldimage (oi_sha1);
361 CREATE SEQUENCE filearchive_fa_id_seq;
362 CREATE TABLE filearchive (
363 fa_id INTEGER NOT NULL PRIMARY KEY DEFAULT nextval('filearchive_fa_id_seq'),
364 fa_name TEXT NOT NULL,
365 fa_archive_name TEXT,
366 fa_storage_group TEXT,
368 fa_deleted_user INTEGER NULL REFERENCES mwuser(user_id) ON DELETE SET NULL DEFERRABLE INITIALLY DEFERRED,
369 fa_deleted_timestamp TIMESTAMPTZ NOT NULL,
370 fa_deleted_reason TEXT,
371 fa_size INTEGER NOT NULL,
372 fa_width INTEGER NOT NULL,
373 fa_height INTEGER NOT NULL,
374 fa_metadata BYTEA NOT NULL DEFAULT '',
377 fa_major_mime TEXT DEFAULT 'unknown',
378 fa_minor_mime TEXT DEFAULT 'unknown',
379 fa_description TEXT NOT NULL,
380 fa_user INTEGER NULL REFERENCES mwuser(user_id) ON DELETE SET NULL DEFERRABLE INITIALLY DEFERRED,
381 fa_user_text TEXT NOT NULL,
382 fa_timestamp TIMESTAMPTZ,
383 fa_deleted SMALLINT NOT NULL DEFAULT 0,
384 fa_sha1 TEXT NOT NULL DEFAULT ''
386 CREATE INDEX fa_name_time ON filearchive (fa_name, fa_timestamp);
387 CREATE INDEX fa_dupe ON filearchive (fa_storage_group, fa_storage_key);
388 CREATE INDEX fa_notime ON filearchive (fa_deleted_timestamp);
389 CREATE INDEX fa_nouser ON filearchive (fa_deleted_user);
390 CREATE INDEX fa_sha1 ON filearchive (fa_sha1);
392 CREATE SEQUENCE uploadstash_us_id_seq;
393 CREATE TYPE media_type AS ENUM ('UNKNOWN','BITMAP','DRAWING','AUDIO','VIDEO','MULTIMEDIA','OFFICE','TEXT','EXECUTABLE','ARCHIVE');
395 CREATE TABLE uploadstash (
396 us_id INTEGER PRIMARY KEY NOT NULL DEFAULT nextval('uploadstash_us_id_seq'),
403 us_timestamp TIMESTAMPTZ,
405 us_chunk_inx INTEGER NULL,
409 us_media_type media_type DEFAULT NULL,
410 us_image_width INTEGER,
411 us_image_height INTEGER,
412 us_image_bits SMALLINT
415 CREATE INDEX us_user_idx ON uploadstash (us_user);
416 CREATE UNIQUE INDEX us_key_idx ON uploadstash (us_key);
417 CREATE INDEX us_timestamp_idx ON uploadstash (us_timestamp);
420 CREATE SEQUENCE recentchanges_rc_id_seq;
421 CREATE TABLE recentchanges (
422 rc_id INTEGER NOT NULL PRIMARY KEY DEFAULT nextval('recentchanges_rc_id_seq'),
423 rc_timestamp TIMESTAMPTZ NOT NULL,
424 rc_cur_time TIMESTAMPTZ NULL,
425 rc_user INTEGER NULL REFERENCES mwuser(user_id) ON DELETE SET NULL DEFERRABLE INITIALLY DEFERRED,
426 rc_user_text TEXT NOT NULL,
427 rc_namespace SMALLINT NOT NULL,
428 rc_title TEXT NOT NULL,
430 rc_minor SMALLINT NOT NULL DEFAULT 0,
431 rc_bot SMALLINT NOT NULL DEFAULT 0,
432 rc_new SMALLINT NOT NULL DEFAULT 0,
433 rc_cur_id INTEGER NULL,
434 rc_this_oldid INTEGER NOT NULL,
435 rc_last_oldid INTEGER NOT NULL,
436 rc_type SMALLINT NOT NULL DEFAULT 0,
437 rc_source TEXT NOT NULL,
438 rc_patrolled SMALLINT NOT NULL DEFAULT 0,
442 rc_deleted SMALLINT NOT NULL DEFAULT 0,
443 rc_logid INTEGER NOT NULL DEFAULT 0,
448 CREATE INDEX rc_timestamp ON recentchanges (rc_timestamp);
449 CREATE INDEX rc_timestamp_bot ON recentchanges (rc_timestamp) WHERE rc_bot = 0;
450 CREATE INDEX rc_namespace_title ON recentchanges (rc_namespace, rc_title);
451 CREATE INDEX rc_cur_id ON recentchanges (rc_cur_id);
452 CREATE INDEX new_name_timestamp ON recentchanges (rc_new, rc_namespace, rc_timestamp);
453 CREATE INDEX rc_ip ON recentchanges (rc_ip);
454 CREATE INDEX rc_name_type_patrolled_timestamp ON recentchanges (rc_namespace, rc_type, rc_patrolled, rc_timestamp);
457 CREATE SEQUENCE watchlist_wl_id_seq;
458 CREATE TABLE watchlist (
459 wl_id INTEGER NOT NULL PRIMARY KEY DEFAULT nextval('watchlist_wl_id_seq'),
460 wl_user INTEGER NOT NULL REFERENCES mwuser(user_id) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
461 wl_namespace SMALLINT NOT NULL DEFAULT 0,
462 wl_title TEXT NOT NULL,
463 wl_notificationtimestamp TIMESTAMPTZ
465 CREATE UNIQUE INDEX wl_user_namespace_title ON watchlist (wl_namespace, wl_title, wl_user);
466 CREATE INDEX wl_user ON watchlist (wl_user);
467 CREATE INDEX wl_user_notificationtimestamp ON watchlist (wl_user, wl_notificationtimestamp);
470 CREATE TABLE interwiki (
471 iw_prefix TEXT NOT NULL UNIQUE,
472 iw_url TEXT NOT NULL,
473 iw_local SMALLINT NOT NULL,
474 iw_trans SMALLINT NOT NULL DEFAULT 0,
475 iw_api TEXT NOT NULL DEFAULT '',
476 iw_wikiid TEXT NOT NULL DEFAULT ''
480 CREATE TABLE querycache (
481 qc_type TEXT NOT NULL,
482 qc_value INTEGER NOT NULL,
483 qc_namespace SMALLINT NOT NULL,
484 qc_title TEXT NOT NULL
486 CREATE INDEX querycache_type_value ON querycache (qc_type, qc_value);
488 CREATE TABLE querycache_info (
489 qci_type TEXT UNIQUE,
490 qci_timestamp TIMESTAMPTZ NULL
493 CREATE TABLE querycachetwo (
494 qcc_type TEXT NOT NULL,
495 qcc_value INTEGER NOT NULL DEFAULT 0,
496 qcc_namespace INTEGER NOT NULL DEFAULT 0,
497 qcc_title TEXT NOT NULL DEFAULT '',
498 qcc_namespacetwo INTEGER NOT NULL DEFAULT 0,
499 qcc_titletwo TEXT NOT NULL DEFAULT ''
501 CREATE INDEX querycachetwo_type_value ON querycachetwo (qcc_type, qcc_value);
502 CREATE INDEX querycachetwo_title ON querycachetwo (qcc_type,qcc_namespace,qcc_title);
503 CREATE INDEX querycachetwo_titletwo ON querycachetwo (qcc_type,qcc_namespacetwo,qcc_titletwo);
505 CREATE TABLE objectcache (
507 value BYTEA NOT NULL DEFAULT '',
508 exptime TIMESTAMPTZ NOT NULL
510 CREATE INDEX objectcacache_exptime ON objectcache (exptime);
512 CREATE TABLE transcache (
513 tc_url TEXT NOT NULL UNIQUE,
514 tc_contents TEXT NOT NULL,
515 tc_time TIMESTAMPTZ NOT NULL
519 CREATE SEQUENCE logging_log_id_seq;
520 CREATE TABLE logging (
521 log_id INTEGER NOT NULL PRIMARY KEY DEFAULT nextval('logging_log_id_seq'),
522 log_type TEXT NOT NULL,
523 log_action TEXT NOT NULL,
524 log_timestamp TIMESTAMPTZ NOT NULL,
525 log_user INTEGER REFERENCES mwuser(user_id) ON DELETE SET NULL DEFERRABLE INITIALLY DEFERRED,
526 log_namespace SMALLINT NOT NULL,
527 log_title TEXT NOT NULL,
530 log_deleted SMALLINT NOT NULL DEFAULT 0,
531 log_user_text TEXT NOT NULL DEFAULT '',
534 CREATE INDEX logging_type_name ON logging (log_type, log_timestamp);
535 CREATE INDEX logging_user_time ON logging (log_timestamp, log_user);
536 CREATE INDEX logging_page_time ON logging (log_namespace, log_title, log_timestamp);
537 CREATE INDEX logging_times ON logging (log_timestamp);
538 CREATE INDEX logging_user_type_time ON logging (log_user, log_type, log_timestamp);
539 CREATE INDEX logging_page_id_time ON logging (log_page, log_timestamp);
540 CREATE INDEX logging_user_text_type_time ON logging (log_user_text, log_type, log_timestamp);
541 CREATE INDEX logging_user_text_time ON logging (log_user_text, log_timestamp);
543 CREATE TABLE log_search (
544 ls_field TEXT NOT NULL,
545 ls_value TEXT NOT NULL,
546 ls_log_id INTEGER NOT NULL DEFAULT 0,
547 PRIMARY KEY (ls_field,ls_value,ls_log_id)
549 CREATE INDEX ls_log_id ON log_search (ls_log_id);
552 CREATE SEQUENCE job_job_id_seq;
554 job_id INTEGER NOT NULL PRIMARY KEY DEFAULT nextval('job_job_id_seq'),
555 job_cmd TEXT NOT NULL,
556 job_namespace SMALLINT NOT NULL,
557 job_title TEXT NOT NULL,
558 job_timestamp TIMESTAMPTZ,
559 job_params TEXT NOT NULL,
560 job_random INTEGER NOT NULL DEFAULT 0,
561 job_attempts INTEGER NOT NULL DEFAULT 0,
562 job_token TEXT NOT NULL DEFAULT '',
563 job_token_timestamp TIMESTAMPTZ,
564 job_sha1 TEXT NOT NULL DEFAULT ''
566 CREATE INDEX job_sha1 ON job (job_sha1);
567 CREATE INDEX job_cmd_token ON job (job_cmd, job_token, job_random);
568 CREATE INDEX job_cmd_token_id ON job (job_cmd, job_token, job_id);
569 CREATE INDEX job_cmd_namespace_title ON job (job_cmd, job_namespace, job_title);
570 CREATE INDEX job_timestamp_idx ON job (job_timestamp);
572 -- Tsearch2 2 stuff. Will fail if we don't have proper access to the tsearch2 tables
573 -- Version 8.3 or higher only. Previous versions would need another parmeter for to_tsvector.
574 -- Make sure you also change patch-tsearch2funcs.sql if the funcs below change.
576 ALTER TABLE page ADD titlevector tsvector;
577 CREATE FUNCTION ts2_page_title() RETURNS TRIGGER LANGUAGE plpgsql AS
580 IF TG_OP = 'INSERT' THEN
581 NEW.titlevector = to_tsvector(REPLACE(NEW.page_title,'/',' '));
582 ELSIF NEW.page_title != OLD.page_title THEN
583 NEW.titlevector := to_tsvector(REPLACE(NEW.page_title,'/',' '));
589 CREATE TRIGGER ts2_page_title BEFORE INSERT OR UPDATE ON page
590 FOR EACH ROW EXECUTE PROCEDURE ts2_page_title();
593 ALTER TABLE pagecontent ADD textvector tsvector;
594 CREATE FUNCTION ts2_page_text() RETURNS TRIGGER LANGUAGE plpgsql AS
597 IF TG_OP = 'INSERT' THEN
598 NEW.textvector = to_tsvector(NEW.old_text);
599 ELSIF NEW.old_text != OLD.old_text THEN
600 NEW.textvector := to_tsvector(NEW.old_text);
606 CREATE TRIGGER ts2_page_text BEFORE INSERT OR UPDATE ON pagecontent
607 FOR EACH ROW EXECUTE PROCEDURE ts2_page_text();
609 -- These are added by the setup script due to version compatibility issues
610 -- If using 8.1, we switch from "gin" to "gist"
612 CREATE INDEX ts2_page_title ON page USING gin(titlevector);
613 CREATE INDEX ts2_page_text ON pagecontent USING gin(textvector);
615 CREATE FUNCTION add_interwiki (TEXT,INT,SMALLINT) RETURNS INT LANGUAGE SQL AS
617 INSERT INTO interwiki (iw_prefix, iw_url, iw_local) VALUES ($1,$2,$3);
621 -- This table is not used unless profiling is turned on
622 CREATE TABLE profiling (
623 pf_count INTEGER NOT NULL DEFAULT 0,
624 pf_time FLOAT NOT NULL DEFAULT 0,
625 pf_memory FLOAT NOT NULL DEFAULT 0,
626 pf_name TEXT NOT NULL,
629 CREATE UNIQUE INDEX pf_name_server ON profiling (pf_name, pf_server);
631 CREATE TABLE protected_titles (
632 pt_namespace SMALLINT NOT NULL,
633 pt_title TEXT NOT NULL,
634 pt_user INTEGER NULL REFERENCES mwuser(user_id) ON DELETE SET NULL DEFERRABLE INITIALLY DEFERRED,
636 pt_timestamp TIMESTAMPTZ NOT NULL,
637 pt_expiry TIMESTAMPTZ NULL,
638 pt_create_perm TEXT NOT NULL DEFAULT ''
640 CREATE UNIQUE INDEX protected_titles_unique ON protected_titles(pt_namespace, pt_title);
643 CREATE TABLE updatelog (
644 ul_key TEXT NOT NULL PRIMARY KEY,
649 CREATE SEQUENCE category_cat_id_seq;
650 CREATE TABLE category (
651 cat_id INTEGER NOT NULL PRIMARY KEY DEFAULT nextval('category_cat_id_seq'),
652 cat_title TEXT NOT NULL,
653 cat_pages INTEGER NOT NULL DEFAULT 0,
654 cat_subcats INTEGER NOT NULL DEFAULT 0,
655 cat_files INTEGER NOT NULL DEFAULT 0,
656 cat_hidden SMALLINT NOT NULL DEFAULT 0
658 CREATE UNIQUE INDEX category_title ON category(cat_title);
659 CREATE INDEX category_pages ON category(cat_pages);
661 CREATE SEQUENCE change_tag_ct_id_seq;
662 CREATE TABLE change_tag (
663 ct_id INTEGER NOT NULL PRIMARY KEY DEFAULT nextval('change_tag_ct_id_seq'),
664 ct_rc_id INTEGER NULL,
665 ct_log_id INTEGER NULL,
666 ct_rev_id INTEGER NULL,
667 ct_tag TEXT NOT NULL,
670 CREATE UNIQUE INDEX change_tag_rc_tag ON change_tag(ct_rc_id,ct_tag);
671 CREATE UNIQUE INDEX change_tag_log_tag ON change_tag(ct_log_id,ct_tag);
672 CREATE UNIQUE INDEX change_tag_rev_tag ON change_tag(ct_rev_id,ct_tag);
673 CREATE INDEX change_tag_tag_id ON change_tag(ct_tag,ct_rc_id,ct_rev_id,ct_log_id);
675 CREATE SEQUENCE tag_summary_ts_id_seq;
676 CREATE TABLE tag_summary (
677 ts_id INTEGER NOT NULL PRIMARY KEY DEFAULT nextval('tag_summary_ts_id_seq'),
678 ts_rc_id INTEGER NULL,
679 ts_log_id INTEGER NULL,
680 ts_rev_id INTEGER NULL,
681 ts_tags TEXT NOT NULL
683 CREATE UNIQUE INDEX tag_summary_rc_id ON tag_summary(ts_rc_id);
684 CREATE UNIQUE INDEX tag_summary_log_id ON tag_summary(ts_log_id);
685 CREATE UNIQUE INDEX tag_summary_rev_id ON tag_summary(ts_rev_id);
687 CREATE TABLE valid_tag (
688 vt_tag TEXT NOT NULL PRIMARY KEY
691 CREATE TABLE user_properties (
692 up_user INTEGER NULL REFERENCES mwuser(user_id) ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED,
693 up_property TEXT NOT NULL,
696 CREATE UNIQUE INDEX user_properties_user_property ON user_properties (up_user,up_property);
697 CREATE INDEX user_properties_property ON user_properties (up_property);
699 CREATE TABLE l10n_cache (
700 lc_lang TEXT NOT NULL,
701 lc_key TEXT NOT NULL,
702 lc_value BYTEA NOT NULL
704 CREATE INDEX l10n_cache_lc_lang_key ON l10n_cache (lc_lang, lc_key);
706 CREATE TABLE iwlinks (
707 iwl_from INTEGER NOT NULL DEFAULT 0,
708 iwl_prefix TEXT NOT NULL DEFAULT '',
709 iwl_title TEXT NOT NULL DEFAULT ''
711 CREATE UNIQUE INDEX iwl_from ON iwlinks (iwl_from, iwl_prefix, iwl_title);
712 CREATE UNIQUE INDEX iwl_prefix_title_from ON iwlinks (iwl_prefix, iwl_title, iwl_from);
713 CREATE UNIQUE INDEX iwl_prefix_from_title ON iwlinks (iwl_prefix, iwl_from, iwl_title);
715 CREATE TABLE module_deps (
716 md_module TEXT NOT NULL,
717 md_skin TEXT NOT NULL,
718 md_deps TEXT NOT NULL
720 CREATE UNIQUE INDEX md_module_skin ON module_deps (md_module, md_skin);
722 CREATE SEQUENCE sites_site_id_seq;
724 site_id INTEGER NOT NULL PRIMARY KEY DEFAULT nextval('sites_site_id_seq'),
725 site_global_key TEXT NOT NULL,
726 site_type TEXT NOT NULL,
727 site_group TEXT NOT NULL,
728 site_source TEXT NOT NULL,
729 site_language TEXT NOT NULL,
730 site_protocol TEXT NOT NULL,
731 site_domain TEXT NOT NULL,
732 site_data TEXT NOT NULL,
733 site_forward SMALLINT NOT NULL,
734 site_config TEXT NOT NULL
736 CREATE UNIQUE INDEX site_global_key ON sites (site_global_key);
737 CREATE INDEX site_type ON sites (site_type);
738 CREATE INDEX site_group ON sites (site_group);
739 CREATE INDEX site_source ON sites (site_source);
740 CREATE INDEX site_language ON sites (site_language);
741 CREATE INDEX site_protocol ON sites (site_protocol);
742 CREATE INDEX site_domain ON sites (site_domain);
743 CREATE INDEX site_forward ON sites (site_forward);
745 CREATE TABLE site_identifiers (
746 si_site INTEGER NOT NULL,
747 si_type TEXT NOT NULL,
750 CREATE UNIQUE INDEX si_type_key ON site_identifiers (si_type, si_key);
751 CREATE INDEX si_site ON site_identifiers (si_site);
752 CREATE INDEX si_key ON site_identifiers (si_key);