~ubuntu-branches/ubuntu/trusty/postgresql-9.3/trusty-proposed

« back to all changes in this revision

Viewing changes to src/backend/commands/trigger.c

  • Committer: Package Import Robot
  • Author(s): Martin Pitt
  • Date: 2014-07-24 16:13:59 UTC
  • mfrom: (1.1.8)
  • Revision ID: package-import@ubuntu.com-20140724161359-uk325qfv03euxuuh
Tags: 9.3.5-0ubuntu0.14.04.1
* New upstream bug fix release: (LP: #1348176)
  - pg_upgrade: Users who upgraded to version 9.3 using pg_upgrade may have
    an issue with transaction information which causes VACUUM to eventually
    fail. These users should run the script provided in the release notes to
    determine if their installation is affected, and then take the remedy
    steps outlined there.
  - Various data integrity and other bug fixes.
  - Secure Unix-domain sockets of temporary postmasters started during make
    check.
    Any local user able to access the socket file could connect as the
    server's bootstrap superuser, then proceed to execute arbitrary code as
    the operating-system user running the test, as we previously noted in
    CVE-2014-0067. This change defends against that risk by placing the
    server's socket in a temporary, mode 0700 subdirectory of /tmp.
  - See release notes for details:
    http://www.postgresql.org/about/news/1534/
* Remove pg_regress patches to support --host=/path, obsolete with above
  upstream changes and not applicable any more.
* Drop tcl8.6 patch, applied upstream.
* Add missing logrotate test dependency.

Show diffs side-by-side

added added

removed removed

Lines of Context:
106
106
 *
107
107
 * constraintOid, if nonzero, says that this trigger is being created
108
108
 * internally to implement that constraint.  A suitable pg_depend entry will
109
 
 * be made to link the trigger to that constraint.      constraintOid is zero when
 
109
 * be made to link the trigger to that constraint.  constraintOid is zero when
110
110
 * executing a user-entered CREATE TRIGGER command.  (For CREATE CONSTRAINT
111
111
 * TRIGGER, we build a pg_constraint entry internally.)
112
112
 *
393
393
        if (funcrettype != TRIGGEROID)
394
394
        {
395
395
                /*
396
 
                 * We allow OPAQUE just so we can load old dump files.  When we see a
 
396
                 * We allow OPAQUE just so we can load old dump files.  When we see a
397
397
                 * trigger function declared OPAQUE, change it to TRIGGER.
398
398
                 */
399
399
                if (funcrettype == OPAQUEOID)
415
415
         * references one of the built-in RI_FKey trigger functions, assume it is
416
416
         * from a dump of a pre-7.3 foreign key constraint, and take steps to
417
417
         * convert this legacy representation into a regular foreign key
418
 
         * constraint.  Ugly, but necessary for loading old dump files.
 
418
         * constraint.  Ugly, but necessary for loading old dump files.
419
419
         */
420
420
        if (stmt->isconstraint && !isInternal &&
421
421
                list_length(stmt->args) >= 6 &&
478
478
 
479
479
        /*
480
480
         * If trigger is internally generated, modify the provided trigger name to
481
 
         * ensure uniqueness by appending the trigger OID.      (Callers will usually
 
481
         * ensure uniqueness by appending the trigger OID.  (Callers will usually
482
482
         * supply a simple constant trigger name in these cases.)
483
483
         */
484
484
        if (isInternal)
602
602
                        int16           attnum;
603
603
                        int                     j;
604
604
 
605
 
                        /* Lookup column name.  System columns are not allowed */
 
605
                        /* Lookup column name.  System columns are not allowed */
606
606
                        attnum = attnameAttNum(rel, name, false);
607
607
                        if (attnum == InvalidAttrNumber)
608
608
                                ereport(ERROR,
707
707
        else
708
708
        {
709
709
                /*
710
 
                 * User CREATE TRIGGER, so place dependencies.  We make trigger be
 
710
                 * User CREATE TRIGGER, so place dependencies.  We make trigger be
711
711
                 * auto-dropped if its relation is dropped or if the FK relation is
712
712
                 * dropped.  (Auto drop is compatible with our pre-7.3 behavior.)
713
713
                 */
776
776
 * full-fledged foreign key constraints.
777
777
 *
778
778
 * The conversion is complex because a pre-7.3 foreign key involved three
779
 
 * separate triggers, which were reported separately in dumps.  While the
 
779
 * separate triggers, which were reported separately in dumps.  While the
780
780
 * single trigger on the referencing table adds no new information, we need
781
781
 * to know the trigger functions of both of the triggers on the referenced
782
782
 * table to build the constraint declaration.  Also, due to lack of proper
2010
2010
        if (newtuple != slottuple)
2011
2011
        {
2012
2012
                /*
2013
 
                 * Return the modified tuple using the es_trig_tuple_slot.      We assume
 
2013
                 * Return the modified tuple using the es_trig_tuple_slot.  We assume
2014
2014
                 * the tuple was allocated in per-tuple memory context, and therefore
2015
2015
                 * will go away by itself. The tuple table slot should not try to
2016
2016
                 * clear it.
2085
2085
        if (newtuple != slottuple)
2086
2086
        {
2087
2087
                /*
2088
 
                 * Return the modified tuple using the es_trig_tuple_slot.      We assume
 
2088
                 * Return the modified tuple using the es_trig_tuple_slot.  We assume
2089
2089
                 * the tuple was allocated in per-tuple memory context, and therefore
2090
2090
                 * will go away by itself. The tuple table slot should not try to
2091
2091
                 * clear it.
2445
2445
        if (newtuple != slottuple)
2446
2446
        {
2447
2447
                /*
2448
 
                 * Return the modified tuple using the es_trig_tuple_slot.      We assume
 
2448
                 * Return the modified tuple using the es_trig_tuple_slot.  We assume
2449
2449
                 * the tuple was allocated in per-tuple memory context, and therefore
2450
2450
                 * will go away by itself. The tuple table slot should not try to
2451
2451
                 * clear it.
2532
2532
        if (newtuple != slottuple)
2533
2533
        {
2534
2534
                /*
2535
 
                 * Return the modified tuple using the es_trig_tuple_slot.      We assume
 
2535
                 * Return the modified tuple using the es_trig_tuple_slot.  We assume
2536
2536
                 * the tuple was allocated in per-tuple memory context, and therefore
2537
2537
                 * will go away by itself. The tuple table slot should not try to
2538
2538
                 * clear it.
2941
2941
 * Per-trigger-event data
2942
2942
 *
2943
2943
 * The actual per-event data, AfterTriggerEventData, includes DONE/IN_PROGRESS
2944
 
 * status bits and one or two tuple CTIDs.      Each event record also has an
 
2944
 * status bits and one or two tuple CTIDs.  Each event record also has an
2945
2945
 * associated AfterTriggerSharedData that is shared across all instances
2946
2946
 * of similar events within a "chunk".
2947
2947
 *
2955
2955
 * Although this is mutable state, we can keep it in AfterTriggerSharedData
2956
2956
 * because all instances of the same type of event in a given event list will
2957
2957
 * be fired at the same time, if they were queued between the same firing
2958
 
 * cycles.      So we need only ensure that ats_firing_id is zero when attaching
 
2958
 * cycles.  So we need only ensure that ats_firing_id is zero when attaching
2959
2959
 * a new event to an existing AfterTriggerSharedData record.
2960
2960
 */
2961
2961
typedef uint32 TriggerFlags;
3002
3002
/*
3003
3003
 * To avoid palloc overhead, we keep trigger events in arrays in successively-
3004
3004
 * larger chunks (a slightly more sophisticated version of an expansible
3005
 
 * array).      The space between CHUNK_DATA_START and freeptr is occupied by
 
3005
 * array).  The space between CHUNK_DATA_START and freeptr is occupied by
3006
3006
 * AfterTriggerEventData records; the space between endfree and endptr is
3007
3007
 * occupied by AfterTriggerSharedData records.
3008
3008
 */
3044
3044
 *
3045
3045
 * firing_counter is incremented for each call of afterTriggerInvokeEvents.
3046
3046
 * We mark firable events with the current firing cycle's ID so that we can
3047
 
 * tell which ones to work on.  This ensures sane behavior if a trigger
 
3047
 * tell which ones to work on.  This ensures sane behavior if a trigger
3048
3048
 * function chooses to do SET CONSTRAINTS: the inner SET CONSTRAINTS will
3049
3049
 * only fire those events that weren't already scheduled for firing.
3050
3050
 *
3052
3052
 * This is saved and restored across failed subtransactions.
3053
3053
 *
3054
3054
 * events is the current list of deferred events.  This is global across
3055
 
 * all subtransactions of the current transaction.      In a subtransaction
 
3055
 * all subtransactions of the current transaction.  In a subtransaction
3056
3056
 * abort, we know that the events added by the subtransaction are at the
3057
3057
 * end of the list, so it is relatively easy to discard them.  The event
3058
3058
 * list chunks themselves are stored in event_cxt.
3080
3080
 * which we similarly use to clean up at subtransaction abort.
3081
3081
 *
3082
3082
 * firing_stack is a stack of copies of subtransaction-start-time
3083
 
 * firing_counter.      We use this to recognize which deferred triggers were
 
3083
 * firing_counter.  We use this to recognize which deferred triggers were
3084
3084
 * fired (or marked for firing) within an aborted subtransaction.
3085
3085
 *
3086
3086
 * We use GetCurrentTransactionNestLevel() to determine the correct array
3087
3087
 * index in these stacks.  maxtransdepth is the number of allocated entries in
3088
 
 * each stack.  (By not keeping our own stack pointer, we can avoid trouble
 
3088
 * each stack.  (By not keeping our own stack pointer, we can avoid trouble
3089
3089
 * in cases where errors during subxact abort cause multiple invocations
3090
3090
 * of AfterTriggerEndSubXact() at the same nesting depth.)
3091
3091
 */
3353
3353
 *      single trigger function.
3354
3354
 *
3355
3355
 *      Frequently, this will be fired many times in a row for triggers of
3356
 
 *      a single relation.      Therefore, we cache the open relation and provide
 
3356
 *      a single relation.  Therefore, we cache the open relation and provide
3357
3357
 *      fmgr lookup cache space at the caller level.  (For triggers fired at
3358
3358
 *      the end of a query, we can even piggyback on the executor's state.)
3359
3359
 *
3870
3870
        }
3871
3871
 
3872
3872
        /*
3873
 
         * Run all the remaining triggers.      Loop until they are all gone, in case
 
3873
         * Run all the remaining triggers.  Loop until they are all gone, in case
3874
3874
         * some trigger queues more for us to do.
3875
3875
         */
3876
3876
        while (afterTriggerMarkEvents(events, NULL, false))
3933
3933
        int                     my_level = GetCurrentTransactionNestLevel();
3934
3934
 
3935
3935
        /*
3936
 
         * Ignore call if the transaction is in aborted state.  (Probably
 
3936
         * Ignore call if the transaction is in aborted state.  (Probably
3937
3937
         * shouldn't happen?)
3938
3938
         */
3939
3939
        if (afterTriggers == NULL)
4012
4012
        CommandId       subxact_firing_id;
4013
4013
 
4014
4014
        /*
4015
 
         * Ignore call if the transaction is in aborted state.  (Probably
 
4015
         * Ignore call if the transaction is in aborted state.  (Probably
4016
4016
         * unneeded)
4017
4017
         */
4018
4018
        if (afterTriggers == NULL)
4144
4144
}
4145
4145
 
4146
4146
/*
4147
 
 * Add a per-trigger item to a SetConstraintState.      Returns possibly-changed
 
4147
 * Add a per-trigger item to a SetConstraintState.  Returns possibly-changed
4148
4148
 * pointer to the state object (it will change if we have to repalloc).
4149
4149
 */
4150
4150
static SetConstraintState
4229
4229
                 * First, identify all the named constraints and make a list of their
4230
4230
                 * OIDs.  Since, unlike the SQL spec, we allow multiple constraints of
4231
4231
                 * the same name within a schema, the specifications are not
4232
 
                 * necessarily unique.  Our strategy is to target all matching
 
4232
                 * necessarily unique.  Our strategy is to target all matching
4233
4233
                 * constraints within the first search-path schema that has any
4234
4234
                 * matches, but disregard matches in schemas beyond the first match.
4235
4235
                 * (This is a bit odd but it's the historical behavior.)
4255
4255
 
4256
4256
                        /*
4257
4257
                         * If we're given the schema name with the constraint, look only
4258
 
                         * in that schema.      If given a bare constraint name, use the
 
4258
                         * in that schema.  If given a bare constraint name, use the
4259
4259
                         * search path to find the first matching constraint.
4260
4260
                         */
4261
4261
                        if (constraint->schemaname)
4359
4359
 
4360
4360
                                /*
4361
4361
                                 * Silently skip triggers that are marked as non-deferrable in
4362
 
                                 * pg_trigger.  This is not an error condition, since a
 
4362
                                 * pg_trigger.  This is not an error condition, since a
4363
4363
                                 * deferrable RI constraint may have some non-deferrable
4364
4364
                                 * actions.
4365
4365
                                 */
4430
4430
 
4431
4431
                        /*
4432
4432
                         * Make sure a snapshot has been established in case trigger
4433
 
                         * functions need one.  Note that we avoid setting a snapshot if
 
4433
                         * functions need one.  Note that we avoid setting a snapshot if
4434
4434
                         * we don't find at least one trigger that has to be fired now.
4435
4435
                         * This is so that BEGIN; SET CONSTRAINTS ...; SET TRANSACTION
4436
4436
                         * ISOLATION LEVEL SERIALIZABLE; ... works properly.  (If we are
4490
4490
                AfterTriggerShared evtshared = GetTriggerSharedData(event);
4491
4491
 
4492
4492
                /*
4493
 
                 * We can ignore completed events.      (Even if a DONE flag is rolled
 
4493
                 * We can ignore completed events.  (Even if a DONE flag is rolled
4494
4494
                 * back by subxact abort, it's OK because the effects of the TRUNCATE
4495
4495
                 * or whatever must get rolled back too.)
4496
4496
                 */
4531
4531
 *      be fired for an event.
4532
4532
 *
4533
4533
 *      NOTE: this is called whenever there are any triggers associated with
4534
 
 *      the event (even if they are disabled).  This function decides which
 
4534
 *      the event (even if they are disabled).  This function decides which
4535
4535
 *      triggers actually need to be queued.
4536
4536
 * ----------
4537
4537
 */