1 1.4 yamt -- $NetBSD: newfs.sql,v 1.4 2012/04/11 14:28:46 yamt Exp $ 2 1.1 yamt 3 1.1 yamt -- Copyright (c)2010,2011 YAMAMOTO Takashi, 4 1.1 yamt -- All rights reserved. 5 1.1 yamt -- 6 1.1 yamt -- Redistribution and use in source and binary forms, with or without 7 1.1 yamt -- modification, are permitted provided that the following conditions 8 1.1 yamt -- are met: 9 1.1 yamt -- 1. Redistributions of source code must retain the above copyright 10 1.1 yamt -- notice, this list of conditions and the following disclaimer. 11 1.1 yamt -- 2. Redistributions in binary form must reproduce the above copyright 12 1.1 yamt -- notice, this list of conditions and the following disclaimer in the 13 1.1 yamt -- documentation and/or other materials provided with the distribution. 14 1.1 yamt -- 15 1.1 yamt -- THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 1.1 yamt -- ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 1.1 yamt -- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 1.1 yamt -- ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 1.1 yamt -- FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 1.1 yamt -- DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 1.1 yamt -- OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 1.1 yamt -- HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 1.1 yamt -- LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 1.1 yamt -- OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 1.1 yamt -- SUCH DAMAGE. 26 1.1 yamt 27 1.1 yamt -- create a new pgfs filesystem. 28 1.1 yamt -- usage: psql -f newfs.sql [dbname] 29 1.1 yamt 30 1.1 yamt -- remove pgfs schema if already exists. 31 1.1 yamt -- XXX this leaves large objects 32 1.1 yamt BEGIN; 33 1.1 yamt DROP SCHEMA pgfs CASCADE; 34 1.1 yamt COMMIT; 35 1.1 yamt 36 1.1 yamt BEGIN; 37 1.1 yamt -- we put everything in the pgfs schema. (except large objects) 38 1.1 yamt CREATE SCHEMA pgfs; 39 1.1 yamt SET search_path TO pgfs; 40 1.1 yamt 41 1.1 yamt -- define various types 42 1.1 yamt CREATE DOMAIN fileid AS int8 NOT NULL CHECK(VALUE > 0); 43 1.1 yamt CREATE DOMAIN uid AS int8 NOT NULL CHECK(VALUE >= 0); 44 1.1 yamt CREATE DOMAIN gid AS int8 NOT NULL CHECK(VALUE >= 0); 45 1.1 yamt CREATE DOMAIN mode AS int8 NOT NULL CHECK(VALUE >= 0 AND VALUE <= 7*8*8+7*8+7); 46 1.1 yamt CREATE DOMAIN nlink AS int8 NOT NULL CHECK(VALUE >= 0); 47 1.4 yamt CREATE SEQUENCE fileid_seq START WITH 1; -- 1 will be used for root directory 48 1.4 yamt CREATE SEQUENCE dircookie_seq START WITH 3; -- see PGFS_DIRCOOKIE_* 49 1.1 yamt CREATE TYPE filetype AS ENUM ( 50 1.1 yamt 'regular', 51 1.1 yamt 'directory', 52 1.1 yamt 'link'); 53 1.1 yamt 54 1.1 yamt -- a row in the file table describes a file. 55 1.1 yamt -- having nlink here is somehow redundant as it could be calculated from 56 1.1 yamt -- filetype and the dirent table. however, users expect that getattr is 57 1.1 yamt -- executed in a nearly constant time. 58 1.1 yamt CREATE TABLE file ( 59 1.1 yamt fileid fileid PRIMARY KEY, 60 1.1 yamt type filetype NOT NULL, 61 1.1 yamt mode mode, 62 1.1 yamt uid uid, 63 1.1 yamt gid gid, 64 1.1 yamt nlink nlink, 65 1.1 yamt rev int8 NOT NULL, 66 1.1 yamt atime timestamptz NOT NULL, 67 1.1 yamt ctime timestamptz NOT NULL, 68 1.1 yamt mtime timestamptz NOT NULL, 69 1.1 yamt btime timestamptz NOT NULL); 70 1.1 yamt 71 1.1 yamt -- datafork table maintains the association between our files and its backing 72 1.1 yamt -- large objects. 73 1.1 yamt CREATE TABLE datafork ( 74 1.2 yamt fileid fileid PRIMARY KEY, -- REFERENCES file 75 1.1 yamt loid Oid NOT NULL UNIQUE); 76 1.1 yamt -- we want the following but lo lives in system catalogs. 77 1.1 yamt -- loid REFERENCES pg_largeobject_metadata(oid); 78 1.1 yamt 79 1.1 yamt -- a row in the dirent table describes a directory entry. 80 1.1 yamt -- the ".." and "." entries are handled differently and never appear here. 81 1.1 yamt CREATE TABLE dirent ( 82 1.3 yamt parent_fileid fileid NOT NULL, -- REFERENCES file, 83 1.1 yamt name text NOT NULL, 84 1.1 yamt cookie int8 NOT NULL UNIQUE DEFAULT nextval('dircookie_seq'), 85 1.3 yamt child_fileid fileid NOT NULL, -- REFERENCES file, 86 1.1 yamt CONSTRAINT dirent_pkey PRIMARY KEY(parent_fileid, name), 87 1.1 yamt CONSTRAINT dirent_notdot CHECK(name <> '.'), 88 1.1 yamt CONSTRAINT dirent_notdotdot CHECK(name <> '..'), 89 1.1 yamt CONSTRAINT dirent_noself CHECK(parent_fileid <> child_fileid)); 90 1.1 yamt CREATE INDEX dirent_child ON dirent (child_fileid); 91 1.1 yamt 92 1.1 yamt -- create the root directory. 93 1.1 yamt INSERT INTO file (fileid, type, mode, uid, gid, nlink, rev, 94 1.1 yamt atime, ctime, mtime, btime) 95 1.1 yamt VALUES (nextval('fileid_seq'), 'directory', 7*8*8 + 5*8 + 5, 0, 0, 1, 0, 96 1.1 yamt current_timestamp, 97 1.1 yamt current_timestamp, 98 1.1 yamt current_timestamp, 99 1.1 yamt current_timestamp); 100 1.1 yamt 101 1.1 yamt -- create a dummy sequence. see pgfs_node_fsync(). 102 1.1 yamt CREATE SEQUENCE dummyseq; 103 1.1 yamt 104 1.1 yamt RESET search_path; 105 1.1 yamt COMMIT; 106