File: acl.out

package info (click to toggle)
postgresql-periods 1.2.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 1,084 kB
  • sloc: sql: 9,744; ansic: 548; makefile: 30; sh: 1
file content (672 lines) | stat: -rw-r--r-- 41,643 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
SELECT CASE
    WHEN setting::integer >= 170000 THEN '17 ..'::text
    WHEN setting::integer >= 110000 THEN '11 .. 16'
    WHEN setting::integer >= 90600 THEN '9.6 .. 10'
    ELSE '.. 9.5' END
FROM pg_settings WHERE name = 'server_version_num';
   case   
----------
 11 .. 16
(1 row)

/* Tests for access control on the history tables */
CREATE ROLE periods_acl_1;
CREATE ROLE periods_acl_2;
CREATE ROLE periods_acl_3;
/* OWNER */
-- We call this query several times, so make it a view for eaiser maintenance
CREATE VIEW show_owners AS
    SELECT c.relnamespace::regnamespace AS schema_name,
           c.relname AS object_name,
           CASE c.relkind
               WHEN 'r' THEN 'table'
               WHEN 'v' THEN 'view'
           END AS object_type,
           c.relowner::regrole AS owner
    FROM pg_class AS c
    WHERE c.relnamespace = 'public'::regnamespace
      AND c.relname = ANY (ARRAY['owner_test', 'owner_test_history', 'owner_test_with_history', 'owner_test__for_portion_of_p'])
    UNION ALL
    SELECT p.pronamespace, p.proname, 'function', p.proowner
    FROM pg_proc AS p
    WHERE p.pronamespace = 'public'::regnamespace
      AND p.proname = ANY (ARRAY['owner_test__as_of', 'owner_test__between', 'owner_test__between_symmetric', 'owner_test__from_to']);
CREATE TABLE owner_test (col text PRIMARY KEY, s integer, e integer);
ALTER TABLE owner_test OWNER TO periods_acl_1;
SELECT periods.add_period('owner_test', 'p', 's', 'e');
 add_period 
------------
 t
(1 row)

SELECT periods.add_for_portion_view('owner_test', 'p');
 add_for_portion_view 
----------------------
 t
(1 row)

SELECT periods.add_system_time_period('owner_test');
 add_system_time_period 
------------------------
 t
(1 row)

SELECT periods.add_system_versioning('owner_test');
NOTICE:  history table "owner_test_history" created for "owner_test", be sure to index it properly
 add_system_versioning 
-----------------------
 
(1 row)

TABLE show_owners ORDER BY object_name;
 schema_name |          object_name          | object_type |     owner     
-------------+-------------------------------+-------------+---------------
 public      | owner_test                    | table       | periods_acl_1
 public      | owner_test__as_of             | function    | periods_acl_1
 public      | owner_test__between           | function    | periods_acl_1
 public      | owner_test__between_symmetric | function    | periods_acl_1
 public      | owner_test__for_portion_of_p  | view        | periods_acl_1
 public      | owner_test__from_to           | function    | periods_acl_1
 public      | owner_test_history            | table       | periods_acl_1
 public      | owner_test_with_history       | view        | periods_acl_1
(8 rows)

-- This should change everything
ALTER TABLE owner_test OWNER TO periods_acl_2;
TABLE show_owners ORDER BY object_name;
 schema_name |          object_name          | object_type |     owner     
-------------+-------------------------------+-------------+---------------
 public      | owner_test                    | table       | periods_acl_2
 public      | owner_test__as_of             | function    | periods_acl_2
 public      | owner_test__between           | function    | periods_acl_2
 public      | owner_test__between_symmetric | function    | periods_acl_2
 public      | owner_test__for_portion_of_p  | view        | periods_acl_2
 public      | owner_test__from_to           | function    | periods_acl_2
 public      | owner_test_history            | table       | periods_acl_2
 public      | owner_test_with_history       | view        | periods_acl_2
(8 rows)

-- These should change nothing
ALTER TABLE owner_test_history OWNER TO periods_acl_3;
ALTER VIEW owner_test_with_history OWNER TO periods_acl_3;
ALTER FUNCTION owner_test__as_of(timestamp with time zone) OWNER TO periods_acl_3;
ALTER FUNCTION owner_test__between(timestamp with time zone, timestamp with time zone) OWNER TO periods_acl_3;
ALTER FUNCTION owner_test__between_symmetric(timestamp with time zone, timestamp with time zone) OWNER TO periods_acl_3;
ALTER FUNCTION owner_test__from_to(timestamp with time zone, timestamp with time zone) OWNER TO periods_acl_3;
TABLE show_owners ORDER BY object_name;
 schema_name |          object_name          | object_type |     owner     
-------------+-------------------------------+-------------+---------------
 public      | owner_test                    | table       | periods_acl_2
 public      | owner_test__as_of             | function    | periods_acl_2
 public      | owner_test__between           | function    | periods_acl_2
 public      | owner_test__between_symmetric | function    | periods_acl_2
 public      | owner_test__for_portion_of_p  | view        | periods_acl_2
 public      | owner_test__from_to           | function    | periods_acl_2
 public      | owner_test_history            | table       | periods_acl_2
 public      | owner_test_with_history       | view        | periods_acl_2
