Top Banner
Query Planner Daniel Antwi http://www.site.uottawa.ca/~dantw005 /csi3130/ CSI 3130 – 2012 – Lab 6
24

Query Planner Daniel Antwi dantw005/csi3130/ CSI 3130 – 2012 – Lab 6.

Dec 18, 2015

Download

Documents

Anis Harrell
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Query Planner Daniel Antwi dantw005/csi3130/ CSI 3130 – 2012 – Lab 6.

Query Planner

Daniel Antwihttp://www.site.uottawa.ca/~dantw005/csi3130/

CSI 3130 – 2012 – Lab 6

Page 2: Query Planner Daniel Antwi dantw005/csi3130/ CSI 3130 – 2012 – Lab 6.

Outline

• Query Planner: The Big Picture

• Modify Source Codes

Page 3: Query Planner Daniel Antwi 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.

Page 4: Query Planner Daniel Antwi dantw005/csi3130/ CSI 3130 – 2012 – Lab 6.
Page 5: Query Planner Daniel Antwi dantw005/csi3130/ CSI 3130 – 2012 – Lab 6.
Page 6: Query Planner Daniel Antwi dantw005/csi3130/ CSI 3130 – 2012 – Lab 6.

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

Page 7: Query Planner Daniel Antwi dantw005/csi3130/ CSI 3130 – 2012 – Lab 6.
Page 8: Query Planner Daniel Antwi dantw005/csi3130/ CSI 3130 – 2012 – Lab 6.
Page 9: Query Planner Daniel Antwi dantw005/csi3130/ CSI 3130 – 2012 – Lab 6.
Page 10: Query Planner Daniel Antwi dantw005/csi3130/ CSI 3130 – 2012 – Lab 6.
Page 11: Query Planner Daniel Antwi dantw005/csi3130/ CSI 3130 – 2012 – Lab 6.
Page 12: Query Planner Daniel Antwi dantw005/csi3130/ CSI 3130 – 2012 – Lab 6.
Page 13: Query Planner Daniel Antwi dantw005/csi3130/ CSI 3130 – 2012 – Lab 6.
Page 14: Query Planner Daniel Antwi dantw005/csi3130/ CSI 3130 – 2012 – Lab 6.
Page 15: Query Planner Daniel Antwi dantw005/csi3130/ CSI 3130 – 2012 – Lab 6.

Modify Planner Source Codes

• We will modify PostgreSQL source codes– query planner

• Under:– src/backend/optimizer– src/backend/optimizer/plan– src/backend/optimizer/path

Page 16: Query Planner Daniel Antwi dantw005/csi3130/ CSI 3130 – 2012 – Lab 6.

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;

Page 17: Query Planner Daniel Antwi dantw005/csi3130/ CSI 3130 – 2012 – Lab 6.

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

Page 18: Query Planner Daniel Antwi dantw005/csi3130/ CSI 3130 – 2012 – Lab 6.

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

Page 19: Query Planner Daniel Antwi dantw005/csi3130/ CSI 3130 – 2012 – Lab 6.

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()

Page 20: Query Planner Daniel Antwi dantw005/csi3130/ CSI 3130 – 2012 – Lab 6.

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

Page 21: Query Planner Daniel Antwi dantw005/csi3130/ CSI 3130 – 2012 – Lab 6.

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)

Page 22: Query Planner Daniel Antwi dantw005/csi3130/ CSI 3130 – 2012 – Lab 6.

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

Page 23: Query Planner Daniel Antwi dantw005/csi3130/ CSI 3130 – 2012 – Lab 6.

• 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

Page 24: Query Planner Daniel Antwi dantw005/csi3130/ CSI 3130 – 2012 – Lab 6.

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