query planner daniel antwi dantw005/csi3130/ csi 3130 – 2012 – lab 6
TRANSCRIPT
Query Planner
Daniel Antwihttp://www.site.uottawa.ca/~dantw005/csi3130/
CSI 3130 – 2012 – Lab 6
PostgreSQL multiuser database server
• postmaster is the PostgreSQL multiuser database server.
• In order for a client application to access a database it connects to a running postmaster.
• The postmaster then starts a separate server process postgres to handle the connection.
• The postmaster also manages the communication among server processes.
Planner/Optimizer
• The task of the planner/optimizer is to create an optimal execution plan.– Scan methods
• seqscan, index scan ...
– Join methods• nested loop join, merge join, hash join
– Join order• for more than two relations
Modify Planner Source Codes
• We will modify PostgreSQL source codes– query planner
• Under:– src/backend/optimizer– src/backend/optimizer/plan– src/backend/optimizer/path
•
Modify the Code: Example
• Add the debug maro– Edit "src/backend/optimizer/plan/planner.c“ and
“src/backend/optimizer/path/allpaths.c”– Add “#define OPTIMIZER_DEBUG”
• Edit “/src/backend/optimizer/util/pathnode.c”– After line: 405, add one line
• pathnode->total_cost=0;
– Make install again– Start the server and try
• explain select * from cust where cid=100;
Debug using GDB• Start GDB
– gdb ./postgres• Set breakpoints
– break pathnode.c:402• Start the server in single-user mode
– start --single -D../data postgres– continue
• Execute a SQL– explain select * from cust where cid=100;– (break at the breakpoint in pathnode.c)
• Track– backtrace (see the calling stack)– step 1 (track step by step)– continue
Related Files
• src/backend/optimizer/plan/planmain.c• src/backend/optimizer/path/allpaths.c
– set_plain_rel_pathlist
• src/backend/optimizer/path/costsize.c• src/backend/optimizer/path/pathkeys.c• src/backend/optimizer/plan/createplan.c• src/backend/optimizer/README
– Optimizer Data Structure– PathKeys
Related Source Codes
• src/backend/optimizer/plan/planmain.c– Line 85: query_planner()
• Generate a plan
• src/backend/optimizer/plan/allpaths.c– Line 86: make_one_relation()
• Find all paths to generate a result relation– Line 166: set_rel_pathlist()– Line 214: set_plain_rel_pathlist()
• Build paths for a base relation
• /src/backend/optimizer/util/pathnode.c– Line 397: create_seqscan_path()
Get Details of Plans• In PSQL use EXPLAIN VERBOSE• From server:
– use “--log-error-verbosity=verbose -d5”• Enable OPTIMIZER_DEBUG
– Add the following line at the beginning of "src/backend/optimizer/plan/planner.c“ and “src/backend/optimizer/path/allpaths.c”
– #define OPTIMIZER_DEBUG– And then make install again
• Try the queries from the last lab– example
List of Commands - Installationcd postgresql-9.0.2
./configure --enable-debug --enable-cassert --enable-depend --prefix=/home/rwchen/pginstall
• Make• Build PostgreSQL• $gmake• Wait for this message: • All of PostgreSQL successfully made. Ready to install.• Install• Install PostgreSQL to $HOME/pginsall directory• $make install• Wait for this message:• PostgreSQL installation complete.
Change to your own directory (use pwd to get
current path)
List of Commands - GDBgdb ./postgres
break pathnode.c:402
start --single -D../data postgres
continue
backtrace
step 1
cd ../pginstall/bin
./initdb –D../data
./postgres –D../data –p 5678
./psql postgres –p 5678
./postgres --single –D../data postgres(use Ctrl+D to exit)
Change to a random port number to avoid conflicts
Single-user mode
• GDB reference card– http://users.ece.utexas.edu/~adnan/gdb-
refcard.pdf
• vi reference card– http://www.digilife.be/quickreferences/QRC/Vi
%20Reference%20Card.pdf
Reference
• Chap. 43.5. Planner/Optimizer– http://www.postgresql.org/docs/8.4/interactive/
planner-optimizer.html
• Chap 17.3. Starting the Database Server– http://www.postgresql.org/docs/8.4/interactive/server-
start.html
• PostgreSQL Internals Presentation– http://www.postgresql.org/files/developer/
internalpics.pdf