(8 rows)

-- This should put the owner back to the base table's owner
SELECT periods.drop_system_versioning('owner_test');
 drop_system_versioning 
------------------------
 t
(1 row)

ALTER TABLE owner_test_history OWNER TO periods_acl_3;
TABLE show_owners ORDER BY object_name;
 schema_name |         object_name          | object_type |     owner     
-------------+------------------------------+-------------+---------------
 public      | owner_test                   | table       | periods_acl_2
 public      | owner_test__for_portion_of_p | view        | periods_acl_2
 public      | owner_test_history           | table       | periods_acl_3
(3 rows)

SELECT periods.add_system_versioning('owner_test');
 add_system_versioning 
-----------------------
 
(1 row)

TABLE show_owners ORDER BY object_name;
 schema_name |          object_name          | object_type |     owner     
-------------+-------------------------------+-------------+---------------
 public      | owner_test                    | table       | periods_acl_2
 public      | owner_test__as_of             | function    | periods_acl_2
 public      | owner_test__between           | function    | periods_acl_2
 public      | owner_test__between_symmetric | function    | periods_acl_2
 public      | owner_test__for_portion_of_p  | view        | periods_acl_2
 public      | owner_test__from_to           | function    | periods_acl_2
 public      | owner_test_history            | table       | periods_acl_2
 public      | owner_test_with_history       | view        | periods_acl_2
(8 rows)

SELECT periods.drop_system_versioning('owner_test', drop_behavior => 'CASCADE', purge => true);
 drop_system_versioning 
------------------------
 t
(1 row)

SELECT periods.drop_for_portion_view('owner_test', NULL);
 drop_for_portion_view 
-----------------------
 t
(1 row)

DROP TABLE owner_test CASCADE;
DROP VIEW show_owners;
/* FOR PORTION OF ACL */
-- We call this query several times, so make it a view for eaiser maintenance
CREATE VIEW show_acls AS
    SELECT row_number() OVER (ORDER BY array_position(ARRAY['table', 'view', 'function'], object_type),
                                       schema_name, object_name, grantee, privilege_type) AS sort_order,
           *
    FROM (
        SELECT c.relnamespace::regnamespace AS schema_name,
               c.relname AS object_name,
               CASE c.relkind
                   WHEN 'r' THEN 'table'
                   WHEN 'v' THEN 'view'
               END AS object_type,
               acl.grantee::regrole::text AS grantee,
               acl.privilege_type
        FROM pg_class AS c
        CROSS JOIN LATERAL aclexplode(COALESCE(c.relacl, acldefault('r', c.relowner))) AS acl
        WHERE c.relname IN ('fpacl', 'fpacl__for_portion_of_p')
    ) AS _;
CREATE TABLE fpacl (col text PRIMARY KEY, s integer, e integer);
ALTER TABLE fpacl OWNER TO periods_acl_1;
SELECT periods.add_period('fpacl', 'p', 's', 'e');
 add_period 
------------
 t
(1 row)

SELECT periods.add_for_portion_view('fpacl', 'p');
 add_for_portion_view 
----------------------
 t
(1 row)

TABLE show_acls ORDER BY sort_order;
 sort_order | schema_name |       object_name       | object_type |    grantee    | privilege_type 
------------+-------------+-------------------------+-------------+---------------+----------------
          1 | public      | fpacl                   | table       | periods_acl_1 | DELETE
          2 | public      | fpacl                   | table       | periods_acl_1 | INSERT
          3 | public      | fpacl                   | table       | periods_acl_1 | REFERENCES
          4 | public      | fpacl                   | table       | periods_acl_1 | SELECT
          5 | public      | fpacl                   | table       | periods_acl_1 | TRIGGER
          6 | public      | fpacl                   | table       | periods_acl_1 | TRUNCATE
          7 | public      | fpacl                   | table       | periods_acl_1 | UPDATE
          8 | public      | fpacl__for_portion_of_p | view        | periods_acl_1 | DELETE
          9 | public      | fpacl__for_portion_of_p | view        | periods_acl_1 | INSERT
         10 | public      | fpacl__for_portion_of_p | view        | periods_acl_1 | REFERENCES
         11 | public      | fpacl__for_portion_of_p | view        | periods_acl_1 | SELECT
         12 | public      | fpacl__for_portion_of_p | view        | periods_acl_1 | TRIGGER
         13 | public      | fpacl__for_portion_of_p | view        | periods_acl_1 | TRUNCATE
         14 | public      | fpacl__for_portion_of_p | view        | periods_acl_1 | UPDATE
(14 rows)

GRANT SELECT, UPDATE ON TABLE fpacl__for_portion_of_p TO periods_acl_2; -- fail
ERROR:  cannot grant SELECT directly to "fpacl__for_portion_of_p"; grant SELECT to "fpacl" instead
CONTEXT:  PL/pgSQL function periods.health_checks() line 143 at RAISE
GRANT SELECT, UPDATE ON TABLE fpacl TO periods_acl_2;
TABLE show_acls ORDER BY sort_order;
 sort_order | schema_name |       object_name       | object_type |    grantee    | privilege_type 
