Skip to content

Commit be854e7

Browse files
committed
auto trigger creation on create_hash_partitions() and create_range_partitions() if upper parent has trigger
1 parent 9c167fe commit be854e7

File tree

5 files changed

+69
-20
lines changed

5 files changed

+69
-20
lines changed

hash.sql

+13
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ CREATE OR REPLACE FUNCTION @extschema@.create_hash_partitions(
2020
tablespaces TEXT[] DEFAULT NULL)
2121
RETURNS INTEGER AS
2222
$$
23+
DECLARE
24+
v_upper_parent REGCLASS;
25+
2326
BEGIN
2427
PERFORM @extschema@.validate_relname(parent_relid);
2528

@@ -45,6 +48,16 @@ BEGIN
4548
partition_names,
4649
tablespaces);
4750

51+
/*
52+
* If there is an upper level parent partitioned by pg_pathman and it has
53+
* update triggers then create them too
54+
*/
55+
v_upper_parent = @extschema@.get_parent_of_partition(parent_relid, false);
56+
IF NOT v_upper_parent IS NULL AND @extschema@.has_update_trigger(v_upper_parent)
57+
THEN
58+
PERFORM @extschema@.create_update_triggers(parent_relid);
59+
END IF;
60+
4861
/* Copy data */
4962
IF partition_data = true THEN
5063
PERFORM @extschema@.set_enable_parent(parent_relid, false);

init.sql

+3-2
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,8 @@ LANGUAGE plpgsql;
533533
* Drop triggers
534534
*/
535535
CREATE OR REPLACE FUNCTION @extschema@.drop_triggers(
536-
parent_relid REGCLASS)
536+
parent_relid REGCLASS,
537+
force BOOL DEFAULT FALSE)
537538
RETURNS VOID AS 'pg_pathman', 'drop_update_triggers'
538539
LANGUAGE C STRICT;
539540

@@ -560,7 +561,7 @@ BEGIN
560561
PERFORM @extschema@.prevent_relation_modification(parent_relid);
561562

562563
/* First, drop all triggers */
563-
PERFORM @extschema@.drop_triggers(parent_relid);
564+
PERFORM @extschema@.drop_triggers(parent_relid, TRUE);
564565

565566
SELECT count(*) FROM @extschema@.pathman_config
566567
WHERE partrel = parent_relid INTO conf_num;

range.sql

+22
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ DECLARE
103103
end_value start_value%TYPE;
104104
part_count INTEGER := 0;
105105
i INTEGER;
106+
v_upper_parent REGCLASS;
106107

107108
BEGIN
108109
expression := lower(expression);
@@ -171,6 +172,16 @@ BEGIN
171172
NULL);
172173
END IF;
173174

175+
/*
176+
* If there is an upper level parent partitioned by pg_pathman and it has
177+
* update triggers then create them too
178+
*/
179+
v_upper_parent = @extschema@.get_parent_of_partition(parent_relid, false);
180+
IF NOT v_upper_parent IS NULL AND @extschema@.has_update_trigger(v_upper_parent)
181+
THEN
182+
PERFORM @extschema@.create_update_triggers(parent_relid);
183+
END IF;
184+
174185
/* Relocate data if asked to */
175186
IF partition_data = true THEN
176187
PERFORM @extschema@.set_enable_parent(parent_relid, false);
@@ -202,6 +213,7 @@ DECLARE
202213
end_value start_value%TYPE;
203214
part_count INTEGER := 0;
204215
i INTEGER;
216+
v_upper_parent REGCLASS;
205217

206218
BEGIN
207219
expression := lower(expression);
@@ -267,6 +279,16 @@ BEGIN
267279
NULL);
268280
END IF;
269281

282+
/*
283+
* If there is an upper level parent partitioned by pg_pathman and it has
284+
* update triggers then create them too
285+
*/
286+
v_upper_parent = @extschema@.get_parent_of_partition(parent_relid, false);
287+
IF NOT v_upper_parent IS NULL AND @extschema@.has_update_trigger(v_upper_parent)
288+
THEN
289+
PERFORM @extschema@.create_update_triggers(parent_relid);
290+
END IF;
291+
270292
/* Relocate data if asked to */
271293
IF partition_data = true THEN
272294
PERFORM @extschema@.set_enable_parent(parent_relid, false);

src/partition_creation.c

+14-5
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "catalog/pg_trigger.h"
2626
#include "catalog/pg_type.h"
2727
#include "catalog/toasting.h"
28+
#include "commands/defrem.h"
2829
#include "commands/event_trigger.h"
2930
#include "commands/sequence.h"
3031
#include "commands/tablecmds.h"
@@ -1823,9 +1824,17 @@ void
18231824
drop_single_update_trigger_internal(Oid relid,
18241825
const char *trigname)
18251826
{
1826-
Oid trigoid;
1827-
1828-
trigoid = get_trigger_oid(relid, trigname, true);
1829-
if (OidIsValid(trigoid))
1830-
RemoveTriggerById(trigoid);
1827+
DropStmt *n = makeNode(DropStmt);
1828+
const char *relname = get_qualified_rel_name(relid);
1829+
List *namelist = stringToQualifiedNameList(relname);
1830+
1831+
namelist = lappend(namelist, makeString((char *) trigname));
1832+
n->removeType = OBJECT_TRIGGER;
1833+
n->missing_ok = true;
1834+
n->objects = list_make1(namelist);
1835+
n->arguments = NIL;
1836+
n->behavior = DROP_RESTRICT; /* default behavior */
1837+
n->concurrent = false;
1838+
1839+
RemoveObjects(n);
18311840
}

src/pl_funcs.c

+17-13
Original file line numberDiff line numberDiff line change
@@ -1607,20 +1607,24 @@ drop_update_triggers(PG_FUNCTION_ARGS)
16071607
Oid relid = PG_GETARG_OID(0),
16081608
parent;
16091609
PartParentSearch parent_search;
1610+
bool force = PG_GETARG_BOOL(1);
16101611

1611-
/*
1612-
* We can drop triggers only if relid is the topmost parent table (or if
1613-
* its parent doesn't have update triggers (which should never happen in
1614-
* the ideal world)
1615-
*/
1616-
parent = get_parent_of_partition(relid, &parent_search);
1617-
if (parent_search == PPS_ENTRY_PART_PARENT)
1618-
if(has_update_trigger_internal(parent))
1619-
ereport(ERROR,
1620-
(errmsg("Parent table must not have an update trigger"),
1621-
errhint("Try to perform SELECT %s.drop_triggers('%s');",
1622-
get_namespace_name(get_pathman_schema()),
1623-
get_qualified_rel_name(parent))));
1612+
if (!force)
1613+
{
1614+
/*
1615+
* We can drop triggers only if relid is the topmost parent table (or if
1616+
* its parent doesn't have update triggers (which should never happen in
1617+
* the ideal world)
1618+
*/
1619+
parent = get_parent_of_partition(relid, &parent_search);
1620+
if (parent_search == PPS_ENTRY_PART_PARENT)
1621+
if(has_update_trigger_internal(parent))
1622+
ereport(ERROR,
1623+
(errmsg("Parent table must not have an update trigger"),
1624+
errhint("Try to perform SELECT %s.drop_triggers('%s');",
1625+
get_namespace_name(get_pathman_schema()),
1626+
get_qualified_rel_name(parent))));
1627+
}
16241628

16251629
/* Recursively drop triggers */
16261630
drop_update_triggers_internal(relid);

0 commit comments

Comments
 (0)