Skip to content

Commit 8e24c01

Browse files
committed
tiny hack: bind custom data to EState using MemoryContext callbacks (estate_mod_data), refactoring, check that partition exists when INSERTing
1 parent 348c507 commit 8e24c01

File tree

4 files changed

+227
-139
lines changed

4 files changed

+227
-139
lines changed

expected/pg_pathman.out

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1267,7 +1267,7 @@ SELECT pathman.disable_auto('test.range_rel');
12671267
(1 row)
12681268

12691269
INSERT INTO test.range_rel (dt) VALUES ('2015-06-01');
1270-
ERROR: There is no suitable partition for key 'Mon Jun 01 00:00:00 2015'
1270+
ERROR: no suitable partition for key 'Mon Jun 01 00:00:00 2015'
12711271
SELECT pathman.enable_auto('test.range_rel');
12721272
enable_auto
12731273
-------------

src/copy_stmt_hooking.c

Lines changed: 9 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,6 @@ static uint64 PathmanCopyFrom(CopyState cstate,
3838
Relation parent_rel,
3939
List *range_table,
4040
bool old_protocol);
41-
static ResultRelInfoHolder *select_partition_for_copy(const PartRelationInfo *prel,
42-
ResultPartsStorage *parts_storage,
43-
Datum value, EState *estate);
4441

4542

4643
/*
@@ -376,7 +373,7 @@ PathmanCopyFrom(CopyState cstate, Relation parent_rel,
376373
/* Initialize ResultPartsStorage */
377374
init_result_parts_storage(&parts_storage, estate, false,
378375
ResultPartsStorageStandard,
379-
check_acl_for_partition, NULL);
376+
NULL, NULL);
380377
parts_storage.saved_rel_info = parent_result_rel;
381378

382379
/* Set up a tuple slot too */
@@ -418,16 +415,19 @@ PathmanCopyFrom(CopyState cstate, Relation parent_rel,
418415
/* Fetch PartRelationInfo for parent relation */
419416
prel = get_pathman_relation_info(RelationGetRelid(parent_rel));
420417

421-
/* Switch into its memory context */
418+
/* Switch into per tuple memory context */
422419
MemoryContextSwitchTo(GetPerTupleMemoryContext(estate));
423420

424421
if (!NextCopyFrom(cstate, econtext, values, nulls, &tuple_oid))
425422
break;
426423

424+
if (nulls[prel->attnum - 1])
425+
elog(ERROR, ERR_PART_ATTR_NULL);
426+
427427
/* Search for a matching partition */
428-
rri_holder_child = select_partition_for_copy(prel, &parts_storage,
429-
values[prel->attnum - 1],
430-
estate);
428+
rri_holder_child = select_partition_for_insert(prel, &parts_storage,
429+
values[prel->attnum - 1],
430+
estate, false);
431431
child_result_rel = rri_holder_child->result_rel_info;
432432
estate->es_result_relation_info = child_result_rel;
433433

@@ -477,8 +477,7 @@ PathmanCopyFrom(CopyState cstate, Relation parent_rel,
477477

478478
if (child_result_rel->ri_NumIndices > 0)
479479
recheckIndexes = ExecInsertIndexTuples(slot, &(tuple->t_self),
480-
estate, false, NULL,
481-
NIL);
480+
estate, false, NULL, NIL);
482481

483482
/* AFTER ROW INSERT Triggers */
484483
ExecARInsertTriggers(estate, child_result_rel, tuple,
@@ -522,39 +521,3 @@ PathmanCopyFrom(CopyState cstate, Relation parent_rel,
522521

523522
return processed;
524523
}
525-
526-
/*
527-
* Smart wrapper for scan_result_parts_storage().
528-
*/
529-
static ResultRelInfoHolder *
530-
select_partition_for_copy(const PartRelationInfo *prel,
531-
ResultPartsStorage *parts_storage,
532-
Datum value, EState *estate)
533-
{
534-
ExprContext *econtext;
535-
ResultRelInfoHolder *rri_holder;
536-
Oid selected_partid = InvalidOid;
537-
Oid *parts;
538-
int nparts;
539-
540-
econtext = GetPerTupleExprContext(estate);
541-
542-
/* Search for matching partitions using partitioned column */
543-
parts = find_partitions_for_value(value, prel, econtext, &nparts);
544-
545-
if (nparts > 1)
546-
elog(ERROR, "PATHMAN COPY selected more than one partition");
547-
else if (nparts == 0)
548-
elog(ERROR,
549-
"There is no suitable partition for key '%s'",
550-
datum_to_cstring(value, prel->atttype));
551-
else
552-
selected_partid = parts[0];
553-
554-
/* Replace parent table with a suitable partition */
555-
MemoryContextSwitchTo(estate->es_query_cxt);
556-
rri_holder = scan_result_parts_storage(selected_partid, parts_storage);
557-
MemoryContextSwitchTo(GetPerTupleMemoryContext(estate));
558-
559-
return rri_holder;
560-
}

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