------------+-------------+-------------------------+-------------+---------------+----------------
          1 | public      | fpacl                   | table       | periods_acl_1 | DELETE
          2 | public      | fpacl                   | table       | periods_acl_1 | INSERT
          3 | public      | fpacl                   | table       | periods_acl_1 | REFERENCES
          4 | public      | fpacl                   | table       | periods_acl_1 | SELECT
          5 | public      | fpacl                   | table       | periods_acl_1 | TRIGGER
          6 | public      | fpacl                   | table       | periods_acl_1 | TRUNCATE
          7 | public      | fpacl                   | table       | periods_acl_1 | UPDATE
          8 | public      | fpacl                   | table       | periods_acl_2 | SELECT
          9 | public      | fpacl                   | table       | periods_acl_2 | UPDATE
         10 | public      | fpacl__for_portion_of_p | view        | periods_acl_1 | DELETE
         11 | public      | fpacl__for_portion_of_p | view        | periods_acl_1 | INSERT
         12 | public      | fpacl__for_portion_of_p | view        | periods_acl_1 | REFERENCES
         13 | public      | fpacl__for_portion_of_p | view        | periods_acl_1 | SELECT
         14 | public      | fpacl__for_portion_of_p | view        | periods_acl_1 | TRIGGER
         15 | public      | fpacl__for_portion_of_p | view        | periods_acl_1 | TRUNCATE
         16 | public      | fpacl__for_portion_of_p | view        | periods_acl_1 | UPDATE
         17 | public      | fpacl__for_portion_of_p | view        | periods_acl_2 | SELECT
         18 | public      | fpacl__for_portion_of_p | view        | periods_acl_2 | UPDATE
(18 rows)

REVOKE UPDATE ON TABLE fpacl__for_portion_of_p FROM periods_acl_2; -- fail
ERROR:  cannot revoke UPDATE directly from "fpacl__for_portion_of_p", revoke UPDATE from "fpacl" instead
CONTEXT:  PL/pgSQL function periods.health_checks() line 255 at RAISE
REVOKE UPDATE ON TABLE fpacl FROM periods_acl_2;
TABLE show_acls ORDER BY sort_order;
 sort_order | schema_name |       object_name       | object_type |    grantee    | privilege_type 
------------+-------------+-------------------------+-------------+---------------+----------------
          1 | public      | fpacl                   | table       | periods_acl_1 | DELETE
          2 | public      | fpacl                   | table       | periods_acl_1 | INSERT
          3 | public      | fpacl                   | table       | periods_acl_1 | REFERENCES
          4 | public      | fpacl                   | table       | periods_acl_1 | SELECT
          5 | public      | fpacl                   | table       | periods_acl_1 | TRIGGER
          6 | public      | fpacl                   | table       | periods_acl_1 | TRUNCATE
          7 | public      | fpacl                   | table       | periods_acl_1 | UPDATE
          8 | public      | fpacl                   | table       | periods_acl_2 | SELECT
          9 | public      | fpacl__for_portion_of_p | view        | periods_acl_1 | DELETE
         10 | public      | fpacl__for_portion_of_p | view        | periods_acl_1 | INSERT
         11 | public      | fpacl__for_portion_of_p | view        | periods_acl_1 | REFERENCES
         12 | public      | fpacl__for_portion_of_p | view        | periods_acl_1 | SELECT
         13 | public      | fpacl__for_portion_of_p | view        | periods_acl_1 | TRIGGER
         14 | public      | fpacl__for_portion_of_p | view        | periods_acl_1 | TRUNCATE
         15 | public      | fpacl__for_portion_of_p | view        | periods_acl_1 | UPDATE
         16 | public      | fpacl__for_portion_of_p | view        | periods_acl_2 | SELECT
(16 rows)

SELECT periods.drop_for_portion_view('fpacl', 'p');
 drop_for_portion_view 
-----------------------
 t
(1 row)

DROP TABLE fpacl CASCADE;
DROP VIEW show_acls;
/* History ACL */
-- We call this query several times, so make it a view for eaiser maintenance
CREATE VIEW show_acls AS
    SELECT row_number() OVER (ORDER BY array_position(ARRAY['table', 'view', 'function'], object_type),
                                       schema_name, object_name, grantee, privilege_type) AS sort_order,
           *
    FROM (
        SELECT c.relnamespace::regnamespace AS schema_name,
               c.relname AS object_name,
               CASE c.relkind
                   WHEN 'r' THEN 'table'
                   WHEN 'v' THEN 'view'
               END AS object_type,
               acl.grantee::regrole::text AS grantee,
               acl.privilege_type
        FROM pg_class AS c
        CROSS JOIN LATERAL aclexplode(COALESCE(c.relacl, acldefault('r', c.relowner))) AS acl
        WHERE c.relname IN ('histacl', 'histacl_history', 'histacl_with_history')
        UNION ALL
        SELECT p.pronamespace::regnamespace,
               p.proname,
               'function',
               acl.grantee::regrole::text,
               acl.privilege_type
        FROM pg_proc AS p
        CROSS JOIN LATERAL aclexplode(COALESCE(p.proacl, acldefault('f', p.proowner))) AS acl
        WHERE p.proname IN ('histacl__as_of', 'histacl__between', 'histacl__between_symmetric', 'histacl__from_to')
    ) AS _;
