8000 Use slots in trigger infrastructure, except for the actual invocation. · m99coder/postgres_cluster@ff11e7f · GitHub
[go: up one dir, main page]

Skip to content

Commit ff11e7f

Browse files
committed
Use slots in trigger infrastructure, except for the actual invocation.
In preparation for abstracting table storage, convert trigger.c to track tuples in slots. Which also happens to make code calling triggers simpler. As the calling interface for triggers themselves is not changed in this patch, HeapTuples still are extracted from the slot at that time. But that's handled solely inside trigger.c, not visible to callers. It's quite likely that we'll want to revise the external trigger interface, but that's a separate large project. As part of this work the slots used for old/new/return tuples are moved from EState into ResultRelInfo, as different updated tables might need different slots. The slots are now also now created on-demand, which is good both from an efficiency POV, but also makes the modifying code simpler. Author: Andres Freund, Amit Khandekar and Ashutosh Bapat Discussion: https://postgr.es/m/20180703070645.wchpu5muyto5n647@alap3.anarazel.de
1 parent b8d7174 commit ff11e7f

File tree

14 files changed

+642
-571
lines changed
  • nodes
  • 14 files changed

    +642
    -571
    lines changed

    contrib/postgres_fdw/postgres_fdw.c

    Lines changed: 10 additions & 6 deletions
    Original file line numberDiff line numberDiff line change
    @@ -3507,8 +3507,13 @@ store_returning_result(PgFdwModifyState *fmstate,
    35073507
    fmstate->retrieved_attrs,
    35083508
    NULL,
    35093509
    fmstate->temp_cxt);
    3510-
    /* tuple will be deleted when it is cleared from the slot */
    3511-
    ExecStoreHeapTuple(newtup, slot, true);
    3510+
    /*
    3511+
    * The returning slot will not necessarily be suitable to store
    3512+
    * heaptuples directly, so allow for conversion.
    3513+
    */
    3514+
    ExecForceStoreHeapTuple(newtup, slot);
    3515+
    ExecMaterializeSlot(slot);
    3516+
    pfree(newtup);
    35123517
    }
    35133518
    PG_CATCH();
    35143519
    {
    @@ -3886,6 +3891,7 @@ apply_returning_filter(PgFdwDirectModifyState *dmstate,
    38863891
    TupleTableSlot *slot,
    38873892
    EState *estate)
    38883893
    {
    3894+
    ResultRelInfo *relInfo = estate->es_result_relation_info;
    38893895
    TupleDesc resultTupType = RelationGetDescr(dmstate->resultRel);
    38903896
    TupleTableSlot *resultSlot;
    38913897
    Datum *values;
    @@ -3895,11 +3901,9 @@ apply_returning_filter(PgFdwDirectModifyState *dmstate,
    38953901
    int i;
    38963902

    38973903
    /*
    3898-
    * Use the trigger tuple slot as a place to store the result tuple.
    3904+
    * Use the return tuple slot as a place to store the result tuple.
    38993905
    */
    3900-
    resultSlot = estate->es_trig_tuple_slot;
    3901-
    if (resultSlot->tts_tupleDescriptor != resultTupType)
    3902-
    ExecSetSlotDescriptor(resultSlot, resultTupType);
    3906+
    resultSlot = ExecGetReturningSlot(estate, relInfo);
    39033907

    39043908
    /*
    39053909
    * Extract all the values of the scan tuple.

    src/backend/commands/copy.c

    Lines changed: 8 additions & 16 deletions
    Original file line numberDiff line numberDiff line change
    @@ -2519,9 +2519,6 @@ CopyFrom(CopyState cstate)
    25192519
    /* Set up a tuple slot too */
    25202520
    myslot = ExecInitExtraTupleSlot(estate, tupDesc,
    25212521
    &TTSOpsHeapTuple);
    2522-
    /* Triggers might need a slot as well */
    2523-
    estate->es_trig_tuple_slot = ExecInitExtraTupleSlot(estate, NULL,
    2524-
    &TTSOpsHeapTuple);
    25252522

    25262523
    /*
    25272524
    * Set up a ModifyTableState so we can let FDW(s) init themselves for
    @@ -2870,7 +2867,7 @@ CopyFrom(CopyState cstate)
    28702867
    * Otherwise, just remember the original unconverted
    28712868
    * tuple, to avoid a needless round trip conversion.
    28722869
    */
    2873-
    cstate->transition_capture->tcs_original_insert_tuple = tuple;
    2870+
    cstate->transition_capture->tcs_original_insert_tuple = myslot;
    28742871
    cstate->transition_capture->tcs_map = NULL;
    28752872
    }
    28762873
    }
    @@ -2907,12 +2904,8 @@ CopyFrom(CopyState cstate)
    29072904
    /* BEFORE ROW INSERT Triggers */
    29082905
    if (has_before_insert_row_trig)
    29092906
    {
    2910-
    slot = ExecBRInsertTriggers(estate, resultRelInfo, slot);
    2911-
    2912-
    if (slot == NULL) /* "do nothing" */
    2913-
    skip_tuple = true;
    2914-
    else /* trigger might have changed tuple */
    2915-
    tuple = ExecFetchSlotHeapTuple(slot, true, NULL);
    2907+
    if (!ExecBRInsertTriggers(estate, resultRelInfo, slot))
    2908+
    skip_tuple = true; /* "do nothing" */
    29162909
    }
    29172910

    29182911
    if (!skip_tuple)
    @@ -2990,9 +2983,6 @@ CopyFrom(CopyState cstate)
    29902983
    if (slot == NULL) /* "do nothing" */
    29912984
    continue; /* next tuple please */
    29922985

    2993-
    /* FDW might have changed tuple */
    2994-
    tuple = ExecFetchSlotHeapTuple(slot, true, NULL);
    2995-
    29962986
    /*
    29972987
    * AFTER ROW Triggers might reference the tableoid
    29982988
    * column, so (re-)initialize tts_tableOid before
    @@ -3002,6 +2992,7 @@ CopyFrom(CopyState cstate)
    30022992
    }
    30032993
    else
    30042994
    {
    2995+
    tuple = ExecFetchSlotHeapTuple(slot, true, NULL);
    30052996
    heap_insert(resultRelInfo->ri_RelationDesc, tuple,
    30062997
    mycid, hi_options, bistate);
    30072998
    ItemPointerCopy(&tuple->t_self, &slot->tts_tid);
    @@ -3018,7 +3009,7 @@ CopyFrom(CopyState cstate)
    30183009
    NIL);
    30193010

    30203011
    /* AFTER ROW INSERT Triggers */
    3021-
    ExecARInsertTriggers(estate, resultRelInfo, tuple,
    3012+
    ExecARInsertTriggers(estate, resultRelInfo, slot,
    30223013
    recheckIndexes, cstate->transition_capture);
    30233014

    30243015
    list_free(recheckIndexes);
    @@ -3158,7 +3149,7 @@ CopyFromInsertBatch(CopyState cstate, EState *estate, CommandId mycid,
    31583149
    ExecInsertIndexTuples(myslot, &(bufferedTuples[i]->t_self),
    31593150
    estate, false, NULL, NIL);
    31603151
    ExecARInsertTriggers(estate, resultRelInfo,
    3161-
    bufferedTuples[i],
    3152+
    myslot,
    31623153
    recheckIndexes, cstate->transition_capture);
    31633154
    list_free(recheckIndexes);
    31643155
    }
    @@ -3175,8 +3166,9 @@ CopyFromInsertBatch(CopyState cstate, EState *estate, CommandId mycid,
    31753166
    for (i = 0; i < nBufferedTuples; i++)
    31763167
    {
    31773168
    cstate->cur_lineno = firstBufferedLineNo + i;
    3169+
    ExecStoreHeapTuple(bufferedTuples[i], myslot, false);
    31783170
    ExecARInsertTriggers(estate, resultRelInfo,
    3179-
    bufferedTuples[i],
    3171+
    myslot,
    31803172
    NIL, cstate->transition_capture);
    31813173
    }
    31823174
    }

    src/backend/commands/tablecmds.c

    Lines changed: 0 additions & 2 deletions
    Original file line numberDiff line numberDiff line change
    @@ -8921,8 +8921,6 @@ validateForeignKeyConstraint(char *conname,
    89218921
    trigdata.tg_trigtuple = tuple;
    89228922
    trigdata.tg_newtuple = NULL;
    89238923
    trigdata.tg_trigger = &trig;
    8924-
    trigdata.tg_trigtuplebuf = scan->rs_cbuf;
    8925-
    trigdata.tg_newtuplebuf = InvalidBuffer;
    89268924

    89278925
    fcinfo->context = (Node *) &trigdata;
    89288926

    0 commit comments

    Comments
     (0)
    0