Skip to content

Commit ef7f55f

Browse files
committed
Merge branch 'master' into rel_future_beta
2 parents ea98072 + e98dd7b commit ef7f55f

File tree

5 files changed

+84
-22
lines changed

5 files changed

+84
-22
lines changed

src/hooks.c

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "catalog/pg_authid.h"
3030
#include "miscadmin.h"
3131
#include "optimizer/cost.h"
32+
#include "optimizer/prep.h"
3233
#include "optimizer/restrictinfo.h"
3334
#include "rewrite/rewriteManip.h"
3435
#include "utils/typcache.h"
@@ -290,6 +291,7 @@ pathman_rel_pathlist_hook(PlannerInfo *root,
290291
if ((prel = get_pathman_relation_info(rte->relid)) != NULL)
291292
{
292293
Relation parent_rel; /* parent's relation (heap) */
294+
PlanRowMark *parent_rowmark; /* parent's rowmark */
293295
Oid *children; /* selected children oids */
294296
List *ranges, /* a list of IndexRanges */
295297
*wrappers; /* a list of WrapperNodes */
@@ -305,6 +307,9 @@ pathman_rel_pathlist_hook(PlannerInfo *root,
305307
/* Make copy of partitioning expression and fix Var's varno attributes */
306308
part_expr = PrelExpressionForRelid(prel, rti);
307309

310+
/* Get partitioning-related clauses (do this before append_child_relation()) */
311+
part_clauses = get_partitioning_clauses(rel->baserestrictinfo, prel, rti);
312+
308313
if (prel->parttype == PT_RANGE)
309314
{
310315
/*
@@ -382,19 +387,25 @@ pathman_rel_pathlist_hook(PlannerInfo *root,
382387
/* Parent has already been locked by rewriter */
383388
parent_rel = heap_open(rte->relid, NoLock);
384389

385-
/* Add parent if asked to */
386-
if (prel->enable_parent)
387-
append_child_relation(root, parent_rel, rti, 0, rte->relid, NULL);
390+
parent_rowmark = get_plan_rowmark(root->rowMarks, rti);
388391

389392
/*
390-
* Iterate all indexes in rangeset and append corresponding child relations.
393+
* WARNING: 'prel' might become invalid after append_child_relation().
391394
*/
395+
396+
/* Add parent if asked to */
397+
if (prel->enable_parent)
398+
append_child_relation(root, parent_rel, parent_rowmark,
399+
rti, 0, rte->relid, NULL);
400+
401+
/* Iterate all indexes in rangeset and append child relations */
392402
foreach(lc, ranges)
393403
{
394404
IndexRange irange = lfirst_irange(lc);
395405

396406
for (i = irange_lower(irange); i <= irange_upper(irange); i++)
397-
append_child_relation(root, parent_rel, rti, i, children[i], wrappers);
407+
append_child_relation(root, parent_rel, parent_rowmark,
408+
rti, i, children[i], wrappers);
398409
}
399410

400411
/* Now close parent relation */
@@ -424,9 +435,6 @@ pathman_rel_pathlist_hook(PlannerInfo *root,
424435
pg_pathman_enable_runtime_merge_append))
425436
return;
426437

427-
/* Get partitioning-related clauses */
428-
part_clauses = get_partitioning_clauses(rel->baserestrictinfo, prel, rti);
429-
430438
/* Skip if there's no PARAMs in partitioning-related clauses */
431439
if (!clause_contains_params((Node *) part_clauses))
432440
return;

src/include/compat/pg_compat.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "compat/debug_compat_features.h"
2323

2424
#include "postgres.h"
25+
#include "commands/trigger.h"
2526
#include "executor/executor.h"
2627
#include "nodes/memnodes.h"
2728
#include "nodes/relation.h"
@@ -562,6 +563,33 @@ extern AttrNumber *convert_tuples_by_name_map(TupleDesc indesc,
562563
#endif
563564

564565

566+
/*
567+
* ExecARInsertTriggers()
568+
*/
569+
#if PG_VERSION_NUM >= 100000
570+
#define ExecARInsertTriggersCompat(estate, relinfo, trigtuple, \
571+
recheck_indexes, transition_capture) \
572+
ExecARInsertTriggers((estate), (relinfo), (trigtuple), \
573+
(recheck_indexes), (transition_capture))
574+
#elif PG_VERSION_NUM >= 90500
575+
#define ExecARInsertTriggersCompat(estate, relinfo, trigtuple, \
576+
recheck_indexes, transition_capture) \
577+
ExecARInsertTriggers((estate), (relinfo), (trigtuple), (recheck_indexes))
578+
#endif
579+
580+
581+
/*
582+
* ExecASInsertTriggers()
583+
*/
584+
#if PG_VERSION_NUM >= 100000
585+
#define ExecASInsertTriggersCompat(estate, relinfo, transition_capture) \
586+
ExecASInsertTriggers((estate), (relinfo), (transition_capture))
587+
#elif PG_VERSION_NUM >= 90500
588+
#define ExecASInsertTriggersCompat(estate, relinfo, transition_capture) \
589+
ExecASInsertTriggers((estate), (relinfo))
590+
#endif
591+
592+
565593

566594
/*
567595
* -------------

src/include/pathman.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,12 @@ Oid get_pathman_config_params_relid(bool invalid_is_ok);
105105
/*
106106
* Create RelOptInfo & RTE for a selected partition.
107107
*/
108-
Index append_child_relation(PlannerInfo *root, Relation parent_relation,
109-
Index parent_rti, int ir_index, Oid child_oid,
108+
Index append_child_relation(PlannerInfo *root,
109+
Relation parent_relation,
110+
PlanRowMark *parent_rowmark,
111+
Index parent_rti,
112+
int ir_index,
113+
Oid child_oid,
110114
List *wrappers);
111115

112116

src/pg_pathman.c

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@
3030
#include "optimizer/restrictinfo.h"
3131
#include "optimizer/cost.h"
3232
#include "utils/datum.h"
33-
#include "utils/lsyscache.h"
3433
#include "utils/rel.h"
34+
#include "utils/lsyscache.h"
35+
#include "utils/syscache.h"
3536
#include "utils/selfuncs.h"
3637
#include "utils/typcache.h"
3738

@@ -364,8 +365,12 @@ get_pathman_config_params_relid(bool invalid_is_ok)
364365
* NOTE: partially based on the expand_inherited_rtentry() function.
365366
*/
366367
Index
367-
append_child_relation(PlannerInfo *root, Relation parent_relation,
368-
Index parent_rti, int ir_index, Oid child_oid,
368+
append_child_relation(PlannerInfo *root,
369+
Relation parent_relation,
370+
PlanRowMark *parent_rowmark,
371+
Index parent_rti,
372+
int ir_index,
373+
Oid child_oid,
369374
List *wrappers)
370375
{
371376
RangeTblEntry *parent_rte,
@@ -375,17 +380,35 @@ append_child_relation(PlannerInfo *root, Relation parent_relation,
375380
Relation child_relation;
376381
AppendRelInfo *appinfo;
377382
Index childRTindex;
378-
PlanRowMark *parent_rowmark,
379-
*child_rowmark;
383+
PlanRowMark *child_rowmark;
380384
Node *childqual;
381385
List *childquals;
382386
ListCell *lc1,
383387
*lc2;
388+
LOCKMODE lockmode;
389+
390+
/* Choose a correct lock mode */
391+
if (parent_rti == root->parse->resultRelation)
392+
lockmode = RowExclusiveLock;
393+
else if (parent_rowmark && RowMarkRequiresRowShareLock(parent_rowmark->markType))
394+
lockmode = RowShareLock;
395+
else
396+
lockmode = AccessShareLock;
397+
398+
/* Acquire a suitable lock on partition */
399+
LockRelationOid(child_oid, lockmode);
400+
401+
/* Check that partition exists */
402+
if (!SearchSysCacheExists1(RELOID, ObjectIdGetDatum(child_oid)))
403+
{
404+
UnlockRelationOid(child_oid, lockmode);
405+
return 0;
406+
}
384407

385408
parent_rel = root->simple_rel_array[parent_rti];
386409
parent_rte = root->simple_rte_array[parent_rti];
387410

388-
/* FIXME: acquire a suitable lock on partition */
411+
/* Open child relation (we've just locked it) */
389412
child_relation = heap_open(child_oid, NoLock);
390413

391414
/* Create RangeTblEntry for child relation */
@@ -408,7 +431,6 @@ append_child_relation(PlannerInfo *root, Relation parent_relation,
408431

409432

410433
/* Create rowmarks required for child rels */
411-
parent_rowmark = get_plan_rowmark(root->rowMarks, parent_rti);
412434
if (parent_rowmark)
413435
{
414436
child_rowmark = makeNode(PlanRowMark);

src/utility_stmt_hooking.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -724,9 +724,9 @@ PathmanCopyFrom(CopyState cstate, Relation parent_rel,
724724
recheckIndexes = ExecInsertIndexTuples(slot, &(tuple->t_self),
725725
estate, false, NULL, NIL);
726726

727-
/* AFTER ROW INSERT Triggers */
728-
ExecARInsertTriggers(estate, child_result_rel, tuple,
729-
recheckIndexes);
727+
/* AFTER ROW INSERT Triggers (FIXME: NULL transition) */
728+
ExecARInsertTriggersCompat(estate, child_result_rel, tuple,
729+
recheckIndexes, NULL);
730730

731731
list_free(recheckIndexes);
732732

@@ -748,8 +748,8 @@ PathmanCopyFrom(CopyState cstate, Relation parent_rel,
748748
if (old_protocol)
749749
pq_endmsgread();
750750

751-
/* Execute AFTER STATEMENT insertion triggers */
752-
ExecASInsertTriggers(estate, parent_result_rel);
751+
/* Execute AFTER STATEMENT insertion triggers (FIXME: NULL transition) */
752+
ExecASInsertTriggersCompat(estate, parent_result_rel, NULL);
753753

754754
/* Handle queued AFTER triggers */
755755
AfterTriggerEndQuery(estate);

0 commit comments

Comments
 (0)