Skip to content

Commit 275d5aa

Browse files
committed
refactoring, improved append_child_relation(): make it act a lot more like postgres does, remove obsolete functions (change_varnos() etc), add safety regression test
1 parent f603e6c commit 275d5aa

File tree

10 files changed

+280
-320
lines changed

10 files changed

+280
-320
lines changed

expected/pathman_basic.out

Lines changed: 61 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -268,16 +268,6 @@ EXPLAIN (COSTS OFF) SELECT * FROM test.hash_rel WHERE value = 2 OR value = 1;
268268
Filter: (value = 1)
269269
(5 rows)
270270

271-
-- Temporarily commented out
272-
-- EXPLAIN (COSTS OFF) SELECT * FROM test.hash_rel WHERE value BETWEEN 1 AND 2;
273-
-- QUERY PLAN
274-
-- -------------------------------------------------
275-
-- Append
276-
-- -> Seq Scan on hash_rel_1
277-
-- Filter: ((value >= 1) AND (value <= 2))
278-
-- -> Seq Scan on hash_rel_2
279-
-- Filter: ((value >= 1) AND (value <= 2))
280-
-- (5 rows)
281271
EXPLAIN (COSTS OFF) SELECT * FROM test.num_range_rel WHERE id > 2500;
282272
QUERY PLAN
283273
-----------------------------------
@@ -1217,7 +1207,7 @@ SELECT pathman.create_partitions_from_range('test."RangeRel"', 'id', 1, 300, 100
12171207
DROP TABLE test."RangeRel" CASCADE;
12181208
NOTICE: drop cascades to 3 other objects
12191209
DROP EXTENSION pg_pathman;
1220-
/* Test that everithing works fine without schemas */
1210+
/* Test that everything works fine without schemas */
12211211
CREATE EXTENSION pg_pathman;
12221212
/* Hash */
12231213
CREATE TABLE hash_rel (
@@ -1405,6 +1395,7 @@ EXPLAIN (COSTS OFF) SELECT * FROM range_rel WHERE dt = '2015-12-15';
14051395
Filter: (dt = 'Tue Dec 15 00:00:00 2015'::timestamp without time zone)
14061396
(3 rows)
14071397

1398+
/* Test foreign keys */
14081399
CREATE TABLE messages(id SERIAL PRIMARY KEY, msg TEXT);
14091400
CREATE TABLE replies(id SERIAL PRIMARY KEY, message_id INTEGER REFERENCES messages(id), msg TEXT);
14101401
INSERT INTO messages SELECT g, md5(g::text) FROM generate_series(1, 10) as g;
@@ -1428,6 +1419,65 @@ EXPLAIN (COSTS OFF) SELECT * FROM messages;
14281419
-> Seq Scan on messages_2
14291420
(3 rows)
14301421

1422+
DROP TABLE messages, replies CASCADE;
1423+
NOTICE: drop cascades to 2 other objects
1424+
/* Special test case (quals generation) -- fixing commit f603e6c5 */
1425+
CREATE TABLE special_case_1_ind_o_s(val serial, comment text);
1426+
INSERT INTO special_case_1_ind_o_s SELECT generate_series(1, 200), NULL;
1427+
SELECT create_range_partitions('special_case_1_ind_o_s', 'val', 1, 50);
1428+
NOTICE: sequence "special_case_1_ind_o_s_seq" does not exist, skipping
1429+
create_range_partitions
1430+
-------------------------
1431+
4
1432+
(1 row)
1433+
1434+
INSERT INTO special_case_1_ind_o_s_2 SELECT 75 FROM generate_series(1, 6000);
1435+
CREATE INDEX ON special_case_1_ind_o_s_2 (val, comment);
1436+
VACUUM ANALYZE special_case_1_ind_o_s_2;
1437+
EXPLAIN (COSTS OFF) SELECT * FROM special_case_1_ind_o_s WHERE val < 75 AND comment = 'a';
1438+
QUERY PLAN
1439+
--------------------------------------------------------------------------------------------------
1440+
Append
1441+
-> Seq Scan on special_case_1_ind_o_s_1
1442+
Filter: (comment = 'a'::text)
1443+
-> Index Only Scan using special_case_1_ind_o_s_2_val_comment_idx on special_case_1_ind_o_s_2
1444+
Index Cond: ((val < 75) AND (comment = 'a'::text))
1445+
(5 rows)
1446+
1447+
SELECT set_enable_parent('special_case_1_ind_o_s', true);
1448+
set_enable_parent
1449+
-------------------
1450+
1451+
(1 row)
1452+
1453+
EXPLAIN (COSTS OFF) SELECT * FROM special_case_1_ind_o_s WHERE val < 75 AND comment = 'a';
1454+
QUERY PLAN
1455+
--------------------------------------------------------------------------------------------------
1456+
Append
1457+
-> Seq Scan on special_case_1_ind_o_s
1458+
Filter: ((val < 75) AND (comment = 'a'::text))
1459+
-> Seq Scan on special_case_1_ind_o_s_1
1460+
Filter: (comment = 'a'::text)
1461+
-> Index Only Scan using special_case_1_ind_o_s_2_val_comment_idx on special_case_1_ind_o_s_2
1462+
Index Cond: ((val < 75) AND (comment = 'a'::text))
1463+
(7 rows)
1464+
1465+
SELECT set_enable_parent('special_case_1_ind_o_s', false);
1466+
set_enable_parent
1467+
-------------------
1468+
1469+
(1 row)
1470+
1471+
EXPLAIN (COSTS OFF) SELECT * FROM special_case_1_ind_o_s WHERE val < 75 AND comment = 'a';
1472+
QUERY PLAN
1473+
--------------------------------------------------------------------------------------------------
1474+
Append
1475+
-> Seq Scan on special_case_1_ind_o_s_1
1476+
Filter: (comment = 'a'::text)
1477+
-> Index Only Scan using special_case_1_ind_o_s_2_val_comment_idx on special_case_1_ind_o_s_2
1478+
Index Cond: ((val < 75) AND (comment = 'a'::text))
1479+
(5 rows)
1480+
14311481
DROP SCHEMA test CASCADE;
14321482
NOTICE: drop cascades to 13 other objects
14331483
DROP EXTENSION pg_pathman CASCADE;

sql/pathman_basic.sql

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -82,16 +82,7 @@ SET enable_seqscan = ON;
8282
EXPLAIN (COSTS OFF) SELECT * FROM test.hash_rel;
8383
EXPLAIN (COSTS OFF) SELECT * FROM test.hash_rel WHERE value = 2;
8484
EXPLAIN (COSTS OFF) SELECT * FROM test.hash_rel WHERE value = 2 OR value = 1;
85-
-- Temporarily commented out
86-
-- EXPLAIN (COSTS OFF) SELECT * FROM test.hash_rel WHERE value BETWEEN 1 AND 2;
87-
-- QUERY PLAN
88-
-- -------------------------------------------------
89-
-- Append
90-
-- -> Seq Scan on hash_rel_1
91-
-- Filter: ((value >= 1) AND (value <= 2))
92-
-- -> Seq Scan on hash_rel_2
93-
-- Filter: ((value >= 1) AND (value <= 2))
94-
-- (5 rows)
85+
9586
EXPLAIN (COSTS OFF) SELECT * FROM test.num_range_rel WHERE id > 2500;
9687
EXPLAIN (COSTS OFF) SELECT * FROM test.num_range_rel WHERE id >= 1000 AND id < 3000;
9788
EXPLAIN (COSTS OFF) SELECT * FROM test.num_range_rel WHERE id >= 1500 AND id < 2500;
@@ -330,7 +321,8 @@ DROP TABLE test."RangeRel" CASCADE;
330321

331322
DROP EXTENSION pg_pathman;
332323

333-
/* Test that everithing works fine without schemas */
324+
325+
/* Test that everything works fine without schemas */
334326
CREATE EXTENSION pg_pathman;
335327

336328
/* Hash */
@@ -378,6 +370,7 @@ SELECT drop_partitions('range_rel', TRUE);
378370
SELECT create_partitions_from_range('range_rel', 'dt', '2015-01-01'::date, '2015-12-01'::date, '1 month'::interval);
379371
EXPLAIN (COSTS OFF) SELECT * FROM range_rel WHERE dt = '2015-12-15';
380372

373+
/* Test foreign keys */
381374
CREATE TABLE messages(id SERIAL PRIMARY KEY, msg TEXT);
382375
CREATE TABLE replies(id SERIAL PRIMARY KEY, message_id INTEGER REFERENCES messages(id), msg TEXT);
383376
INSERT INTO messages SELECT g, md5(g::text) FROM generate_series(1, 10) as g;
@@ -386,6 +379,20 @@ SELECT create_range_partitions('messages', 'id', 1, 100, 2);
386379
ALTER TABLE replies DROP CONSTRAINT replies_message_id_fkey;
387380
SELECT create_range_partitions('messages', 'id', 1, 100, 2);
388381
EXPLAIN (COSTS OFF) SELECT * FROM messages;
382+
DROP TABLE messages, replies CASCADE;
383+
384+
/* Special test case (quals generation) -- fixing commit f603e6c5 */
385+
CREATE TABLE special_case_1_ind_o_s(val serial, comment text);
386+
INSERT INTO special_case_1_ind_o_s SELECT generate_series(1, 200), NULL;
387+
SELECT create_range_partitions('special_case_1_ind_o_s', 'val', 1, 50);
388+
INSERT INTO special_case_1_ind_o_s_2 SELECT 75 FROM generate_series(1, 6000);
389+
CREATE INDEX ON special_case_1_ind_o_s_2 (val, comment);
390+
VACUUM ANALYZE special_case_1_ind_o_s_2;
391+
EXPLAIN (COSTS OFF) SELECT * FROM special_case_1_ind_o_s WHERE val < 75 AND comment = 'a';
392+
SELECT set_enable_parent('special_case_1_ind_o_s', true);
393+
EXPLAIN (COSTS OFF) SELECT * FROM special_case_1_ind_o_s WHERE val < 75 AND comment = 'a';
394+
SELECT set_enable_parent('special_case_1_ind_o_s', false);
395+
EXPLAIN (COSTS OFF) SELECT * FROM special_case_1_ind_o_s WHERE val < 75 AND comment = 'a';
389396

390397

391398
DROP SCHEMA test CASCADE;

src/hooks.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -308,26 +308,26 @@ pathman_rel_pathlist_hook(PlannerInfo *root,
308308

309309
/* Add parent if needed */
310310
if (prel->enable_parent)
311-
append_child_relation(root, rel, rti, rte, 0, rte->relid, NULL);
311+
append_child_relation(root, rti, 0, rte->relid, NULL);
312312

313313
/*
314314
* Iterate all indexes in rangeset and append corresponding child
315315
* relations.
316316
*/
317317
foreach(lc, ranges)
318318
{
319-
IndexRange irange = lfirst_irange(lc);
319+
IndexRange irange = lfirst_irange(lc);
320320

321321
for (i = irange.ir_lower; i <= irange.ir_upper; i++)
322-
append_child_relation(root, rel, rti, rte, i, children[i], wrappers);
322+
append_child_relation(root, rti, i, children[i], wrappers);
323323
}
324324

325325
/* Clear old path list */
326326
list_free(rel->pathlist);
327327

328328
rel->pathlist = NIL;
329-
set_append_rel_pathlist(root, rel, rti, rte, pathkeyAsc, pathkeyDesc);
330-
set_append_rel_size_compat(root, rel, rti, rte);
329+
set_append_rel_pathlist(root, rel, rti, pathkeyAsc, pathkeyDesc);
330+
set_append_rel_size_compat(root, rel, rti);
331331

332332
/* No need to go further (both nodes are disabled), return */
333333
if (!(pg_pathman_enable_runtimeappend ||

src/pathman.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,8 @@ extern List *inheritance_disabled_relids;
123123
extern PathmanState *pmstate;
124124

125125

126-
int append_child_relation(PlannerInfo *root, RelOptInfo *rel, Index rti,
127-
RangeTblEntry *rte, int index, Oid childOID, List *wrappers);
126+
int append_child_relation(PlannerInfo *root, Index parent_rti,
127+
int ir_index, Oid child_oid, List *wrappers);
128128

129129
search_rangerel_result search_range_partition_eq(const Datum value,
130130
FmgrInfo *cmp_func,
@@ -142,8 +142,7 @@ void disable_inheritance_subselect(Query *parse);
142142
void set_append_rel_size(PlannerInfo *root, RelOptInfo *rel,
143143
Index rti, RangeTblEntry *rte);
144144
void set_append_rel_pathlist(PlannerInfo *root, RelOptInfo *rel, Index rti,
145-
RangeTblEntry *rte, PathKey *pathkeyAsc,
146-
PathKey *pathkeyDesc);
145+
PathKey *pathkeyAsc, PathKey *pathkeyDesc);
147146

148147
typedef struct
149148
{

src/pg_compat.c

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@
1111
#include "pg_compat.h"
1212

1313
#include "optimizer/pathnode.h"
14+
#include "optimizer/prep.h"
1415
#include "port.h"
1516
#include "utils.h"
1617

1718
#include <math.h>
1819

1920

2021
void
21-
set_append_rel_size_compat(PlannerInfo *root, RelOptInfo *rel,
22-
Index rti, RangeTblEntry *rte)
22+
set_append_rel_size_compat(PlannerInfo *root, RelOptInfo *rel, Index rti)
2323
{
2424
double parent_rows = 0;
2525
double parent_size = 0;
@@ -63,31 +63,21 @@ set_append_rel_size_compat(PlannerInfo *root, RelOptInfo *rel,
6363
rel->tuples = parent_rows;
6464
}
6565

66-
extern
67-
void copy_targetlist_compat(RelOptInfo *dest, RelOptInfo *rel)
66+
void
67+
adjust_targetlist_compat(PlannerInfo *root, RelOptInfo *dest,
68+
RelOptInfo *rel, AppendRelInfo *appinfo)
6869
{
69-
ListCell *lc;
70-
71-
#if PG_VERSION_NUM >= 90600
72-
dest->reltarget->exprs = NIL;
73-
foreach(lc, rel->reltarget->exprs)
74-
#else
75-
dest->reltargetlist = NIL;
76-
foreach(lc, rel->reltargetlist)
77-
#endif
78-
{
79-
Node *new_target;
80-
Node *node;
81-
82-
node = (Node *) lfirst(lc);
83-
new_target = copyObject(node);
84-
change_varnos(new_target, rel->relid, dest->relid);
8570
#if PG_VERSION_NUM >= 90600
86-
dest->reltarget->exprs = lappend(dest->reltarget->exprs, new_target);
71+
dest->reltarget->exprs = (List *)
72+
adjust_appendrel_attrs(root,
73+
(Node *) rel->reltarget->exprs,
74+
appinfo);
8775
#else
88-
dest->reltargetlist = lappend(dest->reltargetlist, new_target);
76+
dest->reltargetlist = (List *)
77+
adjust_appendrel_attrs(root,
78+
(Node *) rel->reltargetlist,
79+
appinfo);
8980
#endif
90-
}
9181
}
9282

9383
#if PG_VERSION_NUM >= 90600

src/pg_compat.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919
#include "optimizer/paths.h"
2020

2121

22-
extern void set_append_rel_size_compat(PlannerInfo *root, RelOptInfo *rel,
23-
Index rti, RangeTblEntry *rte);
24-
extern void copy_targetlist_compat(RelOptInfo *dest, RelOptInfo *rel);
22+
void set_append_rel_size_compat(PlannerInfo *root, RelOptInfo *rel, Index rti);
23+
void adjust_targetlist_compat(PlannerInfo *root, RelOptInfo *dest,
24+
RelOptInfo *rel, AppendRelInfo *appinfo);
2525

2626
#if PG_VERSION_NUM >= 90600
2727

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy