Header And Logo

PostgreSQL
| The world's most advanced open source database.

Data Structures | Defines | Typedefs | Enumerations | Functions

heapam.h File Reference

#include "access/sdir.h"
#include "access/skey.h"
#include "nodes/primnodes.h"
#include "storage/bufpage.h"
#include "storage/lock.h"
#include "utils/relcache.h"
#include "utils/snapshot.h"
Include dependency graph for heapam.h:

Go to the source code of this file.

Data Structures

struct  HeapUpdateFailureData

Defines

#define HEAP_INSERT_SKIP_WAL   0x0001
#define HEAP_INSERT_SKIP_FSM   0x0002
#define HEAP_INSERT_FROZEN   0x0004
#define MaxLockTupleMode   LockTupleExclusive
#define heap_close(r, l)   relation_close(r,l)
#define HeapScanIsValid(scan)   PointerIsValid(scan)

Typedefs

typedef struct
BulkInsertStateData
BulkInsertState
typedef enum LockTupleMode LockTupleMode
typedef struct
HeapUpdateFailureData 
HeapUpdateFailureData
typedef struct HeapScanDescDataHeapScanDesc

Enumerations

enum  LockTupleMode { LockTupleKeyShare, LockTupleShare, LockTupleNoKeyExclusive, LockTupleExclusive }

Functions

Relation relation_open (Oid relationId, LOCKMODE lockmode)
Relation try_relation_open (Oid relationId, LOCKMODE lockmode)
Relation relation_openrv (const RangeVar *relation, LOCKMODE lockmode)
Relation relation_openrv_extended (const RangeVar *relation, LOCKMODE lockmode, bool missing_ok)
void relation_close (Relation relation, LOCKMODE lockmode)
Relation heap_open (Oid relationId, LOCKMODE lockmode)
Relation heap_openrv (const RangeVar *relation, LOCKMODE lockmode)
Relation heap_openrv_extended (const RangeVar *relation, LOCKMODE lockmode, bool missing_ok)
HeapScanDesc heap_beginscan (Relation relation, Snapshot snapshot, int nkeys, ScanKey key)
HeapScanDesc heap_beginscan_strat (Relation relation, Snapshot snapshot, int nkeys, ScanKey key, bool allow_strat, bool allow_sync)
HeapScanDesc heap_beginscan_bm (Relation relation, Snapshot snapshot, int nkeys, ScanKey key)
void heap_rescan (HeapScanDesc scan, ScanKey key)
void heap_endscan (HeapScanDesc scan)
HeapTuple heap_getnext (HeapScanDesc scan, ScanDirection direction)
bool heap_fetch (Relation relation, Snapshot snapshot, HeapTuple tuple, Buffer *userbuf, bool keep_buf, Relation stats_relation)
bool heap_hot_search_buffer (ItemPointer tid, Relation relation, Buffer buffer, Snapshot snapshot, HeapTuple heapTuple, bool *all_dead, bool first_call)
bool heap_hot_search (ItemPointer tid, Relation relation, Snapshot snapshot, bool *all_dead)
void heap_get_latest_tid (Relation relation, Snapshot snapshot, ItemPointer tid)
void setLastTid (const ItemPointer tid)
BulkInsertState GetBulkInsertState (void)
void FreeBulkInsertState (BulkInsertState)
Oid heap_insert (Relation relation, HeapTuple tup, CommandId cid, int options, BulkInsertState bistate)
void heap_multi_insert (Relation relation, HeapTuple *tuples, int ntuples, CommandId cid, int options, BulkInsertState bistate)
HTSU_Result heap_delete (Relation relation, ItemPointer tid, CommandId cid, Snapshot crosscheck, bool wait, HeapUpdateFailureData *hufd)
HTSU_Result heap_update (Relation relation, ItemPointer otid, HeapTuple newtup, CommandId cid, Snapshot crosscheck, bool wait, HeapUpdateFailureData *hufd, LockTupleMode *lockmode)
HTSU_Result heap_lock_tuple (Relation relation, HeapTuple tuple, CommandId cid, LockTupleMode mode, bool nowait, bool follow_update, Buffer *buffer, HeapUpdateFailureData *hufd)
void heap_inplace_update (Relation relation, HeapTuple tuple)
bool heap_freeze_tuple (HeapTupleHeader tuple, TransactionId cutoff_xid, TransactionId cutoff_multi)
bool heap_tuple_needs_freeze (HeapTupleHeader tuple, TransactionId cutoff_xid, MultiXactId cutoff_multi, Buffer buf)
Oid simple_heap_insert (Relation relation, HeapTuple tup)
void simple_heap_delete (Relation relation, ItemPointer tid)
void simple_heap_update (Relation relation, ItemPointer otid, HeapTuple tup)
void heap_markpos (HeapScanDesc scan)
void heap_restrpos (HeapScanDesc scan)
void heap_sync (Relation relation)
void heap_page_prune_opt (Relation relation, Buffer buffer, TransactionId OldestXmin)
int heap_page_prune (Relation relation, Buffer buffer, TransactionId OldestXmin, bool report_stats, TransactionId *latestRemovedXid)
void heap_page_prune_execute (Buffer buffer, OffsetNumber *redirected, int nredirected, OffsetNumber *nowdead, int ndead, OffsetNumber *nowunused, int nunused)
void heap_get_root_tuples (Page page, OffsetNumber *root_offsets)
void ss_report_location (Relation rel, BlockNumber location)
BlockNumber ss_get_location (Relation rel, BlockNumber relnblocks)
void SyncScanShmemInit (void)
Size SyncScanShmemSize (void)

Define Documentation

#define heap_close (   r,
  l 
)    relation_close(r,l)

Definition at line 95 of file heapam.h.

Referenced by acquire_inherited_sample_rows(), AcquireRewriteLocks(), AddEnumLabel(), AddNewAttributeTuples(), addRangeTableEntry(), AddRoleMems(), afterTriggerInvokeEvents(), AfterTriggerSetState(), AggregateCreate(), AlterConstraintNamespaces(), AlterDatabase(), AlterDatabaseOwner(), AlterDomainAddConstraint(), AlterDomainDefault(), AlterDomainDropConstraint(), AlterDomainNotNull(), AlterDomainValidateConstraint(), AlterEventTrigger(), AlterEventTriggerOwner(), AlterEventTriggerOwner_oid(), AlterExtensionNamespace(), AlterForeignDataWrapper(), AlterForeignDataWrapperOwner(), AlterForeignDataWrapperOwner_oid(), AlterForeignServer(), AlterForeignServerOwner(), AlterForeignServerOwner_oid(), AlterFunction(), AlterObjectNamespace_oid(), AlterRole(), AlterSchemaOwner(), AlterSchemaOwner_oid(), AlterSetting(), AlterTableCreateToastTable(), AlterTableNamespaceInternal(), AlterTableSpaceOptions(), AlterTSConfiguration(), AlterTSDictionary(), AlterTypeNamespaceInternal(), AlterTypeOwner(), AlterTypeOwnerInternal(), AlterUserMapping(), AppendAttributeTuples(), ApplyExtensionUpdates(), AssignTypeArrayOid(), ATAddCheckConstraint(), ATAddForeignKeyConstraint(), ATExecAddColumn(), ATExecAddInherit(), ATExecAddOf(), ATExecAlterColumnGenericOptions(), ATExecAlterColumnType(), ATExecChangeOwner(), ATExecDropColumn(), ATExecDropConstraint(), ATExecDropInherit(), ATExecDropNotNull(), ATExecDropOf(), ATExecGenericOptions(), ATExecSetNotNull(), ATExecSetOptions(), ATExecSetRelOptions(), ATExecSetStatistics(), ATExecSetStorage(), ATExecSetTableSpace(), ATExecValidateConstraint(), ATRewriteTable(), ATRewriteTables(), AttrDefaultFetch(), boot_openrel(), BootstrapToastTable(), build_indices(), build_physical_tlist(), CatalogCacheInitializeCache(), change_owner_fix_column_acls(), changeDependencyFor(), changeDependencyOnOwner(), check_db_file_conflict(), check_functional_grouping(), check_selective_binary_conversion(), CheckConstraintFetch(), checkSharedDependencies(), ChooseConstraintName(), close_lo_relation(), closerel(), cluster(), CollationCreate(), ConstraintNameIsUsed(), ConversionCreate(), copy_heap_data(), copyTemplateDependencies(), create_proc_lang(), create_toast_table(), CreateCast(), CreateComments(), CreateConstraintEntry(), createdb(), CreateForeignDataWrapper(), CreateForeignServer(), CreateForeignTable(), CreateOpFamily(), CreateRole(), CreateSharedComments(), CreateTableSpace(), CreateTrigger(), CreateUserMapping(), currtid_byrelname(), currtid_byreloid(), currtid_for_view(), database_to_xmlschema_internal(), DefineIndex(), DefineOpClass(), DefineQueryRewrite(), DefineSequence(), DefineTSConfiguration(), DefineTSDictionary(), DefineTSParser(), DefineTSTemplate(), DeleteAttributeTuples(), DeleteComments(), deleteDependencyRecordsFor(), deleteDependencyRecordsForClass(), deleteOneObject(), DeleteRelationTuple(), DeleteSecurityLabel(), DeleteSharedComments(), deleteSharedDependencyRecordsFor(), DeleteSharedSecurityLabel(), DeleteSystemAttributeTuples(), deleteWhatDependsOn(), DelRoleMems(), deparseSelectSql(), do_autovacuum(), DoCopy(), drop_parent_dependency(), DropCastById(), dropDatabaseDependencies(), dropdb(), DropProceduralLanguageById(), DropRole(), DropSetting(), DropTableSpace(), EnableDisableRule(), EnableDisableTrigger(), enum_endpoint(), enum_range_internal(), EnumValuesCreate(), EnumValuesDelete(), EvalPlanQualEnd(), EventTriggerSQLDropAddObject(), exec_object_restorecon(), ExecAlterExtensionStmt(), ExecAlterObjectSchemaStmt(), ExecAlterOwnerStmt(), ExecCloseScanRelation(), ExecEndPlan(), ExecGrant_Database(), ExecGrant_Fdw(), ExecGrant_ForeignServer(), ExecGrant_Function(), ExecGrant_Language(), ExecGrant_Largeobject(), ExecGrant_Namespace(), ExecGrant_Relation(), ExecGrant_Tablespace(), ExecGrant_Type(), ExecRefreshMatView(), ExecRenameStmt(), ExecuteTruncate(), expand_inherited_rtentry(), expand_targetlist(), extension_config_remove(), find_inheritance_children(), find_language_template(), find_typed_table_dependencies(), fireRIRrules(), free_parsestate(), get_actual_variable_range(), get_constraint_index(), get_database_list(), get_database_oid(), get_db_info(), get_domain_constraint_oid(), get_extension_name(), get_extension_oid(), get_extension_schema(), get_file_fdw_attribute_options(), get_index_constraint(), get_object_address_relobject(), get_pkey_attnames(), get_rel_oids(), get_relation_constraint_oid(), get_relation_constraints(), get_relation_data_width(), get_relation_info(), get_rewrite_oid_without_relid(), get_tablespace_name(), get_tablespace_oid(), get_trigger_oid(), GetComment(), getConstraintTypeDescription(), GetDatabaseTuple(), GetDatabaseTupleByOid(), GetDefaultOpClass(), GetDomainConstraints(), getExtensionOfObject(), getObjectDescription(), getObjectIdentity(), getOwnedSequences(), getRelationsInNamespace(), GetSecurityLabel(), GetSharedSecurityLabel(), gettype(), GrantRole(), heap_create_with_catalog(), heap_drop_with_catalog(), heap_sync(), heap_truncate(), heap_truncate_find_FKs(), heap_truncate_one_rel(), index_build(), index_constraint_create(), index_create(), index_drop(), index_set_state_flags(), index_update_stats(), insert_event_trigger_tuple(), InsertExtensionTuple(), InsertRule(), intorel_shutdown(), isQueryUsingTempRelation_walker(), LargeObjectCreate(), LargeObjectDrop(), LargeObjectExists(), load_enum_cache_data(), lookup_ts_config_cache(), LookupOpclassInfo(), make_new_heap(), make_viewdef(), makeArrayTypeName(), mark_index_clustered(), MergeAttributes(), MergeAttributesIntoExisting(), MergeConstraintsIntoExisting(), MergeWithExistingConstraint(), movedb(), myLargeObjectExists(), NamespaceCreate(), objectsInSchemaToOids(), OperatorCreate(), OperatorShellMake(), OperatorUpd(), performDeletion(), performMultipleDeletions(), pg_extension_config_dump(), pg_extension_ownercheck(), pg_get_serial_sequence(), pg_get_triggerdef_worker(), pg_identify_object(), pg_largeobject_aclmask_snapshot(), pg_largeobject_ownercheck(), pgrowlocks(), pgstat_collect_oids(), postgresPlanForeignModify(), ProcedureCreate(), process_settings(), RangeCreate(), RangeDelete(), rebuild_relation(), recordMultipleDependencies(), recordSharedDependencyOn(), regclassin(), regoperin(), regprocin(), regtypein(), reindex_index(), reindex_relation(), ReindexDatabase(), RelationBuildRuleLock(), RelationBuildTriggers(), RelationBuildTupleDesc(), RelationGetExclusionInfo(), RelationGetIndexList(), RelationRemoveInheritance(), RelationSetNewRelfilenode(), remove_dbtablespaces(), RemoveAmOpEntryById(), RemoveAmProcEntryById(), RemoveAttrDefault(), RemoveAttrDefaultById(), RemoveAttributeById(), RemoveCollationById(), RemoveConstraintById(), RemoveConversionById(), RemoveDefaultACLById(), RemoveEventTriggerById(), RemoveExtensionById(), RemoveForeignDataWrapperById(), RemoveForeignServerById(), RemoveFunctionById(), RemoveObjects(), RemoveOpClassById(), RemoveOperatorById(), RemoveOpFamilyById(), RemoveRewriteRuleById(), RemoveRoleFromObjectACL(), RemoveSchemaById(), RemoveStatistics(), RemoveTriggerById(), RemoveTSConfigurationById(), RemoveTSDictionaryById(), RemoveTSParserById(), RemoveTSTemplateById(), RemoveTypeById(), RemoveUserMappingById(), renameatt_internal(), RenameConstraint(), RenameConstraintById(), RenameDatabase(), RenameRelationInternal(), RenameRewriteRule(), RenameRole(), RenameSchema(), RenameTableSpace(), renametrig(), RenameType(), RenameTypeInternal(), RewriteQuery(), rewriteTargetView(), RI_FKey_cascade_del(), RI_FKey_cascade_upd(), RI_FKey_check(), RI_FKey_setdefault_del(), RI_FKey_setdefault_upd(), RI_FKey_setnull_del(), RI_FKey_setnull_upd(), ri_restrict_del(), ri_restrict_upd(), ScanPgRelation(), schema_to_xmlschema_internal(), SearchCatCache(), SearchCatCacheList(), sepgsql_attribute_post_create(), sepgsql_database_post_create(), sepgsql_index_modify(), sepgsql_proc_post_create(), sepgsql_proc_setattr(), sepgsql_relation_post_create(), sepgsql_relation_setattr(), sepgsql_schema_post_create(), sequenceIsOwned(), SetDefaultACL(), SetFunctionArgType(), SetFunctionReturnType(), SetRelationHasSubclass(), SetRelationNumChecks(), SetRelationRuleStatus(), SetSecurityLabel(), SetSharedSecurityLabel(), setTargetTable(), shdepDropOwned(), shdepReassignOwned(), StoreAttrDefault(), StoreCatalogInheritance(), storeOperators(), storeProcedures(), swap_relation_files(), table_to_xml_and_xmlschema(), table_to_xmlschema(), ThereIsAtLeastOneRole(), toast_delete_datum(), toast_fetch_datum(), toast_fetch_datum_slice(), toast_save_datum(), toastid_valueid_exists(), transformIndexConstraint(), transformIndexStmt(), transformRuleStmt(), transformTableLikeClause(), transientrel_shutdown(), TypeCreate(), typeInheritsFrom(), TypeShellMake(), update_attstats(), updateAclDependencies(), UpdateIndexRelation(), vac_truncate_clog(), vac_update_datfrozenxid(), vac_update_relstats(), validate_index(), and validateDomainConstraint().

#define HEAP_INSERT_FROZEN   0x0004

Definition at line 29 of file heapam.h.

Referenced by heap_prepare_insert().

#define HEAP_INSERT_SKIP_FSM   0x0002

Definition at line 28 of file heapam.h.

Referenced by intorel_startup(), raw_heap_insert(), and transientrel_startup().

#define HEAP_INSERT_SKIP_WAL   0x0001
#define HeapScanIsValid (   scan  )     PointerIsValid(scan)

Definition at line 104 of file heapam.h.

#define MaxLockTupleMode   LockTupleExclusive

Definition at line 48 of file heapam.h.


Typedef Documentation

Definition at line 31 of file heapam.h.

typedef struct HeapScanDescData* HeapScanDesc

Definition at line 98 of file heapam.h.


Enumeration Type Documentation

Enumerator:
LockTupleKeyShare 
LockTupleShare 
LockTupleNoKeyExclusive 
LockTupleExclusive 

Definition at line 36 of file heapam.h.

{
    /* SELECT FOR KEY SHARE */
    LockTupleKeyShare,
    /* SELECT FOR SHARE */
    LockTupleShare,
    /* SELECT FOR NO KEY UPDATE, and UPDATEs that don't modify key columns */
    LockTupleNoKeyExclusive,
    /* SELECT FOR UPDATE, UPDATEs that modify key columns, and DELETE */
    LockTupleExclusive
} LockTupleMode;


Function Documentation

void FreeBulkInsertState ( BulkInsertState   ) 

Definition at line 1969 of file heapam.c.

References BulkInsertStateData::current_buf, FreeAccessStrategy(), InvalidBuffer, pfree(), ReleaseBuffer(), and BulkInsertStateData::strategy.

Referenced by ATRewriteTable(), CopyFrom(), intorel_shutdown(), and transientrel_shutdown().

{
    if (bistate->current_buf != InvalidBuffer)
        ReleaseBuffer(bistate->current_buf);
    FreeAccessStrategy(bistate->strategy);
    pfree(bistate);
}

BulkInsertState GetBulkInsertState ( void   ) 
HeapScanDesc heap_beginscan ( Relation  relation,
Snapshot  snapshot,
int  nkeys,
ScanKey  key 
)
HeapScanDesc heap_beginscan_bm ( Relation  relation,
Snapshot  snapshot,
int  nkeys,
ScanKey  key 
)

Definition at line 1297 of file heapam.c.

References heap_beginscan_internal().

Referenced by ExecInitBitmapHeapScan().

{
    return heap_beginscan_internal(relation, snapshot, nkeys, key,
                                   false, false, true);
}

HeapScanDesc heap_beginscan_strat ( Relation  relation,
Snapshot  snapshot,
int  nkeys,
ScanKey  key,
bool  allow_strat,
bool  allow_sync 
)

Definition at line 1288 of file heapam.c.

References heap_beginscan_internal().

Referenced by IndexBuildHeapScan(), IndexCheckExclusion(), pgstat_heap(), systable_beginscan(), and validate_index_heapscan().

{
    return heap_beginscan_internal(relation, snapshot, nkeys, key,
                                   allow_strat, allow_sync, false);
}

HTSU_Result heap_delete ( Relation  relation,
ItemPointer  tid,
CommandId  cid,
Snapshot  crosscheck,
bool  wait,
HeapUpdateFailureData hufd 
)

Definition at line 2523 of file heapam.c.

References xl_heap_delete::all_visible_cleared, Assert, XLogRecData::buffer, BUFFER_LOCK_EXCLUSIVE, BUFFER_LOCK_UNLOCK, XLogRecData::buffer_std, BufferGetBlockNumber(), BufferGetPage, CacheInvalidateHeapTuple(), CheckForSerializableConflictIn(), HeapUpdateFailureData::cmax, compute_infobits(), compute_new_xmax_infomask(), HeapUpdateFailureData::ctid, XLogRecData::data, elog, END_CRIT_SECTION, ERROR, GetCurrentTransactionId(), HEAP_XMAX_BITS, HEAP_XMAX_INVALID, HEAP_XMAX_IS_LOCKED_ONLY, HEAP_XMAX_IS_MULTI, HeapTupleBeingUpdated, HeapTupleHasExternal, HeapTupleHeaderAdjustCmax(), HeapTupleHeaderClearHotUpdated, HeapTupleHeaderGetCmax(), HeapTupleHeaderGetRawXmax, HeapTupleHeaderGetUpdateXid, HeapTupleHeaderIsOnlyLocked(), HeapTupleHeaderSetCmax, HeapTupleHeaderSetXmax, HeapTupleInvisible, HeapTupleMayBeUpdated, HeapTupleSatisfiesUpdate(), HeapTupleSatisfiesVisibility, HeapTupleSelfUpdated, HeapTupleUpdated, xl_heap_delete::infobits_set, InvalidBuffer, InvalidSnapshot, ItemIdGetLength, ItemIdIsNormal, ItemPointerGetBlockNumber, ItemPointerGetOffsetNumber, ItemPointerIsValid, XLogRecData::len, LockBuffer(), LockTupleExclusive, LockTupleTuplock, MarkBufferDirty(), MultiXactIdSetOldestMember(), MultiXactIdWait(), MultiXactStatusUpdate, XLogRecData::next, xl_heaptid::node, PageClearAllVisible, PageGetItem, PageGetItemId, PageIsAllVisible, PageSetLSN, PageSetPrunable, pgstat_count_heap_delete(), RelationData::rd_node, RelationData::rd_rel, ReadBuffer(), RelationNeedsWAL, ReleaseBuffer(), RELKIND_MATVIEW, RELKIND_RELATION, START_CRIT_SECTION, HeapTupleHeaderData::t_ctid, HeapTupleData::t_data, HeapTupleHeaderData::t_infomask, HeapTupleHeaderData::t_infomask2, HeapTupleData::t_len, HeapTupleData::t_self, xl_heap_delete::target, xl_heaptid::tid, toast_delete(), TransactionIdEquals, UnlockReleaseBuffer(), UnlockTupleTuplock, UpdateXmaxHintBits(), visibilitymap_clear(), visibilitymap_pin(), XactLockTableWait(), XLOG_HEAP_DELETE, XLogInsert(), xl_heap_delete::xmax, and HeapUpdateFailureData::xmax.