CREATE TABLE histacl (col text);
ALTER TABLE histacl OWNER TO periods_acl_1;
SELECT periods.add_system_time_period('histacl');
 add_system_time_period 
------------------------
 t
(1 row)

SELECT periods.add_system_versioning('histacl');
NOTICE:  history table "histacl_history" created for "histacl", be sure to index it properly
 add_system_versioning 
-----------------------
 
(1 row)

TABLE show_acls ORDER BY sort_order;
 sort_order | schema_name |        object_name         | object_type |    grantee    | privilege_type 
------------+-------------+----------------------------+-------------+---------------+----------------
          1 | public      | histacl                    | table       | periods_acl_1 | DELETE
          2 | public      | histacl                    | table       | periods_acl_1 | INSERT
          3 | public      | histacl                    | table       | periods_acl_1 | REFERENCES
          4 | public      | histacl                    | table       | periods_acl_1 | SELECT
          5 | public      | histacl                    | table       | periods_acl_1 | TRIGGER
          6 | public      | histacl                    | table       | periods_acl_1 | TRUNCATE
          7 | public      | histacl                    | table       | periods_acl_1 | UPDATE
          8 | public      | histacl_history            | table       | periods_acl_1 | SELECT
          9 | public      | histacl_with_history       | view        | periods_acl_1 | SELECT
         10 | public      | histacl__as_of             | function    | periods_acl_1 | EXECUTE
         11 | public      | histacl__between           | function    | periods_acl_1 | EXECUTE
         12 | public      | histacl__between_symmetric | function    | periods_acl_1 | EXECUTE
         13 | public      | histacl__from_to           | function    | periods_acl_1 | EXECUTE
(13 rows)

-- Disconnect, add some privs to the history table, and reconnect
SELECT periods.drop_system_versioning('histacl');
 drop_system_versioning 
------------------------
 t
(1 row)

GRANT ALL ON TABLE histacl_history TO periods_acl_3;
TABLE show_acls ORDER BY sort_order;
 sort_order | schema_name |   object_name   | object_type |    grantee    | privilege_type 
------------+-------------+-----------------+-------------+---------------+----------------
          1 | public      | histacl         | table       | periods_acl_1 | DELETE
          2 | public      | histacl         | table       | periods_acl_1 | INSERT
          3 | public      | histacl         | table       | periods_acl_1 | REFERENCES
          4 | public      | histacl         | table       | periods_acl_1 | SELECT
          5 | public      | histacl         | table       | periods_acl_1 | TRIGGER
          6 | public      | histacl         | table       | periods_acl_1 | TRUNCATE
          7 | public      | histacl         | table       | periods_acl_1 | UPDATE
          8 | public      | histacl_history | table       | periods_acl_1 | SELECT
          9 | public      | histacl_history | table       | periods_acl_3 | DELETE
         10 | public      | histacl_history | table       | periods_acl_3 | INSERT
         11 | public      | histacl_history | table       | periods_acl_3 | REFERENCES
         12 | public      | histacl_history | table       | periods_acl_3 | SELECT
         13 | public      | histacl_history | table       | periods_acl_3 | TRIGGER
         14 | public      | histacl_history | table       | periods_acl_3 | TRUNCATE
         15 | public      | histacl_history | table       | periods_acl_3 | UPDATE
(15 rows)

SELECT periods.add_system_versioning('histacl');
 add_system_versioning 
-----------------------
 
(1 row)

TABLE show_acls ORDER BY sort_order;
 sort_order | schema_name |        object_name         | object_type |    grantee    | privilege_type 
------------+-------------+----------------------------+-------------+---------------+----------------
          1 | public      | histacl                    | table       | periods_acl_1 | DELETE
          2 | public      | histacl                    | table       | periods_acl_1 | INSERT
          3 | public      | histacl                    | table       | periods_acl_1 | REFERENCES
          4 | public      | histacl                    | table       | periods_acl_1 | SELECT
          5 | public      | histacl                    | table       | periods_acl_1 | TRIGGER
          6 | public      | histacl                    | table       | periods_acl_1 | TRUNCATE
          7 | public      | histacl                    | table       | periods_acl_1 | UPDATE
          8 | public      | histacl_history            | table       | periods_acl_1 | SELECT
          9 | public      | histacl_with_history       | view        | periods_acl_1 | SELECT
         10 | public      | histacl__as_of             | function    | periods_acl_1 | EXECUTE
         11 | public      | histacl__between           | function    | periods_acl_1 | EXECUTE
         12 | public      | histacl__between_symmetric | function    | periods_acl_1 | EXECUTE
         13 | public      | histacl__from_to           | function    | periods_acl_1 | EXECUTE
(13 rows)

