-
Notifications
You must be signed in to change notification settings - Fork 58
/
bdr_commandfilter.c
1199 lines (1024 loc) · 28.8 KB
/
bdr_commandfilter.c
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
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -------------------------------------------------------------------------
*
* bdr_commandfilter.c
* prevent execution of utility commands not yet or never supported
*
*
* Copyright (C) 2012-2015, PostgreSQL Global Development Group
*
* IDENTIFICATION
* bdr_commandfilter.c
*
* -------------------------------------------------------------------------
*/
#include "postgres.h"
#include "bdr.h"
#include "bdr_locks.h"
#include "fmgr.h"
#include "miscadmin.h"
#include "access/genam.h"
#include "access/heapam.h"
#include "access/seqam.h"
#include "catalog/namespace.h"
#include "commands/dbcommands.h"
#include "commands/event_trigger.h"
#include "commands/extension.h"
#include "commands/defrem.h"
#include "commands/tablecmds.h"
/* For the client auth filter */
#include "libpq/auth.h"
#include "parser/parse_utilcmd.h"
#include "replication/replication_identifier.h"
#include "storage/standby.h"
#include "tcop/utility.h"
#include "utils/guc.h"
#include "utils/lsyscache.h"
#include "utils/rel.h"
#include "utils/syscache.h"
/*
* bdr_commandfilter.c: a ProcessUtility_hook to prevent a cluster from running
* commands that BDR does not yet support.
*/
static ProcessUtility_hook_type next_ProcessUtility_hook = NULL;
static ClientAuthentication_hook_type next_ClientAuthentication_hook = NULL;
/* GUCs */
bool bdr_permit_unsafe_commands = false;
static void error_unsupported_command(const char *cmdtag) __attribute__((noreturn));
/*
* Check the passed rangevar, locking it and looking it up in the cache
* then determine if the relation requires logging to WAL. If it does, then
* right now BDR won't cope with it and we must reject the operation that
* touches this relation.
*/
static void
error_on_persistent_rv(RangeVar *rv,
const char *cmdtag,
LOCKMODE lockmode,
bool missing_ok)
{
bool needswal;
Relation rel;
if (rv == NULL)
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("Unqualified command %s is unsafe with BDR active.",
cmdtag)));
rel = heap_openrv_extended(rv, lockmode, missing_ok);
if (rel != NULL)
{
needswal = RelationNeedsWAL(rel);
heap_close(rel, lockmode);
if (needswal)
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("%s may only affect UNLOGGED or TEMPORARY tables " \
"when BDR is active; %s is a regular table",
cmdtag, rv->relname)));
}
}
static void
error_unsupported_command(const char *cmdtag)
{
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("%s is not supported when bdr is active",
cmdtag)));
}
static bool ispermanent(const char persistence)
{
/* In case someone adds a new type we don't know about */
Assert(persistence == RELPERSISTENCE_TEMP
|| persistence == RELPERSISTENCE_UNLOGGED
|| persistence == RELPERSISTENCE_PERMANENT);
return persistence == RELPERSISTENCE_PERMANENT;
}
static void
filter_CreateStmt(Node *parsetree,
char *completionTag)
{
CreateStmt *stmt;
ListCell *cell;
bool with_oids = default_with_oids;
stmt = (CreateStmt *) parsetree;
if (stmt->ofTypename != NULL)
error_unsupported_command("CREATE TABLE ... OF TYPE");
/* verify WITH options */
foreach(cell, stmt->options)
{
DefElem *def = (DefElem *) lfirst(cell);
/* reject WITH OIDS */
if (def->defnamespace == NULL &&
pg_strcasecmp(def->defname, "oids") == 0)
{
with_oids = defGetBoolean(def);
}
}
if (with_oids)
{
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("Tables WITH OIDs are not supported with bdr")));
}
/* verify table elements */
foreach(cell, stmt->tableElts)
{
Node *element = lfirst(cell);
if (nodeTag(element) == T_Constraint)
{
Constraint *con = (Constraint *) element;
if (con->contype == CONSTR_EXCLUSION &&
ispermanent(stmt->relation->relpersistence))
{
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("EXCLUDE constraints are unsafe with BDR active")));
}
}
}
}
static void
filter_AlterTableStmt(Node *parsetree,
char *completionTag,
const char *queryString)
{
AlterTableStmt *astmt;
ListCell *cell,
*cell1;
bool hasInvalid;
List *stmts;
Oid relid;
LOCKMODE lockmode;
astmt = (AlterTableStmt *) parsetree;
hasInvalid = false;
/*
* Can't use AlterTableGetLockLevel(astmt->cmds);
* Otherwise we deadlock between the global DDL locks and DML replay.
* ShareUpdateExclusiveLock should be enough to block DDL but not DML.
*/
lockmode = ShareUpdateExclusiveLock;
relid = AlterTableLookupRelation(astmt, lockmode);
stmts = transformAlterTableStmt(relid, astmt, queryString);
foreach(cell, stmts)
{
Node *node = (Node *) lfirst(cell);
AlterTableStmt *at_stmt;
/*
* we ignore all nodes which are not AlterTableCmd statements since
* the standard utility hook will recurse and thus call our handler
* again
*/
if (!IsA(node, AlterTableStmt))
continue;
at_stmt = (AlterTableStmt *) node;
foreach(cell1, at_stmt->cmds)
{
AlterTableCmd *stmt = (AlterTableCmd *) lfirst(cell1);
switch (stmt->subtype)
{
/*
* allowed for now:
*/
case AT_AddColumn:
{
ColumnDef *def = (ColumnDef *) stmt->def;
ListCell *cell;
/*
* Error out if there's a default for the new column,
* that requires a table rewrite which might be
* nondeterministic.
*/
if (def->raw_default != NULL ||
def->cooked_default != NULL)
{
error_on_persistent_rv(
astmt->relation,
"ALTER TABLE ... ADD COLUMN ... DEFAULT",
lockmode,
astmt->missing_ok);
}
/*
* Column defaults can also be represented as
* constraints.
*/
foreach(cell, def->constraints)
{
Constraint *con;
Assert(IsA(lfirst(cell), Constraint));
con = (Constraint *) lfirst(cell);
if (con->contype == CONSTR_DEFAULT)
error_on_persistent_rv(
astmt->relation,
"ALTER TABLE ... ADD COLUMN ... DEFAULT",
lockmode,
astmt->missing_ok);
}
}
case AT_AddIndex: /* produced by for example ALTER TABLE … ADD
* CONSTRAINT … PRIMARY KEY */
case AT_DropColumn:
case AT_DropNotNull:
case AT_SetNotNull:
case AT_ColumnDefault: /* ALTER COLUMN DEFAULT */
case AT_ClusterOn: /* CLUSTER ON */
case AT_DropCluster: /* SET WITHOUT CLUSTER */
case AT_SetRelOptions: /* SET (...) */
case AT_ResetRelOptions: /* RESET (...) */
case AT_ReplaceRelOptions: /* replace reloption list */
case AT_ReplicaIdentity:
case AT_ChangeOwner:
case AT_SetStorage:
break;
case AT_DropConstraint:
break;
case AT_SetTableSpace:
break;
case AT_AddConstraint:
case AT_ProcessedConstraint:
if (IsA(stmt->def, Constraint))
{
Constraint *con = (Constraint *) stmt->def;
if (con->contype == CONSTR_EXCLUSION)
error_on_persistent_rv(astmt->relation,
"ALTER TABLE ... ADD CONSTRAINT ... EXCLUDE",
lockmode,
astmt->missing_ok);
}
break;
case AT_ValidateConstraint: /* VALIDATE CONSTRAINT */
break;
case AT_AlterConstraint:
error_on_persistent_rv(astmt->relation,
"ALTER TABLE ... ALTER CONSTRAINT",
lockmode,
astmt->missing_ok);
break;
case AT_AddIndexConstraint:
/* no deparse support */
error_on_persistent_rv(astmt->relation,
"ALTER TABLE ... ADD CONSTRAINT USING INDEX",
lockmode,
astmt->missing_ok);
break;
case AT_AlterColumnType:
error_on_persistent_rv(astmt->relation,
"ALTER TABLE ... ALTER COLUMN TYPE",
lockmode,
astmt->missing_ok);
break;
case AT_AlterColumnGenericOptions:
error_on_persistent_rv(astmt->relation,
"ALTER TABLE ... ALTER COLUMN OPTIONS",
lockmode,
astmt->missing_ok);
break;
case AT_AddOids:
case AT_DropOids:
error_on_persistent_rv(astmt->relation,
"ALTER TABLE ... SET WITH[OUT] OIDS",
lockmode,
astmt->missing_ok);
break;
case AT_EnableTrig:
case AT_EnableAlwaysTrig:
case AT_EnableReplicaTrig:
case AT_DisableTrig:
case AT_EnableTrigAll:
case AT_DisableTrigAll:
case AT_EnableTrigUser:
case AT_DisableTrigUser:
break;
case AT_EnableRule:
case AT_EnableAlwaysRule:
case AT_EnableReplicaRule:
case AT_DisableRule:
error_on_persistent_rv(astmt->relation,
"ALTER TABLE ... ENABLE|DISABLE [ALWAYS|REPLICA] RULE",
lockmode,
astmt->missing_ok);
break;
case AT_AddInherit:
case AT_DropInherit:
error_on_persistent_rv(astmt->relation,
"ALTER TABLE ... [NO] INHERIT",
lockmode,
astmt->missing_ok);
break;
case AT_AddOf:
case AT_DropOf:
error_on_persistent_rv(astmt->relation,
"ALTER TABLE ... [NOT] OF",
lockmode,
astmt->missing_ok);
break;
case AT_SetStatistics:
break;
case AT_SetOptions:
case AT_ResetOptions:
error_on_persistent_rv(astmt->relation,
"ALTER TABLE ... ALTER COLUMN ... SET STATISTICS|(...)",
lockmode,
astmt->missing_ok);
break;
case AT_GenericOptions:
error_on_persistent_rv(astmt->relation,
"ALTER TABLE ... SET (...)",
lockmode,
astmt->missing_ok);
break;
default:
hasInvalid = true;
break;
}
}
}
if (hasInvalid)
error_on_persistent_rv(astmt->relation,
"This variant of ALTER TABLE",
lockmode,
astmt->missing_ok);
}
static void
filter_CreateSeqStmt(Node *parsetree)
{
ListCell *param;
CreateSeqStmt *stmt;
stmt = (CreateSeqStmt *) parsetree;
if (stmt->accessMethod == NULL || strcmp(stmt->accessMethod, "bdr") != 0)
return;
foreach(param, stmt->options)
{
DefElem *defel = (DefElem *) lfirst(param);
if (strcmp(defel->defname, "owned_by") != 0 &&
strcmp(defel->defname, "start") != 0)
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("CREATE SEQUENCE ... %s is not supported for bdr sequences",
defel->defname)));
}
}
static void
filter_AlterSeqStmt(Node *parsetree)
{
Oid seqoid;
ListCell *param;
AlterSeqStmt *stmt;
Oid seqamid;
HeapTuple ctup;
Form_pg_class pgcform;
stmt = (AlterSeqStmt *) parsetree;
seqoid = RangeVarGetRelid(stmt->sequence, AccessShareLock, true);
if (seqoid == InvalidOid)
return;
seqamid = get_seqam_oid("bdr", true);
/* No bdr sequences? */
if (seqamid == InvalidOid)
return;
/* Fetch a tuple to check for relam */
ctup = SearchSysCache1(RELOID, ObjectIdGetDatum(seqoid));
if (!HeapTupleIsValid(ctup))
elog(ERROR, "pg_class entry for sequence %u unavailable",
seqoid);
pgcform = (Form_pg_class) GETSTRUCT(ctup);
/* Not bdr sequence */
if (pgcform->relam != seqamid)
{
ReleaseSysCache(ctup);
return;
}
ReleaseSysCache(ctup);
foreach(param, stmt->options)
{
DefElem *defel = (DefElem *) lfirst(param);
if (strcmp(defel->defname, "owned_by") != 0)
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("ALTER SEQUENCE ... %s is not supported for bdr sequences",
defel->defname)));
}
}
static void
filter_CreateTableAs(Node *parsetree)
{
CreateTableAsStmt *stmt;
stmt = (CreateTableAsStmt *) parsetree;
if (ispermanent(stmt->into->rel->relpersistence))
error_unsupported_command(CreateCommandTag(parsetree));
}
static bool
statement_affects_only_nonpermanent(Node *parsetree)
{
switch (nodeTag(parsetree))
{
case T_CreateTableAsStmt:
{
CreateTableAsStmt *stmt = (CreateTableAsStmt *) parsetree;
return !ispermanent(stmt->into->rel->relpersistence);
}
case T_CreateStmt:
{
CreateStmt *stmt = (CreateStmt *) parsetree;
return !ispermanent(stmt->relation->relpersistence);
}
case T_DropStmt:
{
DropStmt *stmt = (DropStmt *) parsetree;
ListCell *cell;
/*
* It doesn't make any sense to drop temporary tables
* concurrently.
*/
if (stmt->concurrent)
return false;
/* Figure out if only temporary objects are affected. */
/*
* Only do this for temporary relations and indexes, not
* other objects for now.
*/
switch (stmt->removeType)
{
case OBJECT_INDEX:
case OBJECT_TABLE:
case OBJECT_SEQUENCE:
case OBJECT_VIEW:
case OBJECT_MATVIEW:
case OBJECT_FOREIGN_TABLE:
break;
default:
return false;
}
/* Now check each dropped relation. */
foreach(cell, stmt->objects)
{
Oid relOid;
RangeVar *rv = makeRangeVarFromNameList((List *) lfirst(cell));
Relation rel;
bool istemp;
relOid = RangeVarGetRelidExtended(rv,
AccessExclusiveLock,
stmt->missing_ok,
false,
NULL,
NULL);
if (relOid == InvalidOid)
continue;
/*
* If a schema name is not provided, check to see if the session's
* temporary namespace is first in the search_path and if a relation
* with the same Oid is in the current session's "pg_temp" schema. If
* so, we can safely assume that the DROP statement will refer to this
* object, since the pg_temp schema is session-private.
*/
if (rv->schemaname == NULL)
{
Oid tempNamespaceOid, tempRelOid;
List* searchPath;
bool foundtemprel;
foundtemprel = false;
tempNamespaceOid = LookupExplicitNamespace("pg_temp", true);
if (tempNamespaceOid == InvalidOid)
return false;
searchPath = fetch_search_path(true);
if (searchPath != NULL)
{
ListCell* i;
foreach(i, searchPath)
{
if (lfirst_oid(i) != tempNamespaceOid)
break;
tempRelOid = get_relname_relid(rv->relname, tempNamespaceOid);
if (tempRelOid != relOid)
break;
foundtemprel = true;
break;
}
list_free(searchPath);
}
if (!foundtemprel)
return false;
}
if (stmt->removeType != OBJECT_INDEX)
{
rel = relation_open(relOid, AccessExclusiveLock);
istemp = !ispermanent(rel->rd_rel->relpersistence);
relation_close(rel, NoLock);
}
else
{
rel = index_open(relOid, AccessExclusiveLock);
istemp = !ispermanent(rel->rd_rel->relpersistence);
index_close(rel, NoLock);
}
if (!istemp)
return false;
}
return true;
break;
}
case T_IndexStmt:
{
IndexStmt *stmt = (IndexStmt *) parsetree;
return !ispermanent(stmt->relation->relpersistence);
}
/* FIXME: Add more types of statements */
default:
break;
}
return false;
}
static bool
allowed_on_read_only_node(Node *parsetree)
{
/*
* This list is copied verbatim from check_xact_readonly
* we only do different action on it. */
switch (nodeTag(parsetree))
{
case T_AlterDatabaseStmt:
case T_AlterDatabaseSetStmt:
case T_AlterDomainStmt:
case T_AlterFunctionStmt:
case T_AlterRoleStmt:
case T_AlterRoleSetStmt:
case T_AlterObjectSchemaStmt:
case T_AlterOwnerStmt:
case T_AlterSeqStmt:
case T_AlterTableMoveAllStmt:
case T_AlterTableStmt:
case T_RenameStmt:
case T_CommentStmt:
case T_DefineStmt:
case T_CreateCastStmt:
case T_CreateEventTrigStmt:
case T_AlterEventTrigStmt:
case T_CreateConversionStmt:
case T_CreatedbStmt:
case T_CreateDomainStmt:
case T_CreateFunctionStmt:
case T_CreateRoleStmt:
case T_IndexStmt:
case T_CreatePLangStmt:
case T_CreateOpClassStmt:
case T_CreateOpFamilyStmt:
case T_AlterOpFamilyStmt:
case T_RuleStmt:
case T_CreateSchemaStmt:
case T_CreateSeqStmt:
case T_CreateStmt:
case T_CreateTableAsStmt:
case T_RefreshMatViewStmt:
case T_CreateTableSpaceStmt:
case T_CreateTrigStmt:
case T_CompositeTypeStmt:
case T_CreateEnumStmt:
case T_CreateRangeStmt:
case T_AlterEnumStmt:
case T_ViewStmt:
case T_DropStmt:
case T_DropdbStmt:
case T_DropTableSpaceStmt:
case T_DropRoleStmt:
case T_GrantStmt:
case T_GrantRoleStmt:
case T_AlterDefaultPrivilegesStmt:
case T_TruncateStmt:
case T_DropOwnedStmt:
case T_ReassignOwnedStmt:
case T_AlterTSDictionaryStmt:
case T_AlterTSConfigurationStmt:
case T_CreateExtensionStmt:
case T_AlterExtensionStmt:
case T_AlterExtensionContentsStmt:
case T_CreateFdwStmt:
case T_AlterFdwStmt:
case T_CreateForeignServerStmt:
case T_AlterForeignServerStmt:
case T_CreateUserMappingStmt:
case T_AlterUserMappingStmt:
case T_DropUserMappingStmt:
case T_AlterTableSpaceOptionsStmt:
case T_CreateForeignTableStmt:
case T_SecLabelStmt:
return statement_affects_only_nonpermanent(parsetree);
default:
/* do nothing */
break;
}
return true;
}
static void
bdr_commandfilter_dbname(const char *dbname)
{
if (strcmp(dbname, BDR_SUPERVISOR_DBNAME) == 0)
{
ereport(ERROR,
(errcode(ERRCODE_RESERVED_NAME),
errmsg("The BDR extension reserves the database name "
BDR_SUPERVISOR_DBNAME" for its own use"),
errhint("Use a different database name")));
}
}
static void
prevent_drop_extension_bdr(DropStmt *stmt)
{
ListCell *cell;
/* Only interested in DROP EXTENSION */
if (stmt->removeType != OBJECT_EXTENSION)
return;
/* Check to see if the BDR extension is being dropped */
foreach(cell, stmt->objects)
{
ObjectAddress address;
List *objname = lfirst(cell);
Relation relation = NULL;
/* Get an ObjectAddress for the object. */
address = get_object_address(stmt->removeType,
objname, NULL,
&relation,
AccessExclusiveLock,
stmt->missing_ok);
if (!OidIsValid(address.objectId))
continue;
/* for an extension the object name is unqualified */
Assert(list_length(objname) == 1);
if (strcmp(strVal(linitial(objname)), "bdr") == 0)
ereport(ERROR,
(errmsg("Dropping the BDR extension is prohibited while BDR is active"),
errhint("Part this node with bdr.part_by_node_names(...) first, or if appropriate use bdr.remove_bdr_from_local_node(...)")));
}
}
static void
bdr_commandfilter(Node *parsetree,
const char *queryString,
ProcessUtilityContext context,
ParamListInfo params,
DestReceiver *dest,
char *completionTag)
{
/* take strongest lock by default. */
BDRLockType lock_type = BDR_LOCK_WRITE;
/* don't filter in single user mode */
if (!IsUnderPostmaster)
goto done;
/* Only permit VACUUM on the supervisordb, if it exists */
if (BdrSupervisorDbOid == InvalidOid)
BdrSupervisorDbOid = bdr_get_supervisordb_oid(true);
if (BdrSupervisorDbOid != InvalidOid
&& MyDatabaseId == BdrSupervisorDbOid
&& nodeTag(parsetree) != T_VacuumStmt)
{
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("No commands may be run on the BDR supervisor database")));
}
/* extension contents aren't individually replicated */
if (creating_extension)
goto done;
/* don't perform filtering while replaying */
if (replication_origin_id != InvalidRepNodeId)
goto done;
/*
* Skip transaction control commands first as the following function
* calls might require transaction access.
*/
if (nodeTag(parsetree) == T_TransactionStmt)
goto done;
/* don't filter if this database isn't using bdr */
if (!bdr_is_bdr_activated_db(MyDatabaseId))
goto done;
/* check for read-only mode */
if (bdr_local_node_read_only() && !allowed_on_read_only_node(parsetree))
ereport(ERROR,
(errcode(ERRCODE_READ_ONLY_SQL_TRANSACTION),
errmsg("Cannot run %s on read-only BDR node.",
CreateCommandTag(parsetree))));
/* don't filter if explicitly told so */
if (bdr_permit_unsafe_commands)
goto done;
/* commands we skip (for now) */
switch (nodeTag(parsetree))
{
case T_PlannedStmt:
case T_ClosePortalStmt:
case T_FetchStmt:
case T_DoStmt:
case T_CreateTableSpaceStmt:
case T_DropTableSpaceStmt:
case T_AlterTableSpaceOptionsStmt:
case T_TruncateStmt:
case T_CommentStmt: /* XXX: we could replicate these */;
case T_CopyStmt:
case T_PrepareStmt:
case T_ExecuteStmt:
case T_DeallocateStmt:
case T_GrantStmt: /* XXX: we could replicate some of these these */;
case T_GrantRoleStmt:
case T_AlterDatabaseStmt:
case T_AlterDatabaseSetStmt:
case T_NotifyStmt:
case T_ListenStmt:
case T_UnlistenStmt:
case T_LoadStmt:
case T_ClusterStmt: /* XXX: we could replicate these */;
case T_VacuumStmt:
case T_ExplainStmt:
case T_AlterSystemStmt:
case T_VariableSetStmt:
case T_VariableShowStmt:
case T_DiscardStmt:
case T_CreateEventTrigStmt:
case T_AlterEventTrigStmt:
case T_CreateRoleStmt:
case T_AlterRoleStmt:
case T_AlterRoleSetStmt:
case T_DropRoleStmt:
case T_ReassignOwnedStmt:
case T_LockStmt:
case T_ConstraintsSetStmt:
case T_CheckPointStmt:
case T_ReindexStmt:
goto done;
default:
break;
}
/*
* We stop people from creating a DB named BDR_SUPERVISOR_DBNAME if the BDR
* extension is installed because we reserve that name, even if BDR isn't
* actually active.
*
*/
switch (nodeTag(parsetree))
{
case T_CreatedbStmt:
bdr_commandfilter_dbname(((CreatedbStmt*)parsetree)->dbname);
goto done;
case T_DropdbStmt:
bdr_commandfilter_dbname(((DropdbStmt*)parsetree)->dbname);
goto done;
case T_RenameStmt:
/*
* ALTER DATABASE ... RENAME TO ... is actually a RenameStmt not an
* AlterDatabaseStmt. It's handled here for the database target only
* then falls through for the other rename object type.
*/
if (((RenameStmt*)parsetree)->renameType == OBJECT_DATABASE)
{
bdr_commandfilter_dbname(((RenameStmt*)parsetree)->subname);
bdr_commandfilter_dbname(((RenameStmt*)parsetree)->newname);
}
break;
default:
break;
}
/* statements handled directly in standard_ProcessUtility */
switch (nodeTag(parsetree))
{
case T_DropStmt:
{
DropStmt *stmt = (DropStmt *) parsetree;
prevent_drop_extension_bdr(stmt);
if (EventTriggerSupportsObjectType(stmt->removeType))
break;
else
goto done;
}
case T_RenameStmt:
{
RenameStmt *stmt = (RenameStmt *) parsetree;
if (EventTriggerSupportsObjectType(stmt->renameType))
break;
else
goto done;
}
case T_AlterObjectSchemaStmt:
{
AlterObjectSchemaStmt *stmt = (AlterObjectSchemaStmt *) parsetree;
if (EventTriggerSupportsObjectType(stmt->objectType))
break;
else
goto done;
}
case T_AlterOwnerStmt:
{
AlterOwnerStmt *stmt = (AlterOwnerStmt *) parsetree;
lock_type = BDR_LOCK_DDL;
if (EventTriggerSupportsObjectType(stmt->objectType))
break;
else
goto done;
}
default:
break;
}
/* all commands handled by ProcessUtilitySlow() */
switch (nodeTag(parsetree))
{
case T_CreateSchemaStmt:
lock_type = BDR_LOCK_DDL;
break;
case T_CreateStmt:
filter_CreateStmt(parsetree, completionTag);
lock_type = BDR_LOCK_DDL;
break;
case T_CreateForeignTableStmt:
lock_type = BDR_LOCK_DDL;
break;
case T_AlterTableStmt:
filter_AlterTableStmt(parsetree, completionTag, queryString);
break;
case T_AlterDomainStmt:
/* XXX: we could support this */
error_unsupported_command(CreateCommandTag(parsetree));
break;
case T_DefineStmt:
{
DefineStmt *stmt = (DefineStmt *) parsetree;
switch (stmt->kind)
{
case OBJECT_AGGREGATE:
case OBJECT_OPERATOR:
case OBJECT_TYPE:
break;
default:
error_unsupported_command(CreateCommandTag(parsetree));
break;
}
lock_type = BDR_LOCK_DDL;
break;
}
case T_IndexStmt:
{
IndexStmt *stmt;
stmt = (IndexStmt *) parsetree;
if (stmt->whereClause && stmt->unique)
error_on_persistent_rv(stmt->relation,
"CREATE UNIQUE INDEX ... WHERE",
AccessExclusiveLock, false);
/*
* Non-unique concurrently built indexes can be done in
* parallel with writing.
*/
if (!stmt->unique && stmt->concurrent)
lock_type = BDR_LOCK_DDL;