/* * Copyright (c) 2026 dataStone Inc. * SPDX-License-Identifier: MIT */ /** * @file status.sql * @brief repack_safe.status() on a server that preloads the library, the * extension's fixed schema, and the library's version * * The extension can exist in a database whose server does not preload the * library; it then looks installed and guards nothing. The status function * returns true only on a server that preloads it, and false with a WARNING * otherwise; the extension's comment, which \dx shows, points to it. The * not-preloaded case is in t/009_not_preloaded.pl. */ -- The extension's schema is fixed: it cannot be created in another one. CREATE EXTENSION pg_repack_safe SCHEMA public; ERROR: extension "pg_repack_safe" must be installed in schema "repack_safe" -- A schema of that name that already exists is used only if a superuser -- owns it: its owner could drop the record table once it is empty. An -- earlier test that dropped the extension leaves its empty schema behind. SET client_min_messages = warning; DROP SCHEMA IF EXISTS repack_safe; RESET client_min_messages; CREATE ROLE status_schema_owner; CREATE SCHEMA repack_safe AUTHORIZATION status_schema_owner; -- The context would show the install script's text and line number. \set SHOW_CONTEXT never CREATE EXTENSION pg_repack_safe; ERROR: schema "repack_safe" must be owned by a superuser DETAIL: The schema is owned by role "status_schema_owner", which could drop the record table of extension "pg_repack_safe" and so make every query in this database fail. HINT: Drop the schema or give it to a superuser (ALTER SCHEMA ... OWNER TO), then create the extension again. \set SHOW_CONTEXT errors SELECT count(*) AS extensions FROM pg_extension WHERE extname = 'pg_repack_safe'; extensions ------------ 0 (1 row) ALTER SCHEMA repack_safe OWNER TO CURRENT_USER; CREATE EXTENSION pg_repack_safe; DROP EXTENSION pg_repack_safe; DROP SCHEMA repack_safe; DROP ROLE status_schema_owner; CREATE EXTENSION pg_repack_safe; -- \dx shows the comment, which names the check. SELECT obj_description(oid, 'pg_extension') AS comment FROM pg_extension WHERE extname = 'pg_repack_safe'; comment ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Stops PostgreSQL 19's REPACK (CONCURRENTLY) from making a table look empty to transactions already running: they get an error instead. In force only where SELECT repack_safe.status() returns true. SELECT * FROM pg_get_loaded_modules() shows the library version (1 row) -- Its objects are in the schema repack_safe, which CREATE EXTENSION -- created, and it is version 1.0. SELECT extnamespace::regnamespace AS schema, extrelocatable AS relocatable, extversion AS version FROM pg_extension WHERE extname = 'pg_repack_safe'; schema | relocatable | version -------------+-------------+--------- repack_safe | f | 1.0 (1 row) -- pg_get_loaded_modules() reports the library's name and that it has a -- version, on any server that loaded it, with or without the extension. SELECT module_name, version ~ '^[0-9]+\.[0-9]+\.[0-9]+$' AS has_version FROM pg_get_loaded_modules() WHERE module_name = 'pg_repack_safe'; module_name | has_version ----------------+------------- pg_repack_safe | t (1 row) -- This instance preloads the library (test/temp_instance.conf): true, and -- no warning. SELECT repack_safe.status(); status -------- t (1 row) -- Any role may call it. CREATE ROLE status_user; SET ROLE status_user; SELECT repack_safe.status(); status -------- t (1 row) RESET ROLE; DROP ROLE status_user; -- With the package uninstalled, the call fails with "could not access -- file" (doc/removal.md, step 5). The installed library -- cannot be removed here, since the other suites use it, so this pins the -- error PostgreSQL raises for the same function in a file that does not -- exist. It raises it as soon as the function is declared, since CREATE -- FUNCTION loads a C function's file. CREATE FUNCTION status_missing_library() RETURNS boolean AS '$libdir/pg_repack_safe_missing', 'pg_repack_safe_status' LANGUAGE C; ERROR: could not access file "pg_repack_safe_missing": No such file or directory DROP EXTENSION pg_repack_safe; -- After DROP EXTENSION the function is gone with it (doc/removal.md, -- step 5). SELECT repack_safe.status(); ERROR: function repack_safe.status() does not exist LINE 1: SELECT repack_safe.status(); ^ -- DROP EXTENSION leaves the schema, which is not a member of the extension. SELECT count(*) AS schemas FROM pg_namespace WHERE nspname = 'repack_safe'; schemas --------- 1 (1 row) DROP SCHEMA repack_safe;