-- These next 6 blocks should fail
GRANT ALL ON TABLE histacl_history TO periods_acl_3; -- fail
ERROR:  cannot grant DELETE to "histacl_history"; history objects are read-only
CONTEXT:  PL/pgSQL function periods.health_checks() line 138 at RAISE
GRANT SELECT ON TABLE histacl_history TO periods_acl_3; -- fail
ERROR:  cannot grant SELECT directly to "histacl_history"; grant SELECT to "histacl" instead
CONTEXT:  PL/pgSQL function periods.health_checks() line 143 at RAISE
REVOKE ALL ON TABLE histacl_history FROM periods_acl_1; -- fail
ERROR:  cannot revoke SELECT directly from "histacl_history", revoke SELECT from "histacl" instead
CONTEXT:  PL/pgSQL function periods.health_checks() line 255 at RAISE
TABLE show_acls ORDER BY sort_order;
 sort_order | schema_name |        object_name         | object_type |    grantee    | privilege_type 
------------+-------------+----------------------------+-------------+---------------+----------------
          1 | public      | histacl                    | table       | periods_acl_1 | DELETE
          2 | public      | histacl                    | table       | periods_acl_1 | INSERT
          3 | public      | histacl                    | table       | periods_acl_1 | REFERENCES
          4 | public      | histacl                    | table       | periods_acl_1 | SELECT
          5 | public      | histacl                    | table       | periods_acl_1 | TRIGGER
          6 | public      | histacl                    | table       | periods_acl_1 | TRUNCATE
          7 | public      | histacl                    | table       | periods_acl_1 | UPDATE
          8 | public      | histacl_history            | table       | periods_acl_1 | SELECT
          9 | public      | histacl_with_history       | view        | periods_acl_1 | SELECT
         10 | public      | histacl__as_of             | function    | periods_acl_1 | EXECUTE
         11 | public      | histacl__between           | function    | periods_acl_1 | EXECUTE
         12 | public      | histacl__between_symmetric | function    | periods_acl_1 | EXECUTE
         13 | public      | histacl__from_to           | function    | periods_acl_1 | EXECUTE
(13 rows)

GRANT ALL ON TABLE histacl_with_history TO periods_acl_3; -- fail
ERROR:  cannot grant DELETE to "histacl_with_history"; history objects are read-only
CONTEXT:  PL/pgSQL function periods.health_checks() line 138 at RAISE
GRANT SELECT ON TABLE histacl_with_history TO periods_acl_3; -- fail
ERROR:  cannot grant SELECT directly to "histacl_with_history"; grant SELECT to "histacl" instead
CONTEXT:  PL/pgSQL function periods.health_checks() line 143 at RAISE
REVOKE ALL ON TABLE histacl_with_history FROM periods_acl_1; -- fail
ERROR:  cannot revoke SELECT directly from "histacl_with_history", revoke SELECT from "histacl" instead
CONTEXT:  PL/pgSQL function periods.health_checks() line 255 at RAISE
TABLE show_acls ORDER BY sort_order;
 sort_order | schema_name |        object_name         | object_type |    grantee    | privilege_type 
------------+-------------+----------------------------+-------------+---------------+----------------
          1 | public      | histacl                    | table       | periods_acl_1 | DELETE
          2 | public      | histacl                    | table       | periods_acl_1 | INSERT
          3 | public      | histacl                    | table       | periods_acl_1 | REFERENCES
          4 | public      | histacl                    | table       | periods_acl_1 | SELECT
          5 | public      | histacl                    | table       | periods_acl_1 | TRIGGER
          6 | public      | histacl                    | table       | periods_acl_1 | TRUNCATE
          7 | public      | histacl                    | table       | periods_acl_1 | UPDATE
          8 | public      | histacl_history            | table       | periods_acl_1 | SELECT
          9 | public      | histacl_with_history       | view        | periods_acl_1 | SELECT
         10 | public      | histacl__as_of             | function    | periods_acl_1 | EXECUTE
         11 | public      | histacl__between           | function    | periods_acl_1 | EXECUTE
         12 | public      | histacl__between_symmetric | function    | periods_acl_1 | EXECUTE
         13 | public      | histacl__from_to           | function    | periods_acl_1 | EXECUTE
(13 rows)

GRANT ALL ON FUNCTION histacl__as_of(timestamp with time zone) TO periods_acl_3; -- fail
ERROR:  cannot grant EXECUTE directly to "histacl__as_of(timestamp with time zone)"; grant SELECT to "histacl" instead
CONTEXT:  PL/pgSQL function periods.health_checks() line 143 at RAISE
GRANT EXECUTE ON FUNCTION histacl__as_of(timestamp with time zone) TO periods_acl_3; -- fail
ERROR:  cannot grant EXECUTE directly to "histacl__as_of(timestamp with time zone)"; grant SELECT to "histacl" instead
CONTEXT:  PL/pgSQL function periods.health_checks() line 143 at RAISE
REVOKE ALL ON FUNCTION histacl__as_of(timestamp with time zone) FROM periods_acl_1; -- fail
ERROR:  cannot revoke EXECUTE directly from "histacl__as_of(timestamp with time zone)", revoke SELECT from "histacl" instead
CONTEXT:  PL/pgSQL function periods.health_checks() line 255 at RAISE
TABLE show_acls ORDER BY sort_order;
 sort_order | schema_name |        object_name         | object_type |    grantee    | privilege_type 
