Module Objectives
- Table Inheritance
- Table Partitioning
- Copy Table
C:\Users\Maxwell Pan>psql -U postgres
psql: error: connection to server at "localhost" (::1), port 5432 failed: Connection refused (0x0000274D/10061)
Is the server running on that host and accepting TCP/IP connections?
connection to server at "localhost" (127.0.0.1), port 5432 failed: Connection refused (0x0000274D/10061)
Is the server running on that host and accepting TCP/IP connections?
C:\Users\Maxwell Pan>pg_ctl status
pg_ctl: no server running
C:\Users\Maxwell Pan>pg_ctl start
pg_ctl: another server might be running; trying to start server anyway
waiting for server to start....2023-04-27 08:06:32.820 CST [24924] LOG: redirecting log output to logging collector process
2023-04-27 08:06:32.820 CST [24924] HINT: Future log output will appear in directory "log".
. done
server started
C:\Users\Maxwell Pan>psql -U postgres
Password for user postgres:
psql (15.2)
WARNING: Console code page (437) differs from Windows code page (1252)
8-bit characters might not work correctly. See psql reference
page "Notes for Windows users" for details.
Type "help" for help.
postgres=#
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | ICU Locale | Locale Provider | Access privileges
-----------+----------+----------+----------------------------+----------------------------+------------+-----------------+-----------------------
nano | postgres | UTF8 | English_United States.1252 | English_United States.1252 | | libc | =Tc/postgres +
| | | | | | | postgres=CTc/postgres+
| | | | | | | ron=c/postgres
postgres | postgres | UTF8 | English_United States.1252 | English_United States.1252 | | libc |
template0 | postgres | UTF8 | English_United States.1252 | English_United States.1252 | | libc | =c/postgres +
| | | | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | English_United States.1252 | English_United States.1252 | | libc | =c/postgres +
| | | | | | | postgres=CTc/postgres
(4 rows)
postgres=# \c nano
You are now connected to database "nano" as user "postgres".
nano=#
nano=# create table orders(orderno serial,flightname varchar(100),boarding varchar(100),status varchar(100), source varchar(100));
CREATE TABLE
nano=# \d orders
Table "public.orders"
Column | Type | Collation | Nullable | Default
------------+------------------------+-----------+----------+-----------------------------------------
orderno | integer | | not null | nextval('orders_orderno_seq'::regclass)
flightname | character varying(100) | | |
boarding | character varying(100) | | |
status | character varying(100) | | |
source | character varying(100) | | |
nano=# create table online_booking(price int) inherits (orders);
CREATE TABLE
nano=# \d online_booking
Table "public.online_booking"
Column | Type | Collation | Nullable | Default
------------+------------------------+-----------+----------+-----------------------------------------
orderno | integer | | not null | nextval('orders_orderno_seq'::regclass)
flightname | character varying(100) | | |
boarding | character varying(100) | | |
status | character varying(100) | | |
source | character varying(100) | | |
price | integer | | |
Inherits: orders
nano=# \d+ orders
Table "public.orders"
Column | Type | Collation | Nullable | Default | Storage | Compression | Stats target | Description
------------+------------------------+-----------+----------+-----------------------------------------+----------+-------------+--------------+-------------
orderno | integer | | not null | nextval('orders_orderno_seq'::regclass) | plain | | |
flightname | character varying(100) | | | | extended | | |
boarding | character varying(100) | | | | extended | | |
status | character varying(100) | | | | extended | | |
source | character varying(100) | | | | extended | | |
Child tables: online_booking
Access method: heap
nano=# create table agent_booking(commision int) inherits (orders);
CREATE TABLE
nano=# \d agent_booking
Table "public.agent_booking"
Column | Type | Collation | Nullable | Default
------------+------------------------+-----------+----------+-----------------------------------------
orderno | integer | | not null | nextval('orders_orderno_seq'::regclass)
flightname | character varying(100) | | |
boarding | character varying(100) | | |
status | character varying(100) | | |
source | character varying(100) | | |
commision | integer | | |
Inherits: orders
nano=# \d+ orders
Table "public.orders"
Column | Type | Collation | Nullable | Default | Storage | Compression | Stats target | Description
------------+------------------------+-----------+----------+-----------------------------------------+----------+-------------+--------------+-------------
orderno | integer | | not null | nextval('orders_orderno_seq'::regclass) | plain | | |
flightname | character varying(100) | | | | extended | | |
boarding | character varying(100) | | | | extended | | |
status | character varying(100) | | | | extended | | |
source | character varying(100) | | | | extended | | |
Child tables: agent_booking,
online_booking
Access method: heap
nano=# insert into orders(flightname,boarding,status,source) values('aricanada','yyz','ontime','employee');
INSERT 0 1
nano=# select * from orders;
orderno | flightname | boarding | status | source
---------+------------+----------+--------+----------
1 | aricanada | yyz | ontime | employee
(1 row)
nano=# insert into online_booking(flightname,boarding,status,source,price) values('nippon','hdj','ontime','website',3000);
INSERT 0 1
nano=# insert into online_booking(flightname,boarding,status,source,price) values('lufthansa','hdk','ontime','app',1000);
INSERT 0 1
nano=# select * from online_booking;
orderno | flightname | boarding | status | source | price
---------+------------+----------+--------+---------+-------
2 | nippon | hdj | ontime | website | 3000
3 | lufthansa | hdk | ontime | app | 1000
(2 rows)
nano=# insert into agent_booking(flightname,boarding,status,source,commision) values('ethihad','auh','ontime','agent001',300);
INSERT 0 1
nano=# insert into agent_booking(flightname,boarding,status,source,commision) values('emirates','dxb','ontime','agent007',200);
INSERT 0 1
nano=# select * from agent_booking;
orderno | flightname | boarding | status | source | commision
---------+------------+----------+--------+----------+-----------
4 | ethihad | auh | ontime | agent001 | 300
5 | emirates | dxb | ontime | agent007 | 200
(2 rows)
nano=# select * from orders;
orderno | flightname | boarding | status | source
---------+------------+----------+--------+----------
1 | aricanada | yyz | ontime | employee
2 | nippon | hdj | ontime | website
3 | lufthansa | hdk | ontime | app
4 | ethihad | auh | ontime | agent001
5 | emirates | dxb | ontime | agent007
(5 rows)
nano=# select * from only orders;
orderno | flightname | boarding | status | source
---------+------------+----------+--------+----------
1 | aricanada | yyz | ontime | employee
(1 row)
nano=# \set AUTOCOMMIT off
nano=# select * from orders;
orderno | flightname | boarding | status | source
---------+------------+----------+--------+----------
1 | aricanada | yyz | ontime | employee
2 | nippon | hdj | ontime | website
3 | lufthansa | hdk | ontime | app
4 | ethihad | auh | ontime | agent001
5 | emirates | dxb | ontime | agent007
(5 rows)
nano=*# update orders set status='Delayed';
UPDATE 5
nano=*# select * from orders;
orderno | flightname | boarding | status | source
---------+------------+----------+---------+----------
1 | aricanada | yyz | Delayed | employee
2 | nippon | hdj | Delayed | website
3 | lufthansa | hdk | Delayed | app
4 | ethihad | auh | Delayed | agent001
5 | emirates | dxb | Delayed | agent007
(5 rows)
nano=*# rollback;
ROLLBACK
nano=# select * from orders;
orderno | flightname | boarding | status | source
---------+------------+----------+--------+----------
1 | aricanada | yyz | ontime | employee
2 | nippon | hdj | ontime | website
3 | lufthansa | hdk | ontime | app
4 | ethihad | auh | ontime | agent001
5 | emirates | dxb | ontime | agent007
(5 rows)
nano=*# update only orders set status='Delayed';
UPDATE 1
nano=*# select * from orders;
orderno | flightname | boarding | status | source
---------+------------+----------+---------+----------
1 | aricanada | yyz | Delayed | employee
2 | nippon | hdj | ontime | website
3 | lufthansa | hdk | ontime | app
4 | ethihad | auh | ontime | agent001
5 | emirates | dxb | ontime | agent007
(5 rows)
nano=*# drop table orders cascade;
NOTICE: drop cascades to 2 other objects
DETAIL: drop cascades to table online_booking
drop cascades to table agent_booking
DROP TABLE
nano=*# \dt
List of relations
Schema | Name | Type | Owner
--------+---------+-------+-------
public | tree | table | sam
public | vehicle | table | scott
(2 rows)
nano=*#
Partitioning
- Table Partitioning means splitting a table into smaller pieces
- Table Partitioning holds many performance benefits for tables that hold large amount of data.
- PostgreSQL allows table partitioning via table inheritance
- Each Partition is created as a child table of a single parent table.
- PostgreSQL implements range and list partitioning methods
C:\Users\Maxwell Pan>
C:\Users\Maxwell Pan>
C:\Users\Maxwell Pan>psql -U postgres
Password for user postgres:
psql (15.2)
WARNING: Console code page (437) differs from Windows code page (1252)
8-bit characters might not work correctly. See psql reference
page "Notes for Windows users" for details.
Type "help" for help.
postgres=# \c nano
You are now connected to database "nano" as user "postgres".
nano=#
nano=# create table booking(flightno varchar(100),flightname varchar(100), booking_date timestamp);
CREATE TABLE
nano=# \d bookings
Did not find any relation named "bookings".
nano=# create table bookings(flightno varchar(100),flightname varchar(100), booking_date timestamp);
CREATE TABLE
nano=# \d bookings
Table "public.bookings"
Column | Type | Collation | Nullable | Default
--------------+-----------------------------+-----------+----------+---------
flightno | character varying(100) | | |
flightname | character varying(100) | | |
booking_date | timestamp without time zone | | |
nano=# create table jan_booking (check(booking_date >= '2020-01-01' and booking_date <= '2020-01-31')) inherits(bookings);
CREATE TABLE
nano=# create table jan_bookings (check(booking_date >= '2020-01-01' and booking_date <= '2020-01-31')) inherits(bookings);
CREATE TABLE
nano=# \d jan_bookings
Table "public.jan_bookings"
Column | Type | Collation | Nullable | Default
--------------+-----------------------------+-----------+----------+---------
flightno | character varying(100) | | |
flightname | character varying(100) | | |
booking_date | timestamp without time zone | | |
Check constraints:
"jan_bookings_booking_date_check" CHECK (booking_date >= '2020-01-01 00:00:00'::timestamp without time zone AND booking_date <= '2020-01-31 00:00:00'::timestamp without time zone)
Inherits: bookings
nano=# create table feb_bookings (check(booking_date >= '2020-02-01' and booking_date <= '2020-02-29')) inherits(bookings);
CREATE TABLE
nano=#
nano=# \d feb_bookings
Table "public.feb_bookings"
Column | Type | Collation | Nullable | Default
--------------+-----------------------------+-----------+----------+---------
flightno | character varying(100) | | |
flightname | character varying(100) | | |
booking_date | timestamp without time zone | | |
Check constraints:
"feb_bookings_booking_date_check" CHECK (booking_date >= '2020-02-01 00:00:00'::timestamp without time zone AND booking_date <= '2020-02-29 00:00:00'::timestamp without time zone)
Inherits: bookings
nano=#
nano=# create index jan_index on jan_bookings using btree(booking_date);
CREATE INDEX
nano=# create index feb_index on feb_bookings using btree(booking_date);
CREATE INDEX
nano=# \di
List of relations
Schema | Name | Type | Owner | Table
--------+-----------+-------+----------+--------------
public | feb_index | index | postgres | feb_bookings
public | jan_index | index | postgres | jan_bookings
(2 rows)
nano=#
nano=# create or replace function on_insert() returns trigger as $$
nano$# begin
nano$# if(new.booking_date >= date '2020-01-01' and new.booking_date <=date '2020-01-31') then
nano$# insert into jan_booking values(new.*);
nano$# elsif (new.booking_date >= date '2020-02-01' and new.booking_date <=date '2020-02-29') then
nano$# insert into feb_booking values(new.*);
nano$# else
nano$# raise exception 'Enter valid booking date';
nano$# end if;
nano$#
nano$# return null;
nano$# end;
nano$# $$ LANGUAGE plpgsql;
CREATE FUNCTION
nano=#
nano=# \df
List of functions
Schema | Name | Result data type | Argument data types | Type
--------+-----------+------------------+---------------------+------
public | on_insert | trigger | | func
(1 row)
nano=# create trigger booking_entry before insert on bookings for each row execute procedure on_insert();
CREATE TRIGGER
nano=#
nano=# \d bookings
Table "public.bookings"
Column | Type | Collation | Nullable | Default
--------------+-----------------------------+-----------+----------+---------
flightno | character varying(100) | | |
flightname | character varying(100) | | |
booking_date | timestamp without time zone | | |
Triggers:
booking_entry BEFORE INSERT ON bookings FOR EACH ROW EXECUTE FUNCTION on_insert()
Number of child tables: 3 (Use \d+ to list them.)
nano=# select * from bookings;
flightno | flightname | booking_date
----------+------------+--------------
(0 rows)
nano=# insert into bookings values('dxb102','emirates','2020-01-10');
INSERT 0 0
nano=# insert into bookings values('dxb103','lufthansa','2020-02-23');
ERROR: relation "feb_booking" does not exist
LINE 1: insert into feb_booking values(new.*)
^
QUERY: insert into feb_booking values(new.*)
CONTEXT: PL/pgSQL function on_insert() line 6 at SQL statement
nano=# insert into bookings values('dxb104','british','2020-02-08');
ERROR: relation "feb_booking" does not exist
LINE 1: insert into feb_booking values(new.*)
^
QUERY: insert into feb_booking values(new.*)
CONTEXT: PL/pgSQL function on_insert() line 6 at SQL statement
nano=# insert into bookings values('dxb105','nipon','2020-01-19');
INSERT 0 0
nano=# insert into bookings values('dxb104','british','2020-02-08');
ERROR: relation "feb_booking" does not exist
LINE 1: insert into feb_booking values(new.*)
^
QUERY: insert into feb_booking values(new.*)
CONTEXT: PL/pgSQL function on_insert() line 6 at SQL statement
nano=# select * from bookings;
flightno | flightname | booking_date
----------+------------+---------------------
dxb102 | emirates | 2020-01-10 00:00:00
dxb105 | nipon | 2020-01-19 00:00:00
(2 rows)
nano=# select * from only bookings;
flightno | flightname | booking_date
----------+------------+--------------
(0 rows)
nano=# select * from jan_bookings;
flightno | flightname | booking_date
----------+------------+--------------
(0 rows)
nano=# select * from bookings;
flightno | flightname | booking_date
----------+------------+---------------------
dxb102 | emirates | 2020-01-10 00:00:00
dxb105 | nipon | 2020-01-19 00:00:00
(2 rows)
nano=#
Copy Table
- Copy Table is used to copy the structure of a table along with data.
- Unlike Inheritance table, copy table does not have any relationship with the base table.
- Syntax with data:
CREATE TABLE new_table AS TABLE existing_table;
- Syntax without data:
CREATE TABLE new_table as TABLE existing_table with no data;
nano=#
nano=# create table train_bookings(trainno serial,trainname varchar(200),destination varchar(100));
CREATE TABLE
nano=# insert into train_bookings(trainname,destination)values('express','toronto');
INSERT 0 1
nano=# insert into train_bookings(trainname,destination)values('semiexpress','montreal');
INSERT 0 1
nano=# insert into train_bookings(trainname,destination)values('goods','calgary');
INSERT 0 1
nano=# select * from train_bookings;
trainno | trainname | destination
---------+-------------+-------------
1 | express | toronto
2 | semiexpress | montreal
3 | goods | calgary
(3 rows)
nano=# create table train_dest as table train_bookings;
SELECT 3
nano=# select * from train_dest;
trainno | trainname | destination
---------+-------------+-------------
1 | express | toronto
2 | semiexpress | montreal
3 | goods | calgary
(3 rows)
nano=# \d train_bookings
Table "public.train_bookings"
Column | Type | Collation | Nullable | Default
-------------+------------------------+-----------+----------+-------------------------------------------------
trainno | integer | | not null | nextval('train_bookings_trainno_seq'::regclass)
trainname | character varying(200) | | |
destination | character varying(100) | | |
nano=# \d train_dest
Table "public.train_dest"
Column | Type | Collation | Nullable | Default
-------------+------------------------+-----------+----------+---------
trainno | integer | | |
trainname | character varying(200) | | |
destination | character varying(100) | | |
nano=#
nano=# insert into train_dest(trainname,destination) values('passenger','yukon');
INSERT 0 1
nano=# select * from train_bookings;
trainno | trainname | destination
---------+-------------+-------------
1 | express | toronto
2 | semiexpress | montreal
3 | goods | calgary
(3 rows)
nano=# select * from train_dest;
trainno | trainname | destination
---------+-------------+-------------
1 | express | toronto
2 | semiexpress | montreal
3 | goods | calgary
| passenger | yukon
(4 rows)
nano=# drop table train_dest;
DROP TABLE
nano=# create table train_dest as table train_booking with no data;
ERROR: relation "train_booking" does not exist
LINE 1: create table train_dest as table train_booking with no data;
^
nano=# create table train_dest as table train_bookings with no data;
CREATE TABLE AS
nano=# select * from train_dest;
trainno | trainname | destination
---------+-----------+-------------
(0 rows)
nano=#