#include "postgres.h"
|
|
#include "access/htup_details.h"
|
#include "access/stratnum.h"
|
#include "access/tempcat.h"
|
#include "access/valid.h"
|
#include "access/xact.h"
|
#include "catalog/catalog.h"
|
#include "catalog/pg_class.h"
|
#include "catalog/pg_collation_d.h"
|
#include "lib/ilist.h"
|
#include "lib/rbtree.h"
|
#include "miscadmin.h"
|
#include "utils/datum.h"
|
#include "utils/inval.h"
|
#include "utils/lsyscache.h"
|
#include "utils/memutils.h"
|
#include "utils/typcache.h"
|
|
/* [PG_CUSTOM: TEMP_CATALOG] PG19 GA reserves 0xfffc for versioned datums;
|
* the synthetic TID must use its own offset outside normal heap pages. */
|
#define TEMP_CATALOG_OFFSET_NUMBER ((OffsetNumber) 0xfffb)
|
|
StaticAssertDecl(TEMP_CATALOG_OFFSET_NUMBER > MaxOffsetNumber,
|
"temp catalog offset marker must be outside the heap offset range");
|
StaticAssertDecl(TEMP_CATALOG_OFFSET_NUMBER != SpecTokenOffsetNumber,
|
"temp catalog offset marker must not collide with speculative insertion tokens");
|
StaticAssertDecl(TEMP_CATALOG_OFFSET_NUMBER != MovedPartitionsOffsetNumber,
|
"temp catalog offset marker must not collide with moved-partitions tokens");
|
StaticAssertDecl(TEMP_CATALOG_OFFSET_NUMBER != VersionedDatumOffsetNumber,
|
"temp catalog offset marker must not collide with versioned datums");
|
|
typedef enum TempCatalogUndoOp
|
{
|
TEMP_CATALOG_UNDO_INSERT,
|
TEMP_CATALOG_UNDO_UPDATE,
|
TEMP_CATALOG_UNDO_DELETE
|
} TempCatalogUndoOp;
|
|
typedef struct TempCatalogRelation TempCatalogRelation;
|
typedef struct TempCatalogTuple TempCatalogTuple;
|
typedef struct TempCatalogUndoRecord TempCatalogUndoRecord;
|
|
struct TempCatalogRelation
|
{
|
dlist_node node;
|
Oid catalog_relid;
|
dlist_head tuples;
|
dlist_head indexes;
|
};
|
|
struct TempCatalogTuple
|
{
|
TempCatalogRelation *relation;
|
HeapTuple tuple;
|
ItemPointerData tid;
|
dlist_node node;
|
bool visible;
|
};
|
|
/* Keep the map entry for a deleted tuple until the journal disposes of it. */
|
typedef struct MapItem
|
{
|
RBTNode node;
|
ItemPointerData tid;
|
TempCatalogTuple *tuple;
|
} MapItem;
|
|
/* RBTree deletion copies payloads, so duplicate buckets must not live in nodes. */
|
typedef struct TempCatalogIndexKey
|
{
|
AttrNumber attnum;
|
Oid type;
|
Oid collation;
|
int16 typlen;
|
bool typbyval;
|
Oid opfamily;
|
FmgrInfo cmp;
|
} TempCatalogIndexKey;
|
|
typedef struct IndexItem
|
{
|
dlist_node node;
|
MemoryContext context;
|
RBTree *tree;
|
int nkeys;
|
TempCatalogIndexKey keys[FLEXIBLE_ARRAY_MEMBER];
|
} IndexItem;
|
|
typedef struct TempCatalogBucket
|
{
|
dlist_head members;
|
Datum values[FLEXIBLE_ARRAY_MEMBER];
|
} TempCatalogBucket;
|
|
typedef struct TreeItem
|
{
|
RBTNode node;
|
const Datum *values;
|
TempCatalogBucket *bucket;
|
} TreeItem;
|
|
typedef struct TempCatalogMember
|
{
|
dlist_node node;
|
TempCatalogBucket *bucket;
|
TempCatalogTuple *tuple;
|
} TempCatalogMember;
|
|
struct TempCatalogUndoRecord
|
{
|
dlist_node node;
|
TempCatalogUndoOp op;
|
SubTransactionId owner_subid;
|
TempCatalogRelation *relation;
|
TempCatalogTuple *tuple;
|
HeapTuple old_tuple;
|
};
|
|
struct TempCatalogScan
|
{
|
MemoryContext context;
|
int ntuples;
|
int next_tuple;
|
HeapTuple tuples[FLEXIBLE_ARRAY_MEMBER];
|
};
|
|
static MemoryContext TempCatalogContext = NULL;
|
static dlist_head temp_catalog_relations = DLIST_STATIC_INIT(temp_catalog_relations);
|
static dlist_head temp_catalog_journal = DLIST_STATIC_INIT(temp_catalog_journal);
|
static RBTree *temp_catalog_tid_map = NULL;
|
static bool temp_catalog_index_busy = false;
|
static uint64 temp_catalog_next_tid = 1;
|
static uint32 temp_catalog_write_scope_depth = 0;
|
static bool temp_catalog_callbacks_registered = false;
|
|
static void temp_catalog_xact_callback(XactEvent event, void *arg);
|
static void temp_catalog_subxact_callback(SubXactEvent event,
|
SubTransactionId mySubid,
|
SubTransactionId parentSubid,
|
void *arg);
|
static void temp_catalog_require_catalog_relation(Relation relation);
|
static void temp_catalog_require_transaction_state(void);
|
static TempCatalogRelation *temp_catalog_lookup_relation(Oid catalog_relid);
|
static TempCatalogRelation *temp_catalog_get_relation(Relation relation, bool create);
|
static TempCatalogTuple *temp_catalog_find_tuple(TempCatalogRelation *relation,
|
const ItemPointerData *tid);
|
static void temp_catalog_map_add(TempCatalogTuple *tuple);
|
static void temp_catalog_discard_indexes(TempCatalogRelation *relation);
|
static void temp_catalog_change_indexes(Relation relation,
|
TempCatalogTuple *tuple,
|
HeapTuple old_tuple, bool add);
|
static TempCatalogScan *temp_catalog_index_scan(Relation relation,
|
TempCatalogRelation *temp_relation,
|
int nkeys, ScanKey keys,
|
MemoryContext scan_context);
|
static void temp_catalog_map_remove(TempCatalogTuple *tuple);
|
static bool temp_catalog_relation_has_journal_entry(TempCatalogRelation *relation);
|
static void temp_catalog_prune_relation_if_empty(TempCatalogRelation *relation);
|
static ItemPointerData temp_catalog_allocate_tid(void);
|
static HeapTuple temp_catalog_copy_tuple(Relation relation,
|
HeapTuple tuple,
|
const ItemPointerData *tid);
|
static TempCatalogTuple *temp_catalog_create_tuple(TempCatalogRelation *relation,
|
HeapTuple tuple,
|
const ItemPointerData *tid);
|
static TempCatalogUndoRecord *temp_catalog_create_undo_record(TempCatalogUndoOp op,
|
TempCatalogRelation *relation,
|
TempCatalogTuple *tuple,
|
HeapTuple old_tuple);
|
static void temp_catalog_check_inplace_tuple_shape(HeapTuple old_tuple,
|
HeapTuple new_tuple);
|
static void temp_catalog_publish_insert(TempCatalogTuple *tuple,
|
TempCatalogUndoRecord *record);
|
static void temp_catalog_undo_record(TempCatalogUndoRecord *record);
|
static void temp_catalog_commit_record(TempCatalogUndoRecord *record);
|
static void temp_catalog_abort_records(SubTransactionId owner_subid, bool top_level);
|
static void temp_catalog_commit_subxact(SubTransactionId mySubid,
|
SubTransactionId parentSubid);
|
static void temp_catalog_commit_top_level(void);
|
static void temp_catalog_abort_top_level(void);
|
|
bool enable_temp_memory_catalog = false;
|
|
void
|
temp_catalog_init(void)
|
{
|
if (TempCatalogContext == NULL)
|
TempCatalogContext = AllocSetContextCreate(TopMemoryContext,
|
"TempCatalogContext",
|
ALLOCSET_DEFAULT_SIZES);
|
|
if (!temp_catalog_callbacks_registered)
|
{
|
RegisterXactCallback(temp_catalog_xact_callback, NULL);
|
RegisterSubXactCallback(temp_catalog_subxact_callback, NULL);
|
temp_catalog_callbacks_registered = true;
|
}
|
}
|
|
bool
|
temp_catalog_tid_is_memory(const ItemPointerData *tid)
|
{
|
if (tid == NULL || !ItemPointerIsValid(tid))
|
return false;
|
|
return ItemPointerGetOffsetNumberNoCheck(tid) == TEMP_CATALOG_OFFSET_NUMBER &&
|
ItemPointerGetBlockNumberNoCheck(tid) != InvalidBlockNumber;
|
}
|
|
void
|
temp_catalog_begin_write_scope(void)
|
{
|
temp_catalog_require_transaction_state();
|
|
if (temp_catalog_write_scope_depth == UINT32_MAX)
|
ereport(ERROR,
|
(errmsg_internal("temporary catalog write scope depth overflow")));
|
|
temp_catalog_write_scope_depth++;
|
}
|
|
void
|
temp_catalog_end_write_scope(void)
|
{
|
Assert(temp_catalog_write_scope_depth > 0);
|
if (temp_catalog_write_scope_depth == 0)
|
ereport(ERROR,
|
(errmsg_internal("temporary catalog write scope end without begin")));
|
|
temp_catalog_write_scope_depth--;
|
}
|
|
bool
|
temp_catalog_write_scope_active(void)
|
{
|
return temp_catalog_write_scope_depth != 0;
|
}
|
|
void
|
temp_catalog_insert(Relation relation, HeapTuple tuple)
|
{
|
TempCatalogRelation *temp_relation;
|
ItemPointerData synthetic_tid;
|
HeapTuple stored_tuple = NULL;
|
MemoryContext oldcxt = CurrentMemoryContext;
|
TempCatalogTuple *volatile temp_tuple = NULL;
|
TempCatalogUndoRecord *undo_record = NULL;
|
|
temp_catalog_require_catalog_relation(relation);
|
temp_catalog_require_transaction_state();
|
temp_catalog_init();
|
|
temp_relation = temp_catalog_get_relation(relation, true);
|
synthetic_tid = temp_catalog_allocate_tid();
|
stored_tuple = temp_catalog_copy_tuple(relation, tuple, &synthetic_tid);
|
PG_TRY();
|
{
|
temp_tuple = temp_catalog_create_tuple(temp_relation, stored_tuple,
|
&synthetic_tid);
|
undo_record = temp_catalog_create_undo_record(TEMP_CATALOG_UNDO_INSERT,
|
temp_relation,
|
temp_tuple,
|
NULL);
|
temp_catalog_map_add(temp_tuple);
|
}
|
PG_CATCH();
|
{
|
MemoryContextSwitchTo(oldcxt);
|
if (temp_tuple != NULL)
|
pfree(temp_tuple);
|
if (stored_tuple != NULL)
|
heap_freetuple(stored_tuple);
|
PG_RE_THROW();
|
}
|
PG_END_TRY();
|
|
temp_catalog_publish_insert(temp_tuple, undo_record);
|
temp_catalog_change_indexes(relation, temp_tuple, NULL, true);
|
tuple->t_self = synthetic_tid;
|
tuple->t_data->t_ctid = synthetic_tid;
|
CacheInvalidateHeapTuple(relation, temp_tuple->tuple, NULL);
|
}
|
|
void
|
temp_catalog_update(Relation relation, const ItemPointerData *tid, HeapTuple tuple)
|
{
|
TempCatalogRelation *temp_relation;
|
TempCatalogTuple *temp_tuple;
|
HeapTuple old_tuple;
|
HeapTuple new_tuple = NULL;
|
TempCatalogUndoRecord *undo_record = NULL;
|
|
temp_catalog_require_catalog_relation(relation);
|
temp_catalog_require_transaction_state();
|
temp_catalog_init();
|
|
temp_relation = temp_catalog_get_relation(relation, false);
|
temp_tuple = temp_catalog_find_tuple(temp_relation, tid);
|
if (temp_tuple == NULL)
|
ereport(ERROR,
|
(errmsg_internal("temporary catalog tuple not found for relation %u",
|
RelationGetRelid(relation))));
|
|
old_tuple = temp_tuple->tuple;
|
new_tuple = temp_catalog_copy_tuple(relation, tuple, &temp_tuple->tid);
|
PG_TRY();
|
{
|
undo_record = temp_catalog_create_undo_record(TEMP_CATALOG_UNDO_UPDATE,
|
temp_relation,
|
temp_tuple,
|
old_tuple);
|
}
|
PG_CATCH();
|
{
|
if (new_tuple != NULL)
|
heap_freetuple(new_tuple);
|
PG_RE_THROW();
|
}
|
PG_END_TRY();
|
|
temp_tuple->tuple = new_tuple;
|
dlist_push_tail(&temp_catalog_journal, &undo_record->node);
|
temp_catalog_change_indexes(relation, temp_tuple, old_tuple, true);
|
tuple->t_self = temp_tuple->tid;
|
tuple->t_data->t_ctid = temp_tuple->tid;
|
CacheInvalidateHeapTuple(relation, old_tuple, new_tuple);
|
}
|
|
void
|
temp_catalog_update_inplace(Relation relation, const ItemPointerData *tid,
|
HeapTuple tuple)
|
{
|
TempCatalogRelation *temp_relation;
|
TempCatalogTuple *temp_tuple;
|
HeapTuple old_tuple;
|
HeapTuple published_tuple;
|
HeapTuple volatile replacement_tuple = NULL;
|
bool volatile inplace_inval_prepared = false;
|
|
temp_catalog_require_catalog_relation(relation);
|
temp_catalog_require_transaction_state();
|
temp_catalog_init();
|
|
if (!temp_catalog_tid_is_memory(tid))
|
ereport(ERROR,
|
(errmsg_internal("temporary catalog inplace update requires a synthetic TID")));
|
|
temp_relation = temp_catalog_get_relation(relation, false);
|
temp_tuple = temp_catalog_find_tuple(temp_relation, tid);
|
if (temp_tuple == NULL)
|
ereport(ERROR,
|
(errmsg_internal("temporary catalog tuple not found for relation %u",
|
RelationGetRelid(relation))));
|
|
old_tuple = temp_tuple->tuple;
|
replacement_tuple = temp_catalog_copy_tuple(relation, tuple, &temp_tuple->tid);
|
|
PG_TRY();
|
{
|
temp_catalog_check_inplace_tuple_shape(old_tuple, replacement_tuple);
|
CacheInvalidateHeapTupleInplace(relation, old_tuple);
|
inplace_inval_prepared = true;
|
PreInplace_Inval();
|
/* The critical publication section must not compare or allocate keys. */
|
temp_catalog_discard_indexes(temp_relation);
|
|
/*
|
* Match PostgreSQL's physical inplace path: once publication starts,
|
* finish invalidation without raising ERROR.
|
*/
|
START_CRIT_SECTION();
|
temp_tuple->tuple = replacement_tuple;
|
replacement_tuple = NULL;
|
AtInplace_Inval();
|
END_CRIT_SECTION();
|
}
|
PG_CATCH();
|
{
|
if (inplace_inval_prepared)
|
ForgetInplace_Inval();
|
if (replacement_tuple != NULL)
|
heap_freetuple(replacement_tuple);
|
PG_RE_THROW();
|
}
|
PG_END_TRY();
|
|
published_tuple = temp_tuple->tuple;
|
tuple->t_self = temp_tuple->tid;
|
tuple->t_data->t_ctid = temp_tuple->tid;
|
heap_freetuple(old_tuple);
|
|
AcceptInvalidationMessages();
|
|
if (!IsBootstrapProcessingMode())
|
CacheInvalidateHeapTuple(relation, published_tuple, NULL);
|
}
|
|
void
|
temp_catalog_delete(Relation relation, const ItemPointerData *tid)
|
{
|
TempCatalogRelation *temp_relation;
|
TempCatalogTuple *temp_tuple;
|
TempCatalogUndoRecord *undo_record;
|
|
temp_catalog_require_catalog_relation(relation);
|
temp_catalog_require_transaction_state();
|
temp_catalog_init();
|
|
temp_relation = temp_catalog_get_relation(relation, false);
|
temp_tuple = temp_catalog_find_tuple(temp_relation, tid);
|
if (temp_tuple == NULL)
|
ereport(ERROR,
|
(errmsg_internal("temporary catalog tuple not found for relation %u",
|
RelationGetRelid(relation))));
|
|
undo_record = temp_catalog_create_undo_record(TEMP_CATALOG_UNDO_DELETE,
|
temp_relation,
|
temp_tuple,
|
NULL);
|
|
dlist_delete_thoroughly(&temp_tuple->node);
|
temp_tuple->visible = false;
|
dlist_push_tail(&temp_catalog_journal, &undo_record->node);
|
temp_catalog_change_indexes(relation, temp_tuple, temp_tuple->tuple, false);
|
CacheInvalidateHeapTuple(relation, temp_tuple->tuple, NULL);
|
}
|
|
TempCatalogScan *
|
temp_catalog_beginscan(Relation relation, int nkeys, ScanKey keys)
|
{
|
MemoryContext oldcxt;
|
MemoryContext scan_context;
|
TempCatalogRelation *temp_relation;
|
TempCatalogScan *scan;
|
int nmatches = 0;
|
int match_index = 0;
|
dlist_iter iter;
|
Size size;
|
|
temp_catalog_init();
|
temp_catalog_require_catalog_relation(relation);
|
|
scan_context = AllocSetContextCreate(CurrentMemoryContext,
|
"TempCatalogScanContext",
|
ALLOCSET_DEFAULT_SIZES);
|
temp_relation = temp_catalog_get_relation(relation, false);
|
|
if (temp_relation != NULL && !temp_catalog_index_busy)
|
{
|
scan = temp_catalog_index_scan(relation, temp_relation, nkeys, keys,
|
scan_context);
|
if (scan != NULL)
|
return scan;
|
}
|
|
if (temp_relation != NULL)
|
{
|
dlist_foreach(iter, &temp_relation->tuples)
|
{
|
TempCatalogTuple *temp_tuple;
|
|
temp_tuple = dlist_container(TempCatalogTuple, node, iter.cur);
|
if (HeapKeyTest(temp_tuple->tuple, RelationGetDescr(relation), nkeys,
|
keys))
|
nmatches++;
|
}
|
}
|
|
size = offsetof(TempCatalogScan, tuples) + sizeof(HeapTuple) * nmatches;
|
|
oldcxt = MemoryContextSwitchTo(scan_context);
|
scan = palloc0(size);
|
scan->context = scan_context;
|
scan->ntuples = nmatches;
|
|
if (temp_relation != NULL)
|
{
|
dlist_foreach(iter, &temp_relation->tuples)
|
{
|
TempCatalogTuple *temp_tuple;
|
|
temp_tuple = dlist_container(TempCatalogTuple, node, iter.cur);
|
if (!HeapKeyTest(temp_tuple->tuple, RelationGetDescr(relation), nkeys,
|
keys))
|
continue;
|
|
scan->tuples[match_index++] = heap_copytuple(temp_tuple->tuple);
|
}
|
}
|
|
MemoryContextSwitchTo(oldcxt);
|
|
return scan;
|
}
|
|
HeapTuple
|
temp_catalog_getnext(TempCatalogScan *scan)
|
{
|
if (scan == NULL || scan->next_tuple >= scan->ntuples)
|
return NULL;
|
|
return scan->tuples[scan->next_tuple++];
|
}
|
|
void
|
temp_catalog_endscan(TempCatalogScan *scan)
|
{
|
if (scan == NULL)
|
return;
|
|
MemoryContextDelete(scan->context);
|
}
|
|
bool
|
temp_catalog_relation_has_tuples(Oid catalog_relid)
|
{
|
TempCatalogRelation *relation;
|
|
if (TempCatalogContext == NULL)
|
return false;
|
|
relation = temp_catalog_lookup_relation(catalog_relid);
|
|
return relation != NULL && !dlist_is_empty(&relation->tuples);
|
}
|
|
bool
|
temp_catalog_tuple_exists(Relation relation, const ItemPointerData *tid)
|
{
|
TempCatalogRelation *temp_relation;
|
|
if (TempCatalogContext == NULL)
|
return false;
|
|
temp_catalog_require_catalog_relation(relation);
|
|
temp_relation = temp_catalog_get_relation(relation, false);
|
return temp_catalog_find_tuple(temp_relation, tid) != NULL;
|
}
|
|
bool
|
temp_catalog_object_is_memory(Oid relid)
|
{
|
TempCatalogRelation *pg_class_relation;
|
dlist_iter iter;
|
|
if (TempCatalogContext == NULL)
|
return false;
|
|
pg_class_relation = temp_catalog_lookup_relation(RelationRelationId);
|
if (pg_class_relation == NULL)
|
return false;
|
|
dlist_foreach(iter, &pg_class_relation->tuples)
|
{
|
TempCatalogTuple *temp_tuple;
|
Form_pg_class class_form;
|
|
temp_tuple = dlist_container(TempCatalogTuple, node, iter.cur);
|
class_form = (Form_pg_class) GETSTRUCT(temp_tuple->tuple);
|
|
if (class_form->oid == relid)
|
return true;
|
}
|
|
return false;
|
}
|
|
static void
|
temp_catalog_xact_callback(XactEvent event, void *arg)
|
{
|
switch (event)
|
{
|
case XACT_EVENT_COMMIT:
|
case XACT_EVENT_PARALLEL_COMMIT:
|
temp_catalog_commit_top_level();
|
break;
|
case XACT_EVENT_ABORT:
|
case XACT_EVENT_PARALLEL_ABORT:
|
temp_catalog_abort_top_level();
|
break;
|
default:
|
break;
|
}
|
}
|
|
static void
|
temp_catalog_subxact_callback(SubXactEvent event, SubTransactionId mySubid,
|
SubTransactionId parentSubid, void *arg)
|
{
|
switch (event)
|
{
|
case SUBXACT_EVENT_COMMIT_SUB:
|
temp_catalog_commit_subxact(mySubid, parentSubid);
|
break;
|
case SUBXACT_EVENT_ABORT_SUB:
|
temp_catalog_abort_records(mySubid, false);
|
break;
|
default:
|
break;
|
}
|
}
|
|
static void
|
temp_catalog_require_catalog_relation(Relation relation)
|
{
|
if (!IsCatalogRelation(relation))
|
ereport(ERROR,
|
(errmsg_internal("temporary catalog storage requires a catalog relation")));
|
}
|
|
static void
|
temp_catalog_require_transaction_state(void)
|
{
|
if (!IsTransactionState())
|
ereport(ERROR,
|
(errmsg_internal("temporary catalog mutation requires an active transaction")));
|
}
|
|
static TempCatalogRelation *
|
temp_catalog_lookup_relation(Oid catalog_relid)
|
{
|
dlist_iter iter;
|
|
dlist_foreach(iter, &temp_catalog_relations)
|
{
|
TempCatalogRelation *relation;
|
|
relation = dlist_container(TempCatalogRelation, node, iter.cur);
|
if (relation->catalog_relid == catalog_relid)
|
return relation;
|
}
|
|
return NULL;
|
}
|
|
static TempCatalogRelation *
|
temp_catalog_get_relation(Relation relation, bool create)
|
{
|
TempCatalogRelation *temp_relation;
|
MemoryContext oldcxt;
|
|
temp_relation = temp_catalog_lookup_relation(RelationGetRelid(relation));
|
if (temp_relation != NULL || !create)
|
return temp_relation;
|
|
oldcxt = MemoryContextSwitchTo(TempCatalogContext);
|
|
temp_relation = palloc0(sizeof(TempCatalogRelation));
|
temp_relation->catalog_relid = RelationGetRelid(relation);
|
dlist_init(&temp_relation->tuples);
|
dlist_init(&temp_relation->indexes);
|
dlist_push_tail(&temp_catalog_relations, &temp_relation->node);
|
|
MemoryContextSwitchTo(oldcxt);
|
|
return temp_relation;
|
}
|
|
static int
|
temp_catalog_map_compare(const RBTNode *a, const RBTNode *b, void *arg)
|
{
|
return ItemPointerCompare(&((const MapItem *) a)->tid,
|
&((const MapItem *) b)->tid);
|
}
|
|
static void
|
temp_catalog_map_combine(RBTNode *existing, const RBTNode *newdata, void *arg)
|
{
|
elog(ERROR, "duplicate temporary catalog TID");
|
}
|
|
static RBTNode *
|
temp_catalog_map_alloc(void *arg)
|
{
|
return MemoryContextAlloc(TempCatalogContext, sizeof(MapItem));
|
}
|
|
static void
|
temp_catalog_rbt_free(RBTNode *node, void *arg)
|
{
|
pfree(node);
|
}
|
|
static MapItem *
|
temp_catalog_map_lookup(const ItemPointerData *tid)
|
{
|
MapItem key;
|
|
if (temp_catalog_tid_map == NULL || !temp_catalog_tid_is_memory(tid))
|
return NULL;
|
key.tid = *tid;
|
return (MapItem *) rbt_find(temp_catalog_tid_map, &key.node);
|
}
|
|
static void
|
temp_catalog_map_add(TempCatalogTuple *tuple)
|
{
|
MapItem item;
|
bool isnew;
|
|
if (temp_catalog_tid_map == NULL)
|
{
|
MemoryContext oldcxt = MemoryContextSwitchTo(TempCatalogContext);
|
|
temp_catalog_tid_map = rbt_create(sizeof(MapItem),
|
temp_catalog_map_compare,
|
temp_catalog_map_combine,
|
temp_catalog_map_alloc,
|
temp_catalog_rbt_free, NULL);
|
MemoryContextSwitchTo(oldcxt);
|
}
|
item.tid = tuple->tid;
|
item.tuple = tuple;
|
rbt_insert(temp_catalog_tid_map, &item.node, &isnew);
|
Assert(isnew);
|
}
|
|
/* No allocation or catalog access in the undo and commit paths. */
|
static void
|
temp_catalog_map_remove(TempCatalogTuple *tuple)
|
{
|
MapItem *item = temp_catalog_map_lookup(&tuple->tid);
|
|
Assert(item != NULL && item->tuple == tuple);
|
if (item != NULL && item->tuple == tuple)
|
rbt_delete(temp_catalog_tid_map, &item->node);
|
}
|
|
static TempCatalogTuple *
|
temp_catalog_find_tuple(TempCatalogRelation *relation, const ItemPointerData *tid)
|
{
|
MapItem *item;
|
|
if (relation == NULL)
|
return NULL;
|
item = temp_catalog_map_lookup(tid);
|
if (item == NULL || item->tuple->relation != relation ||
|
!item->tuple->visible || !ItemPointerEquals(&item->tuple->tid, tid))
|
return NULL;
|
return item->tuple;
|
}
|
|
/*
|
* RBTree compares a stable key tuple with the search tuple. Node payloads
|
* contain only pointers; duplicate buckets are never embedded in RBTNode.
|
*/
|
static int
|
temp_catalog_index_compare(const RBTNode *a, const RBTNode *b, void *arg)
|
{
|
IndexItem *index = (IndexItem *) arg;
|
const TreeItem *left = (const TreeItem *) a;
|
const TreeItem *right = (const TreeItem *) b;
|
int i;
|
|
for (i = 0; i < index->nkeys; i++)
|
{
|
TempCatalogIndexKey *key = &index->keys[i];
|
int32 cmp = DatumGetInt32(FunctionCall2Coll(&key->cmp,
|
key->collation,
|
left->values[i],
|
right->values[i]));
|
|
if (cmp != 0)
|
return cmp;
|
}
|
return 0;
|
}
|
|
static void
|
temp_catalog_index_combine(RBTNode *existing, const RBTNode *data, void *arg)
|
{
|
/* All duplicates are appended directly to their stable bucket. */
|
elog(ERROR, "unexpected duplicate temporary catalog RBTree insertion");
|
}
|
|
static RBTNode *
|
temp_catalog_index_alloc(void *arg)
|
{
|
return palloc(sizeof(TreeItem));
|
}
|
|
static void
|
temp_catalog_discard_indexes(TempCatalogRelation *relation)
|
{
|
dlist_mutable_iter iter;
|
|
if (relation == NULL)
|
return;
|
dlist_foreach_modify(iter, &relation->indexes)
|
{
|
IndexItem *index = dlist_container(IndexItem, node, iter.cur);
|
|
dlist_delete(&index->node);
|
MemoryContextDelete(index->context);
|
}
|
}
|
|
/* NULLs cannot satisfy the normal HeapKeyTest contract. */
|
static bool
|
temp_catalog_index_values(IndexItem *index, Relation relation,
|
HeapTuple tuple, Datum *values)
|
{
|
int i;
|
|
for (i = 0; i < index->nkeys; i++)
|
{
|
bool isnull;
|
|
values[i] = heap_getattr(tuple, index->keys[i].attnum,
|
RelationGetDescr(relation), &isnull);
|
if (isnull)
|
return false;
|
}
|
return true;
|
}
|
|
static void
|
temp_catalog_bucket_free(IndexItem *index, TempCatalogBucket *bucket)
|
{
|
int i;
|
|
for (i = 0; i < index->nkeys; i++)
|
if (!index->keys[i].typbyval)
|
pfree(DatumGetPointer(bucket->values[i]));
|
pfree(bucket);
|
}
|
|
static void
|
temp_catalog_index_add(IndexItem *index, Relation relation,
|
TempCatalogTuple *tuple)
|
{
|
Datum values[INDEX_MAX_KEYS];
|
TreeItem search;
|
TreeItem *found;
|
TempCatalogBucket *bucket;
|
TempCatalogMember *member;
|
MemoryContext oldcxt;
|
int i;
|
|
if (!temp_catalog_index_values(index, relation, tuple->tuple, values))
|
return;
|
|
oldcxt = MemoryContextSwitchTo(index->context);
|
search.values = values;
|
found = (TreeItem *) rbt_find(index->tree, &search.node);
|
if (found != NULL)
|
bucket = found->bucket;
|
else
|
{
|
TreeItem insert;
|
bool isnew;
|
|
bucket = palloc0(offsetof(TempCatalogBucket, values) +
|
sizeof(Datum) * index->nkeys);
|
dlist_init(&bucket->members);
|
for (i = 0; i < index->nkeys; i++)
|
bucket->values[i] = datumCopy(values[i],
|
index->keys[i].typbyval,
|
index->keys[i].typlen);
|
insert.values = bucket->values;
|
insert.bucket = bucket;
|
found = (TreeItem *) rbt_insert(index->tree, &insert.node, &isnew);
|
Assert(isnew && found->bucket == bucket);
|
}
|
member = palloc(sizeof(TempCatalogMember));
|
member->bucket = bucket;
|
member->tuple = tuple;
|
dlist_push_tail(&bucket->members, &member->node);
|
MemoryContextSwitchTo(oldcxt);
|
}
|
|
static void
|
temp_catalog_index_remove(IndexItem *index, Relation relation,
|
TempCatalogTuple *tuple, HeapTuple old_tuple)
|
{
|
Datum values[INDEX_MAX_KEYS];
|
TreeItem search;
|
TreeItem *found;
|
dlist_mutable_iter iter;
|
MemoryContext oldcxt;
|
|
if (!temp_catalog_index_values(index, relation, old_tuple, values))
|
return;
|
oldcxt = MemoryContextSwitchTo(index->context);
|
search.values = values;
|
found = (TreeItem *) rbt_find(index->tree, &search.node);
|
Assert(found != NULL);
|
if (found == NULL)
|
elog(ERROR, "temporary catalog RBTree entry missing");
|
|
dlist_foreach_modify(iter, &found->bucket->members)
|
{
|
TempCatalogMember *member =
|
dlist_container(TempCatalogMember, node, iter.cur);
|
|
if (member->tuple == tuple)
|
{
|
TempCatalogBucket *bucket = member->bucket;
|
|
dlist_delete(&member->node);
|
pfree(member);
|
if (dlist_is_empty(&bucket->members))
|
{
|
/*
|
* rbt_delete can copy a successor's payload into this node.
|
* It never moves the stable bucket or its members.
|
*/
|
rbt_delete(index->tree, &found->node);
|
temp_catalog_bucket_free(index, bucket);
|
}
|
MemoryContextSwitchTo(oldcxt);
|
return;
|
}
|
}
|
elog(ERROR, "temporary catalog RBTree duplicate member missing");
|
}
|
|
/*
|
* We can only use the indexed path when the scan operator is exactly the
|
* same B-tree family's operator used by the ordering comparator. Otherwise
|
* the original HeapKeyTest list scan remains authoritative.
|
*/
|
static bool
|
temp_catalog_scan_keys(Relation relation, int nkeys, ScanKey keys,
|
TempCatalogIndexKey *wanted)
|
{
|
TupleDesc desc = RelationGetDescr(relation);
|
int i;
|
|
if (nkeys <= 0 || nkeys > INDEX_MAX_KEYS || keys == NULL)
|
return false;
|
for (i = 0; i < nkeys; i++)
|
{
|
ScanKey key = &keys[i];
|
Form_pg_attribute attr;
|
TypeCacheEntry *type;
|
Oid op;
|
|
if (key->sk_attno <= 0 || key->sk_attno > desc->natts ||
|
key->sk_flags != 0 ||
|
(key->sk_strategy != BTEqualStrategyNumber &&
|
(i != nkeys - 1 ||
|
(key->sk_strategy != BTLessStrategyNumber &&
|
key->sk_strategy != BTLessEqualStrategyNumber &&
|
key->sk_strategy != BTGreaterStrategyNumber &&
|
key->sk_strategy != BTGreaterEqualStrategyNumber))))
|
return false;
|
attr = TupleDescAttr(desc, key->sk_attno - 1);
|
if (attr->attisdropped ||
|
(OidIsValid(key->sk_subtype) &&
|
key->sk_subtype != attr->atttypid) ||
|
!OidIsValid(key->sk_func.fn_oid))
|
return false;
|
type = lookup_type_cache(attr->atttypid,
|
TYPECACHE_BTREE_OPFAMILY |
|
TYPECACHE_CMP_PROC_FINFO);
|
if (!OidIsValid(type->btree_opf) ||
|
!OidIsValid(type->cmp_proc_finfo.fn_oid))
|
return false;
|
op = get_opfamily_member(type->btree_opf, attr->atttypid,
|
attr->atttypid, key->sk_strategy);
|
if (!OidIsValid(op) || get_opcode(op) != key->sk_func.fn_oid)
|
return false;
|
wanted[i].attnum = key->sk_attno;
|
wanted[i].type = attr->atttypid;
|
wanted[i].collation = key->sk_collation;
|
wanted[i].typlen = attr->attlen;
|
wanted[i].typbyval = attr->attbyval;
|
wanted[i].opfamily = type->btree_opf;
|
wanted[i].cmp = type->cmp_proc_finfo;
|
}
|
return true;
|
}
|
|
static bool
|
temp_catalog_index_matches(IndexItem *index, int nkeys,
|
TempCatalogIndexKey *wanted)
|
{
|
int i;
|
|
if (index->nkeys != nkeys)
|
return false;
|
for (i = 0; i < nkeys; i++)
|
{
|
TempCatalogIndexKey *have = &index->keys[i];
|
TempCatalogIndexKey *want = &wanted[i];
|
|
if (have->attnum != want->attnum || have->type != want->type ||
|
have->collation != want->collation ||
|
have->opfamily != want->opfamily ||
|
have->cmp.fn_oid != want->cmp.fn_oid)
|
return false;
|
}
|
return true;
|
}
|
|
static IndexItem *
|
temp_catalog_build_index(Relation relation, TempCatalogRelation *temp_relation,
|
int nkeys, TempCatalogIndexKey *wanted)
|
{
|
MemoryContext oldcxt = CurrentMemoryContext;
|
MemoryContext context;
|
IndexItem *index = NULL;
|
dlist_iter iter;
|
int i;
|
|
context = AllocSetContextCreate(TempCatalogContext,
|
"TempCatalogIndexContext",
|
ALLOCSET_DEFAULT_SIZES);
|
PG_TRY();
|
{
|
MemoryContextSwitchTo(context);
|
index = palloc0(offsetof(IndexItem, keys) +
|
sizeof(TempCatalogIndexKey) * nkeys);
|
index->context = context;
|
index->nkeys = nkeys;
|
for (i = 0; i < nkeys; i++)
|
{
|
index->keys[i] = wanted[i];
|
fmgr_info_copy(&index->keys[i].cmp, &wanted[i].cmp, context);
|
}
|
index->tree = rbt_create(sizeof(TreeItem),
|
temp_catalog_index_compare,
|
temp_catalog_index_combine,
|
temp_catalog_index_alloc,
|
temp_catalog_rbt_free, index);
|
dlist_foreach(iter, &temp_relation->tuples)
|
{
|
TempCatalogTuple *tuple =
|
dlist_container(TempCatalogTuple, node, iter.cur);
|
|
temp_catalog_index_add(index, relation, tuple);
|
}
|
MemoryContextSwitchTo(oldcxt);
|
}
|
PG_CATCH();
|
{
|
MemoryContextSwitchTo(oldcxt);
|
MemoryContextDelete(context);
|
PG_RE_THROW();
|
}
|
PG_END_TRY();
|
|
/* Publish only a fully built derived index. */
|
dlist_push_tail(&temp_relation->indexes, &index->node);
|
return index;
|
}
|
|
/*
|
* The list and undo journal remain authoritative. On any comparator or
|
* allocation ERROR, discard all derived indexes before propagating ERROR.
|
* Undo never invokes comparators, and the next ordinary scan builds lazily.
|
*/
|
static void
|
temp_catalog_change_indexes(Relation relation, TempCatalogTuple *tuple,
|
HeapTuple old_tuple, bool add)
|
{
|
TempCatalogRelation *temp_relation = tuple->relation;
|
dlist_iter iter;
|
MemoryContext oldcxt = CurrentMemoryContext;
|
|
if (dlist_is_empty(&temp_relation->indexes))
|
return;
|
if (temp_catalog_index_busy)
|
{
|
/* Reentrant mutation invalidates any already published derived keys. */
|
temp_catalog_discard_indexes(temp_relation);
|
elog(ERROR, "recursive temporary catalog index mutation");
|
}
|
PG_TRY();
|
{
|
temp_catalog_index_busy = true;
|
dlist_foreach(iter, &temp_relation->indexes)
|
{
|
IndexItem *index =
|
dlist_container(IndexItem, node, iter.cur);
|
int i;
|
|
for (i = 0; i < index->nkeys; i++)
|
{
|
Form_pg_attribute attr = TupleDescAttr(RelationGetDescr(relation),
|
index->keys[i].attnum - 1);
|
|
if (attr->attisdropped || attr->atttypid != index->keys[i].type)
|
{
|
temp_catalog_discard_indexes(temp_relation);
|
goto finished;
|
}
|
}
|
if (old_tuple != NULL)
|
temp_catalog_index_remove(index, relation, tuple, old_tuple);
|
if (add && tuple->visible)
|
temp_catalog_index_add(index, relation, tuple);
|
}
|
finished:
|
temp_catalog_index_busy = false;
|
}
|
PG_CATCH();
|
{
|
MemoryContextSwitchTo(oldcxt);
|
temp_catalog_index_busy = false;
|
temp_catalog_discard_indexes(temp_relation);
|
PG_RE_THROW();
|
}
|
PG_END_TRY();
|
MemoryContextSwitchTo(oldcxt);
|
}
|
|
/*
|
* Range scans use public rbt_find_less/great calls to jump from one matching
|
* key to the next. We never walk the tree from its root on every matching
|
* tuple and never retain a tree iterator across catalog mutation.
|
*/
|
static TempCatalogScan *
|
temp_catalog_index_scan(Relation relation, TempCatalogRelation *temp_relation,
|
int nkeys, ScanKey keys, MemoryContext scan_context)
|
{
|
TempCatalogIndexKey wanted[INDEX_MAX_KEYS];
|
IndexItem *index = NULL;
|
dlist_iter iter;
|
TempCatalogScan *scan = NULL;
|
MemoryContext oldcxt = CurrentMemoryContext;
|
int capacity = 16;
|
int i;
|
|
if (nkeys <= 0 || nkeys > INDEX_MAX_KEYS || keys == NULL)
|
return NULL;
|
|
PG_TRY();
|
{
|
temp_catalog_index_busy = true;
|
if (temp_catalog_scan_keys(relation, nkeys, keys, wanted))
|
{
|
Datum values[INDEX_MAX_KEYS];
|
TreeItem search;
|
TreeItem *found;
|
StrategyNumber strategy = keys[nkeys - 1].sk_strategy;
|
bool exact = (strategy == BTEqualStrategyNumber);
|
bool reverse = (strategy == BTLessStrategyNumber ||
|
strategy == BTLessEqualStrategyNumber);
|
bool inclusive = (strategy == BTLessEqualStrategyNumber ||
|
strategy == BTGreaterEqualStrategyNumber);
|
|
dlist_foreach(iter, &temp_relation->indexes)
|
{
|
IndexItem *existing =
|
dlist_container(IndexItem, node, iter.cur);
|
|
if (temp_catalog_index_matches(existing, nkeys, wanted))
|
{
|
index = existing;
|
break;
|
}
|
}
|
if (index == NULL)
|
index = temp_catalog_build_index(relation, temp_relation,
|
nkeys, wanted);
|
for (i = 0; i < nkeys; i++)
|
values[i] = keys[i].sk_argument;
|
search.values = values;
|
|
MemoryContextSwitchTo(scan_context);
|
scan = palloc0(offsetof(TempCatalogScan, tuples) +
|
sizeof(HeapTuple) * capacity);
|
scan->context = scan_context;
|
|
if (exact)
|
found = (TreeItem *) rbt_find(index->tree, &search.node);
|
else if (reverse)
|
found = (TreeItem *) rbt_find_less(index->tree, &search.node,
|
inclusive);
|
else
|
found = (TreeItem *) rbt_find_great(index->tree, &search.node,
|
inclusive);
|
while (found != NULL)
|
{
|
TempCatalogBucket *bucket = found->bucket;
|
dlist_iter member_iter;
|
bool prefix_ok = true;
|
|
for (i = 0; i < nkeys - 1; i++)
|
{
|
TempCatalogIndexKey *key = &index->keys[i];
|
|
if (DatumGetInt32(FunctionCall2Coll(&key->cmp,
|
key->collation,
|
bucket->values[i],
|
values[i])) != 0)
|
{
|
prefix_ok = false;
|
break;
|
}
|
}
|
if (!prefix_ok)
|
break;
|
|
dlist_foreach(member_iter, &bucket->members)
|
{
|
TempCatalogMember *member =
|
dlist_container(TempCatalogMember, node,
|
member_iter.cur);
|
|
/* Preserve exactly the original catalog scan predicate. */
|
if (!HeapKeyTest(member->tuple->tuple,
|
RelationGetDescr(relation), nkeys, keys))
|
continue;
|
if (scan->ntuples == capacity)
|
{
|
if (capacity > (MaxAllocSize -
|
offsetof(TempCatalogScan, tuples)) /
|
(sizeof(HeapTuple) * 2))
|
elog(ERROR, "temporary catalog scan too large");
|
capacity *= 2;
|
scan = repalloc(scan,
|
offsetof(TempCatalogScan, tuples) +
|
sizeof(HeapTuple) * capacity);
|
}
|
scan->tuples[scan->ntuples++] =
|
heap_copytuple(member->tuple->tuple);
|
}
|
if (exact)
|
break;
|
|
/* The current bucket remains stable until the next search. */
|
search.values = bucket->values;
|
if (reverse)
|
found = (TreeItem *) rbt_find_less(index->tree,
|
&search.node, false);
|
else
|
found = (TreeItem *) rbt_find_great(index->tree,
|
&search.node, false);
|
}
|
}
|
MemoryContextSwitchTo(oldcxt);
|
temp_catalog_index_busy = false;
|
}
|
PG_CATCH();
|
{
|
MemoryContextSwitchTo(oldcxt);
|
temp_catalog_index_busy = false;
|
temp_catalog_discard_indexes(temp_relation);
|
PG_RE_THROW();
|
}
|
PG_END_TRY();
|
return scan;
|
}
|
|
static bool
|
temp_catalog_relation_has_journal_entry(TempCatalogRelation *relation)
|
{
|
dlist_iter iter;
|
|
dlist_foreach(iter, &temp_catalog_journal)
|
{
|
TempCatalogUndoRecord *record;
|
|
record = dlist_container(TempCatalogUndoRecord, node, iter.cur);
|
if (record->relation == relation)
|
return true;
|
}
|
|
return false;
|
}
|
|
static void
|
temp_catalog_prune_relation_if_empty(TempCatalogRelation *relation)
|
{
|
if (relation == NULL)
|
return;
|
|
if (!dlist_is_empty(&relation->tuples))
|
return;
|
|
if (temp_catalog_relation_has_journal_entry(relation))
|
return;
|
|
temp_catalog_discard_indexes(relation);
|
dlist_delete_thoroughly(&relation->node);
|
pfree(relation);
|
}
|
|
static ItemPointerData
|
temp_catalog_allocate_tid(void)
|
{
|
uint64 max_tids;
|
uint64 local_counter;
|
uint64 blockno;
|
ItemPointerData tid;
|
|
max_tids = (uint64) InvalidBlockNumber;
|
|
if (temp_catalog_next_tid == 0 ||
|
temp_catalog_next_tid > max_tids)
|
ereport(ERROR,
|
(errmsg_internal("temporary catalog synthetic TID space exhausted")));
|
|
local_counter = temp_catalog_next_tid++;
|
blockno = local_counter - 1;
|
|
if (blockno >= InvalidBlockNumber)
|
ereport(ERROR,
|
(errmsg_internal("temporary catalog synthetic TID space exhausted")));
|
|
ItemPointerSet(&tid, (BlockNumber) blockno, TEMP_CATALOG_OFFSET_NUMBER);
|
Assert(ItemPointerIsValid(&tid));
|
Assert(temp_catalog_tid_is_memory(&tid));
|
|
return tid;
|
}
|
|
static HeapTuple
|
temp_catalog_copy_tuple(Relation relation, HeapTuple tuple,
|
const ItemPointerData *tid)
|
{
|
HeapTuple copy;
|
MemoryContext oldcxt;
|
|
oldcxt = MemoryContextSwitchTo(TempCatalogContext);
|
copy = heap_copytuple(tuple);
|
MemoryContextSwitchTo(oldcxt);
|
|
/* [PG_CUSTOM: TEMP_CATALOG] Preserve physical heap-tuple semantics:
|
* CatalogTupleInsert bypasses heap_prepare_insert(), which clears
|
* HEAP_IS_DATUM before a tuple becomes part of a system catalog. */
|
if (HeapTupleHeaderIsDatum(copy->t_data))
|
HeapTupleHeaderClearDatum(copy->t_data);
|
|
copy->t_self = *tid;
|
copy->t_tableOid = RelationGetRelid(relation);
|
copy->t_data->t_ctid = *tid;
|
|
return copy;
|
}
|
|
static TempCatalogTuple *
|
temp_catalog_create_tuple(TempCatalogRelation *relation, HeapTuple tuple,
|
const ItemPointerData *tid)
|
{
|
TempCatalogTuple *temp_tuple;
|
MemoryContext oldcxt;
|
|
oldcxt = MemoryContextSwitchTo(TempCatalogContext);
|
temp_tuple = palloc0(sizeof(TempCatalogTuple));
|
MemoryContextSwitchTo(oldcxt);
|
|
temp_tuple->relation = relation;
|
temp_tuple->tuple = tuple;
|
temp_tuple->tid = *tid;
|
temp_tuple->visible = false;
|
dlist_node_init(&temp_tuple->node);
|
|
return temp_tuple;
|
}
|
|
static TempCatalogUndoRecord *
|
temp_catalog_create_undo_record(TempCatalogUndoOp op, TempCatalogRelation *relation,
|
TempCatalogTuple *tuple, HeapTuple old_tuple)
|
{
|
TempCatalogUndoRecord *record;
|
MemoryContext oldcxt;
|
|
oldcxt = MemoryContextSwitchTo(TopTransactionContext);
|
record = palloc0(sizeof(TempCatalogUndoRecord));
|
MemoryContextSwitchTo(oldcxt);
|
|
record->op = op;
|
record->owner_subid = GetCurrentSubTransactionId();
|
record->relation = relation;
|
record->tuple = tuple;
|
record->old_tuple = old_tuple;
|
|
return record;
|
}
|
|
static void
|
temp_catalog_check_inplace_tuple_shape(HeapTuple old_tuple, HeapTuple new_tuple)
|
{
|
uint32 oldlen;
|
uint32 newlen;
|
|
oldlen = old_tuple->t_len - old_tuple->t_data->t_hoff;
|
newlen = new_tuple->t_len - new_tuple->t_data->t_hoff;
|
if (oldlen != newlen ||
|
old_tuple->t_data->t_hoff != new_tuple->t_data->t_hoff)
|
elog(ERROR, "wrong tuple length");
|
}
|
|
static void
|
temp_catalog_publish_insert(TempCatalogTuple *tuple, TempCatalogUndoRecord *record)
|
{
|
dlist_push_tail(&tuple->relation->tuples, &tuple->node);
|
tuple->visible = true;
|
dlist_push_tail(&temp_catalog_journal, &record->node);
|
}
|
|
static void
|
temp_catalog_undo_record(TempCatalogUndoRecord *record)
|
{
|
TempCatalogTuple *temp_tuple = record->tuple;
|
|
switch (record->op)
|
{
|
case TEMP_CATALOG_UNDO_INSERT:
|
if (temp_tuple->visible)
|
{
|
dlist_delete_thoroughly(&temp_tuple->node);
|
temp_tuple->visible = false;
|
}
|
temp_catalog_map_remove(temp_tuple);
|
heap_freetuple(temp_tuple->tuple);
|
pfree(temp_tuple);
|
break;
|
case TEMP_CATALOG_UNDO_UPDATE:
|
heap_freetuple(temp_tuple->tuple);
|
temp_tuple->tuple = record->old_tuple;
|
temp_tuple->tuple->t_self = temp_tuple->tid;
|
temp_tuple->tuple->t_tableOid = temp_tuple->relation->catalog_relid;
|
temp_tuple->tuple->t_data->t_ctid = temp_tuple->tid;
|
break;
|
case TEMP_CATALOG_UNDO_DELETE:
|
Assert(!temp_tuple->visible);
|
dlist_push_tail(&temp_tuple->relation->tuples, &temp_tuple->node);
|
temp_tuple->visible = true;
|
break;
|
}
|
}
|
|
static void
|
temp_catalog_commit_record(TempCatalogUndoRecord *record)
|
{
|
TempCatalogTuple *temp_tuple = record->tuple;
|
|
switch (record->op)
|
{
|
case TEMP_CATALOG_UNDO_INSERT:
|
break;
|
case TEMP_CATALOG_UNDO_UPDATE:
|
heap_freetuple(record->old_tuple);
|
break;
|
case TEMP_CATALOG_UNDO_DELETE:
|
temp_catalog_map_remove(temp_tuple);
|
heap_freetuple(temp_tuple->tuple);
|
pfree(temp_tuple);
|
break;
|
}
|
}
|
|
static void
|
temp_catalog_abort_records(SubTransactionId owner_subid, bool top_level)
|
{
|
dlist_node *cur;
|
dlist_node *prev;
|
|
for (cur = temp_catalog_journal.head.prev;
|
cur != &temp_catalog_journal.head;
|
cur = prev)
|
{
|
TempCatalogUndoRecord *record;
|
TempCatalogRelation *relation;
|
|
prev = cur->prev;
|
record = dlist_container(TempCatalogUndoRecord, node, cur);
|
if (!top_level && record->owner_subid != owner_subid)
|
continue;
|
|
relation = record->relation;
|
temp_catalog_discard_indexes(relation);
|
temp_catalog_undo_record(record);
|
dlist_delete_thoroughly(&record->node);
|
temp_catalog_prune_relation_if_empty(relation);
|
pfree(record);
|
}
|
|
if (top_level)
|
dlist_init(&temp_catalog_journal);
|
}
|
|
static void
|
temp_catalog_commit_subxact(SubTransactionId mySubid, SubTransactionId parentSubid)
|
{
|
dlist_iter iter;
|
|
dlist_foreach(iter, &temp_catalog_journal)
|
{
|
TempCatalogUndoRecord *record;
|
|
record = dlist_container(TempCatalogUndoRecord, node, iter.cur);
|
if (record->owner_subid == mySubid)
|
record->owner_subid = parentSubid;
|
}
|
}
|
|
static void
|
temp_catalog_commit_top_level(void)
|
{
|
dlist_iter iter;
|
dlist_mutable_iter reliter;
|
|
dlist_foreach(iter, &temp_catalog_journal)
|
{
|
TempCatalogUndoRecord *record;
|
|
record = dlist_container(TempCatalogUndoRecord, node, iter.cur);
|
temp_catalog_commit_record(record);
|
}
|
|
dlist_init(&temp_catalog_journal);
|
|
dlist_foreach_modify(reliter, &temp_catalog_relations)
|
{
|
TempCatalogRelation *relation;
|
|
relation = dlist_container(TempCatalogRelation, node, reliter.cur);
|
temp_catalog_prune_relation_if_empty(relation);
|
}
|
}
|
|
static void
|
temp_catalog_abort_top_level(void)
|
{
|
dlist_mutable_iter reliter;
|
|
temp_catalog_abort_records(InvalidSubTransactionId, true);
|
|
dlist_foreach_modify(reliter, &temp_catalog_relations)
|
{
|
TempCatalogRelation *relation;
|
|
relation = dlist_container(TempCatalogRelation, node, reliter.cur);
|
temp_catalog_prune_relation_if_empty(relation);
|
}
|
}
|