Referenced by ExecDelete(), and simple_heap_delete().

{
    HTSU_Result result;
    TransactionId xid = GetCurrentTransactionId();
    ItemId      lp;
    HeapTupleData tp;
    Page        page;
    BlockNumber block;
    Buffer      buffer;
    Buffer      vmbuffer = InvalidBuffer;
    TransactionId new_xmax;
    uint16      new_infomask,
                new_infomask2;
    bool        have_tuple_lock = false;
    bool        iscombo;
    bool        all_visible_cleared = false;

    Assert(ItemPointerIsValid(tid));

    block = ItemPointerGetBlockNumber(tid);
    buffer = ReadBuffer(relation, block);
    page = BufferGetPage(buffer);

    /*
     * Before locking the buffer, pin the visibility map page if it appears to
     * be necessary.  Since we haven't got the lock yet, someone else might be
     * in the middle of changing this, so we'll need to recheck after we have
     * the lock.
     */
    if (PageIsAllVisible(page))
        visibilitymap_pin(relation, block, &vmbuffer);

    LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE);

    /*
     * If we didn't pin the visibility map page and the page has become all
     * visible while we were busy locking the buffer, we'll have to unlock and
     * re-lock, to avoid holding the buffer lock across an I/O.  That's a bit
     * unfortunate, but hopefully shouldn't happen often.
     */
    if (vmbuffer == InvalidBuffer && PageIsAllVisible(page))
    {
        LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
        visibilitymap_pin(relation, block, &vmbuffer);
        LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE);
    }

    lp = PageGetItemId(page, ItemPointerGetOffsetNumber(tid));
    Assert(ItemIdIsNormal(lp));

    tp.t_data = (HeapTupleHeader) PageGetItem(page, lp);
    tp.t_len = ItemIdGetLength(lp);
    tp.t_self = *tid;

l1:
    result = HeapTupleSatisfiesUpdate(tp.t_data, cid, buffer);

    if (result == HeapTupleInvisible)
    {
        UnlockReleaseBuffer(buffer);
        elog(ERROR, "attempted to delete invisible tuple");
    }
    else if (result == HeapTupleBeingUpdated && wait)
    {
        TransactionId xwait;
        uint16      infomask;

        /* must copy state data before unlocking buffer */
        xwait = HeapTupleHeaderGetRawXmax(tp.t_data);
        infomask = tp.t_data->t_infomask;

        LockBuffer(buffer, BUFFER_LOCK_UNLOCK);

        /*
         * Acquire tuple lock to establish our priority for the tuple (see
         * heap_lock_tuple).  LockTuple will release us when we are
         * next-in-line for the tuple.
         *
         * If we are forced to "start over" below, we keep the tuple lock;
         * this arranges that we stay at the head of the line while rechecking
         * tuple state.
         */
        if (!have_tuple_lock)
        {
            LockTupleTuplock(relation, &(tp.t_self), LockTupleExclusive);
            have_tuple_lock = true;
        }

        /*
         * Sleep until concurrent transaction ends.  Note that we don't care
         * which lock mode the locker has, because we need the strongest one.
         */

        if (infomask & HEAP_XMAX_IS_MULTI)
        {
            /* wait for multixact */
            MultiXactIdWait((MultiXactId) xwait, MultiXactStatusUpdate,
                            NULL, infomask);
            LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE);

            /*
             * If xwait had just locked the tuple then some other xact could
             * update this tuple before we get to this point.  Check for xmax
             * change, and start over if so.
             */
            if (!(tp.t_data->t_infomask & HEAP_XMAX_IS_MULTI) ||
                !TransactionIdEquals(HeapTupleHeaderGetRawXmax(tp.t_data),
                                     xwait))
                goto l1;

            /*
             * You might think the multixact is necessarily done here, but not
             * so: it could have surviving members, namely our own xact or
             * other subxacts of this backend.  It is legal for us to delete
             * the tuple in either case, however (the latter case is
             * essentially a situation of upgrading our former shared lock to
             * exclusive).  We don't bother changing the on-disk hint bits
             * since we are about to overwrite the xmax altogether.
             */
        }
        else
        {
            /* wait for regular transaction to end */
            XactLockTableWait(xwait);
            LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE);

            /*
             * xwait is done, but if xwait had just locked the tuple then some
             * other xact could update this tuple before we get to this point.
             * Check for xmax change, and start over if so.
             */
            if ((tp.t_data->t_infomask & HEAP_XMAX_IS_MULTI) ||
                !TransactionIdEquals(HeapTupleHeaderGetRawXmax(tp.t_data),
                                     xwait))
                goto l1;

            /* Otherwise check if it committed or aborted */
            UpdateXmaxHintBits(tp.t_data, buffer, xwait);
        }

        /*
         * We may overwrite if previous xmax aborted, or if it committed but
         * only locked the tuple without updating it.
         */
        if ((tp.t_data->t_infomask & HEAP_XMAX_INVALID) ||
            HEAP_XMAX_IS_LOCKED_ONLY(tp.t_data->t_infomask) ||
            HeapTupleHeaderIsOnlyLocked(tp.t_data))
            result = HeapTupleMayBeUpdated;
        else
            result = HeapTupleUpdated;
    }

    if (crosscheck != InvalidSnapshot && result == HeapTupleMayBeUpdated)
    {
        /* Perform additional check for transaction-snapshot mode RI updates */
        if (!HeapTupleSatisfiesVisibility(&tp, crosscheck, buffer))
            result = HeapTupleUpdated;
    }

    if (result != HeapTupleMayBeUpdated)
    {
        Assert(result == HeapTupleSelfUpdated ||
               result == HeapTupleUpdated ||
               result == HeapTupleBeingUpdated);
        Assert(!(tp.t_data->t_infomask & HEAP_XMAX_INVALID));
        hufd->ctid = tp.t_data->t_ctid;
        hufd->xmax = HeapTupleHeaderGetUpdateXid(tp.t_data);
        if (result == HeapTupleSelfUpdated)
            hufd->cmax = HeapTupleHeaderGetCmax(tp.t_data);
        else
            hufd->cmax = 0;     /* for lack of an InvalidCommandId value */
        UnlockReleaseBuffer(buffer);
        if (have_tuple_lock)
            UnlockTupleTuplock(relation, &(tp.t_self), LockTupleExclusive);
        if (vmbuffer != InvalidBuffer)
            ReleaseBuffer(vmbuffer);
        return result;
    }

    /*
     * We're about to do the actual delete -- check for conflict first, to
     * avoid possibly having to roll back work we've just done.
     */
    CheckForSerializableConflictIn(relation, &tp, buffer);

    /* replace cid with a combo cid if necessary */
    HeapTupleHeaderAdjustCmax(tp.t_data, &cid, &iscombo);

    START_CRIT_SECTION();

    /*
     * If this transaction commits, the tuple will become DEAD sooner or
     * later.  Set flag that this page is a candidate for pruning once our xid
     * falls below the OldestXmin horizon.  If the transaction finally aborts,
     * the subsequent page pruning will be a no-op and the hint will be
     * cleared.
     */
    PageSetPrunable(page, xid);

    if (PageIsAllVisible(page))
    {
        all_visible_cleared = true;
        PageClearAllVisible(page);
        visibilitymap_clear(relation, BufferGetBlockNumber(buffer),
                            vmbuffer);
    }

    /*
     * If this is the first possibly-multixact-able operation in the
     * current transaction, set my per-backend OldestMemberMXactId setting.
     * We can be certain that the transaction will never become a member of
     * any older MultiXactIds than that.  (We have to do this even if we
     * end up just using our own TransactionId below, since some other
     * backend could incorporate our XID into a MultiXact immediately
     * afterwards.)
     */
    MultiXactIdSetOldestMember();

    compute_new_xmax_infomask(HeapTupleHeaderGetRawXmax(tp.t_data),
                              tp.t_data->t_infomask, tp.t_data->t_infomask2,
                              xid, LockTupleExclusive, true,
                              &new_xmax, &new_infomask, &new_infomask2);

    /* store transaction information of xact deleting the tuple */
    tp.t_data->t_infomask &= ~(HEAP_XMAX_BITS | HEAP_MOVED);
    tp.t_data->t_infomask2 &= ~HEAP_KEYS_UPDATED;
    tp.t_data->t_infomask |= new_infomask;
    tp.t_data->t_infomask2 |= new_infomask2;
    HeapTupleHeaderClearHotUpdated(tp.t_data);
    HeapTupleHeaderSetXmax(tp.t_data, new_xmax);
    HeapTupleHeaderSetCmax(tp.t_data, cid, iscombo);
    /* Make sure there is no forward chain link in t_ctid */
    tp.t_data->t_ctid = tp.t_self;

    MarkBufferDirty(buffer);

    /* XLOG stuff */
    if (RelationNeedsWAL(relation))
    {
        xl_heap_delete xlrec;
        XLogRecPtr  recptr;
        XLogRecData rdata[2];

        xlrec.all_visible_cleared = all_visible_cleared;
        xlrec.infobits_set = compute_infobits(tp.t_data->t_infomask,
                                              tp.t_data->t_infomask2);
        xlrec.target.node = relation->rd_node;
        xlrec.target.tid = tp.t_self;
        xlrec.xmax = new_xmax;
        rdata[0].data = (char *) &xlrec;
        rdata[0].len = SizeOfHeapDelete;
        rdata[0].buffer = InvalidBuffer;
        rdata[0].next = &(rdata[1]);

        rdata[1].data = NULL;
        rdata[1].len = 0;
        rdata[1].buffer = buffer;
        rdata[1].buffer_std = true;
        rdata[1].next = NULL;

        recptr = XLogInsert(RM_HEAP_ID, XLOG_HEAP_DELETE, rdata);

        PageSetLSN(page, recptr);
    }

    END_CRIT_SECTION();

    LockBuffer(buffer, BUFFER_LOCK_UNLOCK);

    if (vmbuffer != InvalidBuffer)
        ReleaseBuffer(vmbuffer);

    /*
     * If the tuple has toasted out-of-line attributes, we need to delete
     * those items too.  We have to do this before releasing the buffer
     * because we need to look at the contents of the tuple, but it's OK to
     * release the content lock on the buffer first.
     */
    if (relation->rd_rel->relkind != RELKIND_RELATION &&
        relation->rd_rel->relkind != RELKIND_MATVIEW)
    {
        /* toast table entries should never be recursively toasted */
        Assert(!HeapTupleHasExternal(&tp));
    }
    else if (HeapTupleHasExternal(&tp))
        toast_delete(relation, &tp);

    /*
     * Mark tuple for invalidation from system caches at next command
     * boundary. We have to do this before releasing the buffer because we
     * need to look at the contents of the tuple.
     */
    CacheInvalidateHeapTuple(relation, &tp, NULL);

    /* Now we can release the buffer */
    ReleaseBuffer(buffer);

    /*
     * Release the lmgr tuple lock, if we had it.
     */
    if (have_tuple_lock)
        UnlockTupleTuplock(relation, &(tp.t_self), LockTupleExclusive);

    pgstat_count_heap_delete(relation);

    return HeapTupleMayBeUpdated;
}

void heap_endscan ( HeapScanDesc  scan  ) 

Definition at line 1398 of file heapam.c.

References BufferIsValid, FreeAccessStrategy(), pfree(), RelationDecrementReferenceCount(), ReleaseBuffer(), HeapScanDescData::rs_cbuf, HeapScanDescData::rs_key, HeapScanDescData::rs_rd, and HeapScanDescData::rs_strategy.

Referenced by AlterDomainNotNull(), AlterTableSpaceOptions(), ATRewriteTable(), boot_openrel(), check_db_file_conflict(), copy_heap_data(), CopyTo(), createdb(), DefineQueryRewrite(), do_autovacuum(), DropSetting(), DropTableSpace(), ExecEndBitmapHeapScan(), ExecEndSeqScan(), find_typed_table_dependencies(), get_database_list(), get_rel_oids(), get_rewrite_oid_without_relid(), get_tables_to_cluster(), get_tablespace_name(), get_tablespace_oid(), getRelationsInNamespace(), gettype(), index_update_stats(), IndexBuildHeapScan(), IndexCheckExclusion(), objectsInSchemaToOids(), pgrowlocks(), pgstat_collect_oids(), pgstat_heap(), ReindexDatabase(), remove_dbtablespaces(), RemoveConversionById(), RenameTableSpace(), systable_endscan(), ThereIsAtLeastOneRole(), vac_truncate_clog(), validate_index_heapscan(), validateCheckConstraint(), validateDomainConstraint(), and validateForeignKeyConstraint().

{
    /* Note: no locking manipulations needed */

    /*
     * unpin scan buffers
     */
    if (BufferIsValid(scan->rs_cbuf))
        ReleaseBuffer(scan->rs_cbuf);

    /*
     * decrement relation reference count and free scan descriptor storage
     */
    RelationDecrementReferenceCount(scan->rs_rd);

    if (scan->rs_key)
        pfree(scan->rs_key);

    if (scan->rs_strategy != NULL)
        FreeAccessStrategy(scan->rs_strategy);

    pfree(scan);
}

bool heap_fetch ( Relation  relation,
Snapshot  snapshot,
HeapTuple  tuple,
Buffer userbuf,
bool  keep_buf,
Relation  stats_relation 
)

Definition at line 1515 of file heapam.c.

References BUFFER_LOCK_SHARE, BUFFER_LOCK_UNLOCK, BufferGetPage, CheckForSerializableConflictOut(), HeapTupleSatisfiesVisibility, ItemIdGetLength, ItemIdIsNormal, ItemPointerGetBlockNumber, ItemPointerGetOffsetNumber, LockBuffer(), PageGetItem, PageGetItemId, PageGetMaxOffsetNumber, pgstat_count_heap_fetch, PredicateLockTuple(), ReadBuffer(), RelationGetRelid, ReleaseBuffer(), HeapTupleData::t_data, HeapTupleData::t_len, HeapTupleData::t_self, and HeapTupleData::t_tableOid.

Referenced by AfterTriggerExecute(), EvalPlanQualFetch(), EvalPlanQualFetchRowMarks(), ExecDelete(), ExecLockRows(), heap_lock_updated_tuple_rec(), and TidNext().

{
    ItemPointer tid = &(tuple->t_self);
    ItemId      lp;
    Buffer      buffer;
    Page        page;
    OffsetNumber offnum;
    bool        valid;

    /*
     * Fetch and pin the appropriate page of the relation.
     */
    buffer = ReadBuffer(relation, ItemPointerGetBlockNumber(tid));

    /*
     * Need share lock on buffer to examine tuple commit status.
     */
    LockBuffer(buffer, BUFFER_LOCK_SHARE);
    page = BufferGetPage(buffer);

    /*
     * We'd better check for out-of-range offnum in case of VACUUM since the
     * TID was obtained.
     */
    offnum = ItemPointerGetOffsetNumber(tid);
    if (offnum < FirstOffsetNumber || offnum > PageGetMaxOffsetNumber(page))
    {
        LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
        if (keep_buf)
            *userbuf = buffer;
        else
        {
            ReleaseBuffer(buffer);
            *userbuf = InvalidBuffer;
        }
        tuple->t_data = NULL;
        return false;
    }

    /*
     * get the item line pointer corresponding to the requested tid
     */
    lp = PageGetItemId(page, offnum);

    /*
     * Must check for deleted tuple.
     */
    if (!ItemIdIsNormal(lp))
    {
        LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
        if (keep_buf)
            *userbuf = buffer;
        else
        {
            ReleaseBuffer(buffer);
            *userbuf = InvalidBuffer;
        }
        tuple->t_data = NULL;
        return false;
    }

    /*
     * fill in *tuple fields
     */
    tuple->t_data = (HeapTupleHeader) PageGetItem(page, lp);
    tuple->t_len = ItemIdGetLength(lp);
    tuple->t_tableOid = RelationGetRelid(relation);

    /*
     * check time qualification of tuple, then release lock
     */
    valid = HeapTupleSatisfiesVisibility(tuple, snapshot, buffer);

    if (valid)
        PredicateLockTuple(relation, tuple, snapshot);

    CheckForSerializableConflictOut(valid, relation, tuple, buffer, snapshot);

    LockBuffer(buffer, BUFFER_LOCK_UNLOCK);

    if (valid)
    {
        /*
         * All checks passed, so return the tuple as valid. Caller is now
         * responsible for releasing the buffer.
         */
        *userbuf = buffer;

        /* Count the successful fetch against appropriate rel, if any */
        if (stats_relation != NULL)
            pgstat_count_heap_fetch(stats_relation);

        return true;
    }

    /* Tuple failed time qual, but maybe caller wants to see it anyway. */
    if (keep_buf)
        *userbuf = buffer;
    else
    {
        ReleaseBuffer(buffer);
        *userbuf = InvalidBuffer;
    }

    return false;
}

bool heap_freeze_tuple ( HeapTupleHeader  tuple,
TransactionId  cutoff_xid,
TransactionId  cutoff_multi 
)
void heap_get_latest_tid ( Relation  relation,
Snapshot  snapshot,
ItemPointer  tid 
)

Definition at line 1805 of file heapam.c.

References BUFFER_LOCK_SHARE, BufferGetPage, CheckForSerializableConflictOut(), elog, ERROR, HEAP_XMAX_INVALID, HeapTupleHeaderGetUpdateXid, HeapTupleHeaderGetXmin, HeapTupleHeaderIsOnlyLocked(), HeapTupleSatisfiesVisibility, ItemIdGetLength, ItemIdIsNormal, ItemPointerEquals(), ItemPointerGetBlockNumber, ItemPointerGetOffsetNumber, ItemPointerIsValid, LockBuffer(), PageGetItem, PageGetItemId, PageGetMaxOffsetNumber, ReadBuffer(), RelationGetNumberOfBlocks, RelationGetRelationName, HeapTupleHeaderData::t_ctid, HeapTupleData::t_data, HeapTupleHeaderData::t_infomask, HeapTupleData::t_len, HeapTupleData::t_self, TransactionIdEquals, TransactionIdIsValid, and UnlockReleaseBuffer().

Referenced by currtid_byrelname(), currtid_byreloid(), and TidNext().

{
    BlockNumber blk;
    ItemPointerData ctid;
    TransactionId priorXmax;

    /* this is to avoid Assert failures on bad input */
    if (!ItemPointerIsValid(tid))
        return;

    /*
     * Since this can be called with user-supplied TID, don't trust the input
     * too much.  (RelationGetNumberOfBlocks is an expensive check, so we
     * don't check t_ctid links again this way.  Note that it would not do to
     * call it just once and save the result, either.)
     */
    blk = ItemPointerGetBlockNumber(tid);
    if (blk >= RelationGetNumberOfBlocks(relation))
        elog(ERROR, "block number %u is out of range for relation \"%s\"",
             blk, RelationGetRelationName(relation));

    /*
     * Loop to chase down t_ctid links.  At top of loop, ctid is the tuple we
     * need to examine, and *tid is the TID we will return if ctid turns out
     * to be bogus.
     *
     * Note that we will loop until we reach the end of the t_ctid chain.
     * Depending on the snapshot passed, there might be at most one visible
     * version of the row, but we don't try to optimize for that.
     */
    ctid = *tid;
    priorXmax = InvalidTransactionId;   /* cannot check first XMIN */
    for (;;)
    {
        Buffer      buffer;
        Page        page;
        OffsetNumber offnum;
        ItemId      lp;
        HeapTupleData tp;
        bool        valid;

        /*
         * Read, pin, and lock the page.
         */
        buffer = ReadBuffer(relation, ItemPointerGetBlockNumber(&ctid));
        LockBuffer(buffer, BUFFER_LOCK_SHARE);
        page = BufferGetPage(buffer);

        /*
         * Check for bogus item number.  This is not treated as an error
         * condition because it can happen while following a t_ctid link. We
         * just assume that the prior tid is OK and return it unchanged.
         */
        offnum = ItemPointerGetOffsetNumber(&ctid);
        if (offnum < FirstOffsetNumber || offnum > PageGetMaxOffsetNumber(page))
        {
            UnlockReleaseBuffer(buffer);
            break;
        }
        lp = PageGetItemId(page, offnum);
        if (!ItemIdIsNormal(lp))
        {
            UnlockReleaseBuffer(buffer);
            break;
        }

        /* OK to access the tuple */
        tp.t_self = ctid;
        tp.t_data = (HeapTupleHeader) PageGetItem(page, lp);
        tp.t_len = ItemIdGetLength(lp);

        /*
         * After following a t_ctid link, we might arrive at an unrelated
         * tuple.  Check for XMIN match.
         */
        if (TransactionIdIsValid(priorXmax) &&
            !TransactionIdEquals(priorXmax, HeapTupleHeaderGetXmin(tp.t_data)))
        {
            UnlockReleaseBuffer(buffer);
            break;
        }

        /*
         * Check time qualification of tuple; if visible, set it as the new
         * result candidate.
         */
        valid = HeapTupleSatisfiesVisibility(&tp, snapshot, buffer);
        CheckForSerializableConflictOut(valid, relation, &tp, buffer, snapshot);
        if (valid)
            *tid = ctid;

        /*
         * If there's a valid t_ctid link, follow it, else we're done.
         */
        if ((tp.t_data->t_infomask & HEAP_XMAX_INVALID) ||
            HeapTupleHeaderIsOnlyLocked(tp.t_data) ||
            ItemPointerEquals(&tp.t_self, &tp.t_data->t_ctid))
        {
            UnlockReleaseBuffer(buffer);
            break;
        }

        ctid = tp.t_data->t_ctid;
        priorXmax = HeapTupleHeaderGetUpdateXid(tp.t_data);
        UnlockReleaseBuffer(buffer);
    }                           /* end of loop */
}

void heap_get_root_tuples ( Page  page,
OffsetNumber root_offsets 
)

Definition at line 705 of file pruneheap.c.

References Assert, FirstOffsetNumber, HeapTupleHeaderGetUpdateXid, HeapTupleHeaderGetXmin, HeapTupleHeaderIsHeapOnly, HeapTupleHeaderIsHotUpdated, ItemIdGetRedirect, ItemIdIsDead, ItemIdIsNormal, ItemIdIsRedirected, ItemIdIsUsed, ItemPointerGetOffsetNumber, MaxHeapTuplesPerPage, MemSet, OffsetNumberNext, PageGetItem, PageGetItemId, PageGetMaxOffsetNumber, HeapTupleHeaderData::t_ctid, TransactionIdEquals, and TransactionIdIsValid.

Referenced by IndexBuildHeapScan(), and validate_index_heapscan().

{
    OffsetNumber offnum,
                maxoff;

    MemSet(root_offsets, 0, MaxHeapTuplesPerPage * sizeof(OffsetNumber));

    maxoff = PageGetMaxOffsetNumber(page);
    for (offnum = FirstOffsetNumber; offnum <= maxoff; offnum = OffsetNumberNext(offnum))
    {
        ItemId      lp = PageGetItemId(page, offnum);
        HeapTupleHeader htup;
        OffsetNumber nextoffnum;
        TransactionId priorXmax;

        /* skip unused and dead items */
        if (!ItemIdIsUsed(lp) || ItemIdIsDead(lp))
            continue;

        if (ItemIdIsNormal(lp))
        {
            htup = (HeapTupleHeader) PageGetItem(page, lp);

            /*
             * Check if this tuple is part of a HOT-chain rooted at some other
             * tuple. If so, skip it for now; we'll process it when we find
             * its root.
             */
            if (HeapTupleHeaderIsHeapOnly(htup))
                continue;

            /*
             * This is either a plain tuple or the root of a HOT-chain.
             * Remember it in the mapping.
             */
            root_offsets[offnum - 1] = offnum;

            /* If it's not the start of a HOT-chain, we're done with it */
            if (!HeapTupleHeaderIsHotUpdated(htup))
                continue;

            /* Set up to scan the HOT-chain */
            nextoffnum = ItemPointerGetOffsetNumber(&htup->t_ctid);
            priorXmax = HeapTupleHeaderGetUpdateXid(htup);
        }
        else
        {
            /* Must be a redirect item. We do not set its root_offsets entry */
            Assert(ItemIdIsRedirected(lp));
            /* Set up to scan the HOT-chain */
            nextoffnum = ItemIdGetRedirect(lp);
            priorXmax = InvalidTransactionId;
        }

        /*
         * Now follow the HOT-chain and collect other tuples in the chain.
         *
         * Note: Even though this is a nested loop, the complexity of the
         * function is O(N) because a tuple in the page should be visited not
         * more than twice, once in the outer loop and once in HOT-chain
         * chases.
         */
        for (;;)
        {
            lp = PageGetItemId(page, nextoffnum);

            /* Check for broken chains */
            if (!ItemIdIsNormal(lp))
                break;

            htup = (HeapTupleHeader) PageGetItem(page, lp);

            if (TransactionIdIsValid(priorXmax) &&
                !TransactionIdEquals(priorXmax, HeapTupleHeaderGetXmin(htup)))
                break;

            /* Remember the root line pointer for this item */
            root_offsets[nextoffnum - 1] = offnum;

            /* Advance to next chain member, if any */
            if (!HeapTupleHeaderIsHotUpdated(htup))
                break;

            nextoffnum = ItemPointerGetOffsetNumber(&htup->t_ctid);
            priorXmax = HeapTupleHeaderGetUpdateXid(htup);
        }
    }
}

HeapTuple heap_getnext ( HeapScanDesc  scan,
ScanDirection  direction 
)

Definition at line 1447 of file heapam.c.

References heapgettup(), heapgettup_pagemode(), pgstat_count_heap_getnext, HeapScanDescData::rs_ctup, HeapScanDescData::rs_key, HeapScanDescData::rs_nkeys, HeapScanDescData::rs_pageatatime, HeapScanDescData::rs_rd, and HeapTupleData::t_data.

Referenced by AlterDomainNotNull(), AlterTableSpaceOptions(), ATRewriteTable(), boot_openrel(), check_db_file_conflict(), copy_heap_data(), CopyTo(), createdb(), DefineQueryRewrite(), do_autovacuum(), DropSetting(), DropTableSpace(), find_typed_table_dependencies(), get_database_list(), get_rel_oids(), get_rewrite_oid_without_relid(), get_tables_to_cluster(), get_tablespace_name(), get_tablespace_oid(), getRelationsInNamespace(), gettype(), index_update_stats(), IndexBuildHeapScan(), IndexCheckExclusion(), objectsInSchemaToOids(), pgrowlocks(), pgstat_collect_oids(), pgstat_heap(), ReindexDatabase(), remove_dbtablespaces(), RemoveConversionById(), RenameTableSpace(), SeqNext(), systable_getnext(), ThereIsAtLeastOneRole(), vac_truncate_clog(), validate_index_heapscan(), validateCheckConstraint(), validateDomainConstraint(), and validateForeignKeyConstraint().

{
    /* Note: no locking manipulations needed */

    HEAPDEBUG_1;                /* heap_getnext( info ) */

    if (scan->rs_pageatatime)
        heapgettup_pagemode(scan, direction,
                            scan->rs_nkeys, scan->rs_key);
    else
        heapgettup(scan, direction, scan->rs_nkeys, scan->rs_key);

    if (scan->rs_ctup.t_data == NULL)
    {
        HEAPDEBUG_2;            /* heap_getnext returning EOS */
        return NULL;
    }

    /*
     * if we get here it means we have a new current scan tuple, so point to
     * the proper return buffer and return the tuple.
     */
    HEAPDEBUG_3;                /* heap_getnext returning tuple */

    pgstat_count_heap_getnext(scan->rs_rd);

    return &(scan->rs_ctup);
}

bool heap_hot_search ( ItemPointer  tid,
Relation  relation,
Snapshot  snapshot,
bool all_dead 
)

Definition at line 1777 of file heapam.c.

References BUFFER_LOCK_SHARE, BUFFER_LOCK_UNLOCK, heap_hot_search_buffer(), ItemPointerGetBlockNumber, LockBuffer(), ReadBuffer(), and ReleaseBuffer().

Referenced by _bt_check_unique().

{
    bool        result;
    Buffer      buffer;
    HeapTupleData heapTuple;

    buffer = ReadBuffer(relation, ItemPointerGetBlockNumber(tid));
    LockBuffer(buffer, BUFFER_LOCK_SHARE);
    result = heap_hot_search_buffer(tid, relation, buffer, snapshot,
                                    &heapTuple, all_dead, true);
    LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
    ReleaseBuffer(buffer);
    return result;
}

bool heap_hot_search_buffer ( ItemPointer  tid,
Relation  relation,
Buffer  buffer,
Snapshot  snapshot,
HeapTuple  heapTuple,
bool all_dead,
bool  first_call 
)

Definition at line 1649 of file heapam.c.

References Assert, BufferGetBlockNumber(), BufferGetPage, CheckForSerializableConflictOut(), HeapTupleHeaderGetUpdateXid, HeapTupleHeaderGetXmin, HeapTupleIsHeapOnly, HeapTupleIsHotUpdated, HeapTupleIsSurelyDead(), HeapTupleSatisfiesVisibility, ItemIdGetLength, ItemIdGetRedirect, ItemIdIsNormal, ItemIdIsRedirected, ItemPointerGetBlockNumber, ItemPointerGetOffsetNumber, ItemPointerSetOffsetNumber, PageGetItem, PageGetItemId, PageGetMaxOffsetNumber, PredicateLockTuple(), RelationData::rd_id, RecentGlobalXmin, HeapTupleHeaderData::t_ctid, HeapTupleData::t_data, HeapTupleData::t_len, HeapTupleData::t_self, HeapTupleData::t_tableOid, TransactionIdEquals, and TransactionIdIsValid.

Referenced by bitgetpage(), heap_hot_search(), and index_fetch_heap().

{
    Page        dp = (Page) BufferGetPage(buffer);
    TransactionId prev_xmax = InvalidTransactionId;
    OffsetNumber offnum;
    bool        at_chain_start;
    bool        valid;
    bool        skip;

    /* If this is not the first call, previous call returned a (live!) tuple */
    if (all_dead)
        *all_dead = first_call;

    Assert(TransactionIdIsValid(RecentGlobalXmin));

    Assert(ItemPointerGetBlockNumber(tid) == BufferGetBlockNumber(buffer));
    offnum = ItemPointerGetOffsetNumber(tid);
    at_chain_start = first_call;
    skip = !first_call;

    /* Scan through possible multiple members of HOT-chain */
    for (;;)
    {
        ItemId      lp;

        /* check for bogus TID */
        if (offnum < FirstOffsetNumber || offnum > PageGetMaxOffsetNumber(dp))
            break;

        lp = PageGetItemId(dp, offnum);

        /* check for unused, dead, or redirected items */
        if (!ItemIdIsNormal(lp))
        {
            /* We should only see a redirect at start of chain */
            if (ItemIdIsRedirected(lp) && at_chain_start)
            {
                /* Follow the redirect */
                offnum = ItemIdGetRedirect(lp);
                at_chain_start = false;
                continue;
            }
            /* else must be end of chain */
            break;
        }

        heapTuple->t_data = (HeapTupleHeader) PageGetItem(dp, lp);
        heapTuple->t_len = ItemIdGetLength(lp);
        heapTuple->t_tableOid = relation->rd_id;
        heapTuple->t_self = *tid;

        /*
         * Shouldn't see a HEAP_ONLY tuple at chain start.
         */
        if (at_chain_start && HeapTupleIsHeapOnly(heapTuple))
            break;

        /*
         * The xmin should match the previous xmax value, else chain is
         * broken.
         */
        if (TransactionIdIsValid(prev_xmax) &&
            !TransactionIdEquals(prev_xmax,
                                 HeapTupleHeaderGetXmin(heapTuple->t_data)))
            break;

        /*
         * When first_call is true (and thus, skip is initially false) we'll
         * return the first tuple we find.  But on later passes, heapTuple
         * will initially be pointing to the tuple we returned last time.
         * Returning it again would be incorrect (and would loop forever), so
         * we skip it and return the next match we find.
         */
        if (!skip)
        {
            /* If it's visible per the snapshot, we must return it */
            valid = HeapTupleSatisfiesVisibility(heapTuple, snapshot, buffer);
            CheckForSerializableConflictOut(valid, relation, heapTuple,
                                            buffer, snapshot);
            if (valid)
            {
                ItemPointerSetOffsetNumber(tid, offnum);
                PredicateLockTuple(relation, heapTuple, snapshot);
                if (all_dead)
                    *all_dead = false;
                return true;
            }
        }
        skip = false;

        /*
         * If we can't see it, maybe no one else can either.  At caller
         * request, check whether all chain members are dead to all
         * transactions.
         */
        if (all_dead && *all_dead &&
            !HeapTupleIsSurelyDead(heapTuple->t_data, RecentGlobalXmin))
            *all_dead = false;

        /*
         * Check to see if HOT chain continues past this tuple; if so fetch
         * the next offnum and loop around.
         */
        if (HeapTupleIsHotUpdated(heapTuple))
        {
            Assert(ItemPointerGetBlockNumber(&heapTuple->t_data->t_ctid) ==
                   ItemPointerGetBlockNumber(tid));
            offnum = ItemPointerGetOffsetNumber(&heapTuple->t_data->t_ctid);
            at_chain_start = false;
            prev_xmax = HeapTupleHeaderGetUpdateXid(heapTuple->t_data);
        }
        else
            break;              /* end of chain */
    }

    return false;
}

void heap_inplace_update ( Relation  relation,
HeapTuple  tuple 
)

Definition at line 4968 of file heapam.c.

References XLogRecData::buffer, BUFFER_LOCK_EXCLUSIVE, XLogRecData::buffer_std, BufferGetPage, CacheInvalidateHeapTuple(), XLogRecData::data, elog, END_CRIT_SECTION, ERROR, IsBootstrapProcessingMode, ItemIdGetLength, ItemIdIsNormal, ItemPointerGetBlockNumber, ItemPointerGetOffsetNumber, XLogRecData::len, LockBuffer(), MarkBufferDirty(), XLogRecData::next, xl_heaptid::node, PageGetItem, PageGetItemId, PageGetMaxOffsetNumber, PageSetLSN, RelationData::rd_node, ReadBuffer(), RelationNeedsWAL, START_CRIT_SECTION, HeapTupleData::t_data, HeapTupleHeaderData::t_hoff, HeapTupleData::t_len, HeapTupleData::t_self, xl_heap_inplace::target, xl_heaptid::tid, UnlockReleaseBuffer(), XLOG_HEAP_INPLACE, and XLogInsert().

Referenced by create_toast_table(), index_set_state_flags(), index_update_stats(), vac_update_datfrozenxid(), and vac_update_relstats().

{
    Buffer      buffer;
    Page        page;
    OffsetNumber offnum;
    ItemId      lp = NULL;
    HeapTupleHeader htup;
    uint32      oldlen;
    uint32      newlen;

    buffer = ReadBuffer(relation, ItemPointerGetBlockNumber(&(tuple->t_self)));
    LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE);
    page = (Page) BufferGetPage(buffer);

    offnum = ItemPointerGetOffsetNumber(&(tuple->t_self));
    if (PageGetMaxOffsetNumber(page) >= offnum)
        lp = PageGetItemId(page, offnum);

    if (PageGetMaxOffsetNumber(page) < offnum || !ItemIdIsNormal(lp))
        elog(ERROR, "heap_inplace_update: invalid lp");

    htup = (HeapTupleHeader) PageGetItem(page, lp);

    oldlen = ItemIdGetLength(lp) - htup->t_hoff;
    newlen = tuple->t_len - tuple->t_data->t_hoff;
    if (oldlen != newlen || htup->t_hoff != tuple->t_data->t_hoff)
        elog(ERROR, "heap_inplace_update: wrong tuple length");

    /* NO EREPORT(ERROR) from here till changes are logged */
    START_CRIT_SECTION();

    memcpy((char *) htup + htup->t_hoff,
           (char *) tuple->t_data + tuple->t_data->t_hoff,
           newlen);

    MarkBufferDirty(buffer);

    /* XLOG stuff */
    if (RelationNeedsWAL(relation))
    {
        xl_heap_inplace xlrec;
        XLogRecPtr  recptr;
        XLogRecData rdata[2];

        xlrec.target.node = relation->rd_node;
        xlrec.target.tid = tuple->t_self;

        rdata[0].data = (char *) &xlrec;
        rdata[0].len = SizeOfHeapInplace;
        rdata[0].buffer = InvalidBuffer;
        rdata[0].next = &(rdata[1]);

        rdata[1].data = (char *) htup + htup->t_hoff;
        rdata[1].len = newlen;
        rdata[1].buffer = buffer;
        rdata[1].buffer_std = true;
        rdata[1].next = NULL;

        recptr = XLogInsert(RM_HEAP_ID, XLOG_HEAP_INPLACE, rdata);

        PageSetLSN(page, recptr);
    }

    END_CRIT_SECTION();

    UnlockReleaseBuffer(buffer);

    /*
     * Send out shared cache inval if necessary.  Note that because we only
     * pass the new version of the tuple, this mustn't be used for any
     * operations that could change catcache lookup keys.  But we aren't
     * bothering with index updates either, so that's true a fortiori.
     */
    if (!IsBootstrapProcessingMode())
        CacheInvalidateHeapTuple(relation, tuple, NULL);
}

Oid heap_insert ( Relation  relation,
HeapTuple  tup,
CommandId  cid,
int  options,
BulkInsertState  bistate 
)

Definition at line 2012 of file heapam.c.

References xl_heap_insert::all_visible_cleared, XLogRecData::buffer, XLogRecData::buffer_std, BufferGetPage, CacheInvalidateHeapTuple(), CheckForSerializableConflictIn(), XLogRecData::data, END_CRIT_SECTION, FirstOffsetNumber, GetCurrentTransactionId(), heap_freetuple(), HEAP_INSERT_SKIP_WAL, heap_prepare_insert(), HeapTupleGetOid, InvalidBuffer, ItemPointerGetBlockNumber, ItemPointerGetOffsetNumber, XLogRecData::len, MarkBufferDirty(), XLogRecData::next, xl_heaptid::node, offsetof, PageClearAllVisible, PageGetMaxOffsetNumber, PageIsAllVisible, PageSetLSN, pgstat_count_heap_insert(), RelationData::rd_node, RelationGetBufferForTuple(), RelationNeedsWAL, RelationPutHeapTuple(), ReleaseBuffer(), START_CRIT_SECTION, HeapTupleData::t_data, HeapTupleHeaderData::t_hoff, xl_heap_header::t_hoff, HeapTupleHeaderData::t_infomask, xl_heap_header::t_infomask, HeapTupleHeaderData::t_infomask2, xl_heap_header::t_infomask2, HeapTupleData::t_len, HeapTupleData::t_self, xl_heap_insert::target, xl_heaptid::tid, UnlockReleaseBuffer(), visibilitymap_clear(), and XLogInsert().

Referenced by ATRewriteTable(), CopyFrom(), ExecInsert(), intorel_receive(), simple_heap_insert(), toast_save_datum(), and transientrel_receive().

{
    TransactionId xid = GetCurrentTransactionId();
    HeapTuple   heaptup;
    Buffer      buffer;
    Buffer      vmbuffer = InvalidBuffer;
    bool        all_visible_cleared = false;

    /*
     * Fill in tuple header fields, assign an OID, and toast the tuple if
     * necessary.
     *
     * Note: below this point, heaptup is the data we actually intend to store
     * into the relation; tup is the caller's original untoasted data.
     */
    heaptup = heap_prepare_insert(relation, tup, xid, cid, options);

    /*
     * We're about to do the actual insert -- but check for conflict first, to
     * avoid possibly having to roll back work we've just done.
     *
     * For a heap insert, we only need to check for table-level SSI locks. Our
     * new tuple can't possibly conflict with existing tuple locks, and heap
     * page locks are only consolidated versions of tuple locks; they do not
     * lock "gaps" as index page locks do.  So we don't need to identify a
     * buffer before making the call.
     */
    CheckForSerializableConflictIn(relation, NULL, InvalidBuffer);

    /*
     * Find buffer to insert this tuple into.  If the page is all visible,
     * this will also pin the requisite visibility map page.
     */
    buffer = RelationGetBufferForTuple(relation, heaptup->t_len,
                                       InvalidBuffer, options, bistate,
                                       &vmbuffer, NULL);

    /* NO EREPORT(ERROR) from here till changes are logged */
    START_CRIT_SECTION();

    RelationPutHeapTuple(relation, buffer, heaptup);

    if (PageIsAllVisible(BufferGetPage(buffer)))
    {
        all_visible_cleared = true;
        PageClearAllVisible(BufferGetPage(buffer));
        visibilitymap_clear(relation,
                            ItemPointerGetBlockNumber(&(heaptup->t_self)),
                            vmbuffer);
    }

    /*
     * XXX Should we set PageSetPrunable on this page ?
     *
     * The inserting transaction may eventually abort thus making this tuple
     * DEAD and hence available for pruning. Though we don't want to optimize
     * for aborts, if no other tuple in this page is UPDATEd/DELETEd, the
     * aborted tuple will never be pruned until next vacuum is triggered.
     *
     * If you do add PageSetPrunable here, add it in heap_xlog_insert too.
     */

    MarkBufferDirty(buffer);

    /* XLOG stuff */
    if (!(options & HEAP_INSERT_SKIP_WAL) && RelationNeedsWAL(relation))
    {
        xl_heap_insert xlrec;
        xl_heap_header xlhdr;
        XLogRecPtr  recptr;
        XLogRecData rdata[3];
        Page        page = BufferGetPage(buffer);
        uint8       info = XLOG_HEAP_INSERT;

        xlrec.all_visible_cleared = all_visible_cleared;
        xlrec.target.node = relation->rd_node;
        xlrec.target.tid = heaptup->t_self;
        rdata[0].data = (char *) &xlrec;
        rdata[0].len = SizeOfHeapInsert;
        rdata[0].buffer = InvalidBuffer;
        rdata[0].next = &(rdata[1]);

        xlhdr.t_infomask2 = heaptup->t_data->t_infomask2;
        xlhdr.t_infomask = heaptup->t_data->t_infomask;
        xlhdr.t_hoff = heaptup->t_data->t_hoff;

        /*
         * note we mark rdata[1] as belonging to buffer; if XLogInsert decides
         * to write the whole page to the xlog, we don't need to store
         * xl_heap_header in the xlog.
         */
        rdata[1].data = (char *) &xlhdr;
        rdata[1].len = SizeOfHeapHeader;
        rdata[1].buffer = buffer;
        rdata[1].buffer_std = true;
        rdata[1].next = &(rdata[2]);

        /* PG73FORMAT: write bitmap [+ padding] [+ oid] + data */
        rdata[2].data = (char *) heaptup->t_data + offsetof(HeapTupleHeaderData, t_bits);
        rdata[2].len = heaptup->t_len - offsetof(HeapTupleHeaderData, t_bits);
        rdata[2].buffer = buffer;
        rdata[2].buffer_std = true;
        rdata[2].next = NULL;

        /*
         * If this is the single and first tuple on page, we can reinit the
         * page instead of restoring the whole thing.  Set flag, and hide
         * buffer references from XLogInsert.
         */
        if (ItemPointerGetOffsetNumber(&(heaptup->t_self)) == FirstOffsetNumber &&
            PageGetMaxOffsetNumber(page) == FirstOffsetNumber)
        {
            info |= XLOG_HEAP_INIT_PAGE;
            rdata[1].buffer = rdata[2].buffer = InvalidBuffer;
        }

        recptr = XLogInsert(RM_HEAP_ID, info, rdata);

        PageSetLSN(page, recptr);
    }

    END_CRIT_SECTION();

    UnlockReleaseBuffer(buffer);
    if (vmbuffer != InvalidBuffer)
        ReleaseBuffer(vmbuffer);

    /*
     * If tuple is cachable, mark it for invalidation from the caches in case
     * we abort.  Note it is OK to do this after releasing the buffer, because
     * the heaptup data structure is all in local memory, not in the shared
     * buffer.
     */
    CacheInvalidateHeapTuple(relation, heaptup, NULL);

    pgstat_count_heap_insert(relation, 1);

    /*
     * If heaptup is a private copy, release it.  Don't forget to copy t_self
     * back to the caller's image, too.
     */
    if (heaptup != tup)
    {
        tup->t_self = heaptup->t_self;
        heap_freetuple(heaptup);
    }

    return HeapTupleGetOid(tup);
}

