If you can't read please download the document
Upload
-
View
318
Download
4
Embed Size (px)
Citation preview
PostgreSQL
.
SQL2011
( pgxn)
NoSql
: schema
( Arrays IPV4 IPV6 xml UUID Geometric Composite )
( hadoop ...)
C
PlpgSQL
Python
PLV8
Ruby
Perl
R
TCL
JAVA
...
:
Open Street Map
Sony Online Entertainment
: ( )
: (OS X Lion)
:
(reddit): .
Odoo Open ERP
Open bravo
ORDBMS ( )
CREATE TABLE cities (name text, population real,altitude int);Create type state as ( state_code int, state_name varchar(2));
CREATE TABLE capitals (state state ) INHERITS (cities);insert into capitals values ('mashhad',300,1,(1,'KH'));select * from only cities;
PostgreSQL Advanced Plus : like oracle.
Netezza : a popular database choice for data warehousing.
Tpostgres : with tPostgres, you use the packaged pgtsql language extension to write functions that use T-SQL
BigSQL : is a marriage of the two elephants: PostgreSQL and Hadoop with Hive.BigSQL comes packaged with hadoop_fdw , an FDW for querying and updating Hadoop data sources.
Postgres-XL : built-in Massively Parallel Processing (MPP) capability and data sharding across servers.
Postgresql-XL
OLTP write-intensive workloads
Business Intelligence requiring MPP parallelism
Operational data store
Key-value store
GIS Geospatial
Mixed-workload environments
Multi-tenant provider hosted environments
Service : More than one service can run on a physical server as long as they listen on different ports and dont share data storage.
Database : Default database is postgres.
Schema : immediate next level of organization within each database,Default Schema is Public,everything you create into public by default unless you change the search_path of the database.
Catalog : system schemas that store PostgreSQL built-in functions and meta-data .pg_catalog , which has all the functions, tables, system views, casts, and types packaged with PostgreSQL; and information_schema , which consists of ANSI standard views that expose PostgreSQL metainformation in a format dictated by the ANSI SQL standard.
Variable : search_path,work_mem,superuser_reserved_connections : SHOW ALL
Extension : per database , like add-ons,postgis , procedural languages(plv8,python,perl)
Tableforeign table and foreign data wrapper: like database links , link to Mssql server , Oracle , Mysql , CSV , Webservice like Twitter.
Tablespace:A tablespace is the physical location where data is stored
View : materialized views like Oracle.
Function : same as stored ptocedures in other database engines.
Language : three base procedural languages :SQL,PL/pgSQL, and C,other languages can install : Python, JavaScript, Perl, and R
Operator
Data type : array , Custom DataType
Cast : custom cast.
Sequence : like oracle sequence,PostgresSQL automatically creates sequences when you define a serial column.
Row
Trigger:In version 9.1, a data change in a view can fire a trigger. In version 9.3, data definition language (DDL) events can fire triggers.
Rule
Operator
CREATE OR REPLACE FUNCTION is_zero(int_in integer) RETURNS boolean AS $BODY$ begin if $1 = 0 then return true; else return false; end if; End;$BODY$LANGUAGE plpgsql VOLATILECREATE OPERATOR !# (PROCEDURE = is_zero, LEFTARG = integer);
Operator
CREATE OR REPLACE FUNCTION plus_string(text,text) RETURNS text AS $BODY$ begin return $1 || $2; End;$BODY$LANGUAGE plpgsql VOLATILE;
CREATE OPERATOR + (PROCEDURE = plus_string, LEFTARG = text , RIGHTARG = text);
Select '10' + '10';
CAST
CREATE CAST (source_type AS target_type) WITH FUNCTION function_name (argument_type [, ...]) [ AS ASSIGNMENT | AS IMPLICIT ]
CREATE CAST (source_type AS target_type) WITHOUT FUNCTION [ AS ASSIGNMENT | AS IMPLICIT ]
CREATE CAST (source_type AS target_type) WITH INOUT [ AS ASSIGNMENT | AS IMPLICIT ]
Custom CAST
create function numeric_to_boolean(numeric) returns boolean immutable language sql as $$ select case when $1 > 0 then true else false end$$create cast (numeric as boolean) with function numeric_to_boolean(numeric) as implicit
Rule
create table city_log ( log_time timestamp , description varchar(100));create or replace rule city_log as on insert to citiesdo alsoinsert into city_log values (now() , NEW.name || ' is inserted!' );CREATE RULE "_RETURN" AS ON SELECT TO schema1.vw_t1 DO INSTEAD SELECT * FROM schema2.tbl_t1;
: .
:
Cent OS : /var/lib/pgsql/9.3/dataUbuntu 15.04 : /etc/postgresql/9.3/main Postgresql.conf : IP , Port , Memory, Log.
pg_hba.conf : Controls security. It manages access to the server, dictating which users can log in to which databases, which IP addresses or groups of addresses can connect, and which authentication scheme to expect.
pg_ident.conf : maps an authenticated OS login to a PostgreSQL user.
: SELECT name, setting FROM pg_settings WHERE category = 'File Locations';
:SELECT name, context , unit ,setting, boot_val, reset_val FROM pg_settingsWHERE name IN ( 'listen_addresses', 'max_connections', 'shared_buffers', 'effective_cache_size', 'work_mem', 'maintenance_work_mem')ORDER BY context, name;Show max_connections;Show all;
: SELECT pg_reload_conf();
pg_ctl reload -D your_data_directory_here
service postgresql-9.3 reload
Systemctl reload postgresql.service
SELECT * FROM pg_stat_activity; Returns procidCancel Query : SELECT pg_cancel_backend(procid)Kill the connection : SELECT pg_terminate_backend(procid)Kill all user Connections : SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE usename ='some_role';
Roles
Default role for database is postgres.
CREATE ROLE leo LOGIN PASSWORD 'king' CREATEDB VALID UNTIL 'infinity'; CREATEDB modifier grants database creation rights
CREATE ROLE regina LOGIN PASSWORD 'queen' SUPERUSER VALID UNTIL '2020-1-1 00:00';
Group Roles
login .
CREATE ROLE students INHERIT;any member of royalty will automatically have rights granted to the royalty roleGRANT students TO studentuser1;GRANT students TO studentuser2;
Tablespaces
Tablespaces logical names to physical locations on disk.
pg_default : stores all user data.
pg_global : stores all system data.
CREATE TABLESPACE secondary LOCATION '/data/meedc';
ALTER DATABASE mydb SET TABLESPACE secondary;
ALTER TABLE mytable SET TABLESPACE secondary;
CREATE DATABASE my_db TEMPLATE my_template_db OWNER ali TABLESPACE my_space ENCODING utf8;
DATABASE SCHEMA
Schemas organize your database into logical groupsDefault schema is public.
object names must be unique within a schema.
Same name in different schemas.
search_path = "$user", public in postgresql.conf
GRANT ALL ON ALL TABLES IN SCHEMA public TO mydb_admin WITH GRANT OPTION;
GRANT SELECT, REFERENCES, TRIGGER ON ALL TABLES IN SCHEMA my_schema TO PUBLIC;
Backup & restore on schema.
Extensions
add-ons can install in a PostgreSQL database to extend functionality.
SELECT * FROM pg_available_extensions;
CREATE EXTENSION: create extension in each database.
CREATE EXTENSION fuzzystrmatch SCHEMA my_extensions;Pgxn utility Install extension on server.
Cube extension .
Ltree extension.
Backup and Restore
Backup from Database ,schema , wildcard , exclude tables from backup , compress backup , text backup , binary backup
Example:pg_dump -h localhost -p 5432 -U someuser -F c -b -v -f mydb.backup mydbpg_dump -h localhost -p 5432 -U someuser -C -F p -b -v -f mydb.backup mydbpg_dump -h localhost -p 5432 -U someuser -F c -b -v -t *.pay* -f pay.backup mydbpg_dump -h localhost -p 5432 -U someuser -F c -b -v -n hr -n payroll -f hr.back-up mydbpg_dump -h localhost -p 5432 -U someuser -F c -b -v -N public -f all_sch_except_pub.backup mydb
Backup and Restore
psql -U postgres -f myglobals.sqlpsql -U postgres --set ON_ERROR_STOP=on -f myglobals.sqlpsql -U postgres -d mydb -f select_objects.sql
pg_restore --dbname=mydb --jobs=4 --verbose mydb.backuppg_restore --dbname=postgres --create --jobs=4 --verbose mydb.backup
Foreign Data Wrapper(FDW)
CREATE EXTENSION file_fdw;
CREATE SERVER my_server FOREIGN DATA WRAPPER file_fdw;
CREATE FOREIGN TABLE mem_info (mydata VARCHAR(150)) SERVER my_server OPTIONS (format 'csv', header 'true', filename '/proc/meminfo');
Mysql Foreign DataWrapper
CREATE EXTENSION mysql_fdw;CREATE SERVER mysql_svr FOREIGN DATA WRAPPER mysql_fdw OPTIONS (address '127.0.0.1', port '3306');CREATE FOREIGN TABLE local_students ( stno character varying (10), fname character varying (50), lname character varying (100) );SERVER mysql_svr OPTIONS (query 'SELECT stno,fname,lname from students');CREATE USER MAPPING FOR PUBLIC SERVER mysql_svr OPTIONS (username 'user', password 'password123');
Pgpool II
Synchronous replication
Load balancing, automatic failover, connection
pooling etc.
Collaborating with other replication toolsStreaming replication, Slony-I
Pgpool basic idea
Postgresql Client
Primary
Read/write query
Read query
Read query
secondary
secondary
Read/write query
Pgpool server