------------+-------------+----------------------------+-------------+---------------+----------------
          1 | public      | histacl                    | table       | periods_acl_1 | DELETE
          2 | public      | histacl                    | table       | periods_acl_1 | INSERT
          3 | public      | histacl                    | table       | periods_acl_1 | REFERENCES
          4 | public      | histacl                    | table       | periods_acl_1 | SELECT
          5 | public      | histacl                    | table       | periods_acl_1 | TRIGGER
          6 | public      | histacl                    | table       | periods_acl_1 | TRUNCATE
          7 | public      | histacl                    | table       | periods_acl_1 | UPDATE
          8 | public      | histacl_history            | table       | periods_acl_1 | SELECT
          9 | public      | histacl_with_history       | view        | periods_acl_1 | SELECT
         10 | public      | histacl__as_of             | function    | periods_acl_1 | EXECUTE
         11 | public      | histacl__between           | function    | periods_acl_1 | EXECUTE
         12 | public      | histacl__between_symmetric | function    | periods_acl_1 | EXECUTE
         13 | public      | histacl__from_to           | function    | periods_acl_1 | EXECUTE
(13 rows)

GRANT ALL ON FUNCTION histacl__between(timestamp with time zone, timestamp with time zone) TO periods_acl_3; -- fail
ERROR:  cannot grant EXECUTE directly to "histacl__between(timestamp with time zone,timestamp with time zone)"; grant SELECT to "histacl" instead
CONTEXT:  PL/pgSQL function periods.health_checks() line 143 at RAISE
GRANT EXECUTE ON FUNCTION histacl__between(timestamp with time zone, timestamp with time zone) TO periods_acl_3; -- fail
ERROR:  cannot grant EXECUTE directly to "histacl__between(timestamp with time zone,timestamp with time zone)"; grant SELECT to "histacl" instead
CONTEXT:  PL/pgSQL function periods.health_checks() line 143 at RAISE
REVOKE ALL ON FUNCTION histacl__between(timestamp with time zone, timestamp with time zone) FROM periods_acl_1; -- fail
ERROR:  cannot revoke EXECUTE directly from "histacl__between(timestamp with time zone,timestamp with time zone)", revoke SELECT from "histacl" instead
CONTEXT:  PL/pgSQL function periods.health_checks() line 255 at RAISE
TABLE show_acls ORDER BY sort_order;
 sort_order | schema_name |        object_name         | object_type |    grantee    | privilege_type 
------------+-------------+----------------------------+-------------+---------------+----------------
          1 | public      | histacl                    | table       | periods_acl_1 | DELETE
          2 | public      | histacl                    | table       | periods_acl_1 | INSERT
          3 | public      | histacl                    | table       | periods_acl_1 | REFERENCES
          4 | public      | histacl                    | table       | periods_acl_1 | SELECT
          5 | public      | histacl                    | table       | periods_acl_1 | TRIGGER
          6 | public      | histacl                    | table       | periods_acl_1 | TRUNCATE
          7 | public      | histacl                    | table       | periods_acl_1 | UPDATE
          8 | public      | histacl_history            | table       | periods_acl_1 | SELECT
          9 | public      | histacl_with_history       | view        | periods_acl_1 | SELECT
         10 | public      | histacl__as_of             | function    | periods_acl_1 | EXECUTE
         11 | public      | histacl__between           | function    | periods_acl_1 | EXECUTE
         12 | public      | histacl__between_symmetric | function    | periods_acl_1 | EXECUTE
         13 | public      | histacl__from_to           | function    | periods_acl_1 | EXECUTE
(13 rows)

GRANT ALL ON FUNCTION histacl__between_symmetric(timestamp with time zone, timestamp with time zone) TO periods_acl_3; -- fail
ERROR:  cannot grant EXECUTE directly to "histacl__between_symmetric(timestamp with time zone,timestamp with time zone)"; grant SELECT to "histacl" instead
CONTEXT:  PL/pgSQL function periods.health_checks() line 143 at RAISE
GRANT EXECUTE ON FUNCTION histacl__between_symmetric(timestamp with time zone, timestamp with time zone) TO periods_acl_3; -- fail
ERROR:  cannot grant EXECUTE directly to "histacl__between_symmetric(timestamp with time zone,timestamp with time zone)"; grant SELECT to "histacl" instead
CONTEXT:  PL/pgSQL function periods.health_checks() line 143 at RAISE
REVOKE ALL ON FUNCTION histacl__between_symmetric(timestamp with time zone, timestamp with time zone) FROM periods_acl_1; -- fail
ERROR:  cannot revoke EXECUTE directly from "histacl__between_symmetric(timestamp with time zone,timestamp with time zone)", revoke SELECT from "histacl" instead
CONTEXT:  PL/pgSQL function periods.health_checks() line 255 at RAISE
TABLE show_acls ORDER BY sort_order;
 sort_order | schema_name |        object_name         | object_type |    grantee    | privilege_type 
