postgresai 0.17.0 → 0.18.0-beta.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +289 -1
- package/dist/bin/postgres-ai.js +19389 -12322
- package/dist/sql/01.role.sql +5 -4
- package/dist/sql/02.extensions.sql +2 -1
- package/dist/sql/03.permissions.sql +141 -27
- package/dist/sql/04.optional_rds.sql +3 -1
- package/dist/sql/05.optional_self_managed.sql +1 -0
- package/dist/sql/06.helpers.sql +43 -8
- package/dist/sql/uninit/01.helpers.sql +5 -0
- package/dist/sql/uninit/02.permissions.sql +89 -21
- package/dist/sql/uninit/03.role.sql +9 -5
- package/package.json +2 -2
- package/sql/01.role.sql +5 -4
- package/sql/02.extensions.sql +2 -1
- package/sql/03.permissions.sql +141 -27
- package/sql/04.optional_rds.sql +3 -1
- package/sql/05.optional_self_managed.sql +1 -0
- package/sql/06.helpers.sql +43 -8
- package/sql/uninit/01.helpers.sql +5 -0
- package/sql/uninit/02.permissions.sql +89 -21
- package/sql/uninit/03.role.sql +9 -5
- package/dist/sql/sql/01.role.sql +0 -16
- package/dist/sql/sql/02.extensions.sql +0 -8
- package/dist/sql/sql/03.permissions.sql +0 -86
- package/dist/sql/sql/04.optional_rds.sql +0 -6
- package/dist/sql/sql/05.optional_self_managed.sql +0 -8
- package/dist/sql/sql/06.helpers.sql +0 -317
- package/dist/sql/sql/uninit/01.helpers.sql +0 -5
- package/dist/sql/sql/uninit/02.permissions.sql +0 -30
- package/dist/sql/sql/uninit/03.role.sql +0 -27
package/sql/03.permissions.sql
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
-- Required permissions for postgres_ai monitoring user (template-filled by cli/lib/init.ts)
|
|
2
|
+
-- The first DO block also runs as the admin; pin before any name resolution.
|
|
3
|
+
set search_path = pg_catalog, pg_temp;
|
|
2
4
|
|
|
3
5
|
-- Allow connect
|
|
4
6
|
grant connect on database {{DB_IDENT}} to {{ROLE_IDENT}};
|
|
@@ -7,18 +9,116 @@ grant connect on database {{DB_IDENT}} to {{ROLE_IDENT}};
|
|
|
7
9
|
grant pg_monitor to {{ROLE_IDENT}};
|
|
8
10
|
grant select on pg_catalog.pg_index to {{ROLE_IDENT}};
|
|
9
11
|
|
|
10
|
-
--
|
|
11
|
-
--
|
|
12
|
-
--
|
|
13
|
-
--
|
|
14
|
-
-- in the pg_stat_statements metric. pg_monitor already implies pg_read_all_stats;
|
|
15
|
-
-- granting it explicitly documents the dependency and keeps the metric working if the
|
|
16
|
-
-- pg_monitor grant above is ever narrowed. Idempotent.
|
|
17
|
-
grant pg_read_all_stats to {{ROLE_IDENT}};
|
|
12
|
+
-- Cluster-wide pg_stat_statements collection needs pg_read_all_stats (without it, other
|
|
13
|
+
-- users' rows have queryid and query text redacted to NULL). pg_monitor includes it, so it
|
|
14
|
+
-- is not granted directly: on PostgreSQL 16+ that needs ADMIN OPTION on pg_read_all_stats,
|
|
15
|
+
-- which Supabase's `postgres` role does not have (it has ADMIN OPTION on pg_monitor only).
|
|
18
16
|
|
|
19
17
|
-- Create postgres_ai schema for our objects
|
|
20
18
|
-- Using IF NOT EXISTS for idempotency - prepare-db can be run multiple times
|
|
21
19
|
create schema if not exists postgres_ai;
|
|
20
|
+
-- An existing schema keeps its owner, and CREATE OR REPLACE keeps the owner of
|
|
21
|
+
-- existing functions/views. Refuse a schema that another role can still write:
|
|
22
|
+
-- the monitoring role searches postgres_ai and calls our helper from it.
|
|
23
|
+
do {{DO_QUOTE}}
|
|
24
|
+
declare
|
|
25
|
+
schema_oid oid;
|
|
26
|
+
admin_oid oid;
|
|
27
|
+
schema_owner text;
|
|
28
|
+
create_grantee text;
|
|
29
|
+
schema_extension text;
|
|
30
|
+
foreign_object text;
|
|
31
|
+
begin
|
|
32
|
+
set local search_path = pg_catalog, pg_temp;
|
|
33
|
+
select oid into admin_oid from pg_catalog.pg_roles where rolname = current_user;
|
|
34
|
+
select oid into schema_oid from pg_catalog.pg_namespace where nspname = 'postgres_ai';
|
|
35
|
+
|
|
36
|
+
select pg_catalog.pg_get_userbyid(n.nspowner) into schema_owner
|
|
37
|
+
from pg_catalog.pg_namespace n where n.oid = schema_oid;
|
|
38
|
+
if schema_owner <> current_user then
|
|
39
|
+
raise exception 'postgres_ai schema must be owned by the prepare-db administrator'
|
|
40
|
+
using errcode = '42501',
|
|
41
|
+
detail = pg_catalog.format('Schema owner: %s; prepare-db user: %s', schema_owner, current_user),
|
|
42
|
+
hint = 'Have a DBA inspect postgres_ai, then transfer its ownership to the prepare-db user if safe.';
|
|
43
|
+
end if;
|
|
44
|
+
|
|
45
|
+
select case when a.grantee = 0 then 'PUBLIC' else pg_catalog.pg_get_userbyid(a.grantee) end
|
|
46
|
+
into create_grantee
|
|
47
|
+
from pg_catalog.pg_namespace n,
|
|
48
|
+
pg_catalog.aclexplode(coalesce(n.nspacl, pg_catalog.acldefault('n', n.nspowner))) a
|
|
49
|
+
where n.oid = schema_oid and a.privilege_type = 'CREATE' and a.grantee <> admin_oid
|
|
50
|
+
limit 1;
|
|
51
|
+
if create_grantee is not null then
|
|
52
|
+
raise exception 'postgres_ai schema grants CREATE to another role'
|
|
53
|
+
using errcode = '42501',
|
|
54
|
+
detail = pg_catalog.format('CREATE grantee: %s', create_grantee),
|
|
55
|
+
hint = 'Have a DBA inspect the grant, then revoke CREATE on postgres_ai from that role or PUBLIC if safe.';
|
|
56
|
+
end if;
|
|
57
|
+
|
|
58
|
+
-- Trusted extensions can install bootstrap-owned members without owner
|
|
59
|
+
-- records in pg_shdepend (bootstrap superuser oid 10). Keep extensions out
|
|
60
|
+
-- of the monitoring search path regardless of their extension owner.
|
|
61
|
+
select e.extname into schema_extension
|
|
62
|
+
from pg_catalog.pg_extension e
|
|
63
|
+
where e.extnamespace = schema_oid
|
|
64
|
+
limit 1;
|
|
65
|
+
if schema_extension is not null then
|
|
66
|
+
raise exception 'postgres_ai schema contains an extension'
|
|
67
|
+
using errcode = '42501',
|
|
68
|
+
detail = pg_catalog.format('Extension: %s', schema_extension),
|
|
69
|
+
hint = 'Have a DBA inspect and move or remove this extension if safe before retrying prepare-db.';
|
|
70
|
+
end if;
|
|
71
|
+
|
|
72
|
+
-- Pinned/predefined roles have no pg_shdepend owner rows. Read catalog
|
|
73
|
+
-- owners directly before the dependency scan so those objects cannot hide.
|
|
74
|
+
with owned as (
|
|
75
|
+
select 'pg_class'::pg_catalog.regclass::oid as classid, c.oid as objid, c.relowner as owner_oid
|
|
76
|
+
from pg_catalog.pg_class c where c.relnamespace = schema_oid
|
|
77
|
+
union all select 'pg_proc'::pg_catalog.regclass::oid, p.oid, p.proowner
|
|
78
|
+
from pg_catalog.pg_proc p where p.pronamespace = schema_oid
|
|
79
|
+
union all select 'pg_type'::pg_catalog.regclass::oid, t.oid, t.typowner
|
|
80
|
+
from pg_catalog.pg_type t where t.typnamespace = schema_oid
|
|
81
|
+
union all select 'pg_operator'::pg_catalog.regclass::oid, o.oid, o.oprowner
|
|
82
|
+
from pg_catalog.pg_operator o where o.oprnamespace = schema_oid
|
|
83
|
+
union all select 'pg_opclass'::pg_catalog.regclass::oid, o.oid, o.opcowner
|
|
84
|
+
from pg_catalog.pg_opclass o where o.opcnamespace = schema_oid
|
|
85
|
+
union all select 'pg_opfamily'::pg_catalog.regclass::oid, o.oid, o.opfowner
|
|
86
|
+
from pg_catalog.pg_opfamily o where o.opfnamespace = schema_oid
|
|
87
|
+
union all select 'pg_collation'::pg_catalog.regclass::oid, c.oid, c.collowner
|
|
88
|
+
from pg_catalog.pg_collation c where c.collnamespace = schema_oid
|
|
89
|
+
union all select 'pg_conversion'::pg_catalog.regclass::oid, c.oid, c.conowner
|
|
90
|
+
from pg_catalog.pg_conversion c where c.connamespace = schema_oid
|
|
91
|
+
union all select 'pg_ts_dict'::pg_catalog.regclass::oid, d.oid, d.dictowner
|
|
92
|
+
from pg_catalog.pg_ts_dict d where d.dictnamespace = schema_oid
|
|
93
|
+
union all select 'pg_ts_config'::pg_catalog.regclass::oid, c.oid, c.cfgowner
|
|
94
|
+
from pg_catalog.pg_ts_config c where c.cfgnamespace = schema_oid
|
|
95
|
+
union all select 'pg_statistic_ext'::pg_catalog.regclass::oid, s.oid, s.stxowner
|
|
96
|
+
from pg_catalog.pg_statistic_ext s where s.stxnamespace = schema_oid
|
|
97
|
+
)
|
|
98
|
+
select pg_catalog.format('%s %s (owner %s)', o.type, o.identity,
|
|
99
|
+
pg_catalog.pg_get_userbyid(owned.owner_oid)) into foreign_object
|
|
100
|
+
from owned
|
|
101
|
+
cross join lateral pg_catalog.pg_identify_object(owned.classid, owned.objid, 0) o
|
|
102
|
+
where owned.owner_oid <> admin_oid
|
|
103
|
+
limit 1;
|
|
104
|
+
|
|
105
|
+
-- Retain a dependency scan for object kinds not represented above.
|
|
106
|
+
if foreign_object is null then
|
|
107
|
+
select o.identity into foreign_object
|
|
108
|
+
from pg_catalog.pg_shdepend s
|
|
109
|
+
cross join lateral pg_catalog.pg_identify_object(s.classid, s.objid, s.objsubid) o
|
|
110
|
+
where s.dbid = (select oid from pg_catalog.pg_database where datname = pg_catalog.current_database())
|
|
111
|
+
and s.deptype = 'o' and s.refobjid <> admin_oid
|
|
112
|
+
and o.schema = 'postgres_ai'
|
|
113
|
+
limit 1;
|
|
114
|
+
end if;
|
|
115
|
+
if foreign_object is not null then
|
|
116
|
+
raise exception 'postgres_ai schema contains objects owned by another role'
|
|
117
|
+
using errcode = '42501',
|
|
118
|
+
detail = pg_catalog.format('Foreign-owned object: %s', foreign_object),
|
|
119
|
+
hint = 'Have a DBA inspect this object, then reassign or remove it if safe before retrying prepare-db.';
|
|
120
|
+
end if;
|
|
121
|
+
end {{DO_QUOTE}};
|
|
22
122
|
grant usage on schema postgres_ai to {{ROLE_IDENT}};
|
|
23
123
|
|
|
24
124
|
-- For bloat analysis: expose pg_statistic via a view
|
|
@@ -44,43 +144,57 @@ grant usage on schema public to {{ROLE_IDENT}};
|
|
|
44
144
|
-- Grant access to the schema where pg_stat_statements is installed.
|
|
45
145
|
-- Some providers (e.g., Supabase) install extensions in a separate 'extensions' schema
|
|
46
146
|
-- rather than pg_catalog. This DO block detects the schema and grants USAGE if needed.
|
|
47
|
-
do
|
|
147
|
+
do {{DO_QUOTE}}
|
|
48
148
|
declare
|
|
49
149
|
ext_schema text;
|
|
50
150
|
begin
|
|
51
151
|
select n.nspname into ext_schema
|
|
52
|
-
from pg_extension e
|
|
53
|
-
join pg_namespace n on e.extnamespace = n.oid
|
|
152
|
+
from pg_catalog.pg_extension e
|
|
153
|
+
join pg_catalog.pg_namespace n on e.extnamespace = n.oid
|
|
54
154
|
where e.extname = 'pg_stat_statements';
|
|
55
155
|
|
|
56
156
|
-- Only grant if extension exists and is in a non-standard schema
|
|
57
157
|
if ext_schema is not null and ext_schema not in ('pg_catalog', 'public') then
|
|
58
|
-
execute format('grant usage on schema %I to {{
|
|
158
|
+
execute pg_catalog.format('grant usage on schema %I to %I', ext_schema, {{ROLE_LITERAL}});
|
|
59
159
|
end if;
|
|
60
|
-
end
|
|
160
|
+
end {{DO_QUOTE}};
|
|
61
161
|
|
|
62
|
-
-- [SEARCH_PATH_BLOCK_START]
|
|
63
|
-
--
|
|
64
|
-
|
|
162
|
+
-- [SEARCH_PATH_BLOCK_START] Resolve built-ins from pg_catalog first; keep pg_temp last.
|
|
163
|
+
-- Do not include the role-named schema: it may be writable by that role.
|
|
164
|
+
-- public stays for pg_stat_statements. Residual risk: a function in public with
|
|
165
|
+
-- an exact-type match (e.g. public.quote_ident(name)) still beats pg_catalog.
|
|
166
|
+
-- The path for this database includes its pg_stat_statements schema if it is in a non-standard location.
|
|
167
|
+
do {{DO_QUOTE}}
|
|
65
168
|
declare
|
|
66
169
|
ext_schema text;
|
|
67
|
-
|
|
170
|
+
role_path constant text := 'pg_catalog, postgres_ai, public, pg_temp';
|
|
171
|
+
db_path text := role_path;
|
|
68
172
|
begin
|
|
173
|
+
-- This block runs as the admin: resolve its operators and functions from pg_catalog only
|
|
174
|
+
-- (CVE-2018-1058). The pin is inside the block: a DO block always runs in a transaction,
|
|
175
|
+
-- also in psql autocommit.
|
|
176
|
+
set local search_path = pg_catalog, pg_temp;
|
|
69
177
|
-- Detect pg_stat_statements extension schema
|
|
70
178
|
select n.nspname into ext_schema
|
|
71
|
-
from pg_extension e
|
|
72
|
-
join pg_namespace n on e.extnamespace = n.oid
|
|
179
|
+
from pg_catalog.pg_extension e
|
|
180
|
+
join pg_catalog.pg_namespace n on e.extnamespace = n.oid
|
|
73
181
|
where e.extname = 'pg_stat_statements';
|
|
74
182
|
|
|
75
|
-
-- Build search_path: include extension schema if in non-standard location
|
|
76
183
|
if ext_schema is not null and ext_schema not in ('pg_catalog', 'public') then
|
|
77
|
-
|
|
78
|
-
else
|
|
79
|
-
sp := 'postgres_ai, "$user", public, pg_catalog';
|
|
184
|
+
db_path := 'pg_catalog, postgres_ai, ' || pg_catalog.quote_ident(ext_schema) || ', public, pg_temp';
|
|
80
185
|
end if;
|
|
81
186
|
|
|
82
|
-
|
|
83
|
-
|
|
187
|
+
-- Role-wide value: for databases that prepare-db did not run in. All databases share
|
|
188
|
+
-- this one row, so write it only when it holds another value.
|
|
189
|
+
if not exists (
|
|
190
|
+
select from pg_catalog.pg_db_role_setting
|
|
191
|
+
where setrole = (select oid from pg_catalog.pg_roles where rolname = {{ROLE_LITERAL}})
|
|
192
|
+
and setdatabase = 0
|
|
193
|
+
and ('search_path=' || role_path) = any (setconfig)
|
|
194
|
+
) then
|
|
195
|
+
execute pg_catalog.format('alter user %I set search_path = %s', {{ROLE_LITERAL}}, role_path);
|
|
196
|
+
end if;
|
|
197
|
+
-- This database's value: other databases keep their own pg_stat_statements schema.
|
|
198
|
+
execute pg_catalog.format('alter user %I in database %I set search_path = %s', {{ROLE_LITERAL}}, pg_catalog.current_database(), db_path);
|
|
199
|
+
end {{DO_QUOTE}};
|
|
84
200
|
-- [SEARCH_PATH_BLOCK_END]
|
|
85
|
-
|
|
86
|
-
|
package/sql/04.optional_rds.sql
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
-- Optional permissions for RDS Postgres / Aurora (best effort)
|
|
2
|
+
set search_path = pg_catalog, pg_temp;
|
|
2
3
|
|
|
3
|
-
create
|
|
4
|
+
create schema if not exists rds_tools;
|
|
5
|
+
create extension if not exists rds_tools with schema rds_tools;
|
|
4
6
|
grant execute on function rds_tools.pg_ls_multixactdir() to {{ROLE_IDENT}};
|
|
5
7
|
|
|
6
8
|
|
package/sql/06.helpers.sql
CHANGED
|
@@ -1,6 +1,18 @@
|
|
|
1
1
|
-- Helper functions for postgres_ai monitoring user (template-filled by cli/lib/init.ts)
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
set search_path = pg_catalog, pg_temp;
|
|
3
|
+
|
|
4
|
+
-- Remove the retired helper from databases prepared by older CLI versions.
|
|
5
|
+
-- Only its owner or a superuser can drop it: anyone else gets a warning here. Releases
|
|
6
|
+
-- installed table_describe with the same owner, so replacing it below then fails.
|
|
7
|
+
do {{DO_QUOTE}}
|
|
8
|
+
begin
|
|
9
|
+
-- Runs as the admin: resolve names from pg_catalog only (CVE-2018-1058).
|
|
10
|
+
set local search_path = pg_catalog, pg_temp;
|
|
11
|
+
drop function if exists postgres_ai.explain_generic(text, text, text);
|
|
12
|
+
exception when insufficient_privilege then
|
|
13
|
+
raise warning 'postgres_ai: legacy SECURITY DEFINER helper was NOT removed: %', sqlerrm
|
|
14
|
+
using hint = 'Its owner or a superuser must drop it, then re-run prepare-db.';
|
|
15
|
+
end {{DO_QUOTE}};
|
|
4
16
|
|
|
5
17
|
/*
|
|
6
18
|
* table_describe
|
|
@@ -15,18 +27,25 @@
|
|
|
15
27
|
*
|
|
16
28
|
* Usage:
|
|
17
29
|
* select postgres_ai.table_describe('public.users');
|
|
18
|
-
* select postgres_ai.table_describe('my_table'); --
|
|
30
|
+
* select postgres_ai.table_describe('my_table'); -- pg_catalog, then public
|
|
31
|
+
*
|
|
32
|
+
* SECURITY INVOKER: it reads only the world-readable catalog, so it needs no
|
|
33
|
+
* privilege the caller lacks. Names are resolved by catalog query, not regclass
|
|
34
|
+
* (which demands USAGE on the schema): unqualified in pg_catalog, then public.
|
|
35
|
+
* search_path is pg_catalog, pg_temp so objects in a writable schema cannot
|
|
36
|
+
* shadow a built-in the function calls.
|
|
19
37
|
*/
|
|
20
38
|
create or replace function postgres_ai.table_describe(
|
|
21
39
|
in table_name text,
|
|
22
40
|
out result text
|
|
23
41
|
)
|
|
24
42
|
language plpgsql
|
|
25
|
-
security
|
|
26
|
-
set search_path = pg_catalog,
|
|
43
|
+
security invoker
|
|
44
|
+
set search_path = pg_catalog, pg_temp
|
|
27
45
|
as $$
|
|
28
46
|
declare
|
|
29
47
|
v_oid oid;
|
|
48
|
+
v_ident text[];
|
|
30
49
|
v_schema text;
|
|
31
50
|
v_table text;
|
|
32
51
|
v_relkind char;
|
|
@@ -37,8 +56,24 @@ declare
|
|
|
37
56
|
v_rec record;
|
|
38
57
|
v_constraint_count int := 0;
|
|
39
58
|
begin
|
|
40
|
-
-- Resolve table name to OID
|
|
41
|
-
|
|
59
|
+
-- Resolve table name to OID: [current database.]schema.table (pg_temp is the
|
|
60
|
+
-- caller's temp schema), or an unqualified name in pg_catalog, then public
|
|
61
|
+
v_ident := parse_ident(table_name);
|
|
62
|
+
select c.oid into v_oid
|
|
63
|
+
from pg_class c
|
|
64
|
+
join pg_namespace n on n.oid = c.relnamespace
|
|
65
|
+
where (cardinality(v_ident) <= 2 or (cardinality(v_ident) = 3 and v_ident[1] = current_database()))
|
|
66
|
+
and c.relname = v_ident[cardinality(v_ident)]::name
|
|
67
|
+
and case
|
|
68
|
+
when cardinality(v_ident) = 1 then n.nspname in ('pg_catalog', 'public')
|
|
69
|
+
when v_ident[cardinality(v_ident) - 1] = 'pg_temp' then n.oid = pg_my_temp_schema()
|
|
70
|
+
else n.nspname = v_ident[cardinality(v_ident) - 1]::name
|
|
71
|
+
end
|
|
72
|
+
order by n.nspname = 'public'
|
|
73
|
+
limit 1;
|
|
74
|
+
if v_oid is null then
|
|
75
|
+
raise exception 'relation "%" does not exist', table_name using errcode = '42P01';
|
|
76
|
+
end if;
|
|
42
77
|
|
|
43
78
|
-- Get basic table info
|
|
44
79
|
select
|
|
@@ -207,7 +242,7 @@ begin
|
|
|
207
242
|
when 'f' then v_line := v_line || 'FK: ';
|
|
208
243
|
when 'u' then v_line := v_line || 'UNIQUE: ';
|
|
209
244
|
when 'c' then v_line := v_line || 'CHECK: ';
|
|
210
|
-
else v_line := v_line || v_rec.contype || ': ';
|
|
245
|
+
else v_line := v_line || v_rec.contype::text || ': ';
|
|
211
246
|
end case;
|
|
212
247
|
v_line := v_line || v_rec.conname || ' ' || v_rec.condef;
|
|
213
248
|
v_lines := array_append(v_lines, v_line);
|
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
-- Drop helper functions created by prepare-db (template-filled by cli/lib/init.ts)
|
|
2
2
|
-- Run before dropping the postgres_ai schema.
|
|
3
3
|
|
|
4
|
+
-- Objects in other schemas (for example, a planted public.format) must not run as the admin.
|
|
5
|
+
-- Use a session SET because --print-sql plans may be run by psql in autocommit.
|
|
6
|
+
-- SET LOCAL outside an explicit transaction is ignored there.
|
|
7
|
+
set search_path = pg_catalog, pg_temp;
|
|
8
|
+
|
|
4
9
|
drop function if exists postgres_ai.explain_generic(text, text, text);
|
|
5
10
|
drop function if exists postgres_ai.table_describe(text);
|
|
@@ -1,30 +1,98 @@
|
|
|
1
1
|
-- Revoke permissions and drop objects created by prepare-db (template-filled by cli/lib/init.ts)
|
|
2
2
|
|
|
3
|
+
-- Objects in other schemas (for example, a planted public.=) must not run as the admin.
|
|
4
|
+
-- Printed plans may run in psql autocommit, where SET LOCAL is ignored.
|
|
5
|
+
set search_path = pg_catalog, pg_temp;
|
|
6
|
+
-- The CLI reads the "unprepare-db:" WARNINGs. A higher setting would hide them.
|
|
7
|
+
set client_min_messages = warning;
|
|
8
|
+
|
|
3
9
|
-- Drop the postgres_ai.pg_statistic view
|
|
4
10
|
drop view if exists postgres_ai.pg_statistic;
|
|
5
11
|
|
|
6
|
-
--
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
--
|
|
10
|
-
--
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
end $$;
|
|
12
|
+
-- Revoke only the grants that the role holds. The postgres_ai schema stays
|
|
13
|
+
-- because it may contain customer objects. The pg_read_all_stats membership
|
|
14
|
+
-- comes from older prepare-db versions. The functions come from optional
|
|
15
|
+
-- permissions (04, 05). A non-superuser admin cannot revoke a grant that a
|
|
16
|
+
-- superuser made. Pass 1 revokes. Pass 2 sends one "grant left" WARNING for
|
|
17
|
+
-- each grant that stays. The CLI shows these WARNINGs and exits 1.
|
|
18
|
+
do {{DO_QUOTE}}
|
|
19
|
+
declare
|
|
20
|
+
role_oid oid;
|
|
21
|
+
stmt text;
|
|
22
|
+
begin
|
|
23
|
+
select oid into role_oid from pg_catalog.pg_roles where rolname = {{ROLE_LITERAL}};
|
|
24
|
+
if role_oid is null then
|
|
25
|
+
return; -- The monitoring role was already removed.
|
|
26
|
+
end if;
|
|
22
27
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
+
for pass in 1..2 loop
|
|
29
|
+
for stmt in
|
|
30
|
+
-- PG 16+ keeps one row for each grantor. A revoke without GRANTED BY
|
|
31
|
+
-- removes only the row of the current user. PG < 16 can keep the oid of
|
|
32
|
+
-- a dropped grantor; then the statement has no GRANTED BY.
|
|
33
|
+
select pg_catalog.format('revoke %s from %I', m.roleid::regrole, {{ROLE_LITERAL}})
|
|
34
|
+
|| coalesce(' granted by ' || pg_catalog.quote_ident(g.rolname), '')
|
|
35
|
+
from pg_catalog.pg_auth_members m
|
|
36
|
+
left join pg_catalog.pg_roles g on g.oid = m.grantor
|
|
37
|
+
where m.member = role_oid
|
|
38
|
+
and m.roleid in ('pg_monitor'::regrole, 'pg_read_all_stats'::regrole)
|
|
39
|
+
union
|
|
40
|
+
select pg_catalog.format('revoke select on pg_catalog.pg_index from %I', {{ROLE_LITERAL}})
|
|
41
|
+
from pg_catalog.pg_class c
|
|
42
|
+
where c.oid operator(pg_catalog.=) pg_catalog.to_regclass('pg_catalog.pg_index')::oid
|
|
43
|
+
and exists (
|
|
44
|
+
select from pg_catalog.aclexplode(c.relacl) a
|
|
45
|
+
where a.grantee = role_oid and a.privilege_type = 'SELECT')
|
|
46
|
+
union
|
|
47
|
+
select pg_catalog.format('revoke connect on database %I from %I', d.datname, {{ROLE_LITERAL}})
|
|
48
|
+
from pg_catalog.pg_database d
|
|
49
|
+
where d.datname = pg_catalog.current_database()
|
|
50
|
+
and exists (
|
|
51
|
+
select from pg_catalog.aclexplode(d.datacl) a
|
|
52
|
+
where a.grantee = role_oid and a.privilege_type = 'CONNECT')
|
|
53
|
+
union
|
|
54
|
+
select pg_catalog.format('revoke usage on schema postgres_ai from %I', {{ROLE_LITERAL}})
|
|
55
|
+
from pg_catalog.pg_namespace n
|
|
56
|
+
where n.nspname = 'postgres_ai'
|
|
57
|
+
and exists (
|
|
58
|
+
select from pg_catalog.aclexplode(n.nspacl) a
|
|
59
|
+
where a.grantee = role_oid and a.privilege_type = 'USAGE')
|
|
60
|
+
union
|
|
61
|
+
-- prepare-db grants USAGE when pg_stat_statements lives in a custom
|
|
62
|
+
-- extension schema. Retaining the login role must remove that grant too.
|
|
63
|
+
select pg_catalog.format('revoke usage on schema %I from %I', n.nspname, {{ROLE_LITERAL}})
|
|
64
|
+
from pg_catalog.pg_extension e
|
|
65
|
+
join pg_catalog.pg_namespace n on n.oid = e.extnamespace
|
|
66
|
+
where e.extname = 'pg_stat_statements'
|
|
67
|
+
and n.nspname not in ('pg_catalog', 'public')
|
|
68
|
+
and exists (
|
|
69
|
+
select from pg_catalog.aclexplode(n.nspacl) a
|
|
70
|
+
where a.grantee = role_oid and a.privilege_type = 'USAGE')
|
|
71
|
+
union
|
|
72
|
+
select pg_catalog.format('revoke execute on function %s from %I', p.oid::regprocedure, {{ROLE_LITERAL}})
|
|
73
|
+
from pg_catalog.pg_proc p
|
|
74
|
+
where p.oid in (
|
|
75
|
+
pg_catalog.to_regprocedure('rds_tools.pg_ls_multixactdir()'),
|
|
76
|
+
pg_catalog.to_regprocedure('pg_catalog.pg_stat_file(text)'),
|
|
77
|
+
pg_catalog.to_regprocedure('pg_catalog.pg_stat_file(text, boolean)'),
|
|
78
|
+
pg_catalog.to_regprocedure('pg_catalog.pg_ls_dir(text)'),
|
|
79
|
+
pg_catalog.to_regprocedure('pg_catalog.pg_ls_dir(text, boolean, boolean)'))
|
|
80
|
+
and exists (
|
|
81
|
+
select from pg_catalog.aclexplode(p.proacl) a
|
|
82
|
+
where a.grantee = role_oid and a.privilege_type = 'EXECUTE')
|
|
83
|
+
loop
|
|
84
|
+
if pass = 2 then
|
|
85
|
+
raise warning 'unprepare-db: grant left; a superuser can run: %', stmt;
|
|
86
|
+
else
|
|
87
|
+
begin
|
|
88
|
+
execute stmt;
|
|
89
|
+
exception when insufficient_privilege then
|
|
90
|
+
raise warning 'unprepare-db: % failed: %', stmt, sqlerrm;
|
|
91
|
+
end;
|
|
92
|
+
end if;
|
|
93
|
+
end loop;
|
|
94
|
+
end loop;
|
|
95
|
+
end {{DO_QUOTE}};
|
|
28
96
|
|
|
29
97
|
-- Note: USAGE on public is typically granted by default; we don't revoke it
|
|
30
98
|
-- to avoid breaking other applications that may rely on it.
|
package/sql/uninit/03.role.sql
CHANGED
|
@@ -1,27 +1,31 @@
|
|
|
1
1
|
-- Drop the monitoring role created by prepare-db (template-filled by cli/lib/init.ts)
|
|
2
2
|
-- This must run after revoking all permissions from the role.
|
|
3
3
|
|
|
4
|
+
-- Objects in other schemas (for example, a planted public.format) must not run as the admin.
|
|
5
|
+
-- Printed plans may run in psql autocommit, where SET LOCAL is ignored.
|
|
6
|
+
set search_path = pg_catalog, pg_temp;
|
|
7
|
+
|
|
4
8
|
-- Use a DO block to handle the case where the role doesn't exist
|
|
5
|
-
do
|
|
9
|
+
do {{DO_QUOTE}} begin
|
|
6
10
|
-- Reassign owned objects to current user before dropping
|
|
7
11
|
-- This handles any objects that might have been created by the role
|
|
8
12
|
begin
|
|
9
|
-
execute format('reassign owned by %I to current_user', {{ROLE_LITERAL}});
|
|
13
|
+
execute pg_catalog.format('reassign owned by %I to current_user', {{ROLE_LITERAL}});
|
|
10
14
|
exception when undefined_object then
|
|
11
15
|
null; -- Role doesn't exist, nothing to reassign
|
|
12
16
|
end;
|
|
13
17
|
|
|
14
18
|
-- Drop owned objects (in case reassign didn't work for some objects)
|
|
15
19
|
begin
|
|
16
|
-
execute format('drop owned by %I', {{ROLE_LITERAL}});
|
|
20
|
+
execute pg_catalog.format('drop owned by %I', {{ROLE_LITERAL}});
|
|
17
21
|
exception when undefined_object then
|
|
18
22
|
null; -- Role doesn't exist
|
|
19
23
|
end;
|
|
20
24
|
|
|
21
25
|
-- Drop the role
|
|
22
26
|
begin
|
|
23
|
-
execute format('drop role %I', {{ROLE_LITERAL}});
|
|
27
|
+
execute pg_catalog.format('drop role %I', {{ROLE_LITERAL}});
|
|
24
28
|
exception when undefined_object then
|
|
25
29
|
null; -- Role doesn't exist, that's fine
|
|
26
30
|
end;
|
|
27
|
-
end
|
|
31
|
+
end {{DO_QUOTE}};
|
package/dist/sql/sql/01.role.sql
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
-- Role creation / password update (template-filled by cli/lib/init.ts)
|
|
2
|
-
--
|
|
3
|
-
-- Always uses a race-safe pattern (create if missing, then always alter to set the password):
|
|
4
|
-
-- do $$ begin
|
|
5
|
-
-- if not exists (select 1 from pg_catalog.pg_roles where rolname = '...') then
|
|
6
|
-
-- begin
|
|
7
|
-
-- create user "..." with password '...';
|
|
8
|
-
-- exception when duplicate_object then
|
|
9
|
-
-- null;
|
|
10
|
-
-- end;
|
|
11
|
-
-- end if;
|
|
12
|
-
-- alter user "..." with password '...';
|
|
13
|
-
-- end $$;
|
|
14
|
-
{{ROLE_STMT}}
|
|
15
|
-
|
|
16
|
-
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
-- Extensions required for postgres_ai monitoring
|
|
2
|
-
|
|
3
|
-
-- Enable pg_stat_statements for query performance monitoring
|
|
4
|
-
-- Note: Uses IF NOT EXISTS because extension may already be installed.
|
|
5
|
-
-- We do NOT drop this extension in unprepare-db since it may have been pre-existing.
|
|
6
|
-
create extension if not exists pg_stat_statements;
|
|
7
|
-
|
|
8
|
-
|
|
@@ -1,86 +0,0 @@
|
|
|
1
|
-
-- Required permissions for postgres_ai monitoring user (template-filled by cli/lib/init.ts)
|
|
2
|
-
|
|
3
|
-
-- Allow connect
|
|
4
|
-
grant connect on database {{DB_IDENT}} to {{ROLE_IDENT}};
|
|
5
|
-
|
|
6
|
-
-- Standard monitoring privileges
|
|
7
|
-
grant pg_monitor to {{ROLE_IDENT}};
|
|
8
|
-
grant select on pg_catalog.pg_index to {{ROLE_IDENT}};
|
|
9
|
-
|
|
10
|
-
-- Required for cluster-wide pg_stat_statements collection.
|
|
11
|
-
-- pg_stat_statements is backed by shared memory and holds entries for every database
|
|
12
|
-
-- in the cluster, but a role without pg_read_all_stats sees other users' rows with
|
|
13
|
-
-- queryid and query text redacted to NULL -- which collapses the per-queryid grouping
|
|
14
|
-
-- in the pg_stat_statements metric. pg_monitor already implies pg_read_all_stats;
|
|
15
|
-
-- granting it explicitly documents the dependency and keeps the metric working if the
|
|
16
|
-
-- pg_monitor grant above is ever narrowed. Idempotent.
|
|
17
|
-
grant pg_read_all_stats to {{ROLE_IDENT}};
|
|
18
|
-
|
|
19
|
-
-- Create postgres_ai schema for our objects
|
|
20
|
-
-- Using IF NOT EXISTS for idempotency - prepare-db can be run multiple times
|
|
21
|
-
create schema if not exists postgres_ai;
|
|
22
|
-
grant usage on schema postgres_ai to {{ROLE_IDENT}};
|
|
23
|
-
|
|
24
|
-
-- For bloat analysis: expose pg_statistic via a view
|
|
25
|
-
create or replace view postgres_ai.pg_statistic as
|
|
26
|
-
select
|
|
27
|
-
n.nspname as schemaname,
|
|
28
|
-
c.relname as tablename,
|
|
29
|
-
a.attname,
|
|
30
|
-
s.stanullfrac as null_frac,
|
|
31
|
-
s.stawidth as avg_width,
|
|
32
|
-
false as inherited
|
|
33
|
-
from pg_catalog.pg_statistic s
|
|
34
|
-
join pg_catalog.pg_class c on c.oid = s.starelid
|
|
35
|
-
join pg_catalog.pg_namespace n on n.oid = c.relnamespace
|
|
36
|
-
join pg_catalog.pg_attribute a on a.attrelid = s.starelid and a.attnum = s.staattnum
|
|
37
|
-
where a.attnum > 0 and not a.attisdropped;
|
|
38
|
-
|
|
39
|
-
grant select on postgres_ai.pg_statistic to {{ROLE_IDENT}};
|
|
40
|
-
|
|
41
|
-
-- Hardened clusters sometimes revoke PUBLIC on schema public
|
|
42
|
-
grant usage on schema public to {{ROLE_IDENT}};
|
|
43
|
-
|
|
44
|
-
-- Grant access to the schema where pg_stat_statements is installed.
|
|
45
|
-
-- Some providers (e.g., Supabase) install extensions in a separate 'extensions' schema
|
|
46
|
-
-- rather than pg_catalog. This DO block detects the schema and grants USAGE if needed.
|
|
47
|
-
do $$
|
|
48
|
-
declare
|
|
49
|
-
ext_schema text;
|
|
50
|
-
begin
|
|
51
|
-
select n.nspname into ext_schema
|
|
52
|
-
from pg_extension e
|
|
53
|
-
join pg_namespace n on e.extnamespace = n.oid
|
|
54
|
-
where e.extname = 'pg_stat_statements';
|
|
55
|
-
|
|
56
|
-
-- Only grant if extension exists and is in a non-standard schema
|
|
57
|
-
if ext_schema is not null and ext_schema not in ('pg_catalog', 'public') then
|
|
58
|
-
execute format('grant usage on schema %I to {{ROLE_IDENT}}', ext_schema);
|
|
59
|
-
end if;
|
|
60
|
-
end $$;
|
|
61
|
-
|
|
62
|
-
-- [SEARCH_PATH_BLOCK_START] Keep search_path predictable; postgres_ai first so our objects are found.
|
|
63
|
-
-- Dynamically include the pg_stat_statements extension schema if it's in a non-standard location.
|
|
64
|
-
do $$
|
|
65
|
-
declare
|
|
66
|
-
ext_schema text;
|
|
67
|
-
sp text;
|
|
68
|
-
begin
|
|
69
|
-
-- Detect pg_stat_statements extension schema
|
|
70
|
-
select n.nspname into ext_schema
|
|
71
|
-
from pg_extension e
|
|
72
|
-
join pg_namespace n on e.extnamespace = n.oid
|
|
73
|
-
where e.extname = 'pg_stat_statements';
|
|
74
|
-
|
|
75
|
-
-- Build search_path: include extension schema if in non-standard location
|
|
76
|
-
if ext_schema is not null and ext_schema not in ('pg_catalog', 'public') then
|
|
77
|
-
sp := 'postgres_ai, ' || quote_ident(ext_schema) || ', "$user", public, pg_catalog';
|
|
78
|
-
else
|
|
79
|
-
sp := 'postgres_ai, "$user", public, pg_catalog';
|
|
80
|
-
end if;
|
|
81
|
-
|
|
82
|
-
execute format('alter user {{ROLE_IDENT}} set search_path = %s', sp);
|
|
83
|
-
end $$;
|
|
84
|
-
-- [SEARCH_PATH_BLOCK_END]
|
|
85
|
-
|
|
86
|
-
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
-- Optional permissions for self-managed Postgres (best effort)
|
|
2
|
-
|
|
3
|
-
grant execute on function pg_catalog.pg_stat_file(text) to {{ROLE_IDENT}};
|
|
4
|
-
grant execute on function pg_catalog.pg_stat_file(text, boolean) to {{ROLE_IDENT}};
|
|
5
|
-
grant execute on function pg_catalog.pg_ls_dir(text) to {{ROLE_IDENT}};
|
|
6
|
-
grant execute on function pg_catalog.pg_ls_dir(text, boolean, boolean) to {{ROLE_IDENT}};
|
|
7
|
-
|
|
8
|
-
|