Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 352ea3a

Browse files
committed
Add OAT hook calls for more subcommands of ALTER TABLE
The OAT hooks are added in ALTER TABLE for the following subcommands: - { ENABLE | DISABLE | [NO] FORCE } ROW LEVEL SECURITY - { ENABLE | DISABLE } TRIGGER - { ENABLE | DISABLE } RULE. Note that there was hook for pg_rewrite, but not for relation ALTER'ed in pg_class. Tests are added to test_oat_hook for all the subcommand patterns gaining hooks here. Based on an ask from Legs Mansion. Discussion: https://postgr.es/m/tencent_083B3850655AC6EE04FA0A400766D3FE8309@qq.com
1 parent dca2001 commit 352ea3a

File tree

5 files changed

+225
-1
lines changed

5 files changed

+225
-1
lines changed

src/backend/commands/tablecmds.c

+12
Original file line numberDiff line numberDiff line change
@@ -14843,6 +14843,9 @@ ATExecEnableDisableTrigger(Relation rel, const char *trigname,
1484314843
EnableDisableTrigger(rel, trigname, InvalidOid,
1484414844
fires_when, skip_system, recurse,
1484514845
lockmode);
14846+
14847+
InvokeObjectPostAlterHook(RelationRelationId,
14848+
RelationGetRelid(rel), 0);
1484614849
}
1484714850

