create table users (id int,password varchar(256));
create view view_users as select id from users;
create temp view view_users as select id from users;
create view view_users (no) as select id from users;
9.3版本以前无法更新视图,需要定义更新,删除,插入规则才行
创建索引不能增删改
gin反转索引(支持包含@>,被包含@<,相等=,重叠&&)
create table contacts (id int primary key,name varchar(40),phone varchar(32)[],address text);
create index idx_phone on contacts using gin(phone);
select * from contacts where phone @> array [‘12345’::varchar(32)];
降序建索引
create index a_idx on contacts on contacts(name desc);
如果name字段存在空值可以指定空值排在非空前面,或后面
create index a_idx on contacts on contacts(name desc nulls first/last);
大表并发创建索引create index concurrently idx_test on test (note);
重建索引不支持concurrently,但支持在一个字段建立两个索引,可以建一个新的索引,然后把旧的删掉。
并发创建索引时中断invalid状态的索引必须要删除,因为它会影响查询效率。
修改索引:
alter index idx_a rename to xxx
alter index idx_a set tablespace xxx
pg使用角色的概念管理数据库访问权限,角色可以是用户
create role(无login) create use(有login)
只有superuser可以创建superuser
createdb权限,createrole权限,createuser权限
in role name(指定用户成为哪些角色成员)
如果给用户赋权创建数据库是alter role,给用户创建模式则是grant ,比如grant xxx on xxx
all privileges=grant 所有权限
权限层次
超级用户,创建数据库,用户,login权限
在模式中创建数据库对象(表,索引)
dml权限
操作表字段权限
只读权限:
grant select on all tables in schema public to xxx;
alter default privileges in schema public grant select on tables to xxx;
grant select on all tables in schema xxx to xxx;
alter default privileges in schema xxx grant select on tables to xxx;
pg可以在事务中回滚ddl!
savepoint可以在大事务中把操作拆分,这样出错不必回滚整个事务
rollback to savepoint mypoint;
事务持久化参数(max_prepared_transactions=10)开始一个事务,即使重启库,这个事务也可以提交




