Skip to content

Commit c36721e

Browse files
committed
pathman:
* fix ONLY statement * partition_data PL-function
1 parent 345ca19 commit c36721e

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

contrib/pathman/pathman.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ static void append_child_relation(PlannerInfo *root, RelOptInfo *rel, Index rti,
5252
static Node *wrapper_make_expression(WrapperNode *wrap, int index, bool *alwaysTrue);
5353
static void set_pathkeys(PlannerInfo *root, RelOptInfo *childrel, Path *path);
5454
static void disable_inheritance(Query *parse);
55+
bool inheritance_disabled;
5556

5657
static WrapperNode *walk_expr_tree(Expr *expr, const PartRelationInfo *prel);
5758
static int make_hash(const PartRelationInfo *prel, int value);
@@ -127,6 +128,7 @@ my_planner_hook(Query *parse, int cursorOptions, ParamListInfo boundParams)
127128
if (initialization_needed)
128129
init();
129130

131+
inheritance_disabled = false;
130132
disable_inheritance(parse);
131133
result = standard_planner(parse, cursorOptions, boundParams);
132134
return result;
@@ -157,7 +159,16 @@ disable_inheritance(Query *parse)
157159
prel = (PartRelationInfo *)
158160
hash_search(relations, (const void *) &rte->relid, HASH_FIND, 0);
159161
if (prel != NULL)
162+
{
160163
rte->inh = false;
164+
/*
165+
* Sometimes user uses the ONLY statement and in this case
166+
* rte->inh is also false. We should differ the case
167+
* when user uses ONLY statement from case when we
168+
* make rte->inh false intentionally.
169+
*/
170+
inheritance_disabled = true;
171+
}
161172
}
162173
break;
163174
case RTE_SUBQUERY:
@@ -198,7 +209,7 @@ my_hook(PlannerInfo *root, RelOptInfo *rel, Index rti, RangeTblEntry *rte)
198209
PartRelationInfo *prel = NULL;
199210

200211
/* This works on for SELECT queries */
201-
if (root->parse->commandType != CMD_SELECT)
212+
if (root->parse->commandType != CMD_SELECT || !inheritance_disabled)
202213
return;
203214

204215
/* Lookup partitioning information for parent relation */

contrib/pathman/sql/init.sql

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ CREATE TABLE IF NOT EXISTS @extschema@.pg_pathman_rels (
55
id SERIAL PRIMARY KEY,
66
relname VARCHAR(127),
77
attname VARCHAR(127),
8-
atttype INTEGER,
8+
-- atttype INTEGER,
99
parttype INTEGER
1010
);
1111

@@ -169,6 +169,34 @@ RETURNS VOID AS 'pathman', 'on_partitions_removed' LANGUAGE C STRICT;
169169
CREATE OR REPLACE FUNCTION find_range_partition(relid OID, value ANYELEMENT)
170170
RETURNS OID AS 'pathman', 'find_range_partition' LANGUAGE C STRICT;
171171

172+
CREATE OR REPLACE FUNCTION partition_data(p_parent text)
173+
RETURNS bigint AS
174+
$$
175+
DECLARE
176+
rec RECORD;
177+
BEGIN
178+
FOR rec IN (SELECT child.relname, pg_constraint.consrc
179+
FROM pg_pathman_rels
180+
JOIN pg_class AS parent ON parent.relname = pg_pathman_rels.relname
181+
JOIN pg_inherits ON inhparent = parent.relfilenode
182+
JOIN pg_constraint ON conrelid = inhrelid AND contype='c'
183+
JOIN pg_class AS child ON child.relfilenode = inhrelid
184+
WHERE pg_pathman_rels.relname = p_parent)
185+
LOOP
186+
RAISE NOTICE 'child %, condition %', rec.relname, rec.consrc;
187+
188+
EXECUTE format('WITH part_data AS (
189+
DELETE FROM ONLY %s WHERE %s RETURNING *)
190+
INSERT INTO %s SELECT * FROM part_data'
191+
, p_parent
192+
, rec.consrc
193+
, rec.relname);
194+
END LOOP;
195+
RETURN 0;
196+
END
197+
$$ LANGUAGE plpgsql;
198+
199+
172200
-- CREATE OR REPLACE FUNCTION sample_rel_trigger_func()
173201
-- RETURNS TRIGGER AS $$
174202
-- DECLARE

0 commit comments

Comments
 (0)