1484814851
/*
@@ -14855,6 +14858,9 @@ ATExecEnableDisableRule(Relation rel, const char *rulename,
1485514858
char fires_when, LOCKMODE lockmode)
1485614859
{
1485714860
EnableDisableRule(rel, rulename, fires_when);
14861+
14862+
InvokeObjectPostAlterHook(RelationRelationId,
14863+
RelationGetRelid(rel), 0);
1485814864
}
1485914865

1486014866
/*
@@ -16134,6 +16140,9 @@ ATExecSetRowSecurity(Relation rel, bool rls)
1613416140
((Form_pg_class) GETSTRUCT(tuple))->relrowsecurity = rls;
1613516141
CatalogTupleUpdate(pg_class, &tuple->t_self, tuple);
1613616142

16143+
InvokeObjectPostAlterHook(RelationRelationId,
16144+
RelationGetRelid(rel), 0);
16145+
1613716146
table_close(pg_class, RowExclusiveLock);
1613816147
heap_freetuple(tuple);
1613916148
}
@@ -16160,6 +16169,9 @@ ATExecForceNoForceRowSecurity(Relation rel, bool force_rls)
1616016169
((Form_pg_class) GETSTRUCT(tuple))->relforcerowsecurity = force_rls;
1616116170
CatalogTupleUpdate(pg_class, &tuple->t_self, tuple);
1616216171

16172+
InvokeObjectPostAlterHook(RelationRelationId,
16173+
RelationGetRelid(rel), 0);
16174+
1616316175
table_close(pg_class, RowExclusiveLock);
1616416176
heap_freetuple(tuple);
1616516177
}

src/test/modules/test_oat_hooks/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ OBJS = \
66
test_oat_hooks.o
77
PGFILEDESC = "test_oat_hooks - example use of object access hooks"
88

9-
REGRESS = test_oat_hooks
9+
REGRESS = test_oat_hooks alter_table
1010

1111
# disable installcheck for now
1212
NO_INSTALLCHECK = 1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
--
2+
-- OAT checks for ALTER TABLE
3+
--
4+
-- This test script fails if debug_discard_caches is enabled, because cache
5+
-- flushes cause extra calls of the OAT hook in recomputeNamespacePath,
6+
-- resulting in more NOTICE messages than are in the expected output.
7+
SET debug_discard_caches = 0;
8+
LOAD 'test_oat_hooks';
9+
SET test_oat_hooks.audit = true;
10+
NOTICE: in object_access_hook_str: superuser attempting alter (subId=0x1000, set) [test_oat_hooks.audit]
11+
NOTICE: in object_access_hook_str: superuser finished alter (subId=0x1000, set) [test_oat_hooks.audit]
12+
NOTICE: in process utility: superuser finished SET
13+
CREATE SCHEMA test_oat_schema;
14+
NOTICE: in process utility: superuser attempting CREATE SCHEMA
15+
NOTICE: in object access: superuser attempting create (subId=0x0) [explicit]
16+
NOTICE: in object access: superuser finished create (subId=0x0) [explicit]
17+
NOTICE: in process utility: superuser finished CREATE SCHEMA
18+
CREATE TABLE test_oat_schema.test_oat_tab (c1 int, c2 text);
19+
NOTICE: in process utility: superuser attempting CREATE TABLE
20+
NOTICE: in object access: superuser attempting namespace search (subId=0x0) [report on violation, allowed]
21+
LINE 1: CREATE TABLE test_oat_schema.test_oat_tab (c1 int, c2 text);
22+
^
23+
NOTICE: in object access: superuser finished namespace search (subId=0x0) [report on violation, allowed]
24+
LINE 1: CREATE TABLE test_oat_schema.test_oat_tab (c1 int, c2 text);
25+
^
26+
NOTICE: in object access: superuser attempting namespace search (subId=0x0) [no report on violation, allowed]
27+
NOTICE: in object access: superuser finished namespace search (subId=0x0) [no report on violation, allowed]
28+
NOTICE: in object access: superuser attempting namespace search (subId=0x0) [report on violation, allowed]
29+
NOTICE: in object access: superuser finished namespace search (subId=0x0) [report on violation, allowed]
30+
NOTICE: in object access: superuser attempting create (subId=0x0) [explicit]
31+
NOTICE: in object access: superuser finished create (subId=0x0) [explicit]
32+
NOTICE: in object access: superuser attempting create (subId=0x0) [explicit]
33+
NOTICE: in object access: superuser finished create (subId=0x0) [explicit]
34+
NOTICE: in object access: superuser attempting create (subId=0x0) [explicit]
35+
NOTICE: in object access: superuser finished create (subId=0x0) [explicit]
36+
NOTICE: in object access: superuser attempting create (subId=0x0) [internal]
37+
NOTICE: in object access: superuser finished create (subId=0x0) [internal]
38+
NOTICE: in object access: superuser attempting create (subId=0x0) [internal]
39+
NOTICE: in object access: superuser finished create (subId=0x0) [internal]
40+
NOTICE: in process utility: superuser finished CREATE TABLE
41+
CREATE RULE test_oat_notify AS
42+
ON UPDATE TO test_oat_schema.test_oat_tab
43+
DO ALSO NOTIFY test_oat_tab;
44+
NOTICE: in process utility: superuser attempting CREATE RULE
45+
NOTICE: in object access: superuser attempting namespace search (subId=0x0) [report on violation, allowed]
46+
NOTICE: in object access: superuser finished namespace search (subId=0x0) [report on violation, allowed]
47+
NOTICE: in object access: superuser attempting namespace search (subId=0x0) [report on violation, allowed]
48+
NOTICE: in object access: superuser finished namespace search (subId=0x0) [report on violation, allowed]
49+
NOTICE: in object access: superuser attempting create (subId=0x0) [explicit]
50+
NOTICE: in object access: superuser finished create (subId=0x0) [explicit]
51+
NOTICE: in process utility: superuser finished CREATE RULE
52+
CREATE FUNCTION test_oat_schema.test_trigger()
53+
RETURNS trigger
54+
LANGUAGE plpgsql
55+
AS $$
56+
BEGIN
57+
IF TG_OP = 'DELETE'
58+
THEN
59+
RETURN OLD;
60+
ELSE
61+
RETURN NEW;
62+
END IF;
63+
END; $$;
64+
NOTICE: in process utility: superuser attempting CREATE FUNCTION
65+
NOTICE: in object access: superuser attempting create (subId=0x0) [explicit]
66+
NOTICE: in object access: superuser finished create (subId=0x0) [explicit]
67+
NOTICE: in process utility: superuser finished CREATE FUNCTION
68+
CREATE TRIGGER test_oat_trigger BEFORE INSERT ON test_oat_schema.test_oat_tab
69+
FOR EACH STATEMENT EXECUTE FUNCTION test_oat_schema.test_trigger();
70+
NOTICE: in process utility: superuser attempting CREATE TRIGGER
71+
NOTICE: in object access: superuser attempting namespace search (subId=0x0) [report on violation, allowed]
72+
NOTICE: in object access: superuser finished namespace search (subId=0x0) [report on violation, allowed]
73+
NOTICE: in object access: superuser attempting namespace search (subId=0x0) [report on violation, allowed]
74+
NOTICE: in object access: superuser finished namespace search (subId=0x0) [report on violation, allowed]
75+
NOTICE: in object access: superuser attempting create (subId=0x0) [explicit]
76+
NOTICE: in object access: superuser finished create (subId=0x0) [explicit]
77+
NOTICE: in process utility: superuser finished CREATE TRIGGER
78+
-- RLS
79+
ALTER TABLE test_oat_schema.test_oat_tab ENABLE ROW LEVEL SECURITY;
80+
NOTICE: in process utility: superuser attempting ALTER TABLE
81+
NOTICE: in object access: superuser attempting namespace search (subId=0x0) [report on violation, allowed]
82+
NOTICE: in object access: superuser finished namespace search (subId=0x0) [report on violation, allowed]
83+
NOTICE: in object access: superuser attempting alter (subId=0x0) [explicit without auxiliary object]
84+
NOTICE: in object access: superuser finished alter (subId=0x0) [explicit without auxiliary object]
85+
NOTICE: in process utility: superuser finished ALTER TABLE
86+
ALTER TABLE test_oat_schema.test_oat_tab DISABLE ROW LEVEL SECURITY;
87+
NOTICE: in process utility: superuser attempting ALTER TABLE
88+
NOTICE: in object access: superuser attempting namespace search (subId=0x0) [report on violation, allowed]
89+
NOTICE: in object access: superuser finished namespace search (subId=0x0) [report on violation, allowed]
90+
NOTICE: in object access: superuser attempting alter (subId=0x0) [explicit without auxiliary object]
91+
NOTICE: in object access: superuser finished alter (subId=0x0) [explicit without auxiliary object]
92+
NOTICE: in process utility: superuser finished ALTER TABLE
93+
ALTER TABLE test_oat_schema.test_oat_tab FORCE ROW LEVEL SECURITY;
94+
NOTICE: in process utility: superuser attempting ALTER TABLE
95+
NOTICE: in object access: superuser attempting namespace search (subId=0x0) [report on violation, allowed]
96+
NOTICE: in object access: superuser finished namespace search (subId=0x0) [report on violation, allowed]
97+
NOTICE: in object access: superuser attempting alter (subId=0x0) [explicit without auxiliary object]
98+
NOTICE: in object access: superuser finished alter (subId=0x0) [explicit without auxiliary object]
99+
NOTICE: in process utility: superuser finished ALTER TABLE
100+
ALTER TABLE test_oat_schema.test_oat_tab NO FORCE ROW LEVEL SECURITY;
101+
NOTICE: in process utility: superuser attempting ALTER TABLE
102+
NOTICE: in object access: superuser attempting namespace search (subId=0x0) [report on violation, allowed]
103+
NOTICE: in object access: superuser finished namespace search (subId=0x0) [report on violation, allowed]
104+
NOTICE: in object access: superuser attempting alter (subId=0x0) [explicit without auxiliary object]
105+
NOTICE: in object access: superuser finished alter (subId=0x0) [explicit without auxiliary object]
106+
NOTICE: in process utility: superuser finished ALTER TABLE
107+
-- Rules
108+
ALTER TABLE test_oat_schema.test_oat_tab DISABLE RULE test_oat_notify;
109+
NOTICE: in process utility: superuser attempting ALTER TABLE
110+
NOTICE: in object access: superuser attempting namespace search (subId=0x0) [report on violation, allowed]
111+
NOTICE: in object access: superuser finished namespace search (subId=0x0) [report on violation, allowed]
112+
NOTICE: in object access: superuser attempting alter (subId=0x0) [explicit without auxiliary object]
113+
NOTICE: in object access: superuser finished alter (subId=0x0) [explicit without auxiliary object]
114+
NOTICE: in object access: superuser attempting alter (subId=0x0) [explicit without auxiliary object]
115+
NOTICE: in object access: superuser finished alter (subId=0x0) [explicit without auxiliary object]
116+
NOTICE: in process utility: superuser finished ALTER TABLE
117+
ALTER TABLE test_oat_schema.test_oat_tab ENABLE RULE test_oat_notify;
118+
NOTICE: in process utility: superuser attempting ALTER TABLE
119+
NOTICE: in object access: superuser attempting namespace search (subId=0x0) [report on violation, allowed]
120+
NOTICE: in object access: superuser finished namespace search (subId=0x0) [report on violation, allowed]
121+
NOTICE: in object access: superuser attempting alter (subId=0x0) [explicit without auxiliary object]
122+
NOTICE: in object access: superuser finished alter (subId=0x0) [explicit without auxiliary object]
123+
NOTICE: in object access: superuser attempting alter (subId=0x0) [explicit without auxiliary object]
124+
NOTICE: in object access: superuser finished alter (subId=0x0) [explicit without auxiliary object]
125+
NOTICE: in process utility: superuser finished ALTER TABLE
126+
-- Triggers
127+
ALTER TABLE test_oat_schema.test_oat_tab DISABLE TRIGGER test_oat_trigger;
128+
NOTICE: in process utility: superuser attempting ALTER TABLE
129+
NOTICE: in object access: superuser attempting namespace search (subId=0x0) [report on violation, allowed]
130+
NOTICE: in object access: superuser finished namespace search (subId=0x0) [report on violation, allowed]
131+
NOTICE: in object access: superuser attempting alter (subId=0x0) [explicit without auxiliary object]
132+
NOTICE: in object access: superuser finished alter (subId=0x0) [explicit without auxiliary object]
133+
NOTICE: in object access: superuser attempting alter (subId=0x0) [explicit without auxiliary object]
134+
NOTICE: in object access: superuser finished alter (subId=0x0) [explicit without auxiliary object]
135+
NOTICE: in process utility: superuser finished ALTER TABLE
136+
ALTER TABLE test_oat_schema.test_oat_tab ENABLE TRIGGER test_oat_trigger;
137+
NOTICE: in process utility: superuser attempting ALTER TABLE
138+
NOTICE: in object access: superuser attempting namespace search (subId=0x0) [report on violation, allowed]
139+
NOTICE: in object access: superuser finished namespace search (subId=0x0) [report on violation, allowed]
140+
NOTICE: in object access: superuser attempting alter (subId=0x0) [explicit without auxiliary object]
141+
NOTICE: in object access: superuser finished alter (subId=0x0) [explicit without auxiliary object]
142+
NOTICE: in object access: superuser attempting alter (subId=0x0) [explicit without auxiliary object]
143+
NOTICE: in object access: superuser finished alter (subId=0x0) [explicit without auxiliary object]
144+
NOTICE: in process utility: superuser finished ALTER TABLE
145+
DROP TABLE test_oat_schema.test_oat_tab;
146+
NOTICE: in process utility: superuser attempting DROP TABLE
147+
NOTICE: in object access: superuser attempting namespace search (subId=0x0) [report on violation, allowed]
148+
NOTICE: in object access: superuser finished namespace search (subId=0x0) [report on violation, allowed]
149+
NOTICE: in object access: superuser attempting drop (subId=0x0) []
150+
NOTICE: in object access: superuser finished drop (subId=0x0) []
151+
NOTICE: in object access: superuser attempting drop (subId=0x0) []
152+
NOTICE: in object access: superuser finished drop (subId=0x0) []
153+
NOTICE: in object access: superuser attempting drop (subId=0x0) []
154+
NOTICE: in object access: superuser finished drop (subId=0x0) []
155+
NOTICE: in object access: superuser attempting drop (subId=0x0) []
156+
NOTICE: in object access: superuser finished drop (subId=0x0) []
157+
NOTICE: in object access: superuser attempting drop (subId=0x0) []
158+
NOTICE: in object access: superuser finished drop (subId=0x0) []
159+
NOTICE: in object access: superuser attempting drop (subId=0x0) []
160+
NOTICE: in object access: superuser finished drop (subId=0x0) []
161+
NOTICE: in object access: superuser attempting drop (subId=0x0) []
162+
NOTICE: in object access: superuser finished drop (subId=0x0) []
163+
NOTICE: in process utility: superuser finished DROP TABLE

src/test/modules/test_oat_hooks/meson.build

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ tests += {
2323
'regress': {
2424
'sql': [
2525
'test_oat_hooks',
26+
'alter_table',
2627
],
2728
'regress_args': ['--no-locale', '--encoding=UTF8'],
2829
'runningcheck': false,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
--
2+
-- OAT checks for ALTER TABLE
3+
--
4+
5+
-- This test script fails if debug_discard_caches is enabled, because cache
6+
-- flushes cause extra calls of the OAT hook in recomputeNamespacePath,
7+
-- resulting in more NOTICE messages than are in the expected output.
8+
SET debug_discard_caches = 0;
9+
10+
LOAD 'test_oat_hooks';
11+
SET test_oat_hooks.audit = true;
12+
13+
CREATE SCHEMA test_oat_schema;
14+
CREATE TABLE test_oat_schema.test_oat_tab (c1 int, c2 text);
15+
CREATE RULE test_oat_notify AS
16+
ON UPDATE TO test_oat_schema.test_oat_tab
17+
DO ALSO NOTIFY test_oat_tab;
18+
19+
CREATE FUNCTION test_oat_schema.test_trigger()
20+
RETURNS trigger
21+
LANGUAGE plpgsql
22+
AS $$
23+
BEGIN
24+
IF TG_OP = 'DELETE'
25+
THEN
26+
RETURN OLD;
27+
ELSE
28+
RETURN NEW;
29+
END IF;
30+
END; $$;
31+
CREATE TRIGGER test_oat_trigger BEFORE INSERT ON test_oat_schema.test_oat_tab
32+
FOR EACH STATEMENT EXECUTE FUNCTION test_oat_schema.test_trigger();
33+
34+
-- RLS
35+
ALTER TABLE test_oat_schema.test_oat_tab ENABLE ROW LEVEL SECURITY;
36+
ALTER TABLE test_oat_schema.test_oat_tab DISABLE ROW LEVEL SECURITY;
37+
ALTER TABLE test_oat_schema.test_oat_tab FORCE ROW LEVEL SECURITY;
38+
ALTER TABLE test_oat_schema.test_oat_tab NO FORCE ROW LEVEL SECURITY;
39+
40+
-- Rules
41+
ALTER TABLE test_oat_schema.test_oat_tab DISABLE RULE test_oat_notify;
42+
ALTER TABLE test_oat_schema.test_oat_tab ENABLE RULE test_oat_notify;
43+
44+
-- Triggers
45+
ALTER TABLE test_oat_schema.test_oat_tab DISABLE TRIGGER test_oat_trigger;
46+
ALTER TABLE test_oat_schema.test_oat_tab ENABLE TRIGGER test_oat_trigger;
47+
48+
DROP TABLE test_oat_schema.test_oat_tab;

0 commit comments

Comments
 (0)