HTSU_Result heap_lock_tuple ( Relation  relation,
HeapTuple  tuple,
CommandId  cid,
LockTupleMode  mode,
bool  nowait,
bool  follow_update,
Buffer buffer,
HeapUpdateFailureData hufd 
)

Definition at line 3895 of file heapam.c.

References Assert, XLogRecData::buffer, BUFFER_LOCK_EXCLUSIVE, BUFFER_LOCK_UNLOCK, XLogRecData::buffer_std, BufferGetPage, HeapUpdateFailureData::cmax, compute_infobits(), compute_new_xmax_infomask(), ConditionalLockTupleTuplock, ConditionalMultiXactIdWait(), ConditionalXactLockTableWait(), HeapUpdateFailureData::ctid, XLogRecData::data, elog, END_CRIT_SECTION, ereport, errcode(), errmsg(), ERROR, get_mxact_status_for_lock(), GetCurrentTransactionId(), GetMultiXactIdMembers(), HEAP_KEYS_UPDATED, heap_lock_updated_tuple(), HEAP_XMAX_COMMITTED, HEAP_XMAX_INVALID, HEAP_XMAX_IS_EXCL_LOCKED, HEAP_XMAX_IS_KEYSHR_LOCKED, HEAP_XMAX_IS_LOCKED_ONLY, HEAP_XMAX_IS_MULTI, HEAP_XMAX_IS_SHR_LOCKED, HeapTupleBeingUpdated, HeapTupleHeaderClearHotUpdated, HeapTupleHeaderGetCmax(), HeapTupleHeaderGetRawXmax, HeapTupleHeaderGetUpdateXid, HeapTupleHeaderIsOnlyLocked(), HeapTupleHeaderSetXmax, HeapTupleInvisible, HeapTupleMayBeUpdated, HeapTupleSatisfiesUpdate(), HeapTupleSelfUpdated, HeapTupleUpdated, i, xl_heap_lock::infobits_set, ItemIdGetLength, ItemIdIsNormal, ItemPointerCopy, ItemPointerGetBlockNumber, ItemPointerGetOffsetNumber, XLogRecData::len, LockBuffer(), xl_heap_lock::locking_xid, LockTupleKeyShare, LockTupleNoKeyExclusive, LockTupleShare, LockTupleTuplock, MarkBufferDirty(), MultiXactIdSetOldestMember(), MultiXactIdWait(), MultiXactStatusForKeyShare, MultiXactStatusNoKeyUpdate, XLogRecData::next, xl_heaptid::node, PageGetItem, PageGetItemId, PageSetLSN, pfree(), RelationData::rd_node, ReadBuffer(), RelationGetRelationName, RelationGetRelid, RelationNeedsWAL, START_CRIT_SECTION, status(), HeapTupleHeaderData::t_ctid, HeapTupleData::t_data, HeapTupleHeaderData::t_infomask, HeapTupleHeaderData::t_infomask2, HeapTupleData::t_len, HeapTupleData::t_self, HeapTupleData::t_tableOid, xl_heap_lock::target, xl_heaptid::tid, TransactionIdEquals, TransactionIdIsCurrentTransactionId(), TUPLOCK_from_mxstatus, UnlockReleaseBuffer(), UnlockTupleTuplock, UpdateXmaxHintBits(), XactLockTableWait(), XLOG_HEAP_LOCK, XLogInsert(), and HeapUpdateFailureData::xmax.

Referenced by EvalPlanQualFetch(), ExecLockRows(), and GetTupleForTrigger().

{
    HTSU_Result result;
    ItemPointer tid = &(tuple->t_self);
    ItemId      lp;
    Page        page;
    TransactionId xid,
                xmax;
    uint16      old_infomask,
                new_infomask,
                new_infomask2;
    bool        have_tuple_lock = false;

    *buffer = ReadBuffer(relation, ItemPointerGetBlockNumber(tid));
    LockBuffer(*buffer, BUFFER_LOCK_EXCLUSIVE);

    page = BufferGetPage(*buffer);
    lp = PageGetItemId(page, ItemPointerGetOffsetNumber(tid));
    Assert(ItemIdIsNormal(lp));

    tuple->t_data = (HeapTupleHeader) PageGetItem(page, lp);
    tuple->t_len = ItemIdGetLength(lp);
    tuple->t_tableOid = RelationGetRelid(relation);

l3:
    result = HeapTupleSatisfiesUpdate(tuple->t_data, cid, *buffer);

    if (result == HeapTupleInvisible)
    {
        UnlockReleaseBuffer(*buffer);
        elog(ERROR, "attempted to lock invisible tuple");
    }
    else if (result == HeapTupleBeingUpdated)
    {
        TransactionId xwait;
        uint16      infomask;
        uint16      infomask2;
        bool        require_sleep;
        ItemPointerData t_ctid;

        /* must copy state data before unlocking buffer */
        xwait = HeapTupleHeaderGetRawXmax(tuple->t_data);
        infomask = tuple->t_data->t_infomask;
        infomask2 = tuple->t_data->t_infomask2;
        ItemPointerCopy(&tuple->t_data->t_ctid, &t_ctid);

        LockBuffer(*buffer, BUFFER_LOCK_UNLOCK);

        /*
         * If any subtransaction of the current top transaction already holds a
         * lock as strong or stronger than what we're requesting, we
         * effectively hold the desired lock already.  We *must* succeed
         * without trying to take the tuple lock, else we will deadlock against
         * anyone wanting to acquire a stronger lock.
         */
        if (infomask & HEAP_XMAX_IS_MULTI)
        {
            int     i;
            int     nmembers;
            MultiXactMember *members;

            /*
             * We don't need to allow old multixacts here; if that had been the
             * case, HeapTupleSatisfiesUpdate would have returned MayBeUpdated
             * and we wouldn't be here.
             */
            nmembers = GetMultiXactIdMembers(xwait, &members, false);

            for (i = 0; i < nmembers; i++)
            {
                if (TransactionIdIsCurrentTransactionId(members[i].xid))
                {
                    LockTupleMode   membermode;

                    membermode = TUPLOCK_from_mxstatus(members[i].status);

                    if (membermode >= mode)
                    {
                        if (have_tuple_lock)
                            UnlockTupleTuplock(relation, tid, mode);

                        pfree(members);
                        return HeapTupleMayBeUpdated;
                    }
                }
            }

            pfree(members);
        }

        /*
         * Acquire tuple lock to establish our priority for the tuple.
         * LockTuple will release us when we are next-in-line for the tuple.
         * We must do this even if we are share-locking.
         *
         * If we are forced to "start over" below, we keep the tuple lock;
         * this arranges that we stay at the head of the line while rechecking
         * tuple state.
         */
        if (!have_tuple_lock)
        {
            if (nowait)
            {
                if (!ConditionalLockTupleTuplock(relation, tid, mode))
                    ereport(ERROR,
                            (errcode(ERRCODE_LOCK_NOT_AVAILABLE),
                             errmsg("could not obtain lock on row in relation \"%s\"",
                                    RelationGetRelationName(relation))));
            }
            else
                LockTupleTuplock(relation, tid, mode);
            have_tuple_lock = true;
        }

        /*
         * Initially assume that we will have to wait for the locking
         * transaction(s) to finish.  We check various cases below in which
         * this can be turned off.
         */
        require_sleep = true;
        if (mode == LockTupleKeyShare)
        {
            /*
             * If we're requesting KeyShare, and there's no update present, we
             * don't need to wait.  Even if there is an update, we can still
             * continue if the key hasn't been modified.
             *
             * However, if there are updates, we need to walk the update chain
             * to mark future versions of the row as locked, too.  That way, if
             * somebody deletes that future version, we're protected against
             * the key going away.  This locking of future versions could block
             * momentarily, if a concurrent transaction is deleting a key; or
             * it could return a value to the effect that the transaction
             * deleting the key has already committed.  So we do this before
             * re-locking the buffer; otherwise this would be prone to
             * deadlocks.
             *
             * Note that the TID we're locking was grabbed before we unlocked
             * the buffer.  For it to change while we're not looking, the other
             * properties we're testing for below after re-locking the buffer
             * would also change, in which case we would restart this loop
             * above.
             */
            if (!(infomask2 & HEAP_KEYS_UPDATED))
            {
                bool    updated;

                updated = !HEAP_XMAX_IS_LOCKED_ONLY(infomask);

                /*
                 * If there are updates, follow the update chain; bail out
                 * if that cannot be done.
                 */
                if (follow_updates && updated)
                {
                    HTSU_Result     res;

                    res = heap_lock_updated_tuple(relation, tuple, &t_ctid,
                                                  GetCurrentTransactionId(),
                                                  mode);
                    if (res != HeapTupleMayBeUpdated)
                    {
                        result = res;
                        /* recovery code expects to have buffer lock held */
                        LockBuffer(*buffer, BUFFER_LOCK_EXCLUSIVE);
                        goto failed;
                    }
                }

                LockBuffer(*buffer, BUFFER_LOCK_EXCLUSIVE);

                /*
                 * Make sure it's still an appropriate lock, else start over.
                 * Also, if it wasn't updated before we released the lock, but
                 * is updated now, we start over too; the reason is that we now
                 * need to follow the update chain to lock the new versions.
                 */
                if (!HeapTupleHeaderIsOnlyLocked(tuple->t_data) &&
                    ((tuple->t_data->t_infomask2 & HEAP_KEYS_UPDATED) ||
                     !updated))
                    goto l3;

                /* Things look okay, so we can skip sleeping */
                require_sleep = false;

                /*
                 * Note we allow Xmax to change here; other updaters/lockers
                 * could have modified it before we grabbed the buffer lock.
                 * However, this is not a problem, because with the recheck we
                 * just did we ensure that they still don't conflict with the
                 * lock we want.
                 */
            }
        }
        else if (mode == LockTupleShare)
        {
            /*
             * If we're requesting Share, we can similarly avoid sleeping if
             * there's no update and no exclusive lock present.
             */
            if (HEAP_XMAX_IS_LOCKED_ONLY(infomask) &&
                !HEAP_XMAX_IS_EXCL_LOCKED(infomask))
            {
                LockBuffer(*buffer, BUFFER_LOCK_EXCLUSIVE);

                /*
                 * Make sure it's still an appropriate lock, else start over.
                 * See above about allowing xmax to change.
                 */
                if (!HEAP_XMAX_IS_LOCKED_ONLY(tuple->t_data->t_infomask) ||
                    HEAP_XMAX_IS_EXCL_LOCKED(tuple->t_data->t_infomask))
                    goto l3;
                require_sleep = false;
            }
        }
        else if (mode == LockTupleNoKeyExclusive)
        {
            /*
             * If we're requesting NoKeyExclusive, we might also be able to
             * avoid sleeping; just ensure that there's no other lock type than
             * KeyShare.  Note that this is a bit more involved than just
             * checking hint bits -- we need to expand the multixact to figure
             * out lock modes for each one (unless there was only one such
             * locker).
             */
            if (infomask & HEAP_XMAX_IS_MULTI)
            {
                int     nmembers;
                MultiXactMember *members;

                /*
                 * We don't need to allow old multixacts here; if that had been
                 * the case, HeapTupleSatisfiesUpdate would have returned
                 * MayBeUpdated and we wouldn't be here.
                 */
                nmembers = GetMultiXactIdMembers(xwait, &members, false);

                if (nmembers <= 0)
                {
                    /*
                     * No need to keep the previous xmax here. This is unlikely
                     * to happen.
                     */
                    require_sleep = false;
                }
                else
                {
                    int     i;
                    bool    allowed = true;

                    for (i = 0; i < nmembers; i++)
                    {
                        if (members[i].status != MultiXactStatusForKeyShare)
                        {
                            allowed = false;
                            break;
                        }
                    }
                    if (allowed)
                    {
                        /*
                         * if the xmax changed under us in the meantime, start
                         * over.
                         */
                        LockBuffer(*buffer, BUFFER_LOCK_EXCLUSIVE);
                        if (!(tuple->t_data->t_infomask & HEAP_XMAX_IS_MULTI) ||
                            !TransactionIdEquals(HeapTupleHeaderGetRawXmax(tuple->t_data),
                                                 xwait))
                        {
                            pfree(members);
                            goto l3;
                        }
                        /* otherwise, we're good */
                        require_sleep = false;
                    }

                    pfree(members);
                }
            }
            else if (HEAP_XMAX_IS_KEYSHR_LOCKED(infomask))
            {
                LockBuffer(*buffer, BUFFER_LOCK_EXCLUSIVE);

                /* if the xmax changed in the meantime, start over */
                if ((tuple->t_data->t_infomask & HEAP_XMAX_IS_MULTI) ||
                    !TransactionIdEquals(HeapTupleHeaderGetRawXmax(tuple->t_data),
                                         xwait))
                    goto l3;
                /* otherwise, we're good */
                require_sleep = false;
            }
        }

        /*
         * By here, we either have already acquired the buffer exclusive lock,
         * or we must wait for the locking transaction or multixact; so below
         * we ensure that we grab buffer lock after the sleep.
         */

        if (require_sleep)
        {
            if (infomask & HEAP_XMAX_IS_MULTI)
            {
                MultiXactStatus status = get_mxact_status_for_lock(mode, false);

                /* We only ever lock tuples, never update them */
                if (status >= MultiXactStatusNoKeyUpdate)
                    elog(ERROR, "invalid lock mode in heap_lock_tuple");

                /* wait for multixact to end */
                if (nowait)
                {
                    if (!ConditionalMultiXactIdWait((MultiXactId) xwait,
                                                    status, NULL, infomask))
                        ereport(ERROR,
                                (errcode(ERRCODE_LOCK_NOT_AVAILABLE),
                                 errmsg("could not obtain lock on row in relation \"%s\"",
                                        RelationGetRelationName(relation))));
                }
                else
                    MultiXactIdWait((MultiXactId) xwait, status, NULL, infomask);

                /* if there are updates, follow the update chain */
                if (follow_updates &&
                    !HEAP_XMAX_IS_LOCKED_ONLY(infomask))
                {
                    HTSU_Result     res;

                    res = heap_lock_updated_tuple(relation, tuple, &t_ctid,
                                                  GetCurrentTransactionId(),
                                                  mode);
                    if (res != HeapTupleMayBeUpdated)
                    {
                        result = res;
                        /* recovery code expects to have buffer lock held */
                        LockBuffer(*buffer, BUFFER_LOCK_EXCLUSIVE);
                        goto failed;
                    }
                }

                LockBuffer(*buffer, BUFFER_LOCK_EXCLUSIVE);

                /*
                 * If xwait had just locked the tuple then some other xact
                 * could update this tuple before we get to this point. Check
                 * for xmax change, and start over if so.
                 */
                if (!(tuple->t_data->t_infomask & HEAP_XMAX_IS_MULTI) ||
                    !TransactionIdEquals(HeapTupleHeaderGetRawXmax(tuple->t_data),
                                         xwait))
                    goto l3;

                /*
                 * Of course, the multixact might not be done here: if we're
                 * requesting a light lock mode, other transactions with light
                 * locks could still be alive, as well as locks owned by our
                 * own xact or other subxacts of this backend.  We need to
                 * preserve the surviving MultiXact members.  Note that it
                 * isn't absolutely necessary in the latter case, but doing so
                 * is simpler.
                 */
            }
            else
            {
                /* wait for regular transaction to end */
                if (nowait)
                {
                    if (!ConditionalXactLockTableWait(xwait))
                        ereport(ERROR,
                                (errcode(ERRCODE_LOCK_NOT_AVAILABLE),
                                 errmsg("could not obtain lock on row in relation \"%s\"",
                                        RelationGetRelationName(relation))));
                }
                else
                    XactLockTableWait(xwait);

                /* if there are updates, follow the update chain */
                if (follow_updates &&
                    !HEAP_XMAX_IS_LOCKED_ONLY(infomask))
                {
                    HTSU_Result     res;

                    res = heap_lock_updated_tuple(relation, tuple, &t_ctid,
                                                  GetCurrentTransactionId(),
                                                  mode);
                    if (res != HeapTupleMayBeUpdated)
                    {
                        result = res;
                        /* recovery code expects to have buffer lock held */
                        LockBuffer(*buffer, BUFFER_LOCK_EXCLUSIVE);
                        goto failed;
                    }
                }

                LockBuffer(*buffer, BUFFER_LOCK_EXCLUSIVE);

                /*
                 * xwait is done, but if xwait had just locked the tuple then
                 * some other xact could update this tuple before we get to
                 * this point.  Check for xmax change, and start over if so.
                 */
                if ((tuple->t_data->t_infomask & HEAP_XMAX_IS_MULTI) ||
                    !TransactionIdEquals(HeapTupleHeaderGetRawXmax(tuple->t_data),
                                         xwait))
                    goto l3;

                /*
                 * Otherwise check if it committed or aborted.  Note we cannot
                 * be here if the tuple was only locked by somebody who didn't
                 * conflict with us; that should have been handled above.  So
                 * that transaction must necessarily be gone by now.
                 */
                UpdateXmaxHintBits(tuple->t_data, *buffer, xwait);
            }
        }

        /* By here, we're certain that we hold buffer exclusive lock again */

        /*
         * We may lock if previous xmax aborted, or if it committed but only
         * locked the tuple without updating it; or if we didn't have to wait
         * at all for whatever reason.
         */
        if (!require_sleep ||
            (tuple->t_data->t_infomask & HEAP_XMAX_INVALID) ||
            HEAP_XMAX_IS_LOCKED_ONLY(tuple->t_data->t_infomask) ||
            HeapTupleHeaderIsOnlyLocked(tuple->t_data))
            result = HeapTupleMayBeUpdated;
        else
            result = HeapTupleUpdated;
    }

failed:
    if (result != HeapTupleMayBeUpdated)
    {
        Assert(result == HeapTupleSelfUpdated || result == HeapTupleUpdated);
        Assert(!(tuple->t_data->t_infomask & HEAP_XMAX_INVALID));
        hufd->ctid = tuple->t_data->t_ctid;
        hufd->xmax = HeapTupleHeaderGetUpdateXid(tuple->t_data);
        if (result == HeapTupleSelfUpdated)
            hufd->cmax = HeapTupleHeaderGetCmax(tuple->t_data);
        else
            hufd->cmax = 0;     /* for lack of an InvalidCommandId value */
        LockBuffer(*buffer, BUFFER_LOCK_UNLOCK);
        if (have_tuple_lock)
            UnlockTupleTuplock(relation, tid, mode);
        return result;
    }

    xmax = HeapTupleHeaderGetRawXmax(tuple->t_data);
    old_infomask = tuple->t_data->t_infomask;

    /*
     * We might already hold the desired lock (or stronger), possibly under a
     * different subtransaction of the current top transaction.  If so, there
     * is no need to change state or issue a WAL record.  We already handled
     * the case where this is true for xmax being a MultiXactId, so now check
     * for cases where it is a plain TransactionId.
     *
     * Note in particular that this covers the case where we already hold
     * exclusive lock on the tuple and the caller only wants key share or share
     * lock. It would certainly not do to give up the exclusive lock.
     */
    if (!(old_infomask & (HEAP_XMAX_INVALID |
                          HEAP_XMAX_COMMITTED |
                          HEAP_XMAX_IS_MULTI)) &&
        (mode == LockTupleKeyShare ?
         (HEAP_XMAX_IS_KEYSHR_LOCKED(old_infomask) ||
          HEAP_XMAX_IS_SHR_LOCKED(old_infomask) ||
          HEAP_XMAX_IS_EXCL_LOCKED(old_infomask)) :
         mode == LockTupleShare ?
         (HEAP_XMAX_IS_SHR_LOCKED(old_infomask) ||
          HEAP_XMAX_IS_EXCL_LOCKED(old_infomask)) :
         (HEAP_XMAX_IS_EXCL_LOCKED(old_infomask))) &&
        TransactionIdIsCurrentTransactionId(xmax))
    {
        LockBuffer(*buffer, BUFFER_LOCK_UNLOCK);
        /* Probably can't hold tuple lock here, but may as well check */
        if (have_tuple_lock)
            UnlockTupleTuplock(relation, tid, mode);
        return HeapTupleMayBeUpdated;
    }

    /*
     * If this is the first possibly-multixact-able operation in the
     * current transaction, set my per-backend OldestMemberMXactId setting.
     * We can be certain that the transaction will never become a member of
     * any older MultiXactIds than that.  (We have to do this even if we
     * end up just using our own TransactionId below, since some other
     * backend could incorporate our XID into a MultiXact immediately
     * afterwards.)
     */
    MultiXactIdSetOldestMember();

    /*
     * Compute the new xmax and infomask to store into the tuple.  Note we do
     * not modify the tuple just yet, because that would leave it in the wrong
     * state if multixact.c elogs.
     */
    compute_new_xmax_infomask(xmax, old_infomask, tuple->t_data->t_infomask2,
                              GetCurrentTransactionId(), mode, false,
                              &xid, &new_infomask, &new_infomask2);

    START_CRIT_SECTION();

    /*
     * Store transaction information of xact locking the tuple.
     *
     * Note: Cmax is meaningless in this context, so don't set it; this avoids
     * possibly generating a useless combo CID.  Moreover, if we're locking a
     * previously updated tuple, it's important to preserve the Cmax.
     *
     * Also reset the HOT UPDATE bit, but only if there's no update; otherwise
     * we would break the HOT chain.
     */
    tuple->t_data->t_infomask &= ~HEAP_XMAX_BITS;
    tuple->t_data->t_infomask2 &= ~HEAP_KEYS_UPDATED;
    tuple->t_data->t_infomask |= new_infomask;
    tuple->t_data->t_infomask2 |= new_infomask2;
    if (HEAP_XMAX_IS_LOCKED_ONLY(new_infomask))
        HeapTupleHeaderClearHotUpdated(tuple->t_data);
    HeapTupleHeaderSetXmax(tuple->t_data, xid);

    /*
     * Make sure there is no forward chain link in t_ctid.  Note that in the
     * cases where the tuple has been updated, we must not overwrite t_ctid,
     * because it was set by the updater.  Moreover, if the tuple has been
     * updated, we need to follow the update chain to lock the new versions
     * of the tuple as well.
     */
    if (HEAP_XMAX_IS_LOCKED_ONLY(new_infomask))
        tuple->t_data->t_ctid = *tid;

    MarkBufferDirty(*buffer);

    /*
     * XLOG stuff.  You might think that we don't need an XLOG record because
     * there is no state change worth restoring after a crash.  You would be
     * wrong however: we have just written either a TransactionId or a
     * MultiXactId that may never have been seen on disk before, and we need
     * to make sure that there are XLOG entries covering those ID numbers.
     * Else the same IDs might be re-used after a crash, which would be
     * disastrous if this page made it to disk before the crash.  Essentially
     * we have to enforce the WAL log-before-data rule even in this case.
     * (Also, in a PITR log-shipping or 2PC environment, we have to have XLOG
     * entries for everything anyway.)
     */
    if (RelationNeedsWAL(relation))
    {
        xl_heap_lock xlrec;
        XLogRecPtr  recptr;
        XLogRecData rdata[2];

        xlrec.target.node = relation->rd_node;
        xlrec.target.tid = tuple->t_self;
        xlrec.locking_xid = xid;
        xlrec.infobits_set = compute_infobits(new_infomask,
                                              tuple->t_data->t_infomask2);
        rdata[0].data = (char *) &xlrec;
        rdata[0].len = SizeOfHeapLock;
        rdata[0].buffer = InvalidBuffer;
        rdata[0].next = &(rdata[1]);

        rdata[1].data = NULL;
        rdata[1].len = 0;
        rdata[1].buffer = *buffer;
        rdata[1].buffer_std = true;
        rdata[1].next = NULL;

        recptr = XLogInsert(RM_HEAP_ID, XLOG_HEAP_LOCK, rdata);

        PageSetLSN(page, recptr);
    }

    END_CRIT_SECTION();

    LockBuffer(*buffer, BUFFER_LOCK_UNLOCK);

    /*
     * Don't update the visibility map here. Locking a tuple doesn't change
     * visibility info.
     */

    /*
     * Now that we have successfully marked the tuple as locked, we can
     * release the lmgr tuple lock, if we had it.
     */
    if (have_tuple_lock)
        UnlockTupleTuplock(relation, tid, mode);

    return HeapTupleMayBeUpdated;
}

