Skip to content

Commit

Permalink
Clear the cache on DDL
Browse files Browse the repository at this point in the history
  • Loading branch information
msullivan committed Feb 20, 2024
1 parent 7c3f953 commit cdb9bca
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 1 deletion.
57 changes: 57 additions & 0 deletions edb/pgsql/metaschema.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,61 @@ def __init__(self) -> None:
)


class EvictQueryCacheFunction(dbops.Function):

text = f'''
DECLARE
evict_sql text;
BEGIN
DELETE FROM "edgedb"."_query_cache"
WHERE "key" = cache_key
RETURNING "evict" INTO evict_sql;
IF evict_sql IS NOT NULL THEN
EXECUTE evict_sql;
END IF;
END;
'''

def __init__(self) -> None:
super().__init__(
name=('edgedb', '_evict_query_cache'),
args=[("cache_key", ("uuid",))],
returns=("void",),
language='plpgsql',
volatility='volatile',
text=self.text,
)


class ClearQueryCacheFunction(dbops.Function):

# TODO(fantix): this may consume a lot of memory in Postgres
text = f'''
DECLARE
row record;
BEGIN
FOR row IN
DELETE FROM "edgedb"."_query_cache"
RETURNING "input", "evict"
LOOP
EXECUTE row."evict";
RETURN NEXT row."input";
END LOOP;
END;
'''

def __init__(self) -> None:
super().__init__(
name=('edgedb', '_clear_query_cache'),
args=[],
returns=('bytea',),
set_returning=True,
language='plpgsql',
volatility='volatile',
text=self.text,
)


class BigintDomain(dbops.Domain):
"""Bigint: a variant of numeric that enforces zero digits after the dot.
Expand Down Expand Up @@ -4553,6 +4608,8 @@ async def bootstrap(
dbops.CreateTable(DMLDummyTable()),
dbops.CreateTable(QueryCacheTable()),
dbops.Query(DMLDummyTable.SETUP_QUERY),
dbops.CreateFunction(EvictQueryCacheFunction()),
dbops.CreateFunction(ClearQueryCacheFunction()),
dbops.CreateFunction(UuidGenerateV1mcFunction('edgedbext')),
dbops.CreateFunction(UuidGenerateV4Function('edgedbext')),
dbops.CreateFunction(UuidGenerateV5Function('edgedbext')),
Expand Down
6 changes: 6 additions & 0 deletions edb/server/dbview/dbview.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import pickle
import struct
import time
import typing
import uuid
import weakref

import immutables
Expand Down Expand Up @@ -889,6 +890,11 @@ cdef class DatabaseConnectionView:
self._reset_tx_state()
return side_effects

async def clear_cache_keys(self, conn) -> list[bytes]:
rows = await conn.sql_fetch(b'SELECT "edgedb"."_clear_query_cache"()')
self._db._eql_to_compiled.clear()
return [row[0] for row in rows or []]

async def apply_config_ops(self, conn, ops):
settings = self.get_config_spec()

Expand Down
8 changes: 7 additions & 1 deletion edb/server/protocol/execute.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,9 @@ async def execute(
await persist_cache(be_conn, dbv, compiled)

if query_unit.sql:
if query_unit.ddl_stmt_id:
if query_unit.user_schema:
# TODO(fantix): do this in the same transaction
_recompile_requests = await dbv.clear_cache_keys(be_conn)
ddl_ret = await be_conn.run_ddl(query_unit, state)
if ddl_ret and ddl_ret['new_types']:
new_types = ddl_ret['new_types']
Expand Down Expand Up @@ -320,6 +322,10 @@ async def execute_script(
data = None

try:
if any(query_unit.user_schema for query_unit in unit_group):
# TODO(fantix): do this in the same transaction
_recompile_requests = await dbv.clear_cache_keys(conn)

if conn.last_state == state:
# the current status in be_conn is in sync with dbview, skip the
# state restoring
Expand Down

0 comments on commit cdb9bca

Please sign in to comment.