Skip to content

Commit db1ddcd

Browse files
authored
fix: set search_path once for the pgstac database (#245)
1 parent 496ea89 commit db1ddcd

1 file changed

Lines changed: 29 additions & 12 deletions

File tree

lib/database/bootstrapper_runtime/handler.py

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,6 @@ def update_user_permissions(cursor, db_name: str, username: str) -> None:
130130
"GRANT pgstac_read TO {username};"
131131
"GRANT pgstac_ingest TO {username};"
132132
"GRANT pgstac_admin TO {username};"
133-
"ALTER USER {username} SET search_path TO pgstac, public;" # add pgstac to search_path by default
134133
).format(
135134
db_name=sql.Identifier(db_name),
136135
username=sql.Identifier(username),
@@ -252,6 +251,27 @@ def unregister_pg_cron(cursor) -> None:
252251
)
253252

254253

254+
def set_database_search_path(cursor, db_name: str) -> None:
255+
"""Set the default search_path for all connections to the pgSTAC database.
256+
257+
Uses ``ALTER DATABASE`` so the setting applies to every session (including
258+
pg_cron background workers, which start with no ``search_path``). This is
259+
preferred over per-user or per-connection settings because it is a single
260+
authoritative place.
261+
262+
The cursor must be connected as a superuser (the RDS admin user).
263+
264+
Args:
265+
cursor: Database cursor connected as a superuser.
266+
db_name: Name of the pgSTAC database to configure.
267+
"""
268+
cursor.execute(
269+
sql.SQL("ALTER DATABASE {db_name} SET search_path TO pgstac, public;").format(
270+
db_name=sql.Identifier(db_name),
271+
)
272+
)
273+
274+
255275
def register_pg_cron(cursor, db_name: str, schedule: str) -> None:
256276
"""Install the pg_cron extension and schedule run_queued_queries.
257277
@@ -263,23 +283,12 @@ def register_pg_cron(cursor, db_name: str, schedule: str) -> None:
263283
pg_cron must be listed in ``shared_preload_libraries`` in the RDS parameter
264284
group before this will succeed.
265285
266-
Side effect: sets ``search_path = pgstac, public`` as the default for all
267-
connections to ``db_name`` via ``ALTER DATABASE``. This is required because
268-
pg_cron background worker sessions start with no ``search_path``, and
269-
prepending ``SET search_path`` to the cron command would open a transaction
270-
that prevents ``run_queued_queries()`` from issuing its own ``COMMIT``.
271-
272286
Args:
273287
cursor: Database cursor connected to the ``postgres`` database as a superuser.
274288
db_name: Name of the pgSTAC database where run_queued_queries will run.
275289
schedule: Cron schedule expression (e.g. ``"*/5 * * * *"``).
276290
"""
277291
cursor.execute(sql.SQL("CREATE EXTENSION IF NOT EXISTS pg_cron;"))
278-
cursor.execute(
279-
sql.SQL("ALTER DATABASE {db_name} SET search_path TO pgstac, public;").format(
280-
db_name=sql.Identifier(db_name),
281-
)
282-
)
283292
cursor.execute(
284293
sql.SQL(
285294
"SELECT cron.schedule_in_database({job_name}, {schedule}, 'CALL pgstac.run_queued_queries();', {db_name});"
@@ -337,6 +346,14 @@ def handler(event, context):
337346
password=eoapi_params["password"],
338347
)
339348

349+
print(
350+
f"Setting default search_path for '{eoapi_params['dbname']}' database..."
351+
)
352+
set_database_search_path(
353+
cursor=cur,
354+
db_name=eoapi_params["dbname"],
355+
)
356+
340357
if "use_queue" in params:
341358
if _is_enabled(params, "use_queue"):
342359
schedule = params.get(

0 commit comments

Comments
 (0)