void heap_markpos ( HeapScanDesc  scan  ) 
void heap_multi_insert ( Relation  relation,
HeapTuple tuples,
int  ntuples,
CommandId  cid,
int  options,
BulkInsertState  bistate 
)

Definition at line 2241 of file heapam.c.

References xl_heap_multi_insert::all_visible_cleared, Assert, xl_heap_multi_insert::blkno, XLogRecData::buffer, XLogRecData::buffer_std, BufferGetBlockNumber(), BufferGetPage, CacheInvalidateHeapTuple(), CheckForSerializableConflictIn(), XLogRecData::data, xl_multi_insert_tuple::datalen, END_CRIT_SECTION, FirstOffsetNumber, GetCurrentTransactionId(), HEAP_DEFAULT_FILLFACTOR, heap_prepare_insert(), i, InvalidBuffer, IsSystemRelation(), ItemPointerGetOffsetNumber, XLogRecData::len, MarkBufferDirty(), MAXALIGN, XLogRecData::next, xl_heap_multi_insert::node, xl_heap_multi_insert::ntuples, offsetof, xl_heap_multi_insert::offsets, PageClearAllVisible, PageGetHeapFreeSpace(), PageGetMaxOffsetNumber, PageIsAllVisible, PageSetLSN, palloc(), pgstat_count_heap_insert(), RelationData::rd_node, RelationGetBufferForTuple(), RelationGetTargetPageFreeSpace, RelationNeedsWAL, RelationPutHeapTuple(), ReleaseBuffer(), SHORTALIGN, START_CRIT_SECTION, HeapTupleData::t_data, HeapTupleHeaderData::t_hoff, xl_multi_insert_tuple::t_hoff, HeapTupleHeaderData::t_infomask, xl_multi_insert_tuple::t_infomask, HeapTupleHeaderData::t_infomask2, xl_multi_insert_tuple::t_infomask2, HeapTupleData::t_len, HeapTupleData::t_self, UnlockReleaseBuffer(), visibilitymap_clear(), and XLogInsert().

Referenced by CopyFromInsertBatch().

{
    TransactionId xid = GetCurrentTransactionId();
    HeapTuple  *heaptuples;
    int         i;
    int         ndone;
    char       *scratch = NULL;
    Page        page;
    bool        needwal;
    Size        saveFreeSpace;

    needwal = !(options & HEAP_INSERT_SKIP_WAL) && RelationNeedsWAL(relation);
    saveFreeSpace = RelationGetTargetPageFreeSpace(relation,
                                                   HEAP_DEFAULT_FILLFACTOR);

    /* Toast and set header data in all the tuples */
    heaptuples = palloc(ntuples * sizeof(HeapTuple));
    for (i = 0; i < ntuples; i++)
        heaptuples[i] = heap_prepare_insert(relation, tuples[i],
                                            xid, cid, options);

    /*
     * Allocate some memory to use for constructing the WAL record. Using
     * palloc() within a critical section is not safe, so we allocate this
     * beforehand.
     */
    if (needwal)
        scratch = palloc(BLCKSZ);

    /*
     * We're about to do the actual inserts -- but check for conflict first,
     * to avoid possibly having to roll back work we've just done.
     *
     * For a heap insert, we only need to check for table-level SSI locks. Our
     * new tuple can't possibly conflict with existing tuple locks, and heap
     * page locks are only consolidated versions of tuple locks; they do not
     * lock "gaps" as index page locks do.  So we don't need to identify a
     * buffer before making the call.
     */
    CheckForSerializableConflictIn(relation, NULL, InvalidBuffer);

    ndone = 0;
    while (ndone < ntuples)
    {
        Buffer      buffer;
        Buffer      vmbuffer = InvalidBuffer;
        bool        all_visible_cleared = false;
        int         nthispage;

        /*
         * Find buffer where at least the next tuple will fit.  If the page is
         * all-visible, this will also pin the requisite visibility map page.
         */
        buffer = RelationGetBufferForTuple(relation, heaptuples[ndone]->t_len,
                                           InvalidBuffer, options, bistate,
                                           &vmbuffer, NULL);
        page = BufferGetPage(buffer);

        /* NO EREPORT(ERROR) from here till changes are logged */
        START_CRIT_SECTION();

        /*
         * RelationGetBufferForTuple has ensured that the first tuple fits.
         * Put that on the page, and then as many other tuples as fit.
         */
        RelationPutHeapTuple(relation, buffer, heaptuples[ndone]);
        for (nthispage = 1; ndone + nthispage < ntuples; nthispage++)
        {
            HeapTuple   heaptup = heaptuples[ndone + nthispage];

            if (PageGetHeapFreeSpace(page) < MAXALIGN(heaptup->t_len) + saveFreeSpace)
                break;

            RelationPutHeapTuple(relation, buffer, heaptup);
        }

        if (PageIsAllVisible(page))
        {
            all_visible_cleared = true;
            PageClearAllVisible(page);
            visibilitymap_clear(relation,
                                BufferGetBlockNumber(buffer),
                                vmbuffer);
        }

        /*
         * XXX Should we set PageSetPrunable on this page ? See heap_insert()
         */

        MarkBufferDirty(buffer);

        /* XLOG stuff */
        if (needwal)
        {
            XLogRecPtr  recptr;
            xl_heap_multi_insert *xlrec;
            XLogRecData rdata[2];
            uint8       info = XLOG_HEAP2_MULTI_INSERT;
            char       *tupledata;
            int         totaldatalen;
            char       *scratchptr = scratch;
            bool        init;

            /*
             * If the page was previously empty, we can reinit the page
             * instead of restoring the whole thing.
             */
            init = (ItemPointerGetOffsetNumber(&(heaptuples[ndone]->t_self)) == FirstOffsetNumber &&
                    PageGetMaxOffsetNumber(page) == FirstOffsetNumber + nthispage - 1);

            /* allocate xl_heap_multi_insert struct from the scratch area */
            xlrec = (xl_heap_multi_insert *) scratchptr;
            scratchptr += SizeOfHeapMultiInsert;

            /*
             * Allocate offsets array. Unless we're reinitializing the page,
             * in that case the tuples are stored in order starting at
             * FirstOffsetNumber and we don't need to store the offsets
             * explicitly.
             */
            if (!init)
                scratchptr += nthispage * sizeof(OffsetNumber);

            /* the rest of the scratch space is used for tuple data */
            tupledata = scratchptr;

            xlrec->all_visible_cleared = all_visible_cleared;
            xlrec->node = relation->rd_node;
            xlrec->blkno = BufferGetBlockNumber(buffer);
            xlrec->ntuples = nthispage;

            /*
             * Write out an xl_multi_insert_tuple and the tuple data itself
             * for each tuple.
             */
            for (i = 0; i < nthispage; i++)
            {
                HeapTuple   heaptup = heaptuples[ndone + i];
                xl_multi_insert_tuple *tuphdr;
                int         datalen;

                if (!init)
                    xlrec->offsets[i] = ItemPointerGetOffsetNumber(&heaptup->t_self);
                /* xl_multi_insert_tuple needs two-byte alignment. */
                tuphdr = (xl_multi_insert_tuple *) SHORTALIGN(scratchptr);
                scratchptr = ((char *) tuphdr) + SizeOfMultiInsertTuple;

                tuphdr->t_infomask2 = heaptup->t_data->t_infomask2;
                tuphdr->t_infomask = heaptup->t_data->t_infomask;
                tuphdr->t_hoff = heaptup->t_data->t_hoff;

                /* write bitmap [+ padding] [+ oid] + data */
                datalen = heaptup->t_len - offsetof(HeapTupleHeaderData, t_bits);
                memcpy(scratchptr,
                       (char *) heaptup->t_data + offsetof(HeapTupleHeaderData, t_bits),
                       datalen);
                tuphdr->datalen = datalen;
                scratchptr += datalen;
            }
            totaldatalen = scratchptr - tupledata;
            Assert((scratchptr - scratch) < BLCKSZ);

            rdata[0].data = (char *) xlrec;
            rdata[0].len = tupledata - scratch;
            rdata[0].buffer = InvalidBuffer;
            rdata[0].next = &rdata[1];

            rdata[1].data = tupledata;
            rdata[1].len = totaldatalen;
            rdata[1].buffer = buffer;
            rdata[1].buffer_std = true;
            rdata[1].next = NULL;

            /*
             * If we're going to reinitialize the whole page using the WAL
             * record, hide buffer reference from XLogInsert.
             */
            if (init)
            {
                rdata[1].buffer = InvalidBuffer;
                info |= XLOG_HEAP_INIT_PAGE;
            }

            recptr = XLogInsert(RM_HEAP2_ID, info, rdata);

            PageSetLSN(page, recptr);
        }

        END_CRIT_SECTION();

        UnlockReleaseBuffer(buffer);
        if (vmbuffer != InvalidBuffer)
            ReleaseBuffer(vmbuffer);

        ndone += nthispage;
    }

    /*
     * If tuples are cachable, mark them for invalidation from the caches in
     * case we abort.  Note it is OK to do this after releasing the buffer,
     * because the heaptuples data structure is all in local memory, not in
     * the shared buffer.
     */
    if (IsSystemRelation(relation))
    {
        for (i = 0; i < ntuples; i++)
            CacheInvalidateHeapTuple(relation, heaptuples[i], NULL);
    }

    /*
     * Copy t_self fields back to the caller's original tuples. This does
     * nothing for untoasted tuples (tuples[i] == heaptuples[i)], but it's
     * probably faster to always copy than check.
     */
    for (i = 0; i < ntuples; i++)
        tuples[i]->t_self = heaptuples[i]->t_self;

    pgstat_count_heap_insert(relation, ntuples);
}

Relation heap_open ( Oid  relationId,
LOCKMODE  lockmode 
)

Definition at line 1183 of file heapam.c.

References ereport, errcode(), errmsg(), ERROR, RelationData::rd_rel, relation_open(), RelationGetRelationName, RELKIND_COMPOSITE_TYPE, and RELKIND_INDEX.

Referenced by acquire_inherited_sample_rows(), AcquireRewriteLocks(), AddEnumLabel(), AddNewAttributeTuples(), AddRoleMems(), AfterTriggerSetState(), AggregateCreate(), AlterConstraintNamespaces(), AlterDatabase(), AlterDatabaseOwner(), AlterDomainAddConstraint(), AlterDomainDefault(), AlterDomainDropConstraint(), AlterDomainNotNull(), AlterDomainValidateConstraint(), AlterEventTrigger(), AlterEventTriggerOwner(), AlterEventTriggerOwner_oid(), AlterExtensionNamespace(), AlterForeignDataWrapper(), AlterForeignDataWrapperOwner(), AlterForeignDataWrapperOwner_oid(), AlterForeignServer(), AlterForeignServerOwner(), AlterForeignServerOwner_oid(), AlterFunction(), AlterObjectNamespace_oid(), AlterRole(), AlterSchemaOwner(), AlterSchemaOwner_oid(), AlterSeqNamespaces(), AlterSetting(), AlterTableCreateToastTable(), AlterTableNamespaceInternal(), AlterTableSpaceOptions(), AlterTSConfiguration(), AlterTSDictionary(), AlterTypeNamespaceInternal(), AlterTypeOwner(), AlterTypeOwnerInternal(), AlterUserMapping(), AppendAttributeTuples(), ApplyExtensionUpdates(), AssignTypeArrayOid(), ATAddCheckConstraint(), ATExecAddColumn(), ATExecAddInherit(), ATExecAddOf(), ATExecAlterColumnGenericOptions(), ATExecAlterColumnType(), ATExecChangeOwner(), ATExecDropColumn(), ATExecDropConstraint(), ATExecDropInherit(), ATExecDropNotNull(), ATExecDropOf(), ATExecGenericOptions(), ATExecSetNotNull(), ATExecSetOptions(), ATExecSetRelOptions(), ATExecSetStatistics(), ATExecSetStorage(), ATExecSetTableSpace(), ATExecValidateConstraint(), ATRewriteTable(), ATRewriteTables(), AttrDefaultFetch(), boot_openrel(), build_indices(), build_physical_tlist(), CatalogCacheInitializeCache(), change_owner_fix_column_acls(), change_owner_recurse_to_sequences(), changeDependencyFor(), changeDependencyOnOwner(), check_db_file_conflict(), check_functional_grouping(), check_selective_binary_conversion(), CheckConstraintFetch(), checkSharedDependencies(), ChooseConstraintName(), cluster(), CollationCreate(), ConstraintNameIsUsed(), ConversionCreate(), copy_heap_data(), copyTemplateDependencies(), create_proc_lang(), create_toast_table(), CreateCast(), CreateComments(), CreateConstraintEntry(), createdb(), CreateForeignDataWrapper(), CreateForeignServer(), CreateForeignTable(), CreateOpFamily(), CreateRole(), CreateSharedComments(), CreateTableSpace(), CreateTrigger(), CreateUserMapping(), currtid_byreloid(), database_to_xmlschema_internal(), DefineOpClass(), DefineQueryRewrite(), DefineSequence(), DefineTSConfiguration(), DefineTSDictionary(), DefineTSParser(), DefineTSTemplate(), DeleteAttributeTuples(), DeleteComments(), deleteDependencyRecordsFor(), deleteDependencyRecordsForClass(), deleteOneObject(), DeleteRelationTuple(), DeleteSecurityLabel(), DeleteSharedComments(), deleteSharedDependencyRecordsFor(), DeleteSharedSecurityLabel(), DeleteSystemAttributeTuples(), deleteWhatDependsOn(), DelRoleMems(), deparseSelectSql(), do_autovacuum(), drop_parent_dependency(), DropCastById(), dropDatabaseDependencies(), dropdb(), DropProceduralLanguageById(), DropRole(), DropSetting(), DropTableSpace(), EnableDisableRule(), EnableDisableTrigger(), enum_endpoint(), enum_range_internal(), EnumValuesCreate(), EnumValuesDelete(), EventTriggerSQLDropAddObject(), exec_object_restorecon(), ExecAlterExtensionStmt(), ExecAlterObjectSchemaStmt(), ExecAlterOwnerStmt(), ExecGetTriggerResultRel(), ExecGrant_Database(), ExecGrant_Fdw(), ExecGrant_ForeignServer(), ExecGrant_Function(), ExecGrant_Language(), ExecGrant_Largeobject(), ExecGrant_Namespace(), ExecGrant_Relation(), ExecGrant_Tablespace(), ExecGrant_Type(), ExecOpenScanRelation(), ExecRefreshMatView(), ExecRenameStmt(), ExecuteTruncate(), expand_inherited_rtentry(), expand_targetlist(), extension_config_remove(), find_composite_type_dependencies(), find_inheritance_children(), find_language_template(), find_typed_table_dependencies(), finish_heap_swap(), fireRIRrules(), get_actual_variable_range(), get_constraint_index(), get_database_list(), get_database_oid(), get_db_info(), get_domain_constraint_oid(), get_extension_name(), get_extension_oid(), get_extension_schema(), get_file_fdw_attribute_options(), get_index_constraint(), get_object_address_relobject(), get_pkey_attnames(), get_rel_oids(), get_relation_constraint_oid(), get_relation_constraints(), get_relation_data_width(), get_relation_info(), get_rels_with_domain(), get_rewrite_oid_without_relid(), get_tables_to_cluster(), get_tablespace_name(), get_tablespace_oid(), get_trigger_oid(), GetComment(), getConstraintTypeDescription(), GetDatabaseTuple(), GetDatabaseTupleByOid(), GetDefaultOpClass(), GetDomainConstraints(), getExtensionOfObject(), getObjectDescription(), getObjectIdentity(), getOwnedSequences(), getRelationsInNamespace(), GetSecurityLabel(), GetSharedSecurityLabel(), gettype(), GrantRole(), heap_create_with_catalog(), heap_drop_with_catalog(), heap_sync(), heap_truncate(), heap_truncate_find_FKs(), heap_truncate_one_rel(), index_build(), index_constraint_create(), index_create(), index_drop(), index_set_state_flags(), index_update_stats(), InitPlan(), insert_event_trigger_tuple(), InsertExtensionTuple(), InsertRule(), intorel_startup(), isQueryUsingTempRelation_walker(), LargeObjectCreate(), LargeObjectDrop(), LargeObjectExists(), load_enum_cache_data(), lookup_ts_config_cache(), LookupOpclassInfo(), make_new_heap(), make_viewdef(), makeArrayTypeName(), mark_index_clustered(), MergeAttributesIntoExisting(), MergeConstraintsIntoExisting(), MergeWithExistingConstraint(), movedb(), myLargeObjectExists(), NamespaceCreate(), objectsInSchemaToOids(), open_lo_relation(), OperatorCreate(), OperatorShellMake(), OperatorUpd(), performDeletion(), performMultipleDeletions(), pg_extension_config_dump(), pg_extension_ownercheck(), pg_get_serial_sequence(), pg_get_triggerdef_worker(), pg_identify_object(), pg_largeobject_aclmask_snapshot(), pg_largeobject_ownercheck(), pgstat_collect_oids(), postgresPlanForeignModify(), ProcedureCreate(), process_settings(), RangeCreate(), RangeDelete(), recordMultipleDependencies(), recordSharedDependencyOn(), regclassin(), regoperin(), regprocin(), regtypein(), reindex_index(), reindex_relation(), ReindexDatabase(), RelationBuildRuleLock(), RelationBuildTriggers(), RelationBuildTupleDesc(), RelationGetExclusionInfo(), RelationGetIndexList(), RelationRemoveInheritance(), RelationSetNewRelfilenode(), remove_dbtablespaces(), RemoveAmOpEntryById(), RemoveAmProcEntryById(), RemoveAttrDefault(), RemoveAttrDefaultById(), RemoveAttributeById(), RemoveCollationById(), RemoveConstraintById(), RemoveConversionById(), RemoveDefaultACLById(), RemoveEventTriggerById(), RemoveExtensionById(), RemoveForeignDataWrapperById(), RemoveForeignServerById(), RemoveFunctionById(), RemoveOpClassById(), RemoveOperatorById(), RemoveOpFamilyById(), RemoveRewriteRuleById(), RemoveRoleFromObjectACL(), RemoveSchemaById(), RemoveStatistics(), RemoveTriggerById(), RemoveTSConfigurationById(), RemoveTSDictionaryById(), RemoveTSParserById(), RemoveTSTemplateById(), RemoveTypeById(), RemoveUserMappingById(), renameatt_internal(), RenameConstraint(), RenameConstraintById(), RenameDatabase(), RenameRelationInternal(), RenameRewriteRule(), RenameRole(), RenameSchema(), RenameTableSpace(), renametrig(), RenameType(), RenameTypeInternal(), RewriteQuery(), rewriteTargetView(), RI_FKey_cascade_del(), RI_FKey_cascade_upd(), RI_FKey_check(), RI_FKey_setdefault_del(), RI_FKey_setdefault_upd(), RI_FKey_setnull_del(), RI_FKey_setnull_upd(), ri_restrict_del(), ri_restrict_upd(), ScanPgRelation(), schema_to_xmlschema_internal(), SearchCatCache(), SearchCatCacheList(), sepgsql_attribute_post_create(), sepgsql_database_post_create(), sepgsql_index_modify(), sepgsql_proc_post_create(), sepgsql_proc_setattr(), sepgsql_relation_post_create(), sepgsql_relation_setattr(), sepgsql_schema_post_create(), sequenceIsOwned(), SetDefaultACL(), SetFunctionArgType(), SetFunctionReturnType(), SetRelationHasSubclass(), SetRelationNumChecks(), SetRelationRuleStatus(), SetSecurityLabel(), SetSharedSecurityLabel(), shdepDropOwned(), shdepReassignOwned(), StoreAttrDefault(), StoreCatalogInheritance(), storeOperators(), storeProcedures(), swap_relation_files(), table_to_xml_and_xmlschema(), table_to_xmlschema(), ThereIsAtLeastOneRole(), toast_delete_datum(), toast_fetch_datum(), toast_fetch_datum_slice(), toast_save_datum(), toastid_valueid_exists(), transientrel_startup(), TypeCreate(), typeInheritsFrom(), TypeShellMake(), update_attstats(), updateAclDependencies(), UpdateIndexRelation(), vac_truncate_clog(), vac_update_datfrozenxid(), vac_update_relstats(), and validate_index().

