Skip to content

Commit b1ffed6

Browse files
committed
added query_knobs param to time_query
1 parent e938f8e commit b1ffed6

File tree

3 files changed

+23
-23
lines changed

3 files changed

+23
-23
lines changed

env/pg_conn.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,16 @@ def force_statement_timeout(self, timeout: float) -> None:
110110
retry = True
111111

112112
def time_query(
113-
self, query: str, add_explain: bool = False, timeout: float = 0
113+
self,
114+
query: str,
115+
query_knobs: list[str] = [],
116+
add_explain: bool = False,
117+
timeout: float = 0,
114118
) -> tuple[float, bool, Optional[dict[str, Any]]]:
115119
"""
116-
Run a query with a timeout (in seconds). If you want to attach per-query knobs, attach them to the query string
117-
itself. Following Postgres's convention, timeout=0 indicates "disable timeout"
120+
Run a query with a timeout (in seconds). Following Postgres's convention, timeout=0 indicates "disable timeout".
121+
122+
Use query_knobs to pass query knobs. An example input is query_knobs=["SET (enable_sort on)", "IndexOnlyScan(it)"].
118123
119124
It returns the runtime, whether the query timed out, and the explain data if add_explain is True. Note that if
120125
the query timed out, it won't have any explain data and thus explain_data will be None.
@@ -133,6 +138,9 @@ def time_query(
133138
explain_data = None
134139

135140
try:
141+
if query_knobs:
142+
query = f"/*+ {' '.join(query_knobs)} */ {query}"
143+
136144
if add_explain:
137145
assert (
138146
"explain" not in query.lower()

scripts/pat_test.sh

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ set -euxo pipefail
55
. ./experiments/load_per_machine_envvars.sh
66

77
# space for testing. uncomment this to run individual commands from the script (copy pasting is harder because there are envvars)
8-
python3 task.py tune protox agent hpo job --workload-name-suffix demo --num-samples 2 --max-concurrent 2 --workload-timeout 15 --query-timeout 2 --tune-duration-during-hpo 0.03 --intended-dbdata-hardware $INTENDED_DBDATA_HARDWARE --dbdata-parent-dpath $DBDATA_PARENT_DPATH --build-space-good-for-boot
98
python3 task.py tune protox agent tune job --workload-name-suffix demo
10-
python3 task.py tune protox agent replay job --workload-name-suffix demo
119
exit 0
1210

1311
# benchmark

tune/protox/env/util/execute.py

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
def _acquire_metrics_around_query(
2525
pg_conn: PostgresConn,
2626
query: str,
27+
query_knobs: list[str],
2728
query_timeout: float = 0.0,
2829
observation_space: Optional[StateSpace] = None,
2930
) -> tuple[float, bool, Optional[dict[str, Any]], Any]:
@@ -32,7 +33,7 @@ def _acquire_metrics_around_query(
3233
initial_metrics = observation_space.construct_online(pg_conn.conn())
3334

3435
qid_runtime, did_time_out, explain_data = pg_conn.time_query(
35-
query, add_explain=True, timeout=query_timeout
36+
query, query_knobs=query_knobs, add_explain=True, timeout=query_timeout
3637
)
3738

3839
if observation_space and observation_space.require_metrics():
@@ -61,31 +62,24 @@ def execute_variations(
6162
best_qr = BestQueryRun(None, None, True, None, None)
6263

6364
for qr in runs:
64-
# Attach the specific per-query knobs.
65-
pqk_query = (
66-
"/*+ "
67-
+ " ".join(
68-
[
69-
knob.resolve_per_query_knob(
70-
value,
71-
all_knobs=sysknobs if sysknobs else KnobSpaceContainer({}),
72-
)
73-
for knob, value in qr.qknobs.items()
74-
]
65+
# Get the per-query knobs for this query in the form list[str].
66+
query_knobs = [
67+
knob.resolve_per_query_knob(
68+
value,
69+
all_knobs=sysknobs if sysknobs else KnobSpaceContainer({}),
7570
)
76-
+ " */"
77-
+ query
78-
)
71+
for knob, value in qr.qknobs.items()
72+
]
7973

8074
# Log out the knobs that we are using.
81-
pqkk = [(knob.name(), val) for knob, val in qr.qknobs.items()]
8275
logging.getLogger(DBGYM_LOGGER_NAME).debug(
83-
f"{qr.prefix_qid} executing with {pqkk}"
76+
f"{qr.prefix_qid} executing with {query_knobs}"
8477
)
8578

8679
runtime, did_time_out, explain_data, metric = _acquire_metrics_around_query(
8780
pg_conn=pg_conn,
88-
query=pqk_query,
81+
query=query,
82+
query_knobs=query_knobs,
8983
query_timeout=timeout_limit,
9084
observation_space=observation_space,
9185
)

0 commit comments

Comments
 (0)