Repository navigation
Decision: read catalog metadata on every query, in one round trip #87
CharmingGroot
announced in
Announcements
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Background
Every
/sparqlrequest made 4–5 round trips to PostgreSQL before the first row (#76): the key query of Discussion #41 (prepare, then execute), the column-type probe (prepare only), the prepare of the statement (twice when a column is cast to text) and its execution. Ontop makes 2 (BEGINwith the query, thenCOMMIT). In the benchmark of #69,q01-lookupran its SQL for 0.05 ms and took 2.6 ms over HTTP, most of it waiting on the network. Keys are read on every query on purpose, so that a dropped key is never trusted (Discussions #41 and #84); column types and resolved column names likewise come from the database each time.Decision
Nothing read from the catalog is cached. Keys, column types and resolved column names are read on every query, as before. The round trips are cut by sending the reads together and by preparing the statement within the same round trip when the connection can tell in advance which one it will be:
column_name_probe) and reports column types (column_type_probe). Where the keys decide whether types are needed (whether duplicates must be removed), it is sent anyway.A known plan therefore takes 2 round trips (reads with prepare, then execute); the first query of a plan on a connection takes 3. When the remembered text is stale (a key was dropped, a column type changed), the comparison fails and the statement is prepared again: a stale guess costs one round trip, never a wrong result. The remembered statement is never reused across queries; it is parsed again in the same round trip as the reads, so it is as fresh as preparing it afterwards.
If the combined probe fails, its two steps run separately as before, so an error is reported by the step it belongs to.
Why not cache the metadata
pg_classrow versions, an event trigger counter). Pipelined with the other reads, such a check costs the same round trip as reading the keys and types themselves, whose execution is 0.05–0.1 ms. A cache would save no round trip and add a way to be wrong.What stays the same
Round-trip checks of constants, term keys, set semantics (#41), column resolution,
UNIONtyping and self-join elimination (#84) all run on the fresh keys and types, unchanged; the statement text is compared byte for byte. The SQL PostgreSQL runs is the same.Results
bench/RESULTS.md, counting proxy inbench.py): round trips per request 4–5 → 2 on every query (q08 3 → 2).q01-lookup2.6 → 1.2 ms median, first rows 0.6–1.7 ms earlier; q03 3.6 → 2.6 ms, q07 4.2 → 2.6 ms. The CLI opens one connection per query and gets 3.Costs
Database::connectnow also prepares the key query (no extra round trip).References
PREPARE(custom and generic plans) — https://www.postgresql.org/docs/17/sql-prepare.html배경
/sparql요청마다 첫 행 전에 PostgreSQL과 4~5번 왕복했다(#76): Discussion #41의 키 질의(prepare 후 execute), 컬럼 타입 probe(prepare만), 본 문장 prepare(컬럼을 텍스트로 캐스트하면 두 번), 실행. Ontop은 2번이다(질의와 함께BEGIN, 그다음COMMIT). #69 벤치마크에서q01-lookup은 SQL 실행이 0.05 ms인데 HTTP로 2.6 ms가 걸렸고, 대부분 네트워크 대기였다. 키는 삭제된 키를 믿지 않으려고 일부러 질의마다 읽는다(Discussion #41, #84). 컬럼 타입과 해석된 컬럼 이름도 매번 DB에서 가져온다.결정
카탈로그에서 읽은 것은 캐시하지 않는다. 키, 컬럼 타입, 해석된 컬럼 이름은 지금처럼 질의마다 읽는다. 왕복은 읽기를 한꺼번에 보내고, 연결이 어떤 문장이 될지 미리 알 수 있으면 같은 왕복 안에서 문장을 준비하는 방식으로 줄인다.
column_name_probe)과 컬럼 타입 보고(column_type_probe)를 함께 한다. 타입이 필요한지(중복을 제거해야 하는지)를 키가 정하는 경우에도 일단 보낸다.그래서 아는 계획은 왕복 2번(읽기와 준비, 그리고 실행), 연결에서 처음 보는 계획은 3번이다. 기억한 텍스트가 낡았으면(키 삭제, 컬럼 타입 변경) 비교가 실패하고 문장을 다시 준비한다: 낡은 추측은 왕복 한 번을 더 쓸 뿐, 틀린 결과를 내지 않는다. 기억한 문장을 질의 사이에 재사용하지 않는다. 읽기와 같은 왕복에서 다시 파싱하므로, 그 뒤에 준비하는 것만큼 최신이다.
합친 probe가 실패하면 두 단계를 예전처럼 따로 실행해, 오류가 해당 단계의 오류로 보고되게 한다.
메타데이터를 캐시하지 않는 이유
pg_class행 버전, 이벤트 트리거 카운터). 다른 읽기와 파이프라인으로 보내면 그 검사도 키와 타입을 직접 읽는 것과 같은 왕복을 쓰고, 실행은 0.05~0.1 ms다. 캐시는 왕복을 줄이지 못하고 틀릴 길만 더한다.그대로인 것
상수 왕복 검사, 항 키, 집합 의미(#41), 컬럼 해석,
UNION타입 맞춤, self-join 제거(#84)는 모두 새로 읽은 키와 타입으로 그대로 동작하고, 문장 텍스트는 바이트 단위로 비교한다. PostgreSQL이 실행하는 SQL은 같다.결과
bench/RESULTS.md,bench.py의 왕복 세기 프록시): 요청당 왕복이 모든 질의에서 45 → 2(q08은 3 → 2).1.7 ms 빨라짐. q03 3.6 → 2.6 ms, q07 4.2 → 2.6 ms. CLI는 질의마다 연결을 새로 열어 3번이다.q01-lookup중앙값 2.6 → 1.2 ms, 첫 행이 0.6비용
Database::connect가 키 질의도 준비한다(추가 왕복 없음).참고
PREPARE(custom·generic 계획) — https://www.postgresql.org/docs/17/sql-prepare.htmlAll reactions