------------+-------------+----------------------------+-------------+---------------+----------------
          1 | public      | histacl                    | table       | periods_acl_1 | DELETE
          2 | public      | histacl                    | table       | periods_acl_1 | INSERT
          3 | public      | histacl                    | table       | periods_acl_1 | REFERENCES
          4 | public      | histacl                    | table       | periods_acl_1 | SELECT
          5 | public      | histacl                    | table       | periods_acl_1 | TRIGGER
          6 | public      | histacl                    | table       | periods_acl_1 | TRUNCATE
          7 | public      | histacl                    | table       | periods_acl_1 | UPDATE
          8 | public      | histacl_history            | table       | periods_acl_1 | SELECT
          9 | public      | histacl_with_history       | view        | periods_acl_1 | SELECT
         10 | public      | histacl__as_of             | function    | periods_acl_1 | EXECUTE
         11 | public      | histacl__between           | function    | periods_acl_1 | EXECUTE
         12 | public      | histacl__between_symmetric | function    | periods_acl_1 | EXECUTE
         13 | public      | histacl__from_to           | function    | periods_acl_1 | EXECUTE
(13 rows)

GRANT ALL ON FUNCTION histacl__from_to(timestamp with time zone, timestamp with time zone) TO periods_acl_3; -- fail
ERROR:  cannot grant EXECUTE directly to "histacl__from_to(timestamp with time zone,timestamp with time zone)"; grant SELECT to "histacl" instead
CONTEXT:  PL/pgSQL function periods.health_checks() line 143 at RAISE
GRANT EXECUTE ON FUNCTION histacl__from_to(timestamp with time zone, timestamp with time zone) TO periods_acl_3; -- fail
ERROR:  cannot grant EXECUTE directly to "histacl__from_to(timestamp with time zone,timestamp with time zone)"; grant SELECT to "histacl" instead
CONTEXT:  PL/pgSQL function periods.health_checks() line 143 at RAISE
REVOKE ALL ON FUNCTION histacl__from_to(timestamp with time zone, timestamp with time zone) FROM periods_acl_1; -- fail
ERROR:  cannot revoke EXECUTE directly from "histacl__from_to(timestamp with time zone,timestamp with time zone)", revoke SELECT from "histacl" instead
CONTEXT:  PL/pgSQL function periods.health_checks() line 255 at RAISE
TABLE show_acls ORDER BY sort_order;
 sort_order | schema_name |        object_name         | object_type |    grantee    | privilege_type 
------------+-------------+----------------------------+-------------+---------------+----------------
          1 | public      | histacl                    | table       | periods_acl_1 | DELETE
          2 | public      | histacl                    | table       | periods_acl_1 | INSERT
          3 | public      | histacl                    | table       | periods_acl_1 | REFERENCES
          4 | public      | histacl                    | table       | periods_acl_1 | SELECT
          5 | public      | histacl                    | table       | periods_acl_1 | TRIGGER
          6 | public      | histacl                    | table       | periods_acl_1 | TRUNCATE
          7 | public      | histacl                    | table       | periods_acl_1 | UPDATE
          8 | public      | histacl_history            | table       | periods_acl_1 | SELECT
          9 | public      | histacl_with_history       | view        | periods_acl_1 | SELECT
         10 | public      | histacl__as_of             | function    | periods_acl_1 | EXECUTE
         11 | public      | histacl__between           | function    | periods_acl_1 | EXECUTE
         12 | public      | histacl__between_symmetric | function    | periods_acl_1 | EXECUTE
         13 | public      | histacl__from_to           | function    | periods_acl_1 | EXECUTE
(13 rows)

-- This one should work and propagate
GRANT ALL ON TABLE histacl TO periods_acl_2;
TABLE show_acls ORDER BY sort_order;
 sort_order | schema_name |        object_name         | object_type |    grantee    | privilege_type 