{
    Relation    r;

    r = relation_open(relationId, lockmode);

    if (r->rd_rel->relkind == RELKIND_INDEX)
        ereport(ERROR,
                (errcode(ERRCODE_WRONG_OBJECT_TYPE),
                 errmsg("\"%s\" is an index",
                        RelationGetRelationName(r))));
    else if (r->rd_rel->relkind == RELKIND_COMPOSITE_TYPE)
        ereport(ERROR,
                (errcode(ERRCODE_WRONG_OBJECT_TYPE),
                 errmsg("\"%s\" is a composite type",
                        RelationGetRelationName(r))));

    return r;
}

Relation heap_openrv ( const RangeVar relation,
LOCKMODE  lockmode 
)
Relation heap_openrv_extended ( const RangeVar relation,
LOCKMODE  lockmode,
bool  missing_ok 
)

Definition at line 1240 of file heapam.c.

References ereport, errcode(), errmsg(), ERROR, RelationData::rd_rel, relation_openrv_extended(), RelationGetRelationName, RELKIND_COMPOSITE_TYPE, and RELKIND_INDEX.

Referenced by parserOpenTable().

{
    Relation    r;

    r = relation_openrv_extended(relation, lockmode, missing_ok);

    if (r)
    {
        if (r->rd_rel->relkind == RELKIND_INDEX)
            ereport(ERROR,
                    (errcode(ERRCODE_WRONG_OBJECT_TYPE),
                     errmsg("\"%s\" is an index",
                            RelationGetRelationName(r))));
        else if (r->rd_rel->relkind == RELKIND_COMPOSITE_TYPE)
            ereport(ERROR,
                    (errcode(ERRCODE_WRONG_OBJECT_TYPE),
                     errmsg("\"%s\" is a composite type",
                            RelationGetRelationName(r))));
    }

    return r;
}

int heap_page_prune ( Relation  relation,
Buffer  buffer,
TransactionId  OldestXmin,
bool  report_stats,
TransactionId latestRemovedXid 
)

Definition at line 155 of file pruneheap.c.

References BufferGetPage, END_CRIT_SECTION, FirstOffsetNumber, heap_page_prune_execute(), heap_prune_chain(), ItemIdIsDead, ItemIdIsUsed, PruneState::latestRemovedXid, log_heap_clean(), MarkBufferDirty(), MarkBufferDirtyHint(), PruneState::marked, PruneState::ndead, PruneState::new_prune_xid, PruneState::nowdead, PruneState::nowunused, PruneState::nredirected, PruneState::nunused, OffsetNumberNext, PageClearFull, PageGetItemId, PageGetMaxOffsetNumber, PageIsFull, PageSetLSN, pgstat_update_heap_dead_tuples(), PruneState::redirected, RelationNeedsWAL, and START_CRIT_SECTION.

Referenced by heap_page_prune_opt(), and lazy_scan_heap().

{
    int         ndeleted = 0;
    Page        page = BufferGetPage(buffer);
    OffsetNumber offnum,
                maxoff;
    PruneState  prstate;

    /*
     * Our strategy is to scan the page and make lists of items to change,
     * then apply the changes within a critical section.  This keeps as much
     * logic as possible out of the critical section, and also ensures that
     * WAL replay will work the same as the normal case.
     *
     * First, initialize the new pd_prune_xid value to zero (indicating no
     * prunable tuples).  If we find any tuples which may soon become
     * prunable, we will save the lowest relevant XID in new_prune_xid. Also
     * initialize the rest of our working state.
     */
    prstate.new_prune_xid = InvalidTransactionId;
    prstate.latestRemovedXid = *latestRemovedXid;
    prstate.nredirected = prstate.ndead = prstate.nunused = 0;
    memset(prstate.marked, 0, sizeof(prstate.marked));

    /* Scan the page */
    maxoff = PageGetMaxOffsetNumber(page);
    for (offnum = FirstOffsetNumber;
         offnum <= maxoff;
         offnum = OffsetNumberNext(offnum))
    {
        ItemId      itemid;

        /* Ignore items already processed as part of an earlier chain */
        if (prstate.marked[offnum])
            continue;

        /* Nothing to do if slot is empty or already dead */
        itemid = PageGetItemId(page, offnum);
        if (!ItemIdIsUsed(itemid) || ItemIdIsDead(itemid))
            continue;

        /* Process this item or chain of items */
        ndeleted += heap_prune_chain(relation, buffer, offnum,
                                     OldestXmin,
                                     &prstate);
    }

    /* Any error while applying the changes is critical */
    START_CRIT_SECTION();

    /* Have we found any prunable items? */
    if (prstate.nredirected > 0 || prstate.ndead > 0 || prstate.nunused > 0)
    {
        /*
         * Apply the planned item changes, then repair page fragmentation, and
         * update the page's hint bit about whether it has free line pointers.
         */
        heap_page_prune_execute(buffer,
                                prstate.redirected, prstate.nredirected,
                                prstate.nowdead, prstate.ndead,
                                prstate.nowunused, prstate.nunused);

        /*
         * Update the page's pd_prune_xid field to either zero, or the lowest
         * XID of any soon-prunable tuple.
         */
        ((PageHeader) page)->pd_prune_xid = prstate.new_prune_xid;

        /*
         * Also clear the "page is full" flag, since there's no point in
         * repeating the prune/defrag process until something else happens to
         * the page.
         */
        PageClearFull(page);

        MarkBufferDirty(buffer);

        /*
         * Emit a WAL HEAP_CLEAN record showing what we did
         */
        if (RelationNeedsWAL(relation))
        {
            XLogRecPtr  recptr;

            recptr = log_heap_clean(relation, buffer,
                                    prstate.redirected, prstate.nredirected,
                                    prstate.nowdead, prstate.ndead,
                                    prstate.nowunused, prstate.nunused,
                                    prstate.latestRemovedXid);

            PageSetLSN(BufferGetPage(buffer), recptr);
        }
    }
    else
    {
        /*
         * If we didn't prune anything, but have found a new value for the
         * pd_prune_xid field, update it and mark the buffer dirty. This is
         * treated as a non-WAL-logged hint.
         *
         * Also clear the "page is full" flag if it is set, since there's no
         * point in repeating the prune/defrag process until something else
         * happens to the page.
         */
        if (((PageHeader) page)->pd_prune_xid != prstate.new_prune_xid ||
            PageIsFull(page))
        {
            ((PageHeader) page)->pd_prune_xid = prstate.new_prune_xid;
            PageClearFull(page);
            MarkBufferDirtyHint(buffer);
        }
    }

    END_CRIT_SECTION();

    /*
     * If requested, report the number of tuples reclaimed to pgstats. This is
     * ndeleted minus ndead, because we don't want to count a now-DEAD root
     * item as a deletion for this purpose.
     */
    if (report_stats && ndeleted > prstate.ndead)
        pgstat_update_heap_dead_tuples(relation, ndeleted - prstate.ndead);

    *latestRemovedXid = prstate.latestRemovedXid;

    /*
     * XXX Should we update the FSM information of this page ?
     *
     * There are two schools of thought here. We may not want to update FSM
     * information so that the page is not used for unrelated UPDATEs/INSERTs
     * and any free space in this page will remain available for further
     * UPDATEs in *this* page, thus improving chances for doing HOT updates.
     *
     * But for a large table and where a page does not receive further UPDATEs
     * for a long time, we might waste this space by not updating the FSM
     * information. The relation may get extended and fragmented further.
     *
     * One possibility is to leave "fillfactor" worth of space in this page
     * and update FSM with the remaining space.
     */

    return ndeleted;
}

void heap_page_prune_execute ( Buffer  buffer,
OffsetNumber redirected,
int  nredirected,
OffsetNumber nowdead,
int  ndead,
OffsetNumber nowunused,
int  nunused 
)

Definition at line 641 of file pruneheap.c.

References BufferGetPage, i, ItemIdSetDead, ItemIdSetRedirect, ItemIdSetUnused, PageGetItemId, and PageRepairFragmentation().

Referenced by heap_page_prune(), and heap_xlog_clean().

{
    Page        page = (Page) BufferGetPage(buffer);
    OffsetNumber *offnum;
    int         i;

    /* Update all redirected line pointers */
    offnum = redirected;
    for (i = 0; i < nredirected; i++)
    {
        OffsetNumber fromoff = *offnum++;
        OffsetNumber tooff = *offnum++;
        ItemId      fromlp = PageGetItemId(page, fromoff);

        ItemIdSetRedirect(fromlp, tooff);
    }

    /* Update all now-dead line pointers */
    offnum = nowdead;
    for (i = 0; i < ndead; i++)
    {
        OffsetNumber off = *offnum++;
        ItemId      lp = PageGetItemId(page, off);

        ItemIdSetDead(lp);
    }

    /* Update all now-unused line pointers */
    offnum = nowunused;
    for (i = 0; i < nunused; i++)
    {
        OffsetNumber off = *offnum++;
        ItemId      lp = PageGetItemId(page, off);

        ItemIdSetUnused(lp);
    }

    /*
     * Finally, repair any fragmentation, and update the page's hint bit about
     * whether it has free pointers.
     */
    PageRepairFragmentation(page);
}

void heap_page_prune_opt ( Relation  relation,
Buffer  buffer,
TransactionId  OldestXmin 
)

Definition at line 73 of file pruneheap.c.

References BUFFER_LOCK_UNLOCK, BufferGetPage, ConditionalLockBufferForCleanup(), HEAP_DEFAULT_FILLFACTOR, heap_page_prune(), LockBuffer(), Max, PageGetHeapFreeSpace(), PageIsFull, PageIsPrunable, RecoveryInProgress(), and RelationGetTargetPageFreeSpace.

Referenced by bitgetpage(), heapgetpage(), and index_fetch_heap().

{
    Page        page = BufferGetPage(buffer);
    Size        minfree;

    /*
     * Let's see if we really need pruning.
     *
     * Forget it if page is not hinted to contain something prunable that's
     * older than OldestXmin.
     */
    if (!PageIsPrunable(page, OldestXmin))
        return;

    /*
     * We can't write WAL in recovery mode, so there's no point trying to
     * clean the page. The master will likely issue a cleaning WAL record soon
     * anyway, so this is no particular loss.
     */
    if (RecoveryInProgress())
        return;

    /*
     * We prune when a previous UPDATE failed to find enough space on the page
     * for a new tuple version, or when free space falls below the relation's
     * fill-factor target (but not less than 10%).
     *
     * Checking free space here is questionable since we aren't holding any
     * lock on the buffer; in the worst case we could get a bogus answer. It's
     * unlikely to be *seriously* wrong, though, since reading either pd_lower
     * or pd_upper is probably atomic.  Avoiding taking a lock seems more
     * important than sometimes getting a wrong answer in what is after all
     * just a heuristic estimate.
     */
    minfree = RelationGetTargetPageFreeSpace(relation,
                                             HEAP_DEFAULT_FILLFACTOR);
    minfree = Max(minfree, BLCKSZ / 10);

    if (PageIsFull(page) || PageGetHeapFreeSpace(page) < minfree)
    {
        /* OK, try to get exclusive buffer lock */
        if (!ConditionalLockBufferForCleanup(buffer))
            return;

        /*
         * Now that we have buffer lock, get accurate information about the
         * page's free space, and recheck the heuristic about whether to
         * prune. (We needn't recheck PageIsPrunable, since no one else could
         * have pruned while we hold pin.)
         */
        if (PageIsFull(page) || PageGetHeapFreeSpace(page) < minfree)
        {
            TransactionId ignore = InvalidTransactionId;        /* return value not
                                                                 * needed */

            /* OK to prune */
            (void) heap_page_prune(relation, buffer, OldestXmin, true, &ignore);
        }

        /* And release buffer lock */
        LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
    }
}

void heap_rescan ( HeapScanDesc  scan,
ScanKey  key 
)

Definition at line 1375 of file heapam.c.

References BufferIsValid, initscan(), ReleaseBuffer(), and HeapScanDescData::rs_cbuf.

Referenced by ExecReScanBitmapHeapScan(), and ExecReScanSeqScan().

{
    /*
     * unpin scan buffers
     */
    if (BufferIsValid(scan->rs_cbuf))
        ReleaseBuffer(scan->rs_cbuf);

    /*
     * reinitialize scan descriptor
     */
    initscan(scan, key, true);
}

void heap_restrpos ( HeapScanDesc  scan  ) 

Definition at line 5505 of file heapam.c.

References BufferIsValid, heapgettup(), heapgettup_pagemode(), ItemPointerIsValid, NoMovementScanDirection, ReleaseBuffer(), HeapScanDescData::rs_cblock, HeapScanDescData::rs_cbuf, HeapScanDescData::rs_cindex, HeapScanDescData::rs_ctup, HeapScanDescData::rs_inited, HeapScanDescData::rs_mctid, HeapScanDescData::rs_mindex, HeapScanDescData::rs_pageatatime, HeapTupleData::t_data, and HeapTupleData::t_self.

Referenced by ExecSeqRestrPos().

{
    /* XXX no amrestrpos checking that ammarkpos called */

    if (!ItemPointerIsValid(&scan->rs_mctid))
    {
        scan->rs_ctup.t_data = NULL;

        /*
         * unpin scan buffers
         */
        if (BufferIsValid(scan->rs_cbuf))
            ReleaseBuffer(scan->rs_cbuf);
        scan->rs_cbuf = InvalidBuffer;
        scan->rs_cblock = InvalidBlockNumber;
        scan->rs_inited = false;
    }
    else
    {
        /*
         * If we reached end of scan, rs_inited will now be false.  We must
         * reset it to true to keep heapgettup from doing the wrong thing.
         */
        scan->rs_inited = true;
        scan->rs_ctup.t_self = scan->rs_mctid;
        if (scan->rs_pageatatime)
        {
            scan->rs_cindex = scan->rs_mindex;
            heapgettup_pagemode(scan,
                                NoMovementScanDirection,
                                0,      /* needn't recheck scan keys */
                                NULL);
        }
        else
            heapgettup(scan,
                       NoMovementScanDirection,
                       0,       /* needn't recheck scan keys */
                       NULL);
    }
}

void heap_sync ( Relation  relation  ) 

Definition at line 7148 of file heapam.c.

References AccessShareLock, FlushRelationBuffers(), heap_close, heap_open(), MAIN_FORKNUM, OidIsValid, RelationData::rd_rel, RelationData::rd_smgr, RelationNeedsWAL, and smgrimmedsync().

Referenced by ATRewriteTable(), CopyFrom(), end_heap_rewrite(), intorel_shutdown(), and transientrel_shutdown().

{
    /* non-WAL-logged tables never need fsync */
    if (!RelationNeedsWAL(rel))
        return;

    /* main heap */
    FlushRelationBuffers(rel);
    /* FlushRelationBuffers will have opened rd_smgr */
    smgrimmedsync(rel->rd_smgr, MAIN_FORKNUM);

    /* FSM is not critical, don't bother syncing it */

    /* toast heap, if any */
    if (OidIsValid(rel->rd_rel->reltoastrelid))
    {
        Relation    toastrel;

        toastrel = heap_open(rel->rd_rel->reltoastrelid, AccessShareLock);
        FlushRelationBuffers(toastrel);
        smgrimmedsync(toastrel->rd_smgr, MAIN_FORKNUM);
        heap_close(toastrel, AccessShareLock);
    }
}

bool heap_tuple_needs_freeze ( HeapTupleHeader  tuple,
TransactionId  cutoff_xid,
MultiXactId  cutoff_multi,
Buffer  buf 
)

Definition at line 5441 of file heapam.c.

References HEAP_MOVED, HEAP_XMAX_INVALID, HEAP_XMAX_IS_MULTI, HeapTupleHeaderGetRawXmax, HeapTupleHeaderGetXmin, HeapTupleHeaderGetXvac, MultiXactIdPrecedes(), HeapTupleHeaderData::t_infomask, TransactionIdIsNormal, and TransactionIdPrecedes().

Referenced by lazy_check_needs_freeze().

{
    TransactionId xid;

    xid = HeapTupleHeaderGetXmin(tuple);
    if (TransactionIdIsNormal(xid) &&
        TransactionIdPrecedes(xid, cutoff_xid))
        return true;

    if (!(tuple->t_infomask & HEAP_XMAX_INVALID))
    {
        if (!(tuple->t_infomask & HEAP_XMAX_IS_MULTI))
        {
            xid = HeapTupleHeaderGetRawXmax(tuple);
            if (TransactionIdIsNormal(xid) &&
                TransactionIdPrecedes(xid, cutoff_xid))
                return true;
        }
        else
        {
            MultiXactId multi;

            multi = HeapTupleHeaderGetRawXmax(tuple);
            if (MultiXactIdPrecedes(multi, cutoff_multi))
                return true;
        }
    }

    if (tuple->t_infomask & HEAP_MOVED)
    {
        xid = HeapTupleHeaderGetXvac(tuple);
        if (TransactionIdIsNormal(xid) &&
            TransactionIdPrecedes(xid, cutoff_xid))
            return true;
    }

    return false;
}

HTSU_Result heap_update ( Relation  relation,
ItemPointer  otid,
HeapTuple  newtup,
CommandId  cid,
Snapshot  crosscheck,
bool  wait,
HeapUpdateFailureData hufd,
LockTupleMode lockmode 
)

Definition at line 2906 of file heapam.c.

References Assert, bms_free(), BUFFER_LOCK_EXCLUSIVE, BUFFER_LOCK_UNLOCK, BufferGetBlockNumber(), BufferGetPage, BufferIsValid, CacheInvalidateHeapTuple(), CheckForSerializableConflictIn(), HeapUpdateFailureData::cmax, compute_new_xmax_infomask(), HeapUpdateFailureData::ctid, elog, END_CRIT_SECTION, ERROR, GetCurrentTransactionId(), GetMultiXactIdHintBits(), heap_freetuple(), HEAP_HASOID, HEAP_UPDATED, HEAP_XMAX_BITS, HEAP_XMAX_INVALID, HEAP_XMAX_IS_KEYSHR_LOCKED, HEAP_XMAX_IS_LOCKED_ONLY, HEAP_XMAX_IS_MULTI, HEAP_XMAX_KEYSHR_LOCK, HeapSatisfiesHOTandKeyUpdate(), HeapTupleBeingUpdated, HeapTupleClearHeapOnly, HeapTupleClearHotUpdated, HeapTupleGetOid, HeapTupleGetUpdateXid(), HeapTupleHasExternal, HeapTupleHeaderAdjustCmax(), HeapTupleHeaderGetCmax(), HeapTupleHeaderGetRawXmax, HeapTupleHeaderGetUpdateXid, HeapTupleHeaderSetCmax, HeapTupleHeaderSetCmin, HeapTupleHeaderSetXmax, HeapTupleHeaderSetXmin, HeapTupleInvisible, HeapTupleMayBeUpdated, HeapTupleSatisfiesUpdate(), HeapTupleSatisfiesVisibility, HeapTupleSelfUpdated, HeapTupleSetHeapOnly, HeapTupleSetHotUpdated, HeapTupleSetOid, HeapTupleUpdated, InvalidBuffer, InvalidSnapshot, ItemIdGetLength, ItemIdIsNormal, ItemPointerGetBlockNumber, ItemPointerGetOffsetNumber, ItemPointerIsValid, LockBuffer(), LockTupleTuplock, log_heap_update(), MarkBufferDirty(), MAXALIGN, MultiXactIdSetOldestMember(), MultiXactIdWait(), PageClearAllVisible, PageGetHeapFreeSpace(), PageGetItem, PageGetItemId, PageIsAllVisible, PageSetFull, PageSetLSN, PageSetPrunable, pgstat_count_heap_update(), RelationData::rd_rel, ReadBuffer(), RelationGetBufferForTuple(), RelationGetIndexAttrBitmap(), RelationGetRelid, RelationNeedsWAL, RelationPutHeapTuple(), ReleaseBuffer(), RELKIND_MATVIEW, RELKIND_RELATION, START_CRIT_SECTION, HeapTupleHeaderData::t_ctid, HeapTupleData::t_data, HeapTupleHeaderData::t_infomask, HeapTupleHeaderData::t_infomask2, HeapTupleData::t_len, HeapTupleData::t_self, HeapTupleData::t_tableOid, toast_insert_or_update(), TransactionIdDidAbort(), TransactionIdEquals, TransactionIdIsValid, UnlockReleaseBuffer(), UnlockTupleTuplock, UpdateXmaxHintBits(), visibilitymap_clear(), visibilitymap_pin(), XactLockTableWait(), and HeapUpdateFailureData::xmax.

Referenced by ExecUpdate(), and simple_heap_update().

{
    HTSU_Result result;
    TransactionId xid = GetCurrentTransactionId();
    Bitmapset  *hot_attrs;
    Bitmapset  *key_attrs;
    ItemId      lp;
    HeapTupleData oldtup;
    HeapTuple   heaptup;
    Page        page;
    BlockNumber block;
    MultiXactStatus mxact_status;
    Buffer      buffer,
                newbuf,
                vmbuffer = InvalidBuffer,
                vmbuffer_new = InvalidBuffer;
    bool        need_toast,
                already_marked;
    Size        newtupsize,
                pagefree;
    bool        have_tuple_lock = false;
    bool        iscombo;
    bool        satisfies_hot;
    bool        satisfies_key;
    bool        use_hot_update = false;
    bool        key_intact;
    bool        all_visible_cleared = false;
    bool        all_visible_cleared_new = false;
    bool        checked_lockers;
    bool        locker_remains;
    TransactionId xmax_new_tuple,
                  xmax_old_tuple;
    uint16      infomask_old_tuple,
                infomask2_old_tuple,
                infomask_new_tuple,
                infomask2_new_tuple;

    Assert(ItemPointerIsValid(otid));

    /*
     * Fetch the list of attributes to be checked for HOT update.  This is
     * wasted effort if we fail to update or have to put the new tuple on a
     * different page.  But we must compute the list before obtaining buffer
     * lock --- in the worst case, if we are doing an update on one of the
     * relevant system catalogs, we could deadlock if we try to fetch the list
     * later.  In any case, the relcache caches the data so this is usually
     * pretty cheap.
     *
     * Note that we get a copy here, so we need not worry about relcache flush
     * happening midway through.
     */
    hot_attrs = RelationGetIndexAttrBitmap(relation, false);
    key_attrs = RelationGetIndexAttrBitmap(relation, true);

    block = ItemPointerGetBlockNumber(otid);
    buffer = ReadBuffer(relation, block);
    page = BufferGetPage(buffer);

    /*
     * Before locking the buffer, pin the visibility map page if it appears to
     * be necessary.  Since we haven't got the lock yet, someone else might be
     * in the middle of changing this, so we'll need to recheck after we have
     * the lock.
     */
    if (PageIsAllVisible(page))
        visibilitymap_pin(relation, block, &vmbuffer);

    LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE);

    lp = PageGetItemId(page, ItemPointerGetOffsetNumber(otid));
    Assert(ItemIdIsNormal(lp));

    /*
     * Fill in enough data in oldtup for HeapSatisfiesHOTandKeyUpdate to work
     * properly.
     */
    oldtup.t_tableOid = RelationGetRelid(relation);
    oldtup.t_data = (HeapTupleHeader) PageGetItem(page, lp);
    oldtup.t_len = ItemIdGetLength(lp);
    oldtup.t_self = *otid;

    /* the new tuple is ready, except for this: */
    newtup->t_tableOid = RelationGetRelid(relation);

    /* Fill in OID for newtup */
    if (relation->rd_rel->relhasoids)
    {
#ifdef NOT_USED
        /* this is redundant with an Assert in HeapTupleSetOid */
        Assert(newtup->t_data->t_infomask & HEAP_HASOID);
#endif
        HeapTupleSetOid(newtup, HeapTupleGetOid(&oldtup));
    }
    else
    {
        /* check there is not space for an OID */
        Assert(!(newtup->t_data->t_infomask & HEAP_HASOID));
    }

    /*
     * If we're not updating any "key" column, we can grab a weaker lock type.
     * This allows for more concurrency when we are running simultaneously with
     * foreign key checks.
     *
     * Note that if a column gets detoasted while executing the update, but the
     * value ends up being the same, this test will fail and we will use the
     * stronger lock.  This is acceptable; the important case to optimize is
     * updates that don't manipulate key columns, not those that
     * serendipitiously arrive at the same key values.
     */
    HeapSatisfiesHOTandKeyUpdate(relation, hot_attrs, key_attrs,
                                 &satisfies_hot, &satisfies_key,
                                 &oldtup, newtup);
    if (satisfies_key)
    {
        *lockmode = LockTupleNoKeyExclusive;
        mxact_status = MultiXactStatusNoKeyUpdate;
        key_intact = true;

        /*
         * If this is the first possibly-multixact-able operation in the
         * current transaction, set my per-backend OldestMemberMXactId setting.
         * We can be certain that the transaction will never become a member of
         * any older MultiXactIds than that.  (We have to do this even if we
         * end up just using our own TransactionId below, since some other
         * backend could incorporate our XID into a MultiXact immediately
         * afterwards.)
         */
        MultiXactIdSetOldestMember();
    }
    else
    {
        *lockmode = LockTupleExclusive;
        mxact_status = MultiXactStatusUpdate;
        key_intact = false;
    }

    /*
     * Note: beyond this point, use oldtup not otid to refer to old tuple.
     * otid may very well point at newtup->t_self, which we will overwrite
     * with the new tuple's location, so there's great risk of confusion if we
     * use otid anymore.
     */

l2:
    checked_lockers = false;
    locker_remains = false;
    result = HeapTupleSatisfiesUpdate(oldtup.t_data, cid, buffer);

    /* see below about the "no wait" case */
    Assert(result != HeapTupleBeingUpdated || wait);

    if (result == HeapTupleInvisible)
    {
        UnlockReleaseBuffer(buffer);
        elog(ERROR, "attempted to update invisible tuple");
    }
    else if (result == HeapTupleBeingUpdated && wait)
    {
        TransactionId   xwait;
        uint16      infomask;
        bool        can_continue = false;

        checked_lockers = true;

        /*
         * XXX note that we don't consider the "no wait" case here.  This
         * isn't a problem currently because no caller uses that case, but it
         * should be fixed if such a caller is introduced.  It wasn't a problem
         * previously because this code would always wait, but now that some
         * tuple locks do not conflict with one of the lock modes we use, it is
         * possible that this case is interesting to handle specially.
         *
         * This may cause failures with third-party code that calls heap_update
         * directly.
         */

        /* must copy state data before unlocking buffer */
        xwait = HeapTupleHeaderGetRawXmax(oldtup.t_data);
        infomask = oldtup.t_data->t_infomask;

        LockBuffer(buffer, BUFFER_LOCK_UNLOCK);

        /*
         * Acquire tuple lock to establish our priority for the tuple (see
         * heap_lock_tuple).  LockTuple will release us when we are
         * next-in-line for the tuple.
         *
         * If we are forced to "start over" below, we keep the tuple lock;
         * this arranges that we stay at the head of the line while rechecking
         * tuple state.
         */
        if (!have_tuple_lock)
        {
            LockTupleTuplock(relation, &(oldtup.t_self), *lockmode);
            have_tuple_lock = true;
        }

        /*
         * Now we have to do something about the existing locker.  If it's a
         * multi, sleep on it; we might be awakened before it is completely
         * gone (or even not sleep at all in some cases); we need to preserve
         * it as locker, unless it is gone completely.
         *
         * If it's not a multi, we need to check for sleeping conditions before
         * actually going to sleep.  If the update doesn't conflict with the
         * locks, we just continue without sleeping (but making sure it is
         * preserved).
         */
        if (infomask & HEAP_XMAX_IS_MULTI)
        {
            TransactionId   update_xact;
            int             remain;

            /* wait for multixact */
            MultiXactIdWait((MultiXactId) xwait, mxact_status, &remain,
                            infomask);
            LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE);

            /*
             * If xwait had just locked the tuple then some other xact could
             * update this tuple before we get to this point.  Check for xmax
             * change, and start over if so.
             */
            if (!(oldtup.t_data->t_infomask & HEAP_XMAX_IS_MULTI) ||
                !TransactionIdEquals(HeapTupleHeaderGetRawXmax(oldtup.t_data),
                                     xwait))
                goto l2;

            /*
             * Note that the multixact may not be done by now.  It could have
             * surviving members; our own xact or other subxacts of this
             * backend, and also any other concurrent transaction that locked
             * the tuple with KeyShare if we only got TupleLockUpdate.  If this
             * is the case, we have to be careful to mark the updated tuple
             * with the surviving members in Xmax.
             *
             * Note that there could have been another update in the MultiXact.
             * In that case, we need to check whether it committed or aborted.
             * If it aborted we are safe to update it again; otherwise there is
             * an update conflict, and we have to return HeapTupleUpdated
             * below.
             *
             * In the LockTupleExclusive case, we still need to preserve the
             * surviving members: those would include the tuple locks we had
             * before this one, which are important to keep in case this
             * subxact aborts.
             */
            update_xact = InvalidTransactionId;
            if (!HEAP_XMAX_IS_LOCKED_ONLY(oldtup.t_data->t_infomask))
                update_xact = HeapTupleGetUpdateXid(oldtup.t_data);

            /* there was no UPDATE in the MultiXact; or it aborted. */
            if (!TransactionIdIsValid(update_xact) ||
                TransactionIdDidAbort(update_xact))
                can_continue = true;

            locker_remains = remain != 0;
        }
        else
        {
            /*
             * If it's just a key-share locker, and we're not changing the
             * key columns, we don't need to wait for it to end; but we
             * need to preserve it as locker.
             */
            if (HEAP_XMAX_IS_KEYSHR_LOCKED(infomask) && key_intact)
            {
                LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE);

                /*
                 * recheck the locker; if someone else changed the tuple while we
                 * weren't looking, start over.
                 */
                if ((oldtup.t_data->t_infomask & HEAP_XMAX_IS_MULTI) ||
                    !TransactionIdEquals(HeapTupleHeaderGetRawXmax(oldtup.t_data),
                                         xwait))
                    goto l2;

                can_continue = true;
                locker_remains = true;
            }
            else
            {
                /* wait for regular transaction to end */
                XactLockTableWait(xwait);
                LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE);

                /*
                 * xwait is done, but if xwait had just locked the tuple then some
                 * other xact could update this tuple before we get to this point.
                 * Check for xmax change, and start over if so.
                 */
                if ((oldtup.t_data->t_infomask & HEAP_XMAX_IS_MULTI) ||
                    !TransactionIdEquals(HeapTupleHeaderGetRawXmax(oldtup.t_data),
                                         xwait))
                    goto l2;

                /* Otherwise check if it committed or aborted */
                UpdateXmaxHintBits(oldtup.t_data, buffer, xwait);
                if (oldtup.t_data->t_infomask & HEAP_XMAX_INVALID)
                    can_continue = true;
            }
        }

        result = can_continue ? HeapTupleMayBeUpdated : HeapTupleUpdated;
    }

    if (crosscheck != InvalidSnapshot && result == HeapTupleMayBeUpdated)
    {
        /* Perform additional check for transaction-snapshot mode RI updates */
        if (!HeapTupleSatisfiesVisibility(&oldtup, crosscheck, buffer))
            result = HeapTupleUpdated;
    }

    if (result != HeapTupleMayBeUpdated)
    {
        Assert(result == HeapTupleSelfUpdated ||
               result == HeapTupleUpdated ||
               result == HeapTupleBeingUpdated);
        Assert(!(oldtup.t_data->t_infomask & HEAP_XMAX_INVALID));
        hufd->ctid = oldtup.t_data->t_ctid;
        hufd->xmax = HeapTupleHeaderGetUpdateXid(oldtup.t_data);
        if (result == HeapTupleSelfUpdated)
            hufd->cmax = HeapTupleHeaderGetCmax(oldtup.t_data);
        else
            hufd->cmax = 0;     /* for lack of an InvalidCommandId value */
        UnlockReleaseBuffer(buffer);
        if (have_tuple_lock)
            UnlockTupleTuplock(relation, &(oldtup.t_self), *lockmode);
        if (vmbuffer != InvalidBuffer)
            ReleaseBuffer(vmbuffer);
        bms_free(hot_attrs);
        bms_free(key_attrs);
        return result;
    }

    /*
     * If we didn't pin the visibility map page and the page has become all
     * visible while we were busy locking the buffer, or during some
     * subsequent window during which we had it unlocked, we'll have to unlock
     * and re-lock, to avoid holding the buffer lock across an I/O.  That's a
     * bit unfortunate, especially since we'll now have to recheck whether
     * the tuple has been locked or updated under us, but hopefully it won't
     * happen very often.
     */
    if (vmbuffer == InvalidBuffer && PageIsAllVisible(page))
    {
        LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
        visibilitymap_pin(relation, block, &vmbuffer);
        LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE);
        goto l2;
    }

    /*
     * We're about to do the actual update -- check for conflict first, to
     * avoid possibly having to roll back work we've just done.
     */
    CheckForSerializableConflictIn(relation, &oldtup, buffer);

    /* Fill in transaction status data */

    /*
     * If the tuple we're updating is locked, we need to preserve the locking
     * info in the old tuple's Xmax.  Prepare a new Xmax value for this.
     */
    compute_new_xmax_infomask(HeapTupleHeaderGetRawXmax(oldtup.t_data),
                              oldtup.t_data->t_infomask,
                              oldtup.t_data->t_infomask2,
                              xid, *lockmode, true,
                              &xmax_old_tuple, &infomask_old_tuple,
                              &infomask2_old_tuple);

    /*
     * And also prepare an Xmax value for the new copy of the tuple.  If there
     * was no xmax previously, or there was one but all lockers are now gone,
     * then use InvalidXid; otherwise, get the xmax from the old tuple.  (In
     * rare cases that might also be InvalidXid and yet not have the
     * HEAP_XMAX_INVALID bit set; that's fine.)
     */
    if ((oldtup.t_data->t_infomask & HEAP_XMAX_INVALID) ||
        (checked_lockers && !locker_remains))
        xmax_new_tuple = InvalidTransactionId;
    else
        xmax_new_tuple = HeapTupleHeaderGetRawXmax(oldtup.t_data);

    if (!TransactionIdIsValid(xmax_new_tuple))
    {
        infomask_new_tuple = HEAP_XMAX_INVALID;
        infomask2_new_tuple = 0;
    }
    else
    {
        /*
         * If we found a valid Xmax for the new tuple, then the infomask bits
         * to use on the new tuple depend on what was there on the old one.
         * Note that since we're doing an update, the only possibility is that
         * the lockers had FOR KEY SHARE lock.
         */
        if (oldtup.t_data->t_infomask & HEAP_XMAX_IS_MULTI)
        {
            GetMultiXactIdHintBits(xmax_new_tuple, &infomask_new_tuple,
                                   &infomask2_new_tuple);
        }
        else
        {
            infomask_new_tuple = HEAP_XMAX_KEYSHR_LOCK | HEAP_XMAX_LOCK_ONLY;
            infomask2_new_tuple = 0;
        }
    }

    /*
     * Prepare the new tuple with the appropriate initial values of Xmin and
     * Xmax, as well as initial infomask bits as computed above.
     */
    newtup->t_data->t_infomask &= ~(HEAP_XACT_MASK);
    newtup->t_data->t_infomask2 &= ~(HEAP2_XACT_MASK);
    HeapTupleHeaderSetXmin(newtup->t_data, xid);
    HeapTupleHeaderSetCmin(newtup->t_data, cid);
    newtup->t_data->t_infomask |= HEAP_UPDATED | infomask_new_tuple;
    newtup->t_data->t_infomask2 |= infomask2_new_tuple;
    HeapTupleHeaderSetXmax(newtup->t_data, xmax_new_tuple);

    /*
     * Replace cid with a combo cid if necessary.  Note that we already put
     * the plain cid into the new tuple.
     */
    HeapTupleHeaderAdjustCmax(oldtup.t_data, &cid, &iscombo);

    /*
     * If the toaster needs to be activated, OR if the new tuple will not fit
     * on the same page as the old, then we need to release the content lock
     * (but not the pin!) on the old tuple's buffer while we are off doing
     * TOAST and/or table-file-extension work.  We must mark the old tuple to
     * show that it's already being updated, else other processes may try to
     * update it themselves.
     *
     * We need to invoke the toaster if there are already any out-of-line
     * toasted values present, or if the new tuple is over-threshold.
     */
    if (relation->rd_rel->relkind != RELKIND_RELATION &&
        relation->rd_rel->relkind != RELKIND_MATVIEW)
    {
        /* toast table entries should never be recursively toasted */
        Assert(!HeapTupleHasExternal(&oldtup));
        Assert(!HeapTupleHasExternal(newtup));
        need_toast = false;
    }
    else
        need_toast = (HeapTupleHasExternal(&oldtup) ||
                      HeapTupleHasExternal(newtup) ||
                      newtup->t_len > TOAST_TUPLE_THRESHOLD);

    pagefree = PageGetHeapFreeSpace(page);

    newtupsize = MAXALIGN(newtup->t_len);

    if (need_toast || newtupsize > pagefree)
    {
        /* Clear obsolete visibility flags ... */
        oldtup.t_data->t_infomask &= ~(HEAP_XMAX_BITS | HEAP_MOVED);
        oldtup.t_data->t_infomask2 &= ~HEAP_KEYS_UPDATED;
        HeapTupleClearHotUpdated(&oldtup);
        /* ... and store info about transaction updating this tuple */
        Assert(TransactionIdIsValid(xmax_old_tuple));
        HeapTupleHeaderSetXmax(oldtup.t_data, xmax_old_tuple);
        oldtup.t_data->t_infomask |= infomask_old_tuple;
        oldtup.t_data->t_infomask2 |= infomask2_old_tuple;
        HeapTupleHeaderSetCmax(oldtup.t_data, cid, iscombo);
        /* temporarily make it look not-updated */
        oldtup.t_data->t_ctid = oldtup.t_self;
        already_marked = true;
        LockBuffer(buffer, BUFFER_LOCK_UNLOCK);

        /*
         * Let the toaster do its thing, if needed.
         *
         * Note: below this point, heaptup is the data we actually intend to
         * store into the relation; newtup is the caller's original untoasted
         * data.
         */
        if (need_toast)
        {
            /* Note we always use WAL and FSM during updates */
            heaptup = toast_insert_or_update(relation, newtup, &oldtup, 0);
            newtupsize = MAXALIGN(heaptup->t_len);
        }
        else
            heaptup = newtup;

        /*
         * Now, do we need a new page for the tuple, or not?  This is a bit
         * tricky since someone else could have added tuples to the page while
         * we weren't looking.  We have to recheck the available space after
         * reacquiring the buffer lock.  But don't bother to do that if the
         * former amount of free space is still not enough; it's unlikely
         * there's more free now than before.
         *
         * What's more, if we need to get a new page, we will need to acquire
         * buffer locks on both old and new pages.  To avoid deadlock against
         * some other backend trying to get the same two locks in the other
         * order, we must be consistent about the order we get the locks in.
         * We use the rule "lock the lower-numbered page of the relation
         * first".  To implement this, we must do RelationGetBufferForTuple
         * while not holding the lock on the old page, and we must rely on it
         * to get the locks on both pages in the correct order.
         */
        if (newtupsize > pagefree)
        {
            /* Assume there's no chance to put heaptup on same page. */
            newbuf = RelationGetBufferForTuple(relation, heaptup->t_len,
                                               buffer, 0, NULL,
                                               &vmbuffer_new, &vmbuffer);
        }
        else
        {
            /* Re-acquire the lock on the old tuple's page. */
            LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE);
            /* Re-check using the up-to-date free space */
            pagefree = PageGetHeapFreeSpace(page);
            if (newtupsize > pagefree)
            {
                /*
                 * Rats, it doesn't fit anymore.  We must now unlock and
                 * relock to avoid deadlock.  Fortunately, this path should
                 * seldom be taken.
                 */
                LockBuffer(buffer, BUFFER_LOCK_UNLOCK);
                newbuf = RelationGetBufferForTuple(relation, heaptup->t_len,
                                                   buffer, 0, NULL,
                                                   &vmbuffer_new, &vmbuffer);
            }
            else
            {
                /* OK, it fits here, so we're done. */
                newbuf = buffer;
            }
        }
    }
    else
    {
        /* No TOAST work needed, and it'll fit on same page */
        already_marked = false;
        newbuf = buffer;
        heaptup = newtup;
    }

    /*
     * We're about to create the new tuple -- check for conflict first, to
     * avoid possibly having to roll back work we've just done.
     *
     * NOTE: For a tuple insert, we only need to check for table locks, since
     * predicate locking at the index level will cover ranges for anything
     * except a table scan.  Therefore, only provide the relation.
     */
    CheckForSerializableConflictIn(relation, NULL, InvalidBuffer);

    /*
     * At this point newbuf and buffer are both pinned and locked, and newbuf
     * has enough space for the new tuple.  If they are the same buffer, only
     * one pin is held.
     */

    if (newbuf == buffer)
    {
        /*
         * Since the new tuple is going into the same page, we might be able
         * to do a HOT update.  Check if any of the index columns have been
         * changed.  If not, then HOT update is possible.
         */
        if (satisfies_hot)
            use_hot_update = true;
    }
    else
    {
        /* Set a hint that the old page could use prune/defrag */
        PageSetFull(page);
    }

    /* NO EREPORT(ERROR) from here till changes are logged */
    START_CRIT_SECTION();

    /*
     * If this transaction commits, the old tuple will become DEAD sooner or
     * later.  Set flag that this page is a candidate for pruning once our xid
     * falls below the OldestXmin horizon.  If the transaction finally aborts,
     * the subsequent page pruning will be a no-op and the hint will be
     * cleared.
     *
     * XXX Should we set hint on newbuf as well?  If the transaction aborts,
     * there would be a prunable tuple in the newbuf; but for now we choose
     * not to optimize for aborts.  Note that heap_xlog_update must be kept in
     * sync if this decision changes.
     */
    PageSetPrunable(page, xid);

    if (use_hot_update)
    {
        /* Mark the old tuple as HOT-updated */
        HeapTupleSetHotUpdated(&oldtup);
        /* And mark the new tuple as heap-only */
        HeapTupleSetHeapOnly(heaptup);
        /* Mark the caller's copy too, in case different from heaptup */
        HeapTupleSetHeapOnly(newtup);
    }
    else
    {
        /* Make sure tuples are correctly marked as not-HOT */
        HeapTupleClearHotUpdated(&oldtup);
        HeapTupleClearHeapOnly(heaptup);
        HeapTupleClearHeapOnly(newtup);
    }

    RelationPutHeapTuple(relation, newbuf, heaptup);    /* insert new tuple */

    if (!already_marked)
    {
        /* Clear obsolete visibility flags ... */
        oldtup.t_data->t_infomask &= ~(HEAP_XMAX_BITS | HEAP_MOVED);
        oldtup.t_data->t_infomask2 &= ~HEAP_KEYS_UPDATED;
        /* ... and store info about transaction updating this tuple */
        Assert(TransactionIdIsValid(xmax_old_tuple));
        HeapTupleHeaderSetXmax(oldtup.t_data, xmax_old_tuple);
        oldtup.t_data->t_infomask |= infomask_old_tuple;
        oldtup.t_data->t_infomask2 |= infomask2_old_tuple;
        HeapTupleHeaderSetCmax(oldtup.t_data, cid, iscombo);
    }

    /* record address of new tuple in t_ctid of old one */
    oldtup.t_data->t_ctid = heaptup->t_self;

    /* clear PD_ALL_VISIBLE flags */
    if (PageIsAllVisible(BufferGetPage(buffer)))
    {
        all_visible_cleared = true;
        PageClearAllVisible(BufferGetPage(buffer));
        visibilitymap_clear(relation, BufferGetBlockNumber(buffer),
                            vmbuffer);
    }
    if (newbuf != buffer && PageIsAllVisible(BufferGetPage(newbuf)))
    {
        all_visible_cleared_new = true;
        PageClearAllVisible(BufferGetPage(newbuf));
        visibilitymap_clear(relation, BufferGetBlockNumber(newbuf),
                            vmbuffer_new);
    }

    if (newbuf != buffer)
        MarkBufferDirty(newbuf);
    MarkBufferDirty(buffer);

    /* XLOG stuff */
    if (RelationNeedsWAL(relation))
    {
        XLogRecPtr  recptr = log_heap_update(relation, buffer,
                                             newbuf, &oldtup, heaptup,
                                             all_visible_cleared,
                                             all_visible_cleared_new);

        if (newbuf != buffer)
        {
            PageSetLSN(BufferGetPage(newbuf), recptr);
        }
        PageSetLSN(BufferGetPage(buffer), recptr);
    }

    END_CRIT_SECTION();

    if (newbuf != buffer)
        LockBuffer(newbuf, BUFFER_LOCK_UNLOCK);
    LockBuffer(buffer, BUFFER_LOCK_UNLOCK);

    /*
     * Mark old tuple for invalidation from system caches at next command
     * boundary, and mark the new tuple for invalidation in case we abort. We
     * have to do this before releasing the buffer because oldtup is in the
     * buffer.  (heaptup is all in local memory, but it's necessary to process
     * both tuple versions in one call to inval.c so we can avoid redundant
     * sinval messages.)
     */
    CacheInvalidateHeapTuple(relation, &oldtup, heaptup);

    /* Now we can release the buffer(s) */
    if (newbuf != buffer)
        ReleaseBuffer(newbuf);
    ReleaseBuffer(buffer);
    if (BufferIsValid(vmbuffer_new))
        ReleaseBuffer(vmbuffer_new);
    if (BufferIsValid(vmbuffer))
        ReleaseBuffer(vmbuffer);

    /*
     * Release the lmgr tuple lock, if we had it.
     */
    if (have_tuple_lock)
        UnlockTupleTuplock(relation, &(oldtup.t_self), *lockmode);

    pgstat_count_heap_update(relation, use_hot_update);

    /*
     * If heaptup is a private copy, release it.  Don't forget to copy t_self
     * back to the caller's image, too.
     */
    if (heaptup != newtup)
    {
        newtup->t_self = heaptup->t_self;
        heap_freetuple(heaptup);
    }

    bms_free(hot_attrs);
    bms_free(key_attrs);

    return HeapTupleMayBeUpdated;
}