------------+-------------+----------------------------+-------------+---------------+----------------
          1 | public      | histacl                    | table       | periods_acl_1 | DELETE
          2 | public      | histacl                    | table       | periods_acl_1 | INSERT
          3 | public      | histacl                    | table       | periods_acl_1 | REFERENCES
          4 | public      | histacl                    | table       | periods_acl_1 | SELECT
          5 | public      | histacl                    | table       | periods_acl_1 | TRIGGER
          6 | public      | histacl                    | table       | periods_acl_1 | TRUNCATE
          7 | public      | histacl                    | table       | periods_acl_1 | UPDATE
          8 | public      | histacl                    | table       | periods_acl_2 | DELETE
          9 | public      | histacl                    | table       | periods_acl_2 | INSERT
         10 | public      | histacl                    | table       | periods_acl_2 | REFERENCES
         11 | public      | histacl                    | table       | periods_acl_2 | SELECT
         12 | public      | histacl                    | table       | periods_acl_2 | TRIGGER
         13 | public      | histacl                    | table       | periods_acl_2 | TRUNCATE
         14 | public      | histacl                    | table       | periods_acl_2 | UPDATE
         15 | public      | histacl_history            | table       | periods_acl_1 | SELECT
         16 | public      | histacl_history            | table       | periods_acl_2 | SELECT
         17 | public      | histacl_with_history       | view        | periods_acl_1 | SELECT
         18 | public      | histacl_with_history       | view        | periods_acl_2 | SELECT
         19 | public      | histacl__as_of             | function    | periods_acl_1 | EXECUTE
         20 | public      | histacl__as_of             | function    | periods_acl_2 | EXECUTE
         21 | public      | histacl__between           | function    | periods_acl_1 | EXECUTE
         22 | public      | histacl__between           | function    | periods_acl_2 | EXECUTE
         23 | public      | histacl__between_symmetric | function    | periods_acl_1 | EXECUTE
         24 | public      | histacl__between_symmetric | function    | periods_acl_2 | EXECUTE
         25 | public      | histacl__from_to           | function    | periods_acl_1 | EXECUTE
         26 | public      | histacl__from_to           | function    | periods_acl_2 | EXECUTE
(26 rows)

REVOKE SELECT ON TABLE histacl FROM periods_acl_2;
TABLE show_acls ORDER BY sort_order;
 sort_order | schema_name |        object_name         | object_type |    grantee    | privilege_type 
------------+-------------+----------------------------+-------------+---------------+----------------
          1 | public      | histacl                    | table       | periods_acl_1 | DELETE
          2 | public      | histacl                    | table       | periods_acl_1 | INSERT
          3 | public      | histacl                    | table       | periods_acl_1 | REFERENCES
          4 | public      | histacl                    | table       | periods_acl_1 | SELECT
          5 | public      | histacl                    | table       | periods_acl_1 | TRIGGER
          6 | public      | histacl                    | table       | periods_acl_1 | TRUNCATE
          7 | public      | histacl                    | table       | periods_acl_1 | UPDATE
          8 | public      | histacl                    | table       | periods_acl_2 | DELETE
          9 | public      | histacl                    | table       | periods_acl_2 | INSERT
         10 | public      | histacl                    | table       | periods_acl_2 | REFERENCES
         11 | public      | histacl                    | table       | periods_acl_2 | TRIGGER
         12 | public      | histacl                    | table       | periods_acl_2 | TRUNCATE
         13 | public      | histacl                    | table       | periods_acl_2 | UPDATE
         14 | public      | histacl_history            | table       | periods_acl_1 | SELECT
         15 | public      | histacl_with_history       | view        | periods_acl_1 | SELECT
         16 | public      | histacl__as_of             | function    | periods_acl_1 | EXECUTE
         17 | public      | histacl__between           | function    | periods_acl_1 | EXECUTE
         18 | public      | histacl__between_symmetric | function    | periods_acl_1 | EXECUTE
         19 | public      | histacl__from_to           | function    | periods_acl_1 | EXECUTE
(19 rows)

SELECT periods.drop_system_versioning('histacl', drop_behavior => 'CASCADE', purge => true);
 drop_system_versioning 
------------------------
 t
(1 row)

DROP TABLE histacl CASCADE;
DROP VIEW show_acls;
/* Who can modify the history table? */
CREATE TABLE retention (value integer);
ALTER TABLE retention OWNER TO periods_acl_1;
REVOKE ALL ON TABLE retention FROM PUBLIC;
GRANT ALL ON TABLE retention TO periods_acl_2;
GRANT SELECT ON TABLE retention TO periods_acl_3;
SELECT periods.add_system_time_period('retention');
 add_system_time_period 
------------------------
 t
(1 row)

SELECT periods.add_system_versioning('retention');
NOTICE:  history table "retention_history" created for "retention", be sure to index it properly
 add_system_versioning 
-----------------------
 
(1 row)

INSERT INTO retention (value) VALUES (1);
UPDATE retention SET value = 2;
SET ROLE TO periods_acl_3;
DELETE FROM retention_history; -- fail
ERROR:  permission denied for table retention_history
SET ROLE TO periods_acl_2;
DELETE FROM retention_history; -- fail
ERROR:  permission denied for table retention_history
SET ROLE TO periods_acl_1;
DELETE FROM retention_history; -- fail
ERROR:  permission denied for table retention_history
-- test what the docs say to do
BEGIN;
SELECT periods.drop_system_versioning('retention');
 drop_system_versioning 
------------------------
 t
(1 row)

GRANT DELETE ON TABLE retention_history TO CURRENT_USER;
DELETE FROM retention_history;
SELECT periods.add_system_versioning('retention');
 add_system_versioning 
-----------------------
 
(1 row)

COMMIT;
-- superuser can do anything
RESET ROLE;
DELETE FROM retention_history;
SELECT periods.drop_system_versioning('retention', drop_behavior => 'CASCADE', purge => true);
 drop_system_versioning 
------------------------
 t
(1 row)

DROP TABLE retention CASCADE;
/* Clean up */
DROP ROLE periods_acl_1;
DROP ROLE periods_acl_2;
DROP ROLE periods_acl_3;