void relation_close ( Relation  relation,
LOCKMODE  lockmode 
)

Definition at line 1159 of file heapam.c.

References Assert, LockInfoData::lockRelId, MAX_LOCKMODES, NoLock, RelationData::rd_lockInfo, RelationClose(), and UnlockRelationId().

Referenced by AlterExtensionNamespace(), AlterObjectNamespace_oid(), AlterSeqNamespaces(), AlterSequence(), AlterTableNamespace(), analyze_rel(), ATController(), ATExecChangeOwner(), ATExecSetTableSpace(), ATPostAlterTypeParse(), ATRewriteCatalogs(), ATSimpleRecursion(), ATTypedTableRecursion(), bt_metap(), bt_page_items(), bt_page_stats(), build_row_from_class(), BuildEventTriggerCache(), calculate_indexes_size(), calculate_toast_table_size(), change_owner_recurse_to_sequences(), check_of_type(), CheckAttributeType(), cluster_rel(), CommentObject(), currval_oid(), dblink_build_sql_delete(), dblink_build_sql_insert(), dblink_build_sql_update(), dblink_get_pkey(), DefineRelation(), DefineVirtualRelation(), do_setval(), ExecAlterExtensionContentsStmt(), ExecSecLabelStmt(), ExecuteTruncate(), expandRelation(), find_composite_type_dependencies(), finish_heap_swap(), get_raw_page_internal(), get_rels_with_domain(), get_tables_to_cluster(), heap_drop_with_catalog(), lastval(), load_typcache_tupdesc(), nextval_internal(), pg_freespace(), pg_indexes_size(), pg_relation_is_scannable(), pg_relation_size(), pg_relpages(), pg_sequence_parameters(), pg_table_size(), pg_total_relation_size(), pgstat_heap(), pgstat_index(), pgstatginindex(), pgstatindex(), pltcl_init_load_unknown(), process_owned_by(), relation_is_updatable(), RelationNameGetTupleDesc(), RemoveAttrDefaultById(), RemoveAttributeById(), rename_constraint_internal(), renameatt_internal(), RenameRelationInternal(), RenameRewriteRule(), renametrig(), ResetSequence(), set_relation_column_names(), transformAlterTableStmt(), transformIndexConstraint(), UpdateRangeTableOfViewParse(), and vacuum_rel().

{
    LockRelId   relid = relation->rd_lockInfo.lockRelId;

    Assert(lockmode >= NoLock && lockmode < MAX_LOCKMODES);

    /* The relcache does the real work... */
    RelationClose(relation);

    if (lockmode != NoLock)
        UnlockRelationId(&relid, lockmode);
}

Relation relation_open ( Oid  relationId,
LOCKMODE  lockmode 
)

Definition at line 1013 of file heapam.c.

References Assert, elog, ERROR, LockRelationOid(), MAX_LOCKMODES, MyXactAccessedTempRel, NoLock, pgstat_initstats(), RelationIdGetRelation(), RelationIsValid, and RelationUsesLocalBuffers.

Referenced by AlterObjectNamespace_oid(), AlterSeqNamespaces(), AlterTable(), AlterTableInternal(), AlterTableNamespace(), ATExecChangeOwner(), ATExecSetTableSpace(), ATRewriteCatalogs(), ATSimpleRecursion(), ATTypedTableRecursion(), build_row_from_class(), BuildEventTriggerCache(), calculate_indexes_size(), calculate_toast_table_size(), change_owner_recurse_to_sequences(), check_of_type(), CheckAttributeType(), DefineRelation(), DefineVirtualRelation(), ExecuteTruncate(), expandRelation(), find_composite_type_dependencies(), finish_heap_swap(), get_rels_with_domain(), heap_drop_with_catalog(), heap_open(), index_open(), load_typcache_tupdesc(), open_share_lock(), pg_freespace(), pgstatginindex(), pgstattuplebyid(), relation_openrv(), relation_openrv_extended(), RemoveAttrDefaultById(), RemoveAttributeById(), rename_constraint_internal(), renameatt_internal(), RenameRelationInternal(), RenameRewriteRule(), renametrig(), set_relation_column_names(), and UpdateRangeTableOfViewParse().

{
    Relation    r;

    Assert(lockmode >= NoLock && lockmode < MAX_LOCKMODES);

    /* Get the lock before trying to open the relcache entry */
    if (lockmode != NoLock)
        LockRelationOid(relationId, lockmode);

    /* The relcache does all the real work... */
    r = RelationIdGetRelation(relationId);

    if (!RelationIsValid(r))
        elog(ERROR, "could not open relation with OID %u", relationId);

    /* Make note that we've accessed a temporary relation */
    if (RelationUsesLocalBuffers(r))
        MyXactAccessedTempRel = true;

    pgstat_initstats(r);

    return r;
}

Relation relation_openrv ( const RangeVar relation,
LOCKMODE  lockmode 
)

Definition at line 1091 of file heapam.c.

References AcceptInvalidationMessages(), NoLock, RangeVarGetRelid, and relation_open().

Referenced by ATPostAlterTypeParse(), bt_metap(), bt_page_items(), bt_page_stats(), get_object_address_attribute(), get_raw_page_internal(), heap_openrv(), pg_relpages(), pgstatindex(), pgstattuple(), process_owned_by(), RelationNameGetTupleDesc(), and transformTableLikeClause().

{
    Oid         relOid;

    /*
     * Check for shared-cache-inval messages before trying to open the
     * relation.  This is needed even if we already hold a lock on the
     * relation, because GRANT/REVOKE are executed without taking any lock on
     * the target relation, and we want to be sure we see current ACL
     * information.  We can skip this if asked for NoLock, on the assumption
     * that such a call is not the first one in the current command, and so we
     * should be reasonably up-to-date already.  (XXX this all could stand to
     * be redesigned, but for the moment we'll keep doing this like it's been
     * done historically.)
     */
    if (lockmode != NoLock)
        AcceptInvalidationMessages();

    /* Look up and lock the appropriate relation using namespace search */
    relOid = RangeVarGetRelid(relation, lockmode, false);

    /* Let relation_open do the rest */
    return relation_open(relOid, NoLock);
}

Relation relation_openrv_extended ( const RangeVar relation,
LOCKMODE  lockmode,
bool  missing_ok 
)

Definition at line 1126 of file heapam.c.

References AcceptInvalidationMessages(), NoLock, OidIsValid, RangeVarGetRelid, and relation_open().

Referenced by get_relation_by_qualified_name(), heap_openrv_extended(), pltcl_init_load_unknown(), and transformAlterTableStmt().

{
    Oid         relOid;

    /*
     * Check for shared-cache-inval messages before trying to open the
     * relation.  See comments in relation_openrv().
     */
    if (lockmode != NoLock)
        AcceptInvalidationMessages();

    /* Look up and lock the appropriate relation using namespace search */
    relOid = RangeVarGetRelid(relation, lockmode, missing_ok);

    /* Return NULL on not-found */
    if (!OidIsValid(relOid))
        return NULL;

    /* Let relation_open do the rest */
    return relation_open(relOid, NoLock);
}

void setLastTid ( const ItemPointer  tid  ) 

Definition at line 257 of file tid.c.

Referenced by ExecInsert().

{
    Current_last_tid = *tid;
}

void simple_heap_delete ( Relation  relation,
ItemPointer  tid 
)

Definition at line 2842 of file heapam.c.

References elog, ERROR, GetCurrentCommandId(), heap_delete(), HeapTupleMayBeUpdated, HeapTupleSelfUpdated, HeapTupleUpdated, and InvalidSnapshot.

Referenced by AlterSetting(), ATExecAlterColumnType(), ATExecDropInherit(), changeDependencyFor(), CreateComments(), CreateSharedComments(), DeleteAttributeTuples(), DeleteComments(), deleteDependencyRecordsFor(), deleteDependencyRecordsForClass(), deleteOneObject(), DeleteRelationTuple(), DeleteSecurityLabel(), DeleteSharedComments(), DeleteSharedSecurityLabel(), DeleteSystemAttributeTuples(), DelRoleMems(), drop_parent_dependency(), DropCastById(), DropConfigurationMapping(), dropDatabaseDependencies(), dropdb(), DropProceduralLanguageById(), DropRole(), DropSetting(), DropTableSpace(), EnumValuesDelete(), heap_drop_with_catalog(), index_drop(), inv_truncate(), LargeObjectDrop(), MakeConfigurationMapping(), RangeDelete(), RelationRemoveInheritance(), RemoveAmOpEntryById(), RemoveAmProcEntryById(), RemoveAttrDefaultById(), RemoveAttributeById(), RemoveCollationById(), RemoveConstraintById(), RemoveConversionById(), RemoveDefaultACLById(), RemoveEventTriggerById(), RemoveExtensionById(), RemoveForeignDataWrapperById(), RemoveForeignServerById(), RemoveFunctionById(), RemoveOpClassById(), RemoveOperatorById(), RemoveOpFamilyById(), RemoveRewriteRuleById(), RemoveSchemaById(), RemoveStatistics(), RemoveTriggerById(), RemoveTSConfigurationById(), RemoveTSDictionaryById(), RemoveTSParserById(), RemoveTSTemplateById(), RemoveTypeById(), RemoveUserMappingById(), SetSecurityLabel(), SetSharedSecurityLabel(), shdepChangeDep(), shdepDropDependency(), and toast_delete_datum().

{
    HTSU_Result result;
    HeapUpdateFailureData hufd;

    result = heap_delete(relation, tid,
                         GetCurrentCommandId(true), InvalidSnapshot,
                         true /* wait for commit */,
                         &hufd);
    switch (result)
    {
        case HeapTupleSelfUpdated:
            /* Tuple was already updated in current command? */
            elog(ERROR, "tuple already updated by self");
            break;

        case HeapTupleMayBeUpdated:
            /* done successfully */
            break;

        case HeapTupleUpdated:
            elog(ERROR, "tuple concurrently updated");
            break;

        default:
            elog(ERROR, "unrecognized heap_delete status: %u", result);
            break;
    }
}

Oid simple_heap_insert ( Relation  relation,
HeapTuple  tup 
)
void simple_heap_update ( Relation  relation,
ItemPointer  otid,
HeapTuple  tup 
)

Definition at line 3808 of file heapam.c.

References elog, ERROR, GetCurrentCommandId(), heap_update(), HeapTupleMayBeUpdated, HeapTupleSelfUpdated, HeapTupleUpdated, and InvalidSnapshot.

Referenced by AddRoleMems(), AlterConstraintNamespaces(), AlterDatabase(), AlterDatabaseOwner(), AlterDomainDefault(), AlterDomainNotNull(), AlterDomainValidateConstraint(), AlterEventTrigger(), AlterEventTriggerOwner_internal(), AlterExtensionNamespace(), AlterForeignDataWrapper(), AlterForeignDataWrapperOwner_internal(), AlterForeignServer(), AlterForeignServerOwner_internal(), AlterFunction(), AlterObjectNamespace_internal(), AlterObjectOwner_internal(), AlterObjectRename_internal(), AlterRelationNamespaceInternal(), AlterRole(), AlterSchemaOwner_internal(), AlterSetting(), AlterTableSpaceOptions(), AlterTSDictionary(), AlterTypeNamespaceInternal(), AlterTypeOwner(), AlterTypeOwnerInternal(), AlterUserMapping(), ApplyExtensionUpdates(), ATExecAddColumn(), ATExecAddOf(), ATExecAlterColumnGenericOptions(), ATExecAlterColumnType(), ATExecChangeOwner(), ATExecDropColumn(), ATExecDropConstraint(), ATExecDropInherit(), ATExecDropNotNull(), ATExecDropOf(), ATExecGenericOptions(), ATExecSetNotNull(), ATExecSetOptions(), ATExecSetRelOptions(), ATExecSetStatistics(), ATExecSetStorage(), ATExecSetTableSpace(), ATExecValidateConstraint(), change_owner_fix_column_acls(), changeDependencyFor(), create_proc_lang(), create_toast_table(), CreateComments(), CreateSharedComments(), CreateTrigger(), DefineQueryRewrite(), DelRoleMems(), EnableDisableRule(), EnableDisableTrigger(), ExecGrant_Attribute(), ExecGrant_Database(), ExecGrant_Fdw(), ExecGrant_ForeignServer(), ExecGrant_Function(), ExecGrant_Language(), ExecGrant_Largeobject(), ExecGrant_Namespace(), ExecGrant_Relation(), ExecGrant_Tablespace(), ExecGrant_Type(), extension_config_remove(), index_build(), index_constraint_create(), InsertRule(), inv_truncate(), inv_write(), MakeConfigurationMapping(), mark_index_clustered(), MergeAttributesIntoExisting(), MergeConstraintsIntoExisting(), MergeWithExistingConstraint(), movedb(), OperatorCreate(), OperatorUpd(), pg_extension_config_dump(), ProcedureCreate(), reindex_index(), RelationSetNewRelfilenode(), RemoveAttrDefaultById(), RemoveAttributeById(), RemoveConstraintById(), renameatt_internal(), RenameConstraintById(), RenameDatabase(), RenameRelationInternal(), RenameRewriteRule(), RenameRole(), RenameSchema(), RenameTableSpace(), renametrig(), RenameTypeInternal(), RenumberEnumType(), SetDefaultACL(), SetFunctionArgType(), SetFunctionReturnType(), SetRelationHasSubclass(), SetRelationNumChecks(), SetRelationRuleStatus(), SetSecurityLabel(), SetSharedSecurityLabel(), shdepChangeDep(), StoreAttrDefault(), swap_relation_files(), TypeCreate(), and update_attstats().

{
    HTSU_Result result;
    HeapUpdateFailureData hufd;
    LockTupleMode lockmode;

    result = heap_update(relation, otid, tup,
                         GetCurrentCommandId(true), InvalidSnapshot,
                         true /* wait for commit */,
                         &hufd, &lockmode);
    switch (result)
    {
        case HeapTupleSelfUpdated:
            /* Tuple was already updated in current command? */
            elog(ERROR, "tuple already updated by self");
            break;

        case HeapTupleMayBeUpdated:
            /* done successfully */
            break;

        case HeapTupleUpdated:
            elog(ERROR, "tuple concurrently updated");
            break;

        default:
            elog(ERROR, "unrecognized heap_update status: %u", result);
            break;
    }
}

BlockNumber ss_get_location ( Relation  rel,
BlockNumber  relnblocks 
)

Definition at line 250 of file syncscan.c.

References elog, LOG, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), RelationData::rd_node, RelationGetRelationName, ss_search(), and SyncScanLock.

Referenced by initscan().

{
    BlockNumber startloc;

    LWLockAcquire(SyncScanLock, LW_EXCLUSIVE);
    startloc = ss_search(rel->rd_node, 0, false);
    LWLockRelease(SyncScanLock);

    /*
     * If the location is not a valid block number for this scan, start at 0.
     *
     * This can happen if for instance a VACUUM truncated the table since the
     * location was saved.
     */
    if (startloc >= relnblocks)
        startloc = 0;

#ifdef TRACE_SYNCSCAN
    if (trace_syncscan)
        elog(LOG,
             "SYNC_SCAN: start \"%s\" (size %u) at %u",
             RelationGetRelationName(rel), relnblocks, startloc);
#endif

    return startloc;
}

void ss_report_location ( Relation  rel,
BlockNumber  location 
)

Definition at line 285 of file syncscan.c.

References elog, LOG, LW_EXCLUSIVE, LWLockConditionalAcquire(), LWLockRelease(), RelationData::rd_node, RelationGetRelationName, ss_search(), SYNC_SCAN_REPORT_INTERVAL, and SyncScanLock.

Referenced by heapgettup(), and heapgettup_pagemode().

{
#ifdef TRACE_SYNCSCAN
    if (trace_syncscan)
    {
        if ((location % 1024) == 0)
            elog(LOG,
                 "SYNC_SCAN: scanning \"%s\" at %u",
                 RelationGetRelationName(rel), location);
    }
#endif

    /*
     * To reduce lock contention, only report scan progress every N pages. For
     * the same reason, don't block if the lock isn't immediately available.
     * Missing a few updates isn't critical, it just means that a new scan
     * that wants to join the pack will start a little bit behind the head of
     * the scan.  Hopefully the pages are still in OS cache and the scan
     * catches up quickly.
     */
    if ((location % SYNC_SCAN_REPORT_INTERVAL) == 0)
    {
        if (LWLockConditionalAcquire(SyncScanLock, LW_EXCLUSIVE))
        {
            (void) ss_search(rel->rd_node, location, true);
            LWLockRelease(SyncScanLock);
        }
#ifdef TRACE_SYNCSCAN
        else if (trace_syncscan)
            elog(LOG,
                 "SYNC_SCAN: missed update for \"%s\" at %u",
                 RelationGetRelationName(rel), location);
#endif
    }
}

void SyncScanShmemInit ( void   ) 

Definition at line 132 of file syncscan.c.

References Assert, RelFileNode::dbNode, ss_scan_locations_t::head, i, IsUnderPostmaster, ss_scan_locations_t::items, ss_scan_location_t::location, ss_lru_item_t::location, ss_lru_item_t::next, ss_lru_item_t::prev, ss_scan_location_t::relfilenode, RelFileNode::relNode, ShmemInitStruct(), SizeOfScanLocations, RelFileNode::spcNode, SYNC_SCAN_NELEM, and ss_scan_locations_t::tail.

Referenced by CreateSharedMemoryAndSemaphores().

{
    int         i;
    bool        found;

    scan_locations = (ss_scan_locations_t *)
        ShmemInitStruct("Sync Scan Locations List",
                        SizeOfScanLocations(SYNC_SCAN_NELEM),
                        &found);

    if (!IsUnderPostmaster)
    {
        /* Initialize shared memory area */
        Assert(!found);

        scan_locations->head = &scan_locations->items[0];
        scan_locations->tail = &scan_locations->items[SYNC_SCAN_NELEM - 1];

        for (i = 0; i < SYNC_SCAN_NELEM; i++)
        {
            ss_lru_item_t *item = &scan_locations->items[i];

            /*
             * Initialize all slots with invalid values. As scans are started,
             * these invalid entries will fall off the LRU list and get
             * replaced with real entries.
             */
            item->location.relfilenode.spcNode = InvalidOid;
            item->location.relfilenode.dbNode = InvalidOid;
            item->location.relfilenode.relNode = InvalidOid;
            item->location.location = InvalidBlockNumber;

            item->prev = (i > 0) ?
                (&scan_locations->items[i - 1]) : NULL;
            item->next = (i < SYNC_SCAN_NELEM - 1) ?
                (&scan_locations->items[i + 1]) : NULL;
        }
    }
    else
        Assert(found);
}

Size SyncScanShmemSize ( void   ) 

Definition at line 123 of file syncscan.c.

References SizeOfScanLocations, and SYNC_SCAN_NELEM.

Referenced by CreateSharedMemoryAndSemaphores().

Relation try_relation_open ( Oid  relationId,
LOCKMODE  lockmode 
)

Definition at line 1046 of file heapam.c.

References Assert, elog, ERROR, LockRelationOid(), MAX_LOCKMODES, MyXactAccessedTempRel, NoLock, ObjectIdGetDatum, pgstat_initstats(), RelationIdGetRelation(), RelationIsValid, RelationUsesLocalBuffers, RELOID, SearchSysCacheExists1, and UnlockRelationOid().

Referenced by analyze_rel(), cluster_rel(), pg_indexes_size(), pg_relation_is_scannable(), pg_relation_size(), pg_table_size(), pg_total_relation_size(), relation_is_updatable(), and vacuum_rel().

{
    Relation    r;

    Assert(lockmode >= NoLock && lockmode < MAX_LOCKMODES);

    /* Get the lock first */
    if (lockmode != NoLock)
        LockRelationOid(relationId, lockmode);

    /*
     * Now that we have the lock, probe to see if the relation really exists
     * or not.
     */
    if (!SearchSysCacheExists1(RELOID, ObjectIdGetDatum(relationId)))
    {
        /* Release useless lock */
        if (lockmode != NoLock)
            UnlockRelationOid(relationId, lockmode);

        return NULL;
    }

    /* Should be safe to do a relcache load */
    r = RelationIdGetRelation(relationId);

    if (!RelationIsValid(r))
        elog(ERROR, "could not open relation with OID %u", relationId);

    /* Make note that we've accessed a temporary relation */
    if (RelationUsesLocalBuffers(r))
        MyXactAccessedTempRel = true;

    pgstat_initstats(r);

    return r;
}